Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This is the default Sprite Lamp shader. It is not optimised for speed but rather, readability,
- //so certain things wouldn't make sense in a game environment (for instance you'd probably
- //want to pack things into fewer textures).
- #version 120
- uniform sampler2D normalMap;
- uniform sampler2D diffuseMap;
- uniform sampler2D specularMap; //Optionally contains glossiness in the alpha channel
- uniform vec3 lightPosition;
- uniform vec3 directLightColour;
- uniform vec3 ambientLightColour; //Upper ambient colour
- uniform vec2 textureResolution; //Used for per texel lighting
- uniform float lightWrap;
- uniform float attenuationMultiplier;
- uniform mat2 rotationMatrix; //Simple case of 2D rotation - not suitable for general game engine use
- void main()
- {
- //This value will be the tex coords at the centre of the nearest texel.
- vec2 centredTexCoords = gl_TexCoord[0].xy;
- centredTexCoords *= textureResolution.xy;
- centredTexCoords = floor(centredTexCoords.xy) + vec2(0.5, 0.5);
- centredTexCoords /= textureResolution.xy;
- //All the texture lookups done at the start where possible.
- vec4 normalColour = texture2D(normalMap, gl_TexCoord[0].xy);
- vec4 diffuseColour = texture2D(diffuseMap, gl_TexCoord[0].xy);
- vec4 specularColour = texture2D(specularMap, gl_TexCoord[0].xy);
- if (diffuseColour.a <= 0.1)
- {
- discard;
- }
- vec3 fragPos;
- fragPos.xy = gl_TexCoord[0].xy;
- //This sets the fragment position at the centre of the nearest texel,
- //for per-texel lighting. This unfortunately isn't very reusable in a general game engine sense.
- fragPos.xy *= textureResolution.xy;
- fragPos.xy = floor(fragPos.xy);
- fragPos.xy /= textureResolution.xy;
- fragPos.y = 1.0 - fragPos.y;
- fragPos = fragPos + vec3(-0.5, -0.5, 0.0);
- fragPos.x *= (textureResolution.x / textureResolution.y);
- fragPos.z = 1;
- vec3 normal = (normalColour.rgb - 0.5) * 2.0;
- normal.y *= normalFlip;
- normal.xy *= rotationMatrix;
- vec3 lightVec = lightPosition - fragPos;
- lightVec.xy *= rotationMatrix;
- float lightDistance = length(lightVec) * attenuationMultiplier;
- //Currently attenuation uses certain fudge factors.
- float attenuation = 1.0 / (1.0 + 20.0 * lightDistance + 200.0 * lightDistance * lightDistance);
- normal = normalize(normal);
- lightVec = normalize(lightVec);
- float rawDiffuse = clamp(dot(normal, lightVec) * 1000.0, 0.0, 1.0);
- //Somewhat confusing calculation to determine the diffuse level modified by the light wrap value.
- float diffuseLevel = clamp(dot(normal, lightVec) + lightWrap, 0.0, lightWrap + 1.0) / (lightWrap + 1.0) * attenuation;
- //Specular calculations
- vec3 viewVec = vec3(0.0, 0.0, 1.0); //Orthographic camera makes this nice and simple.
- vec3 bounceVec = reflect(-lightVec, normal);
- //Add 0.5 to the end of this to avoid raising to the zeroth power.
- float glossiness = 5.0;
- float specLevel = pow(clamp(dot(bounceVec, viewVec), 0.0, 1.0), glossiness) * rawDiffuse * 0.5 * attenuation;
- //Everything is comnbined in a big final calculation here.
- gl_FragColor.rgb = (diffuseLevel * directLightColour) * diffuseColour.rgb;
- gl_FragColor.rgb += vec3(specLevel, specLevel, specLevel) * specularColour.rgb * directLightColour;
- gl_FragColor.a = diffuseColour.a;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement