shaderPhong.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package shader
  5. func init() {
  6. AddShader("shaderPhongVertex", shaderPhongVertex)
  7. AddShader("shaderPhongFrag", shaderPhongFrag)
  8. AddProgram("shaderPhong", "shaderPhongVertex", "shaderPhongFrag")
  9. }
  10. //
  11. // Vertex Shade template
  12. //
  13. var shaderPhongVertex = `
  14. #version {{.Version}}
  15. {{template "attributes" .}}
  16. // Model uniforms
  17. uniform mat4 ModelViewMatrix;
  18. uniform mat3 NormalMatrix;
  19. uniform mat4 MVP;
  20. {{template "material" .}}
  21. // Output variables for Fragment shader
  22. out vec4 Position;
  23. out vec3 Normal;
  24. out vec3 CamDir;
  25. out vec2 FragTexcoord;
  26. void main() {
  27. // Transform this vertex position to camera coordinates.
  28. Position = ModelViewMatrix * vec4(VertexPosition, 1.0);
  29. // Transform this vertex normal to camera coordinates.
  30. Normal = normalize(NormalMatrix * VertexNormal);
  31. // Calculate the direction vector from the vertex to the camera
  32. // The camera is at 0,0,0
  33. CamDir = normalize(-Position.xyz);
  34. // Flips texture coordinate Y if requested.
  35. vec2 texcoord = VertexTexcoord;
  36. {{ if .MatTexturesMax }}
  37. if (MatTexFlipY[0] > 0) {
  38. texcoord.y = 1 - texcoord.y;
  39. }
  40. {{ end }}
  41. FragTexcoord = texcoord;
  42. gl_Position = MVP * vec4(VertexPosition, 1.0);
  43. }
  44. `
  45. //
  46. // Fragment Shader template
  47. //
  48. var shaderPhongFrag = `
  49. #version {{.Version}}
  50. // Inputs from vertex shader
  51. in vec4 Position; // Vertex position in camera coordinates.
  52. in vec3 Normal; // Vertex normal in camera coordinates.
  53. in vec3 CamDir; // Direction from vertex to camera
  54. in vec2 FragTexcoord;
  55. {{template "lights" .}}
  56. {{template "material" .}}
  57. {{template "phong_model" .}}
  58. // Final fragment color
  59. out vec4 FragColor;
  60. void main() {
  61. // Combine all texture colors
  62. vec4 texCombined = vec4(1);
  63. {{ range loop .MatTexturesMax }}
  64. if (MatTexVisible[{{.}}] == true) {
  65. vec4 texcolor = texture(MatTexture[{{.}}], FragTexcoord * MatTexRepeat[{{.}}] + MatTexOffset[{{.}}]);
  66. if ({{.}} == 0) {
  67. texCombined = texcolor;
  68. } else {
  69. texCombined = mix(texCombined, texcolor, texcolor.a);
  70. }
  71. }
  72. {{ end }}
  73. // Combine material with texture colors
  74. vec4 matDiffuse = vec4(MatDiffuseColor, MatOpacity) * texCombined;
  75. vec4 matAmbient = vec4(MatAmbientColor, MatOpacity) * texCombined;
  76. // Inverts the fragment normal if not FrontFacing
  77. vec3 fragNormal = Normal;
  78. if (!gl_FrontFacing) {
  79. fragNormal = -fragNormal;
  80. }
  81. // Calculates the Ambient+Diffuse and Specular colors for this fragment using the Phong model.
  82. vec3 Ambdiff, Spec;
  83. phongModel(Position, fragNormal, CamDir, vec3(matAmbient), vec3(matDiffuse), Ambdiff, Spec);
  84. // Final fragment color
  85. FragColor = min(vec4(Ambdiff + Spec, matDiffuse.a), vec4(1.0));
  86. }
  87. `