File size: 3,442 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import * as THREE from 'three';
import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { createEnvironment } from './environment.js';
import { createSubject } from './subject.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.PCFShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.0;
renderer.outputColorSpace = THREE.SRGBColorSpace;
document.getElementById('app').appendChild(renderer.domElement);

const scene = new THREE.Scene();
scene.background = new THREE.Color('#8d7c63');

const pmrem = new THREE.PMREMGenerator(renderer);
scene.environment = pmrem.fromScene(new RoomEnvironment(), 0.04).texture;
scene.environmentIntensity = 0.38;

const camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.05, 120);
camera.position.set(2.32, 1.9, 2.7);

const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0.02, 0.94, 0);
controls.enableDamping = true;
controls.dampingFactor = 0.06;
controls.minDistance = 1.6;
controls.maxDistance = 9;
controls.maxPolarAngle = Math.PI * 0.495;
controls.update();

/* ---------- lighting ---------- */
const hemi = new THREE.HemisphereLight(0xa9c9e2, 0x3a2a1a, 0.34);
scene.add(hemi);

// warm late-afternoon sun through the workshop window
const sun = new THREE.DirectionalLight(0xffd09a, 2.05);
sun.position.set(-4.5, 7.5, -3.8);
sun.target.position.set(0.2, 0.9, 0.4);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -4.2;
sun.shadow.camera.right = 4.2;
sun.shadow.camera.top = 3.6;
sun.shadow.camera.bottom = -1.6;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 18;
sun.shadow.bias = -0.00025;
sun.shadow.normalBias = 0.018;
sun.shadow.radius = 3;
scene.add(sun, sun.target);

// cool bounce fill from the right
const fill = new THREE.DirectionalLight(0x9fc2e2, 0.9);
fill.position.set(5, 3.5, 4);
scene.add(fill);

// warm rim / kicker from front-left
const rim = new THREE.DirectionalLight(0xffbd74, 1.35);
rim.position.set(-3.0, 2.6, 5.2);
scene.add(rim);

// window glow bounce
const windowGlow = new THREE.PointLight(0xffcf8f, 12, 10, 2);
windowGlow.position.set(-1.6, 2.4, -5.4);
scene.add(windowGlow);

const key = new THREE.SpotLight(0xfff1d8, 30, 9, 0.74, 0.95, 2);
key.position.set(1.8, 4.0, 2.3);
key.target.position.set(0.0, 0.98, 0.0);
scene.add(key, key.target);

const subject = createSubject();
scene.add(subject);
const env = createEnvironment(scene);

/* ---------- boot ---------- */
let frames = 0;
const t0 = performance.now();
function animate() {
  requestAnimationFrame(animate);
  const t = (performance.now() - t0) / 1000;
  env.update(t);
  controls.update();
  renderer.render(scene, camera);
  if (frames++ === 1) {
    window.__three = {
      scene, camera, renderer,
      render: () => renderer.render(scene, camera),
    };
    document.body.classList.add('ready');
  }
}
animate();

window.addEventListener('resize', () => {
  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(window.innerWidth, window.innerHeight);
});