RayMarching SparkSL

So I’ve been learning about raymarching and how it works in SparkSL after an amazing Tomas Spark SL workshop. @Tomas_Pietravallo 's project file is super helpful! Now I’m still learning thru his raymarching project from github and I tried to separate each functions as their own dedicated assets but seems like I’m stuck in a dead end. Is it possible to separate them?
Maybe to separate them to something like this: SDF shapes → SDF Scene → Shading → render.

Why you might ask, I think it’s just for accessibility, for example:

  1. to be able to control the transform of each sdf object and light source in it from patch editor.
  2. to be able to change color from patch editor.

Any of you guys have any advice or hint to help me do that?
I’ve tried the export and #import <MyShaderName.sca> but It seems to always give me an error when the exported function requires multiple input.
Can someone give me an example of how to export and import functions that have multiple inputs? Thank you!

Are you defining the exports in the imported files?

export vec4 addition(vec4 color1, vec4 color2) {
  return color1 + color2;
#import <addition.sca>
vec4 main(vec4 color1, vec4 color2) {
  return addition(color1, color2);

spark-sl-imports.arexport (16.0 KB)

1 Like

thanks Josh! how about the lambda function like this?

is it actually just as easy as defining export in there too?

So the " function<vec4(vec2)> " is just like a texture2d data type but with it we can do math operation with other vec4 or other texture?

is it only " function<vec4(vec2)> " and there is no other type/thing like maybe
" function<float(vec2, vec2, float)> " or " function<vec3(float, float, float)> " ?

and also what if from this example you made, that color1 and color2 in addition.sca is also from different .sca files? and for the color1, it’s actually a texture (named “tex1”) that we want to add with color2? so there are tex1.sca and color2.sca

I’m still unclear sometimes about when to use Texture2d or function<vec4(vec2)> to do math or sampling and stuffs.

As long as you use the export keyword, you should be able to access the functions after you import them. If you are making a lot of exports, it might be good to put them into a namespace (shown in adam’s repo) to avoid conflicts with other code.

The difference between function<vec4(vec2)> and texture sampling is that the functions can be chained together, meaning the “texture” can be resampled again without needing a render pass. So if you think a function’s output will need to be resampled within the same shader, then use a function<vec4(vec2)>. In patch land, this is equivalent to a composition patch.

I think there can be any type of function composition, but in practice I haven’t found the need for it.

You can’t store textures in shaders but you could make a passthrough or selection function to return them from a list of inputs.

Here’s an example of storing a bunch of colors, and a texture passthrough.

export std::Texture2d tex2d(std::Texture2d tex){return tex;}
export vec4 red(){return vec4(1, 0, 0, 1);}
export vec4 green(){return vec4(0, 1, 0, 1);}
export vec4 blue(){return vec4(0, 0, 1, 1);}

1 Like

Awesome! Thanks Josh!

Have you played around with raymarching in spark sl too?
I’ve tried some shapes from IQ’s website in the Tomas’ raymarching code but I’m having a problem when trying to generate the plane:
Plane - exact

float sdPlane( vec3 p, vec3 n, float h )
  // n must be normalized
  return dot(p,n) + h;

I mean It shows a plane but the orientation and the position is a bit hard to tell and also seems like it doesn’t cast any shadow on it, while on the other shapes I can see the shadow that changes accordingly if I move the lights.

Another thing is that I’m still trying to figure out how to change color for each shapes, I only managed to change the light’s color. Cuz maybe from there I can start to try to assign different material for each of them (I don’t even know if that’s possible). like maybe a capsule shape is a glass, a sphere is a standard solid color, and maybe a torus is a using pattern texture. But I just can’t wrap my head around if they can be joined together with smooth union. like how could a glass looking shapes then joined like a metaball with other shapes that shaded with a pattern texture. :thinking:

I never got too far into the raymarching stuff because it was so slow on my phone. I know spark has had some issues with the dot function, so you could try making a fallback for that and see if it works.

1 Like