片段着色器代码:
float norm( float t, float a, float b ){
return (t-a) / (b-a);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord/iResolution.xy;
//needle: the value we want to evaluate to create slices
float needle = uv.x;// or fract( uv.y * 2.5); // anything normalised
//slices count (must be const)
const int total = int( 30. );
float values[total];
//store the sum
float sum = 0.;
for( int i = 0; i < total; i++ ){
float r = float( i );
values[i] = sum;
sum = r;
}
//normalize values (divide by the sum)
for( int i = 0; i < total; i++ ){
values[i] /= sum;
}
//retrieve the needle's index
int id = 0;
for( int i = 0; i < total; i++ ){
if( values[i] < needle ){
id = i;
}
}
//find edges to lerp between
float u = values[id];
float v = 1.;
if( id < total -1 ){
v = values[id+1 ];
}
//normalise result
float t = norm( needle, u, v);
fragColor = vec4(vec3(1.-t),1.0);
}
结果: