@motionscript/math 0.0.0-stage → 0.1.0-alpha.4
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/CHANGELOG.md +92 -0
- package/LICENSE +201 -0
- package/NOTICE +48 -0
- package/README.md +385 -3
- package/dist/annotate/angle-mark.d.ts +46 -0
- package/dist/annotate/angle-mark.d.ts.map +1 -0
- package/dist/annotate/angle-mark.js +132 -0
- package/dist/annotate/angle-mark.js.map +1 -0
- package/dist/annotate/curly-brace.d.ts +46 -0
- package/dist/annotate/curly-brace.d.ts.map +1 -0
- package/dist/annotate/curly-brace.js +114 -0
- package/dist/annotate/curly-brace.js.map +1 -0
- package/dist/annotate/emphasis-mark.d.ts +20 -0
- package/dist/annotate/emphasis-mark.d.ts.map +1 -0
- package/dist/annotate/emphasis-mark.js +81 -0
- package/dist/annotate/emphasis-mark.js.map +1 -0
- package/dist/browser/THIRD_PARTY_LICENSES.txt +420 -0
- package/dist/browser/chunks/chunk-3GJB6AVO.js +2 -0
- package/dist/browser/chunks/chunk-3GJB6AVO.js.map +7 -0
- package/dist/browser/chunks/chunk-D5WHCVBC.js +2 -0
- package/dist/browser/chunks/chunk-D5WHCVBC.js.map +7 -0
- package/dist/browser/chunks/chunk-WL4KEX7Y.js +2 -0
- package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +7 -0
- package/dist/browser/chunks/mathjax-DNP3YRES.js +61 -0
- package/dist/browser/chunks/mathjax-DNP3YRES.js.map +7 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/browser/kit.js +2 -0
- package/dist/browser/kit.js.map +7 -0
- package/dist/browser/manifest.json +30 -0
- package/dist/browser/tex.js +2 -0
- package/dist/browser/tex.js.map +7 -0
- package/dist/engine.d.ts +64 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +42 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/kit/brace.d.ts +8 -0
- package/dist/kit/brace.d.ts.map +1 -0
- package/dist/kit/brace.js +36 -0
- package/dist/kit/brace.js.map +1 -0
- package/dist/kit/cache.d.ts +30 -0
- package/dist/kit/cache.d.ts.map +1 -0
- package/dist/kit/cache.js +53 -0
- package/dist/kit/cache.js.map +1 -0
- package/dist/kit/contour.d.ts +8 -0
- package/dist/kit/contour.d.ts.map +1 -0
- package/dist/kit/contour.js +93 -0
- package/dist/kit/contour.js.map +1 -0
- package/dist/kit/coords.d.ts +69 -0
- package/dist/kit/coords.d.ts.map +1 -0
- package/dist/kit/coords.js +224 -0
- package/dist/kit/coords.js.map +1 -0
- package/dist/kit/curve.d.ts +17 -0
- package/dist/kit/curve.d.ts.map +1 -0
- package/dist/kit/curve.js +22 -0
- package/dist/kit/curve.js.map +1 -0
- package/dist/kit/equation.d.ts +61 -0
- package/dist/kit/equation.d.ts.map +1 -0
- package/dist/kit/equation.js +168 -0
- package/dist/kit/equation.js.map +1 -0
- package/dist/kit/equation3d.d.ts +50 -0
- package/dist/kit/equation3d.d.ts.map +1 -0
- package/dist/kit/equation3d.js +103 -0
- package/dist/kit/equation3d.js.map +1 -0
- package/dist/kit/expression.d.ts +35 -0
- package/dist/kit/expression.d.ts.map +1 -0
- package/dist/kit/expression.js +124 -0
- package/dist/kit/expression.js.map +1 -0
- package/dist/kit/field-mark.d.ts +58 -0
- package/dist/kit/field-mark.d.ts.map +1 -0
- package/dist/kit/field-mark.js +129 -0
- package/dist/kit/field-mark.js.map +1 -0
- package/dist/kit/field.d.ts +14 -0
- package/dist/kit/field.d.ts.map +1 -0
- package/dist/kit/field.js +46 -0
- package/dist/kit/field.js.map +1 -0
- package/dist/kit/follow.d.ts +71 -0
- package/dist/kit/follow.d.ts.map +1 -0
- package/dist/kit/follow.js +159 -0
- package/dist/kit/follow.js.map +1 -0
- package/dist/kit/furniture.d.ts +30 -0
- package/dist/kit/furniture.d.ts.map +1 -0
- package/dist/kit/furniture.js +87 -0
- package/dist/kit/furniture.js.map +1 -0
- package/dist/kit/geometry.d.ts +13 -0
- package/dist/kit/geometry.d.ts.map +1 -0
- package/dist/kit/geometry.js +54 -0
- package/dist/kit/geometry.js.map +1 -0
- package/dist/kit/mark.d.ts +47 -0
- package/dist/kit/mark.d.ts.map +1 -0
- package/dist/kit/mark.js +103 -0
- package/dist/kit/mark.js.map +1 -0
- package/dist/kit/part-fills.d.ts +23 -0
- package/dist/kit/part-fills.d.ts.map +1 -0
- package/dist/kit/part-fills.js +46 -0
- package/dist/kit/part-fills.js.map +1 -0
- package/dist/kit/place.d.ts +18 -0
- package/dist/kit/place.d.ts.map +1 -0
- package/dist/kit/place.js +68 -0
- package/dist/kit/place.js.map +1 -0
- package/dist/kit/placement.d.ts +8 -0
- package/dist/kit/placement.d.ts.map +1 -0
- package/dist/kit/placement.js +8 -0
- package/dist/kit/placement.js.map +1 -0
- package/dist/kit/sampler.d.ts +17 -0
- package/dist/kit/sampler.d.ts.map +1 -0
- package/dist/kit/sampler.js +250 -0
- package/dist/kit/sampler.js.map +1 -0
- package/dist/kit/scale.d.ts +12 -0
- package/dist/kit/scale.d.ts.map +1 -0
- package/dist/kit/scale.js +37 -0
- package/dist/kit/scale.js.map +1 -0
- package/dist/kit/source.d.ts +22 -0
- package/dist/kit/source.d.ts.map +1 -0
- package/dist/kit/source.js +45 -0
- package/dist/kit/source.js.map +1 -0
- package/dist/kit/system.d.ts +33 -0
- package/dist/kit/system.d.ts.map +1 -0
- package/dist/kit/system.js +67 -0
- package/dist/kit/system.js.map +1 -0
- package/dist/kit/ticks.d.ts +19 -0
- package/dist/kit/ticks.d.ts.map +1 -0
- package/dist/kit/ticks.js +78 -0
- package/dist/kit/ticks.js.map +1 -0
- package/dist/kit/withdrawn.d.ts +9 -0
- package/dist/kit/withdrawn.d.ts.map +1 -0
- package/dist/kit/withdrawn.js +9 -0
- package/dist/kit/withdrawn.js.map +1 -0
- package/dist/marks/curve-area.d.ts +32 -0
- package/dist/marks/curve-area.d.ts.map +1 -0
- package/dist/marks/curve-area.js +82 -0
- package/dist/marks/curve-area.js.map +1 -0
- package/dist/marks/drop-lines.d.ts +22 -0
- package/dist/marks/drop-lines.d.ts.map +1 -0
- package/dist/marks/drop-lines.js +58 -0
- package/dist/marks/drop-lines.js.map +1 -0
- package/dist/marks/plot-point.d.ts +35 -0
- package/dist/marks/plot-point.d.ts.map +1 -0
- package/dist/marks/plot-point.js +97 -0
- package/dist/marks/plot-point.js.map +1 -0
- package/dist/marks/riemann-sum.d.ts +37 -0
- package/dist/marks/riemann-sum.d.ts.map +1 -0
- package/dist/marks/riemann-sum.js +91 -0
- package/dist/marks/riemann-sum.js.map +1 -0
- package/dist/marks/tangent-line.d.ts +27 -0
- package/dist/marks/tangent-line.d.ts.map +1 -0
- package/dist/marks/tangent-line.js +103 -0
- package/dist/marks/tangent-line.js.map +1 -0
- package/dist/marks/vector-arrow.d.ts +32 -0
- package/dist/marks/vector-arrow.d.ts.map +1 -0
- package/dist/marks/vector-arrow.js +87 -0
- package/dist/marks/vector-arrow.js.map +1 -0
- package/dist/nodes.d.ts +35 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +63 -0
- package/dist/nodes.js.map +1 -0
- package/dist/plots/function-plot.d.ts +67 -0
- package/dist/plots/function-plot.d.ts.map +1 -0
- package/dist/plots/function-plot.js +215 -0
- package/dist/plots/function-plot.js.map +1 -0
- package/dist/plots/points-plot.d.ts +28 -0
- package/dist/plots/points-plot.d.ts.map +1 -0
- package/dist/plots/points-plot.js +99 -0
- package/dist/plots/points-plot.js.map +1 -0
- package/dist/plots/stream-lines.d.ts +35 -0
- package/dist/plots/stream-lines.d.ts.map +1 -0
- package/dist/plots/stream-lines.js +141 -0
- package/dist/plots/stream-lines.js.map +1 -0
- package/dist/plots/traced-path.d.ts +31 -0
- package/dist/plots/traced-path.d.ts.map +1 -0
- package/dist/plots/traced-path.js +56 -0
- package/dist/plots/traced-path.js.map +1 -0
- package/dist/plots/vector-field.d.ts +23 -0
- package/dist/plots/vector-field.d.ts.map +1 -0
- package/dist/plots/vector-field.js +91 -0
- package/dist/plots/vector-field.js.map +1 -0
- package/dist/structures/graph-layout.d.ts +11 -0
- package/dist/structures/graph-layout.d.ts.map +1 -0
- package/dist/structures/graph-layout.js +135 -0
- package/dist/structures/graph-layout.js.map +1 -0
- package/dist/structures/network-graph.d.ts +83 -0
- package/dist/structures/network-graph.d.ts.map +1 -0
- package/dist/structures/network-graph.js +289 -0
- package/dist/structures/network-graph.js.map +1 -0
- package/dist/structures/sample-space.d.ts +62 -0
- package/dist/structures/sample-space.d.ts.map +1 -0
- package/dist/structures/sample-space.js +199 -0
- package/dist/structures/sample-space.js.map +1 -0
- package/dist/systems/axes2d.d.ts +75 -0
- package/dist/systems/axes2d.d.ts.map +1 -0
- package/dist/systems/axes2d.js +193 -0
- package/dist/systems/axes2d.js.map +1 -0
- package/dist/systems/number-line.d.ts +49 -0
- package/dist/systems/number-line.d.ts.map +1 -0
- package/dist/systems/number-line.js +127 -0
- package/dist/systems/number-line.js.map +1 -0
- package/dist/systems/number-plane.d.ts +59 -0
- package/dist/systems/number-plane.d.ts.map +1 -0
- package/dist/systems/number-plane.js +193 -0
- package/dist/systems/number-plane.js.map +1 -0
- package/dist/systems/polar-plane.d.ts +48 -0
- package/dist/systems/polar-plane.d.ts.map +1 -0
- package/dist/systems/polar-plane.js +165 -0
- package/dist/systems/polar-plane.js.map +1 -0
- package/dist/tex/geometry.d.ts +85 -0
- package/dist/tex/geometry.d.ts.map +1 -0
- package/dist/tex/geometry.js +632 -0
- package/dist/tex/geometry.js.map +1 -0
- package/dist/tex/index.d.ts +11 -0
- package/dist/tex/index.d.ts.map +1 -0
- package/dist/tex/index.js +9 -0
- package/dist/tex/index.js.map +1 -0
- package/dist/tex/mathjax.d.ts +33 -0
- package/dist/tex/mathjax.d.ts.map +1 -0
- package/dist/tex/mathjax.js +74 -0
- package/dist/tex/mathjax.js.map +1 -0
- package/dist/tex/matrix.d.ts +31 -0
- package/dist/tex/matrix.d.ts.map +1 -0
- package/dist/tex/matrix.js +73 -0
- package/dist/tex/matrix.js.map +1 -0
- package/dist/tex/node.d.ts +215 -0
- package/dist/tex/node.d.ts.map +1 -0
- package/dist/tex/node.js +595 -0
- package/dist/tex/node.js.map +1 -0
- package/dist/tex/tween.d.ts +50 -0
- package/dist/tex/tween.d.ts.map +1 -0
- package/dist/tex/tween.js +253 -0
- package/dist/tex/tween.js.map +1 -0
- package/dist/three/axes3d.d.ts +87 -0
- package/dist/three/axes3d.d.ts.map +1 -0
- package/dist/three/axes3d.js +228 -0
- package/dist/three/axes3d.js.map +1 -0
- package/dist/three/curve-plot3d.d.ts +60 -0
- package/dist/three/curve-plot3d.d.ts.map +1 -0
- package/dist/three/curve-plot3d.js +143 -0
- package/dist/three/curve-plot3d.js.map +1 -0
- package/dist/three/label3d.d.ts +11 -0
- package/dist/three/label3d.d.ts.map +1 -0
- package/dist/three/label3d.js +40 -0
- package/dist/three/label3d.js.map +1 -0
- package/dist/three/plot-point3d.d.ts +26 -0
- package/dist/three/plot-point3d.d.ts.map +1 -0
- package/dist/three/plot-point3d.js +60 -0
- package/dist/three/plot-point3d.js.map +1 -0
- package/dist/three/space.d.ts +28 -0
- package/dist/three/space.d.ts.map +1 -0
- package/dist/three/space.js +47 -0
- package/dist/three/space.js.map +1 -0
- package/dist/three/surface-plot3d.d.ts +80 -0
- package/dist/three/surface-plot3d.d.ts.map +1 -0
- package/dist/three/surface-plot3d.js +290 -0
- package/dist/three/surface-plot3d.js.map +1 -0
- package/dist/three/vector-arrow3d.d.ts +25 -0
- package/dist/three/vector-arrow3d.d.ts.map +1 -0
- package/dist/three/vector-arrow3d.js +72 -0
- package/dist/three/vector-arrow3d.js.map +1 -0
- package/package.json +79 -3
- package/registry.json +251 -0
- package/src/annotate/angle-mark.ts +147 -0
- package/src/annotate/curly-brace.ts +126 -0
- package/src/annotate/emphasis-mark.ts +89 -0
- package/src/engine.ts +72 -0
- package/src/index.ts +29 -0
- package/src/kit/brace.ts +37 -0
- package/src/kit/cache.ts +61 -0
- package/src/kit/contour.ts +94 -0
- package/src/kit/coords.ts +278 -0
- package/src/kit/curve.ts +29 -0
- package/src/kit/equation.ts +181 -0
- package/src/kit/equation3d.ts +137 -0
- package/src/kit/expression.ts +159 -0
- package/src/kit/field-mark.ts +155 -0
- package/src/kit/field.ts +50 -0
- package/src/kit/follow.ts +203 -0
- package/src/kit/furniture.ts +108 -0
- package/src/kit/geometry.ts +49 -0
- package/src/kit/mark.ts +131 -0
- package/src/kit/part-fills.ts +61 -0
- package/src/kit/place.ts +101 -0
- package/src/kit/placement.ts +8 -0
- package/src/kit/sampler.ts +281 -0
- package/src/kit/scale.ts +39 -0
- package/src/kit/source.ts +57 -0
- package/src/kit/system.ts +99 -0
- package/src/kit/ticks.ts +85 -0
- package/src/kit/withdrawn.ts +8 -0
- package/src/marks/curve-area.ts +89 -0
- package/src/marks/drop-lines.ts +63 -0
- package/src/marks/plot-point.ts +107 -0
- package/src/marks/riemann-sum.ts +98 -0
- package/src/marks/tangent-line.ts +103 -0
- package/src/marks/vector-arrow.ts +98 -0
- package/src/nodes.ts +63 -0
- package/src/plots/function-plot.ts +236 -0
- package/src/plots/points-plot.ts +107 -0
- package/src/plots/stream-lines.ts +151 -0
- package/src/plots/traced-path.ts +52 -0
- package/src/plots/vector-field.ts +88 -0
- package/src/structures/graph-layout.ts +145 -0
- package/src/structures/network-graph.ts +308 -0
- package/src/structures/sample-space.ts +209 -0
- package/src/systems/axes2d.ts +217 -0
- package/src/systems/number-line.ts +131 -0
- package/src/systems/number-plane.ts +203 -0
- package/src/systems/polar-plane.ts +176 -0
- package/src/tex/geometry.ts +703 -0
- package/src/tex/index.ts +14 -0
- package/src/tex/mathjax.ts +82 -0
- package/src/tex/matrix.ts +75 -0
- package/src/tex/node.ts +641 -0
- package/src/tex/tween.ts +308 -0
- package/src/three/axes3d.ts +240 -0
- package/src/three/curve-plot3d.ts +147 -0
- package/src/three/label3d.ts +50 -0
- package/src/three/plot-point3d.ts +66 -0
- package/src/three/space.ts +67 -0
- package/src/three/surface-plot3d.ts +323 -0
- package/src/three/vector-arrow3d.ts +77 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Fills, Graphics2D, Mat, Tex, type Color, type Graphics3D, type SurfaceTexture3D, type Vector3 } from "@motionscript/sdk";
|
|
2
|
+
import { createBoundedCache } from "../kit/cache";
|
|
3
|
+
|
|
4
|
+
/** Text in a 3D graph: a camera-facing card, so a number reads square-on from wherever the scene is seen. */
|
|
5
|
+
|
|
6
|
+
export interface LabelStyle3D {
|
|
7
|
+
/** The height of the text's em, in scene units. */
|
|
8
|
+
size: number;
|
|
9
|
+
color: Color;
|
|
10
|
+
fontFamily: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** The em the text is rasterized at, in buffer pixels, and the card's height around it. */
|
|
14
|
+
const EM = 64;
|
|
15
|
+
const CARD_HEIGHT = 96;
|
|
16
|
+
|
|
17
|
+
// A texture's identity is its source object, so one text in one style is one source for as long as it is asked for.
|
|
18
|
+
const cards = createBoundedCache<{ texture: SurfaceTexture3D; width: number }>(256);
|
|
19
|
+
|
|
20
|
+
function card(text: string, style: LabelStyle3D): { texture: SurfaceTexture3D; width: number } {
|
|
21
|
+
const key = `${style.fontFamily}|${JSON.stringify(style.color)}|${text}`;
|
|
22
|
+
const hit = cards.get(key);
|
|
23
|
+
if (hit) return hit;
|
|
24
|
+
const width = Math.min(1024, Math.max(128, Math.ceil((text.length * EM * 0.62 + EM) / 32) * 32));
|
|
25
|
+
const source = new Graphics2D()
|
|
26
|
+
.text({
|
|
27
|
+
text,
|
|
28
|
+
fontSize: EM,
|
|
29
|
+
...(style.fontFamily === "" ? {} : { fontFamily: style.fontFamily }),
|
|
30
|
+
wrap: false,
|
|
31
|
+
width,
|
|
32
|
+
textAlign: "center",
|
|
33
|
+
})
|
|
34
|
+
.fill(Fills.color(style.color));
|
|
35
|
+
const made = { texture: Tex.surface(source, { width, height: CARD_HEIGHT }), width };
|
|
36
|
+
cards.set(key, made);
|
|
37
|
+
return made;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Draw `text` centred on `at`, a local point. */
|
|
41
|
+
export function drawLabel3D(g3: Graphics3D, text: string, at: Vector3, style: LabelStyle3D): void {
|
|
42
|
+
if (text === "" || !(style.size > 0)) return;
|
|
43
|
+
const { texture, width } = card(text, style);
|
|
44
|
+
const height = (style.size * CARD_HEIGHT) / EM;
|
|
45
|
+
g3.sprite({
|
|
46
|
+
material: Mat.sprite({ map: texture, transparent: true, depthWrite: false }),
|
|
47
|
+
position: at,
|
|
48
|
+
scale: [(height * width) / CARD_HEIGHT, height, 1],
|
|
49
|
+
});
|
|
50
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import {
|
|
2
|
+
colorProperty,
|
|
3
|
+
Geo,
|
|
4
|
+
Graphics3D,
|
|
5
|
+
Mesh3D,
|
|
6
|
+
node,
|
|
7
|
+
property,
|
|
8
|
+
type AssetScope,
|
|
9
|
+
type Color,
|
|
10
|
+
type Geometry3D,
|
|
11
|
+
type Mesh3DProps,
|
|
12
|
+
type NodeConfig,
|
|
13
|
+
} from "@motionscript/sdk";
|
|
14
|
+
import { drawLabel3D } from "./label3d";
|
|
15
|
+
|
|
16
|
+
export interface PlotPoint3dProps extends Mesh3DProps {
|
|
17
|
+
radius?: number;
|
|
18
|
+
label?: string;
|
|
19
|
+
labelSize?: number;
|
|
20
|
+
labelColor?: Color;
|
|
21
|
+
fontFamily?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A dot at graph coordinates in a 3D graph, with an optional label. Put inside
|
|
26
|
+
* a surface it rides it: its `x` and `y` say where, and its height is the surface's.
|
|
27
|
+
*/
|
|
28
|
+
@node({
|
|
29
|
+
key: "plotPoint3d",
|
|
30
|
+
within: ["axes3d", "surfacePlot3d"],
|
|
31
|
+
forkable: true,
|
|
32
|
+
layout: { acceptsChildren: false },
|
|
33
|
+
seed: { fill: "#facc15" },
|
|
34
|
+
})
|
|
35
|
+
export class PlotPoint3d extends Mesh3D<PlotPoint3dProps> {
|
|
36
|
+
/** In scene units, so a point is the same size whatever the axes show. */
|
|
37
|
+
@property({ default: 0.1, min: 0, step: 0.01 }) declare radius: number;
|
|
38
|
+
@property({ default: "", animatable: false }) declare label: string;
|
|
39
|
+
/** The height of the label's text, in scene units. */
|
|
40
|
+
@property({ default: 0.26, min: 0.01, step: 0.01 }) declare labelSize: number;
|
|
41
|
+
@colorProperty({ default: "#e5e7eb" }) declare labelColor: Color;
|
|
42
|
+
@property({ default: "", animatable: false }) declare fontFamily: string;
|
|
43
|
+
|
|
44
|
+
constructor(props?: NodeConfig<PlotPoint3d, PlotPoint3dProps>) {
|
|
45
|
+
super(props ?? {});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
override declareAssets(assets: AssetScope): void {
|
|
49
|
+
super.declareAssets(assets);
|
|
50
|
+
if (this.fontFamily !== "") assets.font(this.fontFamily);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected override buildGeometry(): Geometry3D | undefined {
|
|
54
|
+
return this.radius > 0 ? Geo.sphere({ radius: this.radius, segments: [24, 16] }) : undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
protected override buildGraphics3D(): Graphics3D {
|
|
58
|
+
const g3 = super.buildGraphics3D();
|
|
59
|
+
drawLabel3D(g3, this.label, { x: 0, y: this.radius + this.labelSize * 0.85, z: 0 }, {
|
|
60
|
+
size: this.labelSize,
|
|
61
|
+
color: this.labelColor,
|
|
62
|
+
fontFamily: this.fontFamily,
|
|
63
|
+
});
|
|
64
|
+
return g3;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Node3D, type ChildSpace3D, type Vector3 } from "@motionscript/sdk";
|
|
2
|
+
import type { Interval } from "../kit/coords";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A 3D graph's coordinates, as its axes' local space sees them. A graph is
|
|
6
|
+
* right-handed with z up, and the scene has y up, so graph x, y and z run along
|
|
7
|
+
* the scene's z, x and y: looked at head-on, y is to the right and z is up.
|
|
8
|
+
*/
|
|
9
|
+
export interface GraphSpace extends ChildSpace3D {
|
|
10
|
+
readonly xRange: Interval;
|
|
11
|
+
readonly yRange: Interval;
|
|
12
|
+
readonly zRange: Interval;
|
|
13
|
+
/** Changes exactly when the mapping does: what a mark's geometry is cached under. */
|
|
14
|
+
readonly revision: string;
|
|
15
|
+
/** A step from one graph point to another, as the local one: what a mark's own geometry is drawn in. */
|
|
16
|
+
offset(x: number, y: number, z: number): Vector3;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The space of a box `lengths` on a side, in scene units, showing the three ranges, centred on its node. */
|
|
20
|
+
export function graphSpace(xRange: Interval, yRange: Interval, zRange: Interval, lengths: Vector3): GraphSpace {
|
|
21
|
+
const ux = unit(xRange, lengths.x);
|
|
22
|
+
const uy = unit(yRange, lengths.y);
|
|
23
|
+
const uz = unit(zRange, lengths.z);
|
|
24
|
+
const cx = (xRange[0] + xRange[1]) / 2;
|
|
25
|
+
const cy = (yRange[0] + yRange[1]) / 2;
|
|
26
|
+
const cz = (zRange[0] + zRange[1]) / 2;
|
|
27
|
+
return {
|
|
28
|
+
xRange,
|
|
29
|
+
yRange,
|
|
30
|
+
zRange,
|
|
31
|
+
revision: [xRange, yRange, zRange, lengths.x, lengths.y, lengths.z].join("|"),
|
|
32
|
+
toLocal: (x, y, z) => ({ x: (y - cy) * uy, y: (z - cz) * uz, z: (x - cx) * ux }),
|
|
33
|
+
offset: (x, y, z) => ({ x: y * uy, y: z * uz, z: x * ux }),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function unit([min, max]: Interval, length: number): number {
|
|
38
|
+
const span = max - min;
|
|
39
|
+
return span > 0 ? length / span : 0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface Spaced {
|
|
43
|
+
_graphSpace(): GraphSpace;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The graph a 3D mark is drawn in: its nearest ancestor that has one, up to and including the viewport. */
|
|
47
|
+
export function spaceOf(mark: Node3D): GraphSpace | null {
|
|
48
|
+
for (let at = mark.parent; at; at = at instanceof Node3D ? at.parent : null) {
|
|
49
|
+
const spaced = at as Partial<Spaced>;
|
|
50
|
+
if (typeof spaced._graphSpace === "function") return spaced._graphSpace();
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A unit quaternion turning the scene's +y, the way a cylinder or a cone points, onto `direction`. */
|
|
56
|
+
export function aim(direction: Vector3): { x: number; y: number; z: number; w: number } {
|
|
57
|
+
const length = Math.hypot(direction.x, direction.y, direction.z);
|
|
58
|
+
if (!(length > 0)) return { x: 0, y: 0, z: 0, w: 1 };
|
|
59
|
+
const dx = direction.x / length;
|
|
60
|
+
const dy = direction.y / length;
|
|
61
|
+
const dz = direction.z / length;
|
|
62
|
+
// Straight down is the one turn with no single axis: any in the ground plane will do.
|
|
63
|
+
if (dy < -1 + 1e-9) return { x: 1, y: 0, z: 0, w: 0 };
|
|
64
|
+
const w = 1 + dy;
|
|
65
|
+
const norm = Math.hypot(dz, dx, w);
|
|
66
|
+
return { x: dz / norm, y: 0, z: -dx / norm, w: w / norm };
|
|
67
|
+
}
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import {
|
|
2
|
+
colorOps,
|
|
3
|
+
command,
|
|
4
|
+
Geo,
|
|
5
|
+
Graphics3D,
|
|
6
|
+
Mat,
|
|
7
|
+
Mesh3D,
|
|
8
|
+
node,
|
|
9
|
+
property,
|
|
10
|
+
type ChildSpace3D,
|
|
11
|
+
type Color,
|
|
12
|
+
type Command,
|
|
13
|
+
type CommandArgs,
|
|
14
|
+
type Geometry3D,
|
|
15
|
+
type Material3D,
|
|
16
|
+
type MaterialShorthand3D,
|
|
17
|
+
type Mesh3DProps,
|
|
18
|
+
type NodeConfig,
|
|
19
|
+
type NormalizedColor,
|
|
20
|
+
type Vector3,
|
|
21
|
+
} from "@motionscript/sdk";
|
|
22
|
+
import type { Interval } from "../kit/coords";
|
|
23
|
+
import { parseSurface, surfaceParams, type SurfaceEquation } from "../kit/equation3d";
|
|
24
|
+
import type { ExpressionScope } from "../kit/expression";
|
|
25
|
+
import { PLOT_COLOR } from "../kit/furniture";
|
|
26
|
+
import { clamp, stated } from "../kit/geometry";
|
|
27
|
+
import { colorAt, lerpScale, readScale } from "../kit/scale";
|
|
28
|
+
import { lerpDomain, lerpParams, lerpSource, readSource, revisionOf, scopeFor, type PlotSource } from "../kit/source";
|
|
29
|
+
import { spaceOf, type GraphSpace } from "./space";
|
|
30
|
+
|
|
31
|
+
export interface SurfacePlot3dProps extends Mesh3DProps {
|
|
32
|
+
expression?: string | PlotSource;
|
|
33
|
+
uDomain?: Interval | null;
|
|
34
|
+
vDomain?: Interval | null;
|
|
35
|
+
params?: ExpressionScope;
|
|
36
|
+
segments?: number;
|
|
37
|
+
colorScale?: Color[];
|
|
38
|
+
meshLines?: number;
|
|
39
|
+
meshColor?: Color;
|
|
40
|
+
floorShadow?: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A graph point for grid coordinate `(u, v)`, both 0 to 1, written into `out`. */
|
|
44
|
+
type SurfaceSampler = (u: number, v: number, out: Vector3) => void;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A surface in a 3D graph: a height over x and y (`sin(x) * cos(y)`), or three
|
|
48
|
+
* parts in u and v. A point put inside it rides it.
|
|
49
|
+
*/
|
|
50
|
+
@node({
|
|
51
|
+
key: "surfacePlot3d",
|
|
52
|
+
within: ["axes3d"],
|
|
53
|
+
closed: true,
|
|
54
|
+
forkable: true,
|
|
55
|
+
layout: { acceptsChildren: true },
|
|
56
|
+
seed: { expression: "sin(x) * cos(y)", fill: PLOT_COLOR },
|
|
57
|
+
})
|
|
58
|
+
export class SurfacePlot3d extends Mesh3D<SurfacePlot3dProps> {
|
|
59
|
+
@property({ default: "", mapper: readSource, tween: lerpSource, kind: "surfaceEquation" })
|
|
60
|
+
declare expression: PlotSource;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The span of each variable that is drawn: x and y for a height, which run
|
|
64
|
+
* the width of the axes when left out, u and v for a parametric surface,
|
|
65
|
+
* which run from 0 to 1.
|
|
66
|
+
*/
|
|
67
|
+
@property({ default: null, tween: lerpDomain, kind: "range", defaultMin: -3, defaultMax: 3 }) declare uDomain: Interval | null;
|
|
68
|
+
@property({ default: null, tween: lerpDomain, kind: "range", defaultMin: -3, defaultMax: 3 }) declare vDomain: Interval | null;
|
|
69
|
+
|
|
70
|
+
/** The equation's named values: any name in it that is not a variable, a function or a constant. */
|
|
71
|
+
@property({ default: {}, tween: lerpParams }) declare params: ExpressionScope;
|
|
72
|
+
|
|
73
|
+
/** How finely the surface is sampled, each way. */
|
|
74
|
+
@property({ default: 48, min: 2, max: 256, step: 1, animatable: false }) declare segments: number;
|
|
75
|
+
|
|
76
|
+
/** Colours by height, lowest first, over the axes' z range. Empty paints the surface in its fill. */
|
|
77
|
+
@property({ default: [], mapper: readScale, tween: lerpScale, kind: "vector" }) declare colorScale: Color[];
|
|
78
|
+
|
|
79
|
+
/** How many lines are drawn across the surface each way; zero leaves it bare. */
|
|
80
|
+
@property({ default: 12, min: 0, max: 64, step: 1, animatable: false }) declare meshLines: number;
|
|
81
|
+
@property({ default: "#0b1220", kind: "color" }) declare meshColor: Color;
|
|
82
|
+
|
|
83
|
+
/** The opacity of the surface's shadow, laid flat on the floor of the axes and coloured by height; zero draws none. */
|
|
84
|
+
@property({ default: 0, min: 0, max: 1, step: 0.01 }) declare floorShadow: number;
|
|
85
|
+
|
|
86
|
+
constructor(props?: NodeConfig<SurfacePlot3d, SurfacePlot3dProps>) {
|
|
87
|
+
super(props ?? {});
|
|
88
|
+
if (props?.fill === undefined) this.applyProp("fill", PLOT_COLOR);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Change the equation, the surface bending from the one into the other. */
|
|
92
|
+
@command({ args: ["expression"] })
|
|
93
|
+
morphTo(args: CommandArgs<{ expression: string }> & { duration: number }): Command<SurfacePlot3dProps> {
|
|
94
|
+
return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Animate the equation's named values. */
|
|
98
|
+
@command({ args: ["params"] })
|
|
99
|
+
paramsTo(args: CommandArgs<{ params: ExpressionScope }> & { duration: number }): Command<SurfacePlot3dProps> {
|
|
100
|
+
return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Animate the spans that are drawn. */
|
|
104
|
+
@command({ args: ["uDomain", "vDomain"] })
|
|
105
|
+
domainTo(
|
|
106
|
+
args: CommandArgs<{ uDomain?: Interval | null; vDomain?: Interval | null }> & { duration: number },
|
|
107
|
+
): Command<SurfacePlot3dProps> {
|
|
108
|
+
return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The surface's height above its own origin at graph `(x, y)`, or `NaN`
|
|
113
|
+
* where it has none or is not a height. What a point riding it reads.
|
|
114
|
+
*/
|
|
115
|
+
valueAt(x: number, y: number): number {
|
|
116
|
+
const { source, blend } = this.expression;
|
|
117
|
+
const value = this._heightOf(source, x, y);
|
|
118
|
+
if (!blend) return value;
|
|
119
|
+
const from = this._heightOf(blend.from, x, y);
|
|
120
|
+
return from + (value - from) * blend.t;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** @internal */
|
|
124
|
+
override _spaceOf(): ChildSpace3D | null {
|
|
125
|
+
const space = spaceOf(this);
|
|
126
|
+
if (!space) return null;
|
|
127
|
+
return {
|
|
128
|
+
toLocal: (x, y, z) => {
|
|
129
|
+
const height = this.valueAt(x, y);
|
|
130
|
+
return space.offset(x, y, Number.isFinite(height) ? height : z);
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
protected override buildGeometry(): Geometry3D | undefined {
|
|
136
|
+
const space = spaceOf(this);
|
|
137
|
+
const sample = space && this._sampler(space);
|
|
138
|
+
if (!space || !sample) return undefined;
|
|
139
|
+
const scale = this.colorScale as unknown as NormalizedColor[];
|
|
140
|
+
const point: Vector3 = { x: 0, y: 0, z: 0 };
|
|
141
|
+
const low = space.offset(0, 0, space.zRange[0] - this.z).y;
|
|
142
|
+
const high = space.offset(0, 0, space.zRange[1] - this.z).y;
|
|
143
|
+
return Geo.parametric({
|
|
144
|
+
segments: Math.max(2, Math.round(this.segments)),
|
|
145
|
+
vertex: (u, v) => {
|
|
146
|
+
sample(u, v, point);
|
|
147
|
+
return space.offset(point.x, point.y, point.z);
|
|
148
|
+
},
|
|
149
|
+
...(scale.length > 0
|
|
150
|
+
? { color: (_u, _v, position) => colorAt(scale, high > low ? (position.y - low) / (high - low) : 0) }
|
|
151
|
+
: {}),
|
|
152
|
+
computeNormals: true,
|
|
153
|
+
revision: revisionOf(`${this._key(space)}|${JSON.stringify(scale)}`),
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
protected override materialShorthand(): MaterialShorthand3D {
|
|
158
|
+
const own = super.materialShorthand();
|
|
159
|
+
const scaled = (this.colorScale as unknown as NormalizedColor[]).length > 0;
|
|
160
|
+
// A surface is seen from both sides; a vertex colour multiplies the fill, so a scale is shown on white.
|
|
161
|
+
return { faces: "both", ...own, ...(scaled ? { vertexColors: true, fill: "white" } : {}) };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
protected override buildGraphics3D(): Graphics3D {
|
|
165
|
+
const g3 = new Graphics3D();
|
|
166
|
+
const space = spaceOf(this);
|
|
167
|
+
const lines = space ? this._meshLines(space) : [];
|
|
168
|
+
for (const op of super.buildGraphics3D().ops()) {
|
|
169
|
+
if (op.kind !== "mesh") continue;
|
|
170
|
+
g3.mesh(op.geometry, mapMaterials(op.material, (material) => ({
|
|
171
|
+
...material,
|
|
172
|
+
// Pushed back a hair so the lines drawn on it are not fought for, and blended rather than a hole when faded.
|
|
173
|
+
...(lines.length > 0 ? { polygonOffset: true, polygonOffsetFactor: 1, polygonOffsetUnits: 1 } : {}),
|
|
174
|
+
...((this.opacity ?? 1) < 1 ? { depthWrite: false } : {}),
|
|
175
|
+
} as Material3D)), op.transform);
|
|
176
|
+
}
|
|
177
|
+
if (lines.length > 0) {
|
|
178
|
+
const color = colorOps.resolve(this.meshColor);
|
|
179
|
+
g3.line({ points: lines, segments: true, stroke: { fill: color }, opacity: color[3] * (this.opacity ?? 1) });
|
|
180
|
+
}
|
|
181
|
+
const shadow = space && this.floorShadow > 0 ? this._floorShadow(space) : undefined;
|
|
182
|
+
if (shadow) {
|
|
183
|
+
const scaled = (this.colorScale as unknown as NormalizedColor[]).length > 0;
|
|
184
|
+
g3.mesh(shadow, Mat.basic({
|
|
185
|
+
color: scaled ? "white" : PLOT_COLOR,
|
|
186
|
+
vertexColors: scaled,
|
|
187
|
+
opacity: this.floorShadow * (this.opacity ?? 1),
|
|
188
|
+
transparent: true,
|
|
189
|
+
// Translucent and under everything: writing depth would hide the floor's grid lines behind it.
|
|
190
|
+
depthWrite: false,
|
|
191
|
+
faces: "both",
|
|
192
|
+
}));
|
|
193
|
+
}
|
|
194
|
+
return g3;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private _linesKey = "";
|
|
198
|
+
private _lines: Vector3[] = [];
|
|
199
|
+
|
|
200
|
+
private _heightOf(source: string, x: number, y: number): number {
|
|
201
|
+
const equation = equationOf(source);
|
|
202
|
+
if (equation?.kind !== "height") return NaN;
|
|
203
|
+
return equation.f(x, y, scopeFor(surfaceParams(source), this.params));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private _key(space: GraphSpace): string {
|
|
207
|
+
const { source, blend } = this.expression;
|
|
208
|
+
return [source, blend?.from, blend?.t, space.revision, this.uDomain, this.vDomain, JSON.stringify(this.params), this.x, this.y, this.z].join("|");
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
private _sampler(space: GraphSpace): SurfaceSampler | null {
|
|
212
|
+
const { source, blend } = this.expression;
|
|
213
|
+
const to = this._surface(source, space);
|
|
214
|
+
if (!to || !blend) return to;
|
|
215
|
+
const from = this._surface(blend.from, space);
|
|
216
|
+
if (!from) return to;
|
|
217
|
+
const start: Vector3 = { x: 0, y: 0, z: 0 };
|
|
218
|
+
return (u, v, out) => {
|
|
219
|
+
from(u, v, start);
|
|
220
|
+
to(u, v, out);
|
|
221
|
+
out.x = start.x + (out.x - start.x) * blend.t;
|
|
222
|
+
out.y = start.y + (out.y - start.y) * blend.t;
|
|
223
|
+
out.z = start.z + (out.z - start.z) * blend.t;
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** `source` as graph points, measured from this node's own position. */
|
|
228
|
+
private _surface(source: string, space: GraphSpace): SurfaceSampler | null {
|
|
229
|
+
const equation = equationOf(source);
|
|
230
|
+
if (!equation) return null;
|
|
231
|
+
const params = scopeFor(surfaceParams(source), this.params);
|
|
232
|
+
if (equation.kind === "parametric") {
|
|
233
|
+
const [u0, u1] = this.uDomain ?? [0, 1];
|
|
234
|
+
const [v0, v1] = this.vDomain ?? [0, 1];
|
|
235
|
+
const { x, y, z } = equation;
|
|
236
|
+
return (u, v, out) => {
|
|
237
|
+
const a = u0 + (u1 - u0) * u;
|
|
238
|
+
const b = v0 + (v1 - v0) * v;
|
|
239
|
+
out.x = finite(x(a, b, params));
|
|
240
|
+
out.y = finite(y(a, b, params));
|
|
241
|
+
out.z = finite(z(a, b, params));
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
const [x0, x1] = this.uDomain ?? [space.xRange[0] - this.x, space.xRange[1] - this.x];
|
|
245
|
+
const [y0, y1] = this.vDomain ?? [space.yRange[0] - this.y, space.yRange[1] - this.y];
|
|
246
|
+
// Held inside the axes: a pole would otherwise stretch one triangle across the scene.
|
|
247
|
+
const low = space.zRange[0] - this.z;
|
|
248
|
+
const high = space.zRange[1] - this.z;
|
|
249
|
+
const { f } = equation;
|
|
250
|
+
return (u, v, out) => {
|
|
251
|
+
out.x = x0 + (x1 - x0) * u;
|
|
252
|
+
out.y = y0 + (y1 - y0) * v;
|
|
253
|
+
out.z = clamp(finite(f(out.x, out.y, params)), low, high);
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private _floorShadow(space: GraphSpace): Geometry3D | undefined {
|
|
258
|
+
const sample = this._sampler(space);
|
|
259
|
+
if (!sample) return undefined;
|
|
260
|
+
const scale = this.colorScale as unknown as NormalizedColor[];
|
|
261
|
+
const [low, high] = space.zRange;
|
|
262
|
+
const floor = low - this.z;
|
|
263
|
+
const point: Vector3 = { x: 0, y: 0, z: 0 };
|
|
264
|
+
return Geo.parametric({
|
|
265
|
+
segments: Math.max(2, Math.round(this.segments)),
|
|
266
|
+
vertex: (u, v) => {
|
|
267
|
+
sample(u, v, point);
|
|
268
|
+
return space.offset(point.x, point.y, floor);
|
|
269
|
+
},
|
|
270
|
+
...(scale.length > 0
|
|
271
|
+
? {
|
|
272
|
+
color: (u: number, v: number) => {
|
|
273
|
+
sample(u, v, point);
|
|
274
|
+
return colorAt(scale, high > low ? (point.z + this.z - low) / (high - low) : 0);
|
|
275
|
+
},
|
|
276
|
+
}
|
|
277
|
+
: {}),
|
|
278
|
+
revision: revisionOf(`${this._key(space)}|${JSON.stringify(scale)}|floor`),
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
private _meshLines(space: GraphSpace): Vector3[] {
|
|
283
|
+
const count = Math.round(this.meshLines);
|
|
284
|
+
const sample = count > 0 ? this._sampler(space) : null;
|
|
285
|
+
if (!sample) return [];
|
|
286
|
+
const steps = Math.max(2, Math.round(this.segments));
|
|
287
|
+
const key = `${this._key(space)}|${count}|${steps}`;
|
|
288
|
+
if (key === this._linesKey) return this._lines;
|
|
289
|
+
const lines: Vector3[] = [];
|
|
290
|
+
const point: Vector3 = { x: 0, y: 0, z: 0 };
|
|
291
|
+
const at = (u: number, v: number): Vector3 => {
|
|
292
|
+
sample(u, v, point);
|
|
293
|
+
return space.offset(point.x, point.y, point.z);
|
|
294
|
+
};
|
|
295
|
+
for (let i = 0; i <= count; i++) {
|
|
296
|
+
const fixed = i / count;
|
|
297
|
+
for (let j = 0; j < steps; j++) {
|
|
298
|
+
lines.push(at(fixed, j / steps), at(fixed, (j + 1) / steps));
|
|
299
|
+
lines.push(at(j / steps, fixed), at((j + 1) / steps, fixed));
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
this._lines = lines;
|
|
303
|
+
this._linesKey = key;
|
|
304
|
+
return lines;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function equationOf(source: string): SurfaceEquation | null {
|
|
309
|
+
const equation = parseSurface(source, surfaceParams(source));
|
|
310
|
+
return equation instanceof Error ? null : equation;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// A hole in the data is drawn flat rather than as a hole in the mesh.
|
|
314
|
+
function finite(value: number): number {
|
|
315
|
+
return Number.isFinite(value) ? value : 0;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function mapMaterials(
|
|
319
|
+
material: Material3D | readonly Material3D[],
|
|
320
|
+
change: (material: Material3D) => Material3D,
|
|
321
|
+
): Material3D | readonly Material3D[] {
|
|
322
|
+
return Array.isArray(material) ? (material as readonly Material3D[]).map(change) : change(material as Material3D);
|
|
323
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import {
|
|
2
|
+
command,
|
|
3
|
+
Graphics3D,
|
|
4
|
+
lerpVector3,
|
|
5
|
+
Mesh3D,
|
|
6
|
+
node,
|
|
7
|
+
property,
|
|
8
|
+
resolveVector3,
|
|
9
|
+
type Command,
|
|
10
|
+
type CommandArgs,
|
|
11
|
+
type Mesh3DProps,
|
|
12
|
+
type NodeConfig,
|
|
13
|
+
type Vector3,
|
|
14
|
+
type Vector3Input,
|
|
15
|
+
} from "@motionscript/sdk";
|
|
16
|
+
import { stated } from "../kit/geometry";
|
|
17
|
+
import { aim, spaceOf } from "./space";
|
|
18
|
+
|
|
19
|
+
export interface VectorArrow3dProps extends Mesh3DProps {
|
|
20
|
+
tail?: Vector3Input;
|
|
21
|
+
head?: Vector3Input;
|
|
22
|
+
thickness?: number;
|
|
23
|
+
tip?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** An arrow between two points of a 3D graph: a vector from the origin by default. */
|
|
27
|
+
@node({
|
|
28
|
+
key: "vectorArrow3d",
|
|
29
|
+
within: ["axes3d"],
|
|
30
|
+
forkable: true,
|
|
31
|
+
layout: { acceptsChildren: false },
|
|
32
|
+
seed: { head: { x: 2, y: 1, z: 1.5 }, fill: "#f97316" },
|
|
33
|
+
})
|
|
34
|
+
export class VectorArrow3d extends Mesh3D<VectorArrow3dProps> {
|
|
35
|
+
@property({ default: { x: 0, y: 0, z: 0 }, mapper: resolveVector3, tween: lerpVector3, kind: "vector3" }) declare tail: Vector3;
|
|
36
|
+
@property({ default: { x: 1, y: 1, z: 1 }, mapper: resolveVector3, tween: lerpVector3, kind: "vector3" }) declare head: Vector3;
|
|
37
|
+
/** The radius of the shaft, in scene units; the tip is sized from it. */
|
|
38
|
+
@property({ default: 0.035, min: 0.001, step: 0.005 }) declare thickness: number;
|
|
39
|
+
@property({ default: true, animatable: false }) declare tip: boolean;
|
|
40
|
+
|
|
41
|
+
constructor(props?: NodeConfig<VectorArrow3d, VectorArrow3dProps>) {
|
|
42
|
+
super(props ?? {});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Animate either end, or both. */
|
|
46
|
+
@command({ args: ["tail", "head"] })
|
|
47
|
+
endpointsTo(args: CommandArgs<{ tail?: Vector3Input; head?: Vector3Input }> & { duration: number }): Command<VectorArrow3dProps> {
|
|
48
|
+
return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
protected override buildGraphics3D(): Graphics3D {
|
|
52
|
+
const g3 = new Graphics3D();
|
|
53
|
+
const space = spaceOf(this);
|
|
54
|
+
const { tail, head, thickness } = this;
|
|
55
|
+
// Outside a graph an arrow is drawn in the scene's own units.
|
|
56
|
+
const from = space ? space.offset(tail.x, tail.y, tail.z) : tail;
|
|
57
|
+
const to = space ? space.offset(head.x, head.y, head.z) : head;
|
|
58
|
+
const along: Vector3 = { x: to.x - from.x, y: to.y - from.y, z: to.z - from.z };
|
|
59
|
+
const length = Math.hypot(along.x, along.y, along.z);
|
|
60
|
+
if (!(length > 0)) return g3;
|
|
61
|
+
|
|
62
|
+
const paint = this.materialShorthand();
|
|
63
|
+
const quaternion = aim(along);
|
|
64
|
+
const tipLength = this.tip ? Math.min(thickness * 8, length) : 0;
|
|
65
|
+
const shaft = length - tipLength;
|
|
66
|
+
const at = (distance: number): Vector3 => ({
|
|
67
|
+
x: from.x + (along.x * distance) / length,
|
|
68
|
+
y: from.y + (along.y * distance) / length,
|
|
69
|
+
z: from.z + (along.z * distance) / length,
|
|
70
|
+
});
|
|
71
|
+
if (shaft > 0) g3.cylinder({ ...paint, radius: thickness, height: shaft, segments: [12, 1], position: at(shaft / 2), quaternion });
|
|
72
|
+
if (tipLength > 0) {
|
|
73
|
+
g3.cone({ ...paint, radius: thickness * 2.8, height: tipLength, segments: [20, 1], position: at(shaft + tipLength / 2), quaternion });
|
|
74
|
+
}
|
|
75
|
+
return g3;
|
|
76
|
+
}
|
|
77
|
+
}
|