Spaces:
Running
Running
Blend the floor into the sky at the horizon (world-space sky, floor and grid fade out)
Browse filesFiles changed (3)
- app.js+1-1
- index.html+1-1
- viewer.js+53-18
239 unmodified lines240241242243244245246239 unmodified linesasync function getViewer() {if (viewer || viewerFailed) return viewer;try {const { createViewer } = await import(signed("./viewer.js?v=32"));viewer = createViewer();} catch (e) {console.error("3D viewer failed to start", e);239 unmodified lines240241242243244245246239 unmodified linesasync function getViewer() {if (viewer || viewerFailed) return viewer;try {const { createViewer } = await import(signed("./viewer.js?v=33"));viewer = createViewer();} catch (e) {console.error("3D viewer failed to start", e);
82 unmodified lines83848586878882 unmodified lines</aside></div><script src="app.js?v=34"></script></body></html>82 unmodified lines83848586878882 unmodified lines</aside></div><script src="app.js?v=35"></script></body></html>
10 unmodified lines11121314151617181920212223242526272829107 unmodified lines13713813914014114214314423 unmodified lines16816917017117217314 unmodified lines1881891901911921931949 unmodified lines204205206207208209210211212213263 unmodified lines477478479480481482117 unmodified lines60060160260360460510 unmodified lines// fading into the horizon, one shadow-casting key light plus soft fill.const STUDIO = { top: "#2b2d31", horizon: "#74777c", floor: 0x4f5257, grid: 0x3f4247 };function skyTexture() {const c = document.createElement("canvas");c.width = 2; c.height = 256;const g = c.getContext("2d");const grad = g.createLinearGradient(0, 0, 0, 256);grad.addColorStop(0, STUDIO.top);grad.addColorStop(0.62, "#5b5e63");grad.addColorStop(1, STUDIO.horizon);g.fillStyle = grad;g.fillRect(0, 0, 2, 256);const tex = new THREE.CanvasTexture(c);tex.colorSpace = THREE.SRGBColorSpace;return tex;}// Reflections: an even grey room with soft light panels on every side, so metal and107 unmodified linesstage.prepend(renderer.domElement);const scene = new THREE.Scene();scene.background = skyTexture();scene.fog = new THREE.Fog(STUDIO.horizon, 10, 50);const pmrem = new THREE.PMREMGenerator(renderer);scene.environment = studioEnvironment(pmrem);const hemi = new THREE.HemisphereLight(0xffffff, 0x6a6d72, 0.5);23 unmodified linesnew THREE.MeshStandardMaterial({ color: STUDIO.floor, roughness: 1, metalness: 0 }));floor.rotation.x = -Math.PI / 2;floor.receiveShadow = true;floor.visible = false;scene.add(floor);const camera = new THREE.PerspectiveCamera(35, 1, 0.01, 100);14 unmodified lineslet onJoint = () => {};function setTheme(t) { theme = t === "light" ? "light" : "dark"; } // the studio is the same grey in both// Floor, grid, fog and the key light's shadow box, sized to the current model.function placeGrid() {if (grid) { scene.remove(grid); grid.geometry.dispose(); grid.material.dispose(); grid = null; }floor.visible = !!model;9 unmodified linesgrid = new THREE.GridHelper(span, 400, STUDIO.grid, STUDIO.grid);grid.material.transparent = true;grid.material.opacity = 0.45;grid.position.set(c.x, y + s * 0.0005, c.z);scene.add(grid);scene.fog.near = s * 2.5;scene.fog.far = s * 12;key.position.set(c.x - s * 1.3, c.y + s * 1.9, c.z + s * 0.6); // from the left, so the shadow falls to the rightkey.target.position.copy(c);fill.position.set(c.x + s * 1.5, c.y + s * 0.8, c.z + s * 0.4);263 unmodified linesconst dims = box.getSize(new THREE.Vector3());const size = Math.max(dims.length(), 0.02);camera.near = size / 400; camera.far = size * 80;interior = !!opts.interior;camera.fov = opts.interior ? 68 : 35; // a room needs a wide, photo-like lensif (opts.interior) {117 unmodified linesrequestAnimationFrame(tick);if (settleDirty) settle();controls.update();renderer.render(scene, camera);placeDots();})();10 unmodified lines1112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657107 unmodified lines16516616716816917017117217317423 unmodified lines19819920020120220320414 unmodified lines2192202212222232242259 unmodified lines235236237238239240241242243244245246263 unmodified lines510511512513514515516117 unmodified lines63463563663763863964010 unmodified lines// fading into the horizon, one shadow-casting key light plus soft fill.const STUDIO = { top: "#2b2d31", horizon: "#74777c", floor: 0x4f5257, grid: 0x3f4247 };// The sky is a dome around the camera whose gradient follows the real horizon (not the// screen), and the floor and grid fade out with distance into the same horizon colour, so// the floor melts into the sky with no edge. Colours are sRGB, drawn as they are.const SKY = { top: [0x2b, 0x2d, 0x31], mid: [0x4b, 0x4e, 0x53], horizon: [0x7c, 0x7f, 0x84] };function skyDome() {const v = (c) => new THREE.Vector3(c[0] / 255, c[1] / 255, c[2] / 255);const mat = new THREE.ShaderMaterial({uniforms: { top: { value: v(SKY.top) }, mid: { value: v(SKY.mid) }, horizon: { value: v(SKY.horizon) } },vertexShader: `varying vec3 vDir;void main() {vDir = (modelMatrix * vec4(position, 1.0)).xyz - cameraPosition;gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);}`,fragmentShader: `uniform vec3 top, mid, horizon; varying vec3 vDir;void main() {float h = normalize(vDir).y;vec3 c = mix(horizon, mid, smoothstep(0.0, 0.22, h));c = mix(c, top, smoothstep(0.22, 0.75, h));gl_FragColor = vec4(c, 1.0);}`,side: THREE.BackSide, depthWrite: false, depthTest: false, toneMapped: false, fog: false,});const dome = new THREE.Mesh(new THREE.SphereGeometry(1, 48, 24), mat);dome.renderOrder = -1;dome.frustumCulled = false;return dome;}// Fade a floor-level material out between fade.x and fade.y metres from fade centre (xz).function fadeWithDistance(material, fade) {material.transparent = true;material.onBeforeCompile = (sh) => {sh.uniforms.fadeCenter = fade.center;sh.uniforms.fadeRange = fade.range;sh.vertexShader = sh.vertexShader.replace("#include <common>", "#include <common>\nvarying vec3 vFadePos;").replace("#include <project_vertex>", "#include <project_vertex>\nvFadePos = (modelMatrix * vec4(transformed, 1.0)).xyz;");sh.fragmentShader = sh.fragmentShader.replace("#include <common>", "#include <common>\nvarying vec3 vFadePos;\nuniform vec3 fadeCenter;\nuniform vec2 fadeRange;").replace("#include <dithering_fragment>","#include <dithering_fragment>\ngl_FragColor.a *= 1.0 - smoothstep(fadeRange.x, fadeRange.y, distance(vFadePos.xz, fadeCenter.xz));");};}// Reflections: an even grey room with soft light panels on every side, so metal and107 unmodified linesstage.prepend(renderer.domElement);const scene = new THREE.Scene();scene.background = new THREE.Color().setRGB(...SKY.horizon.map((c) => c / 255), THREE.SRGBColorSpace);const sky = skyDome();scene.add(sky);const fade = { center: { value: new THREE.Vector3() }, range: { value: new THREE.Vector2(10, 50) } };const pmrem = new THREE.PMREMGenerator(renderer);scene.environment = studioEnvironment(pmrem);const hemi = new THREE.HemisphereLight(0xffffff, 0x6a6d72, 0.5);23 unmodified linesnew THREE.MeshStandardMaterial({ color: STUDIO.floor, roughness: 1, metalness: 0 }));floor.rotation.x = -Math.PI / 2;floor.receiveShadow = true;fadeWithDistance(floor.material, fade);floor.visible = false;scene.add(floor);const camera = new THREE.PerspectiveCamera(35, 1, 0.01, 100);14 unmodified lineslet onJoint = () => {};function setTheme(t) { theme = t === "light" ? "light" : "dark"; } // the studio is the same grey in both// Floor, grid, their fade into the horizon and the key light's shadow box, sized to the current model.function placeGrid() {if (grid) { scene.remove(grid); grid.geometry.dispose(); grid.material.dispose(); grid = null; }floor.visible = !!model;9 unmodified linesgrid = new THREE.GridHelper(span, 400, STUDIO.grid, STUDIO.grid);grid.material.transparent = true;grid.material.opacity = 0.45;fadeWithDistance(grid.material, fade);grid.renderOrder = 1;grid.position.set(c.x, y + s * 0.0005, c.z);scene.add(grid);fade.center.value.set(c.x, y, c.z);fade.range.value.set(s * 1.5, s * 7); // the floor and grid melt into the horizon colourkey.position.set(c.x - s * 1.3, c.y + s * 1.9, c.z + s * 0.6); // from the left, so the shadow falls to the rightkey.target.position.copy(c);fill.position.set(c.x + s * 1.5, c.y + s * 0.8, c.z + s * 0.4);263 unmodified linesconst dims = box.getSize(new THREE.Vector3());const size = Math.max(dims.length(), 0.02);camera.near = size / 400; camera.far = size * 80;sky.scale.setScalar(camera.far * 0.5);interior = !!opts.interior;camera.fov = opts.interior ? 68 : 35; // a room needs a wide, photo-like lensif (opts.interior) {117 unmodified linesrequestAnimationFrame(tick);if (settleDirty) settle();controls.update();sky.position.copy(camera.position);renderer.render(scene, camera);placeDots();})();
app.js+1-1
index.html+1-1
viewer.js+53-18