@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.
Files changed (109) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +84 -2
  3. package/THIRD_PARTY_NOTICES.md +145 -0
  4. package/dist/TestStage.d.ts +45 -0
  5. package/dist/TestStage.d.ts.map +1 -0
  6. package/dist/TestStage.js +140 -0
  7. package/dist/TestStage.js.map +1 -0
  8. package/dist/ThreeColours.d.ts +52 -0
  9. package/dist/ThreeColours.d.ts.map +1 -0
  10. package/dist/ThreeColours.js +237 -0
  11. package/dist/ThreeColours.js.map +1 -0
  12. package/dist/ThreeDrag.d.ts +82 -0
  13. package/dist/ThreeDrag.d.ts.map +1 -0
  14. package/dist/ThreeDrag.js +243 -0
  15. package/dist/ThreeDrag.js.map +1 -0
  16. package/dist/ThreeInput.d.ts +69 -0
  17. package/dist/ThreeInput.d.ts.map +1 -0
  18. package/dist/ThreeInput.js +592 -0
  19. package/dist/ThreeInput.js.map +1 -0
  20. package/dist/ThreeInstances.d.ts +47 -0
  21. package/dist/ThreeInstances.d.ts.map +1 -0
  22. package/dist/ThreeInstances.js +392 -0
  23. package/dist/ThreeInstances.js.map +1 -0
  24. package/dist/ThreeRenderer.d.ts +45 -0
  25. package/dist/ThreeRenderer.d.ts.map +1 -0
  26. package/dist/ThreeRenderer.js +121 -0
  27. package/dist/ThreeRenderer.js.map +1 -0
  28. package/dist/ThreeValues.d.ts +15 -0
  29. package/dist/ThreeValues.d.ts.map +1 -0
  30. package/dist/ThreeValues.js +61 -0
  31. package/dist/ThreeValues.js.map +1 -0
  32. package/dist/WardicThree.d.ts +194 -0
  33. package/dist/WardicThree.d.ts.map +1 -0
  34. package/dist/WardicThree.js +162 -0
  35. package/dist/WardicThree.js.map +1 -0
  36. package/dist/index.d.ts +26 -0
  37. package/dist/index.d.ts.map +1 -0
  38. package/dist/index.js +27 -0
  39. package/dist/index.js.map +1 -0
  40. package/dist/internal/dev.d.ts +10 -0
  41. package/dist/internal/dev.d.ts.map +1 -0
  42. package/dist/internal/dev.js +11 -0
  43. package/dist/internal/dev.js.map +1 -0
  44. package/dist/internal/dev.production.d.ts +8 -0
  45. package/dist/internal/dev.production.d.ts.map +1 -0
  46. package/dist/internal/dev.production.js +8 -0
  47. package/dist/internal/dev.production.js.map +1 -0
  48. package/dist/internal/lookup.d.ts +29 -0
  49. package/dist/internal/lookup.d.ts.map +1 -0
  50. package/dist/internal/lookup.js +245 -0
  51. package/dist/internal/lookup.js.map +1 -0
  52. package/dist/internal/shared-material.d.ts +8 -0
  53. package/dist/internal/shared-material.d.ts.map +1 -0
  54. package/dist/internal/shared-material.js +95 -0
  55. package/dist/internal/shared-material.js.map +1 -0
  56. package/dist/solid/feature.d.ts +42 -0
  57. package/dist/solid/feature.d.ts.map +1 -0
  58. package/dist/solid/feature.js +2 -0
  59. package/dist/solid/feature.js.map +1 -0
  60. package/dist/solid/scene-batch.d.ts +10 -0
  61. package/dist/solid/scene-batch.d.ts.map +1 -0
  62. package/dist/solid/scene-batch.js +436 -0
  63. package/dist/solid/scene-batch.js.map +1 -0
  64. package/dist/solid/scene-object.d.ts +27 -0
  65. package/dist/solid/scene-object.d.ts.map +1 -0
  66. package/dist/solid/scene-object.js +111 -0
  67. package/dist/solid/scene-object.js.map +1 -0
  68. package/dist/solid/stage-batches.d.ts +10 -0
  69. package/dist/solid/stage-batches.d.ts.map +1 -0
  70. package/dist/solid/stage-batches.js +6 -0
  71. package/dist/solid/stage-batches.js.map +1 -0
  72. package/dist/solid/stage-drag.d.ts +4 -0
  73. package/dist/solid/stage-drag.d.ts.map +1 -0
  74. package/dist/solid/stage-drag.js +19 -0
  75. package/dist/solid/stage-drag.js.map +1 -0
  76. package/dist/solid/stage.d.ts +107 -0
  77. package/dist/solid/stage.d.ts.map +1 -0
  78. package/dist/solid/stage.js +320 -0
  79. package/dist/solid/stage.js.map +1 -0
  80. package/dist/solid/test-stage.d.ts +29 -0
  81. package/dist/solid/test-stage.d.ts.map +1 -0
  82. package/dist/solid/test-stage.js +16 -0
  83. package/dist/solid/test-stage.js.map +1 -0
  84. package/dist/solid.d.ts +55 -0
  85. package/dist/solid.d.ts.map +1 -0
  86. package/dist/solid.js +9 -0
  87. package/dist/solid.js.map +1 -0
  88. package/package.json +99 -4
  89. package/src/TestStage.ts +177 -0
  90. package/src/ThreeColours.ts +247 -0
  91. package/src/ThreeDrag.ts +341 -0
  92. package/src/ThreeInput.ts +744 -0
  93. package/src/ThreeInstances.ts +481 -0
  94. package/src/ThreeRenderer.ts +159 -0
  95. package/src/ThreeValues.ts +69 -0
  96. package/src/WardicThree.ts +347 -0
  97. package/src/index.ts +26 -0
  98. package/src/internal/dev.production.ts +7 -0
  99. package/src/internal/dev.ts +10 -0
  100. package/src/internal/lookup.ts +247 -0
  101. package/src/internal/shared-material.ts +101 -0
  102. package/src/solid/feature.ts +38 -0
  103. package/src/solid/scene-batch.ts +490 -0
  104. package/src/solid/scene-object.ts +145 -0
  105. package/src/solid/stage-batches.ts +13 -0
  106. package/src/solid/stage-drag.ts +26 -0
  107. package/src/solid/stage.ts +409 -0
  108. package/src/solid/test-stage.ts +40 -0
  109. package/src/solid.ts +111 -0
@@ -0,0 +1,69 @@
1
+ /**
2
+ * A three.js Subject's values for Wardic: where each key it animates starts, found where the writer
3
+ * writes it (a scene object's transforms, its own number, its material's, a uniform's, a TSL uniform
4
+ * node's, a vector's axis), and the scene's Positional keys. A key found nowhere, or whose value isn't
5
+ * a number or a string, is an error naming it: Wardic adds no property to a scene.
6
+ *
7
+ * @example
8
+ * import * as ThreeValues from "@wardic/three/ThreeValues"
9
+ *
10
+ * ThreeValues.read(mesh, "rotateY") // its rotation.y, in degrees
11
+ * ThreeValues.read(mesh, "opacity") // mesh.material.opacity
12
+ */
13
+ // `@wardic/three/ThreeValues` (#883; ADR-0069 decisions 4, 5 and 9, ADR-0073 *Amendment (#883)*, Q3):
14
+ // `@wardic/object`'s start links on three's lookup.
15
+ import * as ObjectValues from "@wardic/object/ObjectValues"
16
+ import * as ReducedMotion from "@wardic/core/ReducedMotion"
17
+ import type * as Spine from "@wardic/core/unstable/Spine"
18
+ import * as Arr from "effect/Array"
19
+ import * as Layer from "effect/Layer"
20
+ import * as Option from "effect/Option"
21
+ import * as Predicate from "effect/Predicate"
22
+ import * as Lookup from "./internal/lookup.ts"
23
+
24
+ /** Any object of a scene. */
25
+ type Holder = Record<string, unknown>
26
+
27
+ /** What `subject` is called in an error: its `name`, else its type's, else "the object". */
28
+ const nameOf = (subject: object): string => {
29
+ const holder = subject as Holder
30
+ if (Predicate.isString(holder.name) && holder.name !== "") return `"${holder.name}"`
31
+ if (Predicate.isString(holder.type)) return `a ${holder.type}`
32
+ return "the object"
33
+ }
34
+
35
+ /** Whether `holder`'s `key` is a three `Color`. */
36
+ const isColorAt = (holder: unknown, key: string): boolean => Predicate.isObject(holder) && Predicate.isObject((holder as Holder)[key]) && ((holder as Holder)[key] as Holder).isColor === true
37
+
38
+ /** Whether `key` is a `Color` on `subject` or its material. */
39
+ const isColorKey = (subject: object, key: string): boolean => isColorAt(subject, key) || Arr.some(Lookup.materialsOf(subject as Holder), (material) => isColorAt(material, key))
40
+
41
+ /**
42
+ * `subject`'s value for `key` now, as a Batch reads a member's before it moves it: where the lookup finds
43
+ * it, `undefined` where it's nowhere.
44
+ */
45
+ export const read: (subject: object, key: string) => unknown = Lookup.read
46
+
47
+ /** `subject`'s own value for `key`, a number or a string, else an error naming the key. */
48
+ const ownValueOf = (subject: object, key: string): Option.Option<number | string> => {
49
+ const value = Lookup.read(subject, key)
50
+ if (Predicate.isNumber(value) || Predicate.isString(value)) return Option.some(value)
51
+ if (isColorKey(subject, key)) {
52
+ throw new Error(`wardic.animate: ${nameOf(subject)}'s "${key}" is a Color, which Wardic reads as a colour at the call: give it a colour, animate(mesh, { ${key}: "#ff8800" }), or its channels in linear light, ${key}R, ${key}G and ${key}B.`)
53
+ }
54
+ if (Predicate.isUndefined(value)) {
55
+ throw new Error(`wardic.animate: ${nameOf(subject)} has no "${key}" to animate: not its own, nor its material's, nor a uniform's, nor a vector's axis (positionX). Give a key it has; Wardic adds no property to a scene.`)
56
+ }
57
+ throw new Error(`wardic.animate: ${nameOf(subject)}'s "${key}" is ${typeof value}, which can't be animated: a key's value is a number or a string.`)
58
+ }
59
+
60
+ /** `subject`'s Base-value links: each key starts from its value where the lookup finds it. */
61
+ // ADR-0022.
62
+ export const startLinksOf: (subject: object) => Spine.StartLinks = /*#__PURE__*/ ObjectValues.startLinksWith(ownValueOf)
63
+
64
+ /** Whether reduced motion snaps `key` in a scene: a transform key on any Subject, and a camera's `fov` and `zoom`. */
65
+ // ADR-0069 decision 9.
66
+ export const isPositional = (key: string): boolean => ReducedMotion.isTransform(key) || key === "fov" || key === "zoom"
67
+
68
+ /** A scene's Positional keys: the vestibular case is a camera's move as much as an object's. */
69
+ export const positionalKeys: Layer.Layer<ReducedMotion.PositionalKeys> = /*#__PURE__*/ Layer.succeed(ReducedMotion.PositionalKeys, { isPositional })
@@ -0,0 +1,347 @@
1
+ /**
2
+ * Wardic for three.js: Motion's `animate` on a scene's meshes, materials, cameras, lights and uniforms,
3
+ * on frames the Host loop pumps. One runtime per Host loop: `make` builds it, the loop calls
4
+ * `frame(time)` once a frame before it renders, and `dispose` ends it.
5
+ *
6
+ * - **Transform keys** are Motion's names: `x`, `y` and `z` move `position`; `rotateX`, `rotateY` and
7
+ * `rotateZ` turn `rotation`, in degrees; `scale` sets all three scales, and `scaleX`, `scaleY` and
8
+ * `scaleZ` one each.
9
+ * - **Any other key** is the object's own number (a camera's `fov`, a light's `intensity`), else its
10
+ * material's (`opacity`, `roughness`: on every mesh that shares the material), a uniform's, or a
11
+ * vector's axis (`emissiveIntensity`, `uProgress`, `positionX`). The types are three's own:
12
+ * `animate(mesh, { rotatY: 90 })` is an error.
13
+ * - **Colours** (`color`, `emissive`, a uniform's `Color`) take a CSS colour, a hex number or a
14
+ * `Color`, and blend in linear light, three's: red to blue passes `#bc00bc`. A spring keeps a
15
+ * colour's speed through a change of target, as it keeps a position's.
16
+ * - **A Batch** (`batch`) moves many objects as one run, or an `InstancedMesh`'s instances: each by
17
+ * its transforms and its instance colour, its matrix written in place.
18
+ * - **Units:** `x`, `y` and `z` are metres, so springs rest within millimetres (`unitScale`).
19
+ * - **three's rules after a write:** a camera's projection keys update its projection matrix, and an
20
+ * `opacity` below 1 draws its material transparent.
21
+ * - **Hover and press** (`hover`, `press`), given `make`'s `canvas`, `camera` and `scene`: a pointer hits
22
+ * the nearest drawn object under it, cast once a frame (`ThreeInput`).
23
+ *
24
+ * @example
25
+ * import * as WardicThree from "@wardic/three/WardicThree"
26
+ *
27
+ * const wardic = WardicThree.make()
28
+ * renderer.setAnimationLoop((time) => {
29
+ * wardic.frame(time)
30
+ * renderer.render(scene, camera)
31
+ * })
32
+ * wardic.animate(cube, { y: 2, rotateY: 180 }, { type: "spring", bounce: 0.3 })
33
+ * await wardic.animate(cube, { opacity: 0 }, { duration: 0.4 })
34
+ */
35
+ // `@wardic/three/WardicThree` (#883; ADR-0069 decisions 3–9 and 21, ADR-0073 *Amendment (#883)*, Q1–Q3):
36
+ // `@wardic/object`'s runtime through `WardicObject.makeWith`, on three's Environment. The scene's input
37
+ // (`hover`, `press`, the raycast) is #883's PR 5a (`ThreeInput`); colours are PR 3's (`ThreeColours`), split at the call
38
+ // into their channels; an `InstancedMesh`'s instances PR 4's (`ThreeInstances`, its Environment's `batchSource`).
39
+ import * as UnitScale from "@wardic/core/UnitScale"
40
+ import type * as Config from "@wardic/core/unstable/Config"
41
+ import * as Imperative from "@wardic/core/unstable/Imperative"
42
+ import * as ObjectBatch from "@wardic/object/ObjectBatch"
43
+ import * as ObjectValues from "@wardic/object/ObjectValues"
44
+ import * as WardicObject from "@wardic/object/WardicObject"
45
+ import * as Arr from "effect/Array"
46
+ import { constUndefined } from "effect/Function"
47
+ import * as Predicate from "effect/Predicate"
48
+ import * as UndefinedOr from "effect/UndefinedOr"
49
+ import type { Camera, Object3D } from "three"
50
+ import * as ThreeColours from "./ThreeColours.ts"
51
+ import type * as InputSource from "@wardic/core/InputSource"
52
+ import * as ThreeInput from "./ThreeInput.ts"
53
+ import * as ThreeInstances from "./ThreeInstances.ts"
54
+ import * as ThreeRenderer from "./ThreeRenderer.ts"
55
+ import * as ThreeValues from "./ThreeValues.ts"
56
+
57
+ /** A scene object's transform keys: its position, its rotation in degrees, and its scales. */
58
+ // ADR-0069 decision 4.
59
+ export type TransformKey = "x" | "y" | "z" | "rotateX" | "rotateY" | "rotateZ" | "scale" | "scaleX" | "scaleY" | "scaleZ"
60
+
61
+ /** `T`'s keys that hold a number. */
62
+ type NumberKey<T> = { readonly [K in keyof T]-?: T[K] extends number ? K : never }[keyof T] & string
63
+
64
+ /** The axes of a three vector, by its flag: `X`, `Y`, and `Z` and `W` where it has them. */
65
+ type AxesOf<V> = V extends { readonly isVector4: true } ? "X" | "Y" | "Z" | "W" : V extends { readonly isVector3: true } ? "X" | "Y" | "Z" : V extends { readonly isVector2: true } ? "X" | "Y" : never
66
+
67
+ /** `T`'s vectors' axes as keys: `positionX`, `uOffsetY`. */
68
+ type VectorKey<T> = { readonly [K in keyof T]-?: K extends string ? `${K}${AxesOf<T[K]>}` : never }[keyof T]
69
+
70
+ /** A three `Color`, by its flag. */
71
+ type IsColor = { readonly isColor: true }
72
+
73
+ /** `T`'s keys that hold a three `Color`. */
74
+ type ColourKeyOf<T> = { readonly [K in keyof T]-?: T[K] extends IsColor ? K : never }[keyof T] & string
75
+
76
+ /** A colour's channel keys, in linear light: `colorR`, `colorG`, `colorB`. */
77
+ type ChannelKey<K extends string> = `${K}${(typeof ThreeColours.CHANNELS)[number]}`
78
+
79
+ /** The names of `U`'s uniforms that hold a number, a vector's axes, or a colour's channels. */
80
+ type UniformKey<U> = {
81
+ readonly [K in keyof U]-?: U[K] extends { value: number } ? K : U[K] extends { value: IsColor } ? (K extends string ? ChannelKey<K> : never) : U[K] extends { value: infer V } ? (K extends string ? `${K}${AxesOf<V>}` : never) : never
82
+ }[keyof U] & string
83
+
84
+ /** The names of `U`'s uniforms that hold a `Color`. */
85
+ type UniformColourKey<U> = { readonly [K in keyof U]-?: U[K] extends { value: IsColor } ? K : never }[keyof U] & string
86
+
87
+ /** The uniforms `M` holds, a `ShaderMaterial`'s. */
88
+ type UniformsOf<M> = M extends { readonly uniforms: infer U } ? U : never
89
+
90
+ /** The number keys of `M`, a material: its numbers, its vectors' axes, its colours' channels, and its uniforms'. */
91
+ type MaterialKey<M> = NumberKey<M> | VectorKey<M> | ChannelKey<ColourKeyOf<M>> | UniformKey<UniformsOf<M>>
92
+
93
+ /** The colour keys of `M`, a material: its `Color`s and its uniforms'. */
94
+ type MaterialColourKey<M> = ColourKeyOf<M> | UniformColourKey<UniformsOf<M>>
95
+
96
+ /** The material a mesh's `material` gives it: each of an array of them. */
97
+ type MaterialOf<T> = T extends { readonly material: infer M } ? (M extends ReadonlyArray<infer E> ? E : M) : never
98
+
99
+ /** The number keys of a three Subject `T`: a scene object's transforms, numbers and vectors' axes, and its material's; a material's own. */
100
+ export type SceneKey<T> = T extends { readonly isObject3D: true } ? TransformKey | NumberKey<T> | VectorKey<T> | MaterialKey<MaterialOf<T>> : T extends { readonly isMaterial: unknown } ? MaterialKey<T> : never
101
+
102
+ /** The colour keys of a three Subject `T`: its material's `Color`s for a scene object, a material's own, a uniforms object's. */
103
+ export type ColourKey<T> = T extends { readonly isObject3D: true } ? ColourKeyOf<T> | MaterialColourKey<MaterialOf<T>> : T extends { readonly isMaterial: unknown } ? MaterialColourKey<T> : UniformColourKey<T>
104
+
105
+ /** A key's value in a scene: a number, or keyframes of them. */
106
+ type SceneValue = number | Config.WardicKeyframes<number>
107
+
108
+ /** A colour key's value: a colour, or keyframes of them. */
109
+ type ColourTargetValue = ThreeColours.ColourValue | Config.WardicKeyframes<ThreeColours.ColourValue>
110
+
111
+ /** A key's value among number keys `K` and colour keys `C`: either, for a key in both (a `ShaderMaterial`'s uniforms, which three types as any name of any value). */
112
+ type ValueOf<Key, K, C, N, Colour> = Key extends C ? (Key extends K ? N | Colour : Colour) : N
113
+
114
+ /** A target for a scene's Subject, by its number keys `K` and its colour keys `C`. */
115
+ type SceneTarget<K extends string, C extends string> = { readonly [Key in K | C]?: ValueOf<Key, K, C, SceneValue, ColourTargetValue> } & {
116
+ readonly transition?: Config.WardicTransition | undefined
117
+ readonly transitionEnd?: { readonly [Key in K | C]?: ValueOf<Key, K, C, number, ThreeColours.ColourValue> } | undefined
118
+ }
119
+
120
+ /**
121
+ * A target for `T`: a scene object's or a material's by its scene keys and colours; a uniforms object's
122
+ * by its uniforms' names; any other object's (a `Vector3`, a plain `{ x }`) by its own keys, as `@wardic/object`'s.
123
+ */
124
+ export type ThreeTarget<T> = [SceneKey<T>] extends [never] ? WardicObject.ObjectTarget<T> & SceneTarget<UniformKey<T>, UniformColourKey<T>> : SceneTarget<SceneKey<T>, ColourKey<T>>
125
+
126
+ /** An `InstancedMesh`, by its flag. */
127
+ type IsInstancedMesh = { readonly isInstancedMesh: true }
128
+
129
+ /** The keys a Batch of `T` takes: an `InstancedMesh`'s instance's, a scene object's scene keys, any other object's numbers. */
130
+ export type BatchKey<T> = T extends ThreeInstances.Instance<IsInstancedMesh> ? ThreeInstances.InstanceKey : [SceneKey<T>] extends [never] ? ObjectBatch.NumberKey<T> : SceneKey<T>
131
+
132
+ /** The colour keys a Batch of `T` takes: an instance's `color`, a scene object's colours. */
133
+ export type BatchColourKey<T> = T extends ThreeInstances.Instance<IsInstancedMesh> ? "color" : T extends { readonly isObject3D: true } ? ColourKey<T> : never
134
+
135
+ /** A Batch of a scene's objects, or of an `InstancedMesh`'s instances: one run for all of them. `@wardic/object`'s, on scene keys. */
136
+ export type ThreeBatch<T extends object> = ObjectBatch.ObjectBatch<T, BatchKey<T>>
137
+
138
+ /** A Batch's stop of a colour: one for every member, or a function of a member's index and the count. */
139
+ type ColourStop = ThreeColours.ColourValue | ((i: number, n: number) => ThreeColours.ColourValue)
140
+
141
+ /** A Batch's value for a number key: a stop, two (its first and its target), or a function giving them. `@wardic/object`'s. */
142
+ type BatchNumberValue = ObjectBatch.Stop | readonly [ObjectBatch.Stop, ObjectBatch.Stop] | ((i: number, n: number) => number | readonly [number, number])
143
+
144
+ /** A Batch's value for a colour key: a stop, two, or a function giving them. */
145
+ type BatchColourValue = ColourStop | readonly [ColourStop, ColourStop] | ((i: number, n: number) => ThreeColours.ColourValue | readonly [ThreeColours.ColourValue, ThreeColours.ColourValue])
146
+
147
+ /** A Batch of `T`'s targets for an `animate` call: each number key and colour key a stop, two, or a function of a member's index giving them. */
148
+ export type ThreeBatchTarget<T> = { readonly [Key in BatchKey<T> | BatchColourKey<T>]?: ValueOf<Key, BatchKey<T>, BatchColourKey<T>, BatchNumberValue, BatchColourValue> }
149
+
150
+ /** A Batch's colour key in a sequence's Segment of it: a colour, `null` for where it is, stops of them, or a function giving them. */
151
+ type ColourSegmentValue =
152
+ | ThreeColours.ColourValue
153
+ | null
154
+ | ReadonlyArray<ThreeColours.ColourValue | null>
155
+ | ((i: number, n: number) => ThreeColours.ColourValue | null | ReadonlyArray<ThreeColours.ColourValue | null>)
156
+
157
+ /** A Segment of a Batch, `[batch, target, options?]`, its colour keys taking colours: every member, or a member's handle, `batch.at(i)`. */
158
+ export type ThreeBatchSegment =
159
+ | readonly [batch: ObjectBatch.ObjectBatch<any, any> | ObjectBatch.Member<any, any>, target: Readonly<Record<string, ObjectBatch.SegmentValue | ColourSegmentValue>>]
160
+ | readonly [batch: ObjectBatch.ObjectBatch<any, any> | ObjectBatch.Member<any, any>, target: Readonly<Record<string, ObjectBatch.SegmentValue | ColourSegmentValue>>, options: Imperative.WardicSegmentOptions]
161
+
162
+ /** A sequence of a scene: `@wardic/object`'s Segments, its Segments of Batches taking colours. Motion's `AnimationSequence`. */
163
+ export type ThreeSequence = ReadonlyArray<WardicObject.ObjectSequence[number] | ThreeBatchSegment>
164
+
165
+ /**
166
+ * `animate(subject, target, options?)` animates a scene's Subject, or each of a list of them, to
167
+ * `target`; `animate(sequence, options?)` plays a sequence. Either returns its Playback controls. Motion's.
168
+ */
169
+ export interface ThreeAnimate {
170
+ <T extends object>(batch: ThreeBatch<T> | ObjectBatch.Member<T, BatchKey<T>>, target: ThreeBatchTarget<T>, options?: Imperative.WardicAnimateOptions): Imperative.WardicPlaybackControls
171
+ <T extends object>(subject: T | ReadonlyArray<T>, target: ThreeTarget<T>, options?: Imperative.WardicAnimateOptions): Imperative.WardicPlaybackControls
172
+ (sequence: ThreeSequence, options?: Imperative.WardicSequenceOptions): Imperative.WardicPlaybackControls
173
+ }
174
+
175
+ /** A runtime's options: `@wardic/object`'s, the scene's unit scale, and its input. */
176
+ export interface Options extends WardicObject.Options {
177
+ /**
178
+ * How many of Motion's units a metre is, for `x`, `y` and `z`'s springs' rest: 100 (a scene in
179
+ * metres), so a 10 m move rests within 5 mm. Give 1 for a scene in pixels.
180
+ */
181
+ readonly unitScale?: number | undefined
182
+ /** The canvas the scene is drawn on, whose pointer events `hover` and `press` hear: with `camera` and `scene`. */
183
+ readonly canvas?: ThreeInput.CanvasLike | undefined
184
+ /** The camera the scene is drawn with, or a function giving the one active now: a pointer's ray is cast from it. */
185
+ readonly camera?: Camera | (() => Camera) | undefined
186
+ /** The scene a pointer hits. */
187
+ readonly scene?: Object3D | undefined
188
+ /** What a pointer hits, in the raycast's place: a BVH's, or a GPU pick's. */
189
+ readonly hitTest?: ThreeInput.HitTest | undefined
190
+ /** Where a held pointer's events are heard once it leaves the canvas: the canvas's window when left out. */
191
+ readonly window?: EventTarget | undefined
192
+ }
193
+
194
+ /**
195
+ * `batch(objects, options?)` makes a Batch of a list of objects; `batch(instanced, options?)` one of an
196
+ * `InstancedMesh`'s instances, a member each. Either runs on the keys of its first `animate` (or `keys`).
197
+ */
198
+ export interface ThreeBatchOf {
199
+ <M extends IsInstancedMesh>(instanced: M, options?: ObjectBatch.Options<ThreeInstances.Instance<M>, ThreeInstances.InstanceKey | "color">): ThreeBatch<ThreeInstances.Instance<M>>
200
+ <T extends object>(objects: ReadonlyArray<T>, options?: ObjectBatch.Options<T, BatchKey<T> | BatchColourKey<T>>): ThreeBatch<T>
201
+ }
202
+
203
+ /** A runtime: what animates a scene on one Host loop. */
204
+ export interface WardicThree extends Omit<WardicObject.WardicObject, "animate" | "batch"> {
205
+ readonly animate: ThreeAnimate
206
+ /**
207
+ * A Batch of `objects`, or of an `InstancedMesh`'s instances: one run for all of them, on the keys of
208
+ * its first `animate` (or `keys`). `animate(batch, target)` moves every member; `animate(batch.at(i),
209
+ * target)` member `i`. An instance takes its transforms and its colour.
210
+ */
211
+ readonly batch: ThreeBatchOf
212
+ /**
213
+ * `hover(subject, onStart)`: `onStart` runs as a pointer comes over `subject` (a scene object, a Batch's
214
+ * member, or each member of a Batch), and what it returns, as the pointer goes off. Returns its cancel.
215
+ * Needs `make`'s `canvas`, `camera` and `scene`.
216
+ */
217
+ readonly hover: (subject: object, onStart: ThreeInput.HoverStart) => () => void
218
+ /**
219
+ * `press(subject, onStart)`: `onStart` runs as a press starts on `subject`, and what it returns, as it
220
+ * ends, told whether it ended on it. Returns its cancel. Needs `make`'s `canvas`, `camera` and `scene`.
221
+ */
222
+ readonly press: (subject: object, onStart: ThreeInput.PressStart) => () => void
223
+ /**
224
+ * The scene's pointer input, as core's `InputSource`, for a binding's gestures: `undefined` with no
225
+ * canvas. Unstable: what `@wardic/three/solid`'s Stage hands its hooks.
226
+ */
227
+ // #885's PR 1 (ADR-0073, *Amendment (#885)*, Q3): a Stage adopting a runtime made outside Solid reads it.
228
+ readonly input: InputSource.InputSource["Service"] | undefined
229
+ }
230
+
231
+ /** A scene's unit scale when the app gives none: a metre counts as 100 of Motion's units. */
232
+ // ADR-0069 decision 8.
233
+ export const UNIT_SCALE = 100
234
+
235
+ /** three's Environment for `@wardic/object`'s runtime, at `unitScale`. */
236
+ export const environment = (unitScale: number = UNIT_SCALE): WardicObject.Environment => ({
237
+ renderer: ThreeRenderer.layer,
238
+ gate: ThreeRenderer.GATE,
239
+ startLinksOf: ThreeValues.startLinksOf,
240
+ ownValues: ObjectValues.ownValues,
241
+ positionalKeys: ThreeValues.positionalKeys,
242
+ batchWriter: ThreeRenderer.writer,
243
+ batchRead: ThreeValues.read,
244
+ batchSource: ThreeInstances.batchSource(),
245
+ layers: UnitScale.layerWith(ThreeColours.unitScale(unitScale)),
246
+ })
247
+
248
+ /** A Segment of a sequence with its colour keys split: a Segment of a Subject or of a Batch, `[subject, target, options?]`; any other as it is. */
249
+ const splitSegment = (segment: unknown): unknown => {
250
+ if (!Arr.isArray(segment)) return segment
251
+ const [subject, target, options] = segment as ReadonlyArray<unknown>
252
+ if (Predicate.isFunction(subject)) return segment
253
+ const [split, splitOptions] = ThreeColours.splitCall(subjectOf(subject), target, options)
254
+ if (split === target) return segment
255
+ if (segment.length < 3) return [subject, split]
256
+ return [subject, split, splitOptions]
257
+ }
258
+
259
+ /** A Batch's handle, or a member's, as a call names it. */
260
+ interface BatchHandle {
261
+ readonly members?: ReadonlyArray<object>
262
+ readonly i?: number
263
+ readonly batch?: { readonly members: ReadonlyArray<object> }
264
+ }
265
+
266
+ /** A Batch's member a call's colours are read by: a member handle's own, else the Batch's first. */
267
+ const memberOf = (handle: BatchHandle): unknown => {
268
+ if (Predicate.isNumber(handle.i) && Predicate.isNotUndefined(handle.batch)) return handle.batch.members[handle.i]
269
+ return UndefinedOr.match(handle.members, { onUndefined: constUndefined, onDefined: (members) => members[0] })
270
+ }
271
+
272
+ /** The Subject a call's target is read for: a Batch's member, the first of a list, or the Subject itself. */
273
+ const subjectOf = (subject: unknown): unknown => {
274
+ if (Predicate.isNotUndefined(ObjectBatch.entryOf(subject))) return memberOf(subject as BatchHandle)
275
+ if (Arr.isArray(subject)) return (subject as ReadonlyArray<unknown>)[0]
276
+ return subject
277
+ }
278
+
279
+ /** `animate` with each colour key split into its channels at the call, for a Subject, a list of them, a Batch, or a sequence's Segments of them. */
280
+ const withColours = (animate: (first: unknown, second?: unknown, third?: unknown) => Imperative.WardicPlaybackControls) =>
281
+ (first: unknown, second?: unknown, third?: unknown): Imperative.WardicPlaybackControls => {
282
+ if (Imperative.isSequence(first)) return animate(Arr.map(first, splitSegment), second)
283
+ const [split, options] = ThreeColours.splitCall(subjectOf(first), second, third)
284
+ return animate(first, split, options)
285
+ }
286
+
287
+ /** `batch` with each colour key in its `keys` given as its channels. */
288
+ const withChannelKeys = (batch: (given: unknown, options?: ObjectBatch.Options<object, string>) => unknown) =>
289
+ (given: unknown, options?: ObjectBatch.Options<object, string>): unknown => {
290
+ if (Predicate.isUndefined(options) || Predicate.isUndefined(options.keys)) return batch(given, options)
291
+ return batch(given, { ...options, keys: ThreeColours.splitBatchKeys(given, options.keys) })
292
+ }
293
+
294
+ /** The scene's input `options` give: `undefined` without a canvas, an error naming what's missing with one. */
295
+ const inputOf = (options: Options): ThreeInput.SceneInput | undefined => {
296
+ const { canvas, camera, scene } = options
297
+ if (Predicate.isUndefined(canvas)) return undefined
298
+ if (Predicate.isUndefined(camera) || Predicate.isUndefined(scene)) {
299
+ throw new Error("WardicThree.make: a canvas's input needs its camera and its scene too, to cast a pointer's ray: make({ canvas, camera, scene }).")
300
+ }
301
+ return ThreeInput.make({ canvas, camera, scene, hitTest: options.hitTest, window: options.window })
302
+ }
303
+
304
+ /** A gesture's call on a runtime with no input: an error naming the options it needs. */
305
+ const noInput = (name: string) => (): never => {
306
+ throw new Error(`wardic.${name}: this runtime has no scene input. Give make the canvas the scene is drawn on, its camera and the scene: WardicThree.make({ canvas: renderer.domElement, camera, scene }).`)
307
+ }
308
+
309
+ /** A runtime on frames the Host loop pumps: `frame(time)` once a frame, before it renders. */
310
+ export const make = (options: Options = {}): WardicThree => {
311
+ const unitScale = UndefinedOr.match(options.unitScale, { onUndefined: () => UNIT_SCALE, onDefined: (given) => given })
312
+ const runtime = WardicObject.makeWith(environment(unitScale), options)
313
+ const input = inputOf(options)
314
+ if (Predicate.isUndefined(input)) {
315
+ return {
316
+ animate: withColours(runtime.animate as never) as unknown as ThreeAnimate,
317
+ batch: withChannelKeys(runtime.batch as never) as unknown as ThreeBatchOf,
318
+ afterWrites: runtime.afterWrites,
319
+ frame: runtime.frame,
320
+ wantsFrame: runtime.wantsFrame,
321
+ dispose: runtime.dispose,
322
+ stageEnvironment: runtime.stageEnvironment,
323
+ hover: noInput("hover"),
324
+ press: noInput("press"),
325
+ input: undefined,
326
+ }
327
+ }
328
+ return {
329
+ animate: withColours(runtime.animate as never) as unknown as ThreeAnimate,
330
+ batch: withChannelKeys(runtime.batch as never) as unknown as ThreeBatchOf,
331
+ afterWrites: runtime.afterWrites,
332
+ // The raycast first, against what was last drawn, then Wardic's writes.
333
+ frame: (time) => {
334
+ input.frame()
335
+ runtime.frame(time)
336
+ },
337
+ wantsFrame: () => runtime.wantsFrame() || input.wantsFrame(),
338
+ dispose: () => {
339
+ input.dispose()
340
+ runtime.dispose()
341
+ },
342
+ stageEnvironment: runtime.stageEnvironment,
343
+ hover: input.hover,
344
+ press: input.press,
345
+ input: input.source,
346
+ }
347
+ }
package/src/index.ts ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * `@wardic/three`: Wardic for three.js. Motion's `animate` on a scene's meshes, materials, cameras,
3
+ * lights and uniforms, by Motion's key names in three's own types, on frames the Host loop pumps. The
4
+ * root re-exports each module as a namespace; code imports the subpath,
5
+ * `import * as WardicThree from "@wardic/three/WardicThree"`.
6
+ *
7
+ * @example
8
+ * import { WardicThree } from "@wardic/three"
9
+ *
10
+ * const wardic = WardicThree.make()
11
+ * renderer.setAnimationLoop((time) => {
12
+ * wardic.frame(time)
13
+ * renderer.render(scene, camera)
14
+ * })
15
+ * wardic.animate(cube, { y: 2, rotateY: 180 })
16
+ *
17
+ * @module
18
+ */
19
+ // #883 (ADR-0068, ADR-0069, ADR-0073): shipped from 1.3 (#564), with `@wardic/object`.
20
+ export * as TestStage from "./TestStage.ts"
21
+ export * as ThreeColours from "./ThreeColours.ts"
22
+ export * as ThreeInput from "./ThreeInput.ts"
23
+ export * as ThreeInstances from "./ThreeInstances.ts"
24
+ export * as ThreeRenderer from "./ThreeRenderer.ts"
25
+ export * as ThreeValues from "./ThreeValues.ts"
26
+ export * as WardicThree from "./WardicThree.ts"
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Development: off. What a build that doesn't resolve the `development` export condition gets for
3
+ * `@wardic/three/internal/dev` (#883, Q6): `dev.ts` is the other half.
4
+ *
5
+ * @module
6
+ */
7
+ export const DEV = false
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Development: on. The package's `development` export condition resolves this module, as Foldkit's
3
+ * `DEV` is (#791, Q10); a production build resolves `dev.production.ts`, so a check under `DEV` and
4
+ * what it calls are dropped from it. Tested in a plain `if` or `&&`, never through `Bool`, as a
5
+ * build-time flag is (`docs/agents/effect-first.md`, *Pitfalls*).
6
+ *
7
+ * @module
8
+ */
9
+ // #883 (ADR-0073, *Amendment (#883)*, Q6): the shared-material warning's gate.
10
+ export const DEV = true