@umituz/web-virtual 2.0.2 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,372 @@
1
+ import * as THREE from 'three'
2
+ import type { Vector3Tuple } from '../../core/types'
3
+
4
+ /**
5
+ * Procedural farm architecture — barn, silo, greenhouse, machine shed,
6
+ * chicken coop and farmhouse. Every piece is optional and parameterized:
7
+ * consumers compose exactly the yard their data calls for. All geometry is
8
+ * primitive-based (no asset loading) so worlds build instantly.
9
+ */
10
+
11
+ export interface BarnOptions {
12
+ width?: number
13
+ depth?: number
14
+ height?: number
15
+ wallColor?: string
16
+ roofColor?: string
17
+ frameColor?: string
18
+ position?: Vector3Tuple
19
+ rotationY?: number
20
+ entityId?: string
21
+ }
22
+
23
+ export function createBarn(options: BarnOptions = {}): THREE.Group {
24
+ const {
25
+ width = 10,
26
+ depth = 7,
27
+ height = 3.4,
28
+ wallColor = '#9d3b2c',
29
+ roofColor = '#3f2f26',
30
+ frameColor = '#f3e7d3',
31
+ position = [0, 0, 0],
32
+ rotationY = 0,
33
+ entityId,
34
+ } = options
35
+
36
+ const group = new THREE.Group()
37
+ group.name = 'barn'
38
+ group.position.set(position[0], position[1], position[2])
39
+ group.rotation.y = rotationY
40
+ if (entityId) group.userData = { entityId }
41
+
42
+ const wallMat = new THREE.MeshStandardMaterial({ color: wallColor, roughness: 0.9 })
43
+ const roofMat = new THREE.MeshStandardMaterial({ color: roofColor, roughness: 0.85 })
44
+ const frameMat = new THREE.MeshStandardMaterial({ color: frameColor, roughness: 0.9 })
45
+
46
+ // Walls
47
+ const walls = new THREE.Mesh(new THREE.BoxGeometry(width, height, depth), wallMat)
48
+ walls.position.y = height / 2
49
+ walls.castShadow = true
50
+ walls.receiveShadow = true
51
+ group.add(walls)
52
+
53
+ // Gable roof — two slabs meeting at a ridge along x
54
+ const roofSlope = Math.hypot(depth / 2, height * 0.45)
55
+ for (const side of [-1, 1]) {
56
+ const slab = new THREE.Mesh(new THREE.BoxGeometry(width * 1.04, 0.16, roofSlope), roofMat)
57
+ slab.position.set(0, height + height * 0.22, (side * depth) / 4)
58
+ slab.rotation.x = side * Math.atan2(height * 0.45, depth / 2)
59
+ slab.castShadow = true
60
+ group.add(slab)
61
+ }
62
+ // Gable triangles
63
+ const gableShape = new THREE.Shape()
64
+ gableShape.moveTo(-depth / 2, 0)
65
+ gableShape.lineTo(depth / 2, 0)
66
+ gableShape.lineTo(0, height * 0.45)
67
+ gableShape.closePath()
68
+ const gableGeo = new THREE.ExtrudeGeometry(gableShape, { depth: 0.12, bevelEnabled: false })
69
+ for (const side of [-1, 1]) {
70
+ const gable = new THREE.Mesh(gableGeo, wallMat)
71
+ gable.position.set((side * width) / 2, height, 0.06)
72
+ gable.rotation.y = (side * Math.PI) / 2
73
+ group.add(gable)
74
+ }
75
+
76
+ // Big center door with white trim
77
+ const doorFrame = new THREE.Mesh(new THREE.BoxGeometry(width * 0.4, height * 0.72, 0.06), frameMat)
78
+ doorFrame.position.set(0, (height * 0.72) / 2, depth / 2)
79
+ group.add(doorFrame)
80
+ const door = new THREE.Mesh(new THREE.BoxGeometry(width * 0.34, height * 0.66, 0.1), roofMat)
81
+ door.position.set(0, (height * 0.66) / 2, depth / 2 + 0.05)
82
+ group.add(door)
83
+
84
+ // Hayloft window
85
+ const loft = new THREE.Mesh(new THREE.BoxGeometry(width * 0.16, height * 0.18, 0.06), frameMat)
86
+ loft.position.set(0, height * 0.8, depth / 2 + 0.03)
87
+ group.add(loft)
88
+
89
+ return group
90
+ }
91
+
92
+ export interface SiloOptions {
93
+ radius?: number
94
+ height?: number
95
+ color?: string
96
+ capColor?: string
97
+ position?: Vector3Tuple
98
+ entityId?: string
99
+ }
100
+
101
+ export function createSilo(options: SiloOptions = {}): THREE.Group {
102
+ const {
103
+ radius = 1.5,
104
+ height = 6,
105
+ color = '#cfd8dc',
106
+ capColor = '#8d99a4',
107
+ position = [0, 0, 0],
108
+ entityId,
109
+ } = options
110
+
111
+ const group = new THREE.Group()
112
+ group.name = 'silo'
113
+ group.position.set(position[0], position[1], position[2])
114
+ if (entityId) group.userData = { entityId }
115
+
116
+ const bodyMat = new THREE.MeshStandardMaterial({ color, roughness: 0.55, metalness: 0.35 })
117
+ const capMat = new THREE.MeshStandardMaterial({ color: capColor, roughness: 0.6, metalness: 0.3 })
118
+
119
+ const body = new THREE.Mesh(new THREE.CylinderGeometry(radius, radius, height, 14), bodyMat)
120
+ body.position.y = height / 2
121
+ body.castShadow = true
122
+ group.add(body)
123
+
124
+ const cap = new THREE.Mesh(new THREE.SphereGeometry(radius, 14, 8, 0, Math.PI * 2, 0, Math.PI / 2), capMat)
125
+ cap.position.y = height
126
+ cap.castShadow = true
127
+ group.add(cap)
128
+
129
+ return group
130
+ }
131
+
132
+ export interface GreenhouseOptions {
133
+ width?: number
134
+ depth?: number
135
+ height?: number
136
+ frameColor?: string
137
+ glassColor?: string
138
+ glassOpacity?: number
139
+ position?: Vector3Tuple
140
+ rotationY?: number
141
+ /** Interior planting benches — plain crop-color strips. */
142
+ benches?: Array<{ color?: string; growth?: number }>
143
+ entityId?: string
144
+ }
145
+
146
+ export function createGreenhouse(options: GreenhouseOptions = {}): THREE.Group {
147
+ const {
148
+ width = 9,
149
+ depth = 6,
150
+ // `height` is accepted for API symmetry — the vault derives from depth.
151
+ height: _height = 2.8,
152
+ frameColor = '#e8eaed',
153
+ glassColor = '#cdebdd',
154
+ glassOpacity = 0.3,
155
+ position = [0, 0, 0],
156
+ rotationY = 0,
157
+ benches,
158
+ entityId,
159
+ } = options
160
+ void _height
161
+
162
+ const group = new THREE.Group()
163
+ group.name = 'greenhouse'
164
+ group.position.set(position[0], position[1], position[2])
165
+ group.rotation.y = rotationY
166
+ if (entityId) group.userData = { entityId }
167
+
168
+ const frameMat = new THREE.MeshStandardMaterial({ color: frameColor, roughness: 0.6, metalness: 0.3 })
169
+ const glassMat = new THREE.MeshPhysicalMaterial({
170
+ color: glassColor,
171
+ transparent: true,
172
+ opacity: glassOpacity,
173
+ roughness: 0.08,
174
+ metalness: 0.1,
175
+ })
176
+
177
+ // Glass shell — barrel vault made of a half-cylinder along x
178
+ const vault = new THREE.Mesh(
179
+ new THREE.CylinderGeometry(depth / 2, depth / 2, width, 18, 1, false, 0, Math.PI),
180
+ glassMat,
181
+ )
182
+ vault.rotation.z = Math.PI / 2
183
+ vault.position.y = 0.02
184
+ vault.castShadow = false
185
+ group.add(vault)
186
+
187
+ // End walls
188
+ for (const side of [-1, 1]) {
189
+ const cap = new THREE.Mesh(new THREE.CircleGeometry(depth / 2, 18, 0, Math.PI), glassMat)
190
+ cap.position.set((side * width) / 2, 0, 0)
191
+ cap.rotation.y = (side * Math.PI) / 2
192
+ group.add(cap)
193
+ }
194
+
195
+ // Structural ribs
196
+ const ribs = Math.max(3, Math.round(width / 2.2))
197
+ for (let i = 0; i <= ribs; i++) {
198
+ const rib = new THREE.Mesh(
199
+ new THREE.TorusGeometry(depth / 2 - 0.03, 0.035, 6, 18, Math.PI),
200
+ frameMat,
201
+ )
202
+ rib.position.set(-width / 2 + (i * width) / ribs, 0, 0)
203
+ rib.rotation.y = Math.PI / 2
204
+ group.add(rib)
205
+ }
206
+ const ridge = new THREE.Mesh(
207
+ new THREE.CylinderGeometry(0.045, 0.045, width, 6),
208
+ frameMat,
209
+ )
210
+ ridge.rotation.z = Math.PI / 2
211
+ ridge.position.y = depth / 2
212
+ group.add(ridge)
213
+
214
+ // Planting benches with crop strips
215
+ if (benches && benches.length > 0) {
216
+ const benchW = (depth * 0.72) / benches.length
217
+ benches.forEach((bench, i) => {
218
+ const z = -depth * 0.36 + benchW * (i + 0.5)
219
+ const table = new THREE.Mesh(
220
+ new THREE.BoxGeometry(width * 0.88, 0.1, benchW * 0.86),
221
+ frameMat,
222
+ )
223
+ table.position.set(0, 0.5, z)
224
+ group.add(table)
225
+
226
+ if ((bench.growth ?? 0.5) > 0.05) {
227
+ const g = THREE.MathUtils.clamp(bench.growth ?? 0.5, 0, 1)
228
+ const crop = new THREE.Mesh(
229
+ new THREE.BoxGeometry(width * 0.84, 0.08 + g * 0.4, benchW * 0.7),
230
+ new THREE.MeshStandardMaterial({ color: bench.color ?? '#4d7c0f', roughness: 0.9 }),
231
+ )
232
+ crop.position.set(0, 0.56 + (g * 0.4) / 2, z)
233
+ group.add(crop)
234
+ }
235
+ })
236
+ }
237
+
238
+ return group
239
+ }
240
+
241
+ export interface MachineShedOptions {
242
+ width?: number
243
+ depth?: number
244
+ height?: number
245
+ color?: string
246
+ roofColor?: string
247
+ position?: Vector3Tuple
248
+ rotationY?: number
249
+ entityId?: string
250
+ }
251
+
252
+ /** Open-front machinery shed — three walls, roof, no front wall. */
253
+ export function createMachineShed(options: MachineShedOptions = {}): THREE.Group {
254
+ const {
255
+ width = 9,
256
+ depth = 6,
257
+ height = 3,
258
+ color = '#5d6b74',
259
+ roofColor = '#39434a',
260
+ position = [0, 0, 0],
261
+ rotationY = 0,
262
+ entityId,
263
+ } = options
264
+
265
+ const group = new THREE.Group()
266
+ group.name = 'machine-shed'
267
+ group.position.set(position[0], position[1], position[2])
268
+ group.rotation.y = rotationY
269
+ if (entityId) group.userData = { entityId }
270
+
271
+ const wallMat = new THREE.MeshStandardMaterial({ color, roughness: 0.85 })
272
+ const roofMat = new THREE.MeshStandardMaterial({ color: roofColor, roughness: 0.8 })
273
+
274
+ const t = 0.14
275
+ // Back + side walls
276
+ const back = new THREE.Mesh(new THREE.BoxGeometry(width, height, t), wallMat)
277
+ back.position.set(0, height / 2, -depth / 2)
278
+ back.castShadow = true
279
+ group.add(back)
280
+ for (const side of [-1, 1]) {
281
+ const wall = new THREE.Mesh(new THREE.BoxGeometry(t, height, depth), wallMat)
282
+ wall.position.set((side * width) / 2, height / 2, 0)
283
+ wall.castShadow = true
284
+ group.add(wall)
285
+ }
286
+ // Flat shallow-pitch roof
287
+ const roof = new THREE.Mesh(new THREE.BoxGeometry(width * 1.08, 0.16, depth * 1.08), roofMat)
288
+ roof.position.y = height + 0.08
289
+ roof.rotation.x = 0.06
290
+ roof.castShadow = true
291
+ group.add(roof)
292
+
293
+ return group
294
+ }
295
+
296
+ export interface CoopOptions {
297
+ position?: Vector3Tuple
298
+ rotationY?: number
299
+ color?: string
300
+ roofColor?: string
301
+ scale?: number
302
+ entityId?: string
303
+ }
304
+
305
+ /** Small chicken coop — raised house with a ramp and a fenced run. */
306
+ export function createCoop(options: CoopOptions = {}): THREE.Group {
307
+ const {
308
+ position = [0, 0, 0],
309
+ rotationY = 0,
310
+ color = '#a97c50',
311
+ roofColor = '#6d5843',
312
+ scale = 1,
313
+ entityId,
314
+ } = options
315
+
316
+ const group = new THREE.Group()
317
+ group.name = 'coop'
318
+ group.position.set(position[0], position[1], position[2])
319
+ group.rotation.y = rotationY
320
+ if (entityId) group.userData = { entityId }
321
+
322
+ const woodMat = new THREE.MeshStandardMaterial({ color, roughness: 0.92 })
323
+ const roofMat = new THREE.MeshStandardMaterial({ color: roofColor, roughness: 0.85 })
324
+ const wireMat = new THREE.MeshStandardMaterial({
325
+ color: '#9aa38f',
326
+ roughness: 0.7,
327
+ wireframe: true,
328
+ transparent: true,
329
+ opacity: 0.5,
330
+ })
331
+
332
+ // Raised house
333
+ const house = new THREE.Mesh(new THREE.BoxGeometry(1.6, 1.0, 1.2), woodMat)
334
+ house.position.y = 0.75
335
+ house.castShadow = true
336
+ group.add(house)
337
+
338
+ const roof = new THREE.Mesh(new THREE.BoxGeometry(1.76, 0.12, 1.4), roofMat)
339
+ roof.position.y = 1.32
340
+ roof.rotation.z = 0.12
341
+ group.add(roof)
342
+
343
+ const door = new THREE.Mesh(new THREE.BoxGeometry(0.34, 0.4, 0.06), roofMat)
344
+ door.position.set(0, 0.72, 0.62)
345
+ group.add(door)
346
+
347
+ // Legs
348
+ for (const [lx, lz] of [
349
+ [-0.65, -0.45],
350
+ [0.65, -0.45],
351
+ [-0.65, 0.45],
352
+ [0.65, 0.45],
353
+ ] as const) {
354
+ const leg = new THREE.Mesh(new THREE.BoxGeometry(0.09, 0.5, 0.09), woodMat)
355
+ leg.position.set(lx, 0.25, lz)
356
+ group.add(leg)
357
+ }
358
+
359
+ // Ramp
360
+ const ramp = new THREE.Mesh(new THREE.BoxGeometry(0.34, 0.05, 1.0), woodMat)
361
+ ramp.position.set(0, 0.38, 1.05)
362
+ ramp.rotation.x = -0.5
363
+ group.add(ramp)
364
+
365
+ // Fenced run
366
+ const run = new THREE.Mesh(new THREE.BoxGeometry(2.6, 1.0, 2.4), wireMat)
367
+ run.position.set(0, 0.5, 2.1)
368
+ group.add(run)
369
+
370
+ group.scale.setScalar(scale)
371
+ return group
372
+ }
@@ -1,4 +1,8 @@
1
1
  export * from './people/AvatarBuilder'
2
2
  export * from './furniture/DeskBuilder'
3
3
  export * from './architecture/OfficeArchitectureBuilder'
4
+ export * from './architecture/FarmArchitectureBuilder'
4
5
  export * from './props/PlantBuilder'
6
+ export * from './nature/index'
7
+ export * from './animals/index'
8
+ export * from './vehicles/index'
@@ -0,0 +1,119 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import * as THREE from 'three'
3
+ import { createCropPlot } from './CropFieldBuilder'
4
+ import { createTree, createTreeRow } from './TreeBuilder'
5
+ import { fencePostsFor, createFence } from './FenceBuilder'
6
+
7
+ function findInstancedPlants(group: THREE.Group): THREE.InstancedMesh | null {
8
+ let found: THREE.InstancedMesh | null = null
9
+ group.traverse((obj) => {
10
+ if (!found && obj instanceof THREE.InstancedMesh) found = obj
11
+ })
12
+ return found
13
+ }
14
+
15
+ describe('createCropPlot', () => {
16
+ it('scales plant count and height with growth', () => {
17
+ const young = findInstancedPlants(createCropPlot({ growth: 0.1, rows: 6 }))!
18
+ const mature = findInstancedPlants(createCropPlot({ growth: 1, rows: 6 }))!
19
+
20
+ expect(young.count).toBeGreaterThan(0)
21
+ expect(mature.count).toBeGreaterThan(young.count)
22
+
23
+ const matrix = new THREE.Matrix4()
24
+ young.getMatrixAt(0, matrix)
25
+ const youngScale = new THREE.Vector3().setFromMatrixScale(matrix)
26
+ mature.getMatrixAt(0, matrix)
27
+ const matureScale = new THREE.Vector3().setFromMatrixScale(matrix)
28
+ expect(matureScale.y).toBeGreaterThan(youngScale.y)
29
+ })
30
+
31
+ it('is deterministic for the same seed', () => {
32
+ const a = findInstancedPlants(createCropPlot({ seed: 42 }))!
33
+ const b = findInstancedPlants(createCropPlot({ seed: 42 }))!
34
+ const c = findInstancedPlants(createCropPlot({ seed: 43 }))!
35
+
36
+ const ma = new THREE.Matrix4()
37
+ const mb = new THREE.Matrix4()
38
+ const mc = new THREE.Matrix4()
39
+ for (let i = 0; i < a.count; i++) {
40
+ a.getMatrixAt(i, ma)
41
+ b.getMatrixAt(i, mb)
42
+ c.getMatrixAt(i, mc)
43
+ expect(ma.equals(mb)).toBe(true)
44
+ }
45
+ expect(ma.equals(mc)).toBe(false)
46
+ })
47
+
48
+ it('places every plant on top of the soil bed', () => {
49
+ const plot = createCropPlot({ growth: 0.8 })
50
+ const plants = findInstancedPlants(plot)!
51
+ const matrix = new THREE.Matrix4()
52
+ const pos = new THREE.Vector3()
53
+ for (let i = 0; i < plants.count; i++) {
54
+ plants.getMatrixAt(i, matrix)
55
+ pos.setFromMatrixPosition(matrix)
56
+ expect(pos.y).toBeGreaterThanOrEqual(0.2) // soilHeight default
57
+ }
58
+ })
59
+ })
60
+
61
+ describe('createTree / createTreeRow', () => {
62
+ it('drops foliage in winter', () => {
63
+ const summer = createTree({ season: 'summer' })
64
+ const winter = createTree({ season: 'winter' })
65
+ const hasFoliage = (tree: THREE.Group) =>
66
+ tree.getObjectByName('tree-foliage') !== undefined
67
+ expect(hasFoliage(summer)).toBe(true)
68
+ expect(hasFoliage(winter)).toBe(false)
69
+ })
70
+
71
+ it('builds a row with the requested number of trees', () => {
72
+ const row = createTreeRow({ count: 7, spacing: 2, axis: 'x' })
73
+ const trees = row.children.filter((c) => c.name === 'tree')
74
+ expect(trees).toHaveLength(7)
75
+ const xs = trees.map((t) => t.position.x).sort((a, b) => a - b)
76
+ expect(xs[0]).toBeCloseTo(-6)
77
+ expect(xs[xs.length - 1]).toBeCloseTo(6)
78
+ })
79
+ })
80
+
81
+ describe('fencePostsFor / createFence', () => {
82
+ it('subdivides long spans so no gap exceeds maxPostGap', () => {
83
+ const posts = fencePostsFor(
84
+ [
85
+ { x: 0, z: 0 },
86
+ { x: 10, z: 0 },
87
+ ],
88
+ 2.4,
89
+ )
90
+ expect(posts.length).toBeGreaterThanOrEqual(5) // 10 / 2.4 → 5 segments
91
+ for (let i = 1; i < posts.length; i++) {
92
+ const prev = posts[i - 1]
93
+ const curr = posts[i]
94
+ if (!prev || !curr) throw new Error('missing post')
95
+ const gap = Math.hypot(curr.x - prev.x, curr.z - prev.z)
96
+ expect(gap).toBeLessThanOrEqual(2.4 + 1e-9)
97
+ }
98
+ })
99
+
100
+ it('subdivides with exact post at both endpoints', () => {
101
+ const posts = fencePostsFor(
102
+ [
103
+ { x: 0, z: 0 },
104
+ { x: 4.8, z: 0 },
105
+ ],
106
+ 2.4,
107
+ )
108
+ const first = posts[0]
109
+ const last = posts[posts.length - 1]
110
+ if (!first || !last) throw new Error('missing endpoints')
111
+ expect(first).toEqual({ x: 0, z: 0 })
112
+ expect(last).toEqual({ x: 4.8, z: 0 })
113
+ })
114
+
115
+ it('returns an empty group for degenerate paths', () => {
116
+ const fence = createFence({ points: [[0, 0, 0]] })
117
+ expect(fence.children).toHaveLength(0)
118
+ })
119
+ })
@@ -0,0 +1,122 @@
1
+ import * as THREE from 'three'
2
+ import { createRng } from '../../engine/random'
3
+
4
+ /**
5
+ * Procedural crop plot — a raised soil bed with instanced crop rows whose
6
+ * height, density and color respond to a `growth` value between 0 (bare
7
+ * soil) and 1 (harvest-ready). One InstancedMesh per plot keeps draw calls
8
+ * flat no matter how dense the planting is.
9
+ */
10
+
11
+ export interface CropPlotOptions {
12
+ /** Plot size in meters along x. */
13
+ width?: number
14
+ /** Plot size in meters along z. */
15
+ depth?: number
16
+ /** Number of crop rows (along the width axis). */
17
+ rows?: number
18
+ /** Row length direction the plants are spaced along. */
19
+ rowDirection?: 'x' | 'z'
20
+ soilColor?: string
21
+ /** Foliage color of the crop. */
22
+ cropColor?: string
23
+ /** 0..1 — scales plant height and thinning. */
24
+ growth?: number
25
+ /** Raised soil bed height in meters. */
26
+ soilHeight?: number
27
+ /** Deterministic scatter seed. */
28
+ seed?: number
29
+ /** Links the whole plot to an entity for raycast selection. */
30
+ entityId?: string
31
+ }
32
+
33
+ const MIN_PLANT_HEIGHT = 0.06
34
+ const MAX_PLANT_HEIGHT = 0.85
35
+
36
+ export function createCropPlot(options: CropPlotOptions = {}): THREE.Group {
37
+ const {
38
+ width = 8,
39
+ depth = 6,
40
+ rows = 5,
41
+ rowDirection = 'z',
42
+ soilColor = '#4a3c28',
43
+ cropColor = '#4d7c0f',
44
+ growth = 0.6,
45
+ soilHeight = 0.22,
46
+ seed = 7,
47
+ entityId,
48
+ } = options
49
+
50
+ const g = THREE.MathUtils.clamp(growth, 0, 1)
51
+ const group = new THREE.Group()
52
+ group.name = 'crop-plot'
53
+ if (entityId) group.userData = { entityId }
54
+
55
+ // 1. Raised soil bed
56
+ const soilMat = new THREE.MeshStandardMaterial({ color: soilColor, roughness: 0.96 })
57
+ const soil = new THREE.Mesh(new THREE.BoxGeometry(width, soilHeight, depth), soilMat)
58
+ soil.position.y = soilHeight / 2
59
+ soil.receiveShadow = true
60
+ soil.castShadow = false
61
+ if (entityId) soil.userData = { entityId }
62
+ group.add(soil)
63
+
64
+ // 2. Crop rows — plants per row scales with the plot's longer axis
65
+ const rng = createRng(seed)
66
+ const spacing = 0.55
67
+ const rowCount = Math.max(1, Math.round(rows * (0.35 + 0.65 * g)))
68
+
69
+ const rowAxis = rowDirection === 'x' ? 'x' : 'z'
70
+ const rowLength = rowAxis === 'x' ? width : depth
71
+ const crossLength = rowAxis === 'x' ? depth : width
72
+
73
+ const perRow = Math.max(2, Math.floor((rowLength - 1) / spacing))
74
+ const plantHeight = MIN_PLANT_HEIGHT + (MAX_PLANT_HEIGHT - MIN_PLANT_HEIGHT) * g
75
+
76
+ // Low-poly plant: a slim cone (stalk + foliage reads as one silhouette)
77
+ const plantGeo = new THREE.ConeGeometry(0.14, 1, 5)
78
+ plantGeo.translate(0, 0.5, 0) // pivot at the base so scale.y grows upward
79
+ const plantMat = new THREE.MeshStandardMaterial({ color: cropColor, roughness: 0.9 })
80
+
81
+ const instanced = new THREE.InstancedMesh(plantGeo, plantMat, rowCount * perRow)
82
+ instanced.name = 'crop-plants'
83
+ instanced.castShadow = g > 0.25
84
+ instanced.receiveShadow = false
85
+
86
+ const dummy = new THREE.Object3D()
87
+ const color = new THREE.Color()
88
+ const hsl = { h: 0, s: 0, l: 0 }
89
+ color.set(cropColor).getHSL(hsl)
90
+
91
+ let index = 0
92
+ for (let r = 0; r < rowCount; r++) {
93
+ const crossPos = (r + 0.5) * (crossLength / rowCount) - crossLength / 2
94
+ for (let p = 0; p < perRow; p++) {
95
+ const rowPos = -rowLength / 2 + 0.5 + p * spacing + (rng() - 0.5) * 0.18
96
+ const h = plantHeight * (0.75 + rng() * 0.5)
97
+ const w = 0.8 + rng() * 0.5
98
+
99
+ if (rowAxis === 'x') {
100
+ dummy.position.set(rowPos, soilHeight, crossPos)
101
+ } else {
102
+ dummy.position.set(crossPos, soilHeight, rowPos)
103
+ }
104
+ dummy.rotation.set(0, rng() * Math.PI * 2, 0)
105
+ dummy.scale.set(w, h, w)
106
+ dummy.updateMatrix()
107
+ instanced.setMatrixAt(index, dummy.matrix)
108
+
109
+ // Slight per-plant lightness jitter for an organic patchwork
110
+ const l = THREE.MathUtils.clamp(hsl.l + (rng() - 0.5) * 0.14, 0.05, 0.9)
111
+ color.setHSL(hsl.h, hsl.s, l)
112
+ instanced.setColorAt(index, color)
113
+ index++
114
+ }
115
+ }
116
+ instanced.count = index
117
+ instanced.instanceMatrix.needsUpdate = true
118
+ if (instanced.instanceColor) instanced.instanceColor.needsUpdate = true
119
+ group.add(instanced)
120
+
121
+ return group
122
+ }