-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshadertoy_sea.py
216 lines (173 loc) · 5.52 KB
/
shadertoy_sea.py
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
from wgpu_shadertoy import Shadertoy
# shadertoy source: https://www.shadertoy.com/view/Ms2SD1 by TDM
# ported to wgsl and modified
shader_code = """
/*
* "Seascape" by Alexander Alekseev aka TDM - 2014
* License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
* Contact: [email protected]
*/
const NUM_STEPS = 8;
const PI = 3.141592;
const EPSILON = 0.001;
const ITER_GEOMETRY = 3;
const ITER_FRAGMENT = 5;
const SEA_HEIGHT = 0.6;
const SEA_CHOPPY = 4.0;
const SEA_SPEED = 0.8;
const SEA_FREQ = 0.16;
const SEA_BASE = vec3<f32>(0.0,0.09,0.18);
const SEA_WATER_COLOR = vec3<f32>(0.48, 0.54, 0.36);
// const octave_m = mat2x2<f32>(1.6, 1.2, -1.2, 1.6);
fn hash( p : vec2<f32> ) -> f32 {
// let h = dot(p,vec2<f32>(127.1,311.7)); // percession issue?
let h = dot(p,vec2<f32>(1.0,113.0));
return fract(sin(h)*43758.5453123);
// return fract(sin(h)); // Use the magic number 43758.5453123 seems to cause some percession issue?
}
// another hash function
// fn hash(p: vec2<f32>) -> f32 {
// var p3 = fract(vec3<f32>(p.xyx) * 0.1031);
// p3 += vec3<f32>( dot(p3, p3.yzx + vec3<f32>(33.33)) );
// return fract((p3.x + p3.y) * p3.z);
// }
// Perlin noise, TODO: try simplex noise
fn noise( p : vec2<f32> ) -> f32 {
let i = floor(p);
let f = fract(p);
let u = f * f * (3.0 - 2.0 * f);
let mix1 = mix( hash( i + vec2<f32>(0.0,0.0) ), hash( i + vec2<f32>(1.0,0.0) ), u.x);
let mix2 = mix( hash( i + vec2<f32>(0.0,1.0) ), hash( i + vec2<f32>(1.0,1.0) ), u.x);
let mix3 = mix(mix1, mix2, u.y);
return -1.0 + 2.0 * mix3;
}
// lighting
fn diffuse( n : vec3<f32>, l : vec3<f32>, p : f32 ) -> f32 {
return pow(dot(n,l) * 0.4 + 0.6, p);
}
fn specular( n : vec3<f32>, l : vec3<f32>, e : vec3<f32>, s : f32 ) -> f32 {
let nrm = (s + 8.0) / (PI * 8.0);
return pow(max(dot(reflect(e,n),l),0.0),s) * nrm;
}
// sky
fn getSkyColor( e_ : vec3<f32> ) -> vec3<f32> {
var e = e_;
e.y = (max(e.y,0.0) * 0.8 + 0.2) * 0.8;
return vec3<f32>(pow(1.0-e.y, 2.0), 1.0-e.y, 0.6+(1.0-e.y)*0.4) * 1.1;
}
// sea
fn sea_octave( uv_ : vec2<f32>, choppy : f32 ) -> f32 {
let uv = uv_ + noise(uv_);
var wv = 1.0-abs(sin(uv));
let swv = abs(cos(uv));
wv = mix(wv,swv,wv);
return pow(1.0-pow(wv.x * wv.y,0.65),choppy);
}
fn _map( p : vec3<f32>, iter: i32 ) -> f32 {
var freq = SEA_FREQ;
var amp = SEA_HEIGHT;
var choppy = SEA_CHOPPY;
let sea_time = 1.0 + i_time * SEA_SPEED;
var uv = p.xz;
uv.x *= 0.75;
var d = 0.0;
var h = 0.0;
for (var i = 0; i < iter; i+=1) {
d = sea_octave((uv+sea_time)*freq, choppy);
d += sea_octave((uv-sea_time)*freq, choppy);
h += d * amp;
uv *= mat2x2<f32>(1.6, 1.2, -1.2, 1.6);
freq *= 1.9;
amp *= 0.22;
choppy = mix(choppy,1.0,0.2);
}
return p.y - h;
}
fn map( p : vec3<f32>) -> f32 {
return _map(p, ITER_GEOMETRY);
}
fn map_detailed( p : vec3<f32> ) -> f32 {
return _map(p, ITER_FRAGMENT);
}
fn getSeaColor( p : vec3<f32>, n : vec3<f32>, l : vec3<f32>, eye : vec3<f32>, dist : vec3<f32> ) -> vec3<f32> {
var fresnel = clamp(1.0 - dot(n,-eye), 0.0, 1.0);
fresnel = pow(fresnel,3.0) * 0.5;
let reflected = getSkyColor(reflect(eye,n));
let refracted = SEA_BASE + diffuse(n,l,80.0) * SEA_WATER_COLOR * 0.12;
var color = mix(refracted,reflected,fresnel);
let atten = max(1.0 - dot(dist,dist) * 0.001, 0.0);
color += SEA_WATER_COLOR * (p.y - SEA_HEIGHT) * 0.18 * atten;
color += vec3<f32>(specular(n, l, eye, 60.0));
return color;
}
// tracing
fn getNormal( p : vec3<f32>, eps : f32 ) -> vec3<f32> {
var n : vec3<f32>;
n.y = map_detailed(p);
n.x = map_detailed(vec3<f32>(p.x+eps,p.y,p.z)) - n.y;
n.z = map_detailed(vec3<f32>(p.x,p.y,p.z+eps)) - n.y;
n.y = eps;
return normalize(n);
}
fn heightMapTracing( ori : vec3<f32>, dir : vec3<f32> ) -> vec3<f32> {
var tm = 0.0;
var tx = 1000.0;
var hx = map(ori + dir * tx);
var p : vec3<f32>;
if (hx > 0.0){
p = ori + dir * tx;
return p;
}
var hm = map(ori + dir * tm);
var tmid = 0.0;
for (var i = 0; i < NUM_STEPS; i+=1) {
tmid = mix(tm,tx, hm/(hm-hx));
p = ori + dir * tmid;
let hmid = map(p);
if (hmid < 0.0) {
tx = tmid;
hx = hmid;
} else {
tm = tmid;
hm = hmid;
}
}
return p;
}
fn getPixel( coord: vec2<f32>, time: f32 ) -> vec3<f32> {
var uv = coord / i_resolution.xy;
uv = uv * 2.0 - 1.0;
uv.x *= i_resolution.x / i_resolution.y;
// ray
let ori = vec3<f32>(0.0,3.5,time*5.0);
let dir = normalize(vec3<f32>(uv.xy,-2.0));
// tracing
var p = heightMapTracing(ori, dir);
let dist = p - ori;
let n = getNormal(p, dot(dist,dist) * (0.1/i_resolution.x));
let light = normalize(vec3<f32>(0.0,1.0,0.8));
// color
return mix(
getSkyColor(dir),
getSeaColor(p,n,light,dir,dist),
pow(smoothstep(0.0,-0.02,dir.y),0.2)
);
}
fn shader_main(frag_coord: vec2<f32>) -> vec4<f32> {
let time = i_time * 0.3 + i_mouse.x * 0.01;
var color: vec3<f32>;
for (var i = -1; i <=1; i+=1) {
for (var j =-1; j <=1; j+=1) {
let uv = frag_coord + vec2<f32>(f32(i),f32(j)) / 3.0;
color += getPixel(uv, time);
}
}
color = color / 9.0;
color = getPixel(frag_coord, time);
// post
return vec4<f32>(pow(color, vec3<f32>(0.65)), 1.0);
}
"""
shader = Shadertoy(shader_code)
if __name__ == "__main__":
shader.show()