@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 @@
|
|
|
1
|
+
{"version":3,"file":"solid.js","sourceRoot":"","sources":["../src/solid.ts"],"names":[],"mappings":"AA4EA,2GAA2G;AAC3G,yGAAyG;AACzG,OAAO,EACL,kBAAkB,IAAI,WAAW,EACjC,iBAAiB,IAAI,UAAU,EAC/B,iBAAiB,EACjB,kBAAkB,IAAI,WAAW,EACjC,kBAAkB,IAAI,WAAW,EACjC,WAAW,IAAI,IAAI,EACnB,WAAW,GACZ,MAAM,eAAe,CAAA;AAEtB,OAAO,EAAE,iBAAiB,EAA4C,MAAM,yBAAyB,CAAA;AACrG,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACjD,OAAO,EAAqB,WAAW,EAAE,cAAc,EAAyD,MAAM,kBAAkB,CAAA;AACxI,OAAO,KAAK,SAAS,MAAM,uBAAuB,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,101 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wardic/three",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "Wardic for three.js: Motion's animate on a scene's meshes, materials, cameras and uniforms, with three's own types, on frames the Host loop pumps.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"animation",
|
|
7
|
+
"motion",
|
|
8
|
+
"spring",
|
|
9
|
+
"effect",
|
|
10
|
+
"three",
|
|
11
|
+
"threejs",
|
|
12
|
+
"webgl",
|
|
13
|
+
"webgpu"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://wardic.dev",
|
|
16
|
+
"bugs": "https://github.com/wardiclabs/wardic/issues",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/wardiclabs/wardic.git",
|
|
20
|
+
"directory": "packages/three"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": {
|
|
24
|
+
"name": "The Wardic contributors",
|
|
25
|
+
"email": "hello@wardic.dev",
|
|
26
|
+
"url": "https://wardic.dev"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist/**/*.js",
|
|
30
|
+
"dist/**/*.d.ts",
|
|
31
|
+
"dist/**/*.map",
|
|
32
|
+
"src",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE",
|
|
35
|
+
"THIRD_PARTY_NOTICES.md"
|
|
36
|
+
],
|
|
37
|
+
"type": "module",
|
|
38
|
+
"sideEffects": false,
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"exports": {
|
|
43
|
+
"./package.json": "./package.json",
|
|
44
|
+
".": {
|
|
45
|
+
"@wardic/source": "./src/index.ts",
|
|
46
|
+
"types": "./dist/index.d.ts",
|
|
47
|
+
"default": "./dist/index.js"
|
|
48
|
+
},
|
|
49
|
+
"./internal/dev": {
|
|
50
|
+
"development": {
|
|
51
|
+
"@wardic/source": "./src/internal/dev.ts",
|
|
52
|
+
"types": "./dist/internal/dev.d.ts",
|
|
53
|
+
"default": "./dist/internal/dev.js"
|
|
54
|
+
},
|
|
55
|
+
"@wardic/source": "./src/internal/dev.production.ts",
|
|
56
|
+
"types": "./dist/internal/dev.production.d.ts",
|
|
57
|
+
"default": "./dist/internal/dev.production.js"
|
|
58
|
+
},
|
|
59
|
+
"./*": {
|
|
60
|
+
"@wardic/source": "./src/*.ts",
|
|
61
|
+
"types": "./dist/*.d.ts",
|
|
62
|
+
"default": "./dist/*.js"
|
|
63
|
+
},
|
|
64
|
+
"./internal/*": null
|
|
65
|
+
},
|
|
66
|
+
"scripts": {
|
|
67
|
+
"build": "../../node_modules/typescript-7/bin/tsc --build",
|
|
68
|
+
"typecheck": "../../node_modules/typescript-7/bin/tsc --build",
|
|
69
|
+
"typecheck:tests": "../../node_modules/typescript-7/bin/tsc -p tsconfig.test.json"
|
|
70
|
+
},
|
|
71
|
+
"dependencies": {
|
|
72
|
+
"@wardic/core": "1.3.0",
|
|
73
|
+
"@wardic/object": "1.3.0"
|
|
74
|
+
},
|
|
75
|
+
"peerDependencies": {
|
|
76
|
+
"@types/three": ">=0.160.0",
|
|
77
|
+
"effect": "^4.1.4",
|
|
78
|
+
"three": ">=0.160.0",
|
|
79
|
+
"@wardic/solid": "^2.1.0",
|
|
80
|
+
"@solidjs/web": ">=2.0.1",
|
|
81
|
+
"solid-js": ">=2.0.1"
|
|
82
|
+
},
|
|
83
|
+
"peerDependenciesMeta": {
|
|
84
|
+
"@wardic/solid": {
|
|
85
|
+
"optional": true
|
|
86
|
+
},
|
|
87
|
+
"@solidjs/web": {
|
|
88
|
+
"optional": true
|
|
89
|
+
},
|
|
90
|
+
"solid-js": {
|
|
91
|
+
"optional": true
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
"devDependencies": {
|
|
95
|
+
"@solidjs/web": "2.0.1",
|
|
96
|
+
"@types/three": "0.187.0",
|
|
97
|
+
"@wardic/solid": "2.1.0",
|
|
98
|
+
"solid-js": "2.0.1",
|
|
99
|
+
"three": "0.187.0"
|
|
100
|
+
}
|
|
101
|
+
}
|
package/src/TestStage.ts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A scene's runtime for tests, with no renderer and no page: `WardicThree.make` on a stand-in canvas,
|
|
3
|
+
* its frames pumped by `frame()`, and a pointer that hovers and presses objects through the scene's own
|
|
4
|
+
* input, hit by three's real raycast. It runs in Node.
|
|
5
|
+
*
|
|
6
|
+
* - **`frame(ms?)`** runs one frame of the Host loop, 1/60 s on by default: Wardic's raycast and writes
|
|
7
|
+
* against the scene as it's built, as though the page had drawn it, then its matrices made current, as a
|
|
8
|
+
* render makes them.
|
|
9
|
+
* - **`pointer`** moves a mouse over the stand-in canvas: `over(mesh)` to where `mesh` is drawn, `at(x, y)`,
|
|
10
|
+
* `down()`, `up()`, `cancel()` and `leave()`. A frame then finds what's under it, as on a page.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* import * as TestStage from "@wardic/three/TestStage"
|
|
14
|
+
*
|
|
15
|
+
* const stage = TestStage.make({ scene, camera, size: { width: 800, height: 600 } })
|
|
16
|
+
* stage.hover(crate, () => {
|
|
17
|
+
* stage.animate(crate, { scale: 1.1 })
|
|
18
|
+
* return () => stage.animate(crate, { scale: 1 })
|
|
19
|
+
* })
|
|
20
|
+
* stage.pointer.over(crate)
|
|
21
|
+
* stage.frame()
|
|
22
|
+
*/
|
|
23
|
+
// `@wardic/three/TestStage` (#883's PR 5a; ADR-0073 decision 26, amended by *Amendment (#883)*, PR 5's Q2
|
|
24
|
+
// and Q6): it stays in `@wardic/three` at 1.3 (ADR-0073, *1.3's release*), beside the runtime it drives,
|
|
25
|
+
// so `@wardic/test` takes no three.js peers. `stage.ward` and `<WardicStage value={stage}>` came with
|
|
26
|
+
// steps 3 and 4.
|
|
27
|
+
import * as Predicate from "effect/Predicate"
|
|
28
|
+
import * as Rec from "effect/Record"
|
|
29
|
+
import type { Camera, Object3D } from "three"
|
|
30
|
+
import { Matrix4, Vector3 } from "three"
|
|
31
|
+
import * as ThreeInstances from "./ThreeInstances.ts"
|
|
32
|
+
import * as WardicThree from "./WardicThree.ts"
|
|
33
|
+
import * as ObjectBatch from "@wardic/object/ObjectBatch"
|
|
34
|
+
|
|
35
|
+
/** `make`'s options: the scene, its camera, the stand-in canvas's size, and `WardicThree.make`'s. */
|
|
36
|
+
export interface Options extends Omit<WardicThree.Options, "canvas" | "camera" | "scene" | "window" | "now"> {
|
|
37
|
+
readonly scene: Object3D
|
|
38
|
+
readonly camera: Camera
|
|
39
|
+
/** The stand-in canvas's size, in CSS pixels, at the page's top left. */
|
|
40
|
+
readonly size: { readonly width: number; readonly height: number }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A mouse over the stand-in canvas. */
|
|
44
|
+
export interface TestPointer {
|
|
45
|
+
/** Moves it to where `target` is drawn: a scene object's middle, a Batch member's, an instance's. */
|
|
46
|
+
readonly over: (target: object) => void
|
|
47
|
+
/** Moves it to `x`, `y` on the canvas, in CSS pixels. */
|
|
48
|
+
readonly at: (x: number, y: number) => void
|
|
49
|
+
/** Presses its main button where it is. */
|
|
50
|
+
readonly down: () => void
|
|
51
|
+
/** Releases it where it is. */
|
|
52
|
+
readonly up: () => void
|
|
53
|
+
/** The platform cancels its press. */
|
|
54
|
+
readonly cancel: () => void
|
|
55
|
+
/** Takes it off the canvas. */
|
|
56
|
+
readonly leave: () => void
|
|
57
|
+
/** Where it is now, in CSS pixels. */
|
|
58
|
+
readonly position: () => { readonly x: number; readonly y: number }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** A test's scene runtime: `WardicThree`'s, its frames pumped by `frame`, with a pointer. */
|
|
62
|
+
export interface TestStage extends Omit<WardicThree.WardicThree, "frame"> {
|
|
63
|
+
/** One frame of the Host loop, `ms` after the last (1/60 s): Wardic's frame on the scene as it's built, then its matrices, as a render makes them. */
|
|
64
|
+
readonly frame: (ms?: number) => void
|
|
65
|
+
/** The stage's time now, in ms. */
|
|
66
|
+
readonly now: () => number
|
|
67
|
+
readonly pointer: TestPointer
|
|
68
|
+
/** The stand-in canvas the pointer's events reach. */
|
|
69
|
+
readonly canvas: EventTarget
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** A pointer event as the stand-in dispatches it: an `Event` with a `PointerEvent`'s fields. */
|
|
73
|
+
const pointerEvent = (type: string, fields: Readonly<Record<string, number | string | boolean>>): Event =>
|
|
74
|
+
Object.defineProperties(new Event(type), Rec.map(fields, (value) => ({ value })))
|
|
75
|
+
|
|
76
|
+
/** Where `target` is drawn, in world space: an instance's geometry's middle through its matrix, an object's through its own. */
|
|
77
|
+
const worldMiddleOf = (target: object): Vector3 => {
|
|
78
|
+
const middle = new Vector3()
|
|
79
|
+
const subject = memberOf(target)
|
|
80
|
+
if (ThreeInstances.isInstance(subject)) {
|
|
81
|
+
const mesh = subject.mesh as unknown as { geometry: { boundingSphere: { center: Vector3 } | null; computeBoundingSphere: () => void }; getMatrixAt: (i: number, m: Matrix4) => Matrix4; matrixWorld: Matrix4 }
|
|
82
|
+
if (Predicate.isNull(mesh.geometry.boundingSphere)) mesh.geometry.computeBoundingSphere()
|
|
83
|
+
middle.copy((mesh.geometry.boundingSphere as { center: Vector3 }).center)
|
|
84
|
+
return middle.applyMatrix4(mesh.getMatrixAt(subject.index, new Matrix4())).applyMatrix4(mesh.matrixWorld)
|
|
85
|
+
}
|
|
86
|
+
const object = subject as Object3D & { geometry?: { boundingSphere: { center: Vector3 } | null; computeBoundingSphere: () => void } }
|
|
87
|
+
const geometry = object.geometry
|
|
88
|
+
if (Predicate.isUndefined(geometry)) return object.getWorldPosition(middle)
|
|
89
|
+
if (Predicate.isNull(geometry.boundingSphere)) geometry.computeBoundingSphere()
|
|
90
|
+
return object.localToWorld(middle.copy((geometry.boundingSphere as { center: Vector3 }).center))
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** A Batch member's handle as its member: a mesh, or an instance; anything else as it is. */
|
|
94
|
+
const memberOf = (target: object): object => {
|
|
95
|
+
if (Predicate.isUndefined(ObjectBatch.entryOf(target))) return target
|
|
96
|
+
const handle = target as { readonly i?: number; readonly batch?: { readonly members: ReadonlyArray<object> } }
|
|
97
|
+
if (Predicate.isNumber(handle.i) && Predicate.isNotUndefined(handle.batch)) return handle.batch.members[handle.i] as object
|
|
98
|
+
throw new Error("TestStage: pointer.over takes one object, a Batch's member (batch.at(i)) or an instance, not a whole Batch.")
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** A scene runtime for a test, on a stand-in canvas of `options.size`. */
|
|
102
|
+
export const make = (options: Options): TestStage => {
|
|
103
|
+
const { width, height } = options.size
|
|
104
|
+
const canvas = new EventTarget() as EventTarget & { getBoundingClientRect: () => { left: number; top: number; width: number; height: number } }
|
|
105
|
+
canvas.getBoundingClientRect = () => ({ left: 0, top: 0, width, height })
|
|
106
|
+
// The page's window, where a held pointer's events are heard.
|
|
107
|
+
const page = new EventTarget()
|
|
108
|
+
let time = 0
|
|
109
|
+
const runtime = WardicThree.make({ ...options, canvas, window: page, now: () => time })
|
|
110
|
+
const { scene, camera } = options
|
|
111
|
+
/** The scene's and the camera's matrices, made current as a render makes them. */
|
|
112
|
+
const draw = (): void => {
|
|
113
|
+
scene.updateMatrixWorld()
|
|
114
|
+
camera.updateMatrixWorld()
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let x = 0
|
|
118
|
+
let y = 0
|
|
119
|
+
let over = false
|
|
120
|
+
let buttons = 0
|
|
121
|
+
/** A pointer event at the pointer, reaching the window in its capture phase and then the canvas, as a page's does. */
|
|
122
|
+
const dispatch = (type: string, button: number): void => {
|
|
123
|
+
const event = pointerEvent(type, { clientX: x, clientY: y, pointerId: 1, pointerType: "mouse", isPrimary: true, button, buttons, timeStamp: time })
|
|
124
|
+
if (type !== "pointerenter" && type !== "pointerleave") page.dispatchEvent(event)
|
|
125
|
+
canvas.dispatchEvent(event)
|
|
126
|
+
}
|
|
127
|
+
const moveTo = (nextX: number, nextY: number): void => {
|
|
128
|
+
x = nextX
|
|
129
|
+
y = nextY
|
|
130
|
+
if (!over) {
|
|
131
|
+
over = true
|
|
132
|
+
dispatch("pointerenter", -1)
|
|
133
|
+
}
|
|
134
|
+
dispatch("pointermove", -1)
|
|
135
|
+
}
|
|
136
|
+
const pointer: TestPointer = {
|
|
137
|
+
over: (target) => {
|
|
138
|
+
// Where it's drawn as it's built now.
|
|
139
|
+
draw()
|
|
140
|
+
const ndc = worldMiddleOf(target).project(camera)
|
|
141
|
+
moveTo(((ndc.x + 1) / 2) * width, ((1 - ndc.y) / 2) * height)
|
|
142
|
+
},
|
|
143
|
+
at: moveTo,
|
|
144
|
+
down: () => {
|
|
145
|
+
buttons = 1
|
|
146
|
+
dispatch("pointerdown", 0)
|
|
147
|
+
},
|
|
148
|
+
up: () => {
|
|
149
|
+
buttons = 0
|
|
150
|
+
dispatch("pointerup", 0)
|
|
151
|
+
},
|
|
152
|
+
cancel: () => {
|
|
153
|
+
buttons = 0
|
|
154
|
+
dispatch("pointercancel", 0)
|
|
155
|
+
},
|
|
156
|
+
leave: () => {
|
|
157
|
+
if (!over) return
|
|
158
|
+
over = false
|
|
159
|
+
dispatch("pointerleave", -1)
|
|
160
|
+
},
|
|
161
|
+
position: () => ({ x, y }),
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
...runtime,
|
|
166
|
+
frame: (ms = 1000 / 60) => {
|
|
167
|
+
time += ms
|
|
168
|
+
// What a test built since the last frame, as though the page had drawn it: the raycast hits it there.
|
|
169
|
+
draw()
|
|
170
|
+
runtime.frame(time)
|
|
171
|
+
draw()
|
|
172
|
+
},
|
|
173
|
+
now: () => time,
|
|
174
|
+
pointer,
|
|
175
|
+
canvas,
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A scene's colours, as Wardic animates them: in linear light, three's working space, each as three
|
|
3
|
+
* channel keys. A colour key's target (a CSS colour, a hex number, or a three `Color`, copied) is read
|
|
4
|
+
* once into its linear channels, `color` into `colorR`, `colorG` and `colorB`, which springs move as
|
|
5
|
+
* they move `x`, keeping their speed through a change of target, and which the writer sets on the
|
|
6
|
+
* `Color` itself. Red to blue passes `#bc00bc`, as three's own `Color.lerp` does.
|
|
7
|
+
*
|
|
8
|
+
* A channel rests by its share of the distance it goes, as a blended key's progress does, so a move
|
|
9
|
+
* from black to a near black animates, and every colour lands in the same time.
|
|
10
|
+
*
|
|
11
|
+
* A Batch's colours split the same way: one for every member, two stops, or a function of a member's
|
|
12
|
+
* index, each member's colour read once. An `InstancedMesh`'s instance's `color` is its instance colour.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* import * as ThreeColours from "@wardic/three/ThreeColours"
|
|
16
|
+
*
|
|
17
|
+
* ThreeColours.split(mesh, { color: "#0000ff", opacity: 1 })
|
|
18
|
+
* // { opacity: 1, colorR: 0, colorG: 0, colorB: 1 }
|
|
19
|
+
*/
|
|
20
|
+
// `@wardic/three/ThreeColours` (#883, PR 3; ADR-0069 decision 7; ADR-0073 *Amendment (#883)*, PR 3's
|
|
21
|
+
// Q1–Q3, a Batch's PR 4's Q5): probe `.tmp/883-colour/` measured channel keys keeping a retarget's
|
|
22
|
+
// velocity, where a colour blended on a progress (Motion's) starts from rest. Split at the call, so core's values stay numbers
|
|
23
|
+
// and strings. A target is read by the Subject's own `Color` class: no import of three, and the app's
|
|
24
|
+
// colour management applies.
|
|
25
|
+
import * as UnitScale from "@wardic/core/UnitScale"
|
|
26
|
+
import * as Arr from "effect/Array"
|
|
27
|
+
import * as Predicate from "effect/Predicate"
|
|
28
|
+
import * as Rec from "effect/Record"
|
|
29
|
+
import * as Result from "effect/Result"
|
|
30
|
+
import * as Lookup from "./internal/lookup.ts"
|
|
31
|
+
import * as ThreeInstances from "./ThreeInstances.ts"
|
|
32
|
+
|
|
33
|
+
/** A three `Color`, as Wardic reads one: its linear channels, and its own way to read a colour. */
|
|
34
|
+
interface ColorLike {
|
|
35
|
+
readonly isColor: true
|
|
36
|
+
readonly r: number
|
|
37
|
+
readonly g: number
|
|
38
|
+
readonly b: number
|
|
39
|
+
readonly clone: () => ColorLike
|
|
40
|
+
readonly set: (value: unknown) => ColorLike
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A colour key's value: a CSS colour (`"#00f"`, `"rgb(0 0 255)"`, `"blue"`), a hex number (`0x0000ff`), or a three `Color`, copied. */
|
|
44
|
+
export type ColourValue = string | number | { readonly isColor: true }
|
|
45
|
+
|
|
46
|
+
/** The suffixes of a colour's channel keys: `colorR`, `colorG`, `colorB`. */
|
|
47
|
+
export const CHANNELS = ["R", "G", "B"] as const
|
|
48
|
+
|
|
49
|
+
/** A colour's channel key: a name, then `R`, `G` or `B`. */
|
|
50
|
+
const CHANNEL_KEY = /[a-z0-9][RGB]$/
|
|
51
|
+
|
|
52
|
+
/** Whether `key` names a colour's channel: `colorR`, `emissiveG`, `uTintB`. */
|
|
53
|
+
export const isChannel = (key: string): boolean => CHANNEL_KEY.test(key)
|
|
54
|
+
|
|
55
|
+
/** The least distance a channel's rest counts, in linear light: about a hundredth of an 8-bit step near black. */
|
|
56
|
+
const CHANNEL_FLOOR = 0.001
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A scene's unit scale: `x`, `y` and `z` at `scale` of Motion's units, a colour's channels by their
|
|
60
|
+
* share of the distance, every other key at 1.
|
|
61
|
+
*/
|
|
62
|
+
export const unitScale = (scale: number): UnitScale.Rule => {
|
|
63
|
+
const position = UnitScale.position(scale)
|
|
64
|
+
return (key, distance) => {
|
|
65
|
+
if (isChannel(key)) return UnitScale.relative(distance, CHANNEL_FLOOR)
|
|
66
|
+
return position(key, distance)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** `value`'s linear channels, read by `colour`'s class: `null`, a keyframe's "where it is", for each. */
|
|
71
|
+
const channelsOfStop = (colour: ColorLike, value: unknown): readonly [number | null, number | null, number | null] => {
|
|
72
|
+
if (Predicate.isNull(value)) return [null, null, null]
|
|
73
|
+
const read = colour.clone().set(value)
|
|
74
|
+
return [read.r, read.g, read.b]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A Batch's function value, `(i, n) => …`, as its three channels' functions: each member's colour read once, its channels from it. */
|
|
78
|
+
const channelsOfFunction = (colour: ColorLike, value: (i: number, n: number) => unknown): readonly [unknown, unknown, unknown] => {
|
|
79
|
+
// Called key by key over the members (core's `BatchCalls`): each member's read kept for its other channels.
|
|
80
|
+
const read: Array<readonly [unknown, unknown, unknown]> = []
|
|
81
|
+
const at = (i: number, n: number): readonly [unknown, unknown, unknown] => {
|
|
82
|
+
const known = read[i]
|
|
83
|
+
if (Predicate.isNotUndefined(known)) return known
|
|
84
|
+
const channels = channelsOf(colour, value(i, n))
|
|
85
|
+
read[i] = channels
|
|
86
|
+
return channels
|
|
87
|
+
}
|
|
88
|
+
return [(i: number, n: number) => at(i, n)[0], (i: number, n: number) => at(i, n)[1], (i: number, n: number) => at(i, n)[2]]
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* A colour key's value, as its three channels' values: a stop each, or keyframes of them (a Batch's two
|
|
93
|
+
* stops, a Segment's stops). A Batch's function of a member's index gives each channel a function.
|
|
94
|
+
*/
|
|
95
|
+
const channelsOf = (colour: ColorLike, value: unknown): readonly [unknown, unknown, unknown] => {
|
|
96
|
+
if (Predicate.isFunction(value)) return channelsOfFunction(colour, value as (i: number, n: number) => unknown)
|
|
97
|
+
if (!Arr.isArray(value)) return channelsOfStop(colour, value)
|
|
98
|
+
const stops = Arr.map(value as ReadonlyArray<unknown>, (stop) => channelsOf(colour, stop))
|
|
99
|
+
return [Arr.map(stops, (stop) => stop[0]), Arr.map(stops, (stop) => stop[1]), Arr.map(stops, (stop) => stop[2])]
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The Color `key` names on `subject`, by whose class a colour is read: the lookup's, or, for an
|
|
104
|
+
* `InstancedMesh`'s instance, its material's for `color`, the instance's colour.
|
|
105
|
+
*/
|
|
106
|
+
const colourFor = (subject: object, key: string): ColorLike | undefined => {
|
|
107
|
+
if (ThreeInstances.isInstance(subject)) {
|
|
108
|
+
if (key !== "color") return undefined
|
|
109
|
+
return Lookup.colourOf(subject.mesh, "color") as unknown as ColorLike | undefined
|
|
110
|
+
}
|
|
111
|
+
return Lookup.colourOf(subject, key) as unknown as ColorLike | undefined
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The keys of `target` that name a Color on `subject`, with it. */
|
|
115
|
+
const coloursIn = (subject: object, target: Readonly<Record<string, unknown>>): ReadonlyArray<readonly [string, ColorLike]> =>
|
|
116
|
+
Arr.filterMap(Rec.keys(target), (key) => {
|
|
117
|
+
if (key === "transition" || key === "transitionEnd") return Result.failVoid
|
|
118
|
+
const colour = colourFor(subject, key)
|
|
119
|
+
if (Predicate.isUndefined(colour)) return Result.failVoid
|
|
120
|
+
return Result.succeed([key, colour] as const)
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
/** `values` with each of `colours`' keys given as its three channels' values. */
|
|
124
|
+
const splitValues = (values: Readonly<Record<string, unknown>>, colours: ReadonlyArray<readonly [string, ColorLike]>): Readonly<Record<string, unknown>> => {
|
|
125
|
+
const out: Record<string, unknown> = { ...values }
|
|
126
|
+
Arr.forEach(colours, ([key, colour]) => {
|
|
127
|
+
if (!(key in values)) return
|
|
128
|
+
const channels = channelsOf(colour, values[key])
|
|
129
|
+
delete out[key]
|
|
130
|
+
Arr.forEach(CHANNELS, (channel, i) => {
|
|
131
|
+
out[`${key}${channel}`] = channels[i]
|
|
132
|
+
})
|
|
133
|
+
})
|
|
134
|
+
return out
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** `transition` with each of `colours`' keys' own entry given to its three channels. */
|
|
138
|
+
const splitEntries = (transition: unknown, colours: ReadonlyArray<readonly [string, ColorLike]>): unknown => {
|
|
139
|
+
if (!Predicate.isObject(transition)) return transition
|
|
140
|
+
const entries = transition as Readonly<Record<string, unknown>>
|
|
141
|
+
if (!Arr.some(colours, ([key]) => key in entries)) return transition
|
|
142
|
+
const out: Record<string, unknown> = { ...entries }
|
|
143
|
+
Arr.forEach(colours, ([key]) => {
|
|
144
|
+
if (!(key in entries)) return
|
|
145
|
+
const entry = entries[key]
|
|
146
|
+
delete out[key]
|
|
147
|
+
Arr.forEach(CHANNELS, (channel) => {
|
|
148
|
+
out[`${key}${channel}`] = entry
|
|
149
|
+
})
|
|
150
|
+
})
|
|
151
|
+
return out
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* A call's target and options on `subject`, each colour key split into its channels: its value or
|
|
156
|
+
* keyframes, its `transitionEnd`, and its own entry in the target's `transition` or the options. A
|
|
157
|
+
* target with no colour key comes back as it is. For a Batch, `subject` is a member, and a value may be
|
|
158
|
+
* two stops or a function of a member's index.
|
|
159
|
+
*/
|
|
160
|
+
export const splitCall = (subject: unknown, target: unknown, options: unknown): readonly [target: unknown, options: unknown] => {
|
|
161
|
+
if (!Predicate.isObject(subject) || !Predicate.isObject(target)) return [target, options]
|
|
162
|
+
const given = target as Readonly<Record<string, unknown>>
|
|
163
|
+
const colours = coloursIn(subject, given)
|
|
164
|
+
if (colours.length === 0) return [target, options]
|
|
165
|
+
const split: Record<string, unknown> = { ...splitValues(given, colours) }
|
|
166
|
+
if (Predicate.isObject(given.transitionEnd)) split.transitionEnd = splitValues(given.transitionEnd as Readonly<Record<string, unknown>>, colours)
|
|
167
|
+
if ("transition" in given) split.transition = splitEntries(given.transition, colours)
|
|
168
|
+
return [split, splitEntries(options, colours)]
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** `target` on `subject`, each colour key split into its channels. */
|
|
172
|
+
export const split = (subject: object, target: Readonly<Record<string, unknown>>): Readonly<Record<string, unknown>> => splitCall(subject, target, undefined)[0] as Readonly<Record<string, unknown>>
|
|
173
|
+
|
|
174
|
+
/** A ward's config's keys that hold a target: values, variant names, or an accessor of either. */
|
|
175
|
+
const TARGET_KEYS: ReadonlySet<string> = new Set(["initial", "animate", "exit", "whileHover", "whileTap", "whileDrag", "whileFocus", "whileInView"])
|
|
176
|
+
|
|
177
|
+
/** A target's value on the Subject now, its colour keys split: values split, names and `false` as they are. */
|
|
178
|
+
const splitTargetValue = (subjectNow: () => unknown, value: unknown): unknown => {
|
|
179
|
+
const subject = subjectNow()
|
|
180
|
+
if (!Predicate.isObject(subject) || !Predicate.isObject(value) || Arr.isArray(value)) return value
|
|
181
|
+
return splitCall(subject, value, undefined)[0]
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** `transition` on the Subject now, each colour key's own entry given to its channels. */
|
|
185
|
+
const splitTransition = (subjectNow: () => unknown, transition: unknown): unknown => {
|
|
186
|
+
const subject = subjectNow()
|
|
187
|
+
if (!Predicate.isObject(subject) || !Predicate.isObject(transition)) return transition
|
|
188
|
+
const colours = Arr.filterMap(Rec.keys(transition as Readonly<Record<string, unknown>>), (key) => {
|
|
189
|
+
const colour = colourFor(subject, key)
|
|
190
|
+
if (Predicate.isUndefined(colour)) return Result.failVoid
|
|
191
|
+
return Result.succeed([key, colour] as const)
|
|
192
|
+
})
|
|
193
|
+
if (colours.length === 0) return transition
|
|
194
|
+
return splitEntries(transition, colours)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** `value` through `f`: an accessor's value as it's read, any other's now. */
|
|
198
|
+
const throughAccessor = (value: unknown, f: (given: unknown) => unknown): unknown => {
|
|
199
|
+
if (Predicate.isFunction(value)) return () => f((value as () => unknown)())
|
|
200
|
+
return f(value)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** A variant through `f`: a function's values as it's called, any other's now. */
|
|
204
|
+
const throughVariant = (variant: unknown, f: (given: unknown) => unknown): unknown => {
|
|
205
|
+
if (Predicate.isFunction(variant)) return (...args: ReadonlyArray<unknown>) => f((variant as (...given: ReadonlyArray<unknown>) => unknown)(...args))
|
|
206
|
+
return f(variant)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* A ward's config on a scene's Subject, each colour key split into its channels in linear light,
|
|
211
|
+
* as a call's are (`splitCall`): each target's values, an accessor's as it's read, each variant's (a
|
|
212
|
+
* function's as it's called), and the transition's colour entries. `subjectNow` is the Subject now, read
|
|
213
|
+
* as each is split, so an accessor Subject's colours are its current one's. Every other key is as given.
|
|
214
|
+
*/
|
|
215
|
+
// #885.
|
|
216
|
+
export const splitConfig = (subjectNow: () => unknown, config: Readonly<Record<string, unknown>>): Readonly<Record<string, unknown>> => {
|
|
217
|
+
const out: Record<string, unknown> = { ...config }
|
|
218
|
+
Arr.forEach(Rec.keys(config), (key) => {
|
|
219
|
+
const value = config[key]
|
|
220
|
+
if (TARGET_KEYS.has(key)) {
|
|
221
|
+
out[key] = throughAccessor(value, (given) => splitTargetValue(subjectNow, given))
|
|
222
|
+
return
|
|
223
|
+
}
|
|
224
|
+
if (key === "transition") {
|
|
225
|
+
out[key] = throughAccessor(value, (given) => splitTransition(subjectNow, given))
|
|
226
|
+
return
|
|
227
|
+
}
|
|
228
|
+
if (key !== "variants" || !Predicate.isObject(value)) return
|
|
229
|
+
out[key] = Rec.map(value as Readonly<Record<string, unknown>>, (variant) => throughVariant(variant, (given) => splitTargetValue(subjectNow, given)))
|
|
230
|
+
})
|
|
231
|
+
return out
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Whether `key` is a colour key of a Batch made of `given`: `color` on an `InstancedMesh`'s instances, a Color on a list's first member. */
|
|
235
|
+
const isBatchColour = (given: unknown, key: string): boolean => {
|
|
236
|
+
if (ThreeInstances.isInstancedMesh(given)) return key === "color"
|
|
237
|
+
if (!Arr.isArray(given)) return false
|
|
238
|
+
const first = (given as ReadonlyArray<unknown>)[0]
|
|
239
|
+
return Predicate.isObject(first) && Predicate.isNotUndefined(colourFor(first, key))
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** The `keys` of a Batch made of `given` (an `InstancedMesh`, or a list), each colour key given as its three channels. */
|
|
243
|
+
export const splitBatchKeys = (given: unknown, keys: ReadonlyArray<string>): ReadonlyArray<string> =>
|
|
244
|
+
Arr.flatMap(keys, (key) => {
|
|
245
|
+
if (!isBatchColour(given, key)) return [key]
|
|
246
|
+
return Arr.map(CHANNELS, (channel) => `${key}${channel}`)
|
|
247
|
+
})
|