lk3d 0.1.0 → 0.2.1
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 +59 -0
- package/README.md +28 -7
- package/bin/lk3d.mjs +24 -13
- package/dist/lk3d-export.min.js +1 -1
- package/dist/lk3d.min.js +73 -35
- package/dist/lk3d.mjs +72 -34
- package/docs/api.md +132 -51
- package/docs/design.md +12 -7
- package/docs/handles-and-lines.md +27 -0
- package/docs/performance.md +42 -10
- package/docs/scene-format.md +31 -29
- package/index.d.ts +53 -2
- package/package.json +2 -3
- package/scene.d.ts +200 -1
- package/schema/scene.schema.json +329 -1
- package/skill/lk3d/SKILL.md +14 -1
- package/src/ao.js +58 -0
- package/src/controls.js +21 -2
- package/src/element.js +111 -0
- package/src/figure/api.js +18 -3
- package/src/figure/camera.js +57 -9
- package/src/figure/context.d.ts +37 -2
- package/src/figure/dimensions.js +140 -0
- package/src/figure/idle.js +57 -19
- package/src/figure/input.js +105 -7
- package/src/figure/labels.js +26 -8
- package/src/figure/loop.js +8 -2
- package/src/figure/pick.js +24 -8
- package/src/figure/steps.js +144 -0
- package/src/flow.js +33 -2
- package/src/index.js +16 -2
- package/src/locales/en.js +7 -0
- package/src/locales/vi.js +7 -0
- package/src/renderer-gl.js +88 -16
- package/src/shaders.js +63 -17
- package/src/stylesheet.js +14 -4
- package/src/validate.js +89 -0
- package/docs/flow.png +0 -0
- package/docs/generator.png +0 -0
- package/docs/pen-blueprint.png +0 -0
- package/docs/pen-dark.png +0 -0
- package/docs/pen-ortho.png +0 -0
- package/docs/shapes/blade.png +0 -0
- package/docs/shapes/blob.png +0 -0
- package/docs/shapes/box.png +0 -0
- package/docs/shapes/capsule.png +0 -0
- package/docs/shapes/cylinder.png +0 -0
- package/docs/shapes/dna.png +0 -0
- package/docs/shapes/ellipsoid.png +0 -0
- package/docs/shapes/extrude.png +0 -0
- package/docs/shapes/helix.png +0 -0
- package/docs/shapes/lathe.png +0 -0
- package/docs/shapes/loop.png +0 -0
- package/docs/shapes/molecule.png +0 -0
- package/docs/shapes/orbit.png +0 -0
- package/docs/shapes/ring.png +0 -0
- package/docs/shapes/scatter.png +0 -0
- package/docs/shapes/shell.png +0 -0
- package/docs/shapes/sphere.png +0 -0
- package/docs/shapes/terrain.png +0 -0
- package/docs/shapes/torus.png +0 -0
- package/docs/shapes/tree.png +0 -0
- package/docs/shapes/tube.png +0 -0
package/schema/scene.schema.json
CHANGED
|
@@ -152,7 +152,7 @@
|
|
|
152
152
|
"description": "A shadow under the model (true by default)."
|
|
153
153
|
},
|
|
154
154
|
"section": {
|
|
155
|
-
"description": "The cut: a plane, or false to offer none. What lies on the side its normal points to, beyond offset along it, is cut away; a page may move it (set({ section }))",
|
|
155
|
+
"description": "The cut: a plane, or false to offer none. What lies on the side its normal points to, beyond offset along it, is cut away; while the model is cut open the reader moves the plane along its normal (dragging the cut face, or the slider) unless it is fixed, and a page may move it (set({ section }))",
|
|
156
156
|
"anyOf": [
|
|
157
157
|
{
|
|
158
158
|
"const": false
|
|
@@ -165,6 +165,30 @@
|
|
|
165
165
|
},
|
|
166
166
|
"offset": {
|
|
167
167
|
"type": "number"
|
|
168
|
+
},
|
|
169
|
+
"fixed": {
|
|
170
|
+
"type": "boolean",
|
|
171
|
+
"description": "true: the reader cannot move the cut (by dragging its face or its slider); the page still can. By default, while the model is cut open, the reader moves the plane along its normal."
|
|
172
|
+
},
|
|
173
|
+
"second": {
|
|
174
|
+
"description": "A second plane, for a quarter cut: then only what lies beyond both planes is cut away (two planes at right angles take out a quarter). false: none (a step's, over the scene's).",
|
|
175
|
+
"anyOf": [
|
|
176
|
+
{
|
|
177
|
+
"const": false
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
"type": "object",
|
|
181
|
+
"properties": {
|
|
182
|
+
"normal": {
|
|
183
|
+
"$ref": "#/$defs/vec3"
|
|
184
|
+
},
|
|
185
|
+
"offset": {
|
|
186
|
+
"type": "number"
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
"additionalProperties": false
|
|
190
|
+
}
|
|
191
|
+
]
|
|
168
192
|
}
|
|
169
193
|
},
|
|
170
194
|
"additionalProperties": false
|
|
@@ -221,6 +245,14 @@
|
|
|
221
245
|
"x-message": "scale is a bar ({bar: {length, label}}) and/or a note ({note: \"not to scale\"})",
|
|
222
246
|
"additionalProperties": false
|
|
223
247
|
},
|
|
248
|
+
"dimensions": {
|
|
249
|
+
"type": "array",
|
|
250
|
+
"minItems": 1,
|
|
251
|
+
"items": {
|
|
252
|
+
"$ref": "#/$defs/dimension"
|
|
253
|
+
},
|
|
254
|
+
"description": "Dimensions drawn on the figure: lengths between points and angles at vertices, with their values, as on a technical drawing; worked out from the points as shown, so a length to a moving part changes as it moves."
|
|
255
|
+
},
|
|
224
256
|
"models": {
|
|
225
257
|
"type": "object",
|
|
226
258
|
"description": "glTF 2.0 models (.glb) by name, for {model: name} shapes.",
|
|
@@ -265,6 +297,14 @@
|
|
|
265
297
|
},
|
|
266
298
|
"x-message": "a scene needs parts",
|
|
267
299
|
"description": "The parts of the model, each a list of shapes with a name and a note."
|
|
300
|
+
},
|
|
301
|
+
"steps": {
|
|
302
|
+
"type": "array",
|
|
303
|
+
"minItems": 1,
|
|
304
|
+
"items": {
|
|
305
|
+
"$ref": "#/$defs/step"
|
|
306
|
+
},
|
|
307
|
+
"description": "Guided steps: the figure explained in order, each with its words and a state of the figure (where the camera looks, taken apart, cut open, labels, a part chosen, motions). The figure starts at the first; the reader goes through them with Back and Next under it, the page with fig.step()."
|
|
268
308
|
}
|
|
269
309
|
},
|
|
270
310
|
"$defs": {
|
|
@@ -1960,6 +2000,294 @@
|
|
|
1960
2000
|
}
|
|
1961
2001
|
],
|
|
1962
2002
|
"x-message": "a joint turns its parts (turn) or slides them (slide)"
|
|
2003
|
+
},
|
|
2004
|
+
"step": {
|
|
2005
|
+
"type": "object",
|
|
2006
|
+
"description": "A step of the figure's explanation: its words, and the figure's state while it is shown. What a step does not say goes back to the figure's start (put together, not cut, see-through off, labels as usual, nothing hidden or chosen, the motions at their start), so a step is the same picture however the reader came to it; only the camera stays where the reader left it unless the step says where (view, focus).",
|
|
2007
|
+
"properties": {
|
|
2008
|
+
"id": {
|
|
2009
|
+
"type": "string",
|
|
2010
|
+
"minLength": 1,
|
|
2011
|
+
"description": "Unique among the steps: what the page names it by (fig.step(id))."
|
|
2012
|
+
},
|
|
2013
|
+
"title": {
|
|
2014
|
+
"type": "string",
|
|
2015
|
+
"minLength": 1,
|
|
2016
|
+
"description": "The step's heading, a few words."
|
|
2017
|
+
},
|
|
2018
|
+
"text": {
|
|
2019
|
+
"type": "string",
|
|
2020
|
+
"minLength": 1,
|
|
2021
|
+
"description": "What the reader is to look at, and why: a sentence or two, with simple formatting (b i em strong sub sup br) as in a part's note."
|
|
2022
|
+
},
|
|
2023
|
+
"view": {
|
|
2024
|
+
"description": "Where the camera goes: a named view (home: the figure's first), or yaw and pitch (degrees), zoom (2: twice as close as the whole model) or dist, and target. Without view or focus the camera stays where it is.",
|
|
2025
|
+
"anyOf": [
|
|
2026
|
+
{
|
|
2027
|
+
"enum": ["front", "side", "top", "iso", "home"]
|
|
2028
|
+
},
|
|
2029
|
+
{
|
|
2030
|
+
"type": "object",
|
|
2031
|
+
"properties": {
|
|
2032
|
+
"yaw": {
|
|
2033
|
+
"type": "number"
|
|
2034
|
+
},
|
|
2035
|
+
"pitch": {
|
|
2036
|
+
"type": "number"
|
|
2037
|
+
},
|
|
2038
|
+
"zoom": {
|
|
2039
|
+
"type": "number",
|
|
2040
|
+
"exclusiveMinimum": 0
|
|
2041
|
+
},
|
|
2042
|
+
"dist": {
|
|
2043
|
+
"type": "number",
|
|
2044
|
+
"exclusiveMinimum": 0
|
|
2045
|
+
},
|
|
2046
|
+
"target": {
|
|
2047
|
+
"$ref": "#/$defs/vec3"
|
|
2048
|
+
}
|
|
2049
|
+
},
|
|
2050
|
+
"additionalProperties": false
|
|
2051
|
+
}
|
|
2052
|
+
]
|
|
2053
|
+
},
|
|
2054
|
+
"focus": {
|
|
2055
|
+
"type": "array",
|
|
2056
|
+
"items": {
|
|
2057
|
+
"type": "string",
|
|
2058
|
+
"minLength": 1
|
|
2059
|
+
},
|
|
2060
|
+
"minItems": 1,
|
|
2061
|
+
"description": "Parts or groups to frame: the camera looks at their middle (as they are once the step has taken them apart) from far enough to see them all, from the view's angles if it gives them."
|
|
2062
|
+
},
|
|
2063
|
+
"explode": {
|
|
2064
|
+
"type": "number",
|
|
2065
|
+
"minimum": 0,
|
|
2066
|
+
"maximum": 1,
|
|
2067
|
+
"description": "How far apart (0, the default: put together; 1: all the way)."
|
|
2068
|
+
},
|
|
2069
|
+
"cut": {
|
|
2070
|
+
"anyOf": [
|
|
2071
|
+
{
|
|
2072
|
+
"type": "boolean"
|
|
2073
|
+
},
|
|
2074
|
+
{
|
|
2075
|
+
"type": "number",
|
|
2076
|
+
"minimum": 0,
|
|
2077
|
+
"maximum": 1
|
|
2078
|
+
}
|
|
2079
|
+
],
|
|
2080
|
+
"description": "Cut open (true), or how far the plane has swept in (0 to 1); swept in as the step comes."
|
|
2081
|
+
},
|
|
2082
|
+
"section": {
|
|
2083
|
+
"type": "object",
|
|
2084
|
+
"properties": {
|
|
2085
|
+
"normal": {
|
|
2086
|
+
"$ref": "#/$defs/vec3"
|
|
2087
|
+
},
|
|
2088
|
+
"offset": {
|
|
2089
|
+
"type": "number"
|
|
2090
|
+
},
|
|
2091
|
+
"second": {
|
|
2092
|
+
"description": "A second plane, for a quarter cut: then only what lies beyond both planes is cut away (two planes at right angles take out a quarter). false: none (a step's, over the scene's).",
|
|
2093
|
+
"anyOf": [
|
|
2094
|
+
{
|
|
2095
|
+
"const": false
|
|
2096
|
+
},
|
|
2097
|
+
{
|
|
2098
|
+
"type": "object",
|
|
2099
|
+
"properties": {
|
|
2100
|
+
"normal": {
|
|
2101
|
+
"$ref": "#/$defs/vec3"
|
|
2102
|
+
},
|
|
2103
|
+
"offset": {
|
|
2104
|
+
"type": "number"
|
|
2105
|
+
}
|
|
2106
|
+
},
|
|
2107
|
+
"additionalProperties": false
|
|
2108
|
+
}
|
|
2109
|
+
]
|
|
2110
|
+
}
|
|
2111
|
+
},
|
|
2112
|
+
"additionalProperties": false,
|
|
2113
|
+
"description": "The cut's plane for this step (the scene's section by default)."
|
|
2114
|
+
},
|
|
2115
|
+
"xray": {
|
|
2116
|
+
"type": "boolean",
|
|
2117
|
+
"description": "Casings see-through."
|
|
2118
|
+
},
|
|
2119
|
+
"labels": {
|
|
2120
|
+
"description": "Which labels show: auto (the part pointed at or chosen, and all of them once the model is well apart; the default), all, none, or a list of parts and groups.",
|
|
2121
|
+
"anyOf": [
|
|
2122
|
+
{
|
|
2123
|
+
"enum": ["auto", "all", "none"]
|
|
2124
|
+
},
|
|
2125
|
+
{
|
|
2126
|
+
"type": "array",
|
|
2127
|
+
"items": {
|
|
2128
|
+
"type": "string",
|
|
2129
|
+
"minLength": 1
|
|
2130
|
+
}
|
|
2131
|
+
}
|
|
2132
|
+
]
|
|
2133
|
+
},
|
|
2134
|
+
"hide": {
|
|
2135
|
+
"type": "array",
|
|
2136
|
+
"items": {
|
|
2137
|
+
"type": "string",
|
|
2138
|
+
"minLength": 1
|
|
2139
|
+
},
|
|
2140
|
+
"minItems": 1,
|
|
2141
|
+
"description": "Parts or groups out of sight in this step."
|
|
2142
|
+
},
|
|
2143
|
+
"select": {
|
|
2144
|
+
"type": "string",
|
|
2145
|
+
"minLength": 1,
|
|
2146
|
+
"description": "A part chosen, as if the reader had picked it: lit, with its name and note in the info box."
|
|
2147
|
+
},
|
|
2148
|
+
"motion": {
|
|
2149
|
+
"type": "object",
|
|
2150
|
+
"additionalProperties": {
|
|
2151
|
+
"type": "number"
|
|
2152
|
+
},
|
|
2153
|
+
"description": "Motions' values by id (the others at their start)."
|
|
2154
|
+
},
|
|
2155
|
+
"play": {
|
|
2156
|
+
"type": "array",
|
|
2157
|
+
"items": {
|
|
2158
|
+
"type": "string",
|
|
2159
|
+
"minLength": 1
|
|
2160
|
+
},
|
|
2161
|
+
"minItems": 1,
|
|
2162
|
+
"description": "Motions played as the step comes, from the value it gives them."
|
|
2163
|
+
},
|
|
2164
|
+
"dimensions": {
|
|
2165
|
+
"type": "array",
|
|
2166
|
+
"items": {
|
|
2167
|
+
"type": "string",
|
|
2168
|
+
"minLength": 1
|
|
2169
|
+
},
|
|
2170
|
+
"description": "The dimensions shown in this step, by id (none: [ ]); without it, the scene's own (those not hidden)."
|
|
2171
|
+
}
|
|
2172
|
+
},
|
|
2173
|
+
"anyOf": [
|
|
2174
|
+
{
|
|
2175
|
+
"required": ["title"]
|
|
2176
|
+
},
|
|
2177
|
+
{
|
|
2178
|
+
"required": ["text"]
|
|
2179
|
+
}
|
|
2180
|
+
],
|
|
2181
|
+
"x-message": "a step needs its words: a title, text, or both",
|
|
2182
|
+
"additionalProperties": false
|
|
2183
|
+
},
|
|
2184
|
+
"point": {
|
|
2185
|
+
"description": "A point of the scene, [x, y, z], or one carried by a part, {part, at}: it moves as the part does (taken apart, moved by a motion or a handle), so what is measured to it is measured as shown.",
|
|
2186
|
+
"anyOf": [
|
|
2187
|
+
{
|
|
2188
|
+
"$ref": "#/$defs/vec3"
|
|
2189
|
+
},
|
|
2190
|
+
{
|
|
2191
|
+
"type": "object",
|
|
2192
|
+
"required": ["part", "at"],
|
|
2193
|
+
"properties": {
|
|
2194
|
+
"part": {
|
|
2195
|
+
"type": "string",
|
|
2196
|
+
"minLength": 1,
|
|
2197
|
+
"description": "The part that carries it."
|
|
2198
|
+
},
|
|
2199
|
+
"at": {
|
|
2200
|
+
"$ref": "#/$defs/vec3",
|
|
2201
|
+
"description": "The point, in the part's own place at rest."
|
|
2202
|
+
}
|
|
2203
|
+
},
|
|
2204
|
+
"additionalProperties": false
|
|
2205
|
+
}
|
|
2206
|
+
],
|
|
2207
|
+
"x-message": "a point is [x, y, z], or {part, at: [x, y, z]}"
|
|
2208
|
+
},
|
|
2209
|
+
"dimension": {
|
|
2210
|
+
"type": "object",
|
|
2211
|
+
"description": "A dimension drawn on the figure, as on a technical drawing: a length between two points (extension lines, a dimension line with arrows, its value), or an angle at a vertex (an arc with arrows, its value in degrees). Its value is worked out from the points as they are shown (in the scene's unit, when it has one), unless text says otherwise.",
|
|
2212
|
+
"properties": {
|
|
2213
|
+
"id": {
|
|
2214
|
+
"type": "string",
|
|
2215
|
+
"minLength": 1,
|
|
2216
|
+
"description": "Unique among the dimensions: what steps and the page name it by."
|
|
2217
|
+
},
|
|
2218
|
+
"name": {
|
|
2219
|
+
"type": "string",
|
|
2220
|
+
"minLength": 1,
|
|
2221
|
+
"description": "What is measured, for screen readers (\"the piston's stroke\")."
|
|
2222
|
+
},
|
|
2223
|
+
"from": {
|
|
2224
|
+
"$ref": "#/$defs/point",
|
|
2225
|
+
"description": "A length's first end."
|
|
2226
|
+
},
|
|
2227
|
+
"to": {
|
|
2228
|
+
"$ref": "#/$defs/point",
|
|
2229
|
+
"description": "A length's other end."
|
|
2230
|
+
},
|
|
2231
|
+
"offset": {
|
|
2232
|
+
"$ref": "#/$defs/vec3",
|
|
2233
|
+
"description": "Where the dimension line is drawn: the two ends moved by this much (default: on the points themselves), with extension lines out to it."
|
|
2234
|
+
},
|
|
2235
|
+
"angle": {
|
|
2236
|
+
"type": "array",
|
|
2237
|
+
"prefixItems": [
|
|
2238
|
+
{
|
|
2239
|
+
"$ref": "#/$defs/point"
|
|
2240
|
+
},
|
|
2241
|
+
{
|
|
2242
|
+
"$ref": "#/$defs/point"
|
|
2243
|
+
},
|
|
2244
|
+
{
|
|
2245
|
+
"$ref": "#/$defs/point"
|
|
2246
|
+
}
|
|
2247
|
+
],
|
|
2248
|
+
"minItems": 3,
|
|
2249
|
+
"maxItems": 3,
|
|
2250
|
+
"description": "An angle: a point on one arm, the vertex, a point on the other arm."
|
|
2251
|
+
},
|
|
2252
|
+
"radius": {
|
|
2253
|
+
"type": "number",
|
|
2254
|
+
"exclusiveMinimum": 0,
|
|
2255
|
+
"description": "An angle's arc radius (default: a third of the shorter arm)."
|
|
2256
|
+
},
|
|
2257
|
+
"arms": {
|
|
2258
|
+
"type": "boolean",
|
|
2259
|
+
"description": "Draw an angle's arms from the vertex (false by default: they are usually edges of the model)."
|
|
2260
|
+
},
|
|
2261
|
+
"text": {
|
|
2262
|
+
"type": "string",
|
|
2263
|
+
"minLength": 1,
|
|
2264
|
+
"description": "The value shown, in place of the one worked out (\"Ø 40\", \"stroke\")."
|
|
2265
|
+
},
|
|
2266
|
+
"digits": {
|
|
2267
|
+
"type": "integer",
|
|
2268
|
+
"minimum": 0,
|
|
2269
|
+
"maximum": 6,
|
|
2270
|
+
"description": "Decimals in the value worked out (default: three significant figures)."
|
|
2271
|
+
},
|
|
2272
|
+
"hidden": {
|
|
2273
|
+
"type": "boolean",
|
|
2274
|
+
"description": "Not shown unless a step or the page names it."
|
|
2275
|
+
},
|
|
2276
|
+
"unit": {
|
|
2277
|
+
"enum": ["mm", "cm", "dm", "m", "km"],
|
|
2278
|
+
"description": "The unit a length's value is given in (the scene's unit by default); converted from the scene's unit, which it needs."
|
|
2279
|
+
}
|
|
2280
|
+
},
|
|
2281
|
+
"anyOf": [
|
|
2282
|
+
{
|
|
2283
|
+
"required": ["from", "to"]
|
|
2284
|
+
},
|
|
2285
|
+
{
|
|
2286
|
+
"required": ["angle"]
|
|
2287
|
+
}
|
|
2288
|
+
],
|
|
2289
|
+
"x-message": "a dimension is a length (from, to) or an angle ([a, vertex, b])",
|
|
2290
|
+
"additionalProperties": false
|
|
1963
2291
|
}
|
|
1964
2292
|
},
|
|
1965
2293
|
"additionalProperties": false
|
package/skill/lk3d/SKILL.md
CHANGED
|
@@ -95,9 +95,22 @@ every number in scene units with one scale for the whole object, and say what is
|
|
|
95
95
|
- **Living and grown forms** (cells, neurons, organs): pen or studio, perspective. Soft shapes get no edge lines.
|
|
96
96
|
- `studio` when materials (metal, glass, tissue, a textured scan) are the point; `ink` for a drawn, warmer page.
|
|
97
97
|
- A `theme` (`light`, `dark`, `blueprint`, `sepia`) only if the page around it asks for one.
|
|
98
|
+
- **Dimensions** where a size or an angle is the point (a stroke, a focal length, a slope): `dimensions` with the
|
|
99
|
+
points as the model has them (on a part, `{part, at}`, if it moves), an `offset` that keeps the line clear of the
|
|
100
|
+
model, and the scene's `unit` so the value is right. A few, never a dimension for every part.
|
|
98
101
|
- The object fills about 55–65% of the stage, with air around it.
|
|
99
102
|
|
|
100
|
-
## 6.
|
|
103
|
+
## 6. Steps, when the figure explains something in order
|
|
104
|
+
|
|
105
|
+
When the figure is to explain (how a machine works, what happens in turn), give it `steps`: three to six, each a
|
|
106
|
+
`title` of a few words and a sentence or two of `text` saying what to look at and why, with the state that shows it
|
|
107
|
+
(`view` or `focus`, `explode`, `cut`, `xray`, `labels`, `select`, `motion`, `play`). Start from the whole object and
|
|
108
|
+
end where the idea is. A step says everything it needs: what it leaves out goes back to the start (put together, not
|
|
109
|
+
cut, nothing hidden), so write each as if the reader came straight to it. Label only what the step's words name.
|
|
110
|
+
`check` catches steps that name what does not exist or hide what they label; `render` draws each step
|
|
111
|
+
(`name-look-step1.png`, …): look at each as the reader will, with its words beside it.
|
|
112
|
+
|
|
113
|
+
## 7. Look like a critic, then hand over
|
|
101
114
|
|
|
102
115
|
Render rest, apart and cut in at least two looks, and at a narrow width (`--width 390`). For each picture ask:
|
|
103
116
|
does it read at a glance? Is every part where it should be, resting on something? Does anything pass through
|
package/src/ao.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Crease shading (ambient occlusion: bvh.js aoJob) in a Web Worker, so the page's main thread stays free while it is
|
|
3
|
+
// worked out (seconds of rays on a slow phone). One worker for the page, made of lk3d's own script, nothing more to
|
|
4
|
+
// ship: the file a <script src> loaded (imported into the worker), or the text of an inline <script> (a built lesson's).
|
|
5
|
+
// Marked so (self.lk3dAO), the script answers jobs instead of drawing. Where no worker can be made (lk3d imported as a
|
|
6
|
+
// module, a page whose policy forbids one), shadeAway says so and the figure shades in idle slices on the page.
|
|
7
|
+
import { buildBVH, aoJob } from './bvh.js';
|
|
8
|
+
|
|
9
|
+
// (where this script came from: read while it first runs, the only time document.currentScript is this script)
|
|
10
|
+
const script =
|
|
11
|
+
typeof document !== 'undefined' ? /** @type {HTMLScriptElement | null} */ (document.currentScript) : null,
|
|
12
|
+
source = script ? (script.src ? `importScripts(${JSON.stringify(script.src)})` : script.textContent) : '';
|
|
13
|
+
/** @type {Worker | null} */
|
|
14
|
+
let worker = null,
|
|
15
|
+
failed = !source,
|
|
16
|
+
jobs = 0;
|
|
17
|
+
const waiting = new Map(); // a job's id → [resolve, reject]
|
|
18
|
+
|
|
19
|
+
// in the worker: each mesh sent shaded, its shading sent back
|
|
20
|
+
export function serveAO() {
|
|
21
|
+
self.onmessage = ({ data }) => {
|
|
22
|
+
const job = aoJob(data.mesh, buildBVH(data.mesh), data.reach);
|
|
23
|
+
job.step(Infinity);
|
|
24
|
+
self.postMessage({ id: data.id, out: job.out }, /** @type {any} */ ([job.out.buffer]));
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// on the page: a mesh's shading, worked out in the worker (a promise of its Uint8Array), or null where there is none
|
|
29
|
+
/** @param {{ positions: Float32Array, normals: Float32Array, indices: any, count: number }} mesh @param {number} reach */
|
|
30
|
+
export function shadeAway(mesh, reach) {
|
|
31
|
+
if (failed) return null;
|
|
32
|
+
try {
|
|
33
|
+
if (!worker) {
|
|
34
|
+
worker = new Worker(URL.createObjectURL(new Blob(['self.lk3dAO=1;' + source], { type: 'text/javascript' })));
|
|
35
|
+
worker.onmessage = ({ data }) => {
|
|
36
|
+
const [resolve] = waiting.get(data.id) || [];
|
|
37
|
+
waiting.delete(data.id);
|
|
38
|
+
if (resolve) resolve(data.out);
|
|
39
|
+
};
|
|
40
|
+
// (a worker that cannot load or breaks: its jobs, and every one after, done on the page)
|
|
41
|
+
worker.onerror = (event) => {
|
|
42
|
+
event.preventDefault();
|
|
43
|
+
failed = true;
|
|
44
|
+
for (const [, [, reject]] of waiting) reject(new Error('lk3d: the crease-shading worker stopped'));
|
|
45
|
+
waiting.clear();
|
|
46
|
+
if (worker) worker.terminate();
|
|
47
|
+
worker = null;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
const id = ++jobs,
|
|
51
|
+
{ positions, normals, indices, count } = mesh;
|
|
52
|
+
worker.postMessage({ id, mesh: { positions, normals, indices, count }, reach });
|
|
53
|
+
return new Promise((resolve, reject) => waiting.set(id, [resolve, reject]));
|
|
54
|
+
} catch {
|
|
55
|
+
failed = true; // (no Worker, no Blob URLs, or a policy that forbids them)
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/controls.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
// The built-in controls under a figure: take apart (a slider and a button), cut open,
|
|
2
|
-
// go round the looks, and the four views. They act through the figure's own API (fig.set, fig.view), so a page that
|
|
1
|
+
// The built-in controls under a figure: take apart (a slider and a button), cut open (and, while it is, where the cut
|
|
2
|
+
// is: a slider), see through, show all labels, go round the looks, and the four views. They act through the figure's own API (fig.set, fig.view), so a page that
|
|
3
3
|
// passes `controls: false` and builds its own buttons gets exactly the same behaviour.
|
|
4
4
|
export function controls({
|
|
5
5
|
el,
|
|
@@ -12,6 +12,8 @@ export function controls({
|
|
|
12
12
|
seeThrough,
|
|
13
13
|
tweenExplode,
|
|
14
14
|
motions,
|
|
15
|
+
cutRange,
|
|
16
|
+
moveCut,
|
|
15
17
|
}) {
|
|
16
18
|
const bar = document.createElement('div');
|
|
17
19
|
bar.className = 'lk3d-bar';
|
|
@@ -87,6 +89,23 @@ export function controls({
|
|
|
87
89
|
() => fig.set({ cut: !state.cut }),
|
|
88
90
|
() => state.cut,
|
|
89
91
|
);
|
|
92
|
+
// where the cut is, while it is on (unless the scene fixes it): a slider across the parts it cuts, from one end to
|
|
93
|
+
// the other (the same as dragging the cut face; the keys move it too)
|
|
94
|
+
if (cuttable && moveCut) {
|
|
95
|
+
const cutSlider = slider(words.cutAt, words.cutAtLabel, 0, 1, 0.005, 0),
|
|
96
|
+
row = /** @type {HTMLElement} */ (cutSlider.parentElement);
|
|
97
|
+
cutSlider.addEventListener('input', () => {
|
|
98
|
+
const [from, to] = cutRange();
|
|
99
|
+
moveCut(from + (to - from) * +cutSlider.value);
|
|
100
|
+
});
|
|
101
|
+
const sync = () => {
|
|
102
|
+
row.style.display = state.cut ? '' : 'none'; // (not the hidden attribute: the row's own display would win)
|
|
103
|
+
const [from, to] = cutRange();
|
|
104
|
+
cutSlider.value = String((state.section.offset - from) / (to - from || 1));
|
|
105
|
+
};
|
|
106
|
+
toggles.push(sync);
|
|
107
|
+
sync();
|
|
108
|
+
}
|
|
90
109
|
if (seeThrough)
|
|
91
110
|
toggle(
|
|
92
111
|
words.xray,
|
package/src/element.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// <lk3d-figure>: a figure as an HTML element, for a page that writes no script of its own (Markdown, a content
|
|
3
|
+
// system, any framework). The scene comes from `src` (its address) or from a JSON script inside the element:
|
|
4
|
+
// <lk3d-figure src="generator.json" look="pen" locale="vi"></lk3d-figure>
|
|
5
|
+
// <lk3d-figure><script type="application/json">{ "parts": [ … ] }</script></lk3d-figure>
|
|
6
|
+
// Attributes: look, theme, labels (auto, all, none), step (a place from 0, or an id) change the figure as they change;
|
|
7
|
+
// src, locale, controls="false", steps="false", info="false" (no line for the part's name and note under it) and
|
|
8
|
+
// transparent mount it again. A script reaches the figure's API as `element.figure` (null until mounted; a new one
|
|
9
|
+
// each time it is mounted again) and `element.ready` (resolves with the first, once complete); the reader's moves come as events that bubble: lk3d-pick
|
|
10
|
+
// ({ id }), lk3d-handle ({ id, value }), lk3d-step ({ index, step }), lk3d-section ({ normal, offset }); a scene that
|
|
11
|
+
// cannot be read or drawn, as lk3d-error ({ message }), its words shown in the element.
|
|
12
|
+
|
|
13
|
+
/** @param {(el: HTMLElement, scene: any, opts?: any) => any} mount */
|
|
14
|
+
export function defineElement(mount) {
|
|
15
|
+
if (typeof customElements === 'undefined' || customElements.get('lk3d-figure')) return;
|
|
16
|
+
const LIVE = ['look', 'theme', 'labels', 'step'],
|
|
17
|
+
MOUNTED = ['src', 'locale', 'controls', 'steps', 'info', 'transparent'];
|
|
18
|
+
class Lk3dFigure extends HTMLElement {
|
|
19
|
+
// (a getter, not a static field: the bundle is built for ES2020, which would bring a helper for the field)
|
|
20
|
+
static get observedAttributes() {
|
|
21
|
+
return [...LIVE, ...MOUNTED];
|
|
22
|
+
}
|
|
23
|
+
constructor() {
|
|
24
|
+
super();
|
|
25
|
+
/** @type {any} the figure's API, once mounted */
|
|
26
|
+
this.figure = null;
|
|
27
|
+
/** @type {any} the scene, read once (a JSON script inside is gone once the figure is drawn in its place) */
|
|
28
|
+
this.scene = null;
|
|
29
|
+
this.loads = 0; // (each load counted: one that finishes after a newer one began is dropped)
|
|
30
|
+
/** @type {(figure: any) => void} */
|
|
31
|
+
let resolve = () => {};
|
|
32
|
+
/** @type {Promise<any>} */
|
|
33
|
+
this.ready = new Promise((done) => (resolve = done));
|
|
34
|
+
this.resolveReady = resolve;
|
|
35
|
+
}
|
|
36
|
+
connectedCallback() {
|
|
37
|
+
if (!this.figure) this.load();
|
|
38
|
+
}
|
|
39
|
+
disconnectedCallback() {
|
|
40
|
+
if (this.figure) this.figure.destroy();
|
|
41
|
+
this.figure = null;
|
|
42
|
+
}
|
|
43
|
+
/** @param {string} name @param {string | null} before @param {string | null} now */
|
|
44
|
+
attributeChangedCallback(name, before, now) {
|
|
45
|
+
if (!this.figure || before === now) return;
|
|
46
|
+
if (name === 'src') this.scene = null;
|
|
47
|
+
if (!LIVE.includes(name)) return void this.load();
|
|
48
|
+
if (name === 'step') this.figure.step(/^-?\d+$/.test(now || '') ? +(now || 0) : now);
|
|
49
|
+
else this.figure.set({ [name === 'look' ? 'style' : name]: now || (name === 'labels' ? 'auto' : null) });
|
|
50
|
+
}
|
|
51
|
+
/** @param {string} type @param {object} detail */
|
|
52
|
+
say(type, detail) {
|
|
53
|
+
this.dispatchEvent(new CustomEvent('lk3d-' + type, { detail, bubbles: true }));
|
|
54
|
+
}
|
|
55
|
+
async load() {
|
|
56
|
+
const load = ++this.loads,
|
|
57
|
+
attribute = (/** @type {string} */ name) => this.getAttribute(name),
|
|
58
|
+
off = (/** @type {string} */ name) => attribute(name) === 'false';
|
|
59
|
+
try {
|
|
60
|
+
if (!this.scene) {
|
|
61
|
+
const inside = this.querySelector('script[type="application/json"]'),
|
|
62
|
+
src = attribute('src');
|
|
63
|
+
if (src) {
|
|
64
|
+
// (a page opened from a file may not fetch others: said so, with what to do, not as "Failed to fetch")
|
|
65
|
+
const response = await fetch(src).catch((/** @type {Error} */ error) => {
|
|
66
|
+
throw new Error(
|
|
67
|
+
`lk3d: ${src} could not be fetched (${error.message})` +
|
|
68
|
+
(location.protocol === 'file:' ? ': a page opened from a file cannot fetch; serve it over http' : ''),
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
if (!response.ok) throw new Error(`lk3d: ${src}: ${response.status} ${response.statusText}`);
|
|
72
|
+
this.scene = await response.json();
|
|
73
|
+
} else if (inside) this.scene = JSON.parse(inside.textContent || '');
|
|
74
|
+
else throw new Error('lk3d: <lk3d-figure> needs a scene: src="scene.json", or a JSON script inside');
|
|
75
|
+
}
|
|
76
|
+
if (load !== this.loads || !this.isConnected) return;
|
|
77
|
+
if (this.figure) this.figure.destroy();
|
|
78
|
+
const info = off('info') ? null : document.createElement('p'),
|
|
79
|
+
step = attribute('step');
|
|
80
|
+
if (info) info.className = 'lk3d-info';
|
|
81
|
+
this.figure = mount(this, this.scene, {
|
|
82
|
+
style: attribute('look') || undefined,
|
|
83
|
+
theme: attribute('theme') || undefined,
|
|
84
|
+
labels: attribute('labels') || undefined,
|
|
85
|
+
locale: attribute('locale') || undefined,
|
|
86
|
+
controls: !off('controls'),
|
|
87
|
+
steps: attribute('steps') == null ? undefined : !off('steps'),
|
|
88
|
+
step: step == null ? undefined : /^-?\d+$/.test(step) ? +step : step,
|
|
89
|
+
transparent: this.hasAttribute('transparent') && !off('transparent'),
|
|
90
|
+
info,
|
|
91
|
+
onPick: (/** @type {any} */ part) => this.say('pick', { id: part ? part.id : null }),
|
|
92
|
+
onHandle: (/** @type {string} */ id, /** @type {number} */ value) => this.say('handle', { id, value }),
|
|
93
|
+
onStep: (/** @type {number} */ index, /** @type {any} */ stepShown) =>
|
|
94
|
+
this.say('step', { index, step: stepShown }),
|
|
95
|
+
onSection: (/** @type {any} */ section) => this.say('section', section),
|
|
96
|
+
});
|
|
97
|
+
// (the name and note of the part pointed at, under the figure and its steps' words)
|
|
98
|
+
if (info) (this.querySelector('.lk3d-steps') || this.querySelector('.lk3d-stage'))?.after(info);
|
|
99
|
+
const figure = this.figure;
|
|
100
|
+
figure.ready.then(() => this.figure === figure && this.resolveReady(figure));
|
|
101
|
+
} catch (error) {
|
|
102
|
+
const message = String(/** @type {Error} */ (error).message || error);
|
|
103
|
+
this.figure = null;
|
|
104
|
+
this.textContent = message;
|
|
105
|
+
console.error(message);
|
|
106
|
+
this.say('error', { message });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
customElements.define('lk3d-figure', Lk3dFigure);
|
|
111
|
+
}
|
package/src/figure/api.js
CHANGED
|
@@ -57,10 +57,19 @@ export function makeApi(c) {
|
|
|
57
57
|
get timeScale() {
|
|
58
58
|
return c.state_.timeScale;
|
|
59
59
|
},
|
|
60
|
+
// the cut's plane now: where the scene, a step, the page or the reader put it
|
|
61
|
+
get section() {
|
|
62
|
+
const { normal, offset, second } = c.state_.section;
|
|
63
|
+
return {
|
|
64
|
+
normal: normal.slice(),
|
|
65
|
+
offset,
|
|
66
|
+
...(second && { second: { normal: (second.normal || [1, 0, 0]).slice(), offset: second.offset || 0 } }),
|
|
67
|
+
};
|
|
68
|
+
},
|
|
60
69
|
_state: c.state_, // the engine's own state: not an API, it may change
|
|
61
70
|
set(changes) {
|
|
62
71
|
if ('explode' in changes) c.explodeNow_(changes.explode); // (the page's own value wins over a running tween)
|
|
63
|
-
for (const key of ['spin', 'cut', 'labels', 'xray', 'style', 'theme', 'projection'])
|
|
72
|
+
for (const key of ['spin', 'cut', 'labels', 'xray', 'style', 'theme', 'projection', 'dimensions'])
|
|
64
73
|
if (key in changes) c.state_[key] = changes[key];
|
|
65
74
|
if ('transparent' in changes) c.state_.transparent = !!changes.transparent;
|
|
66
75
|
if (changes.section) c.state_.section = { ...c.state_.section, ...changes.section };
|
|
@@ -252,12 +261,18 @@ export function makeApi(c) {
|
|
|
252
261
|
}
|
|
253
262
|
);
|
|
254
263
|
},
|
|
264
|
+
// the scene's guided steps: go to one by its place (from 0) or id ({ ms: 0 }: at once), and which is shown (-1:
|
|
265
|
+
// none). The figure starts at the first (or mount's opts.step); moving through them does not call onStep
|
|
266
|
+
steps: c.spec_.steps || [],
|
|
267
|
+
step: (which, options) => c.goToStep_(which, options),
|
|
268
|
+
get currentStep() {
|
|
269
|
+
return c.stepAt_;
|
|
270
|
+
},
|
|
255
271
|
// frame some parts (ids or groups): the camera looks at their middle, from far enough to see them all
|
|
256
272
|
focus(ids, options = {}) {
|
|
257
273
|
const parts = c.built_.parts.filter((part) => ids.includes(part.id) || ids.includes(part.group));
|
|
258
274
|
if (!parts.length) return;
|
|
259
|
-
|
|
260
|
-
c.view_({ ...options, target: box.center, dist: box.radius * 2.6 }, options);
|
|
275
|
+
c.view_({ ...options, ...c.frame_(parts) }, options);
|
|
261
276
|
},
|
|
262
277
|
get refreshMs() {
|
|
263
278
|
return c.refreshMs_;
|