shaderPanel.go 3.6 KB

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