Advertisement
Guest User

StandardPhong.fsd

a guest
Dec 25th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. //This is the default Sprite Lamp shader. It is not optimised for speed but rather, readability,
  2. //so certain things wouldn't make sense in a game environment (for instance you'd probably
  3. //want to pack things into fewer textures).
  4.  
  5. #version 120
  6.  
  7. uniform sampler2D normalMap;
  8. uniform sampler2D diffuseMap;
  9. uniform sampler2D specularMap; //Optionally contains glossiness in the alpha channel
  10.  
  11. uniform vec3 lightPosition;
  12. uniform vec3 directLightColour;
  13. uniform vec3 ambientLightColour; //Upper ambient colour
  14. uniform vec2 textureResolution; //Used for per texel lighting
  15.  
  16. uniform float lightWrap;
  17.  
  18. uniform float attenuationMultiplier;
  19.  
  20. uniform mat2 rotationMatrix; //Simple case of 2D rotation - not suitable for general game engine use
  21.  
  22. void main()
  23. {
  24.  
  25. //This value will be the tex coords at the centre of the nearest texel.
  26. vec2 centredTexCoords = gl_TexCoord[0].xy;
  27.  
  28. centredTexCoords *= textureResolution.xy;
  29. centredTexCoords = floor(centredTexCoords.xy) + vec2(0.5, 0.5);
  30. centredTexCoords /= textureResolution.xy;
  31.  
  32. //All the texture lookups done at the start where possible.
  33. vec4 normalColour = texture2D(normalMap, gl_TexCoord[0].xy);
  34. vec4 diffuseColour = texture2D(diffuseMap, gl_TexCoord[0].xy);
  35. vec4 specularColour = texture2D(specularMap, gl_TexCoord[0].xy);
  36.  
  37.  
  38. if (diffuseColour.a <= 0.1)
  39. {
  40. discard;
  41. }
  42.  
  43. vec3 fragPos;
  44. fragPos.xy = gl_TexCoord[0].xy;
  45.  
  46.  
  47. //This sets the fragment position at the centre of the nearest texel,
  48. //for per-texel lighting. This unfortunately isn't very reusable in a general game engine sense.
  49. fragPos.xy *= textureResolution.xy;
  50. fragPos.xy = floor(fragPos.xy);
  51. fragPos.xy /= textureResolution.xy;
  52.  
  53.  
  54. fragPos.y = 1.0 - fragPos.y;
  55.  
  56.  
  57. fragPos = fragPos + vec3(-0.5, -0.5, 0.0);
  58. fragPos.x *= (textureResolution.x / textureResolution.y);
  59.  
  60. fragPos.z = 1;
  61.  
  62. vec3 normal = (normalColour.rgb - 0.5) * 2.0;
  63. normal.y *= normalFlip;
  64. normal.xy *= rotationMatrix;
  65.  
  66. vec3 lightVec = lightPosition - fragPos;
  67. lightVec.xy *= rotationMatrix;
  68.  
  69. float lightDistance = length(lightVec) * attenuationMultiplier;
  70.  
  71. //Currently attenuation uses certain fudge factors.
  72. float attenuation = 1.0 / (1.0 + 20.0 * lightDistance + 200.0 * lightDistance * lightDistance);
  73.  
  74. normal = normalize(normal);
  75. lightVec = normalize(lightVec);
  76.  
  77. float rawDiffuse = clamp(dot(normal, lightVec) * 1000.0, 0.0, 1.0);
  78.  
  79. //Somewhat confusing calculation to determine the diffuse level modified by the light wrap value.
  80. float diffuseLevel = clamp(dot(normal, lightVec) + lightWrap, 0.0, lightWrap + 1.0) / (lightWrap + 1.0) * attenuation;
  81.  
  82.  
  83.  
  84.  
  85. //Specular calculations
  86. vec3 viewVec = vec3(0.0, 0.0, 1.0); //Orthographic camera makes this nice and simple.
  87. vec3 bounceVec = reflect(-lightVec, normal);
  88. //Add 0.5 to the end of this to avoid raising to the zeroth power.
  89. float glossiness = 5.0;
  90. float specLevel = pow(clamp(dot(bounceVec, viewVec), 0.0, 1.0), glossiness) * rawDiffuse * 0.5 * attenuation;
  91.  
  92. //Everything is comnbined in a big final calculation here.
  93. gl_FragColor.rgb = (diffuseLevel * directLightColour) * diffuseColour.rgb;
  94. gl_FragColor.rgb += vec3(specLevel, specLevel, specLevel) * specularColour.rgb * directLightColour;
  95.  
  96. gl_FragColor.a = diffuseColour.a;
  97.  
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement