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. {{if .MatTexturesMax }}
  69. // Combine all texture colors and opacity
  70. for (int i = 0; i < {{.MatTexturesMax}}; i++) {
  71. if (MatTexVisible[i] == false) {
  72. continue;
  73. }
  74. vec4 texcolor = texture(MatTexture[i], FragTexcoord * MatTexRepeat[i] + MatTexOffset[i]);
  75. if (i == 0) {
  76. texCombined = texcolor;
  77. } else {
  78. texCombined = mix(texCombined, texcolor, texcolor.a);
  79. }
  80. }
  81. {{ end }}
  82. vec4 colorAmbDiff;
  83. vec4 colorSpec;
  84. if (gl_FrontFacing) {
  85. colorAmbDiff = vec4(ColorFrontAmbdiff, MatOpacity);
  86. colorSpec = vec4(ColorFrontSpec, 0);
  87. } else {
  88. colorAmbDiff = vec4(ColorBackAmbdiff, MatOpacity);
  89. colorSpec = vec4(ColorBackSpec, 0);
  90. }
  91. FragColor = min(colorAmbDiff * texCombined + colorSpec, vec4(1));
  92. }
  93. `