PageSourceSearch

https://swiss.gl/demo/DeferredShading.js

js swiss.gl collected 2026-10-03 23:20:36 UTC 2,470 bytes, 59 lines download raw bytes

1/** @license 
2  * Copyright 2023 Google LLC.
3  * SPDX-License-Identifier: Apache-2.0 
4  */
5
6class DeferredShading {
7    frame(glsl, params) {
8        // draw objects
9        const gbuf = glsl({...params, Mesh:[64,128], Grid:[4,4,3],
10              Aspect:'fit', DepthTest:1, Clear:0, VP:`
11        vec3 surface_f(vec2 p) {
12            vec2 c = sin(time+p*vec2(ID)*TAU);
13            vec3 pos = torus(p, 1.0, 0.4 + 0.1*c.x + 0.15*c.y)/8.0;
14            pos.xz *= rot2(float(ID.x+ID.y+ID.z));
15            pos.xyz += (vec3(ID)-vec3(Grid-1)*0.5)*0.4;
16            return pos;
17        }
18        void vertex() {
19            varying vec3 normal, wldPos = SURF(surface_f, UV, normal, 1e-3);
20            VPos = wld2proj(wldPos);
21        }`, FP:`
22            FOut = vec4(0.7, 0.7, 0.7, 1.0);
23            vec2 m = UV*vec2(Mesh)/4.0;
24            FOut.rgb += (isoline(m.x)+isoline(m.y))*0.2; // color
25            FOut1.xyz = normalize(normal);               // normal
26            FOut2 = vec4(wldPos, 1.0);                   // wldPos
27        `}, {format:'rgba16f+depth', tag:'gbuf', layern:3});
28
29        // common light passes code
30        const lightArgs = {...params, Face:'front', Grid:[9, 9, 1],
31            Aspect:'fit', Blend:'s+d', DepthTest:'keep', VP:`
32            varying vec3 lightPos = vec3(ID-Grid/2)*0.2;
33            lightPos.xy *= rot2(time*0.1+lightPos.z)*(0.5+sin(time*0.7)*0.4);
34            lightPos.z = sin((lightPos.x+lightPos.y)*1.5+time*0.25)*0.75;
35            varying vec3 lightColor = hash(ID+1)*5.0;
36            VPos = wld2proj(lightPos+uv2sphere(UV)*lightR);`};
37
38        // accumulate surface lights
39        const light = glsl({...lightArgs, lightR:0.3, 
40            Mesh:[32,64], Clear:0, gbuf, FP:`
41            vec4 wldPos = gbuf(I,2);
42            if (wldPos.w==0.0) discard;
43            vec3 lightDir = lightPos-wldPos.xyz;
44            float r = length(lightDir)+1e-10;
45            if (r>lightR) discard;
46            vec3 color  = gbuf(I,0).rgb;
47            vec3 normal = gbuf(I,1).xyz;
48            float diff = max(dot(normal, lightDir/r), 0.0);
49            float att = 1.5*smoothstep(1.0,0.9,r/lightR) / (1.0+r*r*2e4);
50            FOut = vec4(color.rgb*lightColor*att*diff, 1.0);`
51        }, {format:'rgba16f', tag:'light', depth:gbuf.depth});
52        
53        // add light source points
54        glsl({...lightArgs, lightR:0.005, Mesh:[16,8], FP:`lightColor*0.3,1`}, light);
55
56        // render to screen
57        glsl({light, FP:`sqrt(light(UV))`})
58    }
59}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.