shaderSprite.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 shaderSpriteVertex = `
  6. #version {{.Version}}
  7. {{template "attributes" .}}
  8. // Input uniforms
  9. uniform mat4 MVP;
  10. {{template "material" .}}
  11. // Outputs for fragment shader
  12. out vec3 Color;
  13. out vec2 FragTexcoord;
  14. void main() {
  15. // Applies transformation to vertex position
  16. gl_Position = MVP * vec4(VertexPosition, 1.0);
  17. // Outputs color
  18. Color = MatDiffuseColor;
  19. // Flips texture coordinate Y if requested.
  20. vec2 texcoord = VertexTexcoord;
  21. {{if .MatTexturesMax}}
  22. if (MatTexFlipY[0] > 0) {
  23. texcoord.y = 1 - texcoord.y;
  24. }
  25. {{ end }}
  26. FragTexcoord = texcoord;
  27. }
  28. `
  29. //
  30. // Fragment Shader template
  31. //
  32. const shaderSpriteFrag = `
  33. #version {{.Version}}
  34. {{template "material" .}}
  35. // Inputs from vertex shader
  36. in vec3 Color;
  37. in vec2 FragTexcoord;
  38. // Output
  39. out vec4 FragColor;
  40. void main() {
  41. // Combine all texture colors and opacity
  42. vec4 texCombined = vec4(1);
  43. {{if .MatTexturesMax }}
  44. for (int i = 0; i < {{.MatTexturesMax}}; i++) {
  45. vec4 texcolor = texture(MatTexture[i], FragTexcoord * MatTexRepeat[i] + MatTexOffset[i]);
  46. if (i == 0) {
  47. texCombined = texcolor;
  48. } else {
  49. texCombined = mix(texCombined, texcolor, texcolor.a);
  50. }
  51. }
  52. {{ end }}
  53. // Combine material color with texture
  54. FragColor = min(vec4(Color, MatOpacity) * texCombined, vec4(1));
  55. }
  56. `