eloigil6 commited on
Commit
76a98b2
·
1 Parent(s): e4d14af

Refactor main.js to enhance scene setup and integrate world building. Updated camera positioning, lighting settings, and removed placeholder objects in favor of a new world.js module. Improved intro choreography and added debug functionality for skipping the intro.

Browse files
Files changed (2) hide show
  1. frontend/main.js +43 -123
  2. frontend/world.js +868 -0
frontend/main.js CHANGED
@@ -1,7 +1,9 @@
1
- // LoFinity — 3D scene. Stage 1: sky intro + camera descent to a
2
- // placeholder ground scene (the real low-poly world lands in stage 2).
 
3
 
4
  import * as THREE from "three";
 
5
 
6
  // ---------------------------------------------------------------------------
7
  // Renderer / scene / camera
@@ -11,9 +13,11 @@ const canvas = document.getElementById("scene");
11
  const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
12
  renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
13
  renderer.setSize(window.innerWidth, window.innerHeight);
 
 
14
 
15
  const scene = new THREE.Scene();
16
- scene.fog = new THREE.Fog(0xbfe3ff, 80, 380);
17
 
18
  const camera = new THREE.PerspectiveCamera(
19
  55,
@@ -23,9 +27,9 @@ const camera = new THREE.PerspectiveCamera(
23
  );
24
 
25
  // The camera stays put; we animate what it looks at.
26
- const CAMERA_POS = new THREE.Vector3(0, 5.5, 24);
27
  const LOOK_SKY = new THREE.Vector3(0, 90, -60);
28
- const LOOK_SCENE = new THREE.Vector3(0, 3, 0);
29
  camera.position.copy(CAMERA_POS);
30
  camera.lookAt(LOOK_SKY);
31
 
@@ -70,119 +74,26 @@ scene.add(new THREE.Mesh(new THREE.SphereGeometry(450, 32, 16), skyMaterial));
70
  // Lights
71
  // ---------------------------------------------------------------------------
72
 
73
- scene.add(new THREE.HemisphereLight(0xcfe8ff, 0x7ec850, 0.9));
74
- const sun = new THREE.DirectionalLight(0xfff4e0, 1.6);
75
- sun.position.set(40, 80, 30);
 
 
 
 
 
 
 
 
 
 
76
  scene.add(sun);
77
 
78
  // ---------------------------------------------------------------------------
79
- // Clouds: puffy clusters of flattened, flat-shaded icospheres
80
  // ---------------------------------------------------------------------------
81
 
82
- const cloudMaterial = new THREE.MeshLambertMaterial({
83
- color: 0xffffff,
84
- flatShading: true,
85
- });
86
-
87
- function makeCloud() {
88
- const cloud = new THREE.Group();
89
- const puffs = 5 + Math.floor(Math.random() * 4);
90
- for (let i = 0; i < puffs; i++) {
91
- const r = 3.5 + Math.random() * 4.5;
92
- const puff = new THREE.Mesh(new THREE.IcosahedronGeometry(r, 1), cloudMaterial);
93
- puff.position.set(
94
- (i - puffs / 2) * (r * 0.9) + (Math.random() - 0.5) * 3,
95
- (Math.random() - 0.5) * 2.5,
96
- (Math.random() - 0.5) * 4
97
- );
98
- puff.scale.y = 0.55;
99
- cloud.add(puff);
100
- }
101
- return cloud;
102
- }
103
-
104
- const clouds = [];
105
- for (let i = 0; i < 12; i++) {
106
- const cloud = makeCloud();
107
- cloud.position.set(
108
- -140 + Math.random() * 280,
109
- 32 + Math.random() * 50,
110
- -160 + Math.random() * 110
111
- );
112
- const s = 0.7 + Math.random() * 1.1;
113
- cloud.scale.setScalar(s);
114
- cloud.userData.speed = 0.5 + Math.random() * 0.8;
115
- clouds.push(cloud);
116
- scene.add(cloud);
117
- }
118
-
119
- // ---------------------------------------------------------------------------
120
- // Ground + placeholder props (replaced by the real scene in stage 2)
121
- // ---------------------------------------------------------------------------
122
-
123
- const ground = new THREE.Mesh(
124
- new THREE.CircleGeometry(320, 48),
125
- new THREE.MeshLambertMaterial({ color: 0x6abe4f })
126
- );
127
- ground.rotation.x = -Math.PI / 2;
128
- scene.add(ground);
129
-
130
- // Distant mountains
131
- const mountainMaterial = new THREE.MeshLambertMaterial({
132
- color: 0x4d8a4a,
133
- flatShading: true,
134
- });
135
- for (let i = 0; i < 7; i++) {
136
- const h = 24 + Math.random() * 26;
137
- const mountain = new THREE.Mesh(new THREE.ConeGeometry(h * 1.4, h, 5), mountainMaterial);
138
- mountain.position.set(-180 + i * 60 + (Math.random() - 0.5) * 25, h / 2 - 2, -190);
139
- mountain.rotation.y = Math.random() * Math.PI;
140
- scene.add(mountain);
141
- }
142
-
143
- // Placeholder vending machine: a friendly blue box
144
- const vending = new THREE.Group();
145
- const body = new THREE.Mesh(
146
- new THREE.BoxGeometry(2.4, 4.2, 1.8),
147
- new THREE.MeshLambertMaterial({ color: 0x6db9e8 })
148
- );
149
- body.position.y = 2.1;
150
- const front = new THREE.Mesh(
151
- new THREE.BoxGeometry(1.6, 3.0, 0.1),
152
- new THREE.MeshLambertMaterial({ color: 0xeaf6ff })
153
- );
154
- front.position.set(-0.2, 2.5, 0.92);
155
- vending.add(body, front);
156
- vending.position.set(-3.5, 0, 0);
157
- scene.add(vending);
158
-
159
- // Placeholder bench
160
- const benchMaterial = new THREE.MeshLambertMaterial({ color: 0x4a90c2 });
161
- const bench = new THREE.Group();
162
- const seat = new THREE.Mesh(new THREE.BoxGeometry(4, 0.25, 1.2), benchMaterial);
163
- seat.position.y = 1;
164
- const back = new THREE.Mesh(new THREE.BoxGeometry(4, 1.1, 0.2), benchMaterial);
165
- back.position.set(0, 1.7, -0.5);
166
- bench.add(seat, back);
167
- bench.position.set(3, 0, 1);
168
- scene.add(bench);
169
-
170
- // Placeholder tree
171
- const tree = new THREE.Group();
172
- const trunk = new THREE.Mesh(
173
- new THREE.CylinderGeometry(0.4, 0.6, 5, 7),
174
- new THREE.MeshLambertMaterial({ color: 0x8a5a33, flatShading: true })
175
- );
176
- trunk.position.y = 2.5;
177
- tree.add(trunk);
178
- const leafMaterial = new THREE.MeshLambertMaterial({ color: 0x4caf3f, flatShading: true });
179
- for (let i = 0; i < 4; i++) {
180
- const leaves = new THREE.Mesh(new THREE.IcosahedronGeometry(2.2 - i * 0.3, 1), leafMaterial);
181
- leaves.position.set((Math.random() - 0.5) * 1.6, 5.2 + i * 1.2, (Math.random() - 0.5) * 1.6);
182
- tree.add(leaves);
183
- }
184
- tree.position.set(-9, 0, -4);
185
- scene.add(tree);
186
 
187
  // ---------------------------------------------------------------------------
188
  // Intro: hold on the sky, then ease the gaze down to the scene
@@ -194,17 +105,27 @@ const DESCENT_MS = 3400;
194
  const intro = { phase: "loading", t0: 0 };
195
  const lookTarget = LOOK_SKY.clone();
196
 
197
- window.addEventListener("load", () => {
198
- setTimeout(() => {
199
- intro.phase = "descending";
200
- intro.t0 = performance.now();
201
- document.getElementById("title-overlay").classList.add("hidden");
202
- }, HOLD_MS);
203
- });
 
204
 
205
  const easeInOutCubic = (t) =>
206
  t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
207
 
 
 
 
 
 
 
 
 
 
208
  // ---------------------------------------------------------------------------
209
  // Render loop
210
  // ---------------------------------------------------------------------------
@@ -214,11 +135,10 @@ const clock = new THREE.Clock();
214
  function animate() {
215
  requestAnimationFrame(animate);
216
  const elapsed = clock.getElapsedTime();
217
- const dt = clock.getDelta();
218
 
219
- for (const cloud of clouds) {
220
  cloud.position.x += cloud.userData.speed * 0.016;
221
- if (cloud.position.x > 160) cloud.position.x = -160;
222
  }
223
 
224
  if (intro.phase === "descending") {
 
1
+ // LoFinity — entry point: renderer, camera, sky, intro choreography and the
2
+ // backend bridge. The world itself (vending machine, house, fields…) is
3
+ // built in world.js.
4
 
5
  import * as THREE from "three";
6
+ import { buildWorld } from "/static/world.js";
7
 
8
  // ---------------------------------------------------------------------------
9
  // Renderer / scene / camera
 
13
  const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
14
  renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
15
  renderer.setSize(window.innerWidth, window.innerHeight);
16
+ renderer.shadowMap.enabled = true;
17
+ renderer.shadowMap.type = THREE.PCFSoftShadowMap;
18
 
19
  const scene = new THREE.Scene();
20
+ scene.fog = new THREE.Fog(0xbfe3ff, 90, 420);
21
 
22
  const camera = new THREE.PerspectiveCamera(
23
  55,
 
27
  );
28
 
29
  // The camera stays put; we animate what it looks at.
30
+ const CAMERA_POS = new THREE.Vector3(0, 4.8, 17);
31
  const LOOK_SKY = new THREE.Vector3(0, 90, -60);
32
+ const LOOK_SCENE = new THREE.Vector3(-0.5, 2.8, -3);
33
  camera.position.copy(CAMERA_POS);
34
  camera.lookAt(LOOK_SKY);
35
 
 
74
  // Lights
75
  // ---------------------------------------------------------------------------
76
 
77
+ scene.add(new THREE.HemisphereLight(0xcfe8ff, 0xa8b48a, 0.85));
78
+
79
+ const sun = new THREE.DirectionalLight(0xfff2dc, 1.6);
80
+ sun.position.set(30, 55, 20);
81
+ sun.castShadow = true;
82
+ sun.shadow.mapSize.set(2048, 2048);
83
+ sun.shadow.camera.left = -45;
84
+ sun.shadow.camera.right = 45;
85
+ sun.shadow.camera.top = 45;
86
+ sun.shadow.camera.bottom = -45;
87
+ sun.shadow.camera.near = 5;
88
+ sun.shadow.camera.far = 150;
89
+ sun.shadow.bias = -0.0005;
90
  scene.add(sun);
91
 
92
  // ---------------------------------------------------------------------------
93
+ // World
94
  // ---------------------------------------------------------------------------
95
 
96
+ const world = buildWorld(scene);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  // ---------------------------------------------------------------------------
99
  // Intro: hold on the sky, then ease the gaze down to the scene
 
105
  const intro = { phase: "loading", t0: 0 };
106
  const lookTarget = LOOK_SKY.clone();
107
 
108
+ function startDescent() {
109
+ if (intro.phase !== "loading") return;
110
+ intro.phase = "descending";
111
+ intro.t0 = performance.now();
112
+ document.getElementById("title-overlay").classList.add("hidden");
113
+ }
114
+
115
+ window.addEventListener("load", () => setTimeout(startDescent, HOLD_MS));
116
 
117
  const easeInOutCubic = (t) =>
118
  t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
119
 
120
+ // Dev helper: jump straight to the post-intro state.
121
+ window.lofinityDebug = {
122
+ skipIntro() {
123
+ intro.phase = "idle";
124
+ lookTarget.copy(LOOK_SCENE);
125
+ document.getElementById("title-overlay").classList.add("hidden");
126
+ },
127
+ };
128
+
129
  // ---------------------------------------------------------------------------
130
  // Render loop
131
  // ---------------------------------------------------------------------------
 
135
  function animate() {
136
  requestAnimationFrame(animate);
137
  const elapsed = clock.getElapsedTime();
 
138
 
139
+ for (const cloud of world.clouds) {
140
  cloud.position.x += cloud.userData.speed * 0.016;
141
+ if (cloud.position.x > 170) cloud.position.x = -170;
142
  }
143
 
144
  if (intro.phase === "descending") {
frontend/world.js ADDED
@@ -0,0 +1,868 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // LoFinity — world builder. Constructs the full low-poly scene based on the
2
+ // reference art: sidewalk, vending machine, bench, Asahi Village sign, stone
3
+ // wall, big tree, dirt path, fences, sunflowers, crop fields, house with blue
4
+ // roof, clothesline, lamp post, forest, mountains and clouds.
5
+
6
+ import * as THREE from "three";
7
+
8
+ // --- small helpers ----------------------------------------------------------
9
+
10
+ const lambert = (color, opts = {}) =>
11
+ new THREE.MeshLambertMaterial({ color, ...opts });
12
+
13
+ function box(w, h, d, material) {
14
+ return new THREE.Mesh(new THREE.BoxGeometry(w, h, d), material);
15
+ }
16
+
17
+ function cyl(rTop, rBottom, h, seg, material) {
18
+ return new THREE.Mesh(new THREE.CylinderGeometry(rTop, rBottom, h, seg), material);
19
+ }
20
+
21
+ function ico(r, detail, material) {
22
+ return new THREE.Mesh(new THREE.IcosahedronGeometry(r, detail), material);
23
+ }
24
+
25
+ function shadows(object, cast = true, receive = true) {
26
+ object.traverse((o) => {
27
+ if (o.isMesh) {
28
+ o.castShadow = cast;
29
+ o.receiveShadow = receive;
30
+ }
31
+ });
32
+ return object;
33
+ }
34
+
35
+ function canvasTexture(width, height, draw) {
36
+ const canvas = document.createElement("canvas");
37
+ canvas.width = width;
38
+ canvas.height = height;
39
+ draw(canvas.getContext("2d"), width, height);
40
+ const tex = new THREE.CanvasTexture(canvas);
41
+ tex.anisotropy = 4;
42
+ return tex;
43
+ }
44
+
45
+ const rand = (a, b) => a + Math.random() * (b - a);
46
+ const pick = (arr) => arr[Math.floor(Math.random() * arr.length)];
47
+
48
+ // --- palette ----------------------------------------------------------------
49
+
50
+ const GREENS = [0x4caf3f, 0x46a338, 0x57bd49];
51
+ const DARK_GREENS = [0x3c7d36, 0x35702f, 0x44883d];
52
+ const WOOD = 0x7a5230;
53
+ const WOOD_DARK = 0x5d3e23;
54
+
55
+ // --- sidewalk ---------------------------------------------------------------
56
+
57
+ function buildSidewalk() {
58
+ const g = new THREE.Group();
59
+ const slab = box(80, 0.3, 6, lambert(0xc9cdd2));
60
+ slab.position.set(0, 0.15, 5);
61
+ g.add(slab);
62
+
63
+ const curb = box(80, 0.34, 0.35, lambert(0xb0b5bb));
64
+ curb.position.set(0, 0.17, 8.1);
65
+ g.add(curb);
66
+
67
+ const jointMaterial = lambert(0xb4b9bf);
68
+ for (let x = -36; x <= 36; x += 4) {
69
+ const joint = box(0.07, 0.31, 6, jointMaterial);
70
+ joint.position.set(x, 0.155, 5);
71
+ g.add(joint);
72
+ }
73
+ return shadows(g, false, true);
74
+ }
75
+
76
+ // --- dirt path --------------------------------------------------------------
77
+
78
+ function buildPath() {
79
+ // Shape coords: x = world x, y = depth (mapped to -z after rotation)
80
+ const shape = new THREE.Shape();
81
+ shape.moveTo(0.2, -2);
82
+ shape.lineTo(0.8, 6);
83
+ shape.lineTo(2.2, 14);
84
+ shape.lineTo(1.2, 24);
85
+ shape.lineTo(0.5, 34);
86
+ shape.lineTo(1.0, 40);
87
+ shape.lineTo(3.0, 40);
88
+ shape.lineTo(3.4, 30);
89
+ shape.lineTo(4.4, 20);
90
+ shape.lineTo(5.2, 12);
91
+ shape.lineTo(4.4, 4);
92
+ shape.lineTo(3.8, -2);
93
+ shape.closePath();
94
+
95
+ const path = new THREE.Mesh(
96
+ new THREE.ShapeGeometry(shape),
97
+ lambert(0xcfb579)
98
+ );
99
+ path.rotation.x = -Math.PI / 2;
100
+ path.position.y = 0.02;
101
+ path.receiveShadow = true;
102
+ return path;
103
+ }
104
+
105
+ // --- Asahi Village sign -----------------------------------------------------
106
+
107
+ function signBoard(w, h, drawText) {
108
+ const g = new THREE.Group();
109
+ const board = box(w, h, 0.12, lambert(0x8a6740));
110
+ g.add(board);
111
+
112
+ const frame = box(w + 0.08, h + 0.08, 0.08, lambert(WOOD_DARK));
113
+ frame.position.z = -0.03;
114
+ g.add(frame);
115
+
116
+ const tex = canvasTexture(512, Math.round((512 * h) / w), drawText);
117
+ const text = new THREE.Mesh(
118
+ new THREE.PlaneGeometry(w * 0.96, h * 0.96),
119
+ new THREE.MeshBasicMaterial({ map: tex, transparent: true })
120
+ );
121
+ text.position.z = 0.065;
122
+ g.add(text);
123
+ return g;
124
+ }
125
+
126
+ function buildSign() {
127
+ const g = new THREE.Group();
128
+ const postMaterial = lambert(WOOD_DARK);
129
+ for (const x of [-0.85, 0.85]) {
130
+ const post = box(0.16, 3.1, 0.16, postMaterial);
131
+ post.position.set(x, 1.55, -0.17);
132
+ g.add(post);
133
+ }
134
+
135
+ const cream = "#f5ead0";
136
+ const textStyle = (ctx, size) => {
137
+ ctx.fillStyle = cream;
138
+ ctx.textAlign = "center";
139
+ ctx.textBaseline = "middle";
140
+ ctx.font = `bold ${size}px 'Baloo 2', 'Hiragino Sans', sans-serif`;
141
+ };
142
+
143
+ const b1 = signBoard(2.3, 0.62, (ctx, w, h) => {
144
+ textStyle(ctx, 92);
145
+ ctx.fillText("あさひ村 ☀", w / 2, h / 2 + 4);
146
+ });
147
+ b1.position.y = 2.75;
148
+
149
+ const b2 = signBoard(2.3, 0.5, (ctx, w, h) => {
150
+ textStyle(ctx, 76);
151
+ ctx.fillText("Asahi Village", w / 2, h / 2 + 4);
152
+ });
153
+ b2.position.y = 2.05;
154
+
155
+ const b3 = signBoard(2.3, 0.55, (ctx, w, h) => {
156
+ textStyle(ctx, 84);
157
+ ctx.fillText("← 2km 先", w / 2, h / 2 + 4);
158
+ });
159
+ b3.position.y = 1.35;
160
+
161
+ g.add(b1, b2, b3);
162
+ return shadows(g);
163
+ }
164
+
165
+ function buildFlowerPot() {
166
+ const g = new THREE.Group();
167
+ const pot = cyl(0.34, 0.24, 0.45, 10, lambert(0xb86b4b));
168
+ pot.position.y = 0.22;
169
+ g.add(pot);
170
+
171
+ const foliage = ico(0.42, 1, lambert(pick(GREENS), { flatShading: true }));
172
+ foliage.position.y = 0.64;
173
+ foliage.scale.y = 0.8;
174
+ g.add(foliage);
175
+
176
+ for (let i = 0; i < 7; i++) {
177
+ const flower = ico(0.075, 0, lambert(pick([0xf2d23c, 0xffffff, 0xf2a83c])));
178
+ const a = (i / 7) * Math.PI * 2;
179
+ flower.position.set(Math.cos(a) * 0.3, 0.78 + rand(-0.06, 0.1), Math.sin(a) * 0.3);
180
+ g.add(flower);
181
+ }
182
+ return shadows(g);
183
+ }
184
+
185
+ // --- stone wall -------------------------------------------------------------
186
+
187
+ function buildStoneWall() {
188
+ const g = new THREE.Group();
189
+ const grays = [0x9aa0a6, 0x8a9096, 0xa8aeb4].map((c) =>
190
+ lambert(c, { flatShading: true })
191
+ );
192
+ for (let row = 0; row < 3; row++) {
193
+ for (let col = 0; col < 9; col++) {
194
+ const stone = box(rand(0.7, 1.0), rand(0.42, 0.55), rand(0.5, 0.7), pick(grays));
195
+ stone.position.set(
196
+ col * 0.85 - 3.4 + (row % 2) * 0.4,
197
+ 0.28 + row * 0.48,
198
+ rand(-0.06, 0.06)
199
+ );
200
+ stone.rotation.y = rand(-0.08, 0.08);
201
+ g.add(stone);
202
+ }
203
+ }
204
+ // greenery peeking over the wall
205
+ for (let i = 0; i < 5; i++) {
206
+ const shrub = ico(rand(0.5, 0.85), 1, lambert(pick(DARK_GREENS), { flatShading: true }));
207
+ shrub.position.set(rand(-3.4, 3.8), rand(1.7, 2.1), rand(-0.7, -0.4));
208
+ shrub.scale.y = 0.75;
209
+ g.add(shrub);
210
+ }
211
+ return shadows(g);
212
+ }
213
+
214
+ // --- vending machine --------------------------------------------------------
215
+
216
+ function buildVendingMachine() {
217
+ const g = new THREE.Group();
218
+
219
+ const body = box(2.6, 4.4, 1.9, lambert(0x8ed0f4));
220
+ body.position.y = 2.2;
221
+ g.add(body);
222
+
223
+ const trim = box(2.7, 0.18, 2.0, lambert(0x6db8e0));
224
+ trim.position.y = 4.42;
225
+ g.add(trim);
226
+
227
+ const feetMaterial = lambert(0x33444f);
228
+ for (const [fx, fz] of [[-1.1, 0.75], [1.1, 0.75], [-1.1, -0.75], [1.1, -0.75]]) {
229
+ const foot = box(0.22, 0.14, 0.22, feetMaterial);
230
+ foot.position.set(fx, 0.07, fz);
231
+ g.add(foot);
232
+ }
233
+
234
+ // Lit display window with bottle shelves
235
+ const displayBack = new THREE.Mesh(
236
+ new THREE.PlaneGeometry(1.78, 2.25),
237
+ new THREE.MeshBasicMaterial({ color: 0xf4fbff })
238
+ );
239
+ displayBack.position.set(-0.3, 3.05, 0.955);
240
+ g.add(displayBack);
241
+
242
+ const bottleGeometry = new THREE.CylinderGeometry(0.075, 0.075, 0.32, 8);
243
+ const capGeometry = new THREE.CylinderGeometry(0.04, 0.04, 0.07, 8);
244
+ const shelfMaterial = new THREE.MeshBasicMaterial({ color: 0xdfe9ef });
245
+ const rows = [
246
+ { y: 3.72, colors: [0x3a8fd9, 0x6db3e8, 0xffffff, 0x4aa3e0, 0xe8a13c, 0x3a8fd9, 0x9fd0f0] },
247
+ { y: 3.05, colors: [0xe78a2e, 0xf2b53c, 0xd96f28, 0xf2cf5b, 0xe78a2e, 0xc9892f, 0xf2b53c] },
248
+ { y: 2.38, colors: [0x7a4e2a, 0xa06a38, 0xcaa468, 0x5d3a1f, 0xa06a38, 0x7a4e2a, 0xcaa468] },
249
+ ];
250
+ for (const row of rows) {
251
+ const shelf = box(1.7, 0.05, 0.05, shelfMaterial);
252
+ shelf.position.set(-0.3, row.y - 0.2, 0.98);
253
+ g.add(shelf);
254
+ row.colors.forEach((color, i) => {
255
+ const bottle = new THREE.Mesh(bottleGeometry, lambert(color));
256
+ const x = -0.3 + (i - 3) * 0.23;
257
+ bottle.position.set(x, row.y, 0.99);
258
+ g.add(bottle);
259
+ const cap = new THREE.Mesh(capGeometry, lambert(0xeeeeee));
260
+ cap.position.set(x, row.y + 0.19, 0.99);
261
+ g.add(cap);
262
+ });
263
+ }
264
+
265
+ // Glass pane over the display
266
+ const glass = new THREE.Mesh(
267
+ new THREE.PlaneGeometry(1.82, 2.3),
268
+ new THREE.MeshLambertMaterial({ color: 0xcfe9ff, transparent: true, opacity: 0.14 })
269
+ );
270
+ glass.position.set(-0.3, 3.05, 1.1);
271
+ g.add(glass);
272
+
273
+ // Right control column
274
+ const panel = box(0.62, 2.25, 0.06, lambert(0x35586e));
275
+ panel.position.set(0.86, 3.05, 0.95);
276
+ g.add(panel);
277
+
278
+ const screen = new THREE.Mesh(
279
+ new THREE.PlaneGeometry(0.4, 0.22),
280
+ new THREE.MeshBasicMaterial({ color: 0x1c2f28 })
281
+ );
282
+ screen.position.set(0.86, 3.75, 0.99);
283
+ g.add(screen);
284
+
285
+ const coinSlot = box(0.07, 0.2, 0.03, lambert(0x10181d));
286
+ coinSlot.position.set(0.86, 3.3, 0.985);
287
+ g.add(coinSlot);
288
+
289
+ const button = box(0.18, 0.1, 0.04, lambert(0xd9534f));
290
+ button.position.set(0.86, 2.95, 0.985);
291
+ g.add(button);
292
+
293
+ const returnFlap = box(0.26, 0.16, 0.03, lambert(0x10181d));
294
+ returnFlap.position.set(0.86, 2.25, 0.985);
295
+ g.add(returnFlap);
296
+
297
+ // Poster: おいしい水
298
+ const posterTex = canvasTexture(256, 224, (ctx, w, h) => {
299
+ ctx.fillStyle = "#f7fcff";
300
+ ctx.fillRect(0, 0, w, h);
301
+ ctx.strokeStyle = "#7cc4ea";
302
+ ctx.lineWidth = 10;
303
+ ctx.strokeRect(5, 5, w - 10, h - 10);
304
+ // sky + mountains
305
+ ctx.fillStyle = "#bfe3ff";
306
+ ctx.fillRect(18, 86, w - 36, h - 110);
307
+ ctx.fillStyle = "#4a7fb5";
308
+ ctx.beginPath();
309
+ ctx.moveTo(30, h - 26);
310
+ ctx.lineTo(95, 96);
311
+ ctx.lineTo(160, h - 26);
312
+ ctx.closePath();
313
+ ctx.fill();
314
+ ctx.fillStyle = "#6da3d8";
315
+ ctx.beginPath();
316
+ ctx.moveTo(120, h - 26);
317
+ ctx.lineTo(180, 110);
318
+ ctx.lineTo(236, h - 26);
319
+ ctx.closePath();
320
+ ctx.fill();
321
+ ctx.fillStyle = "#2e6da4";
322
+ ctx.font = "bold 44px 'Hiragino Sans', sans-serif";
323
+ ctx.textAlign = "center";
324
+ ctx.fillText("おいしい水", w / 2, 62);
325
+ });
326
+ const poster = new THREE.Mesh(
327
+ new THREE.PlaneGeometry(1.05, 0.92),
328
+ new THREE.MeshBasicMaterial({ map: posterTex })
329
+ );
330
+ poster.position.set(-0.55, 1.25, 0.96);
331
+ g.add(poster);
332
+
333
+ // Dispenser slot
334
+ const dispenser = box(1.5, 0.55, 0.1, lambert(0x2e4a5c));
335
+ dispenser.position.set(-0.3, 0.52, 0.93);
336
+ g.add(dispenser);
337
+ const dispenserInner = box(1.3, 0.38, 0.06, lambert(0x10181d));
338
+ dispenserInner.position.set(-0.3, 0.5, 0.97);
339
+ g.add(dispenserInner);
340
+
341
+ // Door handle
342
+ const handle = box(0.1, 1.2, 0.07, lambert(0xd8e6ee));
343
+ handle.position.set(0.45, 2.9, 1.0);
344
+ g.add(handle);
345
+
346
+ return shadows(g);
347
+ }
348
+
349
+ // --- bench ------------------------------------------------------------------
350
+
351
+ function buildBench() {
352
+ const g = new THREE.Group();
353
+ const blue = lambert(0x4a90c2);
354
+ const darkBlue = lambert(0x2c5d8a);
355
+
356
+ for (let i = 0; i < 3; i++) {
357
+ const slat = box(3.8, 0.1, 0.32, blue);
358
+ slat.position.set(0, 1.0, -0.4 + i * 0.4);
359
+ g.add(slat);
360
+ }
361
+ for (let i = 0; i < 2; i++) {
362
+ const slat = box(3.8, 0.32, 0.1, blue);
363
+ slat.position.set(0, 1.5 + i * 0.45, -0.62);
364
+ g.add(slat);
365
+ }
366
+ for (const x of [-1.7, 1.7]) {
367
+ const leg = box(0.14, 1.0, 0.9, darkBlue);
368
+ leg.position.set(x, 0.5, -0.05);
369
+ g.add(leg);
370
+ const backPost = box(0.14, 1.1, 0.14, darkBlue);
371
+ backPost.position.set(x, 1.55, -0.6);
372
+ g.add(backPost);
373
+ const arm = box(0.5, 0.1, 0.7, darkBlue);
374
+ arm.position.set(x, 1.35, -0.15);
375
+ g.add(arm);
376
+ }
377
+ return shadows(g);
378
+ }
379
+
380
+ // --- lamp post ---------------------------------------------------------------
381
+
382
+ function buildLamp() {
383
+ const g = new THREE.Group();
384
+ const gray = lambert(0x9aa3ab);
385
+
386
+ const base = cyl(0.22, 0.3, 0.5, 10, gray);
387
+ base.position.y = 0.25;
388
+ g.add(base);
389
+
390
+ const pole = cyl(0.1, 0.14, 8, 10, gray);
391
+ pole.position.y = 4.25;
392
+ g.add(pole);
393
+
394
+ const arm = cyl(0.08, 0.08, 2.0, 8, gray);
395
+ arm.position.set(-0.85, 8.5, 0);
396
+ arm.rotation.z = 1.15;
397
+ g.add(arm);
398
+
399
+ const head = box(1.0, 0.22, 0.42, gray);
400
+ head.position.set(-1.85, 8.85, 0);
401
+ g.add(head);
402
+
403
+ const bulb = new THREE.Mesh(
404
+ new THREE.PlaneGeometry(0.8, 0.3),
405
+ new THREE.MeshBasicMaterial({ color: 0xfff7d6 })
406
+ );
407
+ bulb.rotation.x = Math.PI / 2;
408
+ bulb.position.set(-1.85, 8.73, 0);
409
+ g.add(bulb);
410
+
411
+ return shadows(g);
412
+ }
413
+
414
+ // --- trees ------------------------------------------------------------------
415
+
416
+ function buildBigTree() {
417
+ const g = new THREE.Group();
418
+ const trunk = cyl(0.5, 0.75, 7, 8, lambert(0x8a5a33, { flatShading: true }));
419
+ trunk.position.y = 3.5;
420
+ g.add(trunk);
421
+
422
+ const branch = cyl(0.2, 0.32, 3.2, 7, lambert(0x8a5a33, { flatShading: true }));
423
+ branch.position.set(1.6, 6.4, 0.2);
424
+ branch.rotation.z = -0.7;
425
+ g.add(branch);
426
+
427
+ const canopySpec = [
428
+ [0, 8.5, 0, 3.2],
429
+ [2.6, 9.4, 0.5, 2.6],
430
+ [-2.2, 9.6, -0.4, 2.4],
431
+ [1.0, 11.0, 0, 2.2],
432
+ [4.6, 8.6, 0.8, 2.0],
433
+ ];
434
+ const canopy = [];
435
+ for (const [x, y, z, r] of canopySpec) {
436
+ const leaves = ico(r, 1, lambert(pick(GREENS), { flatShading: true }));
437
+ leaves.position.set(x, y, z);
438
+ leaves.scale.y = 0.85;
439
+ canopy.push(leaves);
440
+ g.add(leaves);
441
+ }
442
+ g.userData.canopy = canopy;
443
+ return shadows(g);
444
+ }
445
+
446
+ function buildBlobTree(scale = 1) {
447
+ const g = new THREE.Group();
448
+ const trunk = cyl(0.25 * scale, 0.4 * scale, 2.6 * scale, 7, lambert(0x7a5230));
449
+ trunk.position.y = 1.3 * scale;
450
+ g.add(trunk);
451
+ const blobs = 2 + Math.floor(Math.random() * 3);
452
+ for (let i = 0; i < blobs; i++) {
453
+ const leaves = ico(rand(1.1, 1.8) * scale, 1, lambert(pick(DARK_GREENS), { flatShading: true }));
454
+ leaves.position.set(
455
+ rand(-0.8, 0.8) * scale,
456
+ (2.8 + i * 1.0) * scale,
457
+ rand(-0.8, 0.8) * scale
458
+ );
459
+ g.add(leaves);
460
+ }
461
+ return shadows(g);
462
+ }
463
+
464
+ function buildPine(scale = 1) {
465
+ const g = new THREE.Group();
466
+ const trunk = cyl(0.2 * scale, 0.3 * scale, 1.6 * scale, 6, lambert(WOOD));
467
+ trunk.position.y = 0.8 * scale;
468
+ g.add(trunk);
469
+ for (let i = 0; i < 3; i++) {
470
+ const cone = new THREE.Mesh(
471
+ new THREE.ConeGeometry((2.0 - i * 0.45) * scale, 2.2 * scale, 7),
472
+ lambert(pick(DARK_GREENS), { flatShading: true })
473
+ );
474
+ cone.position.y = (2.2 + i * 1.3) * scale;
475
+ g.add(cone);
476
+ }
477
+ return shadows(g);
478
+ }
479
+
480
+ // --- fences -----------------------------------------------------------------
481
+
482
+ function buildFenceRun(from, to) {
483
+ const g = new THREE.Group();
484
+ const dir = new THREE.Vector2(to.x - from.x, to.z - from.z);
485
+ const length = dir.length();
486
+ const angle = Math.atan2(dir.y, dir.x);
487
+ const postMaterial = lambert(WOOD);
488
+ const railMaterial = lambert(0x8a6238);
489
+
490
+ const posts = Math.max(2, Math.round(length / 2.2) + 1);
491
+ for (let i = 0; i < posts; i++) {
492
+ const t = i / (posts - 1);
493
+ const post = box(0.15, 1.1, 0.15, postMaterial);
494
+ post.position.set(from.x + dir.x * t, 0.55, from.z + dir.y * t);
495
+ g.add(post);
496
+ }
497
+ for (const y of [0.45, 0.85]) {
498
+ const rail = box(length, 0.09, 0.07, railMaterial);
499
+ rail.position.set(from.x + dir.x / 2, y, from.z + dir.y / 2);
500
+ rail.rotation.y = -angle;
501
+ g.add(rail);
502
+ }
503
+ return shadows(g);
504
+ }
505
+
506
+ // --- sunflowers & flowers ----------------------------------------------------
507
+
508
+ function buildSunflower() {
509
+ const g = new THREE.Group();
510
+ const h = rand(1.5, 2.1);
511
+ const stem = cyl(0.04, 0.05, h, 6, lambert(0x4a8c3a));
512
+ stem.position.y = h / 2;
513
+ g.add(stem);
514
+
515
+ const head = new THREE.Group();
516
+ const petals = new THREE.Mesh(
517
+ new THREE.CylinderGeometry(0.3, 0.3, 0.06, 12),
518
+ lambert(0xf2c83c)
519
+ );
520
+ petals.rotation.x = Math.PI / 2;
521
+ head.add(petals);
522
+ const center = new THREE.Mesh(
523
+ new THREE.CylinderGeometry(0.14, 0.14, 0.08, 10),
524
+ lambert(0x6e4a2f)
525
+ );
526
+ center.rotation.x = Math.PI / 2;
527
+ center.position.z = 0.04;
528
+ head.add(center);
529
+ head.position.set(0, h, 0.05);
530
+ head.rotation.x = rand(-0.15, 0.1);
531
+ g.add(head);
532
+
533
+ const leaf = ico(0.12, 0, lambert(0x4a8c3a));
534
+ leaf.scale.set(1.6, 0.5, 1);
535
+ leaf.position.set(0.12, h * 0.55, 0);
536
+ g.add(leaf);
537
+ return shadows(g);
538
+ }
539
+
540
+ function buildFlowerBush() {
541
+ const g = new THREE.Group();
542
+ const bush = ico(rand(0.35, 0.55), 1, lambert(pick(GREENS), { flatShading: true }));
543
+ bush.position.y = 0.35;
544
+ bush.scale.y = 0.8;
545
+ g.add(bush);
546
+ for (let i = 0; i < 6; i++) {
547
+ const flower = ico(0.06, 0, lambert(pick([0x6f8fd8, 0x9fb8f0, 0xffffff])));
548
+ const a = Math.random() * Math.PI * 2;
549
+ flower.position.set(Math.cos(a) * 0.3, rand(0.5, 0.7), Math.sin(a) * 0.3);
550
+ g.add(flower);
551
+ }
552
+ return shadows(g);
553
+ }
554
+
555
+ // --- crop fields ------------------------------------------------------------
556
+
557
+ function buildCropField(rows, rowLength) {
558
+ const g = new THREE.Group();
559
+ const rowColors = [0x9fb83e, 0x7da33c, 0xb5c44a];
560
+ for (let i = 0; i < rows; i++) {
561
+ const row = box(1.0, 0.24, rowLength, lambert(rowColors[i % 3]));
562
+ row.position.set(i * 1.05, 0.12, 0);
563
+ g.add(row);
564
+ }
565
+ return shadows(g, false, true);
566
+ }
567
+
568
+ // --- house ------------------------------------------------------------------
569
+
570
+ function buildHouse() {
571
+ const g = new THREE.Group();
572
+ const wallMaterial = lambert(0xf2efe6);
573
+ const roofMaterial = lambert(0x4a7fb5, { flatShading: true });
574
+ const darkWood = lambert(0x4a3b2c);
575
+
576
+ const walls = box(8, 3.4, 6, wallMaterial);
577
+ walls.position.y = 1.7;
578
+ g.add(walls);
579
+
580
+ // Gable roof: two slabs + ridge + triangle fills
581
+ const pitch = 0.52;
582
+ const slabLength = 4.1;
583
+ for (const side of [-1, 1]) {
584
+ const slab = box(9.2, 0.18, slabLength, roofMaterial);
585
+ slab.rotation.x = side * pitch;
586
+ slab.position.set(
587
+ 0,
588
+ 3.4 + (slabLength / 2) * Math.sin(pitch) - 0.05,
589
+ side * (slabLength / 2) * Math.cos(pitch)
590
+ );
591
+ g.add(slab);
592
+ }
593
+ const ridge = box(9.3, 0.18, 0.5, lambert(0x3e6c9d));
594
+ ridge.position.set(0, 3.4 + slabLength * Math.sin(pitch) - 0.05, 0);
595
+ g.add(ridge);
596
+
597
+ for (const side of [-1, 1]) {
598
+ const triangle = new THREE.Shape();
599
+ triangle.moveTo(-3, 0);
600
+ triangle.lineTo(3, 0);
601
+ triangle.lineTo(0, 1.95);
602
+ triangle.closePath();
603
+ const gable = new THREE.Mesh(
604
+ new THREE.ExtrudeGeometry(triangle, { depth: 0.2, bevelEnabled: false }),
605
+ wallMaterial
606
+ );
607
+ gable.rotation.y = side * Math.PI / 2;
608
+ gable.position.set(side * 3.9, 3.4, side * 0.1);
609
+ g.add(gable);
610
+ }
611
+
612
+ // Dormer, tucked into the front roof slope
613
+ const dormer = box(1.1, 0.85, 1.0, wallMaterial);
614
+ dormer.position.set(-1.8, 3.95, 1.75);
615
+ g.add(dormer);
616
+ const dormerRoof = box(1.35, 0.12, 1.15, roofMaterial);
617
+ dormerRoof.rotation.x = 0.42;
618
+ dormerRoof.position.set(-1.8, 4.5, 1.75);
619
+ g.add(dormerRoof);
620
+ const dormerWindow = new THREE.Mesh(
621
+ new THREE.PlaneGeometry(0.55, 0.45),
622
+ new THREE.MeshBasicMaterial({ color: 0xbfe0ea })
623
+ );
624
+ dormerWindow.position.set(-1.8, 3.98, 2.26);
625
+ g.add(dormerWindow);
626
+
627
+ // Porch
628
+ const porchRoof = box(3.2, 0.14, 2.2, roofMaterial);
629
+ porchRoof.rotation.x = 0.18;
630
+ porchRoof.position.set(2.0, 2.9, 3.9);
631
+ g.add(porchRoof);
632
+ for (const x of [0.7, 3.3]) {
633
+ const post = box(0.16, 2.6, 0.16, darkWood);
634
+ post.position.set(x, 1.3, 4.7);
635
+ g.add(post);
636
+ }
637
+ const porchFloor = box(3.4, 0.18, 2.2, lambert(0xb9b3a4));
638
+ porchFloor.position.set(2.0, 0.09, 3.9);
639
+ g.add(porchFloor);
640
+
641
+ const door = box(0.95, 1.9, 0.1, darkWood);
642
+ door.position.set(2.0, 0.95, 3.03);
643
+ g.add(door);
644
+
645
+ // Front windows with flower boxes
646
+ for (const x of [-2.4, -0.5]) {
647
+ const frame = box(1.15, 1.0, 0.1, darkWood);
648
+ frame.position.set(x, 1.8, 3.03);
649
+ g.add(frame);
650
+ const glassPane = new THREE.Mesh(
651
+ new THREE.PlaneGeometry(0.95, 0.8),
652
+ new THREE.MeshBasicMaterial({ color: 0xbfe0ea })
653
+ );
654
+ glassPane.position.set(x, 1.8, 3.1);
655
+ g.add(glassPane);
656
+ const flowerBox = box(1.2, 0.22, 0.24, lambert(0x8a6740));
657
+ flowerBox.position.set(x, 1.2, 3.12);
658
+ g.add(flowerBox);
659
+ for (let i = 0; i < 4; i++) {
660
+ const bloom = ico(0.08, 0, lambert(pick([0xf2d23c, 0xe8734a, 0xffffff])));
661
+ bloom.position.set(x - 0.45 + i * 0.3, 1.36, 3.16);
662
+ g.add(bloom);
663
+ }
664
+ }
665
+
666
+ return shadows(g);
667
+ }
668
+
669
+ function buildClothesline() {
670
+ const g = new THREE.Group();
671
+ const poleMaterial = lambert(0x6b6f73);
672
+ for (const x of [-1.8, 1.8]) {
673
+ const pole = cyl(0.05, 0.07, 2.6, 6, poleMaterial);
674
+ pole.position.set(x, 1.3, 0);
675
+ g.add(pole);
676
+ const bar = box(0.7, 0.06, 0.06, poleMaterial);
677
+ bar.position.set(x, 2.55, 0);
678
+ g.add(bar);
679
+ }
680
+ const line = box(3.6, 0.025, 0.025, lambert(0xdddddd));
681
+ line.position.y = 2.5;
682
+ g.add(line);
683
+ const clothMaterial = lambert(0xf5f5f0);
684
+ for (const x of [-1.1, 0, 1.1]) {
685
+ const cloth = box(0.75, rand(0.8, 1.0), 0.05, clothMaterial);
686
+ cloth.position.set(x, 2.05, 0);
687
+ cloth.rotation.y = rand(-0.08, 0.08);
688
+ g.add(cloth);
689
+ }
690
+ return shadows(g);
691
+ }
692
+
693
+ // --- distant scenery ---------------------------------------------------------
694
+
695
+ function buildMountains() {
696
+ const g = new THREE.Group();
697
+ const layers = [
698
+ { z: -130, color: 0x5d9457, count: 4, hMin: 26, hMax: 38 },
699
+ { z: -155, color: 0x6fa07c, count: 4, hMin: 32, hMax: 46 },
700
+ { z: -180, color: 0x7fae9a, count: 3, hMin: 40, hMax: 55 },
701
+ ];
702
+ for (const layer of layers) {
703
+ const material = lambert(layer.color, { flatShading: true });
704
+ for (let i = 0; i < layer.count; i++) {
705
+ const h = rand(layer.hMin, layer.hMax);
706
+ const mountain = new THREE.Mesh(new THREE.ConeGeometry(h * 1.5, h, 5), material);
707
+ mountain.position.set(
708
+ -160 + (i + rand(0.1, 0.6)) * (320 / layer.count),
709
+ h / 2 - 3,
710
+ layer.z + rand(-12, 12)
711
+ );
712
+ mountain.rotation.y = rand(0, Math.PI);
713
+ g.add(mountain);
714
+ }
715
+ }
716
+ return g;
717
+ }
718
+
719
+ function buildClouds() {
720
+ const cloudMaterial = new THREE.MeshLambertMaterial({
721
+ color: 0xffffff,
722
+ flatShading: true,
723
+ emissive: 0xdde9f4,
724
+ emissiveIntensity: 0.38,
725
+ });
726
+
727
+ function makeCloud(scale) {
728
+ const cloud = new THREE.Group();
729
+ const puffs = 5 + Math.floor(Math.random() * 4);
730
+ for (let i = 0; i < puffs; i++) {
731
+ const r = rand(3.5, 8);
732
+ const puff = new THREE.Mesh(new THREE.IcosahedronGeometry(r, 1), cloudMaterial);
733
+ puff.position.set(
734
+ (i - puffs / 2) * (r * 0.85) + rand(-1.5, 1.5),
735
+ rand(-1.5, 1.5),
736
+ rand(-2, 2)
737
+ );
738
+ puff.scale.y = 0.55;
739
+ cloud.add(puff);
740
+ }
741
+ cloud.scale.setScalar(scale);
742
+ return cloud;
743
+ }
744
+
745
+ const clouds = [];
746
+ // big hero cumulus, center-back like the reference
747
+ for (const [x, y, z, s] of [[15, 55, -150, 2.6], [-5, 48, -140, 1.8]]) {
748
+ const cloud = makeCloud(s);
749
+ cloud.position.set(x, y, z);
750
+ cloud.userData.speed = 0.25;
751
+ clouds.push(cloud);
752
+ }
753
+ for (let i = 0; i < 11; i++) {
754
+ const cloud = makeCloud(rand(0.7, 1.5));
755
+ cloud.position.set(rand(-150, 150), rand(34, 80), rand(-170, -50));
756
+ cloud.userData.speed = rand(0.4, 1.2);
757
+ clouds.push(cloud);
758
+ }
759
+ return clouds;
760
+ }
761
+
762
+ // --- assemble ---------------------------------------------------------------
763
+
764
+ export function buildWorld(scene) {
765
+ // ground
766
+ const ground = new THREE.Mesh(
767
+ new THREE.CircleGeometry(340, 48),
768
+ lambert(0x6fae46)
769
+ );
770
+ ground.rotation.x = -Math.PI / 2;
771
+ ground.receiveShadow = true;
772
+ scene.add(ground);
773
+
774
+ scene.add(buildSidewalk());
775
+ scene.add(buildPath());
776
+
777
+ const vending = buildVendingMachine();
778
+ vending.position.set(-4.3, 0.3, 4.2);
779
+ scene.add(vending);
780
+
781
+ const bench = buildBench();
782
+ bench.position.set(1.7, 0.3, 4.4);
783
+ scene.add(bench);
784
+
785
+ const sign = buildSign();
786
+ sign.position.set(-8.3, 0.3, 4.2);
787
+ sign.rotation.y = 0.12;
788
+ scene.add(sign);
789
+
790
+ const pot = buildFlowerPot();
791
+ pot.position.set(-6.8, 0.3, 5.4);
792
+ scene.add(pot);
793
+
794
+ const lamp = buildLamp();
795
+ lamp.position.set(9.6, 0.3, 3.6);
796
+ scene.add(lamp);
797
+
798
+ const wall = buildStoneWall();
799
+ wall.position.set(-13.5, 0, 3.4);
800
+ wall.rotation.y = 0.25;
801
+ scene.add(wall);
802
+
803
+ const bigTree = buildBigTree();
804
+ bigTree.position.set(-14.5, 0, -2.5);
805
+ scene.add(bigTree);
806
+
807
+ // fences along the path and the house garden
808
+ scene.add(buildFenceRun({ x: 4.6, z: -3 }, { x: 5.8, z: -12 }));
809
+ scene.add(buildFenceRun({ x: -1.4, z: -5 }, { x: -2.4, z: -14 }));
810
+ scene.add(buildFenceRun({ x: 6.5, z: -6.8 }, { x: 16.5, z: -6.2 }));
811
+
812
+ // sunflowers behind the garden fence
813
+ for (let i = 0; i < 8; i++) {
814
+ const sunflower = buildSunflower();
815
+ sunflower.position.set(7.2 + i * 1.2, 0, -7.4 + rand(-0.3, 0.3));
816
+ sunflower.rotation.y = rand(-0.4, 0.4);
817
+ scene.add(sunflower);
818
+ }
819
+ for (let i = 0; i < 4; i++) {
820
+ const bush = buildFlowerBush();
821
+ bush.position.set(6.8 + i * 2.6, 0, -6.0 + rand(-0.2, 0.2));
822
+ scene.add(bush);
823
+ }
824
+
825
+ // crop fields
826
+ const field1 = buildCropField(6, 12);
827
+ field1.position.set(-10, 0, -20);
828
+ field1.rotation.y = 0.08;
829
+ scene.add(field1);
830
+ const field2 = buildCropField(5, 9);
831
+ field2.position.set(6.5, 0, -20);
832
+ field2.rotation.y = -0.06;
833
+ scene.add(field2);
834
+ const field3 = buildCropField(8, 11);
835
+ field3.position.set(-4, 0, -34);
836
+ scene.add(field3);
837
+
838
+ const house = buildHouse();
839
+ house.position.set(12.5, 0, -13);
840
+ house.rotation.y = -0.22;
841
+ scene.add(house);
842
+
843
+ const clothesline = buildClothesline();
844
+ clothesline.position.set(7.6, 0, -9.5);
845
+ clothesline.rotation.y = 0.25;
846
+ scene.add(clothesline);
847
+
848
+ // pocket of trees behind/right of the house
849
+ for (let i = 0; i < 8; i++) {
850
+ const tree = Math.random() < 0.5 ? buildBlobTree(rand(1.2, 1.9)) : buildPine(rand(0.9, 1.4));
851
+ tree.position.set(rand(15, 32), 0, rand(-28, -15));
852
+ scene.add(tree);
853
+ }
854
+
855
+ // treeline at the horizon
856
+ for (let i = 0; i < 26; i++) {
857
+ const tree = Math.random() < 0.6 ? buildBlobTree(rand(1.6, 2.6)) : buildPine(rand(1.3, 2.0));
858
+ tree.position.set(rand(-90, 90), 0, rand(-75, -45));
859
+ scene.add(tree);
860
+ }
861
+
862
+ scene.add(buildMountains());
863
+
864
+ const clouds = buildClouds();
865
+ for (const cloud of clouds) scene.add(cloud);
866
+
867
+ return { vending, bench, sign, lamp, bigTree, house, clouds };
868
+ }