shaderTiles.go 3.0 KB

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