JavaScript · 16713 bytes Raw Blame History
1 // Building mesh factories for dougk
2 // Creates 3D meshes for placeable buildings
3
4 import * as THREE from 'three'
5
6 // Create a wooden dock
7 // Dock protrudes into water - shore side at origin, water side extends in -Z direction
8 export function createDock(gradientMap) {
9 const group = new THREE.Group()
10
11 const woodMaterial = new THREE.MeshToonMaterial({
12 color: 0x8b6914,
13 gradientMap
14 })
15
16 const darkWoodMaterial = new THREE.MeshToonMaterial({
17 color: 0x5c4a1a,
18 gradientMap
19 })
20
21 // Dock dimensions - extends into water
22 const dockWidth = 0.8
23 const dockLength = 1.2 // How far it protrudes into water
24 const dockHeight = 0.08
25
26 // Main platform - offset so shore edge is at z=0, water edge at z=-dockLength
27 const platformGeom = new THREE.BoxGeometry(dockWidth, dockHeight, dockLength)
28 const platform = new THREE.Mesh(platformGeom, woodMaterial)
29 platform.position.set(0, 0.04, -dockLength / 2)
30 group.add(platform)
31
32 // Planks (crosswise detail lines)
33 for (let i = 0; i < 6; i++) {
34 const plankGeom = new THREE.BoxGeometry(dockWidth - 0.02, 0.09, 0.03)
35 const plank = new THREE.Mesh(plankGeom, darkWoodMaterial)
36 plank.position.set(0, 0.045, -0.1 - i * 0.2)
37 group.add(plank)
38 }
39
40 // Support posts - two at shore, two at water end
41 const postGeom = new THREE.CylinderGeometry(0.05, 0.06, 0.5, 6)
42 const postPositions = [
43 // Shore posts (shorter, above ground)
44 { x: -dockWidth / 2 + 0.08, z: -0.1, height: 0.3, yOffset: -0.1 },
45 { x: dockWidth / 2 - 0.08, z: -0.1, height: 0.3, yOffset: -0.1 },
46 // Water posts (longer, go into water)
47 { x: -dockWidth / 2 + 0.08, z: -dockLength + 0.15, height: 0.6, yOffset: -0.25 },
48 { x: dockWidth / 2 - 0.08, z: -dockLength + 0.15, height: 0.6, yOffset: -0.25 }
49 ]
50
51 for (const pos of postPositions) {
52 const pGeom = new THREE.CylinderGeometry(0.05, 0.06, pos.height, 6)
53 const post = new THREE.Mesh(pGeom, darkWoodMaterial)
54 post.position.set(pos.x, pos.yOffset, pos.z)
55 group.add(post)
56 }
57
58 // Rope detail on water-end posts
59 const ropeMaterial = new THREE.MeshToonMaterial({
60 color: 0x8b7355,
61 gradientMap
62 })
63 const ropeGeom = new THREE.TorusGeometry(0.06, 0.015, 6, 12)
64
65 // Rope on left water post
66 const ropeLeft = new THREE.Mesh(ropeGeom, ropeMaterial)
67 ropeLeft.position.set(-dockWidth / 2 + 0.08, 0.08, -dockLength + 0.15)
68 ropeLeft.rotation.x = Math.PI / 2
69 group.add(ropeLeft)
70
71 // Rope on right water post
72 const ropeRight = new THREE.Mesh(ropeGeom, ropeMaterial)
73 ropeRight.position.set(dockWidth / 2 - 0.08, 0.08, -dockLength + 0.15)
74 ropeRight.rotation.x = Math.PI / 2
75 group.add(ropeRight)
76
77 return group
78 }
79
80 // Create a fishing hut
81 // Hut sits on shore with front porch extending toward water (-Z direction)
82 export function createFishingHut(gradientMap) {
83 const group = new THREE.Group()
84
85 const woodMaterial = new THREE.MeshToonMaterial({
86 color: 0x9b7653,
87 gradientMap
88 })
89
90 const roofMaterial = new THREE.MeshToonMaterial({
91 color: 0x654321,
92 gradientMap
93 })
94
95 const windowMaterial = new THREE.MeshBasicMaterial({
96 color: 0x87ceeb,
97 transparent: true,
98 opacity: 0.6
99 })
100
101 const darkWoodMaterial = new THREE.MeshToonMaterial({
102 color: 0x5c4a1a,
103 gradientMap
104 })
105
106 // Front porch/deck extending into water
107 const porchGeom = new THREE.BoxGeometry(0.7, 0.06, 0.5)
108 const porch = new THREE.Mesh(porchGeom, darkWoodMaterial)
109 porch.position.set(0, 0.02, -0.25)
110 group.add(porch)
111
112 // Porch support posts (in water)
113 const porchPostGeom = new THREE.CylinderGeometry(0.03, 0.04, 0.4, 6)
114 const porchPosts = [
115 { x: -0.3, z: -0.45 },
116 { x: 0.3, z: -0.45 }
117 ]
118 for (const pos of porchPosts) {
119 const post = new THREE.Mesh(porchPostGeom, darkWoodMaterial)
120 post.position.set(pos.x, -0.15, pos.z)
121 group.add(post)
122 }
123
124 // Main building base/floor - offset onto shore
125 const baseGeom = new THREE.BoxGeometry(0.9, 0.08, 0.7)
126 const base = new THREE.Mesh(baseGeom, woodMaterial)
127 base.position.set(0, 0.04, 0.35)
128 group.add(base)
129
130 // Walls
131 const wallGeom = new THREE.BoxGeometry(0.85, 0.5, 0.65)
132 const walls = new THREE.Mesh(wallGeom, woodMaterial)
133 walls.position.set(0, 0.33, 0.35)
134 group.add(walls)
135
136 // Roof
137 const roofGeom = new THREE.ConeGeometry(0.55, 0.35, 4)
138 const roof = new THREE.Mesh(roofGeom, roofMaterial)
139 roof.position.set(0, 0.75, 0.35)
140 roof.rotation.y = Math.PI / 4
141 group.add(roof)
142
143 // Window (on side wall)
144 const windowGeom = new THREE.PlaneGeometry(0.15, 0.15)
145 const window1 = new THREE.Mesh(windowGeom, windowMaterial)
146 window1.position.set(0.43, 0.37, 0.35)
147 window1.rotation.y = Math.PI / 2
148 group.add(window1)
149
150 // Door (facing water)
151 const doorFrameGeom = new THREE.BoxGeometry(0.22, 0.35, 0.03)
152 const doorFrame = new THREE.Mesh(doorFrameGeom, roofMaterial)
153 doorFrame.position.set(0, 0.26, 0.03)
154 group.add(doorFrame)
155
156 const doorGeom = new THREE.BoxGeometry(0.18, 0.32, 0.02)
157 const door = new THREE.Mesh(doorGeom, darkWoodMaterial)
158 door.position.set(0, 0.24, 0.02)
159 group.add(door)
160
161 return group
162 }
163
164 // Create a mini lighthouse
165 export function createLighthouse(gradientMap) {
166 const group = new THREE.Group()
167
168 const whiteMaterial = new THREE.MeshToonMaterial({
169 color: 0xf5f5f5,
170 gradientMap
171 })
172
173 const redMaterial = new THREE.MeshToonMaterial({
174 color: 0xcc3333,
175 gradientMap
176 })
177
178 const glassMaterial = new THREE.MeshBasicMaterial({
179 color: 0xffffaa,
180 transparent: true,
181 opacity: 0.8
182 })
183
184 // Base
185 const baseGeom = new THREE.CylinderGeometry(0.25, 0.3, 0.15, 8)
186 const base = new THREE.Mesh(baseGeom, whiteMaterial)
187 base.position.y = 0.075
188 group.add(base)
189
190 // Tower - alternating stripes
191 const stripeHeight = 0.2
192 for (let i = 0; i < 4; i++) {
193 const stripeGeom = new THREE.CylinderGeometry(
194 0.18 - i * 0.02,
195 0.2 - i * 0.02,
196 stripeHeight,
197 8
198 )
199 const stripe = new THREE.Mesh(stripeGeom, i % 2 === 0 ? whiteMaterial : redMaterial)
200 stripe.position.y = 0.25 + i * stripeHeight
201 group.add(stripe)
202 }
203
204 // Lamp housing
205 const housingGeom = new THREE.CylinderGeometry(0.12, 0.1, 0.15, 8)
206 const housing = new THREE.Mesh(housingGeom, redMaterial)
207 housing.position.y = 1.02
208 group.add(housing)
209
210 // Glass/light
211 const glassGeom = new THREE.SphereGeometry(0.08, 8, 6)
212 const glass = new THREE.Mesh(glassGeom, glassMaterial)
213 glass.position.y = 1.0
214 group.add(glass)
215
216 // Light beam (animated)
217 const beamGroup = new THREE.Group()
218 beamGroup.position.y = 1.0
219
220 const beamMaterial = new THREE.MeshBasicMaterial({
221 color: 0xffffaa,
222 transparent: true,
223 opacity: 0.3,
224 side: THREE.DoubleSide
225 })
226
227 // Create a cone-shaped beam - tip at lamp, wide end extending outward
228 const beamGeom = new THREE.ConeGeometry(0.8, 2.5, 8, 1, true)
229 beamGeom.rotateX(-Math.PI / 2) // Tip toward -Z, base toward +Z
230 beamGeom.translate(0, 0, 1.25) // Move so tip is at origin (lamp), base extends outward
231 const beam = new THREE.Mesh(beamGeom, beamMaterial)
232 beamGroup.add(beam)
233
234 // Mark for animation
235 beamGroup.userData.isLightBeam = true
236 group.add(beamGroup)
237
238 // Roof cap
239 const capGeom = new THREE.ConeGeometry(0.14, 0.12, 8)
240 const cap = new THREE.Mesh(capGeom, redMaterial)
241 cap.position.y = 1.15
242 group.add(cap)
243
244 // Mark group as lighthouse for animation
245 group.userData.isLighthouse = true
246
247 return group
248 }
249
250 // Create reed cluster
251 export function createReeds(gradientMap) {
252 const group = new THREE.Group()
253
254 const reedMaterial = new THREE.MeshToonMaterial({
255 color: 0x4a7c3f,
256 gradientMap
257 })
258
259 const tipMaterial = new THREE.MeshToonMaterial({
260 color: 0x8b7355,
261 gradientMap
262 })
263
264 // Create 5-7 reeds
265 const reedCount = 5 + Math.floor(Math.random() * 3)
266 const reeds = []
267
268 for (let i = 0; i < reedCount; i++) {
269 const height = 0.4 + Math.random() * 0.3
270 const angle = (i / reedCount) * Math.PI * 2 + Math.random() * 0.5
271 const dist = Math.random() * 0.15
272
273 // Reed group (stalk + tip together for swaying)
274 const reedGroup = new THREE.Group()
275 reedGroup.position.set(
276 Math.cos(angle) * dist,
277 0,
278 Math.sin(angle) * dist
279 )
280
281 // Reed stalk
282 const stalkGeom = new THREE.CylinderGeometry(0.015, 0.02, height, 4)
283 const stalk = new THREE.Mesh(stalkGeom, reedMaterial)
284 stalk.position.y = height / 2 - 0.1
285 reedGroup.add(stalk)
286
287 // Cattail tip
288 const tipGeom = new THREE.CylinderGeometry(0.03, 0.025, 0.1, 6)
289 const tip = new THREE.Mesh(tipGeom, tipMaterial)
290 tip.position.y = height - 0.05
291 reedGroup.add(tip)
292
293 // Mark for animation with random phase
294 reedGroup.userData.isReed = true
295 reedGroup.userData.phase = Math.random() * Math.PI * 2
296 reedGroup.userData.baseRotX = (Math.random() - 0.5) * 0.2
297 reedGroup.userData.baseRotZ = (Math.random() - 0.5) * 0.2
298
299 reeds.push(reedGroup)
300 group.add(reedGroup)
301 }
302
303 // Mark group as reeds cluster for animation
304 group.userData.isReeds = true
305 group.userData.reedChildren = reeds
306
307 return group
308 }
309
310 // Create fence segment
311 export function createFence(gradientMap) {
312 const group = new THREE.Group()
313
314 const woodMaterial = new THREE.MeshToonMaterial({
315 color: 0xa0826d,
316 gradientMap
317 })
318
319 // Two posts
320 const postGeom = new THREE.BoxGeometry(0.06, 0.4, 0.06)
321
322 const post1 = new THREE.Mesh(postGeom, woodMaterial)
323 post1.position.set(-0.25, 0.15, 0)
324 group.add(post1)
325
326 const post2 = new THREE.Mesh(postGeom, woodMaterial)
327 post2.position.set(0.25, 0.15, 0)
328 group.add(post2)
329
330 // Pointed tops
331 const pointGeom = new THREE.ConeGeometry(0.04, 0.08, 4)
332
333 const point1 = new THREE.Mesh(pointGeom, woodMaterial)
334 point1.position.set(-0.25, 0.39, 0)
335 group.add(point1)
336
337 const point2 = new THREE.Mesh(pointGeom, woodMaterial)
338 point2.position.set(0.25, 0.39, 0)
339 group.add(point2)
340
341 // Cross beams
342 const beamGeom = new THREE.BoxGeometry(0.5, 0.04, 0.03)
343
344 const beam1 = new THREE.Mesh(beamGeom, woodMaterial)
345 beam1.position.set(0, 0.25, 0)
346 group.add(beam1)
347
348 const beam2 = new THREE.Mesh(beamGeom, woodMaterial)
349 beam2.position.set(0, 0.1, 0)
350 group.add(beam2)
351
352 return group
353 }
354
355 // Create a giant onion house
356 export function createOnionHouse(gradientMap) {
357 const group = new THREE.Group()
358
359 // Onion colors - layered purples and whites
360 const outerSkinMaterial = new THREE.MeshToonMaterial({
361 color: 0x8b668b, // Dusty purple outer skin
362 gradientMap
363 })
364
365 const innerSkinMaterial = new THREE.MeshToonMaterial({
366 color: 0xdda0dd, // Lighter purple inner layer peeking through
367 gradientMap
368 })
369
370 const rootMaterial = new THREE.MeshToonMaterial({
371 color: 0xd2b48c, // Tan roots
372 gradientMap
373 })
374
375 const doorMaterial = new THREE.MeshToonMaterial({
376 color: 0x4a3728, // Dark wood door
377 gradientMap
378 })
379
380 const chimneyMaterial = new THREE.MeshToonMaterial({
381 color: 0x8b7355, // Stone chimney
382 gradientMap
383 })
384
385 // Main onion body - bulbous bottom
386 const bulbGeom = new THREE.SphereGeometry(0.5, 10, 8)
387 bulbGeom.scale(1, 0.85, 1)
388 const bulb = new THREE.Mesh(bulbGeom, outerSkinMaterial)
389 bulb.position.y = 0.4
390 group.add(bulb)
391
392 // Onion top/neck tapering up
393 const neckGeom = new THREE.CylinderGeometry(0.15, 0.35, 0.4, 8)
394 const neck = new THREE.Mesh(neckGeom, outerSkinMaterial)
395 neck.position.y = 0.95
396 group.add(neck)
397
398 // Dried top sprout/tip
399 const tipGeom = new THREE.ConeGeometry(0.08, 0.25, 6)
400 const tip = new THREE.Mesh(tipGeom, rootMaterial)
401 tip.position.y = 1.27
402 tip.rotation.z = 0.15 // Slight lean for whimsy
403 group.add(tip)
404
405 // Peeling skin detail (decorative flaps)
406 const peelGeom = new THREE.PlaneGeometry(0.2, 0.35)
407 const peel1 = new THREE.Mesh(peelGeom, innerSkinMaterial)
408 peel1.position.set(0.45, 0.5, 0.15)
409 peel1.rotation.y = -0.4
410 peel1.rotation.z = 0.3
411 group.add(peel1)
412
413 const peel2 = new THREE.Mesh(peelGeom, innerSkinMaterial)
414 peel2.position.set(-0.35, 0.6, 0.3)
415 peel2.rotation.y = 0.6
416 peel2.rotation.z = -0.2
417 group.add(peel2)
418
419 // Root tendrils at the bottom
420 for (let i = 0; i < 5; i++) {
421 const angle = (i / 5) * Math.PI * 2 + Math.random() * 0.3
422 const rootGeom = new THREE.CylinderGeometry(0.02, 0.01, 0.15 + Math.random() * 0.1, 4)
423 const root = new THREE.Mesh(rootGeom, rootMaterial)
424 root.position.set(
425 Math.cos(angle) * 0.15,
426 -0.02,
427 Math.sin(angle) * 0.15
428 )
429 root.rotation.x = (Math.random() - 0.5) * 0.4
430 root.rotation.z = (Math.random() - 0.5) * 0.4
431 group.add(root)
432 }
433
434 // Door - cute rounded top
435 const doorGroup = new THREE.Group()
436
437 // Door frame (arch)
438 const doorFrameGeom = new THREE.BoxGeometry(0.22, 0.35, 0.05)
439 const doorFrame = new THREE.Mesh(doorFrameGeom, doorMaterial)
440 doorFrame.position.y = 0.175
441 doorGroup.add(doorFrame)
442
443 // Door arch top
444 const archGeom = new THREE.SphereGeometry(0.11, 8, 4, 0, Math.PI * 2, 0, Math.PI / 2)
445 const arch = new THREE.Mesh(archGeom, doorMaterial)
446 arch.position.y = 0.35
447 arch.rotation.x = Math.PI
448 doorGroup.add(arch)
449
450 // Door knob
451 const knobGeom = new THREE.SphereGeometry(0.02, 6, 4)
452 const knobMaterial = new THREE.MeshToonMaterial({ color: 0xffd700, gradientMap })
453 const knob = new THREE.Mesh(knobGeom, knobMaterial)
454 knob.position.set(0.07, 0.2, 0.03)
455 doorGroup.add(knob)
456
457 doorGroup.position.set(0.48, 0.02, 0)
458 doorGroup.rotation.y = Math.PI / 2
459 group.add(doorGroup)
460
461 // Chimney - whimsically placed on the side/top
462 const chimneyGroup = new THREE.Group()
463
464 const chimneyBaseGeom = new THREE.CylinderGeometry(0.06, 0.07, 0.3, 6)
465 const chimneyBase = new THREE.Mesh(chimneyBaseGeom, chimneyMaterial)
466 chimneyBase.position.y = 0.15
467 chimneyGroup.add(chimneyBase)
468
469 // Chimney cap
470 const capGeom = new THREE.CylinderGeometry(0.08, 0.06, 0.04, 6)
471 const cap = new THREE.Mesh(capGeom, chimneyMaterial)
472 cap.position.y = 0.32
473 chimneyGroup.add(cap)
474
475 // Position chimney at a jaunty angle on the onion
476 chimneyGroup.position.set(-0.25, 0.75, 0.2)
477 chimneyGroup.rotation.z = 0.3 // Tilted for whimsy
478 chimneyGroup.rotation.x = -0.15
479 group.add(chimneyGroup)
480
481 // Smoke puffs - create several that will be animated
482 const smokeGroup = new THREE.Group()
483 smokeGroup.userData.isSmoke = true
484
485 const smokeMaterial = new THREE.MeshBasicMaterial({
486 color: 0xcccccc,
487 transparent: true,
488 opacity: 0.6
489 })
490
491 // Create 4 smoke puffs at different phases
492 for (let i = 0; i < 4; i++) {
493 const puffGeom = new THREE.SphereGeometry(0.04, 6, 4)
494 const puff = new THREE.Mesh(puffGeom, smokeMaterial.clone())
495 puff.userData.isSmokePuff = true
496 puff.userData.phase = i * 0.25 // Stagger the animation phases
497 puff.userData.baseY = 0
498 puff.position.set(
499 (Math.random() - 0.5) * 0.03,
500 0,
501 (Math.random() - 0.5) * 0.03
502 )
503 smokeGroup.add(puff)
504 }
505
506 // Position smoke at chimney top (in world space, accounting for chimney tilt)
507 // Chimney cap is at local y=0.32, chimney group at (-0.25, 0.75, 0.2)
508 smokeGroup.position.set(-0.35, 1.05, 0.15)
509 group.add(smokeGroup)
510
511 // Little window
512 const windowGeom = new THREE.CircleGeometry(0.08, 8)
513 const windowMaterial = new THREE.MeshBasicMaterial({
514 color: 0xffffcc,
515 transparent: true,
516 opacity: 0.7
517 })
518 const windowMesh = new THREE.Mesh(windowGeom, windowMaterial)
519 windowMesh.position.set(0.1, 0.55, 0.49)
520 group.add(windowMesh)
521
522 // Window frame
523 const windowFrameGeom = new THREE.TorusGeometry(0.08, 0.012, 4, 12)
524 const windowFrame = new THREE.Mesh(windowFrameGeom, doorMaterial)
525 windowFrame.position.set(0.1, 0.55, 0.485)
526 group.add(windowFrame)
527
528 return group
529 }
530
531 // Factory function to create building by type
532 export function createBuilding(type, gradientMap) {
533 switch (type) {
534 case 'dock_wooden':
535 return createDock(gradientMap)
536 case 'fishing_hut':
537 return createFishingHut(gradientMap)
538 case 'lighthouse':
539 return createLighthouse(gradientMap)
540 case 'reeds':
541 return createReeds(gradientMap)
542 case 'fence':
543 return createFence(gradientMap)
544 case 'onion_house':
545 return createOnionHouse(gradientMap)
546 default:
547 console.warn('Unknown building type:', type)
548 return new THREE.Group()
549 }
550 }
551
552 // Create ghost (preview) version of a building
553 export function createGhostBuilding(type, gradientMap, isValid) {
554 const building = createBuilding(type, gradientMap)
555
556 // Make all materials transparent and tinted
557 const color = isValid ? 0x44ff44 : 0xff4444
558 const opacity = 0.5
559
560 building.traverse((child) => {
561 if (child.isMesh) {
562 child.material = new THREE.MeshBasicMaterial({
563 color,
564 transparent: true,
565 opacity
566 })
567 }
568 })
569
570 return building
571 }