Branchwell

PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / Resources / SpatialVisuals.metal

Raw 20.8 KB on 1cf89eeacc946d122aba3a62ada6cb1600fbf757
1#include <metal_stdlib>
2using namespace metal;
3
4// Preserve highlight intensity and coverage when projecting colored effects to grayscale.
5static float monochromeIntensity(float3 color) { return max(color.r, max(color.g, color.b)); }
6struct SpatialFrame {
7 float2 resolution; float time; int scene;
8 float4 rhythm; // pulse, beat drive, bass, treble
9 float4 style; // brightness, size, fog, zoom
10 float4 camera; // rotation, chaos, liquid detail / halo count, zoom speed
11 float4 colors[5];
12};
13struct SpatialPixel { float4 position [[position]]; float2 uv; };
14vertex SpatialPixel spatialVertex(uint id [[vertex_id]]) {
15 float2 p = float2((id << 1) & 2, id & 2);
16 return {float4(p * 2 - 1, 0, 1), p};
17}
18float2 spin(float2 p, float a) { float c = cos(a), s = sin(a); return float2(c*p.x-s*p.y,s*p.x+c*p.y); }
19float3 tint(float phase, constant SpatialFrame &f) {
20 float cycle = phase - floor(phase / 2) * 2;
21 float t = (cycle < 1 ? cycle : 2-cycle) * 4;
22 int i = min(3, int(t)); return mix(f.colors[i].rgb, f.colors[i+1].rgb, t-i);
23}
24float cellHash(float3 p) { return fract(sin(dot(p,float3(127.1,311.7,74.7))) * 43758.5453); }
25float2 inkBend(float z) { return float2(sin(z*.11)*2,cos(z*.09)*1.8); }
26float mist(float3 p) {
27 return sin(p.x+sin(p.z*.7))*sin(p.y+cos(p.x*.8))*sin(p.z+cos(p.y*.6));
28}
29// Source attacks launch local waves; high Chaos gives a stronger liquid response.
30float organicStrength(constant SpatialFrame &f) {
31 return f.rhythm.x*(.2+.55*f.camera.y)+f.rhythm.y*.08+f.rhythm.z*.12;
32}
33float field(float3 p, constant SpatialFrame &f) {
34 float t=f.time, strength=organicStrength(f), size=clamp(f.style.y, .3f, 3.f);
35 float beat=f.rhythm.x*(.55+.45*sin(dot(p,float3(.61,.83,.47))-t*1.6));
36 float3 world=p;
37 if(f.scene!=23 && f.scene!=12 && f.scene!=26) p+=sin(p.zxy*.9+float3(-t*.9,t*.7,t*.8))*strength*.20;
38 if (f.scene == 12) {
39 // Spheres can cross their cell edges while bouncing. Evaluate all adjacent
40 // centers instead of cutting the field at the nearest-cell boundary.
41 float2 base=floor(p.xz/2.8);
42 float distance=100;
43 for(int z=0;z<2;z++) for(int x=0;x<2;x++) {
44 float2 cell=base+float2(x,z), q=p.xz-cell*2.8;
45 float bounce=f.rhythm.z*(.9+.9*sin(cell.x*1.37+cell.y*.91+t*2.3))+f.rhythm.w*.45*sin(cell.x*2.1-cell.y*1.7+t*3.1);
46 // Keep both rows clear of the camera without clipping their bounce.
47 float h=2.3*tanh((sin(cell.x*.53+t*.5)*.8+cos(cell.y*.42-t*.36)*.9+bounce)/2.3);
48 float y=min(abs(p.y-h),abs(p.y-(9-h)));
49 float localAttack=f.rhythm.x*(.55+.45*sin(cell.x*.61+cell.y*.47-t*1.6));
50 float radius=.95+size*.10+localAttack*.18+f.rhythm.w*.14*cellHash(float3(cell,1));
51 distance=min(distance,length(float3(q.x,y,q.y))-radius);
52 }
53 return distance;
54 }
55 if (f.scene == 23) {
56 float detail=clamp(f.camera.z,0.f,1.f), frequency=.9+detail*.9;
57 // Keep the camera in an open cavity while the surrounding surface flows.
58 p=(p-float3(.9817477))*frequency+float3(.7853982);
59 float flow=smoothstep(0.f,.55f,length(p-float3(.7853982)));
60 p+=sin(p.zxy*1.4+float3(t*.6,-t*.47,t*.52))*(.15+detail*.08)*flow;
61 // Ripple different folds in different directions, keeping the camera cavity open.
62 p+=sin(p.yzx*2.3+float3(-t*1.3,t*1.1,t*.9))*strength*.38*flow;
63 p+=sin(p.zxy*5.1+float3(t*.8,-t*.7,t))*strength*.10*flow;
64 p+=sin(p.yzx*(4+detail*7)+float3(-t,t*.8,t*.7))*(.035+detail*.20)*flow;
65 float g=dot(sin(p),cos(p.yzx));
66 g+=flow*.22*dot(sin(p*2+t*.18),cos(p.zxy*2-t*.14));
67 g+=flow*detail*.22*dot(sin(p*7-t*.45),cos(p.yzx*7+t*.41));
68 float localSwelling=strength*.09*(.5+.5*sin(dot(p,float3(1.7,2.1,1.3))-t*1.4));
69 return (abs(g)-(.16+size*.03+localSwelling))*(.10/frequency);
70 }
71 if (f.scene == 24) {
72 p+=sin(p.zxy*1.1+t*.8)*(.25+beat*.25);
73 float3 q=p-floor(p/4+0.5)*4;
74 return min(length(q.xy),min(length(q.yz),length(q.xz)))-(.035+size*.035);
75 }
76 if (f.scene == 27) {
77 // Mirrored skyline: seeded towers rise from the floor and ceiling.
78 float2 cell=floor(p.xz/3.2+0.5), q=p.xz-cell*3.2;
79 float seed=cellHash(float3(cell.x,0,cell.y));
80 float height=1.2+seed*1.3+(.5+.5*sin(t*.8+seed*6.283))*(.5+beat*.6)+f.rhythm.z*.7*(.5+.5*sin(seed*17+t*2.4));
81 float y=min(abs(p.y-(height-3.8)),abs(p.y-(9.8-height)));
82 float3 d=abs(float3(q.x,y,q.y))-float3(.72+size*.12,height,.85+size*.08);
83 return length(max(d,0.f))+min(max(d.x,max(d.y,d.z)),0.f)-.08;
84 }
85 if (f.scene == 28) {
86 // Rings surround the flight path in staggered, independently tumbling groups.
87 float count=clamp(round(f.camera.z),1.f,12.f), period=7.2;
88 float3 group=floor(p/period+.5), local=p-group*period;
89 float row=group.z;
90 float2 orbitPoint=spin(local.xy,t*.18+row*.53);
91 float sector=6.2831853/count;
92 float nearest=floor(atan2(orbitPoint.y,orbitPoint.x)/sector+.5);
93 float distance=100;
94 // Only neighbouring angular sectors can contribute; cost stays bounded at twelve rings.
95 for(int neighbour=-1;neighbour<=1;neighbour++) {
96 float index=nearest+neighbour;
97 float identity=index-floor(index/count)*count;
98 float seed=cellHash(float3(identity+group.x*13,group.y*29,row));
99 float angle=index*sector;
100 float orbit=1.85+sin(t*.32+seed*6.283)*(.15+beat*.12);
101 float3 q=float3(orbitPoint-float2(cos(angle),sin(angle))*orbit,local.z);
102 q.xy=spin(q.xy,seed*6.283+t*(.18+seed*.24));
103 q.yz=spin(q.yz,seed*4+sin(t*.33+seed*6)*.85);
104 float radius=.7+seed*.28+beat*.15;
105 float thickness=.085+clamp(size,.3f,6.f)*.04;
106 distance=min(distance,length(float2(length(q.xy)-radius,q.z))-thickness);
107 }
108 return distance;
109 }
110 if (f.scene == 29) {
111 // A camera inside continuous wave walls; the positive field is the open waterway.
112 // Low levels heave the waves; highs add a fast chop along the walls.
113 float surge=.45+beat*.25+f.rhythm.z*.55;
114 float chop=f.rhythm.w*.22*sin(p.z*3.1+p.x*1.7-t*2.4);
115 float ceiling=2.3+sin(p.x*.8+p.z*.55+t*.7)*surge+sin(p.z*1.5-t*.4)*.18+chop;
116 float wall=3.4+sin(p.y*.9-p.z*.42+t*.55)*(.6+beat*.25+f.rhythm.z*.45)+chop;
117 return min(ceiling-abs(p.y),wall-abs(p.x))*.45;
118 }
119 // Prisms overlap cell boundaries when rotated: use the neighboring-object union.
120 float3 base=floor(p/3.8);
121 float distance=100;
122 for(int z=0;z<2;z++) for(int y=0;y<2;y++) for(int x=0;x<2;x++) {
123 float3 cell=base+float3(x,y,z), q=p-cell*3.8;
124 float seed=cellHash(cell);
125 q-=(float3(seed,cellHash(cell+5.7),cellHash(cell+9.2))-.5)*1.2;
126 q.xy=spin(q.xy,t*(.18+seed*.3)+seed*6.283);
127 q.yz=spin(q.yz,t*.24+seed*4);
128 float pump=f.rhythm.z*(.2+.25*sin(seed*23+t*2.2));
129 float localAttack=f.rhythm.x*(.55+.45*sin(seed*19-t*1.6));
130 float prism=(dot(abs(q),float3(1))-(1.1+size*.16+localAttack*.23+pump))*.57735-.07;
131 distance=min(distance,prism);
132 }
133 float3 lane=world-float3(1.5,1.8,0);
134 float3 laneDirection=normalize(float3(0,-2,1.9));
135 float corridor=1.25-length(lane-laneDirection*dot(lane,laneDirection));
136 return max(distance,corridor);
137}
138// Exact ray intersections for the joined cell objects. This avoids raymarch
139// iteration limits on flat prism faces and preserves silhouettes at export resolution.
140float pearlRay(float3 ro, float3 rd, float3 center, float radius) {
141 float3 delta=ro-center;
142 float b=dot(delta,rd), discriminant=b*b-dot(delta,delta)+radius*radius;
143 if(discriminant<0) return 1e4;
144 float root=sqrt(discriminant), near=-b-root;
145 return near>=0 ? near : (-b+root>=0 ? -b+root : 1e4);
146}
147float prismRay(float3 ro, float3 rd, float radius, float corridorExit) {
148 float near=max(0.f,corridorExit), far=32;
149 for(int z=0;z<2;z++) for(int y=0;y<2;y++) for(int x=0;x<2;x++) {
150 float3 face=float3(x?1:-1,y?1:-1,z?1:-1);
151 float denominator=dot(face,rd), numerator=radius-dot(face,ro);
152 if(abs(denominator)<1e-6) {if(numerator<0)return 1e4;}
153 else if(denominator>0) far=min(far,numerator/denominator);
154 else near=max(near,numerator/denominator);
155 }
156 return near<=far ? near : 1e4;
157}
158float joinedCellRay(float3 ro,float3 rd,constant SpatialFrame &f) {
159 bool pearls=f.scene==12;
160 float spacing=pearls ? 2.8 : 3.8;
161 float3 cell=floor(ro/spacing);
162 float3 safeDirection=select(-max(abs(rd),float3(1e-6)),max(abs(rd),float3(1e-6)),rd>=0);
163 float3 direction=select(float3(-1),float3(1),rd>=0);
164 float3 boundary=(cell+select(float3(0),float3(1),rd>=0))*spacing;
165 float3 next=(boundary-ro)/safeDirection, interval=spacing/abs(safeDirection);
166 float corridorExit=0;
167 if(pearls){cell.y=0;next.y=1e5;interval.y=1e5;}
168 else {
169 float3 axis=normalize(float3(0,-2,1.9)), delta=ro-float3(1.5,1.8,0);
170 float3 origin=delta-axis*dot(delta,axis), ray=rd-axis*dot(rd,axis);
171 float a=dot(ray,ray), b=dot(origin,ray), c=dot(origin,origin)-1.25*1.25;
172 if(a<1e-8)return -1;
173 corridorExit=max(0.f,(-b+sqrt(max(0.f,b*b-a*c)))/a);
174 }
175 float closest=1e4, size=clamp(f.style.y,.3f,3.f), entry=0;
176 for(int step=0;step<32;step++) {
177 if(entry>min(32.f,closest))break;
178 for(int z=0;z<2;z++) for(int y=0;y<(pearls?1:2);y++) for(int x=0;x<2;x++) {
179 float3 centerCell=cell+float3(x,y,z);
180 if(pearls) {
181 float2 id=centerCell.xz;
182 float bounce=f.rhythm.z*(.9+.9*sin(id.x*1.37+id.y*.91+f.time*2.3))+f.rhythm.w*.45*sin(id.x*2.1-id.y*1.7+f.time*3.1);
183 float height=2.3*tanh((sin(id.x*.53+f.time*.5)*.8+cos(id.y*.42-f.time*.36)*.9+bounce)/2.3);
184 float attack=f.rhythm.x*(.55+.45*sin(id.x*.61+id.y*.47-f.time*1.6));
185 float radius=.95+size*.10+attack*.18+f.rhythm.w*.14*cellHash(float3(id,1));
186 float3 center=float3(id.x*spacing,height,id.y*spacing);
187 closest=min(closest,pearlRay(ro,rd,center,radius)); center.y=9-height;
188 closest=min(closest,pearlRay(ro,rd,center,radius));
189 } else {
190 float seed=cellHash(centerCell);
191 float3 offset=(float3(seed,cellHash(centerCell+5.7),cellHash(centerCell+9.2))-.5)*1.2;
192 float3 origin=ro-centerCell*spacing-offset, ray=rd;
193 origin.xy=spin(origin.xy,f.time*(.18+seed*.3)+seed*6.283);ray.xy=spin(ray.xy,f.time*(.18+seed*.3)+seed*6.283);
194 origin.yz=spin(origin.yz,f.time*.24+seed*4);ray.yz=spin(ray.yz,f.time*.24+seed*4);
195 float pump=f.rhythm.z*(.2+.25*sin(seed*23+f.time*2.2));
196 float attack=f.rhythm.x*(.55+.45*sin(seed*19-f.time*1.6));
197 float radius=1.1+size*.16+attack*.23+pump+.07/.57735;
198 closest=min(closest,prismRay(origin,ray,radius,corridorExit));
199 }
200 }
201 entry=min(next.x,min(next.y,next.z));
202 float3 crossed=float3(next.x<=entry+1e-5?1:0,next.y<=entry+1e-5?1:0,next.z<=entry+1e-5?1:0);
203 cell+=direction*crossed; next+=interval*crossed;
204 }
205 return closest<=32 ? closest : -1;
206}
207float3 volume(float3 ro, float3 rd, constant SpatialFrame &f) {
208 float3 result=0; float transmission=1;
209 for (int i=0;i<36;i++) {
210 float distance=.5+i*.55;
211 float3 p=ro+rd*distance;
212 float strength=organicStrength(f);
213 p+=sin(p.yzx*.8+float3(f.time*.9,-f.time*.7,f.time*.6))*strength*.25;
214 float density, phase;
215 if (f.scene==10) {
216 float bend=sin(p.z*.34+f.time*.3)*2.1+sin(p.y*.24+f.time*.4);
217 float sheet=sin((p.x+bend)*1.4);
218 float ribbons=exp(-abs(sheet)*(9.f/clamp(f.style.y,.5f,3.f)));
219 float folds=.55+.45*sin(p.y*.7+p.z*.3+f.time*.55);
220 density=ribbons*folds*(.15+f.rhythm.x*.13);
221 phase=p.y*.06+p.z*.025+f.time*.012;
222 } else {
223 // Fly inside a curved world-space ink tunnel: nearby walls expand past the camera.
224 float2 wall=spin(p.xy-inkBend(p.z),p.z*.18);
225 float angle=atan2(wall.y,wall.x), radius=length(wall);
226 float3 flow=float3(wall,p.z*.42);
227 float cloud=mist(flow*1.1+f.time*.07)+.4*mist(flow*2.3-f.time*.11);
228 float tunnelRadius=2.35+sin(p.z*.57+angle*3)*.26+cloud*.32
229 +strength*.34*sin(angle*4+p.z*.8-f.time*1.2);
230 float sheet=exp(-pow((radius-tunnelRadius)/(.48+clamp(f.style.y,.3f,3.f)*.12),2.f)*2);
231 float spiral=.5+.5*sin(angle*5+p.z*1.3+cloud*1.4-f.time*.12);
232 float ribbons=.20+.80*pow(spiral,3.f);
233 density=sheet*ribbons*(.55+f.rhythm.x*.40)*(.55+.45*smoothstep(-.8f,.8f,cloud));
234 phase=p.z*.045+sin(angle)*.16+cloud*.19+f.time*.014;
235 }
236 density*=exp(-distance*clamp(f.style.z,0.f,1.f)*.025);
237 float3 color=tint(phase,f);
238 result+=transmission*density*color*(1.3+f.rhythm.x*.5);
239 transmission*=1-density*.65;
240 }
241 return result;
242}
243// One continuous coordinate field keeps neighbouring cell edges joined as the funnel bends.
244float3 meshVortex(float2 uv, constant SpatialFrame &f) {
245 float t=f.time, beat=f.rhythm.x, strength=organicStrength(f);
246 float2 center=float2(sin(t*.17)*.38,cos(t*.13)*.28);
247 // Smooth camera drift avoids lateral jumps on individual attacks.
248 float2 p=uv/clamp(f.style.w,.3f,2.f)-center;
249 float radius=length(p), angle=atan2(p.y,p.x);
250 float depth=-log(max(radius*.45,.018f))*7;
251 float twist=angle+depth*.13+t*.19+sin(depth*.23-t*.4)*.12;
252 // 24 cells around the circumference; an even repeat keeps the angular seam connected.
253 float2 lattice=float2(twist*(48/6.2831853),depth*1.2+t*clamp(f.camera.w,0.f,3.f)*3.4);
254 lattice.y+=sin(angle*3+t*.32)*.35;
255 // Warp one shared coordinate field, so neighbouring cells keep joined edges.
256 // Integer angular harmonics preserve the seam around the tunnel circumference.
257 float2 wave=float2(sin(angle*5+depth*.85-t*1.4),sin(angle*3-depth*.67+t*1.1));
258 float2 fine=float2(sin(angle*7-depth*1.2+t*.8),cos(angle*4+depth*.93-t*1.3));
259 lattice+=strength*(wave*.68+fine*.20);
260 // A staggered lattice tiles exclusively with six-sided honeycomb cells.
261 const float2 spacing=float2(2,3.46410162);
262 float2 a=lattice-spacing*round(lattice/spacing);
263 float2 shifted=lattice-spacing*.5;
264 float2 b=shifted-spacing*round(shifted/spacing);
265 float2 cell=abs(dot(a,a)<dot(b,b) ? a : b);
266 float distance=abs(1-max(cell.x,dot(cell,float2(.5,.8660254))));
267 float2 dx=dfdx(lattice), dy=dfdy(lattice);
268 // Unwrap the circumference before measuring pixel width at the atan2 seam.
269 dx.x-=round(dx.x/48)*48; dy.x-=round(dy.x/48)*48;
270 float2 footprint=abs(dx)+abs(dy);
271 float width=max(.0001f,min(.32f,length(footprint)*(.55+clamp(f.style.y,.3f,6.f)*.22)));
272 float core=exp(-pow(distance/width,2.f)*2.5);
273 float glow=exp(-distance/(width*2.8))*.28;
274 float depthFade=exp(-max(0.f,depth-6)*clamp(f.style.z,0.f,1.f)*.025);
275 float centerFade=smoothstep(.025f,.10f,radius);
276 float phase=.48+uv.y*.23+sin(angle+t*.14)*.065+t*.018;
277 float localLight=beat*(.5+.5*sin(angle*5+depth*.6-t));
278 float3 color=tint(phase,f)*(core*(2.0+localLight*.35)+glow)*depthFade*centerFade;
279 color+=tint(.65+t*.018,f)*exp(-radius*35)*.35;
280 color*=max(0.f,f.style.x);
281 return 1-exp(-color*1.45);
282}
283fragment float4 spatialFragment(SpatialPixel input [[stage_in]], constant SpatialFrame &f [[buffer(0)]]) {
284 float2 uv=(input.position.xy/f.resolution-.5)*float2(2*f.resolution.x/f.resolution.y,-2);
285 uv=spin(uv,f.camera.x);
286 float t=f.time, beat=f.rhythm.x;
287 if(f.scene==22) return float4(float3(monochromeIntensity(meshVortex(uv, f))), 1);
288 float zoomSpeed=clamp(f.camera.w,0.f,3.f);
289 float3 ro=float3(sin(t*.23)*.3,cos(t*.19)*.3,t*1.9*zoomSpeed);
290 float3 rd=normalize(float3(uv,1.45*clamp(f.style.w,.5f,2.5f)));
291 if (f.scene==12) { ro.y=4.2; rd.y-=.24+sin(t*.14)*.12; rd=normalize(rd); }
292 if (f.scene==23) { ro=float3(.9817477); rd=normalize(float3(uv*1.8,1.05*clamp(f.style.w,.5f,2.5f))); rd.xy=spin(rd.xy,t*.13); rd.yz=spin(rd.yz,sin(t*.17)*.45); }
293 if (f.scene==24) { ro.xy+=float2(1.6,1.7); }
294 if (f.scene==25) { ro.z=t*3.2*zoomSpeed; ro.xy=inkBend(ro.z); }
295 if (f.scene==26) { ro.y=-t*2.0*zoomSpeed+1.8; ro.x+=1.5; }
296 if (f.scene==27) { ro=float3(1.6,3,t*3.2*zoomSpeed); rd.y-=.12+sin(t*.2)*.12; }
297 if (f.scene==28) { ro.xy=float2(sin(t*.17)*.15,cos(t*.13)*.15); rd.xy=spin(rd.xy,sin(t*.19)*.25); }
298 if (f.scene==29) { ro=float3(sin(t*.23)*.7,cos(t*.29)*.4,t*2.6*zoomSpeed); rd.xy=spin(rd.xy,sin(t*.24)*.3); }
299 if(f.scene==12 || f.scene==26 || f.scene==27) rd.xz=spin(rd.xz,sin(t*.09)*.22+sin(t*.037)*.12);
300 rd=normalize(rd);
301 float3 atmospheric=tint(.15+uv.y*.09+t*.01,f)*(.045+.02*(.5+.5*sin(uv.x*2+uv.y*3+t*.3)));
302 if(f.scene==23 || f.scene==25) atmospheric*=.16;
303 float3 color=atmospheric;
304 if (f.scene==10 || f.scene==25) {
305 color+=volume(ro,rd,f);
306 } else {
307 float travel=0, glow=0; bool hit=false;
308 bool joinedCells=f.scene==12 || f.scene==26;
309 int steps=(f.scene==23 || joinedCells) ? 160 : (f.scene>=27 ? 128 : 64);
310 float previousTravel=0;
311 if(joinedCells){travel=joinedCellRay(ro,rd,f);hit=travel>=0;}
312 for(int i=0;i<steps && !joinedCells;i++) {
313 float d=field(ro+rd*travel,f);
314 if(f.scene==24) glow+=exp(-abs(d)*16)*.025;
315 if(joinedCells && d<0) {
316 float low=previousTravel, high=travel;
317 for(int refine=0;refine<7;refine++) {
318 float middle=(low+high)*.5;
319 if(field(ro+rd*middle,f)>0) low=middle; else high=middle;
320 }
321 travel=(low+high)*.5; hit=true; break;
322 }
323 if(abs(d)<(joinedCells ? .004 : (f.scene==23 ? .003 : .012))) {hit=true;break;}
324 float advance=abs(d)*.75;
325 // Repeated cells have discontinuous nearest-cell choices: cross them in bounded steps.
326 if(joinedCells) advance=min(advance,.35f);
327 if(f.scene==27 || f.scene==28) advance=min(advance,.4f);
328 previousTravel=travel;
329 travel+=max(joinedCells ? .002 : (f.scene==23 ? .003 : .015),advance);
330 if(travel>32) break;
331 }
332 if(hit) {
333 float3 p=ro+rd*travel;
334 float e=joinedCells ? .018 : (f.scene==23 ? .006 : .018);
335 float3 n=normalize(float3(field(p+float3(e,0,0),f)-field(p-float3(e,0,0),f),field(p+float3(0,e,0),f)-field(p-float3(0,e,0),f),field(p+float3(0,0,e),f)-field(p-float3(0,0,e),f))+float3(.00001));
336 float3 light=normalize(float3(-.4,.7,-.6));
337 float diffuse=abs(dot(n,light));
338 float fresnel=pow(1-abs(dot(n,-rd)),3.f);
339 float spec=pow(max(0.f,dot(reflect(rd,n),light)),24.f);
340 float phase=dot(p,float3(.05,.04,.035))+t*.018+f.rhythm.z*.1;
341 float shade=.24+diffuse*.65+spec*.7+fresnel*.34;
342 if(f.scene==23) {
343 float detail=clamp(f.camera.z,0.f,1.f);
344 float ripple=.5+.5*sin(dot(p,float3(5,4,6))+sin(p.y*9+t)*detail-t*.7);
345 phase=.15+(n.y*.5+.5)*.55+ripple*.2;
346 float reflection=pow(max(0.f,dot(reflect(rd,n),light)),12.f);
347 shade=.035+pow(diffuse,2.f)*.7+reflection*1.8+fresnel*1.2;
348 shade*=.65+ripple*(.35+detail*.4);
349 }
350 if(f.scene==24) shade=1.5+beat;
351 if(f.scene==28) { phase=dot(p,float3(.08,.06,.09))+t*.02; shade=.3+diffuse*.65+spec*1.3+fresnel*.7; }
352 if(f.scene==29) {
353 float ripple=.5+.5*sin(p.z*2.7+p.x*1.3+sin(p.y*3)-t*.6);
354 phase=.2+ripple*.45+n.y*.12+f.rhythm.z*.15;
355 // Crests catch light on the highs: bright bands travel along the walls.
356 float crest=pow(.5+.5*sin(p.z*1.9-p.y*1.2-t*2.2),6.f)*f.rhythm.w;
357 shade=.3+diffuse*.5+spec*1.6+fresnel*.7+ripple*.16+crest*1.4;
358 }
359 if(f.scene==26 || f.scene==27) {
360 float3 cell=f.scene==27 ? float3(floor(p.xz/3.2+0.5),0).xzy : floor(p/(f.scene==4 ? 3.2 : 3.8)+0.5);
361 float lit=.5+.5*sin(cellHash(cell)*19+t*2.3);
362 phase+=f.rhythm.w*.2*lit;
363 shade+=(f.rhythm.z*.55+f.rhythm.w*.4)*lit*lit;
364 }
365 if(f.scene==12) {
366 float2 cell=floor(p.xz/2.8+0.5);
367 float lit=.5+.5*sin(cell.x*1.37+cell.y*.91+t*2.3);
368 phase+=f.rhythm.w*.25*lit;
369 shade+=(f.rhythm.z*.7+f.rhythm.w*.5)*lit*lit;
370 }
371 float fog=exp(-travel*(.015+clamp(f.style.z,0.f,1.f)*.032));
372 color=mix(atmospheric,tint(phase,f)*shade,fog);
373 }
374 color+=tint(.35+t*.025,f)*glow*(1+beat);
375 }
376 color*=max(0.f,f.style.x);
377 // Tone-map each palette channel without introducing unrelated rainbow hues.
378 color=1-exp(-color*1.45);
379 return float4(float3(monochromeIntensity(color)), 1);
380}