Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- normal maping
- function setup()
- m = mesh()
- m.shader = shader("Documents:NormalMaping")
- m.shader.Resolution = vec2(1.0,1.0)
- m.shader.LightPos = vec3(0,0,0.075)
- m.shader.LightColor = vec4(0.94,0.77,0.17,2.0)
- m.shader.AmbientColor = vec4(0.6,0.6,1.0,0.5)
- m.shader.Falloff = vec3(0.4,2.0,10.0)
- m.shader.s_multitex = "Dropbox:bricks_indented_texture_9181161_NRM" --rgb normal map
- m.shader.tex = "Dropbox:bricks_indented_texture_9181161_COLOR" --color map
- m:addRect(300,300,1000,1000)
- tchx=0
- tchy=0
- end
- function draw()
- m.shader.LightPos = vec3(tchx/WIDTH,tchy/HEIGHT,0.075)
- m:draw()
- end
- function touched(touch)
- if touch.state == BEGAN
- or touch.state == MOVING then
- tchx=touch.x+50
- tchy=touch.y+50
- end
- end
- [[[[[[[[[[[[Shader]]]]]]]]]]]]]]]
- --------vertex shader---------
- attribute vec4 position;
- attribute vec4 color;
- attribute vec2 texCoord;
- varying vec2 vTexCoord;
- varying vec4 vColor;
- uniform sampler2D s_multitex;
- uniform mat4 modelViewProjection;
- void main()
- {
- vec4 object_space_pos = vec4(position.x,position.y,position.z,1.0);
- gl_Position = modelViewProjection * position;
- vColor = color;
- vTexCoord = texCoord;
- }
- ---------Fragment shader------------
- //Default precision qualifier
- precision highp float;
- varying vec2 vTexCoord;
- varying vec4 vColor;
- uniform sampler2D tex;
- uniform lowp sampler2D s_multitex;
- uniform vec2 Resolution;
- uniform vec3 LightPos;
- uniform vec4 LightColor;
- uniform vec4 AmbientColor;
- uniform vec3 Falloff;
- void main()
- {
- vec4 DiffuseColor = texture2D(tex,vTexCoord);
- vec3 NormalMap = texture2D(s_multitex,vTexCoord).rgb;
- NormalMap.g = 1.0 - NormalMap.g;
- vec3 LightDir = vec3(LightPos.xy-(vTexCoord.xy/Resolution.xy),LightPos.z);
- LightDir.x *= Resolution.x/Resolution.y;
- float D = length(LightDir);
- vec3 N = normalize(NormalMap*2.0-1.0);
- vec3 L = normalize(LightDir);
- vec3 Diffuse = (LightColor.rgb * LightColor.a) * max(dot(N,L), 0.0);
- vec3 Ambient = AmbientColor.rgb * AmbientColor.a;
- float Attenuation = 1.0 / (Falloff.x + (Falloff.y*D) + (Falloff.z*D*D));
- vec3 Intensity = Ambient + Diffuse * Attenuation;
- vec3 FinalColor = DiffuseColor.rgb * Intensity;
- gl_FragColor = vColor * vec4(FinalColor,DiffuseColor.a);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement