@wardic/three 0.0.0-stage → 1.3.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/LICENSE +21 -0
- package/README.md +84 -2
- package/THIRD_PARTY_NOTICES.md +145 -0
- package/dist/TestStage.d.ts +45 -0
- package/dist/TestStage.d.ts.map +1 -0
- package/dist/TestStage.js +140 -0
- package/dist/TestStage.js.map +1 -0
- package/dist/ThreeColours.d.ts +52 -0
- package/dist/ThreeColours.d.ts.map +1 -0
- package/dist/ThreeColours.js +237 -0
- package/dist/ThreeColours.js.map +1 -0
- package/dist/ThreeDrag.d.ts +82 -0
- package/dist/ThreeDrag.d.ts.map +1 -0
- package/dist/ThreeDrag.js +243 -0
- package/dist/ThreeDrag.js.map +1 -0
- package/dist/ThreeInput.d.ts +69 -0
- package/dist/ThreeInput.d.ts.map +1 -0
- package/dist/ThreeInput.js +592 -0
- package/dist/ThreeInput.js.map +1 -0
- package/dist/ThreeInstances.d.ts +47 -0
- package/dist/ThreeInstances.d.ts.map +1 -0
- package/dist/ThreeInstances.js +392 -0
- package/dist/ThreeInstances.js.map +1 -0
- package/dist/ThreeRenderer.d.ts +45 -0
- package/dist/ThreeRenderer.d.ts.map +1 -0
- package/dist/ThreeRenderer.js +121 -0
- package/dist/ThreeRenderer.js.map +1 -0
- package/dist/ThreeValues.d.ts +15 -0
- package/dist/ThreeValues.d.ts.map +1 -0
- package/dist/ThreeValues.js +61 -0
- package/dist/ThreeValues.js.map +1 -0
- package/dist/WardicThree.d.ts +194 -0
- package/dist/WardicThree.d.ts.map +1 -0
- package/dist/WardicThree.js +162 -0
- package/dist/WardicThree.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/dev.d.ts +10 -0
- package/dist/internal/dev.d.ts.map +1 -0
- package/dist/internal/dev.js +11 -0
- package/dist/internal/dev.js.map +1 -0
- package/dist/internal/dev.production.d.ts +8 -0
- package/dist/internal/dev.production.d.ts.map +1 -0
- package/dist/internal/dev.production.js +8 -0
- package/dist/internal/dev.production.js.map +1 -0
- package/dist/internal/lookup.d.ts +29 -0
- package/dist/internal/lookup.d.ts.map +1 -0
- package/dist/internal/lookup.js +245 -0
- package/dist/internal/lookup.js.map +1 -0
- package/dist/internal/shared-material.d.ts +8 -0
- package/dist/internal/shared-material.d.ts.map +1 -0
- package/dist/internal/shared-material.js +95 -0
- package/dist/internal/shared-material.js.map +1 -0
- package/dist/solid/feature.d.ts +42 -0
- package/dist/solid/feature.d.ts.map +1 -0
- package/dist/solid/feature.js +2 -0
- package/dist/solid/feature.js.map +1 -0
- package/dist/solid/scene-batch.d.ts +10 -0
- package/dist/solid/scene-batch.d.ts.map +1 -0
- package/dist/solid/scene-batch.js +436 -0
- package/dist/solid/scene-batch.js.map +1 -0
- package/dist/solid/scene-object.d.ts +27 -0
- package/dist/solid/scene-object.d.ts.map +1 -0
- package/dist/solid/scene-object.js +111 -0
- package/dist/solid/scene-object.js.map +1 -0
- package/dist/solid/stage-batches.d.ts +10 -0
- package/dist/solid/stage-batches.d.ts.map +1 -0
- package/dist/solid/stage-batches.js +6 -0
- package/dist/solid/stage-batches.js.map +1 -0
- package/dist/solid/stage-drag.d.ts +4 -0
- package/dist/solid/stage-drag.d.ts.map +1 -0
- package/dist/solid/stage-drag.js +19 -0
- package/dist/solid/stage-drag.js.map +1 -0
- package/dist/solid/stage.d.ts +107 -0
- package/dist/solid/stage.d.ts.map +1 -0
- package/dist/solid/stage.js +320 -0
- package/dist/solid/stage.js.map +1 -0
- package/dist/solid/test-stage.d.ts +29 -0
- package/dist/solid/test-stage.d.ts.map +1 -0
- package/dist/solid/test-stage.js +16 -0
- package/dist/solid/test-stage.js.map +1 -0
- package/dist/solid.d.ts +55 -0
- package/dist/solid.d.ts.map +1 -0
- package/dist/solid.js +9 -0
- package/dist/solid.js.map +1 -0
- package/package.json +99 -4
- package/src/TestStage.ts +177 -0
- package/src/ThreeColours.ts +247 -0
- package/src/ThreeDrag.ts +341 -0
- package/src/ThreeInput.ts +744 -0
- package/src/ThreeInstances.ts +481 -0
- package/src/ThreeRenderer.ts +159 -0
- package/src/ThreeValues.ts +69 -0
- package/src/WardicThree.ts +347 -0
- package/src/index.ts +26 -0
- package/src/internal/dev.production.ts +7 -0
- package/src/internal/dev.ts +10 -0
- package/src/internal/lookup.ts +247 -0
- package/src/internal/shared-material.ts +101 -0
- package/src/solid/feature.ts +38 -0
- package/src/solid/scene-batch.ts +490 -0
- package/src/solid/scene-object.ts +145 -0
- package/src/solid/stage-batches.ts +13 -0
- package/src/solid/stage-drag.ts +26 -0
- package/src/solid/stage.ts +409 -0
- package/src/solid/test-stage.ts +40 -0
- package/src/solid.ts +111 -0
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a key a three.js Subject animates lives, read and written alike: a scene object's transform
|
|
3
|
+
* keys on its `position`, `rotation` (in degrees) and `scale`; any other key the Subject's own number,
|
|
4
|
+
* else found on its material, its uniforms or a vector's axis, as Motion's `motion/three` finds it.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
// `@wardic/three`'s lookup (#883; ADR-0069 decisions 4–6, ADR-0073 decision 23 and *Amendment (#883)*, Q3).
|
|
9
|
+
// A key's place is found once per Subject and kept, so a frame's write allocates nothing; it's found
|
|
10
|
+
// again once the Subject's material changes. Plain code: the writer calls it per frame.
|
|
11
|
+
import { DEV } from "@wardic/three/internal/dev"
|
|
12
|
+
import * as Predicate from "effect/Predicate"
|
|
13
|
+
import * as SharedMaterial from "./shared-material.ts"
|
|
14
|
+
|
|
15
|
+
/** Any object Wardic reads a key of. */
|
|
16
|
+
type Holder = Record<string, unknown>
|
|
17
|
+
|
|
18
|
+
/** Degrees to radians: a rotation's keys are in degrees, three's `rotation` in radians. */
|
|
19
|
+
export const RADIANS = Math.PI / 180
|
|
20
|
+
|
|
21
|
+
/** The transform keys a scene object takes (ADR-0069 decision 4): written as its position, rotation and scale. */
|
|
22
|
+
const TRANSFORMS: ReadonlySet<string> = new Set(["x", "y", "z", "rotateX", "rotateY", "rotateZ", "scale", "scaleX", "scaleY", "scaleZ"])
|
|
23
|
+
|
|
24
|
+
/** Whether `key` is a scene object's transform key. */
|
|
25
|
+
export const isTransformKey = (key: string): boolean => TRANSFORMS.has(key)
|
|
26
|
+
|
|
27
|
+
/** Whether `subject` is a scene object: three's `Object3D`, by its flag (ADR-0069 decision 3). */
|
|
28
|
+
export const isObject3D = (subject: unknown): boolean => Predicate.isObject(subject) && (subject as Holder).isObject3D === true
|
|
29
|
+
|
|
30
|
+
/** Whether `holder` is a material, by its flag. */
|
|
31
|
+
export const isMaterial = (holder: unknown): boolean => Predicate.isObject(holder) && (holder as Holder).isMaterial === true
|
|
32
|
+
|
|
33
|
+
/** Whether `value` is a uniform: an object holding a `value`, a `ShaderMaterial`'s or a bare uniforms object's. */
|
|
34
|
+
const isUniform = (value: unknown): value is { value: unknown } => Predicate.isObject(value) && "value" in value
|
|
35
|
+
|
|
36
|
+
/** Whether `value` is a key's value Wardic animates: a number, or a string. */
|
|
37
|
+
const isAnimatable = (value: unknown): value is number | string => Predicate.isNumber(value) || Predicate.isString(value)
|
|
38
|
+
|
|
39
|
+
/** The materials `subject` draws with: its `material`, or each of an array of them. None for a Subject without one. */
|
|
40
|
+
export const materialsOf = (subject: Holder): ReadonlyArray<Holder> => {
|
|
41
|
+
const material = subject.material
|
|
42
|
+
if (Array.isArray(material)) return material.filter(isMaterial) as ReadonlyArray<Holder>
|
|
43
|
+
if (isMaterial(material)) return [material as Holder]
|
|
44
|
+
return []
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Where a key is written: each holder's own property, by its name, in parallel lists. */
|
|
48
|
+
export interface Place {
|
|
49
|
+
readonly holders: ReadonlyArray<Holder>
|
|
50
|
+
readonly props: ReadonlyArray<string>
|
|
51
|
+
/** The Subject's `material` the place was found under: found again once it changes. */
|
|
52
|
+
readonly material: unknown
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A key at `holder` itself: a TSL uniform node set for it (three draws the node over the property), its number, or a uniform it names. */
|
|
56
|
+
const atHolder = (holder: Holder, key: string): readonly [Holder, string] | undefined => {
|
|
57
|
+
const node = holder[`${key}Node`]
|
|
58
|
+
if (Predicate.isObject(node) && (node as Holder).isUniformNode === true) return [node as Holder, "value"]
|
|
59
|
+
const own = holder[key]
|
|
60
|
+
if (isAnimatable(own)) return [holder, key]
|
|
61
|
+
if (isUniform(own) && isAnimatable(own.value)) return [own as Holder, "value"]
|
|
62
|
+
return undefined
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A key among `uniforms`: the uniform of its name. */
|
|
66
|
+
const inUniforms = (uniforms: unknown, key: string): readonly [Holder, string] | undefined => {
|
|
67
|
+
if (!Predicate.isObject(uniforms)) return undefined
|
|
68
|
+
const uniform = (uniforms as Holder)[key]
|
|
69
|
+
if (isUniform(uniform) && isAnimatable(uniform.value)) return [uniform as Holder, "value"]
|
|
70
|
+
return undefined
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The axes a vector key may end in, `positionX` is `position.x`, and a colour's channels, `colorR` is `color.r`. */
|
|
74
|
+
const AXES: Readonly<Record<string, string>> = { X: "x", Y: "y", Z: "z", W: "w", R: "r", G: "g", B: "b" }
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* A key as a vector's axis or a colour's channel at `holder`, `normalScaleX` → `holder.normalScale.x`,
|
|
78
|
+
* `colorR` → `holder.color.r`, or a uniform's, `uOffsetX` → `uniforms.uOffset.value.x`.
|
|
79
|
+
*/
|
|
80
|
+
const asAxis = (holder: Holder, key: string): readonly [Holder, string] | undefined => {
|
|
81
|
+
const axis = AXES[key.slice(-1)]
|
|
82
|
+
if (Predicate.isUndefined(axis)) return undefined
|
|
83
|
+
const base = key.slice(0, -1)
|
|
84
|
+
const vector = holder[base]
|
|
85
|
+
if (Predicate.isObject(vector) && Predicate.isNumber((vector as Holder)[axis])) return [vector as Holder, axis]
|
|
86
|
+
// A bare uniforms object's: `uSkyR` → `uniforms.uSky.value.r`.
|
|
87
|
+
if (isUniform(vector) && Predicate.isObject(vector.value) && Predicate.isNumber((vector.value as Holder)[axis])) return [vector.value as Holder, axis]
|
|
88
|
+
const uniforms = holder.uniforms
|
|
89
|
+
if (!Predicate.isObject(uniforms)) return undefined
|
|
90
|
+
const uniform = (uniforms as Holder)[base]
|
|
91
|
+
if (!isUniform(uniform)) return undefined
|
|
92
|
+
const value = uniform.value
|
|
93
|
+
if (Predicate.isObject(value) && Predicate.isNumber((value as Holder)[axis])) return [value as Holder, axis]
|
|
94
|
+
return undefined
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** A place on each holder that has the key one way: the materials of an array, each that has it. */
|
|
98
|
+
const placeIn = (holders: ReadonlyArray<Holder>, find: (holder: Holder, key: string) => readonly [Holder, string] | undefined, key: string, material: unknown): Place | undefined => {
|
|
99
|
+
const found: Array<Holder> = []
|
|
100
|
+
const props: Array<string> = []
|
|
101
|
+
for (const holder of holders) {
|
|
102
|
+
const at = find(holder, key)
|
|
103
|
+
if (Predicate.isUndefined(at)) continue
|
|
104
|
+
found.push(at[0])
|
|
105
|
+
props.push(at[1])
|
|
106
|
+
}
|
|
107
|
+
if (found.length === 0) return undefined
|
|
108
|
+
return { holders: found, props, material }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Where `key` lives on `subject`, in Motion's order: the Subject itself (a TSL uniform node, its number,
|
|
113
|
+
* a uniform it names), then its material's (each of an array of them), then its uniforms', then a vector's
|
|
114
|
+
* axis. `undefined` where it's nowhere. Not the transform keys of a scene object: they're its own.
|
|
115
|
+
*/
|
|
116
|
+
const find = (subject: Holder, key: string): Place | undefined => {
|
|
117
|
+
const material = subject.material
|
|
118
|
+
const own = placeIn([subject], atHolder, key, material)
|
|
119
|
+
if (Predicate.isNotUndefined(own)) return own
|
|
120
|
+
const materials = materialsOf(subject)
|
|
121
|
+
const onMaterial = placeIn(materials, atHolder, key, material)
|
|
122
|
+
if (Predicate.isNotUndefined(onMaterial)) {
|
|
123
|
+
if (DEV) SharedMaterial.check(subject, key, materials)
|
|
124
|
+
return onMaterial
|
|
125
|
+
}
|
|
126
|
+
const uniforms = placeIn([subject, ...materials], (holder, name) => inUniforms(holder.uniforms, name), key, material)
|
|
127
|
+
if (Predicate.isNotUndefined(uniforms)) return uniforms
|
|
128
|
+
const axis = placeIn([subject], asAxis, key, material)
|
|
129
|
+
if (Predicate.isNotUndefined(axis)) return axis
|
|
130
|
+
return placeIn(materials, asAxis, key, material)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Whether `value` is a three `Color`, by its flag. */
|
|
134
|
+
const isColor = (value: unknown): boolean => Predicate.isObject(value) && (value as Holder).isColor === true
|
|
135
|
+
|
|
136
|
+
/** The Color at `holder`'s `key`: its own, or a uniform's value it names. */
|
|
137
|
+
const colourAt = (holder: Holder, key: string): Holder | undefined => {
|
|
138
|
+
const own = holder[key]
|
|
139
|
+
if (isColor(own)) return own as Holder
|
|
140
|
+
// A bare uniforms object's.
|
|
141
|
+
if (isUniform(own) && isColor(own.value)) return own.value as Holder
|
|
142
|
+
const uniforms = holder.uniforms
|
|
143
|
+
if (!Predicate.isObject(uniforms)) return undefined
|
|
144
|
+
const uniform = (uniforms as Holder)[key]
|
|
145
|
+
if (isUniform(uniform) && isColor(uniform.value)) return uniform.value as Holder
|
|
146
|
+
return undefined
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The Color `key` names on `subject`: its own, else its material's (the first of an array that has one), its own uniform's or its material's. */
|
|
150
|
+
export const colourOf = (subject: object, key: string): Holder | undefined => {
|
|
151
|
+
const holder = subject as Holder
|
|
152
|
+
const own = colourAt(holder, key)
|
|
153
|
+
if (Predicate.isNotUndefined(own)) return own
|
|
154
|
+
for (const material of materialsOf(holder)) {
|
|
155
|
+
const found = colourAt(material, key)
|
|
156
|
+
if (Predicate.isNotUndefined(found)) return found
|
|
157
|
+
}
|
|
158
|
+
return undefined
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Each Subject's places, by key, as found. */
|
|
162
|
+
// Module state: held weakly by the Subject, so it goes with it.
|
|
163
|
+
const PLACES = new WeakMap<object, Map<string, Place>>()
|
|
164
|
+
|
|
165
|
+
/** Where `key` lives on `subject`, kept until its material changes. */
|
|
166
|
+
export const placeOf = (subject: Holder, key: string): Place | undefined => {
|
|
167
|
+
let places = PLACES.get(subject)
|
|
168
|
+
if (Predicate.isUndefined(places)) {
|
|
169
|
+
places = new Map()
|
|
170
|
+
PLACES.set(subject, places)
|
|
171
|
+
}
|
|
172
|
+
const kept = places.get(key)
|
|
173
|
+
if (Predicate.isNotUndefined(kept) && kept.material === subject.material) return kept
|
|
174
|
+
const found = find(subject, key)
|
|
175
|
+
if (Predicate.isUndefined(found)) {
|
|
176
|
+
places.delete(key)
|
|
177
|
+
return undefined
|
|
178
|
+
}
|
|
179
|
+
places.set(key, found)
|
|
180
|
+
return found
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Where a transform key is read on a scene object: its vector, its axis, and its unit (degrees for a rotation). */
|
|
184
|
+
const TRANSFORM_READS: Readonly<Record<string, readonly [vector: string, axis: string, per: number]>> = {
|
|
185
|
+
x: ["position", "x", 1],
|
|
186
|
+
y: ["position", "y", 1],
|
|
187
|
+
z: ["position", "z", 1],
|
|
188
|
+
rotateX: ["rotation", "x", RADIANS],
|
|
189
|
+
rotateY: ["rotation", "y", RADIANS],
|
|
190
|
+
rotateZ: ["rotation", "z", RADIANS],
|
|
191
|
+
// `scale` is its `x`, as Motion's.
|
|
192
|
+
scale: ["scale", "x", 1],
|
|
193
|
+
scaleX: ["scale", "x", 1],
|
|
194
|
+
scaleY: ["scale", "y", 1],
|
|
195
|
+
scaleZ: ["scale", "z", 1],
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** A scene object's transform key, read from its position, rotation in degrees, or scale. */
|
|
199
|
+
const readTransform = (subject: Holder, key: string): number => {
|
|
200
|
+
const [vector, axis, per] = TRANSFORM_READS[key]
|
|
201
|
+
return ((subject[vector] as Holder)[axis] as number) / per
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** `subject`'s value for `key` now, where the lookup finds it: `undefined` where it's nowhere. */
|
|
205
|
+
export const read = (subject: object, key: string): unknown => {
|
|
206
|
+
const holder = subject as Holder
|
|
207
|
+
if (isObject3D(holder) && isTransformKey(key)) return readTransform(holder, key)
|
|
208
|
+
const place = placeOf(holder, key)
|
|
209
|
+
if (Predicate.isUndefined(place)) return undefined
|
|
210
|
+
return place.holders[0][place.props[0]]
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Materials whose `transparent` Wardic set, so it puts it back once their opacity is 1 again. */
|
|
214
|
+
// Module state: held weakly by the material.
|
|
215
|
+
const MADE_TRANSPARENT = new WeakSet<object>()
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* three's rule after `opacity` on a material: below 1 it's drawn transparent, and once it's back at 1
|
|
219
|
+
* Wardic puts back the `transparent` it set. `needsUpdate` only on a flip, which recompiles it.
|
|
220
|
+
*/
|
|
221
|
+
const ruleOpacity = (material: Holder, opacity: unknown): void => {
|
|
222
|
+
if (!Predicate.isNumber(opacity)) return
|
|
223
|
+
if (opacity < 1) {
|
|
224
|
+
if (material.transparent === true) return
|
|
225
|
+
material.transparent = true
|
|
226
|
+
material.needsUpdate = true
|
|
227
|
+
MADE_TRANSPARENT.add(material)
|
|
228
|
+
return
|
|
229
|
+
}
|
|
230
|
+
if (!MADE_TRANSPARENT.has(material)) return
|
|
231
|
+
MADE_TRANSPARENT.delete(material)
|
|
232
|
+
material.transparent = false
|
|
233
|
+
material.needsUpdate = true
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** `value` written to `key` on `subject`, where the lookup finds it, with `opacity`'s rule after it: nowhere, nothing written. */
|
|
237
|
+
export const write = (subject: Holder, key: string, value: unknown): void => {
|
|
238
|
+
const place = placeOf(subject, key)
|
|
239
|
+
if (Predicate.isUndefined(place)) return
|
|
240
|
+
const holders = place.holders
|
|
241
|
+
const props = place.props
|
|
242
|
+
for (let i = 0; i < holders.length; i++) {
|
|
243
|
+
const holder = holders[i]
|
|
244
|
+
holder[props[i]] = value
|
|
245
|
+
if (key === "opacity" && holder.isMaterial === true) ruleOpacity(holder, value)
|
|
246
|
+
}
|
|
247
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dev warning at a key Wardic finds on a mesh's material that other meshes share: they all change
|
|
3
|
+
* with it. Once per material, at the first key found on it, by one walk of the mesh's scene.
|
|
4
|
+
*
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
// ADR-0073 decision 23 (Q27, Q33) and *Amendment (#883)*, Q6: in dev only, under `DEV`; three has no
|
|
8
|
+
// link from a material back to its meshes, so the walk is from the mesh's root.
|
|
9
|
+
import * as Arr from "effect/Array"
|
|
10
|
+
import * as Predicate from "effect/Predicate"
|
|
11
|
+
|
|
12
|
+
/** Where the warning goes: the host's console. */
|
|
13
|
+
// @wardic/three compiles against ES2022 alone, which doesn't declare `console`: a host global wherever Wardic runs.
|
|
14
|
+
declare const console: { readonly warn: (message: string) => void }
|
|
15
|
+
|
|
16
|
+
/** Any object of the scene. */
|
|
17
|
+
type Holder = Record<string, unknown>
|
|
18
|
+
|
|
19
|
+
/** Materials already checked: each is walked for once. */
|
|
20
|
+
// Module state: held weakly by the material.
|
|
21
|
+
const CHECKED = new WeakSet<object>()
|
|
22
|
+
|
|
23
|
+
/** What an object is called in the warning: its `name`, else its type's. */
|
|
24
|
+
const nameOf = (object: Holder): string => {
|
|
25
|
+
if (Predicate.isString(object.name) && object.name !== "") return object.name
|
|
26
|
+
if (Predicate.isString(object.type)) return `a ${object.type}`
|
|
27
|
+
return "an object"
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** How the code to fix it names `mesh`: its name, as a variable, else `mesh`. */
|
|
31
|
+
const variableOf = (mesh: Holder): string => {
|
|
32
|
+
if (Predicate.isString(mesh.name) && /^[A-Za-z_$][\w$]*$/.test(mesh.name)) return mesh.name
|
|
33
|
+
return "mesh"
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The root of `object`'s graph: its scene, where it's in one. */
|
|
37
|
+
const rootOf = (object: Holder): Holder => {
|
|
38
|
+
if (Predicate.isObject(object.parent)) return rootOf(object.parent as Holder)
|
|
39
|
+
return object
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Whether `object` draws with `material`: as its material, or one of an array of them. */
|
|
43
|
+
const drawsWith = (object: Holder, material: Holder): boolean => {
|
|
44
|
+
const own = object.material
|
|
45
|
+
if (Arr.isArray(own)) return Arr.contains(own as ReadonlyArray<unknown>, material)
|
|
46
|
+
return own === material
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The other objects of `mesh`'s scene that draw with `material`. */
|
|
50
|
+
const sharersOf = (mesh: Holder, material: Holder): ReadonlyArray<Holder> => {
|
|
51
|
+
const root = rootOf(mesh)
|
|
52
|
+
const found: Array<Holder> = []
|
|
53
|
+
if (!Predicate.isFunction(root.traverse)) return found
|
|
54
|
+
const traverse = root.traverse as (this: Holder, visit: (object: Holder) => void) => void
|
|
55
|
+
traverse.call(root, (object) => {
|
|
56
|
+
if (object !== mesh && drawsWith(object, material)) found.push(object)
|
|
57
|
+
})
|
|
58
|
+
return found
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** A material's kind, as three names it: its type, else "material". */
|
|
62
|
+
const kindOf = (material: Holder): string => {
|
|
63
|
+
if (Predicate.isString(material.type)) return material.type
|
|
64
|
+
return "material"
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Up to three of `others`' names, and an ellipsis for the rest. */
|
|
68
|
+
const listOf = (others: ReadonlyArray<Holder>): string => {
|
|
69
|
+
const listed = Arr.join(Arr.map(Arr.take(others, 3), nameOf), ", ")
|
|
70
|
+
if (others.length > 3) return `${listed}, …`
|
|
71
|
+
return listed
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** How many others share it. */
|
|
75
|
+
const shareOf = (others: ReadonlyArray<Holder>): string => {
|
|
76
|
+
if (others.length === 1) return "1 other mesh shares"
|
|
77
|
+
return `${others.length} other meshes share`
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** What the key does to every one of them, and to the mesh alone: an opacity fades. */
|
|
81
|
+
const changeOf = (key: string): readonly [every: string, alone: string] => {
|
|
82
|
+
if (key === "opacity") return ["fades", "fade"]
|
|
83
|
+
return [`changes its ${key}`, "change"]
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The warning, naming the mesh, the key, the material, and up to three of the others. */
|
|
87
|
+
export const message = (mesh: Holder, key: string, material: Holder, others: ReadonlyArray<Holder>): string => {
|
|
88
|
+
const meshName = nameOf(mesh)
|
|
89
|
+
const [every, alone] = changeOf(key)
|
|
90
|
+
const name = variableOf(mesh)
|
|
91
|
+
return `[wardic] ${meshName}'s ${key} is on a ${kindOf(material)} that ${shareOf(others)} (${listOf(others)}).\nEvery one of them ${every}. Give ${meshName} its own material (${name}.material = ${name}.material.clone()) to ${alone} it alone.`
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Warn, once per material, where `key` found on `mesh`'s `materials` changes other meshes too. */
|
|
95
|
+
export const check = (mesh: Holder, key: string, materials: ReadonlyArray<Holder>): void =>
|
|
96
|
+
Arr.forEach(materials, (material) => {
|
|
97
|
+
if (CHECKED.has(material)) return
|
|
98
|
+
CHECKED.add(material)
|
|
99
|
+
const others = sharersOf(mesh, material)
|
|
100
|
+
if (others.length > 0) console.warn(message(mesh, key, material, others))
|
|
101
|
+
})
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Stage's features (#885's PR 5): what a scene app adds to `createStage`'s `features` for what it uses,
|
|
3
|
+
* as a page's Features are given to its provider, so a scene that uses none carries none of their code.
|
|
4
|
+
*
|
|
5
|
+
* - **`stageDrag`** drags a ward's Subject (`ward(mesh, { drag })`), `@wardic/three`'s `ThreeDrag`.
|
|
6
|
+
* - **`stageBatches`** makes `createBatch`'s scene Batches (`object`, `instances`).
|
|
7
|
+
*
|
|
8
|
+
* A ward's `drag`, or a scene's `createBatch`, on a Stage without its feature is an error in development
|
|
9
|
+
* naming it.
|
|
10
|
+
*
|
|
11
|
+
* @module
|
|
12
|
+
*/
|
|
13
|
+
// #885's PR 5: the owner's choice, opt-in, after drag (PR 4) and Batches (PR 5) were 5.1 KB gzipped in every scene app.
|
|
14
|
+
import type { Stage, StageDrag, StageWardEnvironment } from "@wardic/solid"
|
|
15
|
+
import type { Camera, Object3D } from "three"
|
|
16
|
+
import type * as WardicThree from "../WardicThree.ts"
|
|
17
|
+
|
|
18
|
+
/** What a Stage drags with: its camera and its canvas, once there is one. A Stage adopting a runtime has neither. */
|
|
19
|
+
export interface StageView {
|
|
20
|
+
readonly camera: Camera | (() => Camera)
|
|
21
|
+
/** Its canvas, where it is on the page. */
|
|
22
|
+
readonly canvas: () => { readonly getBoundingClientRect: () => { readonly left: number; readonly top: number; readonly width: number; readonly height: number } } | undefined
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** What a feature builds on: the Stage's runtime, its view, its wards' environment, and its scene. */
|
|
26
|
+
export interface StageParts {
|
|
27
|
+
readonly runtime: WardicThree.WardicThree
|
|
28
|
+
readonly view: StageView | undefined
|
|
29
|
+
readonly scene: Object3D | undefined
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** A Stage's feature: the drag it gives, or the Batches it makes, from its parts. */
|
|
33
|
+
export interface StageFeature {
|
|
34
|
+
/** Its drag: none on a Stage with no view of its own. */
|
|
35
|
+
readonly drag?: ((parts: StageParts) => StageDrag | undefined) | undefined
|
|
36
|
+
/** Its Batches, made on its wards' environment. */
|
|
37
|
+
readonly batch?: ((parts: StageParts, environment: StageWardEnvironment) => NonNullable<Stage["batch"]>) | undefined
|
|
38
|
+
}
|