File size: 2,938 Bytes
59c7fd7 | 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 | import * as THREE from 'three';
import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
import { createSubject } from './subject.js';
import { createEnvironment } from './environment.js';
const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: 'high-performance' });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 0.98;
renderer.outputColorSpace = THREE.SRGBColorSpace;
document.getElementById('app').appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(38, window.innerWidth / window.innerHeight, 0.1, 400);
camera.position.set(7.0, 4.0, 8.4);
camera.lookAt(0, 1.25, 0);
// environment reflections
const pmrem = new THREE.PMREMGenerator(renderer);
const roomEnv = new RoomEnvironment();
scene.environment = pmrem.fromScene(roomEnv, 0.04).texture;
scene.environmentIntensity = 0.55;
roomEnv.dispose?.();
pmrem.dispose();
// subject + environment
const subject = createSubject();
scene.add(subject);
createEnvironment(scene);
// gentle animation: subtle idle bob of the scene? keep subject static (evaluation orbits it)
// animate dust motes for atmosphere
const dustGeo = new THREE.BufferGeometry();
const dustCount = 260;
const dustPos = new Float32Array(dustCount * 3);
const dustSeed = new Float32Array(dustCount);
for (let i = 0; i < dustCount; i++) {
dustPos[i * 3] = (Math.random() - 0.5) * 18;
dustPos[i * 3 + 1] = Math.random() * 6 + 0.4;
dustPos[i * 3 + 2] = (Math.random() - 0.5) * 18;
dustSeed[i] = Math.random() * 100;
}
dustGeo.setAttribute('position', new THREE.BufferAttribute(dustPos, 3));
const dustMat = new THREE.PointsMaterial({
color: 0xffe6c0, size: 0.035, transparent: true, opacity: 0.55,
depthWrite: false, blending: THREE.AdditiveBlending, sizeAttenuation: true,
});
const dust = new THREE.Points(dustGeo, dustMat);
scene.add(dust);
const clock = new THREE.Clock();
function render() {
renderer.render(scene, camera);
}
function animate() {
const t = clock.getElapsedTime();
const arr = dustGeo.attributes.position.array;
for (let i = 0; i < dustCount; i++) {
arr[i * 3 + 1] += Math.sin(t * 0.35 + dustSeed[i]) * 0.0009;
arr[i * 3] += Math.sin(t * 0.22 + dustSeed[i] * 0.7) * 0.0006;
if (arr[i * 3 + 1] < 0.3) arr[i * 3 + 1] = 6.2;
}
dustGeo.attributes.position.needsUpdate = true;
render();
requestAnimationFrame(animate);
}
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
render();
window.__three = { scene, camera, renderer, render };
requestAnimationFrame(animate);
|