-
Notifications
You must be signed in to change notification settings - Fork 73
/
clouds.shader
254 lines (199 loc) · 7.81 KB
/
clouds.shader
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
The MIT License (MIT)
Copyright (c) 2016 Huw Bowles, Daniel Zimmermann, Beibei Wang
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//
// For diagram shader showing how samples are taken:
//
// https://www.shadertoy.com/view/llcGRS
//
// We are in the process of writing up this technique. The following github repos
// is the home of this research.
//
// https://github.com/huwb/volsample
//
//
//
// Additional credits - this scene is mostly mash up of these two amazing shaders:
//
// Clouds by iq: https://www.shadertoy.com/view/XslGRr
// Cloud Ten by nimitz: https://www.shadertoy.com/view/XtS3DD
//
#define SAMPLE_COUNT 32
#define PERIOD 1.
// mouse toggle
bool STRUCTURED = true;
// cam moving in a straight line
vec3 lookDir = vec3(cos(.53*iGlobalTime),0.,sin(iGlobalTime));
vec3 camVel = vec3(-20.,0.,0.);
float zoom = 1.2; // 1.5;
vec3 sundir = normalize(vec3(-1.0,0.0,-1.));
// LUT based 3d value noise
float noise( in vec3 x )
{
vec3 p = floor(x);
vec3 f = fract(x);
f = f*f*(3.0-2.0*f);
vec2 uv = (p.xy+vec2(37.0,17.0)*p.z) + f.xy;
vec2 rg = texture2D( iChannel0, (uv+ 0.5)/256.0, -100.0 ).yx;
return mix( rg.x, rg.y, f.z );
}
vec4 map( in vec3 p )
{
float d = 0.1 + .8 * sin(0.6*p.z)*sin(0.5*p.x) - p.y;
vec3 q = p;
float f;
f = 0.5000*noise( q ); q = q*2.02;
f += 0.2500*noise( q ); q = q*2.03;
f += 0.1250*noise( q ); q = q*2.01;
f += 0.0625*noise( q );
d += 2.75 * f;
d = clamp( d, 0.0, 1.0 );
vec4 res = vec4( d );
vec3 col = 1.15 * vec3(1.0,0.95,0.8);
col += vec3(1.,0.,0.) * exp2(res.x*10.-10.);
res.xyz = mix( col, vec3(0.7,0.7,0.7), res.x );
return res;
}
// to share with unity hlsl
#define float2 vec2
#define float3 vec3
#define fmod mod
float mysign( float x ) { return x < 0. ? -1. : 1. ; }
float2 mysign( float2 x ) { return float2( x.x < 0. ? -1. : 1., x.y < 0. ? -1. : 1. ) ; }
// compute ray march start offset and ray march step delta and blend weight for the current ray
void SetupSampling( out float2 t, out float2 dt, out float2 wt, in float3 ro, in float3 rd )
{
if( !STRUCTURED )
{
dt = float2(PERIOD,PERIOD);
t = dt;
wt = float2(0.5,0.5);
return;
}
// structured sampling pattern line normals
float3 n0 = (abs( rd.x ) > abs( rd.z )) ? float3(1., 0., 0.) : float3(0., 0., 1.); // non diagonal
float3 n1 = float3(mysign( rd.x * rd.z ), 0., 1.); // diagonal
// normal lengths (used later)
float2 ln = float2(length( n0 ), length( n1 ));
n0 /= ln.x;
n1 /= ln.y;
// some useful DPs
float2 ndotro = float2(dot( ro, n0 ), dot( ro, n1 ));
float2 ndotrd = float2(dot( rd, n0 ), dot( rd, n1 ));
// step size
float2 period = ln * PERIOD;
dt = period / abs( ndotrd );
// dist to line through origin
float2 dist = abs( ndotro / ndotrd );
// raymarch start offset - skips leftover bit to get from ro to first strata lines
t = -mysign( ndotrd ) * fmod( ndotro, period ) / abs( ndotrd );
if( ndotrd.x > 0. ) t.x += dt.x;
if( ndotrd.y > 0. ) t.y += dt.y;
// sample weights
float minperiod = PERIOD;
float maxperiod = sqrt( 2. )*PERIOD;
wt = smoothstep( maxperiod, minperiod, dt/ln );
wt /= (wt.x + wt.y);
}
vec4 raymarch( in vec3 ro, in vec3 rd )
{
vec4 sum = vec4(0, 0, 0, 0);
// setup sampling - compute intersection of ray with 2 sets of planes
float2 t, dt, wt;
SetupSampling( t, dt, wt, ro, rd );
// fade samples at far extent
float f = .6; // magic number - TODO justify this
float endFade = f*float(SAMPLE_COUNT)*PERIOD;
float startFade = .8*endFade;
for(int i=0; i<SAMPLE_COUNT; i++)
{
if( sum.a > 0.99 ) continue;
// data for next sample
vec4 data = t.x < t.y ? vec4( t.x, wt.x, dt.x, 0. ) : vec4( t.y, wt.y, 0., dt.y );
// somewhat similar to: https://www.shadertoy.com/view/4dX3zl
//vec4 data = mix( vec4( t.x, wt.x, dt.x, 0. ), vec4( t.y, wt.y, 0., dt.y ), float(t.x > t.y) );
vec3 pos = ro + data.x * rd;
float w = data.y;
t += data.zw;
// fade samples at far extent
w *= smoothstep( endFade, startFade, data.x );
vec4 col = map( pos );
// iqs goodness
float dif = clamp((col.w - map(pos+0.6*sundir).w)/0.6, 0.0, 1.0 );
vec3 lin = vec3(0.51, 0.53, 0.63)*1.35 + 0.55*vec3(0.85, 0.57, 0.3)*dif;
col.xyz *= lin;
col.xyz *= col.xyz;
col.a *= 0.5;
col.rgb *= col.a;
// integrate
sum += col * (1.0 - sum.a) * w;
}
sum.xyz /= (0.001+sum.w);
return clamp( sum, 0.0, 1.0 );
}
vec3 sky( vec3 rd )
{
vec3 col = vec3(0.);
float hort = 1. - clamp(abs(rd.y), 0., 1.);
col += 0.5*vec3(.99,.5,.0)*exp2(hort*8.-8.);
col += 0.1*vec3(.5,.9,1.)*exp2(hort*3.-3.);
col += 0.55*vec3(.6,.6,.9);
float sun = clamp( dot(sundir,rd), 0.0, 1.0 );
col += .2*vec3(1.0,0.3,0.2)*pow( sun, 2.0 );
col += .5*vec3(1.,.9,.9)*exp2(sun*650.-650.);
col += .1*vec3(1.,1.,0.1)*exp2(sun*100.-100.);
col += .3*vec3(1.,.7,0.)*exp2(sun*50.-50.);
col += .5*vec3(1.,0.3,0.05)*exp2(sun*10.-10.);
float ax = atan(rd.y,length(rd.xz))/1.;
float ay = atan(rd.z,rd.x)/2.;
float st = texture2D( iChannel0, vec2(ax,ay) ).x;
float st2 = texture2D( iChannel0, .25*vec2(ax,ay) ).x;
st *= st2;
st = smoothstep(0.65,.9,st);
col = mix(col,col+1.8*st,clamp(1.-1.1*length(col),0.,1.));
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// click mouse to use naive raymarching
if( iMouse.z > 0. )
STRUCTURED = false;
vec2 q = fragCoord.xy / iResolution.xy;
vec2 p = -1.0 + 2.0*q;
p.x *= iResolution.x/ iResolution.y;
vec2 mo = -1.0 + 2.0*iMouse.xy / iResolution.xy;
// camera
vec3 ro = vec3(0.,1.5,0.) + iGlobalTime*camVel;
vec3 ta = ro + lookDir; //vec3(ro.x, ro.y, ro.z-1.);
vec3 ww = normalize( ta - ro);
vec3 uu = normalize(cross( vec3(0.0,1.0,0.0), ww ));
vec3 vv = normalize(cross(ww,uu));
vec3 rd = normalize( p.x*uu + 1.2*p.y*vv + 1.5*ww );
// divide by forward component to get fixed z layout instead of fixed dist layout
//vec3 rd_layout = rd/mix(dot(rd,ww),1.0,samplesCurvature);
vec4 clouds = raymarch( ro, rd );
vec3 col = clouds.xyz;
// sky if visible
if( clouds.w <= 0.99 )
col = mix( sky(rd), col, clouds.w );
col = clamp(col, 0., 1.);
col = smoothstep(0.,1.,col);
col *= pow( 16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y), 0.12 ); //Vign
fragColor = vec4( col, 1.0 );
}