shaderPanel.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. uniform vec4 Panel[7];
  45. const int bounds = 0; // index of uniform array for bounds coordinates
  46. const int border = 1; // index of uniform array for border coordinates
  47. const int padding = 2; // index of uniform array for padding coordinates
  48. const int content = 3; // index of uniform array for content coordinates
  49. const int borderColor = 4; // index of uniform array for border color
  50. const int paddingColor = 5; // index of uniform array for padding color
  51. const int contentColor = 6; // index of uniform array for content color
  52. // Output
  53. out vec4 FragColor;
  54. /***
  55. * Checks if current fragment texture coordinate is inside the
  56. * supplied rectangle in texture coordinates:
  57. * rect[0] - position x [0,1]
  58. * rect[1] - position y [0,1]
  59. * rect[2] - width [0,1]
  60. * rect[3] - height [0,1]
  61. */
  62. bool checkRect(vec4 rect) {
  63. if (FragTexcoord.x < rect[0]) {
  64. return false;
  65. }
  66. if (FragTexcoord.x > rect[0] + rect[2]) {
  67. return false;
  68. }
  69. if (FragTexcoord.y < rect[1]) {
  70. return false;
  71. }
  72. if (FragTexcoord.y > rect[1] + rect[3]) {
  73. return false;
  74. }
  75. return true;
  76. }
  77. void main() {
  78. // Discard fragment outside of received bounds
  79. // Bounds[0] - xmin
  80. // Bounds[1] - ymin
  81. // Bounds[2] - xmax
  82. // Bounds[3] - ymax
  83. if (FragTexcoord.x <= Panel[bounds][0] || FragTexcoord.x >= Panel[bounds][2]) {
  84. discard;
  85. }
  86. if (FragTexcoord.y <= Panel[bounds][1] || FragTexcoord.y >= Panel[bounds][3]) {
  87. discard;
  88. }
  89. // Check if fragment is inside content area
  90. if (checkRect(Panel[content])) {
  91. // If no texture, the color will be the material color.
  92. vec4 color = Panel[contentColor];
  93. {{ if .MatTexturesMax }}
  94. // Adjust texture coordinates to fit texture inside the content area
  95. vec2 offset = vec2(-Panel[content][0], -Panel[content][1]);
  96. vec2 factor = vec2(1/Panel[content][2], 1/Panel[content][3]);
  97. vec2 texcoord = (FragTexcoord + offset) * factor;
  98. color = texture(MatTexture[0], texcoord * MatTexRepeat[0] + MatTexOffset[0]);
  99. {{ end }}
  100. if (color.a == 0) {
  101. discard;
  102. }
  103. FragColor = color;
  104. return;
  105. }
  106. // Checks if fragment is inside paddings area
  107. if (checkRect(Panel[padding])) {
  108. FragColor = Panel[paddingColor];
  109. return;
  110. }
  111. // Checks if fragment is inside borders area
  112. if (checkRect(Panel[border])) {
  113. FragColor = Panel[borderColor];
  114. return;
  115. }
  116. // Fragment is in margins area (always transparent)
  117. FragColor = vec4(1,1,1,0);
  118. }
  119. `