Loop in visual shader

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PaperMartin

is there any way to run loops in visual shaders right now? I wanna do ray marching and that requires a loop, which is possible in a text shader (some dude has an open source godot ray marching project with shaders that do it) but I can’t find any way to do it in a visual shader

Can you add to your (edit) question a link to the ray marching project. Maybe that can give us hints.

clemens.tolboom | 2021-04-28 18:10

Was my answer useful? If so please mark it as such :slight_smile:

clemens.tolboom | 2021-07-08 10:50

:bust_in_silhouette: Reply From: clemens.tolboom

Thanks for the link. I tested with an Expression node with an input Scalar times and output Color color.

color = vec3(0.5,0.1,0.1);
for(int i=0; i < int(times);i++) {
  color = color + 0.1;

so you can add loops :slight_smile:

This loop gets injected into the VisualShader output like:

// Expression:3
vec3 n_out3p0;
n_out3p0 = vec3(0.0, 0.0, 0.0);
	n_out3p0 = vec3(0.5,0.1,0.1);
	for(int i=0; i < int(n_out4p0);i++) {
	  n_out3p0 = n_out3p0 + 0.1;

Technically this works but the idea was to create the loop, and the content of said loop, entirely using node

PaperMartin | 2021-07-08 11:26

But this is the only method available using an Expression so is the right answer IMHO :wink:

clemens.tolboom | 2021-07-09 11:06