File size: 9,371 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import * as THREE from 'three';
import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
import {
  makeFloor, makeWallpaper, makePaintedWood, makeSkyTexture, makeShadowTexture, makeFloorFade, rng,
} from './textures.js';

function cloneRepeat(tex, rx, ry) {
  const t = tex.clone();
  t.repeat.set(rx, ry);
  t.needsUpdate = true;
  return t;
}

export function buildEnvironment(scene) {
  const env = new THREE.Group();
  env.name = 'environment';
  scene.add(env);

  const floorTex = makeFloor(21);
  const wallpaperTex = makeWallpaper(41);
  const painted = makePaintedWood(31, '#c9b999');
  const skyTex = makeSkyTexture();
  const shadowTex = makeShadowTexture();
  const fadeTex = makeFloorFade();

  /* ---------- floor ---------- */
  const floorMat = new THREE.MeshPhysicalMaterial({
    map: floorTex.map,
    bumpMap: floorTex.bumpMap,
    bumpScale: 0.014,
    roughnessMap: floorTex.roughnessMap,
    roughness: 0.78,
    metalness: 0.0,
    specularIntensity: 0.0,
    clearcoat: 0.0,
    clearcoatRoughness: 0.6,
    envMapIntensity: 0.04,
    color: 0xa89886,
  });
  const floor = new THREE.Mesh(new THREE.PlaneGeometry(24, 24), floorMat);
  floor.rotation.x = -Math.PI / 2;
  floor.receiveShadow = true;
  env.add(floor);

  const fade = new THREE.Mesh(
    new THREE.PlaneGeometry(24, 24),
    new THREE.MeshBasicMaterial({ map: fadeTex, transparent: true, depthWrite: false, opacity: 0.95 })
  );
  fade.rotation.x = -Math.PI / 2;
  fade.position.y = 0.004;
  fade.renderOrder = 0;
  env.add(fade);

  const contact = new THREE.Mesh(
    new THREE.PlaneGeometry(1.75, 1.25),
    new THREE.MeshBasicMaterial({ map: shadowTex, transparent: true, opacity: 0.82, depthWrite: false, color: 0x000000 })
  );
  contact.rotation.x = -Math.PI / 2;
  contact.position.set(0, 0.007, 0.02);
  contact.renderOrder = 1;
  env.add(contact);

  /* ---------- room dimensions ---------- */
  const WALL_Z = -5.6;
  const FRONT_Z = 5.6;
  const SIDE_X = 6.2;
  const CEIL_Y = 5.2;
  const WAINSCOT_H = 1.24;

  const wallpaperMat = new THREE.MeshStandardMaterial({
    map: cloneRepeat(wallpaperTex.map, 8.0, 4.0),
    bumpMap: cloneRepeat(wallpaperTex.bumpMap, 8.0, 4.0),
    bumpScale: 0.012, roughness: 0.82, metalness: 0.0, envMapIntensity: 0.3,
  });
  const paintMat = new THREE.MeshPhysicalMaterial({
    map: cloneRepeat(painted.map, 7, 1),
    bumpMap: cloneRepeat(painted.bumpMap, 7, 1),
    bumpScale: 0.014,
    color: 0xf0e7d2, roughness: 0.56, metalness: 0.0,
    clearcoat: 0.16, clearcoatRoughness: 0.5, envMapIntensity: 0.45,
  });
  const paintDark = paintMat.clone();
  paintDark.color = new THREE.Color(0xd8cdb4);
  const trimMat = new THREE.MeshPhysicalMaterial({
    map: cloneRepeat(painted.map, 7, 1),
    bumpMap: cloneRepeat(painted.bumpMap, 7, 1),
    bumpScale: 0.01,
    color: 0xe2d6b8, roughness: 0.48, clearcoat: 0.24, clearcoatRoughness: 0.4,
    envMapIntensity: 0.5,
  });

  // shared wainscot panel geometry
  const pW = 0.86, pH = 0.94, pT = 0.075;
  const frameShape = new THREE.Shape();
  frameShape.moveTo(-pW / 2, -pH / 2); frameShape.lineTo(pW / 2, -pH / 2);
  frameShape.lineTo(pW / 2, pH / 2); frameShape.lineTo(-pW / 2, pH / 2); frameShape.lineTo(-pW / 2, -pH / 2);
  const hole = new THREE.Path();
  hole.moveTo(-pW / 2 + pT, -pH / 2 + pT); hole.lineTo(-pW / 2 + pT, pH / 2 - pT);
  hole.lineTo(pW / 2 - pT, pH / 2 - pT); hole.lineTo(pW / 2 - pT, -pH / 2 + pT);
  hole.lineTo(-pW / 2 + pT, -pH / 2 + pT);
  frameShape.holes.push(hole);
  const frameGeo = new THREE.ExtrudeGeometry(frameShape, {
    depth: 0.026, bevelEnabled: true, bevelThickness: 0.006, bevelSize: 0.006, bevelSegments: 2, curveSegments: 4,
  });
  frameGeo.computeVertexNormals();
  const innerGeo = new THREE.BoxGeometry(pW - pT * 2 - 0.035, pH - pT * 2 - 0.035, 0.015);

  function buildWall(width, panelCount, group) {
    // local +z faces into the room; wall front plane at local z=0
    const paper = new THREE.Mesh(new THREE.PlaneGeometry(width, CEIL_Y - WAINSCOT_H + 0.05), wallpaperMat);
    paper.position.set(0, WAINSCOT_H + (CEIL_Y - WAINSCOT_H) / 2, 0.001);
    paper.receiveShadow = true;
    group.add(paper);
    const lower = new THREE.Mesh(new THREE.PlaneGeometry(width, WAINSCOT_H), paintMat);
    lower.position.set(0, WAINSCOT_H / 2, 0.002);
    lower.receiveShadow = true;
    group.add(lower);

    const frames = new THREE.InstancedMesh(frameGeo, trimMat, panelCount);
    const inners = new THREE.InstancedMesh(innerGeo, paintDark, panelCount);
    const m4 = new THREE.Matrix4();
    const span = width - 1.4;
    for (let i = 0; i < panelCount; i++) {
      const x = -span / 2 + i * (span / (panelCount - 1));
      m4.makeTranslation(x, 0.63, 0.018);
      frames.setMatrixAt(i, m4);
      m4.makeTranslation(x, 0.63, 0.035);
      inners.setMatrixAt(i, m4);
    }
    frames.receiveShadow = true; inners.receiveShadow = true;
    group.add(frames, inners);

    const base = new THREE.Mesh(new RoundedBoxGeometry(width, 0.19, 0.06, 2, 0.008), trimMat);
    base.position.set(0, 0.095, 0.045); base.receiveShadow = true; group.add(base);
    const baseCap = new THREE.Mesh(new RoundedBoxGeometry(width, 0.025, 0.085, 2, 0.006), trimMat);
    baseCap.position.set(0, 0.196, 0.055); group.add(baseCap);
    const chair = new THREE.Mesh(new RoundedBoxGeometry(width, 0.055, 0.075, 2, 0.01), trimMat);
    chair.position.set(0, WAINSCOT_H, 0.05); group.add(chair);
    const chair2 = new THREE.Mesh(new RoundedBoxGeometry(width, 0.018, 0.1, 2, 0.005), trimMat);
    chair2.position.set(0, WAINSCOT_H + 0.039, 0.06); group.add(chair2);
    const crown = new THREE.Mesh(new RoundedBoxGeometry(width, 0.09, 0.12, 3, 0.015), trimMat);
    crown.position.set(0, CEIL_Y - 0.04, 0.055); group.add(crown);
    const crown2 = new THREE.Mesh(new RoundedBoxGeometry(width, 0.035, 0.16, 2, 0.01), trimMat);
    crown2.position.set(0, CEIL_Y - 0.105, 0.075); group.add(crown2);
  }

  // back wall
  const back = new THREE.Group();
  back.position.z = WALL_Z;
  buildWall(18, 17, back);
  env.add(back);

  // front wall (facing into the room, behind the hero camera — gives rear orbits a backdrop)
  const front = new THREE.Group();
  front.position.z = FRONT_Z;
  front.rotation.y = Math.PI;
  buildWall(18, 17, front);
  env.add(front);

  /* ---------- side walls ---------- */
  for (const sx of [-1, 1]) {
    const g = new THREE.Group();
    g.position.x = sx * SIDE_X;
    g.rotation.y = -sx * Math.PI / 2;
    env.add(g);
    const paperSide = new THREE.Mesh(new THREE.PlaneGeometry(12, CEIL_Y - WAINSCOT_H + 0.05), wallpaperMat);
    paperSide.position.set(0, WAINSCOT_H + (CEIL_Y - WAINSCOT_H) / 2, -0.01);
    paperSide.receiveShadow = true;
    g.add(paperSide);
    const lowerSide = new THREE.Mesh(new THREE.PlaneGeometry(12, WAINSCOT_H), paintMat);
    lowerSide.position.set(0, WAINSCOT_H / 2, 0.0);
    lowerSide.receiveShadow = true;
    g.add(lowerSide);
    for (let i = 0; i < 9; i++) {
      const x = -5.3 + i * 1.32;
      const fr = new THREE.Mesh(frameGeo, trimMat);
      fr.position.set(x, 0.63, 0.02); g.add(fr);
      const inn = new THREE.Mesh(innerGeo, paintDark);
      inn.position.set(x, 0.63, 0.038); g.add(inn);
    }
    const b = new THREE.Mesh(new RoundedBoxGeometry(12, 0.19, 0.06, 2, 0.008), trimMat);
    b.position.set(0, 0.095, 0.035); g.add(b);
    const bc = new THREE.Mesh(new RoundedBoxGeometry(12, 0.025, 0.085, 2, 0.006), trimMat);
    bc.position.set(0, 0.196, 0.045); g.add(bc);
    const ch = new THREE.Mesh(new RoundedBoxGeometry(12, 0.055, 0.075, 2, 0.01), trimMat);
    ch.position.set(0, WAINSCOT_H, 0.04); g.add(ch);
    const cr = new THREE.Mesh(new RoundedBoxGeometry(12, 0.09, 0.12, 3, 0.015), trimMat);
    cr.position.set(0, CEIL_Y - 0.05, 0.05); g.add(cr);
  }

  /* ---------- ceiling ---------- */
  const ceilMat = new THREE.MeshStandardMaterial({ color: 0x1c1610, roughness: 0.96, metalness: 0 });
  const ceil = new THREE.Mesh(new THREE.PlaneGeometry(14, 14), ceilMat);
  ceil.rotation.x = Math.PI / 2;
  ceil.position.y = CEIL_Y;
  env.add(ceil);

  /* ---------- sky dome ---------- */
  const sky = new THREE.Mesh(
    new THREE.SphereGeometry(38, 32, 20),
    new THREE.MeshBasicMaterial({ map: skyTex, side: THREE.BackSide, depthWrite: false })
  );
  sky.position.y = 2;
  env.add(sky);

  /* ---------- dust motes ---------- */
  const r = rng(5);
  const N = 220;
  const pos = new Float32Array(N * 3);
  const spd = new Float32Array(N);
  for (let i = 0; i < N; i++) {
    pos[i * 3] = (r() - 0.5) * 7.0;
    pos[i * 3 + 1] = 0.25 + r() * 3.4;
    pos[i * 3 + 2] = (r() - 0.5) * 5.5;
    spd[i] = 0.02 + r() * 0.06;
  }
  const dustGeo = new THREE.BufferGeometry();
  dustGeo.setAttribute('position', new THREE.BufferAttribute(pos, 3));
  const dustMat = new THREE.PointsMaterial({
    color: 0xffd9a3, size: 0.008, transparent: true, opacity: 0.5,
    depthWrite: false, blending: THREE.AdditiveBlending, sizeAttenuation: true,
  });
  const dust = new THREE.Points(dustGeo, dustMat);
  env.add(dust);

  env.userData.update = (dt, t) => {
    const p = dust.geometry.attributes.position;
    for (let i = 0; i < N; i++) {
      let y = p.getY(i) + spd[i] * dt;
      if (y > 3.8) y = 0.25;
      p.setY(i, y);
      p.setX(i, p.getX(i) + Math.sin(t * 0.25 + i) * 0.00035);
    }
    p.needsUpdate = true;
  };

  return env;
}