shaderPoint.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // Outputs for fragment shader
  21. out vec3 Color;
  22. flat out mat2 Rotation;
  23. void main() {
  24. // Rotation matrix for fragment shader
  25. float rotSin = sin(MatPointRotationZ);
  26. float rotCos = cos(MatPointRotationZ);
  27. Rotation = mat2(rotCos, rotSin, - rotSin, rotCos);
  28. // Sets the vertex position
  29. vec4 pos = MVP * vec4(VertexPosition, 1.0);
  30. gl_Position = pos;
  31. // Sets the size of the rasterized point decreasing with distance
  32. gl_PointSize = (1.0 - pos.z / pos.w) * MatPointSize;
  33. // Outputs color
  34. Color = MatEmissiveColor;
  35. }
  36. `
  37. //
  38. // Fragment Shader template
  39. //
  40. const shaderPointFrag = `
  41. #version {{.Version}}
  42. {{template "material" .}}
  43. // Inputs from vertex shader
  44. in vec3 Color;
  45. flat in mat2 Rotation;
  46. // Output
  47. out vec4 FragColor;
  48. void main() {
  49. // Combine all texture colors and opacity
  50. vec4 texCombined = vec4(1);
  51. {{ range loop .MatTexturesMax }}
  52. {
  53. vec2 pt = gl_PointCoord - vec2(0.5);
  54. vec4 texcolor = texture(MatTexture[{{.}}], (Rotation * pt + vec2(0.5)) * MatTexRepeat({{.}}) + MatTexOffset({{.}}));
  55. if ({{.}} == 0) {
  56. texCombined = texcolor;
  57. } else {
  58. texCombined = mix(texCombined, texcolor, texcolor.a);
  59. }
  60. }
  61. {{ end }}
  62. // Combine material color with texture
  63. FragColor = min(vec4(Color, MatOpacity) * texCombined, vec4(1));
  64. }
  65. `