@umituz/web-virtual 2.0.1 → 2.1.0
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.
- package/README.md +44 -1
- package/package.json +1 -1
- package/src/components/VirtualWorld.tsx +14 -0
- package/src/core/environment.test.ts +1 -1
- package/src/core/types.ts +24 -1
- package/src/engine/index.ts +1 -0
- package/src/engine/random.ts +25 -0
- package/src/engine/renderer.ts +30 -1
- package/src/entities/animals/AnimalBuilder.test.ts +82 -0
- package/src/entities/animals/AnimalBuilder.ts +219 -0
- package/src/entities/animals/index.ts +1 -0
- package/src/entities/architecture/FarmArchitectureBuilder.ts +370 -0
- package/src/entities/index.ts +4 -0
- package/src/entities/nature/CropFieldBuilder.test.ts +119 -0
- package/src/entities/nature/CropFieldBuilder.ts +123 -0
- package/src/entities/nature/FenceBuilder.ts +108 -0
- package/src/entities/nature/PondBuilder.ts +87 -0
- package/src/entities/nature/TreeBuilder.ts +176 -0
- package/src/entities/nature/index.ts +4 -0
- package/src/entities/vehicles/TractorBuilder.ts +132 -0
- package/src/entities/vehicles/index.ts +1 -0
- package/src/presets/farmPreset.ts +186 -66
- package/src/styles/virtual.css +4 -0
|
@@ -0,0 +1,370 @@
|
|
|
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 = 2.8,
|
|
151
|
+
frameColor = '#e8eaed',
|
|
152
|
+
glassColor = '#cdebdd',
|
|
153
|
+
glassOpacity = 0.3,
|
|
154
|
+
position = [0, 0, 0],
|
|
155
|
+
rotationY = 0,
|
|
156
|
+
benches,
|
|
157
|
+
entityId,
|
|
158
|
+
} = options
|
|
159
|
+
|
|
160
|
+
const group = new THREE.Group()
|
|
161
|
+
group.name = 'greenhouse'
|
|
162
|
+
group.position.set(position[0], position[1], position[2])
|
|
163
|
+
group.rotation.y = rotationY
|
|
164
|
+
if (entityId) group.userData = { entityId }
|
|
165
|
+
|
|
166
|
+
const frameMat = new THREE.MeshStandardMaterial({ color: frameColor, roughness: 0.6, metalness: 0.3 })
|
|
167
|
+
const glassMat = new THREE.MeshPhysicalMaterial({
|
|
168
|
+
color: glassColor,
|
|
169
|
+
transparent: true,
|
|
170
|
+
opacity: glassOpacity,
|
|
171
|
+
roughness: 0.08,
|
|
172
|
+
metalness: 0.1,
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
// Glass shell — barrel vault made of a half-cylinder along x
|
|
176
|
+
const vault = new THREE.Mesh(
|
|
177
|
+
new THREE.CylinderGeometry(depth / 2, depth / 2, width, 18, 1, false, 0, Math.PI),
|
|
178
|
+
glassMat,
|
|
179
|
+
)
|
|
180
|
+
vault.rotation.z = Math.PI / 2
|
|
181
|
+
vault.position.y = 0.02
|
|
182
|
+
vault.castShadow = false
|
|
183
|
+
group.add(vault)
|
|
184
|
+
|
|
185
|
+
// End walls
|
|
186
|
+
for (const side of [-1, 1]) {
|
|
187
|
+
const cap = new THREE.Mesh(new THREE.CircleGeometry(depth / 2, 18, 0, Math.PI), glassMat)
|
|
188
|
+
cap.position.set((side * width) / 2, 0, 0)
|
|
189
|
+
cap.rotation.y = (side * Math.PI) / 2
|
|
190
|
+
group.add(cap)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Structural ribs
|
|
194
|
+
const ribs = Math.max(3, Math.round(width / 2.2))
|
|
195
|
+
for (let i = 0; i <= ribs; i++) {
|
|
196
|
+
const rib = new THREE.Mesh(
|
|
197
|
+
new THREE.TorusGeometry(depth / 2 - 0.03, 0.035, 6, 18, Math.PI),
|
|
198
|
+
frameMat,
|
|
199
|
+
)
|
|
200
|
+
rib.position.set(-width / 2 + (i * width) / ribs, 0, 0)
|
|
201
|
+
rib.rotation.y = Math.PI / 2
|
|
202
|
+
group.add(rib)
|
|
203
|
+
}
|
|
204
|
+
const ridge = new THREE.Mesh(
|
|
205
|
+
new THREE.CylinderGeometry(0.045, 0.045, width, 6),
|
|
206
|
+
frameMat,
|
|
207
|
+
)
|
|
208
|
+
ridge.rotation.z = Math.PI / 2
|
|
209
|
+
ridge.position.y = depth / 2
|
|
210
|
+
group.add(ridge)
|
|
211
|
+
|
|
212
|
+
// Planting benches with crop strips
|
|
213
|
+
if (benches && benches.length > 0) {
|
|
214
|
+
const benchW = (depth * 0.72) / benches.length
|
|
215
|
+
benches.forEach((bench, i) => {
|
|
216
|
+
const z = -depth * 0.36 + benchW * (i + 0.5)
|
|
217
|
+
const table = new THREE.Mesh(
|
|
218
|
+
new THREE.BoxGeometry(width * 0.88, 0.1, benchW * 0.86),
|
|
219
|
+
frameMat,
|
|
220
|
+
)
|
|
221
|
+
table.position.set(0, 0.5, z)
|
|
222
|
+
group.add(table)
|
|
223
|
+
|
|
224
|
+
if ((bench.growth ?? 0.5) > 0.05) {
|
|
225
|
+
const g = THREE.MathUtils.clamp(bench.growth ?? 0.5, 0, 1)
|
|
226
|
+
const crop = new THREE.Mesh(
|
|
227
|
+
new THREE.BoxGeometry(width * 0.84, 0.08 + g * 0.4, benchW * 0.7),
|
|
228
|
+
new THREE.MeshStandardMaterial({ color: bench.color ?? '#4d7c0f', roughness: 0.9 }),
|
|
229
|
+
)
|
|
230
|
+
crop.position.set(0, 0.56 + (g * 0.4) / 2, z)
|
|
231
|
+
group.add(crop)
|
|
232
|
+
}
|
|
233
|
+
})
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return group
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export interface MachineShedOptions {
|
|
240
|
+
width?: number
|
|
241
|
+
depth?: number
|
|
242
|
+
height?: number
|
|
243
|
+
color?: string
|
|
244
|
+
roofColor?: string
|
|
245
|
+
position?: Vector3Tuple
|
|
246
|
+
rotationY?: number
|
|
247
|
+
entityId?: string
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Open-front machinery shed — three walls, roof, no front wall. */
|
|
251
|
+
export function createMachineShed(options: MachineShedOptions = {}): THREE.Group {
|
|
252
|
+
const {
|
|
253
|
+
width = 9,
|
|
254
|
+
depth = 6,
|
|
255
|
+
height = 3,
|
|
256
|
+
color = '#5d6b74',
|
|
257
|
+
roofColor = '#39434a',
|
|
258
|
+
position = [0, 0, 0],
|
|
259
|
+
rotationY = 0,
|
|
260
|
+
entityId,
|
|
261
|
+
} = options
|
|
262
|
+
|
|
263
|
+
const group = new THREE.Group()
|
|
264
|
+
group.name = 'machine-shed'
|
|
265
|
+
group.position.set(position[0], position[1], position[2])
|
|
266
|
+
group.rotation.y = rotationY
|
|
267
|
+
if (entityId) group.userData = { entityId }
|
|
268
|
+
|
|
269
|
+
const wallMat = new THREE.MeshStandardMaterial({ color, roughness: 0.85 })
|
|
270
|
+
const roofMat = new THREE.MeshStandardMaterial({ color: roofColor, roughness: 0.8 })
|
|
271
|
+
|
|
272
|
+
const t = 0.14
|
|
273
|
+
// Back + side walls
|
|
274
|
+
const back = new THREE.Mesh(new THREE.BoxGeometry(width, height, t), wallMat)
|
|
275
|
+
back.position.set(0, height / 2, -depth / 2)
|
|
276
|
+
back.castShadow = true
|
|
277
|
+
group.add(back)
|
|
278
|
+
for (const side of [-1, 1]) {
|
|
279
|
+
const wall = new THREE.Mesh(new THREE.BoxGeometry(t, height, depth), wallMat)
|
|
280
|
+
wall.position.set((side * width) / 2, height / 2, 0)
|
|
281
|
+
wall.castShadow = true
|
|
282
|
+
group.add(wall)
|
|
283
|
+
}
|
|
284
|
+
// Flat shallow-pitch roof
|
|
285
|
+
const roof = new THREE.Mesh(new THREE.BoxGeometry(width * 1.08, 0.16, depth * 1.08), roofMat)
|
|
286
|
+
roof.position.y = height + 0.08
|
|
287
|
+
roof.rotation.x = 0.06
|
|
288
|
+
roof.castShadow = true
|
|
289
|
+
group.add(roof)
|
|
290
|
+
|
|
291
|
+
return group
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export interface CoopOptions {
|
|
295
|
+
position?: Vector3Tuple
|
|
296
|
+
rotationY?: number
|
|
297
|
+
color?: string
|
|
298
|
+
roofColor?: string
|
|
299
|
+
scale?: number
|
|
300
|
+
entityId?: string
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** Small chicken coop — raised house with a ramp and a fenced run. */
|
|
304
|
+
export function createCoop(options: CoopOptions = {}): THREE.Group {
|
|
305
|
+
const {
|
|
306
|
+
position = [0, 0, 0],
|
|
307
|
+
rotationY = 0,
|
|
308
|
+
color = '#a97c50',
|
|
309
|
+
roofColor = '#6d5843',
|
|
310
|
+
scale = 1,
|
|
311
|
+
entityId,
|
|
312
|
+
} = options
|
|
313
|
+
|
|
314
|
+
const group = new THREE.Group()
|
|
315
|
+
group.name = 'coop'
|
|
316
|
+
group.position.set(position[0], position[1], position[2])
|
|
317
|
+
group.rotation.y = rotationY
|
|
318
|
+
if (entityId) group.userData = { entityId }
|
|
319
|
+
|
|
320
|
+
const woodMat = new THREE.MeshStandardMaterial({ color, roughness: 0.92 })
|
|
321
|
+
const roofMat = new THREE.MeshStandardMaterial({ color: roofColor, roughness: 0.85 })
|
|
322
|
+
const wireMat = new THREE.MeshStandardMaterial({
|
|
323
|
+
color: '#9aa38f',
|
|
324
|
+
roughness: 0.7,
|
|
325
|
+
wireframe: true,
|
|
326
|
+
transparent: true,
|
|
327
|
+
opacity: 0.5,
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
// Raised house
|
|
331
|
+
const house = new THREE.Mesh(new THREE.BoxGeometry(1.6, 1.0, 1.2), woodMat)
|
|
332
|
+
house.position.y = 0.75
|
|
333
|
+
house.castShadow = true
|
|
334
|
+
group.add(house)
|
|
335
|
+
|
|
336
|
+
const roof = new THREE.Mesh(new THREE.BoxGeometry(1.76, 0.12, 1.4), roofMat)
|
|
337
|
+
roof.position.y = 1.32
|
|
338
|
+
roof.rotation.z = 0.12
|
|
339
|
+
group.add(roof)
|
|
340
|
+
|
|
341
|
+
const door = new THREE.Mesh(new THREE.BoxGeometry(0.34, 0.4, 0.06), roofMat)
|
|
342
|
+
door.position.set(0, 0.72, 0.62)
|
|
343
|
+
group.add(door)
|
|
344
|
+
|
|
345
|
+
// Legs
|
|
346
|
+
for (const [lx, lz] of [
|
|
347
|
+
[-0.65, -0.45],
|
|
348
|
+
[0.65, -0.45],
|
|
349
|
+
[-0.65, 0.45],
|
|
350
|
+
[0.65, 0.45],
|
|
351
|
+
] as const) {
|
|
352
|
+
const leg = new THREE.Mesh(new THREE.BoxGeometry(0.09, 0.5, 0.09), woodMat)
|
|
353
|
+
leg.position.set(lx, 0.25, lz)
|
|
354
|
+
group.add(leg)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Ramp
|
|
358
|
+
const ramp = new THREE.Mesh(new THREE.BoxGeometry(0.34, 0.05, 1.0), woodMat)
|
|
359
|
+
ramp.position.set(0, 0.38, 1.05)
|
|
360
|
+
ramp.rotation.x = -0.5
|
|
361
|
+
group.add(ramp)
|
|
362
|
+
|
|
363
|
+
// Fenced run
|
|
364
|
+
const run = new THREE.Mesh(new THREE.BoxGeometry(2.6, 1.0, 2.4), wireMat)
|
|
365
|
+
run.position.set(0, 0.5, 2.1)
|
|
366
|
+
group.add(run)
|
|
367
|
+
|
|
368
|
+
group.scale.setScalar(scale)
|
|
369
|
+
return group
|
|
370
|
+
}
|
package/src/entities/index.ts
CHANGED
|
@@ -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,123 @@
|
|
|
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 crossAxis = rowAxis === 'x' ? 'z' : 'x'
|
|
71
|
+
const rowLength = rowAxis === 'x' ? width : depth
|
|
72
|
+
const crossLength = rowAxis === 'x' ? depth : width
|
|
73
|
+
|
|
74
|
+
const perRow = Math.max(2, Math.floor((rowLength - 1) / spacing))
|
|
75
|
+
const plantHeight = MIN_PLANT_HEIGHT + (MAX_PLANT_HEIGHT - MIN_PLANT_HEIGHT) * g
|
|
76
|
+
|
|
77
|
+
// Low-poly plant: a slim cone (stalk + foliage reads as one silhouette)
|
|
78
|
+
const plantGeo = new THREE.ConeGeometry(0.14, 1, 5)
|
|
79
|
+
plantGeo.translate(0, 0.5, 0) // pivot at the base so scale.y grows upward
|
|
80
|
+
const plantMat = new THREE.MeshStandardMaterial({ color: cropColor, roughness: 0.9 })
|
|
81
|
+
|
|
82
|
+
const instanced = new THREE.InstancedMesh(plantGeo, plantMat, rowCount * perRow)
|
|
83
|
+
instanced.name = 'crop-plants'
|
|
84
|
+
instanced.castShadow = g > 0.25
|
|
85
|
+
instanced.receiveShadow = false
|
|
86
|
+
|
|
87
|
+
const dummy = new THREE.Object3D()
|
|
88
|
+
const color = new THREE.Color()
|
|
89
|
+
const hsl = { h: 0, s: 0, l: 0 }
|
|
90
|
+
color.set(cropColor).getHSL(hsl)
|
|
91
|
+
|
|
92
|
+
let index = 0
|
|
93
|
+
for (let r = 0; r < rowCount; r++) {
|
|
94
|
+
const crossPos = (r + 0.5) * (crossLength / rowCount) - crossLength / 2
|
|
95
|
+
for (let p = 0; p < perRow; p++) {
|
|
96
|
+
const rowPos = -rowLength / 2 + 0.5 + p * spacing + (rng() - 0.5) * 0.18
|
|
97
|
+
const h = plantHeight * (0.75 + rng() * 0.5)
|
|
98
|
+
const w = 0.8 + rng() * 0.5
|
|
99
|
+
|
|
100
|
+
if (rowAxis === 'x') {
|
|
101
|
+
dummy.position.set(rowPos, soilHeight, crossPos)
|
|
102
|
+
} else {
|
|
103
|
+
dummy.position.set(crossPos, soilHeight, rowPos)
|
|
104
|
+
}
|
|
105
|
+
dummy.rotation.set(0, rng() * Math.PI * 2, 0)
|
|
106
|
+
dummy.scale.set(w, h, w)
|
|
107
|
+
dummy.updateMatrix()
|
|
108
|
+
instanced.setMatrixAt(index, dummy.matrix)
|
|
109
|
+
|
|
110
|
+
// Slight per-plant lightness jitter for an organic patchwork
|
|
111
|
+
const l = THREE.MathUtils.clamp(hsl.l + (rng() - 0.5) * 0.14, 0.05, 0.9)
|
|
112
|
+
color.setHSL(hsl.h, hsl.s, l)
|
|
113
|
+
instanced.setColorAt(index, color)
|
|
114
|
+
index++
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
instanced.count = index
|
|
118
|
+
instanced.instanceMatrix.needsUpdate = true
|
|
119
|
+
if (instanced.instanceColor) instanced.instanceColor.needsUpdate = true
|
|
120
|
+
group.add(instanced)
|
|
121
|
+
|
|
122
|
+
return group
|
|
123
|
+
}
|