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

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 (233) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +39 -33
  3. package/dist/annotate/angle-mark.d.ts +77 -27
  4. package/dist/annotate/angle-mark.d.ts.map +1 -1
  5. package/dist/annotate/angle-mark.js +80 -115
  6. package/dist/annotate/angle-mark.js.map +1 -1
  7. package/dist/annotate/curly-brace.d.ts +94 -25
  8. package/dist/annotate/curly-brace.d.ts.map +1 -1
  9. package/dist/annotate/curly-brace.js +85 -100
  10. package/dist/annotate/curly-brace.js.map +1 -1
  11. package/dist/annotate/emphasis-mark.d.ts +69 -9
  12. package/dist/annotate/emphasis-mark.d.ts.map +1 -1
  13. package/dist/annotate/emphasis-mark.js +33 -34
  14. package/dist/annotate/emphasis-mark.js.map +1 -1
  15. package/dist/browser/chunks/chunk-ABYBW64J.js +2 -0
  16. package/dist/browser/chunks/chunk-ABYBW64J.js.map +7 -0
  17. package/dist/browser/chunks/chunk-K77KLQX4.js +2 -0
  18. package/dist/browser/chunks/chunk-K77KLQX4.js.map +7 -0
  19. package/dist/browser/index.js +1 -1
  20. package/dist/browser/index.js.map +3 -3
  21. package/dist/browser/kit.js +1 -1
  22. package/dist/browser/manifest.json +1 -1
  23. package/dist/browser/tex.js +1 -1
  24. package/dist/browser/tex.js.map +2 -2
  25. package/dist/engine.d.ts +26 -19
  26. package/dist/engine.d.ts.map +1 -1
  27. package/dist/engine.js +17 -14
  28. package/dist/engine.js.map +1 -1
  29. package/dist/kit/component.d.ts +51 -0
  30. package/dist/kit/component.d.ts.map +1 -0
  31. package/dist/kit/component.js +30 -0
  32. package/dist/kit/component.js.map +1 -0
  33. package/dist/kit/curve.d.ts +22 -11
  34. package/dist/kit/curve.d.ts.map +1 -1
  35. package/dist/kit/curve.js +25 -18
  36. package/dist/kit/curve.js.map +1 -1
  37. package/dist/kit/field-mark.d.ts +56 -35
  38. package/dist/kit/field-mark.d.ts.map +1 -1
  39. package/dist/kit/field-mark.js +100 -120
  40. package/dist/kit/field-mark.js.map +1 -1
  41. package/dist/kit/follow.d.ts +67 -29
  42. package/dist/kit/follow.d.ts.map +1 -1
  43. package/dist/kit/follow.js +114 -110
  44. package/dist/kit/follow.js.map +1 -1
  45. package/dist/kit/furniture.d.ts +3 -1
  46. package/dist/kit/furniture.d.ts.map +1 -1
  47. package/dist/kit/furniture.js +5 -0
  48. package/dist/kit/furniture.js.map +1 -1
  49. package/dist/kit/graph.d.ts +46 -0
  50. package/dist/kit/graph.d.ts.map +1 -0
  51. package/dist/kit/graph.js +90 -0
  52. package/dist/kit/graph.js.map +1 -0
  53. package/dist/kit/mark.d.ts +64 -39
  54. package/dist/kit/mark.d.ts.map +1 -1
  55. package/dist/kit/mark.js +80 -96
  56. package/dist/kit/mark.js.map +1 -1
  57. package/dist/kit/place.d.ts +10 -8
  58. package/dist/kit/place.d.ts.map +1 -1
  59. package/dist/kit/place.js +28 -30
  60. package/dist/kit/place.js.map +1 -1
  61. package/dist/kit/system.d.ts +27 -29
  62. package/dist/kit/system.d.ts.map +1 -1
  63. package/dist/kit/system.js +62 -58
  64. package/dist/kit/system.js.map +1 -1
  65. package/dist/kit/withdrawn.d.ts +3 -3
  66. package/dist/kit/withdrawn.js +3 -3
  67. package/dist/kit/withdrawn.js.map +1 -1
  68. package/dist/marks/curve-area.d.ts +37 -18
  69. package/dist/marks/curve-area.d.ts.map +1 -1
  70. package/dist/marks/curve-area.js +50 -73
  71. package/dist/marks/curve-area.js.map +1 -1
  72. package/dist/marks/drop-lines.d.ts +45 -10
  73. package/dist/marks/drop-lines.d.ts.map +1 -1
  74. package/dist/marks/drop-lines.js +37 -52
  75. package/dist/marks/drop-lines.js.map +1 -1
  76. package/dist/marks/plot-point.d.ts +56 -20
  77. package/dist/marks/plot-point.d.ts.map +1 -1
  78. package/dist/marks/plot-point.js +65 -91
  79. package/dist/marks/plot-point.js.map +1 -1
  80. package/dist/marks/riemann-sum.d.ts +47 -26
  81. package/dist/marks/riemann-sum.d.ts.map +1 -1
  82. package/dist/marks/riemann-sum.js +57 -85
  83. package/dist/marks/riemann-sum.js.map +1 -1
  84. package/dist/marks/tangent-line.d.ts +58 -14
  85. package/dist/marks/tangent-line.d.ts.map +1 -1
  86. package/dist/marks/tangent-line.js +73 -87
  87. package/dist/marks/tangent-line.js.map +1 -1
  88. package/dist/marks/vector-arrow.d.ts +48 -20
  89. package/dist/marks/vector-arrow.d.ts.map +1 -1
  90. package/dist/marks/vector-arrow.js +58 -78
  91. package/dist/marks/vector-arrow.js.map +1 -1
  92. package/dist/nodes.d.ts +13 -5
  93. package/dist/nodes.d.ts.map +1 -1
  94. package/dist/nodes.js +0 -6
  95. package/dist/nodes.js.map +1 -1
  96. package/dist/plots/function-plot.d.ts +135 -43
  97. package/dist/plots/function-plot.d.ts.map +1 -1
  98. package/dist/plots/function-plot.js +146 -177
  99. package/dist/plots/function-plot.js.map +1 -1
  100. package/dist/plots/points-plot.d.ts +55 -19
  101. package/dist/plots/points-plot.d.ts.map +1 -1
  102. package/dist/plots/points-plot.js +31 -58
  103. package/dist/plots/points-plot.js.map +1 -1
  104. package/dist/plots/stream-lines.d.ts +60 -19
  105. package/dist/plots/stream-lines.d.ts.map +1 -1
  106. package/dist/plots/stream-lines.js +60 -79
  107. package/dist/plots/stream-lines.js.map +1 -1
  108. package/dist/plots/traced-path.d.ts +54 -15
  109. package/dist/plots/traced-path.d.ts.map +1 -1
  110. package/dist/plots/traced-path.js +33 -48
  111. package/dist/plots/traced-path.js.map +1 -1
  112. package/dist/plots/vector-field.d.ts +48 -11
  113. package/dist/plots/vector-field.d.ts.map +1 -1
  114. package/dist/plots/vector-field.js +50 -64
  115. package/dist/plots/vector-field.js.map +1 -1
  116. package/dist/structures/network-graph.d.ts +140 -49
  117. package/dist/structures/network-graph.d.ts.map +1 -1
  118. package/dist/structures/network-graph.js +168 -217
  119. package/dist/structures/network-graph.js.map +1 -1
  120. package/dist/structures/sample-space.d.ts +90 -38
  121. package/dist/structures/sample-space.d.ts.map +1 -1
  122. package/dist/structures/sample-space.js +132 -170
  123. package/dist/structures/sample-space.js.map +1 -1
  124. package/dist/systems/axes2d.d.ts +159 -37
  125. package/dist/systems/axes2d.d.ts.map +1 -1
  126. package/dist/systems/axes2d.js +147 -183
  127. package/dist/systems/axes2d.js.map +1 -1
  128. package/dist/systems/number-line.d.ts +80 -28
  129. package/dist/systems/number-line.d.ts.map +1 -1
  130. package/dist/systems/number-line.js +82 -118
  131. package/dist/systems/number-line.js.map +1 -1
  132. package/dist/systems/number-plane.d.ts +140 -31
  133. package/dist/systems/number-plane.d.ts.map +1 -1
  134. package/dist/systems/number-plane.js +142 -149
  135. package/dist/systems/number-plane.js.map +1 -1
  136. package/dist/systems/polar-plane.d.ts +76 -26
  137. package/dist/systems/polar-plane.d.ts.map +1 -1
  138. package/dist/systems/polar-plane.js +107 -133
  139. package/dist/systems/polar-plane.js.map +1 -1
  140. package/dist/tex/index.d.ts.map +1 -1
  141. package/dist/tex/index.js +1 -0
  142. package/dist/tex/index.js.map +1 -1
  143. package/dist/tex/matrix.d.ts +88 -18
  144. package/dist/tex/matrix.d.ts.map +1 -1
  145. package/dist/tex/matrix.js +33 -50
  146. package/dist/tex/matrix.js.map +1 -1
  147. package/dist/tex/node.d.ts +84 -201
  148. package/dist/tex/node.d.ts.map +1 -1
  149. package/dist/tex/node.js +23 -593
  150. package/dist/tex/node.js.map +1 -1
  151. package/dist/tex/typeset.d.ts +133 -0
  152. package/dist/tex/typeset.d.ts.map +1 -0
  153. package/dist/tex/typeset.js +403 -0
  154. package/dist/tex/typeset.js.map +1 -0
  155. package/dist/three/axes3d.d.ts +214 -54
  156. package/dist/three/axes3d.d.ts.map +1 -1
  157. package/dist/three/axes3d.js +163 -204
  158. package/dist/three/axes3d.js.map +1 -1
  159. package/dist/three/curve-plot3d.d.ts +108 -42
  160. package/dist/three/curve-plot3d.d.ts.map +1 -1
  161. package/dist/three/curve-plot3d.js +97 -132
  162. package/dist/three/curve-plot3d.js.map +1 -1
  163. package/dist/three/entries.d.ts +17 -0
  164. package/dist/three/entries.d.ts.map +1 -0
  165. package/dist/three/entries.js +15 -0
  166. package/dist/three/entries.js.map +1 -0
  167. package/dist/three/plot-point3d.d.ts +85 -12
  168. package/dist/three/plot-point3d.d.ts.map +1 -1
  169. package/dist/three/plot-point3d.js +27 -53
  170. package/dist/three/plot-point3d.js.map +1 -1
  171. package/dist/three/space.d.ts +17 -2
  172. package/dist/three/space.d.ts.map +1 -1
  173. package/dist/three/space.js +19 -8
  174. package/dist/three/space.js.map +1 -1
  175. package/dist/three/surface-plot3d.d.ts +176 -49
  176. package/dist/three/surface-plot3d.d.ts.map +1 -1
  177. package/dist/three/surface-plot3d.js +227 -261
  178. package/dist/three/surface-plot3d.js.map +1 -1
  179. package/dist/three/vector-arrow3d.d.ts +87 -16
  180. package/dist/three/vector-arrow3d.d.ts.map +1 -1
  181. package/dist/three/vector-arrow3d.js +48 -67
  182. package/dist/three/vector-arrow3d.js.map +1 -1
  183. package/package.json +4 -4
  184. package/src/annotate/angle-mark.ts +95 -105
  185. package/src/annotate/curly-brace.ts +107 -85
  186. package/src/annotate/emphasis-mark.ts +55 -26
  187. package/src/engine.ts +35 -21
  188. package/src/kit/component.ts +57 -0
  189. package/src/kit/curve.ts +38 -20
  190. package/src/kit/field-mark.ts +124 -108
  191. package/src/kit/follow.ts +146 -120
  192. package/src/kit/furniture.ts +6 -1
  193. package/src/kit/graph.ts +116 -0
  194. package/src/kit/mark.ts +135 -114
  195. package/src/kit/place.ts +32 -56
  196. package/src/kit/system.ts +77 -71
  197. package/src/kit/withdrawn.ts +3 -3
  198. package/src/marks/curve-area.ts +65 -65
  199. package/src/marks/drop-lines.ts +50 -42
  200. package/src/marks/plot-point.ts +77 -82
  201. package/src/marks/riemann-sum.ts +73 -72
  202. package/src/marks/tangent-line.ts +87 -71
  203. package/src/marks/vector-arrow.ts +73 -73
  204. package/src/nodes.ts +41 -5
  205. package/src/plots/function-plot.ts +155 -171
  206. package/src/plots/points-plot.ts +46 -50
  207. package/src/plots/stream-lines.ts +75 -66
  208. package/src/plots/traced-path.ts +51 -33
  209. package/src/plots/vector-field.ts +68 -49
  210. package/src/structures/network-graph.ts +168 -185
  211. package/src/structures/sample-space.ts +149 -148
  212. package/src/systems/axes2d.ts +165 -156
  213. package/src/systems/number-line.ts +91 -92
  214. package/src/systems/number-plane.ts +149 -120
  215. package/src/systems/polar-plane.ts +117 -116
  216. package/src/tex/index.ts +2 -1
  217. package/src/tex/matrix.ts +62 -40
  218. package/src/tex/node.ts +54 -621
  219. package/src/tex/typeset.ts +461 -0
  220. package/src/three/axes3d.ts +187 -160
  221. package/src/three/curve-plot3d.ts +107 -110
  222. package/src/three/entries.ts +42 -0
  223. package/src/three/plot-point3d.ts +42 -45
  224. package/src/three/space.ts +34 -9
  225. package/src/three/surface-plot3d.ts +312 -323
  226. package/src/three/vector-arrow3d.ts +65 -54
  227. package/dist/browser/chunks/chunk-3GJB6AVO.js +0 -2
  228. package/dist/browser/chunks/chunk-3GJB6AVO.js.map +0 -7
  229. package/dist/browser/chunks/chunk-D5WHCVBC.js +0 -2
  230. package/dist/browser/chunks/chunk-D5WHCVBC.js.map +0 -7
  231. package/dist/browser/chunks/chunk-WL4KEX7Y.js +0 -2
  232. package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +0 -7
  233. package/src/kit/placement.ts +0 -8
@@ -1,281 +1,247 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import { colorOps, command, Geo, Graphics3D, Mat, Mesh3D, node, property, } from "@motionscript/sdk";
1
+ import { colorOps, component3d, Geo, Graphics3D, Mat, Material, Types, } from "@motionscript/sdk";
8
2
  import { parseSurface, surfaceParams } from "../kit/equation3d.js";
9
3
  import { PLOT_COLOR } from "../kit/furniture.js";
10
- import { clamp, stated } from "../kit/geometry.js";
4
+ import { clamp } from "../kit/geometry.js";
11
5
  import { colorAt, lerpScale, readScale } from "../kit/scale.js";
12
- import { lerpDomain, lerpParams, lerpSource, readSource, revisionOf, scopeFor } from "../kit/source.js";
13
- import { spaceOf } from "./space.js";
6
+ import { lerpParams, lerpSource, readSource, revisionOf, scopeFor } from "../kit/source.js";
7
+ import { domainEntry, meshEntries } from "./entries.js";
8
+ import { graphEntry, graphOf } from "./space.js";
9
+ const state = {
10
+ ...meshEntries,
11
+ expression: Types.custom("", {
12
+ resolve: readSource,
13
+ lerp: lerpSource,
14
+ kind: "surfaceEquation",
15
+ }),
16
+ /**
17
+ * The span of each variable that is drawn: x and y for a height, which run
18
+ * the width of the axes when left out, u and v for a parametric surface,
19
+ * which run from 0 to 1.
20
+ */
21
+ uDomain: domainEntry(-3, 3),
22
+ vDomain: domainEntry(-3, 3),
23
+ /** The equation's named values: any name in it that is not a variable, a function or a constant. */
24
+ params: Types.custom({}, { lerp: lerpParams }),
25
+ /** How finely the surface is sampled, each way. */
26
+ segments: Types.number(48, { min: 2, max: 256, step: 1, animatable: false }),
27
+ /** Colours by height, lowest first, over the axes' z range. Empty paints the surface in its fill. */
28
+ colorScale: Types.custom([], { resolve: readScale, lerp: lerpScale, kind: "vector" }),
29
+ /** How many lines are drawn across the surface each way; zero leaves it bare. */
30
+ meshLines: Types.number(12, { min: 0, max: 64, step: 1, animatable: false }),
31
+ meshColor: Types.custom("#0b1220", { kind: "color" }),
32
+ /** The opacity of the surface's shadow, laid flat on the floor of the axes and coloured by height; zero draws none. */
33
+ floorShadow: Types.number(0, { min: 0, max: 1, step: 0.01 }),
34
+ graph: graphEntry(),
35
+ };
14
36
  /**
15
37
  * A surface in a 3D graph: a height over x and y (`sin(x) * cos(y)`), or three
16
38
  * parts in u and v. A point put inside it rides it.
17
39
  */
18
- let SurfacePlot3d = class SurfacePlot3d extends Mesh3D {
19
- constructor(props) {
20
- super(props ?? {});
21
- if (props?.fill === undefined)
22
- this.applyProp("fill", PLOT_COLOR);
23
- }
24
- /** Change the equation, the surface bending from the one into the other. */
25
- morphTo(args) {
26
- return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
27
- }
28
- /** Animate the equation's named values. */
29
- paramsTo(args) {
30
- return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
31
- }
32
- /** Animate the spans that are drawn. */
33
- domainTo(args) {
34
- return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
35
- }
36
- /**
37
- * The surface's height above its own origin at graph `(x, y)`, or `NaN`
38
- * where it has none or is not a height. What a point riding it reads.
39
- */
40
- valueAt(x, y) {
41
- const { source, blend } = this.expression;
42
- const value = this._heightOf(source, x, y);
43
- if (!blend)
44
- return value;
45
- const from = this._heightOf(blend.from, x, y);
46
- return from + (value - from) * blend.t;
47
- }
48
- /** @internal */
49
- _spaceOf() {
50
- const space = spaceOf(this);
51
- if (!space)
52
- return null;
53
- return {
54
- toLocal: (x, y, z) => {
55
- const height = this.valueAt(x, y);
56
- return space.offset(x, y, Number.isFinite(height) ? height : z);
57
- },
58
- };
59
- }
60
- buildGeometry() {
61
- const space = spaceOf(this);
62
- const sample = space && this._sampler(space);
63
- if (!space || !sample)
64
- return undefined;
65
- const scale = this.colorScale;
66
- const point = { x: 0, y: 0, z: 0 };
67
- const low = space.offset(0, 0, space.zRange[0] - this.z).y;
68
- const high = space.offset(0, 0, space.zRange[1] - this.z).y;
69
- return Geo.parametric({
70
- segments: Math.max(2, Math.round(this.segments)),
71
- vertex: (u, v) => {
72
- sample(u, v, point);
73
- return space.offset(point.x, point.y, point.z);
74
- },
75
- ...(scale.length > 0
76
- ? { color: (_u, _v, position) => colorAt(scale, high > low ? (position.y - low) / (high - low) : 0) }
77
- : {}),
78
- computeNormals: true,
79
- revision: revisionOf(`${this._key(space)}|${JSON.stringify(scale)}`),
80
- });
81
- }
82
- materialShorthand() {
83
- const own = super.materialShorthand();
84
- const scaled = this.colorScale.length > 0;
85
- // A surface is seen from both sides; a vertex colour multiplies the fill, so a scale is shown on white.
86
- return { faces: "both", ...own, ...(scaled ? { vertexColors: true, fill: "white" } : {}) };
87
- }
88
- buildGraphics3D() {
89
- const g3 = new Graphics3D();
90
- const space = spaceOf(this);
91
- const lines = space ? this._meshLines(space) : [];
92
- for (const op of super.buildGraphics3D().ops()) {
93
- if (op.kind !== "mesh")
94
- continue;
95
- g3.mesh(op.geometry, mapMaterials(op.material, (material) => ({
96
- ...material,
97
- // Pushed back a hair so the lines drawn on it are not fought for, and blended rather than a hole when faded.
98
- ...(lines.length > 0 ? { polygonOffset: true, polygonOffsetFactor: 1, polygonOffsetUnits: 1 } : {}),
99
- ...((this.opacity ?? 1) < 1 ? { depthWrite: false } : {}),
100
- })), op.transform);
101
- }
102
- if (lines.length > 0) {
103
- const color = colorOps.resolve(this.meshColor);
104
- g3.line({ points: lines, segments: true, stroke: { fill: color }, opacity: color[3] * (this.opacity ?? 1) });
105
- }
106
- const shadow = space && this.floorShadow > 0 ? this._floorShadow(space) : undefined;
107
- if (shadow) {
108
- const scaled = this.colorScale.length > 0;
109
- g3.mesh(shadow, Mat.basic({
110
- color: scaled ? "white" : PLOT_COLOR,
111
- vertexColors: scaled,
112
- opacity: this.floorShadow * (this.opacity ?? 1),
113
- transparent: true,
114
- // Translucent and under everything: writing depth would hide the floor's grid lines behind it.
115
- depthWrite: false,
116
- faces: "both",
117
- }));
118
- }
119
- return g3;
120
- }
121
- _linesKey = "";
122
- _lines = [];
123
- _heightOf(source, x, y) {
124
- const equation = equationOf(source);
125
- if (equation?.kind !== "height")
126
- return NaN;
127
- return equation.f(x, y, scopeFor(surfaceParams(source), this.params));
40
+ export const SurfacePlot3d = component3d({
41
+ key: "surfacePlot3d",
42
+ state,
43
+ defaults: { fill: PLOT_COLOR },
44
+ commands: {
45
+ /** Change the equation, the surface bending from the one into the other. */
46
+ morphTo: { args: ["expression"] },
47
+ /** Animate the equation's named values. */
48
+ paramsTo: { args: ["params"] },
49
+ /** Animate the spans that are drawn. */
50
+ domainTo: { args: ["uDomain", "vDomain"] },
51
+ },
52
+ space: (s) => surfaceSpace(s),
53
+ render: (ctx, s) => ctx.draw(drawSurface(s)),
54
+ });
55
+ /**
56
+ * The surface's height above its own origin at graph `(x, y)`, or `NaN`
57
+ * where it has none or is not a height. What a point riding it reads.
58
+ */
59
+ export function surfaceValueAt(surface, x, y) {
60
+ const { source, blend } = surface.expression;
61
+ const value = heightOf(surface, source, x, y);
62
+ if (!blend)
63
+ return value;
64
+ const from = heightOf(surface, blend.from, x, y);
65
+ return from + (value - from) * blend.t;
66
+ }
67
+ /** A point inside a surface stands on it: its `x` and `y` say where, and its height is the surface's. */
68
+ function surfaceSpace(s) {
69
+ const space = graphOf(s);
70
+ if (!space)
71
+ return null;
72
+ return {
73
+ toLocal: (x, y, z) => {
74
+ const height = surfaceValueAt(s, x, y);
75
+ return space.offset(x, y, Number.isFinite(height) ? height : z);
76
+ },
77
+ };
78
+ }
79
+ function drawSurface(s) {
80
+ const g3 = new Graphics3D();
81
+ const space = graphOf(s);
82
+ const lines = space ? meshLines(s, space) : [];
83
+ const scaled = s.colorScale.length > 0;
84
+ // A surface is seen from both sides; a vertex colour multiplies the fill, so a scale is shown on white.
85
+ const surface = { ...s, faces: s.faces ?? "both", ...(scaled ? { vertexColors: true, fill: "white" } : {}) };
86
+ for (const op of Material.mesh(surface, space ? surfaceGeometry(s, space) : undefined).ops()) {
87
+ if (op.kind !== "mesh")
88
+ continue;
89
+ g3.mesh(op.geometry, mapMaterials(op.material, (material) => ({
90
+ ...material,
91
+ // Pushed back a hair so the lines drawn on it are not fought for, and blended rather than a hole when faded.
92
+ ...(lines.length > 0 ? { polygonOffset: true, polygonOffsetFactor: 1, polygonOffsetUnits: 1 } : {}),
93
+ ...((s.opacity ?? 1) < 1 ? { depthWrite: false } : {}),
94
+ })), op.transform);
128
95
  }
129
- _key(space) {
130
- const { source, blend } = this.expression;
131
- return [source, blend?.from, blend?.t, space.revision, this.uDomain, this.vDomain, JSON.stringify(this.params), this.x, this.y, this.z].join("|");
96
+ if (lines.length > 0) {
97
+ const color = colorOps.resolve(s.meshColor);
98
+ g3.line({ points: lines, segments: true, stroke: { fill: color }, opacity: color[3] * (s.opacity ?? 1) });
132
99
  }
133
- _sampler(space) {
134
- const { source, blend } = this.expression;
135
- const to = this._surface(source, space);
136
- if (!to || !blend)
137
- return to;
138
- const from = this._surface(blend.from, space);
139
- if (!from)
140
- return to;
141
- const start = { x: 0, y: 0, z: 0 };
142
- return (u, v, out) => {
143
- from(u, v, start);
144
- to(u, v, out);
145
- out.x = start.x + (out.x - start.x) * blend.t;
146
- out.y = start.y + (out.y - start.y) * blend.t;
147
- out.z = start.z + (out.z - start.z) * blend.t;
148
- };
100
+ const shadow = space && s.floorShadow > 0 ? floorShadow(s, space) : undefined;
101
+ if (shadow) {
102
+ g3.mesh(shadow, Mat.basic({
103
+ color: scaled ? "white" : PLOT_COLOR,
104
+ vertexColors: scaled,
105
+ opacity: s.floorShadow * (s.opacity ?? 1),
106
+ transparent: true,
107
+ // Translucent and under everything: writing depth would hide the floor's grid lines behind it.
108
+ depthWrite: false,
109
+ faces: "both",
110
+ }));
149
111
  }
150
- /** `source` as graph points, measured from this node's own position. */
151
- _surface(source, space) {
152
- const equation = equationOf(source);
153
- if (!equation)
154
- return null;
155
- const params = scopeFor(surfaceParams(source), this.params);
156
- if (equation.kind === "parametric") {
157
- const [u0, u1] = this.uDomain ?? [0, 1];
158
- const [v0, v1] = this.vDomain ?? [0, 1];
159
- const { x, y, z } = equation;
160
- return (u, v, out) => {
161
- const a = u0 + (u1 - u0) * u;
162
- const b = v0 + (v1 - v0) * v;
163
- out.x = finite(x(a, b, params));
164
- out.y = finite(y(a, b, params));
165
- out.z = finite(z(a, b, params));
166
- };
167
- }
168
- const [x0, x1] = this.uDomain ?? [space.xRange[0] - this.x, space.xRange[1] - this.x];
169
- const [y0, y1] = this.vDomain ?? [space.yRange[0] - this.y, space.yRange[1] - this.y];
170
- // Held inside the axes: a pole would otherwise stretch one triangle across the scene.
171
- const low = space.zRange[0] - this.z;
172
- const high = space.zRange[1] - this.z;
173
- const { f } = equation;
112
+ return g3;
113
+ }
114
+ function surfaceGeometry(s, space) {
115
+ const sample = sampler(s, space);
116
+ if (!sample)
117
+ return undefined;
118
+ const scale = s.colorScale;
119
+ const point = { x: 0, y: 0, z: 0 };
120
+ const low = space.offset(0, 0, space.zRange[0] - s.z).y;
121
+ const high = space.offset(0, 0, space.zRange[1] - s.z).y;
122
+ return Geo.parametric({
123
+ segments: Math.max(2, Math.round(s.segments)),
124
+ vertex: (u, v) => {
125
+ sample(u, v, point);
126
+ return space.offset(point.x, point.y, point.z);
127
+ },
128
+ ...(scale.length > 0
129
+ ? { color: (_u, _v, position) => colorAt(scale, high > low ? (position.y - low) / (high - low) : 0) }
130
+ : {}),
131
+ computeNormals: true,
132
+ revision: revisionOf(`${keyOf(s, space)}|${JSON.stringify(scale)}`),
133
+ });
134
+ }
135
+ function heightOf(surface, source, x, y) {
136
+ const equation = equationOf(source);
137
+ if (equation?.kind !== "height")
138
+ return NaN;
139
+ return equation.f(x, y, scopeFor(surfaceParams(source), surface.params));
140
+ }
141
+ function keyOf(s, space) {
142
+ const { source, blend } = s.expression;
143
+ return [source, blend?.from, blend?.t, space.revision, s.uDomain, s.vDomain, JSON.stringify(s.params), s.x, s.y, s.z].join("|");
144
+ }
145
+ function sampler(s, space) {
146
+ const { source, blend } = s.expression;
147
+ const to = surfaceOf(s, source, space);
148
+ if (!to || !blend)
149
+ return to;
150
+ const from = surfaceOf(s, blend.from, space);
151
+ if (!from)
152
+ return to;
153
+ const start = { x: 0, y: 0, z: 0 };
154
+ return (u, v, out) => {
155
+ from(u, v, start);
156
+ to(u, v, out);
157
+ out.x = start.x + (out.x - start.x) * blend.t;
158
+ out.y = start.y + (out.y - start.y) * blend.t;
159
+ out.z = start.z + (out.z - start.z) * blend.t;
160
+ };
161
+ }
162
+ /** `source` as graph points, measured from the surface's own position. */
163
+ function surfaceOf(s, source, space) {
164
+ const equation = equationOf(source);
165
+ if (!equation)
166
+ return null;
167
+ const params = scopeFor(surfaceParams(source), s.params);
168
+ if (equation.kind === "parametric") {
169
+ const [u0, u1] = s.uDomain ?? [0, 1];
170
+ const [v0, v1] = s.vDomain ?? [0, 1];
171
+ const { x, y, z } = equation;
174
172
  return (u, v, out) => {
175
- out.x = x0 + (x1 - x0) * u;
176
- out.y = y0 + (y1 - y0) * v;
177
- out.z = clamp(finite(f(out.x, out.y, params)), low, high);
173
+ const a = u0 + (u1 - u0) * u;
174
+ const b = v0 + (v1 - v0) * v;
175
+ out.x = finite(x(a, b, params));
176
+ out.y = finite(y(a, b, params));
177
+ out.z = finite(z(a, b, params));
178
178
  };
179
179
  }
180
- _floorShadow(space) {
181
- const sample = this._sampler(space);
182
- if (!sample)
183
- return undefined;
184
- const scale = this.colorScale;
185
- const [low, high] = space.zRange;
186
- const floor = low - this.z;
187
- const point = { x: 0, y: 0, z: 0 };
188
- return Geo.parametric({
189
- segments: Math.max(2, Math.round(this.segments)),
190
- vertex: (u, v) => {
191
- sample(u, v, point);
192
- return space.offset(point.x, point.y, floor);
193
- },
194
- ...(scale.length > 0
195
- ? {
196
- color: (u, v) => {
197
- sample(u, v, point);
198
- return colorAt(scale, high > low ? (point.z + this.z - low) / (high - low) : 0);
199
- },
200
- }
201
- : {}),
202
- revision: revisionOf(`${this._key(space)}|${JSON.stringify(scale)}|floor`),
203
- });
204
- }
205
- _meshLines(space) {
206
- const count = Math.round(this.meshLines);
207
- const sample = count > 0 ? this._sampler(space) : null;
208
- if (!sample)
209
- return [];
210
- const steps = Math.max(2, Math.round(this.segments));
211
- const key = `${this._key(space)}|${count}|${steps}`;
212
- if (key === this._linesKey)
213
- return this._lines;
214
- const lines = [];
215
- const point = { x: 0, y: 0, z: 0 };
216
- const at = (u, v) => {
180
+ const [x0, x1] = s.uDomain ?? [space.xRange[0] - s.x, space.xRange[1] - s.x];
181
+ const [y0, y1] = s.vDomain ?? [space.yRange[0] - s.y, space.yRange[1] - s.y];
182
+ // Held inside the axes: a pole would otherwise stretch one triangle across the scene.
183
+ const low = space.zRange[0] - s.z;
184
+ const high = space.zRange[1] - s.z;
185
+ const { f } = equation;
186
+ return (u, v, out) => {
187
+ out.x = x0 + (x1 - x0) * u;
188
+ out.y = y0 + (y1 - y0) * v;
189
+ out.z = clamp(finite(f(out.x, out.y, params)), low, high);
190
+ };
191
+ }
192
+ function floorShadow(s, space) {
193
+ const sample = sampler(s, space);
194
+ if (!sample)
195
+ return undefined;
196
+ const scale = s.colorScale;
197
+ const [low, high] = space.zRange;
198
+ const floor = low - s.z;
199
+ const point = { x: 0, y: 0, z: 0 };
200
+ return Geo.parametric({
201
+ segments: Math.max(2, Math.round(s.segments)),
202
+ vertex: (u, v) => {
217
203
  sample(u, v, point);
218
- return space.offset(point.x, point.y, point.z);
219
- };
220
- for (let i = 0; i <= count; i++) {
221
- const fixed = i / count;
222
- for (let j = 0; j < steps; j++) {
223
- lines.push(at(fixed, j / steps), at(fixed, (j + 1) / steps));
224
- lines.push(at(j / steps, fixed), at((j + 1) / steps, fixed));
204
+ return space.offset(point.x, point.y, floor);
205
+ },
206
+ ...(scale.length > 0
207
+ ? {
208
+ color: (u, v) => {
209
+ sample(u, v, point);
210
+ return colorAt(scale, high > low ? (point.z + s.z - low) / (high - low) : 0);
211
+ },
225
212
  }
213
+ : {}),
214
+ revision: revisionOf(`${keyOf(s, space)}|${JSON.stringify(scale)}|floor`),
215
+ });
216
+ }
217
+ /** The last lines drawn for each surface, kept while what they are drawn from is unchanged. */
218
+ const linesDrawn = new WeakMap();
219
+ function meshLines(s, space) {
220
+ const count = Math.round(s.meshLines);
221
+ const sample = count > 0 ? sampler(s, space) : null;
222
+ if (!sample)
223
+ return [];
224
+ const steps = Math.max(2, Math.round(s.segments));
225
+ const key = `${keyOf(s, space)}|${count}|${steps}`;
226
+ const kept = linesDrawn.get(s);
227
+ if (kept?.key === key)
228
+ return kept.lines;
229
+ const lines = [];
230
+ const point = { x: 0, y: 0, z: 0 };
231
+ const at = (u, v) => {
232
+ sample(u, v, point);
233
+ return space.offset(point.x, point.y, point.z);
234
+ };
235
+ for (let i = 0; i <= count; i++) {
236
+ const fixed = i / count;
237
+ for (let j = 0; j < steps; j++) {
238
+ lines.push(at(fixed, j / steps), at(fixed, (j + 1) / steps));
239
+ lines.push(at(j / steps, fixed), at((j + 1) / steps, fixed));
226
240
  }
227
- this._lines = lines;
228
- this._linesKey = key;
229
- return lines;
230
241
  }
231
- };
232
- __decorate([
233
- property({ default: "", mapper: readSource, tween: lerpSource, kind: "surfaceEquation" })
234
- ], SurfacePlot3d.prototype, "expression", void 0);
235
- __decorate([
236
- property({ default: null, tween: lerpDomain, kind: "range", defaultMin: -3, defaultMax: 3 })
237
- ], SurfacePlot3d.prototype, "uDomain", void 0);
238
- __decorate([
239
- property({ default: null, tween: lerpDomain, kind: "range", defaultMin: -3, defaultMax: 3 })
240
- ], SurfacePlot3d.prototype, "vDomain", void 0);
241
- __decorate([
242
- property({ default: {}, tween: lerpParams })
243
- ], SurfacePlot3d.prototype, "params", void 0);
244
- __decorate([
245
- property({ default: 48, min: 2, max: 256, step: 1, animatable: false })
246
- ], SurfacePlot3d.prototype, "segments", void 0);
247
- __decorate([
248
- property({ default: [], mapper: readScale, tween: lerpScale, kind: "vector" })
249
- ], SurfacePlot3d.prototype, "colorScale", void 0);
250
- __decorate([
251
- property({ default: 12, min: 0, max: 64, step: 1, animatable: false })
252
- ], SurfacePlot3d.prototype, "meshLines", void 0);
253
- __decorate([
254
- property({ default: "#0b1220", kind: "color" })
255
- ], SurfacePlot3d.prototype, "meshColor", void 0);
256
- __decorate([
257
- property({ default: 0, min: 0, max: 1, step: 0.01 })
258
- ], SurfacePlot3d.prototype, "floorShadow", void 0);
259
- __decorate([
260
- command({ args: ["expression"] })
261
- ], SurfacePlot3d.prototype, "morphTo", null);
262
- __decorate([
263
- command({ args: ["params"] })
264
- ], SurfacePlot3d.prototype, "paramsTo", null);
265
- __decorate([
266
- command({ args: ["uDomain", "vDomain"] })
267
- ], SurfacePlot3d.prototype, "domainTo", null);
268
- SurfacePlot3d = __decorate([
269
- node({
270
- key: "surfacePlot3d",
271
- within: ["axes3d"],
272
- closed: true,
273
- forkable: true,
274
- layout: { acceptsChildren: true },
275
- seed: { expression: "sin(x) * cos(y)", fill: PLOT_COLOR },
276
- })
277
- ], SurfacePlot3d);
278
- export { SurfacePlot3d };
242
+ linesDrawn.set(s, { key, lines });
243
+ return lines;
244
+ }
279
245
  function equationOf(source) {
280
246
  const equation = parseSurface(source, surfaceParams(source));
281
247
  return equation instanceof Error ? null : equation;