example2.gdshader 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. shader_type spatial;
  2. render_mode wireframe;
  3. const lowp vec3 v[1] = lowp vec3[1] ( vec3(0, 0, 1) );
  4. void fn() {
  5. // The required amount of scalars
  6. vec4 a0 = vec4(0.0, 1.0, 2.0, 3.0);
  7. // Complementary vectors and/or scalars
  8. vec4 a1 = vec4(vec2(0.0, 1.0), vec2(2.0, 3.0));
  9. vec4 a2 = vec4(vec3(0.0, 1.0, 2.0), 3.0);
  10. // A single scalar for the whole vector
  11. vec4 a3 = vec4(0.0);
  12. mat2 m2 = mat2(vec2(1.0, 0.0), vec2(0.0, 1.0));
  13. mat3 m3 = mat3(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
  14. mat4 identity = mat4(1.0);
  15. mat3 basis = mat3(identity);
  16. mat4 m4 = mat4(basis);
  17. mat2 m2a = mat2(m4);
  18. vec4 a = vec4(0.0, 1.0, 2.0, 3.0);
  19. vec3 b = a.rgb; // Creates a vec3 with vec4 components.
  20. vec3 b1 = a.ggg; // Also valid; creates a vec3 and fills it with a single vec4 component.
  21. vec3 b2 = a.bgr; // "b" will be vec3(2.0, 1.0, 0.0).
  22. vec3 b3 = a.xyz; // Also rgba, xyzw are equivalent.
  23. vec3 b4 = a.stp; // And stpq (for texture coordinates).
  24. b.bgr = a.rgb; // Valid assignment. "b"'s "blue" component will be "a"'s "red" and vice versa.
  25. lowp vec4 v0 = vec4(0.0, 1.0, 2.0, 3.0); // low precision, usually 8 bits per component mapped to 0-1
  26. mediump vec4 v1 = vec4(0.0, 1.0, 2.0, 3.0); // medium precision, usually 16 bits or half float
  27. highp vec4 v2 = vec4(0.0, 1.0, 2.0, 3.0); // high precision, uses full float or integer range (default)
  28. const vec2 aa = vec2(0.0, 1.0);
  29. vec2 bb;
  30. bb = aa; // valid
  31. const vec2 V1 = vec2(1, 1), V2 = vec2(2, 2);
  32. float fa = 1.0;
  33. float fb = 1.0f;
  34. float fc = 1e-1;
  35. uint ua = 1u;
  36. uint ub = uint(1);
  37. bool cond = false;
  38. // `if` and `else`.
  39. if (cond) {
  40. } else {
  41. }
  42. // Ternary operator.
  43. // This is an expression that behaves like `if`/`else` and returns the value.
  44. // If `cond` evaluates to `true`, `result` will be `9`.
  45. // Otherwise, `result` will be `5`.
  46. int i, result = cond ? 9 : 5;
  47. // `switch`.
  48. switch (i) { // `i` should be a signed integer expression.
  49. case -1:
  50. break;
  51. case 0:
  52. return; // `break` or `return` to avoid running the next `case`.
  53. case 1: // Fallthrough (no `break` or `return`): will run the next `case`.
  54. case 2:
  55. break;
  56. //...
  57. default: // Only run if no `case` above matches. Optional.
  58. break;
  59. }
  60. // `for` loop. Best used when the number of elements to iterate on
  61. // is known in advance.
  62. for (int i = 0; i < 10; i++) {
  63. }
  64. // `while` loop. Best used when the number of elements to iterate on
  65. // is not known in advance.
  66. while (cond) {
  67. }
  68. // `do while`. Like `while`, but always runs at least once even if `cond`
  69. // never evaluates to `true`.
  70. do {
  71. } while (cond);
  72. }
  73. const float PI_ = 3.14159265358979323846;
  74. struct PointLight {
  75. vec3 position;
  76. vec3 color;
  77. float intensity;
  78. };
  79. struct Scene {
  80. PointLight lights[2];
  81. };
  82. const Scene scene = Scene(PointLight[2](
  83. PointLight(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), 1.0),
  84. PointLight(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), 1.0)
  85. ));
  86. Scene construct_scene(PointLight light1, PointLight light2) {
  87. return Scene({light1, light2});
  88. }
  89. varying flat vec3 some_color;
  90. varying float var_arr[3];
  91. varying smooth vec3 some_light;
  92. uniform float some_value;
  93. uniform vec4 color : hint_color;
  94. uniform float amount : hint_range(0, 1);
  95. uniform vec4 other_color : hint_color = vec4(1.0);
  96. uniform vec4 some_vector = vec4(0.0);
  97. uniform vec4 some_color2 : hint_color = vec4(1.0);
  98. void vertex() {
  99. const float arr[] = { 1.0, 0.5, 0.0 };
  100. COLOR.r = arr[0]; // valid
  101. float arr2[3];
  102. arr2[0] = 1.0; // setter
  103. COLOR.r = arr2[0]; // getter
  104. PointLight light;
  105. light.position = vec3(0.0);
  106. light.color = vec3(1.0, 0.0, 0.0);
  107. light.intensity = 0.5;
  108. COLOR.rgb = construct_scene(
  109. PointLight(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), 1.0),
  110. PointLight(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 1.0), 1.0)
  111. ).lights[0].color;
  112. some_color = NORMAL; // Make the normal the color.
  113. var_arr[0] = 1.0;
  114. var_arr[1] = 0.0;
  115. }
  116. void fragment() {
  117. float arr[3];
  118. float float_arr[3] = float[3] (1.0, 0.5, 0.0); // first constructor
  119. int int_arr[3] = int[] (2, 1, 0); // second constructor
  120. vec2 vec2_arr[3] = { vec2(1.0, 1.0), vec2(0.5, 0.5), vec2(0.0, 0.0) }; // third constructor
  121. bool bool_arr[] = { true, true, false }; // fourth constructor - size is defined automatically from the element count
  122. float a[3] = float[3] (1.0, 0.5, 0.0),
  123. b[2] = { 1.0, 0.5 },
  124. c[] = { 0.7 },
  125. d = 0.0,
  126. e[5];
  127. float arr2[] = { 0.0, 1.0, 0.5, -1.0 };
  128. for (int i = 0; i < arr2.length(); i++) {
  129. }
  130. ALBEDO = v[0];
  131. PointLight light = PointLight(vec3(0.0), vec3(1.0, 0.0, 0.0), 0.5);
  132. ALBEDO = scene.lights[0].color;
  133. const float EPSILON = 0.0001, value = 0.f;
  134. if (value >= 0.3 - EPSILON && value <= 0.3 + EPSILON) {
  135. discard;
  136. }
  137. ALBEDO = vec3(var_arr[0], var_arr[1], var_arr[2]); // red color
  138. some_light = ALBEDO * 100.0; // Make a shining light.
  139. }
  140. void sum2(int a, in int b, inout int result) {
  141. result = a + b;
  142. }
  143. void sub2(const int a, const in int b, out int result) {
  144. result = a - b;
  145. }
  146. global uniform sampler2D global1;
  147. instance uniform int un = 0;
  148. void light() {
  149. DIFFUSE_LIGHT = some_color * 100.; // optionally
  150. DIFFUSE_LIGHT = some_light;
  151. }