@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.
@@ -0,0 +1,108 @@
1
+ import * as THREE from 'three'
2
+
3
+ /**
4
+ * Ranch fence along a polyline — posts at every point (subdivided so gaps
5
+ * never exceed `maxPostGap`) plus two horizontal rails per segment.
6
+ */
7
+
8
+ export interface FenceOptions {
9
+ /** Corner points of the fence path, in world space (y is ignored). */
10
+ points: Array<[number, number, number]>
11
+ height?: number
12
+ postColor?: string
13
+ railColor?: string
14
+ /** Maximum spacing between posts before a span is subdivided. */
15
+ maxPostGap?: number
16
+ /** `picket` runs rails between every post pair; `ranch` spans full corners. */
17
+ style?: 'ranch' | 'picket'
18
+ }
19
+
20
+ interface FencePoint {
21
+ x: number
22
+ z: number
23
+ }
24
+
25
+ export function fencePostsFor(points: FencePoint[], maxPostGap: number): FencePoint[] {
26
+ const posts: FencePoint[] = []
27
+ for (let i = 0; i < points.length - 1; i++) {
28
+ const a = points[i]
29
+ const b = points[i + 1]
30
+ if (!a || !b) continue
31
+ const dx = b.x - a.x
32
+ const dz = b.z - a.z
33
+ const len = Math.hypot(dx, dz)
34
+ const segments = Math.max(1, Math.ceil(len / maxPostGap))
35
+ for (let s = 0; s < segments; s++) {
36
+ posts.push({ x: a.x + (dx * s) / segments, z: a.z + (dz * s) / segments })
37
+ }
38
+ }
39
+ const last = points[points.length - 1]
40
+ if (last) {
41
+ posts.push(last)
42
+ }
43
+ return posts
44
+ }
45
+
46
+ export function createFence(options: FenceOptions): THREE.Group {
47
+ const {
48
+ points,
49
+ height = 1.05,
50
+ postColor = '#8a6a45',
51
+ railColor = '#9c7b52',
52
+ maxPostGap = 2.4,
53
+ style = 'ranch',
54
+ } = options
55
+
56
+ const group = new THREE.Group()
57
+ group.name = 'fence'
58
+ if (points.length < 2) return group
59
+
60
+ const flat: FencePoint[] = points.map(([x, , z]) => ({ x, z }))
61
+ const posts = fencePostsFor(flat, maxPostGap)
62
+
63
+ const postMat = new THREE.MeshStandardMaterial({ color: postColor, roughness: 0.92 })
64
+ const railMat = new THREE.MeshStandardMaterial({ color: railColor, roughness: 0.92 })
65
+
66
+ const postGeo = new THREE.BoxGeometry(0.1, height, 0.1)
67
+ for (const p of posts) {
68
+ const post = new THREE.Mesh(postGeo, postMat)
69
+ post.position.set(p.x, height / 2, p.z)
70
+ post.castShadow = true
71
+ group.add(post)
72
+ }
73
+
74
+ // Picket fences follow every post pair (tight, garden feel); ranch fences
75
+ // stretch single rails across each full corner-to-corner span.
76
+ const spans: Array<{ from: FencePoint; to: FencePoint }> = []
77
+ if (style === 'picket') {
78
+ for (let i = 0; i < posts.length - 1; i++) {
79
+ const from = posts[i]
80
+ const to = posts[i + 1]
81
+ if (from && to) spans.push({ from, to })
82
+ }
83
+ } else {
84
+ for (let i = 0; i < flat.length - 1; i++) {
85
+ const from = flat[i]
86
+ const to = flat[i + 1]
87
+ if (from && to) spans.push({ from, to })
88
+ }
89
+ }
90
+
91
+ for (const { from, to } of spans) {
92
+ const dx = to.x - from.x
93
+ const dz = to.z - from.z
94
+ const len = Math.hypot(dx, dz)
95
+ if (len < 0.01) continue
96
+ const angle = Math.atan2(dx, dz)
97
+
98
+ for (const railY of [height * 0.42, height * 0.82] as const) {
99
+ const rail = new THREE.Mesh(new THREE.BoxGeometry(0.05, 0.09, len * 1.02), railMat)
100
+ rail.position.set((from.x + to.x) / 2, railY, (from.z + to.z) / 2)
101
+ rail.rotation.y = angle
102
+ rail.castShadow = true
103
+ group.add(rail)
104
+ }
105
+ }
106
+
107
+ return group
108
+ }
@@ -0,0 +1,87 @@
1
+ import * as THREE from 'three'
2
+ import type { WorldTickFn, UnregisterTick } from '../../core/types'
3
+
4
+ /**
5
+ * Procedural pond — a water disc sunk slightly below a muddy border, with
6
+ * two ripple rings expanding outward forever. When `registerTick` is given
7
+ * the ripples run on the engine loop; otherwise the tick is returned.
8
+ */
9
+
10
+ export interface PondOptions {
11
+ radius?: number
12
+ waterColor?: string
13
+ mudColor?: string
14
+ /** Deterministic ripple phase. */
15
+ seed?: number
16
+ position?: [number, number, number]
17
+ registerTick?: (fn: WorldTickFn) => UnregisterTick
18
+ }
19
+
20
+ export function createPond(options: PondOptions = {}): THREE.Group {
21
+ const {
22
+ radius = 3,
23
+ waterColor = '#1d6a8f',
24
+ mudColor = '#3f3a2c',
25
+ seed = 3,
26
+ position = [0, 0, 0],
27
+ registerTick,
28
+ } = options
29
+
30
+ const group = new THREE.Group()
31
+ group.name = 'pond'
32
+ group.position.set(position[0], position[1], position[2])
33
+
34
+ const mud = new THREE.Mesh(
35
+ new THREE.CircleGeometry(radius * 1.18, 24),
36
+ new THREE.MeshStandardMaterial({ color: mudColor, roughness: 1 }),
37
+ )
38
+ mud.rotation.x = -Math.PI / 2
39
+ mud.position.y = -0.02
40
+ mud.receiveShadow = true
41
+ group.add(mud)
42
+
43
+ const waterMat = new THREE.MeshStandardMaterial({
44
+ color: waterColor,
45
+ roughness: 0.15,
46
+ metalness: 0.35,
47
+ transparent: true,
48
+ opacity: 0.92,
49
+ })
50
+ const water = new THREE.Mesh(new THREE.CircleGeometry(radius, 28), waterMat)
51
+ water.rotation.x = -Math.PI / 2
52
+ water.position.y = 0.01
53
+ group.add(water)
54
+
55
+ const rippleMat = new THREE.MeshBasicMaterial({
56
+ color: '#cfe8f3',
57
+ transparent: true,
58
+ opacity: 0.35,
59
+ side: THREE.DoubleSide,
60
+ })
61
+ const ripples: Array<{ mesh: THREE.Mesh<THREE.RingGeometry, THREE.MeshBasicMaterial>; offset: number }> = []
62
+ for (let i = 0; i < 2; i++) {
63
+ const ring = new THREE.Mesh(new THREE.RingGeometry(radius * 0.55, radius * 0.6, 28), rippleMat.clone())
64
+ ring.rotation.x = -Math.PI / 2
65
+ ring.position.y = 0.02
66
+ ring.position.x = (seed % 3 - 1) * radius * 0.25
67
+ ring.position.z = ((seed + i) % 3 - 1) * radius * 0.2
68
+ group.add(ring)
69
+ ripples.push({ mesh: ring, offset: (i * 2.1) / ripples.length + seed * 0.37 })
70
+ }
71
+
72
+ const tick: WorldTickFn = (_delta, elapsed) => {
73
+ for (const { mesh, offset } of ripples) {
74
+ const cycle = (elapsed * 0.35 + offset) % 1
75
+ const s = 0.3 + cycle * 1.5
76
+ mesh.scale.setScalar(s)
77
+ mesh.material.opacity = 0.38 * (1 - cycle)
78
+ }
79
+ }
80
+
81
+ if (registerTick) {
82
+ registerTick(tick)
83
+ } else {
84
+ group.userData.tick = tick
85
+ }
86
+ return group
87
+ }
@@ -0,0 +1,176 @@
1
+ import * as THREE from 'three'
2
+ import { createRng, jitter } from '../../engine/random'
3
+ import type { WorldTickFn, UnregisterTick } from '../../core/types'
4
+
5
+ /**
6
+ * Procedural low-poly trees — a trunk plus layered foliage blobs (round /
7
+ * broad) or stacked cones (conifer). Winter trees drop their foliage. All
8
+ * trees pivot at their base so `applyWindSway` can rock them naturally.
9
+ */
10
+
11
+ export type TreeSeason = 'spring' | 'summer' | 'autumn' | 'winter'
12
+ export type TreeShape = 'round' | 'conifer' | 'broad'
13
+
14
+ export interface TreeOptions {
15
+ /** Overall height in meters (3–8 reads well at farm scale). */
16
+ height?: number
17
+ trunkColor?: string
18
+ foliageColor?: string
19
+ /** Season preset — autumn warms the foliage, winter strips it. */
20
+ season?: TreeSeason
21
+ shape?: TreeShape
22
+ /** Deterministic silhouette seed. */
23
+ seed?: number
24
+ }
25
+
26
+ const SEASON_FOLIAGE: Record<TreeSeason, string | null> = {
27
+ spring: '#7bc96f',
28
+ summer: '#3f7d3a',
29
+ autumn: '#d97706',
30
+ winter: null,
31
+ }
32
+
33
+ export function createTree(options: TreeOptions = {}): THREE.Group {
34
+ const {
35
+ height = 4.5,
36
+ trunkColor = '#6b4a2f',
37
+ foliageColor,
38
+ season = 'summer',
39
+ shape = 'round',
40
+ seed = 1,
41
+ } = options
42
+
43
+ const rng = createRng(seed)
44
+ const group = new THREE.Group()
45
+ group.name = 'tree'
46
+
47
+ const trunkMat = new THREE.MeshStandardMaterial({ color: trunkColor, roughness: 0.95 })
48
+ const trunkH = height * (shape === 'conifer' ? 0.28 : 0.42)
49
+ const trunk = new THREE.Mesh(
50
+ new THREE.CylinderGeometry(height * 0.025, height * 0.045, trunkH, 6),
51
+ trunkMat,
52
+ )
53
+ trunk.position.y = trunkH / 2
54
+ trunk.castShadow = true
55
+ group.add(trunk)
56
+
57
+ const leafColor = foliageColor ?? SEASON_FOLIAGE[season]
58
+ if (leafColor) {
59
+ const leafMat = new THREE.MeshStandardMaterial({ color: leafColor, roughness: 0.9 })
60
+ const foliage = new THREE.Group()
61
+ foliage.name = 'tree-foliage'
62
+
63
+ if (shape === 'conifer') {
64
+ const tiers = 3
65
+ for (let i = 0; i < tiers; i++) {
66
+ const t = i / tiers
67
+ const cone = new THREE.Mesh(
68
+ new THREE.ConeGeometry(
69
+ height * 0.26 * (1 - t * 0.55),
70
+ height * 0.34 * (1 - t * 0.25),
71
+ 7,
72
+ ),
73
+ leafMat,
74
+ )
75
+ cone.position.y = trunkH + height * 0.22 + t * height * 0.3
76
+ cone.castShadow = true
77
+ cone.userData.windLayer = i
78
+ foliage.add(cone)
79
+ }
80
+ } else {
81
+ const blobs = shape === 'broad' ? 4 : 3
82
+ for (let i = 0; i < blobs; i++) {
83
+ const blob = new THREE.Mesh(
84
+ new THREE.IcosahedronGeometry(jitter(rng, height * 0.19, height * 0.04), 0),
85
+ leafMat,
86
+ )
87
+ const angle = (i / blobs) * Math.PI * 2 + rng() * 0.8
88
+ blob.position.set(
89
+ Math.cos(angle) * height * (shape === 'broad' ? 0.13 : 0.07),
90
+ trunkH + height * 0.28 + (i % 2) * height * 0.1,
91
+ Math.sin(angle) * height * (shape === 'broad' ? 0.13 : 0.07),
92
+ )
93
+ blob.castShadow = true
94
+ blob.userData.windLayer = i % 2
95
+ foliage.add(blob)
96
+ }
97
+ }
98
+ group.add(foliage)
99
+ }
100
+
101
+ return group
102
+ }
103
+
104
+ export interface TreeRowOptions extends TreeOptions {
105
+ count?: number
106
+ /** Gap between trunks in meters. */
107
+ spacing?: number
108
+ /** Row axis — trees line up along it, centered on the group origin. */
109
+ axis?: 'x' | 'z'
110
+ }
111
+
112
+ /** A windbreak / avenue line of trees with per-tree silhouette variation. */
113
+ export function createTreeRow(options: TreeRowOptions = {}): THREE.Group {
114
+ const { count = 6, spacing = 3, axis = 'x', seed = 1, ...tree } = options
115
+ const group = new THREE.Group()
116
+ group.name = 'tree-row'
117
+
118
+ for (let i = 0; i < count; i++) {
119
+ const t = createTree({ ...tree, seed: seed + i * 31 })
120
+ const pos = (i - (count - 1) / 2) * spacing
121
+ if (axis === 'x') {
122
+ t.position.x = pos
123
+ } else {
124
+ t.position.z = pos
125
+ }
126
+ group.add(t)
127
+ }
128
+ return group
129
+ }
130
+
131
+ export interface WindSwayOptions {
132
+ /** Max lean angle in radians (0.01–0.05 is a light breeze). */
133
+ intensity?: number
134
+ /** Oscillations per second. */
135
+ speed?: number
136
+ /** Phase offset so neighbouring objects don't move in lockstep. */
137
+ phase?: number
138
+ }
139
+
140
+ /**
141
+ * Rock an object (or group of trees) with a sine sway around its base.
142
+ * Registers onto the engine tick loop when `registerTick` is provided,
143
+ * otherwise returns the tick function for manual stepping.
144
+ */
145
+ export function applyWindSway(
146
+ target: THREE.Object3D,
147
+ registerTick?: (fn: WorldTickFn) => UnregisterTick,
148
+ options: WindSwayOptions = {},
149
+ ): WorldTickFn {
150
+ const { intensity = 0.028, speed = 1.4, phase = 0 } = options
151
+
152
+ const swayable: Array<{ obj: THREE.Object3D; layer: number; phase: number }> = []
153
+ target.traverse((obj) => {
154
+ if (obj.userData && typeof obj.userData.windLayer === 'number') {
155
+ swayable.push({ obj, layer: obj.userData.windLayer as number, phase: (obj.id % 97) / 97 })
156
+ }
157
+ })
158
+ // Whole-object fallback when nothing tagged windLayer (e.g. crop groups)
159
+ if (swayable.length === 0) {
160
+ swayable.push({ obj: target, layer: 0, phase })
161
+ }
162
+
163
+ const tick: WorldTickFn = (_delta, elapsed) => {
164
+ const t = elapsed * speed + phase
165
+ for (const { obj, layer, phase: p } of swayable) {
166
+ const wobble = Math.sin(t + p + layer * 0.7)
167
+ obj.rotation.z = wobble * intensity
168
+ obj.rotation.x = Math.cos(t * 0.7 + p) * intensity * 0.5
169
+ }
170
+ }
171
+
172
+ if (registerTick) {
173
+ registerTick(tick)
174
+ }
175
+ return tick
176
+ }
@@ -0,0 +1,4 @@
1
+ export * from './CropFieldBuilder'
2
+ export * from './TreeBuilder'
3
+ export * from './FenceBuilder'
4
+ export * from './PondBuilder'
@@ -0,0 +1,132 @@
1
+ import * as THREE from 'three'
2
+ import type { WorldTickFn, UnregisterTick, EntityStatus } from '../../core/types'
3
+
4
+ /**
5
+ * Procedural low-poly tractor (or towed implement) — big rear wheels, slim
6
+ * fronts, cabin and exhaust. `status: 'running'` spins the wheels and adds
7
+ * a subtle engine shiver via the engine tick loop.
8
+ */
9
+
10
+ export interface TractorOptions {
11
+ bodyColor?: string
12
+ status?: EntityStatus
13
+ scale?: number
14
+ /** Front attachment — a mower/implement disc for working scenes. */
15
+ implement?: 'none' | 'mower' | 'trailer'
16
+ registerTick?: (fn: WorldTickFn) => UnregisterTick
17
+ entityId?: string
18
+ }
19
+
20
+ export function createTractor(options: TractorOptions = {}): THREE.Group {
21
+ const {
22
+ bodyColor = '#2e7d32',
23
+ status = 'idle',
24
+ scale = 1,
25
+ implement = 'none',
26
+ registerTick,
27
+ entityId,
28
+ } = options
29
+
30
+ const group = new THREE.Group()
31
+ group.name = 'tractor'
32
+ if (entityId) group.userData = { entityId }
33
+
34
+ const bodyMat = new THREE.MeshStandardMaterial({ color: bodyColor, roughness: 0.55, metalness: 0.15 })
35
+ const darkMat = new THREE.MeshStandardMaterial({ color: '#25211c', roughness: 0.85 })
36
+ const glassMat = new THREE.MeshStandardMaterial({
37
+ color: '#bfe3f2',
38
+ roughness: 0.1,
39
+ metalness: 0.2,
40
+ transparent: true,
41
+ opacity: 0.55,
42
+ })
43
+
44
+ // Chassis
45
+ const chassis = new THREE.Mesh(new THREE.BoxGeometry(0.78, 0.34, 1.5), bodyMat)
46
+ chassis.position.y = 0.52
47
+ chassis.castShadow = true
48
+ group.add(chassis)
49
+
50
+ // Hood sloping forward
51
+ const hood = new THREE.Mesh(new THREE.BoxGeometry(0.66, 0.34, 0.72), bodyMat)
52
+ hood.position.set(0, 0.84, 0.32)
53
+ hood.castShadow = true
54
+ group.add(hood)
55
+
56
+ // Cabin
57
+ const cabin = new THREE.Mesh(new THREE.BoxGeometry(0.7, 0.62, 0.56), bodyMat)
58
+ cabin.position.set(0, 1.14, -0.34)
59
+ cabin.castShadow = true
60
+ group.add(cabin)
61
+
62
+ const cabGlass = new THREE.Mesh(new THREE.BoxGeometry(0.62, 0.4, 0.5), glassMat)
63
+ cabGlass.position.set(0, 1.2, -0.33)
64
+ group.add(cabGlass)
65
+
66
+ // Exhaust
67
+ const exhaust = new THREE.Mesh(new THREE.CylinderGeometry(0.035, 0.045, 0.5, 6), darkMat)
68
+ exhaust.position.set(0.26, 1.2, 0.5)
69
+ group.add(exhaust)
70
+
71
+ // Wheels — chunky rear, slim front, spin when running
72
+ const wheels: THREE.Mesh[] = []
73
+ const wheelSpecs: Array<{ r: number; w: number; x: number; z: number }> = [
74
+ { r: 0.44, w: 0.3, x: 0.52, z: -0.42 },
75
+ { r: 0.44, w: 0.3, x: -0.52, z: -0.42 },
76
+ { r: 0.28, w: 0.2, x: 0.46, z: 0.62 },
77
+ { r: 0.28, w: 0.2, x: -0.46, z: 0.62 },
78
+ ]
79
+ const hubGeo = new THREE.CylinderGeometry(1, 1, 1, 10)
80
+ for (const spec of wheelSpecs) {
81
+ const wheel = new THREE.Mesh(hubGeo, darkMat)
82
+ wheel.scale.set(spec.r, spec.w, spec.r)
83
+ wheel.rotation.z = Math.PI / 2
84
+ wheel.position.set(spec.x, spec.r, spec.z)
85
+ wheel.castShadow = true
86
+ group.add(wheel)
87
+ wheels.push(wheel)
88
+ }
89
+
90
+ // Implements
91
+ if (implement === 'mower') {
92
+ const bar = new THREE.Mesh(new THREE.BoxGeometry(1.5, 0.08, 0.12), darkMat)
93
+ bar.position.set(0, 0.34, 1.0)
94
+ group.add(bar)
95
+ const disc = new THREE.Mesh(new THREE.CylinderGeometry(0.16, 0.16, 0.05, 8), darkMat)
96
+ disc.rotation.z = Math.PI / 2
97
+ disc.position.set(0.6, 0.3, 1.0)
98
+ group.add(disc)
99
+ } else if (implement === 'trailer') {
100
+ const bed = new THREE.Mesh(new THREE.BoxGeometry(1.0, 0.36, 1.3), bodyMat)
101
+ bed.position.set(0, 0.5, -1.45)
102
+ bed.castShadow = true
103
+ group.add(bed)
104
+ for (const [tx, tz] of [
105
+ [0.42, -1.1],
106
+ [-0.42, -1.1],
107
+ [0.42, -1.8],
108
+ [-0.42, -1.8],
109
+ ] as const) {
110
+ const tw = new THREE.Mesh(hubGeo, darkMat)
111
+ tw.scale.set(0.26, 0.18, 0.26)
112
+ tw.rotation.z = Math.PI / 2
113
+ tw.position.set(tx, 0.26, tz)
114
+ group.add(tw)
115
+ }
116
+ }
117
+
118
+ group.scale.setScalar(scale)
119
+
120
+ const running = status === 'running' || status === 'working'
121
+ if (registerTick && running) {
122
+ registerTick((delta, elapsed) => {
123
+ for (const wheel of wheels) {
124
+ wheel.rotation.x += delta * 5.2
125
+ }
126
+ // Engine idle shiver
127
+ group.position.y = Math.sin(elapsed * 34) * 0.006
128
+ })
129
+ }
130
+
131
+ return group
132
+ }
@@ -0,0 +1 @@
1
+ export * from './TractorBuilder'