# SDF - indexing the folding space

I had been repetitively watching this part of the video just trying to understand his point:

you can optionally use the return value as cell index ... as random seed to introduce some variation ...

at the near end of the video he stress that:

don't use for loop ... anything can be achieved by repeating space ... I will make an operator to repeat an interval ... give it additional values like the first and the last cell index ... start by -2 and end at +2 then you get 5 objects ...

How can we achieve like what he say? Do we use clamp() to do that? Something like:

float a = clamp(pMod1(pos.x,2),start_value, end_value);


It is brilliant that everything he present is just like magic to me.

Update01

I dig deeper into SDF research and reading more codes from shadertoy, and discovered this interesting capsule code:

float sdCapsule( float3 p, float3 a, float3 b, float r ) {
float3 pa = p - a,
ba = b - a;
float h = clamp( dot(pa, ba) / dot(ba , ba), 0.0, 1.0 );
return length( pa - ba * h ) - r;
}


Basically, length(x)-r will get sphere, for x as the origin of the sphere and r is radius of the sphere. so the sdCapsule to some extend, is a variation of sphere, with the clamp(). That means my speculation might be correct. However, I still not sure what that clamp() do and how to adapt it to my use.

Modulus is the operation that is used to repeat space.

If you take N mod 5, as you increase N, you'll get [0, 5) over and over and over.

If you want to constrain it to just repeating a certain number of times, you can use clamp before modulus. When doing this, make sure that the value you clamp at is associated with empty space though, so you don't have geometry extending off into infinity.

Here is a link you are likely interested in (:

http://iquilezles.org/www/articles/distfunctions/distfunctions.htm

• I understand that. But that will be like you said, it goes on to infinity. What I want is just do the repetition for 4 times. For example, how to achieve that? – sooon Apr 17 '17 at 22:35
• Yeah, one way would be to clamp before modulus. You want to make sure that where you clamp is empty space so that when what you clamp extends off to infinity, that it's empty space, like you want. – Alan Wolfe Apr 17 '17 at 22:52
• Now it make sense. I keep seeing some weird artifact at the space of after the clamp that I don't understand, that is because my clamp just touches the space with part of the object, and that object will extend to infinity. Do you mind to incorporate your comment into the answer? So that people will have a clearer picture when they read the answer. – sooon Apr 18 '17 at 0:20