shaderPoint.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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("shaderPointVertex", shaderPointVertex)
  7. AddShader("shaderPointFrag", shaderPointFrag)
  8. AddProgram("shaderPoint", "shaderPointVertex", "shaderPointFrag")
  9. }
  10. //
  11. // Vertex Shader template
  12. //
  13. const shaderPointVertex = `
  14. #version {{.Version}}
  15. {{template "attributes" .}}
  16. // Model uniforms
  17. uniform mat4 MVP;
  18. // Material uniforms
  19. {{template "material" .}}
  20. // Specific uniforms
  21. uniform float PointSize;
  22. uniform float RotationZ;
  23. // Outputs for fragment shader
  24. out vec3 Color;
  25. flat out mat2 Rotation;
  26. void main() {
  27. // Rotation matrix for fragment shader
  28. float rotSin = sin(RotationZ);
  29. float rotCos = cos(RotationZ);
  30. Rotation = mat2(rotCos, rotSin, - rotSin, rotCos);
  31. // Sets the vertex position
  32. vec4 pos = MVP * vec4(VertexPosition, 1.0);
  33. gl_Position = pos;
  34. // Sets the size of the rasterized point decreasing with distance
  35. gl_PointSize = (1.0 - pos.z / pos.w) * PointSize;
  36. // Outputs color
  37. Color = MatEmissiveColor;
  38. }
  39. `
  40. //
  41. // Fragment Shader template
  42. //
  43. const shaderPointFrag = `
  44. #version {{.Version}}
  45. {{template "material" .}}
  46. // Inputs from vertex shader
  47. in vec3 Color;
  48. flat in mat2 Rotation;
  49. // Output
  50. out vec4 FragColor;
  51. void main() {
  52. // Combine all texture colors and opacity
  53. vec4 texCombined = vec4(1);
  54. {{ range loop .MatTexturesMax }}
  55. {
  56. vec2 pt = gl_PointCoord - vec2(0.5);
  57. vec4 texcolor = texture(MatTexture[{{.}}], (Rotation * pt + vec2(0.5)) * MatTexinfo[{{.}}]TexRepeat + MatTexinfo[{{.}}]TexOffset);
  58. if ({{.}} == 0) {
  59. texCombined = texcolor;
  60. } else {
  61. texCombined = mix(texCombined, texcolor, texcolor.a);
  62. }
  63. }
  64. {{ end }}
  65. // Combine material color with texture
  66. FragColor = min(vec4(Color, MatOpacity) * texCombined, vec4(1));
  67. }
  68. `