shaderStandard.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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("shaderStandardVertex", shaderStandardVertex)
  7. AddShader("shaderStandardFrag", shaderStandardFrag)
  8. AddProgram("shaderStandard", "shaderStandardVertex", "shaderStandardFrag")
  9. }
  10. //
  11. // Vertex Shader template
  12. //
  13. const shaderStandardVertex = `
  14. #version {{.Version}}
  15. {{template "attributes" .}}
  16. // Model uniforms
  17. uniform mat4 ModelViewMatrix;
  18. uniform mat3 NormalMatrix;
  19. uniform mat4 MVP;
  20. {{template "lights" .}}
  21. {{template "material" .}}
  22. {{template "phong_model" .}}
  23. // Outputs for the fragment shader.
  24. out vec3 ColorFrontAmbdiff;
  25. out vec3 ColorFrontSpec;
  26. out vec3 ColorBackAmbdiff;
  27. out vec3 ColorBackSpec;
  28. out vec2 FragTexcoord;
  29. void main() {
  30. // Transform this vertex normal to camera coordinates.
  31. vec3 normal = normalize(NormalMatrix * VertexNormal);
  32. // Calculate this vertex position in camera coordinates
  33. vec4 position = ModelViewMatrix * vec4(VertexPosition, 1.0);
  34. // Calculate the direction vector from the vertex to the camera
  35. // The camera is at 0,0,0
  36. vec3 camDir = normalize(-position.xyz);
  37. // Calculates the vertex Ambient+Diffuse and Specular colors using the Phong model
  38. // for the front and back
  39. phongModel(position, normal, camDir, MatAmbientColor, MatDiffuseColor, ColorFrontAmbdiff, ColorFrontSpec);
  40. phongModel(position, -normal, camDir, MatAmbientColor, MatDiffuseColor, ColorBackAmbdiff, ColorBackSpec);
  41. vec2 texcoord = VertexTexcoord;
  42. {{if .MatTexturesMax }}
  43. // Flips texture coordinate Y if requested.
  44. if (MatTexFlipY[0] > 0) {
  45. texcoord.y = 1 - texcoord.y;
  46. }
  47. {{ end }}
  48. FragTexcoord = texcoord;
  49. gl_Position = MVP * vec4(VertexPosition, 1.0);
  50. }
  51. `
  52. //
  53. // Fragment Shader template
  54. //
  55. const shaderStandardFrag = `
  56. #version {{.Version}}
  57. {{template "material" .}}
  58. // Inputs from Vertex shader
  59. in vec3 ColorFrontAmbdiff;
  60. in vec3 ColorFrontSpec;
  61. in vec3 ColorBackAmbdiff;
  62. in vec3 ColorBackSpec;
  63. in vec2 FragTexcoord;
  64. // Output
  65. out vec4 FragColor;
  66. void main() {
  67. vec4 texCombined = vec4(1);
  68. // Combine all texture colors and opacity
  69. // Use Go templates to unroll the loop because non-const
  70. // array indexes are not allowed until GLSL 4.00.
  71. {{ range loop .MatTexturesMax }}
  72. if (MatTexVisible[{{.}}] == true) {
  73. vec4 texcolor = texture(MatTexture[{{.}}], FragTexcoord * MatTexRepeat[{{.}}] + MatTexOffset[{{.}}]);
  74. if ({{.}} == 0) {
  75. texCombined = texcolor;
  76. } else {
  77. texCombined = mix(texCombined, texcolor, texcolor.a);
  78. }
  79. }
  80. {{ end }}
  81. vec4 colorAmbDiff;
  82. vec4 colorSpec;
  83. if (gl_FrontFacing) {
  84. colorAmbDiff = vec4(ColorFrontAmbdiff, MatOpacity);
  85. colorSpec = vec4(ColorFrontSpec, 0);
  86. } else {
  87. colorAmbDiff = vec4(ColorBackAmbdiff, MatOpacity);
  88. colorSpec = vec4(ColorBackSpec, 0);
  89. }
  90. FragColor = min(colorAmbDiff * texCombined + colorSpec, vec4(1));
  91. }
  92. `