Advertisement
1980geeksquad

Normal mapping for Codea

Feb 19th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. -- normal maping
  2.  
  3. function setup()
  4. m = mesh()
  5. m.shader = shader("Documents:NormalMaping")
  6. m.shader.Resolution = vec2(1.0,1.0)
  7. m.shader.LightPos = vec3(0,0,0.075)
  8. m.shader.LightColor = vec4(0.94,0.77,0.17,2.0)
  9. m.shader.AmbientColor = vec4(0.6,0.6,1.0,0.5)
  10. m.shader.Falloff = vec3(0.4,2.0,10.0)
  11. m.shader.s_multitex = "Dropbox:bricks_indented_texture_9181161_NRM" --rgb normal map
  12. m.shader.tex = "Dropbox:bricks_indented_texture_9181161_COLOR" --color map
  13. m:addRect(300,300,1000,1000)
  14. tchx=0
  15. tchy=0
  16. end
  17.  
  18.  
  19. function draw()
  20. m.shader.LightPos = vec3(tchx/WIDTH,tchy/HEIGHT,0.075)
  21. m:draw()
  22. end
  23.  
  24. function touched(touch)
  25. if touch.state == BEGAN
  26. or touch.state == MOVING then
  27. tchx=touch.x+50
  28. tchy=touch.y+50
  29. end
  30. end
  31.  
  32.  
  33.  
  34. [[[[[[[[[[[[Shader]]]]]]]]]]]]]]]
  35.  
  36. --------vertex shader---------
  37.  
  38. attribute vec4 position;
  39. attribute vec4 color;
  40. attribute vec2 texCoord;
  41.  
  42. varying vec2 vTexCoord;
  43. varying vec4 vColor;
  44.  
  45. uniform sampler2D s_multitex;
  46.  
  47. uniform mat4 modelViewProjection;
  48.  
  49. void main()
  50. {
  51. vec4 object_space_pos = vec4(position.x,position.y,position.z,1.0);
  52. gl_Position = modelViewProjection * position;
  53.  
  54. vColor = color;
  55. vTexCoord = texCoord;
  56. }
  57.  
  58.  
  59.  
  60. ---------Fragment shader------------
  61.  
  62. //Default precision qualifier
  63. precision highp float;
  64.  
  65. varying vec2 vTexCoord;
  66. varying vec4 vColor;
  67.  
  68. uniform sampler2D tex;
  69.  
  70. uniform lowp sampler2D s_multitex;
  71.  
  72. uniform vec2 Resolution;
  73. uniform vec3 LightPos;
  74. uniform vec4 LightColor;
  75. uniform vec4 AmbientColor;
  76. uniform vec3 Falloff;
  77.  
  78. void main()
  79. {
  80. vec4 DiffuseColor = texture2D(tex,vTexCoord);
  81. vec3 NormalMap = texture2D(s_multitex,vTexCoord).rgb;
  82. NormalMap.g = 1.0 - NormalMap.g;
  83. vec3 LightDir = vec3(LightPos.xy-(vTexCoord.xy/Resolution.xy),LightPos.z);
  84. LightDir.x *= Resolution.x/Resolution.y;
  85. float D = length(LightDir);
  86. vec3 N = normalize(NormalMap*2.0-1.0);
  87. vec3 L = normalize(LightDir);
  88. vec3 Diffuse = (LightColor.rgb * LightColor.a) * max(dot(N,L), 0.0);
  89. vec3 Ambient = AmbientColor.rgb * AmbientColor.a;
  90. float Attenuation = 1.0 / (Falloff.x + (Falloff.y*D) + (Falloff.z*D*D));
  91. vec3 Intensity = Ambient + Diffuse * Attenuation;
  92. vec3 FinalColor = DiffuseColor.rgb * Intensity;
  93. gl_FragColor = vColor * vec4(FinalColor,DiffuseColor.a);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement