@algorisys/tinyfly 0.82.0 → 0.87.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/README.md +13 -4
  2. package/bin/tinyfly.mjs +44 -6
  3. package/lib/addons/adapters.js +411 -248
  4. package/lib/addons/characters.js +1652 -1470
  5. package/lib/addons/dom-adapter-Cphr2HZn.js +372 -0
  6. package/lib/addons/gsap-compat.js +197 -187
  7. package/lib/addons/load-scene-vp14_Jj-.js +324 -0
  8. package/lib/addons/maps.js +1 -1
  9. package/lib/addons/scene-3d-webgl.js +240 -0
  10. package/lib/addons/scene-3d.js +306 -0
  11. package/lib/addons/shading-o5T_v9DT.js +89 -0
  12. package/lib/addons/{sketch-CA8ZXhxI.js → sketch-CYPUbk3Q.js} +2 -2
  13. package/lib/browser/tinyfly-scene-3d-webgl.iife.js +116 -0
  14. package/lib/browser/tinyfly-scene-3d.iife.js +3 -0
  15. package/lib/browser/tinyfly.iife.js +4 -4
  16. package/lib/browser/tinyfly.js +4333 -3726
  17. package/lib/browser/tinyfly.umd.js +4 -4
  18. package/lib/cli/tools.js +137 -22
  19. package/lib/embed/tinyfly-embed.iife.js +4 -4
  20. package/lib/embed/tinyfly-embed.js +921 -833
  21. package/lib/engine/tinyfly-engine.js +1022 -776
  22. package/lib/engine/tinyfly-engine.umd.cjs +2 -2
  23. package/lib/headless/headless.js +410 -8
  24. package/lib/player/tinyfly-player.es.js +459 -371
  25. package/lib/player/tinyfly-player.iife.js +2 -2
  26. package/lib/player/tinyfly-player.umd.js +2 -2
  27. package/lib/types/adapters/canvas/bloom.d.ts +33 -0
  28. package/lib/types/adapters/canvas/canvas-adapter.d.ts +36 -0
  29. package/lib/types/adapters/canvas/index.d.ts +2 -0
  30. package/lib/types/adapters/canvas/offscreen.d.ts +13 -0
  31. package/lib/types/adapters/transform-3d.d.ts +99 -0
  32. package/lib/types/adapters/webgl/webgl-adapter.d.ts +17 -0
  33. package/lib/types/browser/index.d.ts +2 -0
  34. package/lib/types/characters/character.d.ts +20 -1
  35. package/lib/types/characters/index.d.ts +2 -0
  36. package/lib/types/characters/rig/body-plan.d.ts +3 -1
  37. package/lib/types/characters/rig/skeleton.d.ts +57 -0
  38. package/lib/types/characters/scene-objects.d.ts +2 -0
  39. package/lib/types/compat/gsap/vars.d.ts +8 -0
  40. package/lib/types/engine/core/bake.d.ts +3 -1
  41. package/lib/types/engine/index.d.ts +1 -0
  42. package/lib/types/engine/interpolation/interpolators.d.ts +10 -3
  43. package/lib/types/engine/math/index.d.ts +11 -0
  44. package/lib/types/engine/math/mat4.d.ts +37 -0
  45. package/lib/types/engine/math/quat.d.ts +39 -0
  46. package/lib/types/engine/math/vec3.d.ts +16 -0
  47. package/lib/types/engine/types.d.ts +23 -2
  48. package/lib/types/headless/index.d.ts +1 -0
  49. package/lib/types/headless/loop-check.d.ts +41 -0
  50. package/lib/types/headless/node-video.d.ts +8 -0
  51. package/lib/types/headless/video-scene.d.ts +7 -1
  52. package/lib/types/scene-3d/camera.d.ts +27 -0
  53. package/lib/types/scene-3d/canvas-2d-renderer.d.ts +21 -0
  54. package/lib/types/scene-3d/geometry/extrude.d.ts +17 -0
  55. package/lib/types/scene-3d/geometry/mesh.d.ts +36 -0
  56. package/lib/types/scene-3d/geometry/primitives.d.ts +16 -0
  57. package/lib/types/scene-3d/geometry/triangulate.d.ts +24 -0
  58. package/lib/types/scene-3d/index.d.ts +20 -0
  59. package/lib/types/scene-3d/load-scene.d.ts +31 -0
  60. package/lib/types/scene-3d/object-kind.d.ts +54 -0
  61. package/lib/types/scene-3d/resolve-scene.d.ts +122 -0
  62. package/lib/types/scene-3d/scene-3d-adapter.d.ts +46 -0
  63. package/lib/types/scene-3d/scene-types.d.ts +192 -0
  64. package/lib/types/scene-3d/shading.d.ts +20 -0
  65. package/lib/types/scene-3d/validate-scene.d.ts +11 -0
  66. package/lib/types/scene-3d/webgl/gl-data.d.ts +29 -0
  67. package/lib/types/scene-3d/webgl/index.d.ts +16 -0
  68. package/lib/types/scene-3d/webgl/shaders.d.ts +29 -0
  69. package/lib/types/scene-3d/webgl/webgl2-renderer.d.ts +33 -0
  70. package/package.json +10 -2
  71. package/lib/addons/dom-adapter-CrNwoomd.js +0 -243
@@ -0,0 +1,122 @@
1
+ import { type Mat4, type Vec3 } from '../engine/math';
2
+ import type { AnimatableValue } from '../engine/types';
3
+ import type { LoadedScene3D, PreparedMesh } from './load-scene';
4
+ import type { LightObject, Material3D, Scene3D } from './scene-types';
5
+ import type { Drawable } from './object-kind';
6
+ /**
7
+ * From a loaded scene and the animation's values at one moment to plain data
8
+ * a renderer draws: world matrices, the camera, lights in world space, and
9
+ * every visible triangle on screen, sorted far to near. Pure: no canvas, no
10
+ * GPU, the same input always gives the same frame.
11
+ *
12
+ * Tracks address objects as `<sceneId>/<objectId>`:
13
+ * - every object: `x`, `y`, `z` or `position`; `rotateX`, `rotateY`,
14
+ * `rotateZ` (degrees, YXZ) or `quaternion` (a slerp track); `scale` or
15
+ * `scaleX` / `scaleY` / `scaleZ`; `visible` (0 or 1); `opacity`;
16
+ * - meshes and lights: `color`; lights: `intensity`, `target`;
17
+ * - cameras: `fov` (perspective), `height` (orthographic), `lookAt`;
18
+ * - the scene itself, `<sceneId>`: `activeCamera` (a camera's id: a cut is one keyframe).
19
+ */
20
+ /** Animated values by target, then property: an `AnimationState`'s `values`. */
21
+ export type SceneValues = ReadonlyMap<string, ReadonlyMap<string, AnimatableValue>>;
22
+ export interface ScreenPoint {
23
+ x: number;
24
+ y: number;
25
+ }
26
+ export interface ResolvedCamera {
27
+ id: string;
28
+ view: Mat4;
29
+ projection: Mat4;
30
+ /** World position */
31
+ position: Vec3;
32
+ /** The way it looks, unit length */
33
+ forward: Vec3;
34
+ orthographic: boolean;
35
+ near: number;
36
+ far: number;
37
+ }
38
+ export interface ResolvedLight {
39
+ id: string;
40
+ light: LightObject['light'];
41
+ color: string;
42
+ intensity: number;
43
+ position: Vec3;
44
+ /** Directional and spot: the way the light travels, unit length */
45
+ direction: Vec3;
46
+ range?: number;
47
+ /** Spot: the cone's half-angle, degrees */
48
+ angle: number;
49
+ }
50
+ export interface DrawTriangle {
51
+ objectId: string;
52
+ objectIndex: number;
53
+ /** The object's draw-order group: lower first */
54
+ layer: number;
55
+ face: number;
56
+ /** On the canvas, px (y down) */
57
+ screen: [ScreenPoint, ScreenPoint, ScreenPoint];
58
+ /** Distance in front of the camera, metres: what the draw order sorts by */
59
+ depth: number;
60
+ /** World centre of the face (lighting, fog) */
61
+ centroid: Vec3;
62
+ /** World face normal, unit, toward the viewer */
63
+ normal: Vec3;
64
+ /** World vertex normals, for smooth shading */
65
+ vertexNormals: [Vec3, Vec3, Vec3];
66
+ material: Material3D;
67
+ /** The object's own colour, over the material's (an animated `color`) */
68
+ color?: string;
69
+ /** The object's opacity times the material's */
70
+ opacity: number;
71
+ /** Edges to ink on top of this triangle (silhouettes, creases, open rims), when the material has an outline */
72
+ outline: Array<[ScreenPoint, ScreenPoint]>;
73
+ }
74
+ /**
75
+ * A visible mesh in world space, for renderers that keep geometry 3D (the
76
+ * WebGL2 renderer): the scene's meshes and those added kinds place.
77
+ */
78
+ export interface ResolvedMesh {
79
+ objectId: string;
80
+ objectIndex: number;
81
+ mesh: PreparedMesh;
82
+ world: Mat4;
83
+ material: Material3D;
84
+ /** An animated colour over the material's */
85
+ color?: string;
86
+ /** The object's opacity times the material's */
87
+ opacity: number;
88
+ }
89
+ /** A drawable from an added object kind, in the draw order. */
90
+ export interface ResolvedDrawable extends Drawable {
91
+ objectId: string;
92
+ objectIndex: number;
93
+ layer: number;
94
+ }
95
+ export interface ResolvedScene3D {
96
+ id: string;
97
+ width: number;
98
+ height: number;
99
+ /** ms: the moment drawn (for looks that move with time, such as a boiling pencil line) */
100
+ time: number;
101
+ background?: string;
102
+ fog?: Scene3D['fog'];
103
+ camera: ResolvedCamera;
104
+ lights: ResolvedLight[];
105
+ /** World matrices by object id */
106
+ worlds: Map<string, Mat4>;
107
+ /** By layer, then far to near: draw in this order */
108
+ triangles: DrawTriangle[];
109
+ /** Objects of added kinds (characters), each placed among the triangles by layer and depth */
110
+ drawables: ResolvedDrawable[];
111
+ /** Every visible mesh in world space (renderers with a depth buffer draw these instead of `triangles`) */
112
+ meshes: ResolvedMesh[];
113
+ }
114
+ export interface ResolveOptions {
115
+ /** Canvas size, px */
116
+ width: number;
117
+ height: number;
118
+ /** ms: the moment drawn, for looks that move with time (default 0) */
119
+ time?: number;
120
+ }
121
+ /** The frame at these animated values (an `AnimationState`'s `values`, or none for the scene as authored). */
122
+ export declare function resolveScene3D(loaded: LoadedScene3D, values: SceneValues | undefined, options: ResolveOptions): ResolvedScene3D;
@@ -0,0 +1,46 @@
1
+ import type { AnimationState } from '../engine/types';
2
+ import type { LoadedScene3D } from './load-scene';
3
+ import { type SceneValues } from './resolve-scene';
4
+ import { type Renderer3D } from './canvas-2d-renderer';
5
+ /**
6
+ * Plays 3D scenes from a timeline, like the other adapters: `applyState`
7
+ * takes the engine's values, `render` hands each scene's resolved frame to
8
+ * the renderer. Knows nothing about time.
9
+ *
10
+ * ```js
11
+ * const scene = loadScene3D(sceneJson)
12
+ * const adapter = new Scene3DAdapter(new Canvas2DRenderer(ctx), { width: 800, height: 450 })
13
+ * adapter.registerScene(scene)
14
+ * timeline.onUpdate = (state) => { adapter.applyState(state); adapter.render() }
15
+ * ```
16
+ */
17
+ export declare class Scene3DAdapter {
18
+ private readonly renderer;
19
+ private width;
20
+ private height;
21
+ private readonly scenes;
22
+ private values;
23
+ constructor(renderer: Renderer3D, options: {
24
+ width: number;
25
+ height: number;
26
+ });
27
+ /** Add a scene; its tracks address `<scene.id>/<objectId>`. */
28
+ registerScene(scene: LoadedScene3D): void;
29
+ unregisterScene(id: string): void;
30
+ resize(width: number, height: number): void;
31
+ applyState(state: Pick<AnimationState, 'values'> & {
32
+ currentTime?: number;
33
+ }): void;
34
+ private time;
35
+ render(): void;
36
+ }
37
+ /**
38
+ * Draw a scene at these animated values in one call: for a headless video
39
+ * scene's `draw(ctx, frame)` (`drawScene3D(ctx, scene, frame.state?.values, frame)`)
40
+ * or a canvas target's custom draw.
41
+ */
42
+ export declare function drawScene3D(ctx: CanvasRenderingContext2D, scene: LoadedScene3D, values: SceneValues | undefined, size: {
43
+ width: number;
44
+ height: number;
45
+ time?: number;
46
+ }): void;
@@ -0,0 +1,192 @@
1
+ /**
2
+ * A 3D scene as plain JSON: objects in world space (metres, right-handed,
3
+ * +y up, +z toward the default camera), a camera to look through, lights and
4
+ * materials. Its animation is ordinary tracks addressed to
5
+ * `<sceneId>/<objectId>` (see `resolve-scene.ts`), so it plays on any
6
+ * timeline and in any renderer.
7
+ *
8
+ * Angles are degrees; Euler rotations apply in YXZ order (yaw, pitch, roll),
9
+ * as `quat.fromEuler`; quaternions are `[x, y, z, w]`.
10
+ */
11
+ export type Vec3Tuple = [number, number, number];
12
+ export type QuatTuple = [number, number, number, number];
13
+ export interface Scene3D {
14
+ /** The format of this scene (1) */
15
+ formatVersion?: number;
16
+ id: string;
17
+ /** The camera at time 0; a film cuts between cameras with an `activeCamera` track on the scene */
18
+ camera: string;
19
+ /** In order: parents before children; array order breaks depth ties */
20
+ objects: Object3D[];
21
+ materials?: Record<string, Material3D>;
22
+ /** Background colour, or 'transparent' (default) */
23
+ background?: string;
24
+ /** Fog from `near` to `far` metres from the camera, toward `color` */
25
+ fog?: {
26
+ color: string;
27
+ near: number;
28
+ far: number;
29
+ };
30
+ /** Realistic mode: an equirectangular HDR asset for light and reflections (stylized ignores it) */
31
+ environment?: string;
32
+ }
33
+ export interface Transform3D {
34
+ /** Metres; default [0, 0, 0] */
35
+ position?: Vec3Tuple;
36
+ /** Euler degrees, YXZ order (use `quaternion` instead for tumbling) */
37
+ rotation?: Vec3Tuple;
38
+ quaternion?: QuatTuple;
39
+ /** One number for all three axes, or per axis; default 1 */
40
+ scale?: Vec3Tuple | number;
41
+ }
42
+ export interface ObjectBase extends Transform3D {
43
+ /** Unique in the scene; tracks address it as `<sceneId>/<id>` */
44
+ id: string;
45
+ name?: string;
46
+ /** Id of an earlier object this one moves with */
47
+ parent?: string;
48
+ /** Default true */
49
+ visible?: boolean;
50
+ /**
51
+ * Draw-order group (default 0): lower layers draw first, whatever their
52
+ * depth, then far to near within a layer. A floor at -1 never covers what
53
+ * stands on it.
54
+ */
55
+ layer?: number;
56
+ }
57
+ export interface GroupObject extends ObjectBase {
58
+ kind: 'group';
59
+ }
60
+ export interface MeshObject extends ObjectBase {
61
+ kind: 'mesh';
62
+ geometry: Geometry3D;
63
+ /** A key of `materials` */
64
+ material: string;
65
+ }
66
+ export interface PerspectiveCamera extends ObjectBase {
67
+ kind: 'camera';
68
+ projection: 'perspective';
69
+ /** Vertical field of view, degrees */
70
+ fov: number;
71
+ /** Nearest and furthest distances drawn, metres */
72
+ near: number;
73
+ far: number;
74
+ /** A point to look at, overriding the camera's rotation */
75
+ lookAt?: Vec3Tuple;
76
+ }
77
+ export interface OrthographicCamera extends ObjectBase {
78
+ kind: 'camera';
79
+ projection: 'orthographic';
80
+ /** Height of the view, metres */
81
+ height: number;
82
+ near: number;
83
+ far: number;
84
+ lookAt?: Vec3Tuple;
85
+ }
86
+ export type CameraObject = PerspectiveCamera | OrthographicCamera;
87
+ export interface LightObject extends ObjectBase {
88
+ kind: 'light';
89
+ light: 'ambient' | 'directional' | 'point' | 'spot';
90
+ color: string;
91
+ /**
92
+ * Strength. Stylized renderers read it as a multiplier (1 is full light);
93
+ * the realistic renderer maps it to physical units.
94
+ */
95
+ intensity: number;
96
+ /** Directional and spot: they shine from their position toward this point (default the origin) */
97
+ target?: Vec3Tuple;
98
+ /** Point and spot: the light fades to nothing at this distance, metres (default: no fade) */
99
+ range?: number;
100
+ /** Spot: the cone's half-angle, degrees (default 30) */
101
+ angle?: number;
102
+ /** Realistic mode: casts shadow maps */
103
+ castShadow?: boolean;
104
+ }
105
+ /**
106
+ * A character (tinyfly's v2 human) standing in the scene. Drawing it needs the
107
+ * characters add-on: `loadScene3D(scene, { kinds: [characterObjects] })` with
108
+ * `characterObjects` from `@algorisys/tinyfly/characters`.
109
+ */
110
+ export interface CharacterObject3D extends ObjectBase {
111
+ kind: 'character';
112
+ /** How it looks: `character()` options (figure, look, ink, skin, hands, …) */
113
+ character?: Record<string, unknown>;
114
+ /** Height, metres (default 1.7) */
115
+ height?: number;
116
+ /** Its pose: character pose fields (`arm.right.spread`, `turn`, …); tracks animate them */
117
+ pose?: Record<string, number>;
118
+ /** A soft shadow on the ground under it (default true) */
119
+ shadow?: boolean;
120
+ /**
121
+ * `pen` (default): drawn by its pens, in its 2D look (clean, pencil,
122
+ * silhouette) seen in perspective. `solid`: built of shaded capsules and an
123
+ * ellipsoid head, lit and outlined like the scene's meshes.
124
+ */
125
+ look?: 'pen' | 'solid';
126
+ /** The solid look's colours and shading (default: a slate body, skin, toon with ink) */
127
+ solid?: {
128
+ color?: string;
129
+ skin?: string;
130
+ shading?: 'unlit' | 'flat' | 'lambert' | 'toon';
131
+ outline?: {
132
+ width: number;
133
+ color: string;
134
+ } | false;
135
+ };
136
+ }
137
+ export type Object3D = GroupObject | MeshObject | CameraObject | LightObject | CharacterObject3D;
138
+ export type Geometry3D = {
139
+ type: 'box';
140
+ size: Vec3Tuple;
141
+ } | {
142
+ type: 'sphere';
143
+ radius: number;
144
+ segments?: number;
145
+ } | {
146
+ type: 'cylinder';
147
+ radius: number;
148
+ height: number;
149
+ segments?: number;
150
+ } | {
151
+ type: 'cone';
152
+ radius: number;
153
+ height: number;
154
+ segments?: number;
155
+ } | {
156
+ type: 'plane';
157
+ size: [number, number];
158
+ segments?: number;
159
+ } | {
160
+ type: 'torus';
161
+ radius: number;
162
+ tube: number;
163
+ segments?: number;
164
+ }
165
+ /** An SVG path made solid, `depth` thick, scaled to `width` metres wide (default 1); subpaths inside others are holes */
166
+ | {
167
+ type: 'extrude';
168
+ path: string;
169
+ depth: number;
170
+ width?: number;
171
+ curveSegments?: number;
172
+ };
173
+ export interface Material3D {
174
+ color: string;
175
+ /** 0..1, default 1 */
176
+ opacity?: number;
177
+ /** Draw both sides (default: only the front, the side the outward normal faces) */
178
+ doubleSided?: boolean;
179
+ /** unlit: the colour as is; flat: lit per face; lambert: lit, smoothed; toon: lit in bands */
180
+ shading?: 'unlit' | 'flat' | 'lambert' | 'toon';
181
+ /** Toon: number of light bands (default 3) */
182
+ bands?: number;
183
+ /** Ink outline along silhouettes and creases, px */
184
+ outline?: {
185
+ width: number;
186
+ color: string;
187
+ };
188
+ /** Light the surface gives off itself, added after lighting (both modes): neon, screens, lamps; feeds bloom */
189
+ emissive?: string;
190
+ roughness?: number;
191
+ metalness?: number;
192
+ }
@@ -0,0 +1,20 @@
1
+ import { type Vec3 } from '../engine/math';
2
+ import type { DrawTriangle, ResolvedCamera, ResolvedLight, ResolvedScene3D } from './resolve-scene';
3
+ /**
4
+ * Stylized shading, pure: the colour of one triangle under the scene's
5
+ * lights, as flat, smooth (lambert) or banded (toon) light, then fog. Light
6
+ * intensity is a multiplier here (1 is full light); a point or spot light
7
+ * fades to nothing at its `range`.
8
+ */
9
+ export type Rgb = [number, number, number];
10
+ /** `#rgb`, `#rrggbb`, `rgb()` / `rgba()` as 0..1 channels; anything else is white. */
11
+ export declare function parseColor(color: string): Rgb;
12
+ /** The light reaching point `p` with normal `n`, as RGB (can exceed 1). */
13
+ export declare function lightAt(p: Vec3, n: Vec3, lights: ResolvedLight[]): Rgb;
14
+ /** How much fog covers a point `distance` metres from the camera, 0..1. */
15
+ export declare function fogAmount(fog: ResolvedScene3D['fog'], distance: number): number;
16
+ /** The fill of one triangle: a CSS colour and its opacity. */
17
+ export declare function shadeTriangle(triangle: DrawTriangle, lights: ResolvedLight[], camera: ResolvedCamera, fog?: ResolvedScene3D['fog']): {
18
+ color: string;
19
+ alpha: number;
20
+ };
@@ -0,0 +1,11 @@
1
+ import type { Scene3D } from './scene-types';
2
+ /**
3
+ * Problems with a scene, as plain sentences (empty when it is fine): ids,
4
+ * parents, the camera, materials, sizes. With `tracks`, also a channel
5
+ * animated two ways at once (x and position, rotateY and quaternion), which
6
+ * would otherwise need a hidden rule to pick a winner.
7
+ */
8
+ export declare function validateScene3D(scene: Scene3D, tracks?: ReadonlyArray<{
9
+ target: string;
10
+ property: string;
11
+ }>): string[];
@@ -0,0 +1,29 @@
1
+ import type { ResolvedLight } from '../resolve-scene';
2
+ import type { Material3D } from '../scene-types';
3
+ /**
4
+ * Pure packing for the WebGL2 renderer: lights into uniform arrays, materials
5
+ * into shader numbers, normal matrices. No GL here, so it is tested in Node.
6
+ */
7
+ export interface LightUniforms {
8
+ count: number;
9
+ kind: Int32Array;
10
+ color: Float32Array;
11
+ intensity: Float32Array;
12
+ position: Float32Array;
13
+ direction: Float32Array;
14
+ range: Float32Array;
15
+ /** Spot cone: cos(outer angle), cos(inner angle) */
16
+ cone: Float32Array;
17
+ }
18
+ /** The scene's lights as uniform arrays (the first {@link MAX_LIGHTS}; the default pair when there are none). */
19
+ export declare function packLights(lights: ResolvedLight[]): LightUniforms;
20
+ /** A material's numbers for the surface shader, with an object's own colour and opacity over it. */
21
+ export declare function surfaceUniforms(material: Material3D, color: string | undefined, opacity: number): {
22
+ color: import("../shading").Rgb;
23
+ emissive: [number, number, number];
24
+ opacity: number;
25
+ shading: 0 | 2 | 1 | 3;
26
+ bands: number;
27
+ };
28
+ /** The 3×3 normal matrix (inverse transpose of the upper 3×3), column-major, from a column-major 4×4. */
29
+ export declare function normalMatrix3(m: number[]): Float32Array;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * `@algorisys/tinyfly/scene-3d/webgl`: the WebGL2 renderer for 3D scenes. A
3
+ * depth buffer, light per pixel and inverted-hull outlines, for scenes too
4
+ * big or too tangled for the Canvas 2D renderer. Its own entry, so pages
5
+ * without it never download it.
6
+ *
7
+ * ```js
8
+ * import { loadScene3D, resolveScene3D } from '@algorisys/tinyfly/scene-3d'
9
+ * import { WebGL2Renderer } from '@algorisys/tinyfly/scene-3d/webgl'
10
+ * const renderer = new WebGL2Renderer(canvas.getContext('webgl2'), { overlay })
11
+ * renderer.render(resolveScene3D(scene, values, { width, height }))
12
+ * ```
13
+ */
14
+ export { WebGL2Renderer } from './webgl2-renderer';
15
+ export { packLights, surfaceUniforms, normalMatrix3, type LightUniforms } from './gl-data';
16
+ export { MAX_LIGHTS, SHADING_CODE, LIGHT_CODE } from './shaders';
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The WebGL2 renderer's shaders (GLSL ES 3.0). The light is the stylized
3
+ * light of `shading.ts`, per pixel: ambient, directional, point and spot
4
+ * lights fading at their range, unlit / flat / smooth / toon, then fog.
5
+ */
6
+ export declare const MAX_LIGHTS = 8;
7
+ /** Shading modes as the fragment shader numbers them. */
8
+ export declare const SHADING_CODE: {
9
+ readonly unlit: 0;
10
+ readonly flat: 1;
11
+ readonly lambert: 2;
12
+ readonly toon: 3;
13
+ };
14
+ /** Light kinds as the fragment shader numbers them. */
15
+ export declare const LIGHT_CODE: {
16
+ readonly ambient: 0;
17
+ readonly directional: 1;
18
+ readonly point: 2;
19
+ readonly spot: 3;
20
+ };
21
+ export declare const SURFACE_VERTEX = "#version 300 es\nin vec3 a_position;\nin vec3 a_normal;\nuniform mat4 u_model;\nuniform mat4 u_view;\nuniform mat4 u_projection;\nuniform mat3 u_normalMatrix;\nout vec3 v_world;\nout vec3 v_normal;\nvoid main() {\n vec4 world = u_model * vec4(a_position, 1.0);\n v_world = world.xyz;\n v_normal = u_normalMatrix * a_normal;\n gl_Position = u_projection * u_view * world;\n}\n";
22
+ export declare const SURFACE_FRAGMENT = "#version 300 es\nprecision highp float;\n#define MAX_LIGHTS 8\nuniform int u_lightCount;\nuniform int u_lightKind[MAX_LIGHTS];\nuniform vec3 u_lightColor[MAX_LIGHTS];\nuniform float u_lightIntensity[MAX_LIGHTS];\nuniform vec3 u_lightPosition[MAX_LIGHTS];\nuniform vec3 u_lightDirection[MAX_LIGHTS];\nuniform float u_lightRange[MAX_LIGHTS];\nuniform vec2 u_lightCone[MAX_LIGHTS];\nuniform vec3 u_color;\nuniform vec3 u_emissive;\nuniform float u_opacity;\nuniform int u_shading;\nuniform float u_bands;\nuniform vec3 u_eye;\nuniform vec3 u_forward;\nuniform bool u_orthographic;\nuniform bool u_fog;\nuniform vec3 u_fogColor;\nuniform vec2 u_fogRange;\nin vec3 v_world;\nin vec3 v_normal;\nout vec4 outColor;\n\nvec3 lightAt(vec3 p, vec3 n) {\n vec3 total = vec3(0.0);\n for (int i = 0; i < MAX_LIGHTS; i++) {\n if (i >= u_lightCount) break;\n float amount;\n int kind = u_lightKind[i];\n if (kind == 0) {\n amount = u_lightIntensity[i];\n } else if (kind == 1) {\n amount = u_lightIntensity[i] * max(0.0, -dot(n, u_lightDirection[i]));\n } else {\n vec3 toLight = u_lightPosition[i] - p;\n float distance = length(toLight);\n vec3 l = distance > 0.0 ? toLight / distance : n;\n float fade = u_lightRange[i] > 0.0 ? pow(max(0.0, 1.0 - distance / u_lightRange[i]), 2.0) : 1.0;\n amount = u_lightIntensity[i] * max(0.0, dot(n, l)) * fade;\n if (kind == 3) amount *= smoothstep(u_lightCone[i].x, u_lightCone[i].y, -dot(l, u_lightDirection[i]));\n }\n total += u_lightColor[i] * amount;\n }\n return total;\n}\n\nvoid main() {\n vec3 rgb = u_color;\n if (u_shading != 0) {\n // Flat: the face's own normal, from the surface's slope; otherwise the smooth one.\n vec3 n = u_shading == 1 ? normalize(cross(dFdx(v_world), dFdy(v_world))) : normalize(v_normal);\n vec3 toEye = u_orthographic ? -u_forward : normalize(u_eye - v_world);\n if (dot(n, toEye) < 0.0) n = -n;\n vec3 light = lightAt(v_world, n);\n if (u_shading == 3) {\n // Band the brightness, not each channel, so coloured lights keep their hue.\n float level = max(light.r, max(light.g, light.b));\n float steps = max(2.0, floor(u_bands + 0.5));\n float banded = min(1.2, ceil(min(1.0, level) * steps - 1e-6) / steps);\n light = level > 0.0 ? light * (banded / level) : vec3(0.0);\n }\n rgb *= light;\n }\n rgb += u_emissive;\n if (u_fog) {\n float amount = clamp((distance(v_world, u_eye) - u_fogRange.x) / (u_fogRange.y - u_fogRange.x), 0.0, 1.0);\n rgb = mix(rgb, u_fogColor, amount);\n }\n outColor = vec4(clamp(rgb, 0.0, 1.0), u_opacity);\n}\n";
23
+ /**
24
+ * Ink outlines by the inverted hull: each mesh again, its back faces only,
25
+ * pushed out along their normals by the outline's width in pixels, in the
26
+ * ink colour. Behind the surface, only the rim shows: a silhouette.
27
+ */
28
+ export declare const OUTLINE_VERTEX = "#version 300 es\nin vec3 a_position;\nin vec3 a_normal;\nuniform mat4 u_model;\nuniform mat4 u_view;\nuniform mat4 u_projection;\nuniform mat3 u_normalMatrix;\nuniform vec2 u_viewport;\nuniform float u_width;\nvoid main() {\n mat4 viewProjection = u_projection * u_view;\n vec4 clip = viewProjection * u_model * vec4(a_position, 1.0);\n vec3 worldNormal = normalize(u_normalMatrix * a_normal);\n vec4 tip = viewProjection * (u_model * vec4(a_position, 1.0) + vec4(worldNormal * 0.01, 0.0));\n vec2 direction = tip.xy / tip.w - clip.xy / clip.w;\n float length2 = dot(direction, direction);\n vec2 push = length2 > 0.0 ? direction * inversesqrt(length2) : vec2(0.0);\n clip.xy += push * u_width * 2.0 / u_viewport * clip.w;\n gl_Position = clip;\n}\n";
29
+ export declare const OUTLINE_FRAGMENT = "#version 300 es\nprecision highp float;\nuniform vec4 u_ink;\nout vec4 outColor;\nvoid main() {\n outColor = u_ink;\n}\n";
@@ -0,0 +1,33 @@
1
+ import type { ResolvedScene3D } from '../resolve-scene';
2
+ /**
3
+ * Draws a resolved 3D scene with WebGL2: the same frame the Canvas 2D
4
+ * renderer paints, but with a depth buffer (shapes that cut through each
5
+ * other are drawn right), light per pixel (smooth curves, toon bands, fog per
6
+ * pixel) and ink outlines by the inverted hull. Opaque meshes first, then
7
+ * see-through ones far to near.
8
+ *
9
+ * Added objects drawn whole (a character in its pen look, its shadow) are not
10
+ * meshes: they go on `overlay`, a 2D canvas laid over this one, after the
11
+ * meshes (so in front of them). Characters in the solid look are meshes and
12
+ * sort properly.
13
+ *
14
+ * Works on a page canvas or an OffscreenCanvas in a Worker. Not in Node
15
+ * (there is no WebGL2 there): headless video uses the Canvas 2D renderer.
16
+ */
17
+ export declare class WebGL2Renderer {
18
+ private readonly gl;
19
+ private readonly surface;
20
+ private readonly outline;
21
+ private readonly buffers;
22
+ private readonly overlay?;
23
+ constructor(gl: WebGL2RenderingContext, options?: {
24
+ overlay?: CanvasRenderingContext2D;
25
+ });
26
+ render(frame: ResolvedScene3D): void;
27
+ private drawSurface;
28
+ private drawOutline;
29
+ /** Upload a mesh once; bind its buffers to the program's attributes. */
30
+ private bindMesh;
31
+ /** Release the programs (mesh buffers go with their meshes). */
32
+ destroy(): void;
33
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@algorisys/tinyfly",
3
- "version": "0.82.0",
3
+ "version": "0.87.0",
4
4
  "type": "module",
5
5
  "description": "Lightweight, API-driven animation engine and editor. The engine is framework-agnostic and runs in browsers, Web Workers, and Node.",
6
6
  "license": "MIT",
@@ -105,6 +105,14 @@
105
105
  "types": "./lib/types/maps/index.d.ts",
106
106
  "import": "./lib/addons/maps.js"
107
107
  },
108
+ "./scene-3d": {
109
+ "types": "./lib/types/scene-3d/index.d.ts",
110
+ "import": "./lib/addons/scene-3d.js"
111
+ },
112
+ "./scene-3d/webgl": {
113
+ "types": "./lib/types/scene-3d/webgl/index.d.ts",
114
+ "import": "./lib/addons/scene-3d-webgl.js"
115
+ },
108
116
  "./headless": {
109
117
  "types": "./lib/types/headless/index.d.ts",
110
118
  "import": "./lib/headless/headless.js"
@@ -137,7 +145,7 @@
137
145
  "test": "vitest",
138
146
  "test:run": "vitest run",
139
147
  "build:addons": "vite build --config vite.config.addons.ts",
140
- "build:browser": "vite build --config vite.config.browser.ts && vite build --config vite.config.browser-maps.ts",
148
+ "build:browser": "vite build --config vite.config.browser.ts && vite build --config vite.config.browser-maps.ts && vite build --config vite.config.browser-scene-3d.ts && vite build --config vite.config.browser-scene-3d-webgl.ts",
141
149
  "prepublishOnly": "npm run test:run && npm run build:libs",
142
150
  "e2e": "node e2e/run.mjs",
143
151
  "release:oss": "./scripts/publish-oss.sh --push",