SHVertShader.vsh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. attribute highp vec3 inVertex;
  2. attribute mediump vec3 inNormal;
  3. attribute mediump vec2 inTexCoord;
  4. uniform highp mat4 MVPMatrix;
  5. uniform highp mat3 Model;
  6. // Precalculated constants used for lighting
  7. uniform highp vec4 cAr;
  8. uniform highp vec4 cAg;
  9. uniform highp vec4 cAb;
  10. uniform highp vec4 cBr;
  11. uniform highp vec4 cBg;
  12. uniform highp vec4 cBb;
  13. uniform highp vec3 cC;
  14. // varyings
  15. varying lowp vec4 LightColour;
  16. varying mediump vec2 TexCoord;
  17. void main()
  18. {
  19. highp vec4 r0, r1;
  20. highp vec3 r2, r3;
  21. // Transform position
  22. gl_Position = MVPMatrix * vec4(inVertex, 1.0);
  23. // Transform the Normal and add a homogenous 1
  24. r0 = vec4(Model * inNormal, 1.0);
  25. // Compute 1st 4 basis functions - linear + constant
  26. // r0 is the normal with a homegenous 1
  27. // c* are precomputed constants
  28. r2.x = dot(cAr, r0);
  29. r2.y = dot(cAg, r0);
  30. r2.z = dot(cAb, r0);
  31. // Compute polynomials for the next 4 basis functions
  32. r1 = r0.yzzx * r0.xyzz; // r1 is { yx, zy, z^2, xz}
  33. // Add contributions and store them in r3
  34. r3.x = dot(cBr, r1);
  35. r3.y = dot(cBg, r1);
  36. r3.z = dot(cBb, r1);
  37. // Compute the final basis function x^2 - y^2
  38. r0.z = r0.y * r0.y;
  39. r0.w = (r0.x * r0.x) - r0.z;
  40. // Combine the first 2 sets : 8 basis functions
  41. r1.xyz = r2 + r3;
  42. // Add in the final 9th basis function to create the final RGB Lighting
  43. LightColour.xyz = (cC * r0.w) + r1.xyz;
  44. // Set light alpha to 1.0
  45. LightColour.a = 1.0;
  46. // Pass through texcoords
  47. TexCoord = inTexCoord;
  48. }