Repository navigation
Expand file tree
/
Copy pathfirespikes.glsl
More file actions
214 lines (172 loc) · 5.54 KB
/
Copy pathfirespikes.glsl
File metadata and controls
214 lines (172 loc) · 5.54 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
203
204
205
206
207
208
209
210
211
212
213
214
// Based on Shanes' Fiery Spikeball https://www.shadertoy.com/view/4lBXzy (I think that his implementation is more understandable than the original :) )
// Relief come from Siggraph workshop by Beautypi/2015 https://www.shadertoy.com/view/MtsSRf
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0
// Converted to standalone GLSL for use with shader.cpp host
uniform float iTime;
uniform vec3 iResolution;
//#define ULTRAVIOLET
#define DITHERING
#define pi 3.14159265
#define R(p, a) p=cos(a)*p+sin(a)*vec2(p.y, -p.x)
// Procedural value-noise replacement for IQ's texture-based noise.
float hash3( vec3 p ) {
p = fract(p * vec3(0.1031, 0.1030, 0.0973));
p += dot(p, p.yzx + 19.19);
return fract((p.x + p.y) * p.z);
}
float pn( in vec3 p )
{
vec3 ip = floor(p);
vec3 f = fract(p);
f = f*f*(3.0-2.0*f);
float n000 = hash3(ip + vec3(0,0,0));
float n100 = hash3(ip + vec3(1,0,0));
float n010 = hash3(ip + vec3(0,1,0));
float n110 = hash3(ip + vec3(1,1,0));
float n001 = hash3(ip + vec3(0,0,1));
float n101 = hash3(ip + vec3(1,0,1));
float n011 = hash3(ip + vec3(0,1,1));
float n111 = hash3(ip + vec3(1,1,1));
float nx00 = mix(n000, n100, f.x);
float nx10 = mix(n010, n110, f.x);
float nx01 = mix(n001, n101, f.x);
float nx11 = mix(n011, n111, f.x);
float nxy0 = mix(nx00, nx10, f.y);
float nxy1 = mix(nx01, nx11, f.y);
return mix(nxy0, nxy1, f.z);
}
// FBM (reduced from 3 octaves to 2 to lighten GPU load)
float fpn(vec3 p) {
return pn(p*.06125)*.65 + pn(p*.25)*.30;
}
float rand(vec2 co){// implementation found at: lumina.sourceforge.net/Tutorials/Noise.html
return fract(sin(dot(co*0.123,vec2(12.9898,78.233))) * 43758.5453);
}
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*1.2,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 SunSurface( in vec3 pos )
{
float h = 0.0;
vec2 q = pos.xz*0.5;
float s = 0.5;
float d2 = 0.0;
for( int i=0; i<4; i++ )
{
h += s*cosNoise( q );
q = m2*q*0.85;
q += vec2(2.41,8.13);
s *= 0.48 + 0.2*h;
}
h *= 2.0;
float d1 = pos.y - h;
// rings
vec3 r1 = mod(2.3+pos+1.0,10.0)-5.0;
r1.y = pos.y-0.1 - 0.7*h + 0.5*sin( 3.0*iTime+pos.x + 3.0*pos.z);
float c = cos(pos.x); float s1 = 1.0;//sin(pos.x);
r1.xz=c*r1.xz+s1*vec2(r1.z, -r1.x);
d2 = sdTorus( r1.xzy, vec2(clamp(abs(pos.x/pos.z),0.7,2.5), 0.20) );
return smin( d1, d2, 1.0 );
}
float map(vec3 p) {
p.z += 1.;
R(p.yz, -25.5);
R(p.xz, iTime*0.05);
return SunSurface(p) + fpn(p*50.+iTime*25.) * 0.45;
}
// See "Combustible Voronoi"
// https://www.shadertoy.com/view/4tlSzl
vec3 firePalette(float i){
float T = 1400. + 1300.*i; // Temperature range (in Kelvin).
vec3 L = vec3(7.4, 5.6, 4.4); // Red, green, blue wavelengths (in hundreds of nanometers).
L = pow(L,vec3(5.0)) * (exp(1.43876719683e5/(T*L))-1.0);
return 1.0-exp(-5e8/L); // Exposure level. Set to "50." For "70," change the "5" to a "7," etc.
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// p: position on the ray
// rd: direction of the ray
vec3 rd = normalize(vec3((gl_FragCoord.xy-0.5*iResolution.xy)/iResolution.y, 1.));
vec3 ro = vec3(0., 0., -28.);
// ld, td: local, total density
// w: weighting factor
float ld=0., td=0., w=0.;
// t: length of the ray
// d: distance function
float d=1., t=1.;
// Distance threshold.
const float h = .1;
// total color
vec3 tc = vec3(0.);
#ifdef DITHERING
vec2 pos = ( fragCoord.xy / iResolution.xy );
vec2 seed = pos + fract(iTime);
//t=(1.+0.2*rand(seed));
#endif
// rm loop
for (int i=0; i<36; i++) {
// Loop break conditions. Seems to work, but let me
// know if I've overlooked something.
if(td>(1.-1./80.) || d<0.001*t || t>40.)break;
// evaluate distance function
d = map(ro+t*rd);
// fix some holes deep inside
//d=max(d,-.3);
// check whether we are close enough (step)
// compute local density and weighting factor
//const float h = .1;
ld = (h - d) * step(d, h);
w = (1. - td) * ld;
// accumulate color and density
tc += w*w + 1./50.; // Different weight distribution.
td += w + 1./200.;
// dithering implementation come from Eiffies' https://www.shadertoy.com/view/MsBGRh
#ifdef DITHERING
#ifdef ULTRAVIOLET
// enforce minimum stepsize
d = max(d, 0.04);
// add in noise to reduce banding and create fuzz
d=abs(d)*(1.+0.28*rand(seed*vec2(i)));
#else
// add in noise to reduce banding and create fuzz (intensified)
d=abs(d)*(.6+0.7*rand(seed*vec2(i)));
// enforce minimum stepsize
d = max(d, 0.04);
#endif
#else
// enforce minimum stepsize
d = max(d, 0.04);
#endif
// step forward
t += d*0.5;
}
// Fire palette.
tc = firePalette(tc.x);
#ifdef ULTRAVIOLET
tc *= 1. / exp( ld * 2.82 ) * 1.05;
#endif
fragColor = vec4(tc, 1.0);
}
void main() {
// Horizontal scanline skip: every other row black, saves 50% GPU
vec2 fc = gl_FragCoord.xy;
if (mod(floor(fc.y), 2.0) < 1.0) {
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
return;
}
vec4 fragColor;
mainImage(fragColor, fc);
gl_FragColor = fragColor;
}