shaderPhong.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. {{if .MatTexturesMax }}
  64. for (int i = 0; i < {{.MatTexturesMax}}; i++) {
  65. if (MatTexVisible[i] == false) {
  66. continue;
  67. }
  68. vec4 texcolor = texture(MatTexture[i], FragTexcoord * MatTexRepeat[i] + MatTexOffset[i]);
  69. if (i == 0) {
  70. texCombined = texcolor;
  71. } else {
  72. texCombined = mix(texCombined, texcolor, texcolor.a);
  73. }
  74. }
  75. {{ end }}
  76. // Combine material with texture colors
  77. vec4 matDiffuse = vec4(MatDiffuseColor, MatOpacity) * texCombined;
  78. vec4 matAmbient = vec4(MatAmbientColor, MatOpacity) * texCombined;
  79. // Inverts the fragment normal if not FrontFacing
  80. vec3 fragNormal = Normal;
  81. if (!gl_FrontFacing) {
  82. fragNormal = -fragNormal;
  83. }
  84. // Calculates the Ambient+Diffuse and Specular colors for this fragment using the Phong model.
  85. vec3 Ambdiff, Spec;
  86. phongModel(Position, fragNormal, CamDir, vec3(matAmbient), vec3(matDiffuse), Ambdiff, Spec);
  87. // Final fragment color
  88. FragColor = min(vec4(Ambdiff + Spec, matDiffuse.a), vec4(1.0));
  89. }
  90. `