Repository navigation
Expand file tree
/
Copy pathseascape.glsl
More file actions
202 lines (159 loc) · 4.57 KB
/
Copy pathseascape.glsl
File metadata and controls
202 lines (159 loc) · 4.57 KB
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
// Standalone GLSL for use with shader.cpp host
uniform float iTime;
uniform vec3 iResolution;
// Procedural value-noise replacement for iChannel0 lookups.
float hash2( vec2 p ) {
p = fract(p * vec2(0.1031, 0.1030));
p += dot(p, p.yx + 19.19);
return fract((p.x + p.y) * p.x);
}
float vnoise2( vec2 p ) {
vec2 ip = floor(p);
vec2 f = fract(p);
f = f*f*(3.0-2.0*f);
float a = hash2(ip + vec2(0,0));
float b = hash2(ip + vec2(1,0));
float c = hash2(ip + vec2(0,1));
float d = hash2(ip + vec2(1,1));
return mix(mix(a,b,f.x), mix(c,d,f.x), f.y);
}
float cosNoise( in vec2 p )
{
return 0.5*( sin(p.x) + sin(p.y) );
}
const mat2 m2 = mat2(1.6,-1.2,
1.2, 1.6);
float sdTorus( vec3 p, vec2 t )
{
return length( vec2(length(p.xz)-t.x,p.y) )-t.y;
}
float smin( float a, float b, float k )
{
float h = clamp( 0.5 + 0.5*(b-a)/k, 0.0, 1.0 );
return mix( b, a, h ) - k*h*(1.0-h);
}
float map( in vec3 pos )
{
float h = 0.0;
vec2 q = pos.xz*0.5;
float s = 0.5;
for( int i=0; i<6; i++ )
{
h += s*cosNoise( q );
q = m2*q*0.85;
q += vec2(2.41,8.13);
s *= 0.48 + 0.2*h;
}
h *= 3.0;
float d1 = pos.y - h;
// rings
vec3 r1 = mod(2.3+pos+5.0,10.0)-5.0;
r1.y = pos.y-0.1 - 0.7*h;
float d2 = sdTorus( r1.xzy, vec2(1.0,0.05) );
return smin( d1, d2, 1.0 );
}
float mapH( in vec3 pos )
{
float h = 0.0;
vec2 q = pos.xz*0.5;
float s = 0.5;
for( int i=0; i<12; i++ )
{
h += s*cosNoise( q );
q = m2*q*0.85;
q += vec2(2.41,8.13);
s *= 0.48 + 0.2*h;
}
h *= 3.0;
float d1 = pos.y - h;
// rings
vec3 r1 = mod(2.3+pos+5.0,10.0)-5.0;
r1.y = pos.y-0.1 - 0.7*h;
float d2 = sdTorus( r1.xzy, vec2(1.0,0.05) );
return smin( d1, d2, 1.0 );
}
vec3 calcNormal( in vec3 pos )
{
vec2 e = vec2(1.0,-1.0) * 0.001;
return normalize( e.xyy*mapH( pos + e.xyy ) +
e.yyx*mapH( pos + e.yyx ) +
e.yxy*mapH( pos + e.yxy ) +
e.xxx*mapH( pos + e.xxx ) );
}
float softShadows( in vec3 ro, in vec3 rd )
{
float res = 1.0;
float t = 0.01;
for( int i=0; i<64; i++ )
{
vec3 pos = ro + rd*t;
float h = map( pos );
res = min( res, max(h,0.0)*164.0/t );
if( res<0.001 ) break;
t += h*0.5;
}
return res;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = fragCoord.xy / iResolution.xy;
vec2 q = (-iResolution.xy + 2.0* fragCoord.xy) / iResolution.y;
float ani = iTime*0.5;
// ray
vec3 ro = vec3( 0.0, 2.5, -ani*0.5 );
float roll = 0.2*sin(0.1*ani);
q = mat2( cos(roll), -sin(roll), sin(roll), cos(roll) ) * q;
vec3 rd = normalize( vec3(q-vec2(-0.4,0.4),-2.0) );
vec3 col = vec3( 0.7, 0.8, 1.0 );
col *= 1.0 - 0.5*rd.y;
// raymarcher
float tmax = 120.0;
float t = 0.0;
for( int i=0; i<200; i++ )
{
vec3 pos = ro + rd*t;
float h = map( pos );
if( h<0.001 || t>tmax ) break;
t += h*0.5;
}
vec3 light = normalize( vec3( 1.0, 0.5, -1.0) );
// hit
if( t<tmax )
{
// shade and light
vec3 pos = ro + t*rd;
vec3 nor = calcNormal( pos );
float bak = clamp( dot(nor,normalize(-vec3(light.x,0.0,light.z))), 0.0, 1.0 );
float dif = clamp( dot(nor,light), 0.0, 1.0 );
float sha =softShadows( pos+nor*.01, light );
vec3 lig = vec3(2.0,1.5,1.0)*dif*1.5*sha;
lig += vec3(0.2,0.3,0.4)*max(nor.y,0.0)*0.9;
lig += vec3(0.1,0.1,0.1)*bak*0.5;
vec3 mate = vec3(0.3,0.3,0.3)*0.5;
mate = mix( mate, vec3(0.2,0.15,0.1)*0.73, smoothstep( 0.7,0.9,nor.y) );
mate *= 0.5 + vnoise2( 0.5*pos.xz )*1.0;
col = mate * lig;
float fog = exp( -0.0015*t*t );
col *= fog;
col += (1.0-fog)*vec3(0.5,0.6,0.7);
}
float sun = clamp( dot(rd,light), 0.0, 1.0 );
col += vec3(1.0,0.8,0.6)*0.4*pow(sun,16.0);
col += vec3(1.0,0.8,0.6)*0.3*pow(sun,8.0);
col = sqrt( col );
col *= 0.5 + 0.5*pow(16.0*p.x*p.y*(1.0-p.x)*(1.0-p.y),0.2);
col = smoothstep( 0.0, 1.0, col );
col = mix( col, vec3(dot(col,vec3(0.33))), -0.25 );
fragColor = vec4( col, 1.0 );
}
void main() {
// Vertical scanline skip: every other column black, saves 50% GPU
vec2 fc = gl_FragCoord.xy;
if (mod(floor(fc.x), 2.0) < 1.0) {
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
return;
}
vec4 fragColor;
mainImage(fragColor, fc);
gl_FragColor = fragColor;
}