@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
package/src/ThreeDrag.ts
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drag in 3D (ADR-0070 decision 9, kept by ADR-0073 decision 18): a pointer moves in 2D, so each of its
|
|
3
|
+
* moves is cast into the scene, and the object moves with what it meets.
|
|
4
|
+
*
|
|
5
|
+
* - **`drag: true`** moves the object on the plane through the point grabbed, facing the camera, as
|
|
6
|
+
* three's `DragControls` and drei's do.
|
|
7
|
+
* - **`drag: "x" | "y" | "z"`** moves it along that axis of its parent's space, matching the `x`, `y`
|
|
8
|
+
* and `z` keys.
|
|
9
|
+
* - **`dragPlane`** drags it on a fixed plane, in world space: the ground.
|
|
10
|
+
* - **`dragConstraints`**, `{ x: [-2, 2], z: [-2, 2] }`, are its ranges per axis in its parent's space,
|
|
11
|
+
* which `dragElastic` lets it be pulled past.
|
|
12
|
+
* - **Its release** glides on at the pointer's speed, cast into the scene the same way, in world units a
|
|
13
|
+
* second, on Motion's inertia per axis (`dragMomentum`, `dragTransition`), back within its ranges,
|
|
14
|
+
* or back where it was picked up from (`dragSnapToOrigin`). Its rest thresholds are Motion's, in the
|
|
15
|
+
* scene's units through its unit scale.
|
|
16
|
+
*
|
|
17
|
+
* Its moves are written through `animate`, a 0 s change each: a ward on the object takes them as the
|
|
18
|
+
* Stage's `animate` (#433), holding `x`, `y` and `z` where the drag leaves them until its own target
|
|
19
|
+
* for one changes. `@wardic/three/solid`'s Stage drags with it.
|
|
20
|
+
*
|
|
21
|
+
* @module
|
|
22
|
+
*/
|
|
23
|
+
// #885's PR 4 (ADR-0073, *Amendment (#885)*, Q8).
|
|
24
|
+
import * as Drag from "@wardic/core/Drag"
|
|
25
|
+
import * as DragRecognizer from "@wardic/core/DragRecognizer"
|
|
26
|
+
import type * as InputFeed from "@wardic/core/InputFeed"
|
|
27
|
+
import type * as InputSource from "@wardic/core/InputSource"
|
|
28
|
+
import type * as Point from "@wardic/core/Point"
|
|
29
|
+
import type * as UnitScale from "@wardic/core/UnitScale"
|
|
30
|
+
import type * as Config from "@wardic/core/unstable/Config"
|
|
31
|
+
import type * as Imperative from "@wardic/core/unstable/Imperative"
|
|
32
|
+
import * as Arr from "effect/Array"
|
|
33
|
+
import * as Bool from "effect/Boolean"
|
|
34
|
+
import * as Effect from "effect/Effect"
|
|
35
|
+
import * as Option from "effect/Option"
|
|
36
|
+
import * as Predicate from "effect/Predicate"
|
|
37
|
+
import type * as Scope from "effect/Scope"
|
|
38
|
+
import * as UndefinedOr from "effect/UndefinedOr"
|
|
39
|
+
import { type Camera, type Object3D, Plane, type Ray, Raycaster, Vector2, Vector3 } from "three"
|
|
40
|
+
|
|
41
|
+
/** An axis of an object's parent's space. */
|
|
42
|
+
export type DragAxis = "x" | "y" | "z"
|
|
43
|
+
|
|
44
|
+
/** A range on one axis: its least, then its most. */
|
|
45
|
+
export type DragRange = readonly [min: number, max: number]
|
|
46
|
+
|
|
47
|
+
/** A release's inertia, as `type: "inertia"`'s: Motion's `dragTransition`. */
|
|
48
|
+
export type DragTransition = Omit<Config.WardicInertia, "type" | "velocity" | "min" | "max">
|
|
49
|
+
|
|
50
|
+
/** A drag's config, read at each gesture's start. */
|
|
51
|
+
export interface DragConfig {
|
|
52
|
+
readonly drag?: boolean | DragAxis | undefined
|
|
53
|
+
readonly dragPlane?: Plane | undefined
|
|
54
|
+
readonly dragConstraints?: Partial<Readonly<Record<DragAxis, DragRange>>> | undefined
|
|
55
|
+
readonly dragElastic?: number | undefined
|
|
56
|
+
readonly dragMomentum?: boolean | undefined
|
|
57
|
+
readonly dragTransition?: DragTransition | undefined
|
|
58
|
+
readonly dragSnapToOrigin?: boolean | undefined
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** What a drag tells: each pointer sample, after the object has moved, and its release coming to rest. */
|
|
62
|
+
export interface DragHandlers {
|
|
63
|
+
readonly sample: (sample: Drag.Sample) => void
|
|
64
|
+
readonly released: () => void
|
|
65
|
+
readonly onDefect?: InputFeed.OnDefect | undefined
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** A canvas, as the drag reads it: where it is on the page. */
|
|
69
|
+
export interface CanvasRect {
|
|
70
|
+
readonly getBoundingClientRect: () => { readonly left: number; readonly top: number; readonly width: number; readonly height: number }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** What a scene's drags run on: its `animate`, its camera, its canvas, and its unit scale. */
|
|
74
|
+
export interface Options {
|
|
75
|
+
readonly animate: (subject: object, target: Readonly<Record<string, unknown>>, options?: Imperative.WardicAnimateOptions) => Imperative.WardicPlaybackControls
|
|
76
|
+
/** The camera it's drawn with, or a function giving the one active now. */
|
|
77
|
+
readonly camera: Camera | (() => Camera)
|
|
78
|
+
/** Its canvas, once there is one. */
|
|
79
|
+
readonly canvas: () => CanvasRect | undefined
|
|
80
|
+
/** How many of Motion's units a key counts as, for the release's rest thresholds. */
|
|
81
|
+
readonly unitScale: UnitScale.Rule
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** A scene's drags. */
|
|
85
|
+
export interface ThreeDrag {
|
|
86
|
+
/** Watch `subject`'s drags on the scene's input: `config()` read at each gesture's start. */
|
|
87
|
+
readonly watch: (subject: object, config: () => DragConfig, handlers: DragHandlers) => Effect.Effect<void, never, Scope.Scope | InputSource.InputSource>
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** How far an object is pulled past a range by default: Motion's. */
|
|
91
|
+
export const DEFAULT_ELASTIC = 0.35
|
|
92
|
+
|
|
93
|
+
const AXES: ReadonlyArray<DragAxis> = ["x", "y", "z"]
|
|
94
|
+
|
|
95
|
+
/** Each axis's unit vector. */
|
|
96
|
+
const UNIT: Readonly<Record<DragAxis, Vector3>> = { x: new Vector3(1, 0, 0), y: new Vector3(0, 1, 0), z: new Vector3(0, 0, 1) }
|
|
97
|
+
|
|
98
|
+
/** Motion's release: a 750 ms time constant, power 0.8, at rest within 1 of its units and below 10 a second. */
|
|
99
|
+
const RELEASE = { power: 0.8, timeConstant: 750 } as const
|
|
100
|
+
const REST_DELTA = 1
|
|
101
|
+
const REST_SPEED = 10
|
|
102
|
+
/** Its bounce back within a range: soft with an elastic edge, at once with none. Motion's. */
|
|
103
|
+
const ELASTIC_BOUNCE = { bounceStiffness: 200, bounceDamping: 40 } as const
|
|
104
|
+
const RIGID_BOUNCE = { bounceStiffness: 1e6, bounceDamping: 1e7 } as const
|
|
105
|
+
|
|
106
|
+
/** A 1/60 s step back along the pointer's velocity: where a release's speed is cast from. */
|
|
107
|
+
const STEP = 1 / 60
|
|
108
|
+
|
|
109
|
+
/** A drag's moves, written at once. */
|
|
110
|
+
const AT_ONCE: Imperative.WardicAnimateOptions = { duration: 0 }
|
|
111
|
+
|
|
112
|
+
// Module state: scratch for the casts, each used and read before the next.
|
|
113
|
+
const raycaster = new Raycaster()
|
|
114
|
+
const ndc = new Vector2()
|
|
115
|
+
|
|
116
|
+
/** A gesture: what it moves, on what, from where. */
|
|
117
|
+
interface Grab {
|
|
118
|
+
readonly object: Object3D
|
|
119
|
+
readonly config: DragConfig
|
|
120
|
+
/** The keys it moves: one for an axis drag, else all three. */
|
|
121
|
+
readonly keys: ReadonlyArray<DragAxis>
|
|
122
|
+
/** What the pointer's ray meets, in world space. */
|
|
123
|
+
readonly plane: Plane
|
|
124
|
+
/** An axis drag's axis, in world space: what's met on the plane is taken to the line along it. */
|
|
125
|
+
readonly line: Vector3 | undefined
|
|
126
|
+
/** Where the plane was met at the down, in world space. */
|
|
127
|
+
readonly grabbed: Vector3
|
|
128
|
+
/** The object's world position at the down. */
|
|
129
|
+
readonly from: Vector3
|
|
130
|
+
/** Its position at the down, in its parent's space. */
|
|
131
|
+
readonly start: Readonly<Record<DragAxis, number>>
|
|
132
|
+
/** Where it is now, in its parent's space. */
|
|
133
|
+
readonly now: Record<DragAxis, number>
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** The camera now: one given, or the one a function gives. */
|
|
137
|
+
const cameraOf = (camera: Options["camera"]): Camera => {
|
|
138
|
+
if (Predicate.isFunction(camera)) return (camera as () => Camera)()
|
|
139
|
+
return camera
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** The pointer's ray through `point` on the page, from the camera. */
|
|
143
|
+
const rayAt = (point: Point.Point, rect: ReturnType<CanvasRect["getBoundingClientRect"]>, camera: Camera): Ray => {
|
|
144
|
+
ndc.set(((point.x - rect.left) / rect.width) * 2 - 1, -((point.y - rect.top) / rect.height) * 2 + 1)
|
|
145
|
+
raycaster.setFromCamera(ndc, camera)
|
|
146
|
+
return raycaster.ray
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Where `ray` meets the drag: on its plane, and for an axis drag on its line; none where the ray runs along the plane. */
|
|
150
|
+
const meet = (plane: Plane, line: Vector3 | undefined, through: Vector3, ray: Ray, out: Vector3): Vector3 | undefined => {
|
|
151
|
+
if (Predicate.isNull(ray.intersectPlane(plane, out))) return undefined
|
|
152
|
+
return UndefinedOr.match(line, {
|
|
153
|
+
onUndefined: () => out,
|
|
154
|
+
onDefined: (along) => {
|
|
155
|
+
const d = out.sub(through).dot(along)
|
|
156
|
+
return out.copy(through).addScaledVector(along, d)
|
|
157
|
+
},
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* The plane an axis drag moves on: through `through`, holding the axis `line`, and facing the camera as
|
|
163
|
+
* nearly as one can. Looking down the axis, one holding it at all.
|
|
164
|
+
*/
|
|
165
|
+
const axisPlane = (line: Vector3, through: Vector3, camera: Camera): Plane => {
|
|
166
|
+
const view = camera.getWorldDirection(new Vector3())
|
|
167
|
+
const normal = view.addScaledVector(line, -view.dot(line))
|
|
168
|
+
if (normal.lengthSq() < 1e-12) normal.crossVectors(line, Bool.match(Math.abs(line.y) < 0.9, { onTrue: () => UNIT.y, onFalse: () => UNIT.x }))
|
|
169
|
+
return new Plane().setFromNormalAndCoplanarPoint(normal.normalize(), through)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** `object`'s parent: none at the root. */
|
|
173
|
+
const parentOf = (object: Object3D): Object3D | undefined => Option.getOrUndefined(Option.fromNullishOr(object.parent))
|
|
174
|
+
|
|
175
|
+
/** `world`, a point, in `object`'s parent's space: as it is at the root. */
|
|
176
|
+
const toParent = (object: Object3D, world: Vector3): Vector3 =>
|
|
177
|
+
UndefinedOr.match(parentOf(object), { onUndefined: () => world, onDefined: (parent) => parent.worldToLocal(world) })
|
|
178
|
+
|
|
179
|
+
/** `config`'s range on `axis`, as core's: none without one. */
|
|
180
|
+
const rangeOf = (config: DragConfig, axis: DragAxis): Drag.AxisRange | undefined => {
|
|
181
|
+
const constraints = config.dragConstraints
|
|
182
|
+
if (Predicate.isUndefined(constraints)) return undefined
|
|
183
|
+
return UndefinedOr.map(constraints[axis], ([min, max]): Drag.AxisRange => ({ min, max }))
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** The object's place, its keys' values. */
|
|
187
|
+
const valuesOf = (grab: Grab): Readonly<Record<string, number>> => {
|
|
188
|
+
const values: Record<string, number> = {}
|
|
189
|
+
Arr.forEach(grab.keys, (key) => {
|
|
190
|
+
values[key] = grab.now[key]
|
|
191
|
+
})
|
|
192
|
+
return values
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** A scene's drags on `options`' camera and canvas, moving each object through `options.animate`. */
|
|
196
|
+
export const make = (options: Options): ThreeDrag => {
|
|
197
|
+
/** Its canvas's place on the page now, once there's a canvas. */
|
|
198
|
+
const rectNow = (): ReturnType<CanvasRect["getBoundingClientRect"]> | undefined => UndefinedOr.map(options.canvas(), (canvas) => canvas.getBoundingClientRect())
|
|
199
|
+
|
|
200
|
+
/** A gesture's start: where the pointer went down on the object, and what it moves on from there. */
|
|
201
|
+
const begin = (object: Object3D, config: DragConfig, sample: Drag.Sample): Grab | undefined => {
|
|
202
|
+
const rect = rectNow()
|
|
203
|
+
if (Predicate.isUndefined(rect)) return undefined
|
|
204
|
+
const camera = cameraOf(options.camera)
|
|
205
|
+
object.updateWorldMatrix(true, false)
|
|
206
|
+
const down = { x: sample.point.x - sample.offset.x, y: sample.point.y - sample.offset.y }
|
|
207
|
+
const ray = rayAt(down, rect, camera)
|
|
208
|
+
const from = object.getWorldPosition(new Vector3())
|
|
209
|
+
// Where it was grabbed: where the pointer's ray met it, else its middle.
|
|
210
|
+
const through = Option.match(Arr.head(raycaster.intersectObject(object, true)), {
|
|
211
|
+
onNone: () => from.clone(),
|
|
212
|
+
onSome: (hit) => hit.point.clone(),
|
|
213
|
+
})
|
|
214
|
+
const axis = Option.getOrUndefined(Arr.findFirst(AXES, (one) => one === config.drag))
|
|
215
|
+
const line = UndefinedOr.map(axis, (one) =>
|
|
216
|
+
UndefinedOr.match(parentOf(object), {
|
|
217
|
+
onUndefined: () => UNIT[one].clone(),
|
|
218
|
+
onDefined: (parent) => UNIT[one].clone().transformDirection(parent.matrixWorld),
|
|
219
|
+
}),
|
|
220
|
+
)
|
|
221
|
+
const plane = UndefinedOr.match(config.dragPlane, {
|
|
222
|
+
onDefined: (given) => given,
|
|
223
|
+
onUndefined: () =>
|
|
224
|
+
UndefinedOr.match(line, {
|
|
225
|
+
onDefined: (along) => axisPlane(along, through, camera),
|
|
226
|
+
onUndefined: () => new Plane().setFromNormalAndCoplanarPoint(camera.getWorldDirection(new Vector3()), through),
|
|
227
|
+
}),
|
|
228
|
+
})
|
|
229
|
+
return UndefinedOr.map(meet(plane, line, through, ray, new Vector3()), (grabbed) => {
|
|
230
|
+
const start = { x: object.position.x, y: object.position.y, z: object.position.z }
|
|
231
|
+
const keys: ReadonlyArray<DragAxis> = UndefinedOr.match(axis, { onUndefined: () => AXES, onDefined: (one) => [one] })
|
|
232
|
+
return { object, config, keys, plane, line, grabbed, from, start, now: { ...start } }
|
|
233
|
+
})
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Scratch for a move's cast: each used and read before the next.
|
|
237
|
+
const met = new Vector3()
|
|
238
|
+
const world = new Vector3()
|
|
239
|
+
|
|
240
|
+
/** The object where the pointer at `point` has it, within its ranges' give, written. Whether the plane was met. */
|
|
241
|
+
const moveTo = (grab: Grab, point: Point.Point): boolean =>
|
|
242
|
+
UndefinedOr.match(rectNow(), {
|
|
243
|
+
onUndefined: () => false,
|
|
244
|
+
onDefined: (rect) =>
|
|
245
|
+
UndefinedOr.match(meet(grab.plane, grab.line, grab.grabbed, rayAt(point, rect, cameraOf(options.camera)), met), {
|
|
246
|
+
onUndefined: () => false,
|
|
247
|
+
onDefined: (hit) => {
|
|
248
|
+
const local = toParent(grab.object, world.copy(grab.from).add(hit).sub(grab.grabbed))
|
|
249
|
+
const elastic = UndefinedOr.match(grab.config.dragElastic, { onUndefined: () => DEFAULT_ELASTIC, onDefined: (given) => given })
|
|
250
|
+
Arr.forEach(grab.keys, (key) => {
|
|
251
|
+
grab.now[key] = Drag.constrain(local[key], rangeOf(grab.config, key), elastic)
|
|
252
|
+
})
|
|
253
|
+
void options.animate(grab.object, valuesOf(grab), AT_ONCE)
|
|
254
|
+
return true
|
|
255
|
+
},
|
|
256
|
+
}),
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
/** The pointer's velocity at its release, cast into the scene: in its parent's space's units a second. */
|
|
260
|
+
const velocityOf = (grab: Grab, sample: Drag.Sample): Readonly<Record<DragAxis, number>> => {
|
|
261
|
+
const none = { x: 0, y: 0, z: 0 }
|
|
262
|
+
if (Bool.and(sample.velocity.x === 0, sample.velocity.y === 0)) return none
|
|
263
|
+
return UndefinedOr.match(rectNow(), {
|
|
264
|
+
onUndefined: () => none,
|
|
265
|
+
onDefined: (rect) => {
|
|
266
|
+
const camera = cameraOf(options.camera)
|
|
267
|
+
const back = { x: sample.point.x - sample.velocity.x * STEP, y: sample.point.y - sample.velocity.y * STEP }
|
|
268
|
+
const here = meet(grab.plane, grab.line, grab.grabbed, rayAt(sample.point, rect, camera), new Vector3())
|
|
269
|
+
const there = meet(grab.plane, grab.line, grab.grabbed, rayAt(back, rect, camera), new Vector3())
|
|
270
|
+
if (Bool.or(Predicate.isUndefined(here), Predicate.isUndefined(there))) return none
|
|
271
|
+
const a = toParent(grab.object, (here as Vector3).clone())
|
|
272
|
+
const b = toParent(grab.object, (there as Vector3).clone())
|
|
273
|
+
const out = { ...none }
|
|
274
|
+
Arr.forEach(grab.keys, (key) => {
|
|
275
|
+
out[key] = (a[key] - b[key]) / STEP
|
|
276
|
+
})
|
|
277
|
+
return out
|
|
278
|
+
},
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Its release, Motion's: inertia per key at the release's speed (none with `dragMomentum: false`),
|
|
284
|
+
* bounded by its ranges, or by where it was picked up from with `dragSnapToOrigin`.
|
|
285
|
+
*/
|
|
286
|
+
const release = (grab: Grab, sample: Drag.Sample, handlers: DragHandlers): void => {
|
|
287
|
+
const config = grab.config
|
|
288
|
+
const velocity = Bool.match(config.dragMomentum === false, { onTrue: () => ({ x: 0, y: 0, z: 0 }), onFalse: () => velocityOf(grab, sample) })
|
|
289
|
+
const elastic = UndefinedOr.match(config.dragElastic, { onUndefined: () => DEFAULT_ELASTIC, onDefined: (given) => given })
|
|
290
|
+
const bounce = Bool.match(elastic > 0, { onTrue: () => ELASTIC_BOUNCE, onFalse: () => RIGID_BOUNCE })
|
|
291
|
+
const transition: Record<string, unknown> = {}
|
|
292
|
+
Arr.forEach(grab.keys, (key) => {
|
|
293
|
+
const scale = options.unitScale(key, 1)
|
|
294
|
+
const range = Bool.match(config.dragSnapToOrigin === true, {
|
|
295
|
+
onTrue: (): Drag.AxisRange | undefined => ({ min: grab.start[key], max: grab.start[key] }),
|
|
296
|
+
onFalse: () => rangeOf(config, key),
|
|
297
|
+
})
|
|
298
|
+
transition[key] = {
|
|
299
|
+
type: "inertia",
|
|
300
|
+
velocity: velocity[key],
|
|
301
|
+
...RELEASE,
|
|
302
|
+
restDelta: REST_DELTA / scale,
|
|
303
|
+
restSpeed: REST_SPEED / scale,
|
|
304
|
+
...bounce,
|
|
305
|
+
...range,
|
|
306
|
+
...config.dragTransition,
|
|
307
|
+
}
|
|
308
|
+
})
|
|
309
|
+
void options.animate(grab.object, valuesOf(grab), transition as Imperative.WardicAnimateOptions).then(handlers.released)
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return {
|
|
313
|
+
watch: (subject, config, handlers) => {
|
|
314
|
+
let grab: Grab | undefined
|
|
315
|
+
return DragRecognizer.watchDrag(
|
|
316
|
+
subject,
|
|
317
|
+
{ axis: "xy" },
|
|
318
|
+
{
|
|
319
|
+
on: (sample) => {
|
|
320
|
+
if (sample.phase === "start") {
|
|
321
|
+
grab = begin(subject as Object3D, config(), sample)
|
|
322
|
+
UndefinedOr.map(grab, (g) => moveTo(g, sample.point))
|
|
323
|
+
}
|
|
324
|
+
if (sample.phase === "move") UndefinedOr.map(grab, (g) => moveTo(g, sample.point))
|
|
325
|
+
if (sample.phase === "end") {
|
|
326
|
+
// A cancelled gesture stops where it is: no release, as Motion's `cancel`.
|
|
327
|
+
UndefinedOr.map(grab, (g) => {
|
|
328
|
+
if (sample.cancelled === true) return
|
|
329
|
+
moveTo(g, sample.point)
|
|
330
|
+
release(g, sample, handlers)
|
|
331
|
+
})
|
|
332
|
+
grab = undefined
|
|
333
|
+
}
|
|
334
|
+
handlers.sample(sample)
|
|
335
|
+
},
|
|
336
|
+
onDefect: handlers.onDefect,
|
|
337
|
+
},
|
|
338
|
+
).pipe(Effect.asVoid)
|
|
339
|
+
},
|
|
340
|
+
}
|
|
341
|
+
}
|