shaderPanel.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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("shaderPanelVertex", shaderPanelVertex)
  7. AddShader("shaderPanelFrag", shaderPanelFrag)
  8. AddProgram("shaderPanel", "shaderPanelVertex", "shaderPanelFrag")
  9. }
  10. const shaderPanelVertex = `
  11. #version {{.Version}}
  12. // Vertex attributes
  13. {{template "attributes" .}}
  14. // Input uniforms
  15. uniform mat4 ModelMatrix;
  16. // Outputs for fragment shader
  17. out vec2 FragTexcoord;
  18. void main() {
  19. // Always flip texture coordinates
  20. vec2 texcoord = VertexTexcoord;
  21. texcoord.y = 1 - texcoord.y;
  22. FragTexcoord = texcoord;
  23. // Set position
  24. vec4 pos = vec4(VertexPosition.xyz, 1);
  25. gl_Position = ModelMatrix * pos;
  26. }
  27. `
  28. //
  29. // Fragment Shader template
  30. //
  31. const shaderPanelFrag = `
  32. #version {{.Version}}
  33. {{template "material" .}}
  34. // Inputs from vertex shader
  35. in vec2 FragTexcoord;
  36. // Input uniforms
  37. uniform vec4 Bounds;
  38. uniform vec4 Border;
  39. uniform vec4 Padding;
  40. uniform vec4 Content;
  41. uniform vec4 BorderColor;
  42. uniform vec4 PaddingColor;
  43. uniform vec4 ContentColor;
  44. // Output
  45. out vec4 FragColor;
  46. /***
  47. * Checks if current fragment texture coordinate is inside the
  48. * supplied rectangle in texture coordinates:
  49. * rect[0] - position x [0,1]
  50. * rect[1] - position y [0,1]
  51. * rect[2] - width [0,1]
  52. * rect[3] - height [0,1]
  53. */
  54. bool checkRect(vec4 rect) {
  55. if (FragTexcoord.x < rect[0]) {
  56. return false;
  57. }
  58. if (FragTexcoord.x > rect[0] + rect[2]) {
  59. return false;
  60. }
  61. if (FragTexcoord.y < rect[1]) {
  62. return false;
  63. }
  64. if (FragTexcoord.y > rect[1] + rect[3]) {
  65. return false;
  66. }
  67. return true;
  68. }
  69. void main() {
  70. // Discard fragment outside of received bounds
  71. // Bounds[0] - xmin
  72. // Bounds[1] - ymin
  73. // Bounds[2] - xmax
  74. // Bounds[3] - ymax
  75. if (FragTexcoord.x <= Bounds[0] || FragTexcoord.x >= Bounds[2]) {
  76. discard;
  77. }
  78. if (FragTexcoord.y <= Bounds[1] || FragTexcoord.y >= Bounds[3]) {
  79. discard;
  80. }
  81. // Check if fragment is inside content area
  82. if (checkRect(Content)) {
  83. // If no texture, the color will be the material color.
  84. vec4 color = ContentColor;
  85. {{ if .MatTexturesMax }}
  86. // Adjust texture coordinates to fit texture inside the content area
  87. vec2 offset = vec2(-Content[0], -Content[1]);
  88. vec2 factor = vec2(1/Content[2], 1/Content[3]);
  89. vec2 texcoord = (FragTexcoord + offset) * factor;
  90. color = texture(MatTexture[0], texcoord * MatTexRepeat[0] + MatTexOffset[0]);
  91. {{ end }}
  92. if (color.a == 0) {
  93. discard;
  94. }
  95. FragColor = color;
  96. return;
  97. }
  98. // Checks if fragment is inside paddings area
  99. if (checkRect(Padding)) {
  100. FragColor = PaddingColor;
  101. return;
  102. }
  103. // Checks if fragment is inside borders area
  104. if (checkRect(Border)) {
  105. FragColor = BorderColor;
  106. return;
  107. }
  108. // Fragment is in margins area (always transparent)
  109. FragColor = vec4(1,1,1,0);
  110. }
  111. `