@motionscript/math 0.1.0-alpha.7 → 0.1.0-alpha.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +49 -48
  4. package/README.md +390 -391
  5. package/dist/annotate/angle-mark.d.ts +6 -6
  6. package/dist/annotate/angle-mark.js.map +1 -1
  7. package/dist/annotate/curly-brace.d.ts +9 -9
  8. package/dist/annotate/curly-brace.js.map +1 -1
  9. package/dist/annotate/emphasis-mark.d.ts +9 -9
  10. package/dist/annotate/emphasis-mark.js.map +1 -1
  11. package/dist/browser/chunks/{chunk-ABYBW64J.js → chunk-IRZCG6PM.js} +2 -2
  12. package/dist/browser/chunks/chunk-IRZCG6PM.js.map +7 -0
  13. package/dist/browser/chunks/{chunk-K77KLQX4.js → chunk-WMVW6ZIV.js} +2 -2
  14. package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +7 -0
  15. package/dist/browser/chunks/{mathjax-DNP3YRES.js → mathjax-WV55AC2P.js} +1 -1
  16. package/dist/browser/chunks/{mathjax-DNP3YRES.js.map → mathjax-WV55AC2P.js.map} +1 -1
  17. package/dist/browser/index.js +1 -1
  18. package/dist/browser/index.js.map +3 -3
  19. package/dist/browser/kit.js +1 -1
  20. package/dist/browser/manifest.json +1 -1
  21. package/dist/browser/tex.js +1 -1
  22. package/dist/browser/tex.js.map +1 -1
  23. package/dist/engine.d.ts +1 -1
  24. package/dist/engine.js +1 -1
  25. package/dist/engine.js.map +1 -1
  26. package/dist/index.js.map +1 -1
  27. package/dist/kit/brace.js.map +1 -1
  28. package/dist/kit/cache.js.map +1 -1
  29. package/dist/kit/component.d.ts.map +1 -1
  30. package/dist/kit/component.js.map +1 -1
  31. package/dist/kit/contour.js.map +1 -1
  32. package/dist/kit/coords.js.map +1 -1
  33. package/dist/kit/curve.js.map +1 -1
  34. package/dist/kit/equation.js.map +1 -1
  35. package/dist/kit/equation3d.js.map +1 -1
  36. package/dist/kit/expression.js.map +1 -1
  37. package/dist/kit/field-mark.d.ts.map +1 -1
  38. package/dist/kit/field-mark.js.map +1 -1
  39. package/dist/kit/field.js.map +1 -1
  40. package/dist/kit/follow.d.ts +2 -2
  41. package/dist/kit/follow.d.ts.map +1 -1
  42. package/dist/kit/follow.js.map +1 -1
  43. package/dist/kit/furniture.js.map +1 -1
  44. package/dist/kit/geometry.js.map +1 -1
  45. package/dist/kit/graph.js.map +1 -1
  46. package/dist/kit/mark.js.map +1 -1
  47. package/dist/kit/part-fills.js.map +1 -1
  48. package/dist/kit/place.js.map +1 -1
  49. package/dist/kit/sampler.js +1 -1
  50. package/dist/kit/sampler.js.map +1 -1
  51. package/dist/kit/scale.js.map +1 -1
  52. package/dist/kit/source.js.map +1 -1
  53. package/dist/kit/system.js.map +1 -1
  54. package/dist/kit/ticks.js.map +1 -1
  55. package/dist/kit/withdrawn.js.map +1 -1
  56. package/dist/marks/curve-area.js.map +1 -1
  57. package/dist/marks/drop-lines.d.ts +6 -6
  58. package/dist/marks/drop-lines.js.map +1 -1
  59. package/dist/marks/plot-point.d.ts +3 -3
  60. package/dist/marks/plot-point.js.map +1 -1
  61. package/dist/marks/riemann-sum.d.ts +3 -3
  62. package/dist/marks/riemann-sum.js.map +1 -1
  63. package/dist/marks/tangent-line.d.ts +6 -6
  64. package/dist/marks/tangent-line.js.map +1 -1
  65. package/dist/marks/vector-arrow.d.ts +6 -6
  66. package/dist/marks/vector-arrow.js.map +1 -1
  67. package/dist/nodes.js.map +1 -1
  68. package/dist/plots/function-plot.d.ts +8 -8
  69. package/dist/plots/function-plot.d.ts.map +1 -1
  70. package/dist/plots/function-plot.js.map +1 -1
  71. package/dist/plots/points-plot.js.map +1 -1
  72. package/dist/plots/stream-lines.js.map +1 -1
  73. package/dist/plots/traced-path.js.map +1 -1
  74. package/dist/plots/vector-field.js.map +1 -1
  75. package/dist/structures/graph-layout.js.map +1 -1
  76. package/dist/structures/network-graph.js.map +1 -1
  77. package/dist/structures/sample-space.js.map +1 -1
  78. package/dist/systems/axes2d.d.ts.map +1 -1
  79. package/dist/systems/axes2d.js.map +1 -1
  80. package/dist/systems/number-line.js.map +1 -1
  81. package/dist/systems/number-plane.js.map +1 -1
  82. package/dist/systems/polar-plane.js.map +1 -1
  83. package/dist/tex/geometry.js.map +1 -1
  84. package/dist/tex/index.js.map +1 -1
  85. package/dist/tex/mathjax.js.map +1 -1
  86. package/dist/tex/matrix.d.ts +2 -2
  87. package/dist/tex/matrix.js.map +1 -1
  88. package/dist/tex/node.d.ts +2 -2
  89. package/dist/tex/node.js.map +1 -1
  90. package/dist/tex/tween.js.map +1 -1
  91. package/dist/tex/typeset.d.ts +4 -4
  92. package/dist/tex/typeset.d.ts.map +1 -1
  93. package/dist/tex/typeset.js.map +1 -1
  94. package/dist/three/axes3d.js.map +1 -1
  95. package/dist/three/curve-plot3d.d.ts +27 -27
  96. package/dist/three/curve-plot3d.d.ts.map +1 -1
  97. package/dist/three/curve-plot3d.js +3 -2
  98. package/dist/three/curve-plot3d.js.map +1 -1
  99. package/dist/three/entries.js.map +1 -1
  100. package/dist/three/label3d.d.ts.map +1 -1
  101. package/dist/three/label3d.js +3 -1
  102. package/dist/three/label3d.js.map +1 -1
  103. package/dist/three/plot-point3d.d.ts +27 -27
  104. package/dist/three/plot-point3d.js.map +1 -1
  105. package/dist/three/space.js.map +1 -1
  106. package/dist/three/surface-plot3d.d.ts +36 -36
  107. package/dist/three/surface-plot3d.js +6 -1
  108. package/dist/three/surface-plot3d.js.map +1 -1
  109. package/dist/three/vector-arrow3d.d.ts +27 -27
  110. package/dist/three/vector-arrow3d.js.map +1 -1
  111. package/package.json +5 -4
  112. package/registry.json +251 -251
  113. package/src/annotate/angle-mark.ts +137 -137
  114. package/src/annotate/curly-brace.ts +148 -148
  115. package/src/annotate/emphasis-mark.ts +118 -118
  116. package/src/engine.ts +86 -86
  117. package/src/index.ts +29 -29
  118. package/src/kit/brace.ts +37 -37
  119. package/src/kit/cache.ts +61 -61
  120. package/src/kit/component.ts +57 -57
  121. package/src/kit/contour.ts +94 -94
  122. package/src/kit/coords.ts +278 -278
  123. package/src/kit/curve.ts +47 -47
  124. package/src/kit/equation.ts +181 -181
  125. package/src/kit/equation3d.ts +137 -137
  126. package/src/kit/expression.ts +159 -159
  127. package/src/kit/field-mark.ts +171 -171
  128. package/src/kit/field.ts +50 -50
  129. package/src/kit/follow.ts +229 -229
  130. package/src/kit/furniture.ts +113 -113
  131. package/src/kit/geometry.ts +49 -49
  132. package/src/kit/graph.ts +116 -116
  133. package/src/kit/mark.ts +152 -152
  134. package/src/kit/part-fills.ts +61 -61
  135. package/src/kit/place.ts +77 -77
  136. package/src/kit/sampler.ts +281 -281
  137. package/src/kit/scale.ts +39 -39
  138. package/src/kit/source.ts +57 -57
  139. package/src/kit/system.ts +105 -105
  140. package/src/kit/ticks.ts +85 -85
  141. package/src/kit/withdrawn.ts +8 -8
  142. package/src/marks/curve-area.ts +89 -89
  143. package/src/marks/drop-lines.ts +71 -71
  144. package/src/marks/plot-point.ts +102 -102
  145. package/src/marks/riemann-sum.ts +99 -99
  146. package/src/marks/tangent-line.ts +119 -119
  147. package/src/marks/vector-arrow.ts +98 -98
  148. package/src/nodes.ts +99 -99
  149. package/src/plots/function-plot.ts +220 -220
  150. package/src/plots/points-plot.ts +103 -103
  151. package/src/plots/stream-lines.ts +160 -160
  152. package/src/plots/traced-path.ts +70 -70
  153. package/src/plots/vector-field.ts +107 -107
  154. package/src/structures/graph-layout.ts +145 -145
  155. package/src/structures/network-graph.ts +291 -291
  156. package/src/structures/sample-space.ts +210 -210
  157. package/src/systems/axes2d.ts +226 -226
  158. package/src/systems/number-line.ts +130 -130
  159. package/src/systems/number-plane.ts +232 -232
  160. package/src/systems/polar-plane.ts +177 -177
  161. package/src/tex/geometry.ts +703 -703
  162. package/src/tex/index.ts +15 -15
  163. package/src/tex/mathjax.ts +82 -82
  164. package/src/tex/matrix.ts +97 -97
  165. package/src/tex/node.ts +74 -74
  166. package/src/tex/tween.ts +308 -308
  167. package/src/tex/typeset.ts +461 -461
  168. package/src/three/axes3d.ts +267 -267
  169. package/src/three/curve-plot3d.ts +146 -144
  170. package/src/three/entries.ts +42 -42
  171. package/src/three/label3d.ts +52 -50
  172. package/src/three/plot-point3d.ts +63 -63
  173. package/src/three/space.ts +92 -92
  174. package/src/three/surface-plot3d.ts +318 -312
  175. package/src/three/vector-arrow3d.ts +88 -88
  176. package/dist/browser/chunks/chunk-ABYBW64J.js.map +0 -7
  177. package/dist/browser/chunks/chunk-K77KLQX4.js.map +0 -7
  178. package/dist/kit/placement.d.ts +0 -8
  179. package/dist/kit/placement.d.ts.map +0 -1
  180. package/dist/kit/placement.js +0 -8
  181. package/dist/kit/placement.js.map +0 -1
@@ -1,144 +1,146 @@
1
- import {
2
- component3d,
3
- Geo,
4
- Material,
5
- Types,
6
- type Geometry3D,
7
- type Mesh3DProps,
8
- type NodeOf,
9
- type Values,
10
- type Vector3,
11
- // Named so the emitted declaration reaches these through the sdk, not core.
12
- type Blend3D as _Blend3D,
13
- type Color as _Color,
14
- type Faces3D as _Faces3D,
15
- type Fill3D as _Fill3D,
16
- type Material3D as _Material3D,
17
- type Node3D as _Node3D,
18
- type Node3DProps as _Node3DProps,
19
- type NormalizedColor as _NormalizedColor,
20
- type Shading3D as _Shading3D,
21
- type Texture3D as _Texture3D,
22
- } from "@motionscript/sdk";
23
- import type { Interval } from "../kit/coords";
24
- import { parseSpaceCurve, spaceCurveParams, type SpaceCurveEquation } from "../kit/equation3d";
25
- import type { ExpressionScope } from "../kit/expression";
26
- import { PLOT_COLOR } from "../kit/furniture";
27
- import { clamp } from "../kit/geometry";
28
- import { lerpParams, lerpSource, readSource, scopeFor, type PlotSource } from "../kit/source";
29
- import { domainEntry, meshEntries } from "./entries";
30
- import { graphEntry, graphOf, type GraphSpace } from "./space";
31
-
32
- export interface CurvePlot3dProps extends Mesh3DProps {
33
- expression?: string | PlotSource;
34
- domain?: Interval | null;
35
- params?: ExpressionScope;
36
- samples?: number;
37
- thickness?: number;
38
- start?: number;
39
- end?: number;
40
- }
41
-
42
- const state = {
43
- ...meshEntries,
44
- expression: Types.custom<string | PlotSource, PlotSource>("", {
45
- resolve: readSource,
46
- lerp: lerpSource,
47
- kind: "spaceCurveEquation",
48
- }),
49
- /** The span of `t` that is drawn; 0 to 1 when left out. */
50
- domain: domainEntry(0, 1),
51
- params: Types.custom<ExpressionScope>({}, { lerp: lerpParams }),
52
- /** How many points the curve is drawn through. */
53
- samples: Types.number(160, { min: 2, max: 2000, step: 1, animatable: false }),
54
- /** The radius of the line, in scene units. */
55
- thickness: Types.number(0.04, { min: 0.001, step: 0.005 }),
56
- /** The part of the curve that is drawn, as shares of its length in `t`. */
57
- start: Types.number(0, { min: 0, max: 1, step: 0.01 }),
58
- end: Types.number(1, { min: 0, max: 1, step: 0.01 }),
59
- graph: graphEntry(),
60
- };
61
-
62
- type CurveValues = Values<typeof state>;
63
-
64
- /** A curve through a 3D graph, three parts in `t`: `(cos(t), sin(t), t / 4)`. */
65
- export const CurvePlot3d = component3d({
66
- key: "curvePlot3d",
67
- state,
68
- defaults: { fill: PLOT_COLOR },
69
- commands: {
70
- /** Change the equation, the curve bending from the one into the other. */
71
- morphTo: { args: ["expression"] },
72
- /** Animate the equation's named values. */
73
- paramsTo: { args: ["params"] },
74
- /** Animate the span of `t` that is drawn. */
75
- domainTo: { args: ["domain"] },
76
- /** Draw the curve on, or wipe it off: animates `start` and `end`. */
77
- progressTo: { args: ["start", "end"] },
78
- },
79
- render: (ctx, s) => ctx.draw(Material.mesh(s, curveGeometry(s))),
80
- });
81
-
82
- export type CurvePlot3d = NodeOf<typeof CurvePlot3d>;
83
-
84
- function curveGeometry(s: CurveValues): Geometry3D | undefined {
85
- const space = graphOf(s);
86
- if (!space) return undefined;
87
- const points = pointsOf(s, space);
88
- if (points.length < 2) return undefined;
89
- return Geo.tube({ points, radius: s.thickness, segments: [points.length - 1, 8] });
90
- }
91
-
92
- /** The last run drawn for each curve, kept while what it is drawn from is unchanged. */
93
- const runs = new WeakMap<object, { readonly key: string; readonly run: Vector3[] }>();
94
-
95
- function pointsOf(s: CurveValues, space: GraphSpace): Vector3[] {
96
- const { source, blend } = s.expression;
97
- const start = clamp(s.start, 0, 1);
98
- const end = clamp(s.end, 0, 1);
99
- const samples = Math.max(2, Math.round(s.samples));
100
- const key = [source, blend?.from, blend?.t, space.revision, s.domain, JSON.stringify(s.params), start, end, samples].join("|");
101
- const kept = runs.get(s);
102
- if (kept?.key === key) return kept.run;
103
- const run: Vector3[] = [];
104
- runs.set(s, { key, run });
105
- const to = curveOf(s, source);
106
- if (!to || !(end > start)) return run;
107
- const from = blend ? curveOf(s, blend.from) : null;
108
- const steps = Math.max(1, Math.round(samples * (end - start)));
109
- const a: Vector3 = { x: 0, y: 0, z: 0 };
110
- const b: Vector3 = { x: 0, y: 0, z: 0 };
111
- for (let i = 0; i <= steps; i++) {
112
- const u = start + ((end - start) * i) / steps;
113
- if (!to(u, b)) continue;
114
- if (from && blend) {
115
- if (!from(u, a)) continue;
116
- b.x = a.x + (b.x - a.x) * blend.t;
117
- b.y = a.y + (b.y - a.y) * blend.t;
118
- b.z = a.z + (b.z - a.z) * blend.t;
119
- }
120
- run.push(space.offset(b.x, b.y, b.z));
121
- }
122
- return run;
123
- }
124
-
125
- /** `source` as a graph point for a share `u` of its span, or `false` where it has none. */
126
- function curveOf(s: CurveValues, source: string): ((u: number, out: Vector3) => boolean) | null {
127
- const equation = equationOf(source);
128
- if (!equation) return null;
129
- const params = scopeFor(spaceCurveParams(source), s.params);
130
- const [t0, t1] = s.domain ?? [0, 1];
131
- const { x, y, z } = equation;
132
- return (u, out) => {
133
- const t = t0 + (t1 - t0) * u;
134
- out.x = x(t, 0, params);
135
- out.y = y(t, 0, params);
136
- out.z = z(t, 0, params);
137
- return Number.isFinite(out.x) && Number.isFinite(out.y) && Number.isFinite(out.z);
138
- };
139
- }
140
-
141
- function equationOf(source: string): SpaceCurveEquation | null {
142
- const equation = parseSpaceCurve(source, spaceCurveParams(source));
143
- return equation instanceof Error ? null : equation;
144
- }
1
+ import {
2
+ component3d,
3
+ Geo,
4
+ Graphics3D,
5
+ Material,
6
+ Types,
7
+ type Geometry3D,
8
+ type Mesh3DProps,
9
+ type NodeOf,
10
+ type Values,
11
+ type Vector3,
12
+ // Named so the emitted declaration reaches these through the sdk, not core.
13
+ type Blend3D as _Blend3D,
14
+ type Color as _Color,
15
+ type Faces3D as _Faces3D,
16
+ type Fill3D as _Fill3D,
17
+ type Material3D as _Material3D,
18
+ type Node3D as _Node3D,
19
+ type Node3DProps as _Node3DProps,
20
+ type NormalizedColor as _NormalizedColor,
21
+ type Shading3D as _Shading3D,
22
+ type Texture3D as _Texture3D,
23
+ } from "@motionscript/sdk";
24
+ import type { Interval } from "../kit/coords";
25
+ import { parseSpaceCurve, spaceCurveParams, type SpaceCurveEquation } from "../kit/equation3d";
26
+ import type { ExpressionScope } from "../kit/expression";
27
+ import { PLOT_COLOR } from "../kit/furniture";
28
+ import { clamp } from "../kit/geometry";
29
+ import { lerpParams, lerpSource, readSource, scopeFor, type PlotSource } from "../kit/source";
30
+ import { domainEntry, meshEntries } from "./entries";
31
+ import { graphEntry, graphOf, type GraphSpace } from "./space";
32
+
33
+ export interface CurvePlot3dProps extends Mesh3DProps {
34
+ expression?: string | PlotSource;
35
+ domain?: Interval | null;
36
+ params?: ExpressionScope;
37
+ samples?: number;
38
+ thickness?: number;
39
+ start?: number;
40
+ end?: number;
41
+ }
42
+
43
+ const state = {
44
+ ...meshEntries,
45
+ expression: Types.custom<string | PlotSource, PlotSource>("", {
46
+ resolve: readSource,
47
+ lerp: lerpSource,
48
+ kind: "spaceCurveEquation",
49
+ }),
50
+ /** The span of `t` that is drawn; 0 to 1 when left out. */
51
+ domain: domainEntry(0, 1),
52
+ params: Types.custom<ExpressionScope>({}, { lerp: lerpParams }),
53
+ /** How many points the curve is drawn through. */
54
+ samples: Types.number(160, { min: 2, max: 2000, step: 1, animatable: false }),
55
+ /** The radius of the line, in scene units. */
56
+ thickness: Types.number(0.04, { min: 0.001, step: 0.005 }),
57
+ /** The part of the curve that is drawn, as shares of its length in `t`. */
58
+ start: Types.number(0, { min: 0, max: 1, step: 0.01 }),
59
+ end: Types.number(1, { min: 0, max: 1, step: 0.01 }),
60
+ graph: graphEntry(),
61
+ };
62
+
63
+ type CurveValues = Values<typeof state>;
64
+
65
+ /** A curve through a 3D graph, three parts in `t`: `(cos(t), sin(t), t / 4)`. */
66
+ export const CurvePlot3d = component3d({
67
+ key: "curvePlot3d",
68
+ state,
69
+ defaults: { fill: PLOT_COLOR },
70
+ commands: {
71
+ /** Change the equation, the curve bending from the one into the other. */
72
+ morphTo: { args: ["expression"] },
73
+ /** Animate the equation's named values. */
74
+ paramsTo: { args: ["params"] },
75
+ /** Animate the span of `t` that is drawn. */
76
+ domainTo: { args: ["domain"] },
77
+ /** Draw the curve on, or wipe it off: animates `start` and `end`. */
78
+ progressTo: { args: ["start", "end"] },
79
+ },
80
+ // Declaring, its paint on a stand-in: only a material loads, and the curve's points are not sampled.
81
+ render: (ctx, s) => ctx.draw(ctx.declaring ? new Graphics3D().plane(Material.shorthand(s)) : Material.mesh(s, curveGeometry(s))),
82
+ });
83
+
84
+ export type CurvePlot3d = NodeOf<typeof CurvePlot3d>;
85
+
86
+ function curveGeometry(s: CurveValues): Geometry3D | undefined {
87
+ const space = graphOf(s);
88
+ if (!space) return undefined;
89
+ const points = pointsOf(s, space);
90
+ if (points.length < 2) return undefined;
91
+ return Geo.tube({ points, radius: s.thickness, segments: [points.length - 1, 8] });
92
+ }
93
+
94
+ /** The last run drawn for each curve, kept while what it is drawn from is unchanged. */
95
+ const runs = new WeakMap<object, { readonly key: string; readonly run: Vector3[] }>();
96
+
97
+ function pointsOf(s: CurveValues, space: GraphSpace): Vector3[] {
98
+ const { source, blend } = s.expression;
99
+ const start = clamp(s.start, 0, 1);
100
+ const end = clamp(s.end, 0, 1);
101
+ const samples = Math.max(2, Math.round(s.samples));
102
+ const key = [source, blend?.from, blend?.t, space.revision, s.domain, JSON.stringify(s.params), start, end, samples].join("|");
103
+ const kept = runs.get(s);
104
+ if (kept?.key === key) return kept.run;
105
+ const run: Vector3[] = [];
106
+ runs.set(s, { key, run });
107
+ const to = curveOf(s, source);
108
+ if (!to || !(end > start)) return run;
109
+ const from = blend ? curveOf(s, blend.from) : null;
110
+ const steps = Math.max(1, Math.round(samples * (end - start)));
111
+ const a: Vector3 = { x: 0, y: 0, z: 0 };
112
+ const b: Vector3 = { x: 0, y: 0, z: 0 };
113
+ for (let i = 0; i <= steps; i++) {
114
+ const u = start + ((end - start) * i) / steps;
115
+ if (!to(u, b)) continue;
116
+ if (from && blend) {
117
+ if (!from(u, a)) continue;
118
+ b.x = a.x + (b.x - a.x) * blend.t;
119
+ b.y = a.y + (b.y - a.y) * blend.t;
120
+ b.z = a.z + (b.z - a.z) * blend.t;
121
+ }
122
+ run.push(space.offset(b.x, b.y, b.z));
123
+ }
124
+ return run;
125
+ }
126
+
127
+ /** `source` as a graph point for a share `u` of its span, or `false` where it has none. */
128
+ function curveOf(s: CurveValues, source: string): ((u: number, out: Vector3) => boolean) | null {
129
+ const equation = equationOf(source);
130
+ if (!equation) return null;
131
+ const params = scopeFor(spaceCurveParams(source), s.params);
132
+ const [t0, t1] = s.domain ?? [0, 1];
133
+ const { x, y, z } = equation;
134
+ return (u, out) => {
135
+ const t = t0 + (t1 - t0) * u;
136
+ out.x = x(t, 0, params);
137
+ out.y = y(t, 0, params);
138
+ out.z = z(t, 0, params);
139
+ return Number.isFinite(out.x) && Number.isFinite(out.y) && Number.isFinite(out.z);
140
+ };
141
+ }
142
+
143
+ function equationOf(source: string): SpaceCurveEquation | null {
144
+ const equation = parseSpaceCurve(source, spaceCurveParams(source));
145
+ return equation instanceof Error ? null : equation;
146
+ }
@@ -1,42 +1,42 @@
1
- import {
2
- Material,
3
- Types,
4
- type EntryOptions,
5
- type Geometry3D,
6
- type Type,
7
- // Named so the emitted declaration reaches these through the sdk, not core.
8
- type Blend3D as _Blend3D,
9
- type Color as _Color,
10
- type Faces3D as _Faces3D,
11
- type Fill3D as _Fill3D,
12
- type Material3D as _Material3D,
13
- type NormalizedColor as _NormalizedColor,
14
- type Shading3D as _Shading3D,
15
- type Texture3D as _Texture3D,
16
- } from "@motionscript/sdk";
17
- import type { Interval } from "../kit/coords";
18
- import { lerpDomain } from "../kit/source";
19
-
20
- type MeshState = { readonly geometry: Type<Geometry3D | undefined> } & typeof Material.state;
21
- // Restated key by key, so a mark spreading these names each entry's type through the sdk: spread
22
- // as inferred, a mark's declaration copied `emission`'s colour types from core's own.
23
- type Restated<T> = { [K in Extract<keyof T, string>]: T[K] };
24
-
25
- /**
26
- * A mesh's entries, as a 3D mark draws with them: its surface, and the
27
- * `geometry` a mesh takes, which a mark builds itself and never reads.
28
- */
29
- export const meshEntries: Restated<MeshState> = {
30
- geometry: Types.custom<Geometry3D | undefined>(undefined),
31
- ...Material.state,
32
- };
33
-
34
- interface RangeHints extends EntryOptions {
35
- readonly defaultMin: number;
36
- readonly defaultMax: number;
37
- }
38
-
39
- /** A span of a variable, unset until given: what a host offers as a range from `defaultMin` to `defaultMax`. */
40
- export function domainEntry(defaultMin: number, defaultMax: number): Type<Interval | null> {
41
- return Types.custom<Interval | null, Interval | null, RangeHints>(null, { lerp: lerpDomain, kind: "range", defaultMin, defaultMax });
42
- }
1
+ import {
2
+ Material,
3
+ Types,
4
+ type EntryOptions,
5
+ type Geometry3D,
6
+ type Type,
7
+ // Named so the emitted declaration reaches these through the sdk, not core.
8
+ type Blend3D as _Blend3D,
9
+ type Color as _Color,
10
+ type Faces3D as _Faces3D,
11
+ type Fill3D as _Fill3D,
12
+ type Material3D as _Material3D,
13
+ type NormalizedColor as _NormalizedColor,
14
+ type Shading3D as _Shading3D,
15
+ type Texture3D as _Texture3D,
16
+ } from "@motionscript/sdk";
17
+ import type { Interval } from "../kit/coords";
18
+ import { lerpDomain } from "../kit/source";
19
+
20
+ type MeshState = { readonly geometry: Type<Geometry3D | undefined> } & typeof Material.state;
21
+ // Restated key by key, so a mark spreading these names each entry's type through the sdk: spread
22
+ // as inferred, a mark's declaration copied `emission`'s colour types from core's own.
23
+ type Restated<T> = { [K in Extract<keyof T, string>]: T[K] };
24
+
25
+ /**
26
+ * A mesh's entries, as a 3D mark draws with them: its surface, and the
27
+ * `geometry` a mesh takes, which a mark builds itself and never reads.
28
+ */
29
+ export const meshEntries: Restated<MeshState> = {
30
+ geometry: Types.custom<Geometry3D | undefined>(undefined),
31
+ ...Material.state,
32
+ };
33
+
34
+ interface RangeHints extends EntryOptions {
35
+ readonly defaultMin: number;
36
+ readonly defaultMax: number;
37
+ }
38
+
39
+ /** A span of a variable, unset until given: what a host offers as a range from `defaultMin` to `defaultMax`. */
40
+ export function domainEntry(defaultMin: number, defaultMax: number): Type<Interval | null> {
41
+ return Types.custom<Interval | null, Interval | null, RangeHints>(null, { lerp: lerpDomain, kind: "range", defaultMin, defaultMax });
42
+ }
@@ -1,50 +1,52 @@
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
- }
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
+ // Its pixels never change, so the renderer rasterizes it once rather than every frame.
36
+ const texture: SurfaceTexture3D = { ...Tex.surface(source, { width, height: CARD_HEIGHT }), static: true, identity: `label3d:${key}` };
37
+ const made = { texture, width };
38
+ cards.set(key, made);
39
+ return made;
40
+ }
41
+
42
+ /** Draw `text` centred on `at`, a local point. */
43
+ export function drawLabel3D(g3: Graphics3D, text: string, at: Vector3, style: LabelStyle3D): void {
44
+ if (text === "" || !(style.size > 0)) return;
45
+ const { texture, width } = card(text, style);
46
+ const height = (style.size * CARD_HEIGHT) / EM;
47
+ g3.sprite({
48
+ material: Mat.sprite({ map: texture, transparent: true, depthWrite: false }),
49
+ position: at,
50
+ scale: [(height * width) / CARD_HEIGHT, height, 1],
51
+ });
52
+ }
@@ -1,63 +1,63 @@
1
- import {
2
- component3d,
3
- Geo,
4
- Material,
5
- Types,
6
- type Color,
7
- type Mesh3DProps,
8
- type NodeOf,
9
- // Named so the emitted declaration reaches these through the sdk, not core.
10
- type Blend3D as _Blend3D,
11
- type Color as _Color,
12
- type Faces3D as _Faces3D,
13
- type Fill3D as _Fill3D,
14
- type Geometry3D as _Geometry3D,
15
- type Material3D as _Material3D,
16
- type Node3D as _Node3D,
17
- type Node3DProps as _Node3DProps,
18
- type NormalizedColor as _NormalizedColor,
19
- type Shading3D as _Shading3D,
20
- type Texture3D as _Texture3D,
21
- } from "@motionscript/sdk";
22
- import { meshEntries } from "./entries";
23
- import { drawLabel3D } from "./label3d";
24
-
25
- export interface PlotPoint3dProps extends Mesh3DProps {
26
- radius?: number;
27
- label?: string;
28
- labelSize?: number;
29
- labelColor?: Color;
30
- fontFamily?: string;
31
- }
32
-
33
- /**
34
- * A dot at graph coordinates in a 3D graph, with an optional label. Put inside
35
- * a surface it rides it: its `x` and `y` say where, and its height is the surface's.
36
- */
37
- export const PlotPoint3d = component3d({
38
- key: "plotPoint3d",
39
- state: {
40
- ...meshEntries,
41
- /** In scene units, so a point is the same size whatever the axes show. */
42
- radius: Types.number(0.1, { min: 0, step: 0.01 }),
43
- label: Types.text("", { animatable: false }),
44
- /** The height of the label's text, in scene units. */
45
- labelSize: Types.number(0.26, { min: 0.01, step: 0.01 }),
46
- labelColor: Types.color("#e5e7eb"),
47
- fontFamily: Types.text("", { animatable: false }),
48
- },
49
- load: (loader, s) => {
50
- if (s.fontFamily !== "") loader.font(s.fontFamily);
51
- },
52
- render: (ctx, s) => {
53
- const g3 = Material.mesh(s, s.radius > 0 ? Geo.sphere({ radius: s.radius, segments: [24, 16] }) : undefined);
54
- drawLabel3D(g3, s.label, { x: 0, y: s.radius + s.labelSize * 0.85, z: 0 }, {
55
- size: s.labelSize,
56
- color: s.labelColor,
57
- fontFamily: s.fontFamily,
58
- });
59
- ctx.draw(g3);
60
- },
61
- });
62
-
63
- export type PlotPoint3d = NodeOf<typeof PlotPoint3d>;
1
+ import {
2
+ component3d,
3
+ Geo,
4
+ Material,
5
+ Types,
6
+ type Color,
7
+ type Mesh3DProps,
8
+ type NodeOf,
9
+ // Named so the emitted declaration reaches these through the sdk, not core.
10
+ type Blend3D as _Blend3D,
11
+ type Color as _Color,
12
+ type Faces3D as _Faces3D,
13
+ type Fill3D as _Fill3D,
14
+ type Geometry3D as _Geometry3D,
15
+ type Material3D as _Material3D,
16
+ type Node3D as _Node3D,
17
+ type Node3DProps as _Node3DProps,
18
+ type NormalizedColor as _NormalizedColor,
19
+ type Shading3D as _Shading3D,
20
+ type Texture3D as _Texture3D,
21
+ } from "@motionscript/sdk";
22
+ import { meshEntries } from "./entries";
23
+ import { drawLabel3D } from "./label3d";
24
+
25
+ export interface PlotPoint3dProps extends Mesh3DProps {
26
+ radius?: number;
27
+ label?: string;
28
+ labelSize?: number;
29
+ labelColor?: Color;
30
+ fontFamily?: string;
31
+ }
32
+
33
+ /**
34
+ * A dot at graph coordinates in a 3D graph, with an optional label. Put inside
35
+ * a surface it rides it: its `x` and `y` say where, and its height is the surface's.
36
+ */
37
+ export const PlotPoint3d = component3d({
38
+ key: "plotPoint3d",
39
+ state: {
40
+ ...meshEntries,
41
+ /** In scene units, so a point is the same size whatever the axes show. */
42
+ radius: Types.number(0.1, { min: 0, step: 0.01 }),
43
+ label: Types.text("", { animatable: false }),
44
+ /** The height of the label's text, in scene units. */
45
+ labelSize: Types.number(0.26, { min: 0.01, step: 0.01 }),
46
+ labelColor: Types.color("#e5e7eb"),
47
+ fontFamily: Types.text("", { animatable: false }),
48
+ },
49
+ load: (loader, s) => {
50
+ if (s.fontFamily !== "") loader.font(s.fontFamily);
51
+ },
52
+ render: (ctx, s) => {
53
+ const g3 = Material.mesh(s, s.radius > 0 ? Geo.sphere({ radius: s.radius, segments: [24, 16] }) : undefined);
54
+ drawLabel3D(g3, s.label, { x: 0, y: s.radius + s.labelSize * 0.85, z: 0 }, {
55
+ size: s.labelSize,
56
+ color: s.labelColor,
57
+ fontFamily: s.fontFamily,
58
+ });
59
+ ctx.draw(g3);
60
+ },
61
+ });
62
+
63
+ export type PlotPoint3d = NodeOf<typeof PlotPoint3d>;