-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial_iq_happy_jumping.glsl
221 lines (170 loc) · 4.81 KB
/
tutorial_iq_happy_jumping.glsl
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Tutorial from https://www.youtube.com/watch?v=Cfe5UQ-1L9Q
// Thanks iq!
// Shadertoy defines
#define O gl_FragColor
#define U gl_FragCoord.xy
#define iGlobalTime iTime
#define R iResolution.xy
float sdSphere( in vec3 pos, float rad )
{
return length(pos) - rad;
}
float sdStick( in vec3 p, vec3 a, vec3 b, float ra, float rb )
{
vec3 ba = b-a;
vec3 pa = p-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0);
float r = mix( ra, rb, h );
return length(pa-h*ba) - r;
}
float sdElipsoid( in vec3 pos, vec3 rad )
{
float k0 = length(pos/rad);
float k1 = length(pos/rad/rad);
return k0*(k0-1.0)/k1;
}
float smin( in float a, in float b, float k )
{
float h = max( k - abs(a-b), 0.0 );
return min(a,b) - h*h/(k*4.0);
}
float smax( in float a, in float b, float k )
{
float h = max( k - abs(a-b), 0.0 );
return max(a,b) + h*h/(k*4.0);
}
vec2 sdGuy( in vec3 pos )
{
float t = fract(iTime);
float y = 4.0*t*(1.0-t);
float dy = 4.0*(1.0-2.0*t);
vec2 u = normalize(vec2( 1.0, -dy ));
vec2 v = vec2( dy, 1.0 );
vec3 cen = vec3(0.0,y,0.0);
float sy = 0.5 + 0.5*y;
float sz = 1.0/sy;
vec3 rad = vec3(0.25,0.25*sy,0.25*sz);
vec3 q = pos-cen;
//q.yz = vec2( dot(u,q.yz), dot(v,q.yz) );
float d = sdElipsoid(q,rad);
vec3 h = q;
vec3 sh = vec3( abs(h.x), h.yz );
// head
float d2 = sdElipsoid(h - vec3(0.0,0.28,0.0),vec3(0.15,0.2,0.23));
float d3 = sdElipsoid(h - vec3(0.0,0.28,-0.1),vec3(0.23,0.2,0.2));
d2 = smin(d2,d3,0.05);
d = smin(d, d2, 0.1);
// eyebrows
vec3 eb = sh-vec3(0.12,0.34,0.15);
eb.xy = (mat2(3,4,-4,3)/5.0)*eb.xy;
d2 = sdElipsoid(eb,vec3(0.06,0.035,0.05));
d = smin(d, d2, 0.04);
// mouth
d2 = sdElipsoid(h-vec3(0.0,0.15 + 3.*h.x*h.x,0.15),vec3(0.1,0.035,.2));
d = smax(d,-d2,0.03);
// ears
d2 = sdStick( sh, vec3(0.1, 0.4, -0.01), vec3(0.2,0.55,0.02),0.01,0.03);
d = smin(d,d2,0.03);
vec2 res = vec2(d,2.0);
// eye
float d4 = sdSphere( sh - vec3(0.08,0.28,0.16), 0.05 );
if( d4<d ) res = vec2(d4, 3.0);
d4 = sdSphere(sh - vec3(0.09,0.28,0.18), 0.02);
if( d4<d ) res = vec2(d4, 4.0);
d = min(d,d4);
return res;
}
vec2 map( in vec3 pos )
{
float cr = 0.10;
vec2 d1 = sdGuy(pos);
float d2 = pos.y - (-0.25);
return (d2<d1.x) ? vec2(d2,1.0) : d1;
}
vec3 calcNormal( in vec3 pos )
{
vec2 e = vec2(0.0001, 0.0);
return normalize( vec3(map(pos+e.xyy).x-map(pos-e.xyy).x,
map(pos+e.yxy).x-map(pos-e.yxy).x,
map(pos+e.yyx).x-map(pos-e.yyx).x ) );
}
float castShadow( in vec3 ro, vec3 rd )
{
float res = 1.0;
float t = 0.001;
for( int i=0; i<100; i++ )
{
vec3 pos = ro + t*rd;
float h = map( pos ).x;
res = min( res, 16.0*h/t );
if ( res<0.0001 ) break;
t += h;
if( t > 20.0 ) break;
}
return clamp(res,0.0,1.0);
}
vec2 castRay( in vec3 ro, vec3 rd )
{
float m = -1.0;
float t = 0.0;
for( int i=0; i<100; i++ )
{
vec3 pos = ro + t*rd;
vec2 h = map( pos );
m = h.y;
if( h.x<0.0001 )
break;
t += h.x;
if( t>20.0 )
break;
}
if( t>20.0 ) m=-1.0;
return vec2(t,m);
}
void main() {
float time = iTime;
vec2 p = (2.*U-R)/R.y;
float an = 10.0*iMouse.x/iResolution.x;
vec3 ta = vec3(0.0,0.5,1.0);
vec3 ro = ta + vec3(1.5*sin(an),0.0,1.5*cos(an));
vec3 ww = normalize( ta-ro );
vec3 uu = normalize( cross(ww, vec3(0,1,0)) );
vec3 vv = normalize( cross(uu,ww) );
vec3 rd = normalize( p.x*uu + p.y*vv + 1.8*ww );
vec3 col = vec3(0.4,0.75,1.0) - 0.5*rd.y;
col = mix( col, vec3(0.7,0.75,0.8), exp(-10.0*rd.y) );
vec2 tm = castRay(ro, rd);
if( tm.y>0.0 )
{
float t = tm.x;
vec3 pos = ro + t*rd;
vec3 nor = calcNormal(pos);
vec3 mate = vec3(0.18);
if( tm.y < 1.5 )
{
mate = vec3(0.05,0.1,0.02);
}
else if( tm.y<2.5 )
{
mate = vec3(0.2,0.1,0.02);
}
else if( tm.y<3.5 )
{
mate = vec3(0.9);
}
else if( tm.y<4.5 )
{
mate = vec3(0.02);
}
vec3 sun_dir = normalize( vec3(0.8,0.4,0.2) );
float sun_dif = clamp( dot(nor,sun_dir),0.0,1.0);
float sun_sha = castShadow( pos+nor*0.001, sun_dir );
float sky_dif = clamp( 0.5 + 0.5*dot(nor,vec3(0.0,1.0,0.0)), 0.0, 1.0);
float bou_dif = clamp( 0.5 + 0.5*dot(nor,vec3(0.0,-1.0,0.0)), 0.0, 1.0);
col = mate*vec3(7.0,4.5,3.0)*sun_dif*sun_sha;
col += mate*vec3(0.5,0.8,0.9)*sky_dif;
col += mate*vec3(0.7,0.3,0.2)*bou_dif;
}
col = pow( col, vec3(0.4545) );
O = vec4(col, 1.0);
}