Skip to content

Commit edc704d

Browse files
authored
Merge pull request #3178 from Chaosus/shader_structs
Added docs for shader struct
2 parents d13551a + cbfa37a commit edc704d

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

tutorials/shading/shading_reference/shading_language.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,69 @@ Global constants are useful when you want to have access to a value throughout y
290290
291291
const float PI = 3.14159265358979323846;
292292
293+
294+
Structs
295+
-------
296+
297+
Structs are compound types which can be used for better abstaction of shader code. You can declare them at the global scope like:
298+
299+
.. code-block:: glsl
300+
struct PointLight {
301+
vec3 position;
302+
vec3 color;
303+
float intensity;
304+
};
305+
306+
After declaration, you can instantiate and initialize them like:
307+
308+
.. code-block:: glsl
309+
void fragment()
310+
{
311+
PointLight light;
312+
light.position = vec3(0.0);
313+
light.color = vec3(1.0, 0.0, 0.0);
314+
light.intensity = 0.5;
315+
}
316+
317+
Or use struct constructor for same purpose:
318+
319+
.. code-block:: glsl
320+
PointLight light = PointLight(vec3(0.0), vec3(1.0, 0.0, 0.0), 0.5);
321+
322+
Structs may contain other struct or array, you can also instance them as global constant:
323+
324+
.. code-block:: glsl
325+
shader_type spatial;
326+
327+
...
328+
329+
struct Scene {
330+
PointLight lights[2];
331+
};
332+
333+
const Scene scene = Scene(PointLight[2](PointLight(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), 1.0), PointLight(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), 1.0)));
334+
335+
void fragment()
336+
{
337+
ALBEDO = scene.lights[0].color;
338+
}
339+
340+
You can also pass them to functions:
341+
342+
.. code-block:: glsl
343+
shader_type canvas_item;
344+
345+
...
346+
347+
Scene construct_scene(PointLight light1, PointLight light2) {
348+
return Scene({light1, light2});
349+
}
350+
351+
void fragment()
352+
{
353+
COLOR.rgb = construct_scene(PointLight(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), 1.0), PointLight(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 1.0), 1.0)).lights[0].color;
354+
}
355+
293356
Operators
294357
---------
295358

0 commit comments

Comments
 (0)