SHADER 学习日志

SHADER 学习日志

smoothstep 发光

image-20231225195921548
1
2
3
4
5
6
7
8
9
10
11
12
13
vec2 fixUV(in vec2 c) {
return (2. * c - iResolution.xy) / min(iResolution.x, iResolution.y);
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fixUV(fragCoord);
vec3 col = vec3( 0.9 , 0.65 , 0.1 ) ;
float d= length(uv);
col = mix(col.rgb , vec3(0. ) , smoothstep( 0.5 ,1. , d ) );
fragColor = vec4(col,1.0);
}
image-20231225201133149
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
float remap01( float a , float b , float t ){

return clamp( (t-a)/(b-a), 0. , 1. );
}


vec2 fixUV(in vec2 c) {
return (2. * c - iResolution.xy) / min(iResolution.x, iResolution.y);
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fixUV(fragCoord);
vec3 col = vec3( 0.9 , 0.65 , 0.1 ) ;
float d= length(uv);


float edgeShader = remap01( 0.45 , 0.8, d );
col *= edgeShader;


col = mix( col , vec3( 0.) , step( 0.8 , d ) ) ;
fragColor = vec4(col,1.0);
}

映射

1
2
3
4
5
6
7
8
9
10
11
12
13
// t ∈ [a,b] 。 输出 0 ~ 1
float remap01( float a , float b , float t ){
return clamp( (t-a)/(b-a), 0. , 1. );
}

// t ∈ [a,b] 。 把[a,b] 区间的数值映射到 [c,d]区间 。 [a,b] → [c,d]
float remap ( float a , float b , float c , float d , float t ){


return remap01( a , b , t ) * ( d - c ) + c;

}

image-20231225202006049
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
float remap01( float a , float b , float t ){

return clamp( (t-a)/(b-a), 0. , 1. );
}


vec2 fixUV(in vec2 c) {
return (2. * c - iResolution.xy) / min(iResolution.x, iResolution.y);
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fixUV(fragCoord);
vec3 col = vec3( 0.9 , 0.65 , 0.1 ) ;
float d= length(uv);


float edgeShader = remap01( 0.45 , 0.8, d );
col *= 1. - edgeShader;


// col = mix( col , vec3( 0.) , step( 0.8 , d ) ) ;
fragColor = vec4(col,1.0);
}
image-20231225214201831
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
float remap01( float a , float b , float t ){

return clamp( (t-a)/(b-a), 0. , 1. );
}


float remap ( float a , float b , float c , float d , float t ){


return remap01( a , b , t ) * ( d - c ) + c;

}


vec2 fixUV(in vec2 c) {
return (2. * c - iResolution.xy) / min(iResolution.x, iResolution.y);
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fixUV(fragCoord);
vec3 col = vec3( 0.9 , 0.65 , 0.1 ) ;
float d= length(uv);


float highLight = remap ( -1. , 1. , 0. , 0.5 , uv.y );





// col = mix( col , vec3( 0.) , step( 0.8 , d ) ) ;
fragColor = vec4(col * highLight,1.0);
}

局部坐标

1
2
3
vec2 within(vec2 uv , vec4 rect){
return vec2(( uv - rect.xy ) / ( rect.zw - rect.xy ) );
}
image-20231225215813487
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
vec2 within(vec2 uv , vec4 rect){

return vec2(( uv - rect.xy ) / ( rect.zw - rect.xy ) );

}

vec2 fixUV(in vec2 c) {
return (2. * c - iResolution.xy) / min(iResolution.x, iResolution.y);
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fixUV(fragCoord);
vec3 col = vec3( 0.9 , 0.65 , 0.1 ) ;




vec2 locUv = within(uv , vec4( 0.3 , 0.3 , 0.8 , 0.8 ));

float d= length(locUv);

col = mix( col , vec3( 0.) , step( 0.8 , d ) ) ;
fragColor = vec4(col ,1.0);
}

SHADER 学习日志
http://example.com/2024/05/23/webgl/SHADER 学习日志/
Author
John Doe
Posted on
May 23, 2024
Licensed under