PuzzleSDK
shaderTest.psh.h
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#define STRINGIFY(A) #A
26
27static const char* starFlame = STRINGIFY(
28
29
30 uniform vec2 center;
31 uniform vec2 resolution;
32
33
34 vec2 iResolution = resolution; // viewport resolution (in pixels)
35 float iGlobalTime = CC_Time[1]; // shader playback time (in seconds)
36 //uniform float iChannelTime[4]; // channel playback time (in seconds)
37 //uniform vec3 iChannelResolution[4]; // channel resolution (in pixels)
38 vec4 iMouse = vec4(0,0,0,0); // mouse pixel coords. xy: current (if MLB down), zw: click
39 //uniform sampler2D iChannel0; // input channel. XX = 2D/Cube
40
41 float noise(vec3 p) //Thx to Las^Mercury
42{
43 vec3 i = floor(p);
44 vec4 a = dot(i, vec3(1., 57., 21.)) + vec4(0., 57., 21., 78.);
45 vec3 f = cos((p-i)*acos(-1.))*(-.5)+.5;
46 a = mix(sin(cos(a)*a),sin(cos(1.+a)*(1.+a)), f.x);
47 a.xy = mix(a.xz, a.yw, f.y);
48 return mix(a.x, a.y, f.z);
49}
50
51 float sphere(vec3 p, vec4 spr)
52{
53 return length(spr.xyz-p) - spr.w;
54}
55
56 float flame(vec3 p)
57{
58 float d = sphere(p*vec3(1.,.5,1.), vec4(.0,-1.,.0,1.));
59 return d + (noise(p+vec3(.0,iGlobalTime*2.,.0)) + noise(p*3.)*.5)*.25*(p.y) ;
60}
61
62 float scene(vec3 p)
63{
64 return min(100.-length(p) , abs(flame(p)) );
65}
66
67 vec4 raymarch(vec3 org, vec3 dir)
68{
69 float d = 0.0 ;
70 float glow = 0.0;
71 float eps = 0.02;
72 vec3 p = org;
73 bool glowed = false;
74
75 for(int i=0; i<64; i++)
76 {
77 d = scene(p) + eps;
78 p += d * dir;
79 if( d>eps )
80 {
81 if(flame(p) < .0)
82 glowed=true;
83 if(glowed)
84 glow = float(i)/64.;
85 }
86 }
87 return vec4(p,glow);
88}
89
90 void main()
91{
92 vec2 v = -1.0 + (2.0 * gl_FragCoord.xy)/ iResolution.xy;
93 //vec2 v = 2.0 * gl_FragCoord.xy / iResolution.xy;
94 v.x *= iResolution.x/iResolution.y;
95
96 vec3 org = vec3(0., -2., 4.);
97 vec3 dir = normalize(vec3(v.x*1.6, -v.y, -1.5));
98
99 vec4 p = raymarch(org, dir);
100 float glow = p.w;
101
102 vec4 col = mix(vec4(1.,.5,.1,1.), vec4(0.1,.5,1.,1.), p.y*.02+.4);
103
104 gl_FragColor = mix(vec4(0.), col, pow(glow*2.,4.));
105 //gl_FragColor = col;
106 //gl_FragColor = mix(vec4(1.), mix(vec4(1.,.5,.1,1.),vec4(0.1,.5,1.,1.),p.y*.02+.4), pow(glow*2.,4.));
107
108}
109
110);
111
112
113
114
115static const char* starNestFrg = STRINGIFY(
116
117
118
119 uniform vec2 center;
120 uniform vec2 resolution;
121
122 vec2 iCenter = center;
123 vec2 iResolution = resolution; // viewport resolution (in pixels)
124 float iGlobalTime = CC_Time[1]; // shader playback time (in seconds)
125 //uniform float iChannelTime[4]; // channel playback time (in seconds)
126 //uniform vec3 iChannelResolution[4]; // channel resolution (in pixels)
127 vec4 iMouse = vec4(0,0,0,0); // mouse pixel coords. xy: current (if MLB down), zw: click
128 //uniform sampler2D iChannel0; // input channel. XX = 2D/Cube
129
130 // Star Nest by Pablo Román Andrioli
131
132 // This content is under the MIT License.
133
134 int iterations = 17;
135 float formuparam = 0.53;
136
137 int volsteps = 20;
138 float stepsize = 0.1;
139
140 float zoom = 0.800;
141 float tile = 0.850;
142 float speed = 0.010;
143
144 float brightness = 0.0015;
145 float darkmatter = 0.300;
146 float distfading = 0.730;
147 float saturation = 0.850;
148
149
150void main()
151{
152 // iCenter = vec2(300, 300);
153 //get coords and direction
154 vec2 uv=gl_FragCoord.xy/iResolution.xy -.5;
155 //vec2 uv=gl_FragCoord.xy/iResolution.xy ;
156 //vec2 uv = ( 2* gl_FragCoord.xy - iCenter.xy) / resolution.xy;
157 uv.y*=iResolution.y/iResolution.x;
158
159 //vec2 uv = 2.0* (gl_FragCoord.xy-iResolution.xy)/min(iResolution.y,iResolution.x);
160
161 vec3 dir=vec3(uv*zoom,1.);
162 float time=iGlobalTime*speed+.25;
163
164 //mouse rotation
165 float a1=.5+iMouse.x/iResolution.x*2.;
166 float a2=.8+iMouse.y/iResolution.y*2.;
167 mat2 rot1=mat2(cos(a1),sin(a1),-sin(a1),cos(a1));
168 mat2 rot2=mat2(cos(a2),sin(a2),-sin(a2),cos(a2));
169 dir.xz*=rot1;
170 dir.xy*=rot2;
171 vec3 from=vec3(1.,.5,0.5);
172 from+=vec3(time*2.,time,-2.);
173 from.xz*=rot1;
174 from.xy*=rot2;
175
176 //volumetric rendering
177 float s=0.1;
178 float fade=1.;
179 vec3 v=vec3(0.);
180 for (int r=0; r<volsteps; r++) {
181 vec3 p=from+s*dir*.5;
182 p = abs(vec3(tile)-mod(p,vec3(tile*2.))); // tiling fold
183
184 float a= 0.;
185 float pa=0.;
186 for (int i=0; i<iterations; i++) {
187 p=abs(p)/dot(p,p)-formuparam; // the magic formula
188 a+=abs(length(p)-pa); // absolute sum of average change
189 pa=length(p);
190 }
191 float dm=max(0.,darkmatter-a*a*.001); //dark matter
192 a*=a*a; // add contrast
193 if (r>6) fade*=1.-dm; // dark matter, don't render near
194 //v+=vec3(dm,dm*.5,0.);
195 v+=fade;
196 v+=vec3(s,s*s,s*s*s*s)*a*brightness*fade; // coloring based on distance
197 fade*=distfading; // distance fading
198 s+=stepsize;
199 }
200 v=mix(vec3(length(v)),v,saturation); //color adjust
201 gl_FragColor = vec4(v*.01,1.);
202
203}
204);
205
206
208
209
210uniform vec2 center;
211uniform vec2 resolution;
212
213vec2 iCenter = center;
214vec2 iResolution = resolution; // viewport resolution (in pixels)
215float iGlobalTime = CC_Time[1]; // shader playback time (in seconds)
216 //uniform float iChannelTime[4]; // channel playback time (in seconds)
217//uniform vec3 iChannelResolution[4]; // channel resolution (in pixels)
218
219vec4 iMouse = vec4(0,0,0,0); // mouse pixel coords. xy: current (if MLB down), zw: click
220//uniform sampler2D iChannel0; // input channel. XX = 2D/Cube
221
222// srtuss, 2013
223// collecting some design ideas for a new game project.
224// no raymarching is used.
225
226// if i could add a custom soundtrack, it'd use this one (essential for desired sensation)
227// http://www.youtube.com/watch?v=1uFAu65tZpo
228
229
230//#define GREEN_VERSION
231
232// ** improved camera shaking
233// ** cleaned up code
234// ** added stuff to the gates
235
236
237
238float time = iGlobalTime;
239
240vec2 rotate(vec2 p, float a)
241{
242 return vec2(p.x * cos(a) - p.y * sin(a), p.x * sin(a) + p.y * cos(a));
243}
244 float box(vec2 p, vec2 b, float r)
245{
246 return length(max(abs(p) - b, 0.0)) - r;
247}
248
249// iq's ray-plane-intersection code
250vec3 intersect(in vec3 o, in vec3 d, vec3 c, vec3 u, vec3 v)
251{
252 vec3 q = o - c;
253 return vec3(
254 dot(cross(u, v), q),
255 dot(cross(q, u), d),
256 dot(cross(v, q), d)) / dot(cross(v, u), d);
257}
258
259// some noise functions for fast developing
260float rand11(float p)
261{
262 return fract(sin(p * 591.32) * 43758.5357);
263}
264
265float rand12(vec2 p)
266{
267 return fract(sin(dot(p.xy, vec2(12.9898, 78.233))) * 43758.5357);
268}
269
270vec2 rand21(float p)
271{
272 return fract(vec2(sin(p * 591.32), cos(p * 391.32)));
273}
274
275vec2 rand22(in vec2 p)
276{
277 return fract(vec2(sin(p.x * 591.32 + p.y * 154.077), cos(p.x * 391.32 + p.y * 49.077)));
278}
279
280
281float noise11(float p)
282{
283 float fl = floor(p);
284 return mix(rand11(fl), rand11(fl + 1.0), fract(p));//smoothstep(0.0, 1.0, fract(p)));
285}
286
287float fbm11(float p)
288{
289 return noise11(p) * 0.5 + noise11(p * 2.0) * 0.25 + noise11(p * 5.0) * 0.125;
290}
291
292vec3 noise31(float p)
293{
294 return vec3(noise11(p), noise11(p + 18.952), noise11(p - 11.372)) * 2.0 - 1.0;
295}
296
297// something that looks a bit like godrays coming from the surface
298float sky(vec3 p)
299{
300 float a = atan(p.x, p.z);
301 float t = time * 0.1;
302 float v = rand11(floor(a * 4.0 + t)) * 0.5 + rand11(floor(a * 8.0 - t)) * 0.25 + rand11(floor(a * 16.0 + t)) * 0.125;
303 return v;
304}
305
306vec3 voronoi(in vec2 x)
307{
308 vec2 n = floor(x); // grid cell id
309 vec2 f = fract(x); // grid internal position
310 vec2 mg; // shortest distance...
311 vec2 mr; // ..and second shortest distance
312 float md = 8.0;
313 float md2 = 8.0;
314 for(int j = -1; j <= 1; j ++)
315 {
316 for(int i = -1; i <= 1; i ++)
317 {
318 vec2 g = vec2(float(i), float(j)); // cell id
319 vec2 o = rand22(n + g); // offset to edge point
320 vec2 r = g + o - f;
321
322 float d = max(abs(r.x), abs(r.y)); // distance to the edge
323
324 if(d < md)
325 {md2 = md; md = d; mr = r; mg = g;}
326 else if(d < md2)
327 {md2 = d;}
328 }
329 }
330 return vec3(n + mg, md2 - md);
331}
332
333//#define A2V(a) vec2(sin((a) * 6.28318531 / 100.0), cos((a) * 6.28318531 / 100.0))
334vec2 A2V(float a)
335{
336 return vec2(sin((a) * 6.28318531 / 100.0), cos((a) * 6.28318531 / 100.0));
337}
338
339
340
341float circles(vec2 p)
342{
343 float v;
344 float w;
345 float l;
346 float c;
347 vec2 pp;
348 l = length(p);
349
350
351 pp = rotate(p, time * 3.0);
352 c = max(dot(pp, normalize(vec2(-0.2, 0.5))), -dot(pp, normalize(vec2(0.2, 0.5))));
353 c = min(c, max(dot(pp, normalize(vec2(0.5, -0.5))), -dot(pp, normalize(vec2(0.2, -0.5)))));
354 c = min(c, max(dot(pp, normalize(vec2(0.3, 0.5))), -dot(pp, normalize(vec2(0.2, 0.5)))));
355
356 // innerest stuff
357 v = abs(l - 0.5) - 0.03;
358 v = max(v, -c);
359 v = min(v, abs(l - 0.54) - 0.02);
360 v = min(v, abs(l - 0.64) - 0.05);
361
362 pp = rotate(p, time * -1.333);
363 c = max(dot(pp, A2V(-5.0)), -dot(pp, A2V(5.0)));
364 c = min(c, max(dot(pp, A2V(25.0 - 5.0)), -dot(pp, A2V(25.0 + 5.0))));
365 c = min(c, max(dot(pp, A2V(50.0 - 5.0)), -dot(pp, A2V(50.0 + 5.0))));
366 c = min(c, max(dot(pp, A2V(75.0 - 5.0)), -dot(pp, A2V(75.0 + 5.0))));
367
368 w = abs(l - 0.83) - 0.09;
369 v = min(v, max(w, c));
370
371 return v;
372}
373
374float shade1(float d)
375{
376 float v = 1.0 - smoothstep(0.0, mix(0.012, 0.2, 0.0), d);
377 float g = exp(d * -20.0);
378 return v + g * 0.5;
379}
380
381void main()
382{
383 vec2 uv = gl_FragCoord.xy / iResolution.xy;
384 uv = uv * 2.0 - 1.0;
385 uv.x *= iResolution.x / iResolution.y;
386
387
388 // using an iq styled camera this time :)
389 // ray origin
390 vec3 ro = 0.7 * vec3(cos(0.2 * time), 0.0, sin(0.2 * time));
391 ro.y = cos(0.6 * time) * 0.3 + 0.65;
392 // camera look at
393 vec3 ta = vec3(0.0, 0.2, 0.0);
394
395 // camera shake intensity
396 float shake = clamp(3.0 * (1.0 - length(ro.yz)), 0.3, 1.0);
397 float st = mod(time, 10.0) * 143.0;
398
399 // build camera matrix
400 vec3 ww = normalize(ta - ro + noise31(st) * shake * 0.01);
401 vec3 uu = normalize(cross(ww, normalize(vec3(0.0, 1.0, 0.2 * sin(time)))));
402 vec3 vv = normalize(cross(uu, ww));
403 // obtain ray direction
404 vec3 rd = normalize(uv.x * uu + uv.y * vv + 1.0 * ww);
405
406 // shaking and movement
407 ro += noise31(-st) * shake * 0.015;
408 ro.x += time * 2.0;
409
410 float inten = 0.0;
411
412 // background
413 float sd = dot(rd, vec3(0.0, 1.0, 0.0));
414 inten = pow(1.0 - abs(sd), 20.0) + pow(sky(rd), 5.0) * step(0.0, rd.y) * 0.2;
415
416 vec3 its;
417 float v;
418 float g;
419
420 // voronoi floor layers
421 for(int i = 0; i < 4; i ++)
422 {
423 float layer = float(i);
424 its = intersect(ro, rd, vec3(0.0, -5.0 - layer * 5.0, 0.0), vec3(1.0, 0.0, 0.0), vec3(0.0, 0.0, 1.0));
425 if(its.x > 0.0)
426 {
427 vec3 vo = voronoi((its.yz) * 0.05 + 8.0 * rand21(float(i)));
428 v = exp(-100.0 * (vo.z - 0.02));
429
430 float fx = 0.0;
431
432 // add some special fx to lowest layer
433 if(i == 3)
434 {
435 float crd = 0.0;//fract(time * 0.2) * 50.0 - 25.0;
436 float fxi = cos(vo.x * 0.2 + time * 1.5);//abs(crd - vo.x);
437 fx = clamp(smoothstep(0.9, 1.0, fxi), 0.0, 0.9) * 1.0 * rand12(vo.xy);
438 fx *= exp(-3.0 * vo.z) * 2.0;
439 }
440 inten += v * 0.1 + fx;
441 }
442 }
443
444 // draw the gates, 4 should be enough
445 float gatex = floor(ro.x / 8.0 + 0.5) * 8.0 + 4.0;
446 float go = -16.0;
447 for(int i = 0; i < 4; i ++)
448 {
449 its = intersect(ro, rd, vec3(gatex + go, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
450 if(dot(its.yz, its.yz) < 2.0 && its.x > 0.0)
451 {
452 v = circles(its.yz);
453 inten += shade1(v);
454 }
455
456 go += 8.0;
457 }
458
459 // draw the stream
460 for(int j = 0; j < 20; j ++)
461 {
462 float id = float(j);
463
464 vec3 bp = vec3(0.0, (rand11(id) * 2.0 - 1.0) * 0.25, 0.0);
465 vec3 its = intersect(ro, rd, bp, vec3(1.0, 0.0, 0.0), vec3(0.0, 0.0, 1.0));
466
467 if(its.x > 0.0)
468 {
469 vec2 pp = its.yz;
470 float spd = (1.0 + rand11(id) * 3.0) * 2.5;
471 pp.y += time * spd;
472 pp += (rand21(id) * 2.0 - 1.0) * vec2(0.3, 1.0);
473 float rep = rand11(id) + 1.5;
474 pp.y = mod(pp.y, rep * 2.0) - rep;
475 float d = box(pp, vec2(0.02, 0.3), 0.1);
476 float foc = 0.0;
477 float v = 1.0 - smoothstep(0.0, 0.03, abs(d) - 0.001);
478 float g = min(exp(d * -20.0), 2.0);
479
480 inten += (v + g * 0.7) * 0.5;
481
482 }
483 }
484
485 inten *= 0.4 + (sin(time) * 0.5 + 0.5) * 0.6;
486
487 // find a color for the computed intensity
488#ifdef GREEN_VERSION
489 vec3 col = pow(vec3(inten), vec3(2.0, 0.15, 9.0));
490#else
491 vec3 col = pow(vec3(inten), 1.5 * vec3(0.15, 2.0, 9.0));
492#endif
493
494 gl_FragColor = vec4(col, 1.0);
495}
496
497
498);
int main(int argc, char *argv[])
Definition: main.m:12
static const char * starNestFrg
static const char * starFlame
static const char * shadertoyRelentlessFrag
#define STRINGIFY(A)