@margoengine/gltf 0.0.0-stage → 0.1.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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1438 -3
- package/dist/accessor-DuqjpuxI.js +1 -0
- package/dist/accessor-nVsu_f1L.js +1 -0
- package/dist/accessor.d.ts +12 -0
- package/dist/asset-1b8ejkZX.js +4 -0
- package/dist/asset-entry.d.ts +1 -0
- package/dist/asset-entry.js +1 -0
- package/dist/asset.d.ts +319 -0
- package/dist/audio.d.ts +47 -0
- package/dist/audio.js +1 -0
- package/dist/auto.d.ts +29 -0
- package/dist/auto.js +1 -0
- package/dist/base-textures.d.ts +20 -0
- package/dist/coalesce.d.ts +29 -0
- package/dist/compression-D4IkO5vE.js +1 -0
- package/dist/compression.d.ts +15 -0
- package/dist/coordinates.d.ts +1 -0
- package/dist/coordinates.js +1 -0
- package/dist/dds.d.ts +7 -0
- package/dist/dds.js +1 -0
- package/dist/decoder-default-CxLH8Vz0.js +1 -0
- package/dist/decoder-default.d.ts +5 -0
- package/dist/default-materials.d.ts +8 -0
- package/dist/draco-state.d.ts +7 -0
- package/dist/draw-batch.d.ts +83 -0
- package/dist/draw-memo.d.ts +136 -0
- package/dist/errors-THVaUwlL.js +1 -0
- package/dist/extended.d.ts +12 -0
- package/dist/extended.js +1 -0
- package/dist/gltf.worker.d.ts +1 -0
- package/dist/group.d.ts +6 -0
- package/dist/image-limit-DvFSfR5T.js +1 -0
- package/dist/image-limit.d.ts +6 -0
- package/dist/index.d.ts +84 -0
- package/dist/index.js +1 -0
- package/dist/instanced-shader-BebTnbkj.js +5 -0
- package/dist/instanced-shader.d.ts +14 -0
- package/dist/instancing.d.ts +9 -0
- package/dist/instancing.js +1 -0
- package/dist/interactivity-graph.d.ts +193 -0
- package/dist/interactivity-model.d.ts +94 -0
- package/dist/interactivity-ops.d.ts +20 -0
- package/dist/interactivity.d.ts +33 -0
- package/dist/interactivity.js +1 -0
- package/dist/lights.d.ts +98 -0
- package/dist/lights.js +1 -0
- package/dist/limits-_LZ441oV.js +1 -0
- package/dist/material-draw-state-BHeTb1FP.js +1 -0
- package/dist/material-draw-state.d.ts +7 -0
- package/dist/material-draw.d.ts +18 -0
- package/dist/material-draw.js +1 -0
- package/dist/material-extension.d.ts +96 -0
- package/dist/mesh-source-DvU8YvMc.js +1 -0
- package/dist/meshopt.d.ts +9 -0
- package/dist/meshopt.js +1 -0
- package/dist/metadata.d.ts +114 -0
- package/dist/metadata.js +1 -0
- package/dist/model-entry.d.ts +2 -0
- package/dist/model-entry.js +1 -0
- package/dist/model-iRvuj19h.js +39 -0
- package/dist/model.d.ts +1011 -0
- package/dist/node-image-decode.d.ts +6 -0
- package/dist/node-matrix-R9BipGnp.js +1 -0
- package/dist/node-matrix.d.ts +16 -0
- package/dist/occlusion.d.ts +8 -0
- package/dist/optimize-7SEzNA23.js +1 -0
- package/dist/pack-BVDYuqZe.js +1 -0
- package/dist/pack.d.ts +27 -0
- package/dist/parts.d.ts +54 -0
- package/dist/pbr-instancing.d.ts +83 -0
- package/dist/pbr-instancing.js +1 -0
- package/dist/pbr.d.ts +13 -0
- package/dist/pbr.js +1 -0
- package/dist/physics.d.ts +148 -0
- package/dist/physics.js +1 -0
- package/dist/prepare.d.ts +26 -0
- package/dist/prepare.js +1 -0
- package/dist/progressive.d.ts +59 -0
- package/dist/progressive.js +4 -0
- package/dist/progressive.worker.d.ts +1 -0
- package/dist/quantized.d.ts +8 -0
- package/dist/quantized.js +1 -0
- package/dist/render-state.d.ts +18 -0
- package/dist/replica.d.ts +37 -0
- package/dist/report.d.ts +128 -0
- package/dist/report.js +1 -0
- package/dist/scene-data.d.ts +61 -0
- package/dist/scene-data.js +1 -0
- package/dist/scene-scope-CZDEkd24.js +4 -0
- package/dist/scene.d.ts +61 -0
- package/dist/scene.js +4 -0
- package/dist/shader-DPqfUcmp.js +405 -0
- package/dist/shader.d.ts +222 -0
- package/dist/shared/caster.d.ts +152 -0
- package/dist/shared/gltf.d.ts +257 -0
- package/dist/shared/iridescence.d.ts +2 -0
- package/dist/shared/prepared-geometry.d.ts +8 -0
- package/dist/side-shader.d.ts +1 -0
- package/dist/soft-mesh.d.ts +94 -0
- package/dist/soft-mesh.js +4 -0
- package/dist/spec-gloss.d.ts +21 -0
- package/dist/spec-gloss.js +18 -0
- package/dist/splat-state.d.ts +7 -0
- package/dist/splats.d.ts +18 -0
- package/dist/splats.js +1 -0
- package/dist/src-WMRHArDp.js +4 -0
- package/dist/stream-C3bKMy1B.js +1 -0
- package/dist/stream.d.ts +87 -0
- package/dist/texture-coordinates-XFsEMdU6.js +2 -0
- package/dist/texture-coordinates.d.ts +26 -0
- package/dist/texture-stream-Cu_XpyVO.js +4 -0
- package/dist/texture-stream.d.ts +88 -0
- package/dist/textured.d.ts +34 -0
- package/dist/textured.js +5 -0
- package/dist/uniform-arena.d.ts +78 -0
- package/dist/unit-light-DvtsuFYP.js +109 -0
- package/dist/version-BmU1nesQ.js +1 -0
- package/dist/version.d.ts +8 -0
- package/dist/vertex-inputs.d.ts +47 -0
- package/dist/view-uniform.d.ts +13 -0
- package/dist/visibility-C_Oqz9Sr.js +1 -0
- package/dist/worker-CY1TQnYX.js +1 -0
- package/dist/worker.d.ts +21 -0
- package/dist/worker.js +4 -0
- package/dist/workers/gltf.js +322 -0
- package/dist/workers/progressive.js +1 -0
- package/package.json +213 -6
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { PassContext } from '@margoengine/core/extension';
|
|
2
|
+
type Context = Extract<PassContext, {
|
|
3
|
+
backend: 'webgpu';
|
|
4
|
+
}>;
|
|
5
|
+
/**
|
|
6
|
+
* Draws a model's PBR shader into a caller-owned render pass on another device: prepare() encodes deformation, draw() appends the draw calls, dispose() releases the replica.
|
|
7
|
+
*/
|
|
8
|
+
export interface PBRDeviceRenderer {
|
|
9
|
+
/** Encode deformation on this renderer's device before opening the eye pass. */
|
|
10
|
+
prepare(context: Pick<Context, 'backend' | 'device' | 'encoder'>): void;
|
|
11
|
+
/**
|
|
12
|
+
* Append the ordinary PBR shader to a caller-owned forward-Z eye pass. Returns the pass that is open afterwards: `pass`
|
|
13
|
+
* itself, or the one `options.snapshot` reopened. Keep recording on the returned pass.
|
|
14
|
+
*/
|
|
15
|
+
draw(context: Context, pass: GPURenderPassEncoder, view: ArrayLike<number>, options?: PBRDeviceDrawOptions): GPURenderPassEncoder;
|
|
16
|
+
dispose(): void;
|
|
17
|
+
}
|
|
18
|
+
/** Options of {@link PBRDeviceRenderer.draw}. */
|
|
19
|
+
export interface PBRDeviceDrawOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Lets glass (KHR_materials_transmission) on a display-encoded target (any format but `rgba16float`) add its light to
|
|
22
|
+
* the light behind it in linear values. Before each glass surface the renderer calls it with the open pass and a
|
|
23
|
+
* texture of the context's width, height and format: end `pass`, write the colour drawn so far into `target` (the
|
|
24
|
+
* resolved colour for a multisampled pass, for example with `copyTextureToTexture`), and return a pass reopened on the
|
|
25
|
+
* same attachments with `loadOp: 'load'`, viewport and scissor. Default: unset, and glass multiplies and adds in the
|
|
26
|
+
* target's stored values, exact only on a linear `rgba16float` target.
|
|
27
|
+
*/
|
|
28
|
+
snapshot?(pass: GPURenderPassEncoder, target: GPUTexture): GPURenderPassEncoder;
|
|
29
|
+
}
|
|
30
|
+
/** Encodes only a model's skinning and morph deformation on another device; dispose() releases it. */
|
|
31
|
+
export interface PBRDeformationRenderer {
|
|
32
|
+
prepare(context: Pick<Context, 'backend' | 'device' | 'encoder'>): void;
|
|
33
|
+
dispose(): void;
|
|
34
|
+
}
|
|
35
|
+
/** Own only replica resources. The source retains its nodes, decoded images and device. */
|
|
36
|
+
export declare function deviceRenderer(device: GPUDevice, prepare: PBRDeviceRenderer['prepare'], draw: PBRDeviceRenderer['draw'], release: () => void, onDispose: (cleanup: () => void) => () => void): PBRDeviceRenderer;
|
|
37
|
+
export {};
|
package/dist/report.d.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@margoengine/gltf/report`: what a glTF 2.0 file holds and what is wrong with it, without a GPU (GAP-FILM-GLTFREPORT).
|
|
3
|
+
* Counts, world bounds of what the default scene draws, extensions, image sizes and problems, read with the loader's own
|
|
4
|
+
* accessor reader; with `alpha`, the drawn bounds of cut-out (MASK and BLEND) primitives measured by texture alpha.
|
|
5
|
+
*
|
|
6
|
+
* Sources: the glTF 2.0 specification (Khronos, 2021: sections 3.5 to 3.9 for nodes, meshes, accessors and materials,
|
|
7
|
+
* 3.7.2.1 for the triangle modes, 3.9.4 for alpha coverage); PNG (W3C, 2003, IHDR), JPEG (ITU-T T.81, SOFn markers),
|
|
8
|
+
* WebP (RFC 9649) and KTX 2.0 (Khronos) headers for image sizes; the signed volume of a closed triangle mesh as a sum
|
|
9
|
+
* of signed tetrahedra (Zhang and Chen 2001, "Efficient feature extraction for 2D/3D objects in mesh representation",
|
|
10
|
+
* ICIP). The rival feature check is in docs/clean-room.md (FE-4).
|
|
11
|
+
*/
|
|
12
|
+
import { type GLTFDecoderHooks } from './asset.js';
|
|
13
|
+
export type { GLTFDecoderHooks } from './asset.js';
|
|
14
|
+
/** One problem found, with a stable `code` and a sentence naming where and what. */
|
|
15
|
+
export interface GLTFProblem {
|
|
16
|
+
code: 'not-gltf' | 'json' | 'extension' | 'external' | 'image-format' | 'accessor' | 'index-range' | 'not-finite' | 'no-normals' | 'no-uvs' | 'no-material' | 'zero-area' | 'inside-out' | 'decode' | 'empty';
|
|
17
|
+
detail: string;
|
|
18
|
+
/** The node or mesh name the problem belongs to, when it has one. */
|
|
19
|
+
where?: string;
|
|
20
|
+
}
|
|
21
|
+
/** Axis-aligned world bounds in metres. */
|
|
22
|
+
export interface GLTFBounds {
|
|
23
|
+
min: [number, number, number];
|
|
24
|
+
max: [number, number, number];
|
|
25
|
+
size: [number, number, number];
|
|
26
|
+
centre: [number, number, number];
|
|
27
|
+
}
|
|
28
|
+
/** One image: index, name, the format its header says, and its size in pixels (0 when unread). */
|
|
29
|
+
export interface GLTFImageReport {
|
|
30
|
+
index: number;
|
|
31
|
+
name?: string;
|
|
32
|
+
format: 'png' | 'jpeg' | 'webp' | 'ktx2' | 'unknown' | 'external';
|
|
33
|
+
width: number;
|
|
34
|
+
height: number;
|
|
35
|
+
}
|
|
36
|
+
/** What `reportGLTF` returns. */
|
|
37
|
+
export interface GLTFReport {
|
|
38
|
+
/** True when `problems` is empty. */
|
|
39
|
+
ok: boolean;
|
|
40
|
+
counts: {
|
|
41
|
+
/** Triangles the default scene draws, each instance counted (mesh instancing and node reuse). */
|
|
42
|
+
triangles: number;
|
|
43
|
+
/** Vertices of those draws, each instance counted. */
|
|
44
|
+
vertices: number;
|
|
45
|
+
/** Primitives drawn, each instance counted. */
|
|
46
|
+
primitives: number;
|
|
47
|
+
meshes: number;
|
|
48
|
+
nodes: number;
|
|
49
|
+
materials: number;
|
|
50
|
+
textures: number;
|
|
51
|
+
images: number;
|
|
52
|
+
/** Sum of every image's width x height. */
|
|
53
|
+
texturePixels: number;
|
|
54
|
+
/** The largest image side in pixels. */
|
|
55
|
+
largestTexture: number;
|
|
56
|
+
/** Bytes the images take as uncompressed RGBA8 (texturePixels x 4, no mip chain). */
|
|
57
|
+
rgbaBytes: number;
|
|
58
|
+
/** Drawn triangles whose area is under `zeroArea` square metres. */
|
|
59
|
+
zeroArea: number;
|
|
60
|
+
/** Points and lines primitives drawn (modes 0 to 3), which hold no triangles. */
|
|
61
|
+
pointsAndLines: number;
|
|
62
|
+
};
|
|
63
|
+
/** World bounds of every drawn triangle vertex, or null when nothing is drawn. */
|
|
64
|
+
bounds: GLTFBounds | null;
|
|
65
|
+
/** Bounds of what cut-out primitives draw where their texture alpha passes, with `alpha: true`; else undefined. */
|
|
66
|
+
cutouts?: {
|
|
67
|
+
primitives: number;
|
|
68
|
+
bounds: GLTFBounds | null;
|
|
69
|
+
samples: number;
|
|
70
|
+
};
|
|
71
|
+
extensions: {
|
|
72
|
+
used: string[];
|
|
73
|
+
required: string[];
|
|
74
|
+
unsupported: string[];
|
|
75
|
+
};
|
|
76
|
+
images: GLTFImageReport[];
|
|
77
|
+
/** Each scene node that draws zero-area triangles, in scene order, so a repair knows which meshes to open. */
|
|
78
|
+
zeroAreaByNode: GLTFZeroArea[];
|
|
79
|
+
problems: GLTFProblem[];
|
|
80
|
+
}
|
|
81
|
+
/** The zero-area triangles one scene node draws. */
|
|
82
|
+
export interface GLTFZeroArea {
|
|
83
|
+
/** The node's index. */
|
|
84
|
+
node: number;
|
|
85
|
+
/** The mesh it draws. */
|
|
86
|
+
mesh: number;
|
|
87
|
+
/** The node's name, or `node with mesh <mesh>`. */
|
|
88
|
+
where: string;
|
|
89
|
+
/** Zero-area triangles it draws, each instance counted. */
|
|
90
|
+
zeroArea: number;
|
|
91
|
+
/** Triangles of its primitives that hold them, each instance counted. */
|
|
92
|
+
triangles: number;
|
|
93
|
+
}
|
|
94
|
+
/** Options of `reportGLTF`. */
|
|
95
|
+
export interface GLTFReportOptions {
|
|
96
|
+
/** Aborts the report. */
|
|
97
|
+
signal?: AbortSignal;
|
|
98
|
+
/** A triangle under this area (square metres) counts as zero area. Default 1e-9. */
|
|
99
|
+
zeroArea?: number;
|
|
100
|
+
/**
|
|
101
|
+
* Decode the base colour textures of MASK and BLEND primitives and measure where their alpha passes (MASK: the
|
|
102
|
+
* material's `alphaCutoff`, default 0.5; BLEND: `blendCutoff`), for `cutouts`. Default false.
|
|
103
|
+
*/
|
|
104
|
+
alpha?: boolean;
|
|
105
|
+
/** Alpha at or above which a BLEND texel counts as drawn. Default 0.5. */
|
|
106
|
+
blendCutoff?: number;
|
|
107
|
+
/** Most texel samples taken over all cut-out triangles; past it each triangle is sampled more coarsely. Default 4,194,304. */
|
|
108
|
+
maxSamples?: number;
|
|
109
|
+
/** Required extensions a later load supports (a decoder hook, a materials entry): not listed as unsupported. */
|
|
110
|
+
supported?: readonly string[];
|
|
111
|
+
/**
|
|
112
|
+
* Decoders for compressed files, as `loadGLTF` takes them. Default: the built-in decoders of
|
|
113
|
+
* `@margoengine/decoders`, imported only for a file that needs one.
|
|
114
|
+
*/
|
|
115
|
+
decoders?: GLTFDecoderHooks;
|
|
116
|
+
}
|
|
117
|
+
/** Width and height from an image's header: PNG IHDR, JPEG SOFn, WebP VP8/VP8L/VP8X, KTX2; undefined otherwise. */
|
|
118
|
+
export declare function imageHeader(b: Uint8Array): {
|
|
119
|
+
format: GLTFImageReport['format'];
|
|
120
|
+
width: number;
|
|
121
|
+
height: number;
|
|
122
|
+
} | undefined;
|
|
123
|
+
/**
|
|
124
|
+
* Reports a glTF 2.0 file (GLB bytes, or glTF JSON with embedded buffers) without a GPU: counts, world bounds of what
|
|
125
|
+
* the default scene draws, extensions, image sizes and problems. Never throws on a bad file: a file that is not glTF
|
|
126
|
+
* gives one `'not-gltf'` problem. External buffers and images cannot be read from bytes alone and are problems.
|
|
127
|
+
*/
|
|
128
|
+
export declare function reportGLTF(input: ArrayBuffer | ArrayBufferView | string, options?: GLTFReportOptions): Promise<GLTFReport>;
|
package/dist/report.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{t as e}from"./version-BmU1nesQ.js";import{t}from"./decoder-default-CxLH8Vz0.js";import{c as n,t as r}from"./asset-1b8ejkZX.js";var i=/* @__PURE__ */ new Set([`KHR_mesh_quantization`,`KHR_texture_transform`,`KHR_materials_emissive_strength`,`KHR_materials_unlit`,`KHR_lights_punctual`,`KHR_node_visibility`,`KHR_materials_clearcoat`,`KHR_materials_transmission`,`KHR_materials_ior`,`KHR_materials_dispersion`,`KHR_materials_volume`,`KHR_materials_sheen`,`KHR_materials_specular`,`KHR_materials_variants`,`KHR_materials_iridescence`,`KHR_materials_anisotropy`,`KHR_materials_diffuse_roughness`,`KHR_texture_basisu`,`KHR_draco_mesh_compression`,`EXT_meshopt_compression`,`KHR_meshopt_compression`,`EXT_texture_webp`,`EXT_texture_avif`,`EXT_mesh_gpu_instancing`]),a=()=>[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1];function o(e,t){let n=Array(16);for(let r=0;r<4;r++)for(let i=0;i<4;i++)n[r*4+i]=e[i]*t[r*4]+e[4+i]*t[r*4+1]+e[8+i]*t[r*4+2]+e[12+i]*t[r*4+3];return n}function s(e,t,n){let[r,i,a,o]=t;return[(1-2*(i*i+a*a))*n[0],2*(r*i+a*o)*n[0],2*(r*a-i*o)*n[0],0,2*(r*i-a*o)*n[1],(1-2*(r*r+a*a))*n[1],2*(i*a+r*o)*n[1],0,2*(r*a+i*o)*n[2],2*(i*a-r*o)*n[2],(1-2*(r*r+i*i))*n[2],0,e[0],e[1],e[2],1]}var c=e=>e.matrix?.slice()??s(e.translation??[0,0,0],e.rotation??[0,0,0,1],e.scale??[1,1,1]);function l(e){let t=new DataView(e.buffer,e.byteOffset,e.byteLength);if(e.length>=24&&t.getUint32(0)===2303741511)return{format:`png`,width:t.getUint32(16),height:t.getUint32(20)};if(e.length>=4&&e[0]===255&&e[1]===216)for(let n=2;n+9<e.length;){if(e[n]!==255)return;let r=e[n+1];if(r===255){n++;continue}if(r===216||r===1||r>=208&&r<=215){n+=2;continue}let i=t.getUint16(n+2);if(r>=192&&r<=207&&r!==196&&r!==200&&r!==204)return{format:`jpeg`,width:t.getUint16(n+7),height:t.getUint16(n+5)};n+=2+i}else{if(e.length>=30&&t.getUint32(0)===1380533830&&t.getUint32(8)===1464156752){let n=String.fromCharCode(e[12],e[13],e[14],e[15]);if(n===`VP8 `)return{format:`webp`,width:t.getUint16(26,!0)&16383,height:t.getUint16(28,!0)&16383};if(n===`VP8L`){let e=t.getUint32(21,!0);return{format:`webp`,width:(e&16383)+1,height:(e>>>14&16383)+1}}return n===`VP8X`?{format:`webp`,width:1+(e[24]|e[25]<<8|e[26]<<16),height:1+(e[27]|e[28]<<8|e[29]<<16)}:void 0}if(e.length>=28&&t.getUint32(0)===2873840728&&t.getUint32(4)===540160010)return{format:`ktx2`,width:t.getUint32(20,!0),height:Math.max(1,t.getUint32(24,!0))}}}function u(e,t){let n=e.doc,r=n.nodes??[],i=n.scenes?.[n.scene??0],l=new Set(r.flatMap(e=>e.children??[])),u=i?i.nodes??[]:r.map((e,t)=>t).filter(e=>!l.has(e)),d=[],f=/* @__PURE__ */ new Set,p=(n,i)=>{let a=r[n];if(!a)return void t.push({code:`json`,detail:`the scene names node ${n}, which does not exist`});if(f.has(n))return void t.push({code:`json`,detail:`node ${n} is its own ancestor (a cycle)`});f.add(n);let l=o(i,c(a)),u=[l],m=a.extensions?.EXT_mesh_gpu_instancing?.attributes;if(m&&a.mesh!==void 0)try{let t=m.TRANSLATION===void 0?void 0:e.accessor(m.TRANSLATION),n=m.ROTATION===void 0?void 0:e.accessor(m.ROTATION),r=m.SCALE===void 0?void 0:e.accessor(m.SCALE),i=t?t.length/3:n?n.length/4:r?r.length/3:0;u=[];for(let e=0;e<i;e++)u.push(o(l,s(t?[t[e*3],t[e*3+1],t[e*3+2]]:[0,0,0],n?[n[e*4],n[e*4+1],n[e*4+2],n[e*4+3]]:[0,0,0,1],r?[r[e*3],r[e*3+1],r[e*3+2]]:[1,1,1])))}catch(e){t.push({code:`accessor`,where:a.name,detail:`node ${a.name??n} instancing: ${e.message}`})}u.some(e=>e.some(e=>!Number.isFinite(e)))&&t.push({code:`not-finite`,where:a.name,detail:`node ${a.name??n} has a transform that is not finite`}),d.push({index:n,node:a,matrices:u});for(let e of a.children??[])p(e,l);f.delete(n)};for(let e of u)p(e,a());return d}function d(e,t,n){if(n.length=0,e===4)for(let e=0;e+2<t.length;e+=3)n.push(t[e],t[e+1],t[e+2]);else if(e===5)for(let e=0;e+2<t.length;e++)e%2?n.push(t[e+1],t[e],t[e+2]):n.push(t[e],t[e+1],t[e+2]);else for(let e=1;e+1<t.length;e++)n.push(t[0],t[e],t[e+1])}var f=(e,t,n,r,i)=>{n<e[0]&&(e[0]=n),r<e[1]&&(e[1]=r),i<e[2]&&(e[2]=i),n>t[0]&&(t[0]=n),r>t[1]&&(t[1]=r),i>t[2]&&(t[2]=i)},p=(e,t)=>e.every(Number.isFinite)&&t.every(Number.isFinite)?{min:[e[0],e[1],e[2]],max:[t[0],t[1],t[2]],size:[t[0]-e[0],t[1]-e[1],t[2]-e[2]],centre:[(t[0]+e[0])/2,(t[1]+e[1])/2,(t[2]+e[2])/2]}:null,m=e=>[e.pbrMetallicRoughness?.baseColorTexture,e.pbrMetallicRoughness?.metallicRoughnessTexture,e.normalTexture,e.occlusionTexture,e.emissiveTexture].filter(e=>!!e);async function h(a,o={}){let s=[],c=o.zeroArea??1e-9,h={triangles:0,vertices:0,primitives:0,meshes:0,nodes:0,materials:0,textures:0,images:0,texturePixels:0,largestTexture:0,rgbaBytes:0,zeroArea:0,pointsAndLines:0},v={ok:!1,counts:h,bounds:null,extensions:{used:[],required:[],unsupported:[]},images:[],zeroAreaByNode:[],problems:s},y=()=>(v.ok=s.length===0,v);o.signal?.throwIfAborted();let b,x;try{if({doc:b,binary:x}=n(a),!b||typeof b!=`object`||!e(b.asset))throw TypeError(`Expected glTF 2.0`)}catch(e){return s.push({code:`not-gltf`,detail:e.message}),y()}let S=Array.isArray(b.extensionsUsed)?[...b.extensionsUsed]:[],C=Array.isArray(b.extensionsRequired)?[...b.extensionsRequired]:[],w=C.filter(e=>!i.has(e)&&!o.supported?.includes(e));v.extensions={used:S,required:C,unsupported:w};for(let e of w)s.push({code:`extension`,detail:`${e} is required and Margo's loaders do not read it`});h.meshes=b.meshes?.length??0,h.nodes=b.nodes?.length??0,h.materials=b.materials?.length??0,h.textures=b.textures?.length??0,h.images=b.images?.length??0;let T=(b.buffers??[]).some(e=>e?.uri!==void 0&&!String(e.uri).startsWith(`data:`));T&&s.push({code:`external`,detail:`a buffer points at a file outside these bytes; pass a GLB or embed the buffers`});let E;try{let e={...b,extensionsRequired:C.filter(e=>!w.includes(e))};E=new r(e,T?[]:x?[x]:[],x,t(o).decoders)}catch(e){return s.push({code:`json`,detail:e.message}),y()}let D=!0;try{await E.decode(o.signal)}catch(e){o.signal?.throwIfAborted(),D=!1,s.push({code:`decode`,detail:`compressed geometry could not be decoded here: ${e.message}`})}let O=[];for(let[e,t]of(b.images??[]).entries()){let n;try{if(t.bufferView!==void 0)n=E.view(t.bufferView);else if(typeof t.uri==`string`&&t.uri.startsWith(`data:`)){let e=t.uri.indexOf(`,`);n=Uint8Array.from(atob(t.uri.slice(e+1)),e=>e.charCodeAt(0))}}catch(n){s.push({code:`json`,detail:`image ${t.name??e}: ${n.message}`})}if(O.push(n),!n){v.images.push({index:e,name:t.name,format:`external`,width:0,height:0}),t.uri!==void 0&&s.push({code:`external`,detail:`image ${t.name??e} points at ${String(t.uri).slice(0,80)}, outside these bytes`});continue}let r=l(n);r?(v.images.push({index:e,name:t.name,...r}),h.texturePixels+=r.width*r.height,h.largestTexture=Math.max(h.largestTexture,r.width,r.height)):(v.images.push({index:e,name:t.name,format:`unknown`,width:0,height:0}),s.push({code:`image-format`,detail:`image ${t.name??e} is not PNG, JPEG, WebP or KTX2`}))}h.rgbaBytes=h.texturePixels*4;let k=[1/0,1/0,1/0],A=[-1/0,-1/0,-1/0],j={primitives:0,samples:0,min:[1/0,1/0,1/0],max:[-1/0,-1/0,-1/0]},M=[],N=[],P=/* @__PURE__ */ new Map,F=e=>{let t=P.get(e);return t||P.set(e,t=E.accessor(e,void 0,!1)),t},I=/* @__PURE__ */ new Map;for(let{index:e,node:t,matrices:n}of u(E,s)){if(o.signal?.throwIfAborted(),t.mesh===void 0)continue;let r=b.meshes?.[t.mesh],i=t.name??`node with mesh ${t.mesh}`;if(!r)s.push({code:`json`,where:t.name,detail:`${i} names mesh ${t.mesh}, which does not exist`});else for(let[a,l]of(r.primitives??[]).entries()){let r=`${i} primitive ${a}`,u=l.mode??4,p=l.attributes??{},_=n.length;if(h.primitives+=_,u<4){h.pointsAndLines+=_;continue}let v=l.material===void 0?void 0:b.materials?.[l.material];l.material===void 0?s.push({code:`no-material`,where:i,detail:`${r} has no material (drawn with the default)`}):v||s.push({code:`json`,where:i,detail:`${r} names material ${l.material}, which does not exist`}),p.NORMAL===void 0&&!v?.extensions?.KHR_materials_unlit&&s.push({code:`no-normals`,where:i,detail:`${r} has no NORMAL (flat normals are made at load)`});for(let e of v?m(v):[])if(p[`TEXCOORD_${e.texCoord??0}`]===void 0){s.push({code:`no-uvs`,where:i,detail:`${r} is textured and has no TEXCOORD_${e.texCoord??0}`});break}if(p.POSITION===void 0){s.push({code:`accessor`,where:i,detail:`${r} has no POSITION`});continue}if(l.extensions?.KHR_draco_mesh_compression&&!D){let e=b.accessors?.[p.POSITION],t=(l.indices===void 0?void 0:b.accessors?.[l.indices])?.count??e?.count??0;h.vertices+=(e?.count??0)*_,h.triangles+=(u===4?Math.floor(t/3):Math.max(0,t-2))*_;let r=e?.min,i=e?.max;if(r&&i)for(let e of n)for(let t=0;t<8;t++){let n=t&1?i[0]:r[0],a=t&2?i[1]:r[1],o=t&4?i[2]:r[2];f(k,A,e[0]*n+e[4]*a+e[8]*o+e[12],e[1]*n+e[5]*a+e[9]*o+e[13],e[2]*n+e[6]*a+e[10]*o+e[14])}continue}let y,x;try{y=F(p.POSITION);for(let[e,t]of Object.entries(p))F(t).some(e=>!Number.isFinite(e))&&s.push({code:`not-finite`,where:i,detail:`${r} ${e} holds NaN or infinity`});let e=y.length/3;if(x=l.indices===void 0?Uint32Array.from({length:e},(e,t)=>t):F(l.indices),Array.prototype.some.call(x,t=>t>=e||t<0)){s.push({code:`index-range`,where:i,detail:`${r} has an index past its ${e} vertices`});continue}}catch(e){s.push({code:`accessor`,where:i,detail:`${r}: ${e.message}`});continue}d(u,x,N);let S=y.length/3,C=new Uint8Array(S);h.vertices+=S*_,h.triangles+=N.length/3*_;for(let e of N)C[e]=1;for(let r of n){let n=0;for(let e=0;e<N.length;e+=3){let t=N[e]*3,i=N[e+1]*3,a=N[e+2]*3,o=r[0]*y[t]+r[4]*y[t+1]+r[8]*y[t+2],s=r[1]*y[t]+r[5]*y[t+1]+r[9]*y[t+2],l=r[2]*y[t]+r[6]*y[t+1]+r[10]*y[t+2],u=r[0]*y[i]+r[4]*y[i+1]+r[8]*y[i+2]-o,d=r[1]*y[i]+r[5]*y[i+1]+r[9]*y[i+2]-s,f=r[2]*y[i]+r[6]*y[i+1]+r[10]*y[i+2]-l,p=r[0]*y[a]+r[4]*y[a+1]+r[8]*y[a+2]-o,m=r[1]*y[a]+r[5]*y[a+1]+r[9]*y[a+2]-s,h=r[2]*y[a]+r[6]*y[a+1]+r[10]*y[a+2]-l;Math.hypot(d*h-f*m,f*p-u*h,u*m-d*p)/2<c&&n++}if(h.zeroArea+=n,n){let r=I.get(e)??{node:e,mesh:t.mesh,where:i,zeroArea:0,triangles:0};r.zeroArea+=n,r.triangles+=N.length/3,I.set(e,r)}for(let e=0;e<S;e++)if(C[e]){let t=y[e*3],n=y[e*3+1],i=y[e*3+2];f(k,A,r[0]*t+r[4]*n+r[8]*i+r[12],r[1]*t+r[5]*n+r[9]*i+r[13],r[2]*t+r[6]*n+r[10]*i+r[14])}}let w=g(y,N);w!==void 0&&w<0&&s.push({code:`inside-out`,where:i,detail:`${r} is closed and its triangles face inward (signed volume ${w.toPrecision(3)})`});let T=v?.pbrMetallicRoughness?.baseColorTexture,E=v?.alphaMode??`OPAQUE`;if(o.alpha&&T&&(E===`MASK`||E===`BLEND`)&&p[`TEXCOORD_${T.texCoord??0}`]!==void 0)try{M.push({positions:y,uvs:F(p[`TEXCOORD_${T.texCoord??0}`]),tris:N.slice(),matrices:n,texture:T.index,cutoff:(E===`MASK`?v?.alphaCutoff??.5:o.blendCutoff??.5)/(v?.pbrMetallicRoughness?.baseColorFactor?.[3]??1)})}catch(e){s.push({code:`accessor`,where:i,detail:`${r}: ${e.message}`})}}}if(v.bounds=p(k,A),h.triangles===0&&h.pointsAndLines===0&&s.push({code:`empty`,detail:`the default scene draws nothing`}),v.zeroAreaByNode=[...I.values()],h.zeroArea&&s.push({code:`zero-area`,detail:`${h.zeroArea} of ${h.triangles} drawn triangles have zero area`}),o.alpha){let{decodeImage:e}=await import(`@margoengine/decoders/image`),t=/* @__PURE__ */ new Map,n=o.maxSamples??4194304,r=M.reduce((e,t)=>e+t.tris.length/3,0);for(let i of M){let a=b.textures?.[i.texture]?.source;if(a===void 0||!O[a])continue;t.has(a)||t.set(a,await e(O[a],{signal:o.signal}).catch(e=>(s.push({code:`image-format`,detail:`image ${a} could not be decoded for alpha: ${e.message}`}),null)));let c=t.get(a);c&&(j.primitives++,j.samples+=_(i,c,Math.max(1,Math.floor(n/Math.max(1,r))),j.min,j.max))}v.cutouts={primitives:j.primitives,bounds:p(j.min,j.max),samples:j.samples}}return y()}function g(e,t){let n=/* @__PURE__ */ new Map,r=new Int32Array(e.length/3);for(let t=0;t<r.length;t++){let i=`${Math.round(e[t*3]*1e5)},${Math.round(e[t*3+1]*1e5)},${Math.round(e[t*3+2]*1e5)}`,a=n.get(i);a===void 0&&n.set(i,a=n.size),r[t]=a}let i=/* @__PURE__ */ new Map,a=n.size,o=0;for(let n=0;n<t.length;n+=3){let s=t[n]*3,c=t[n+1]*3,l=t[n+2]*3,u=e;o+=(u[s]*(u[c+1]*u[l+2]-u[c+2]*u[l+1])-u[s+1]*(u[c]*u[l+2]-u[c+2]*u[l])+u[s+2]*(u[c]*u[l+1]-u[c+1]*u[l]))/6;for(let e=0;e<3;e++){let o=r[t[n+e]],s=r[t[n+(e+1)%3]];if(o===s)continue;let c=Math.min(o,s)*a+Math.max(o,s);i.set(c,(i.get(c)??0)+1)}}if(i.size){for(let e of i.values())if(e!==2)return;return o}}function _(e,t,n,r,i){let{positions:a,uvs:o,tris:s}=e,{width:c,height:l,data:u}=t,d=Math.min(1,Math.max(0,e.cutoff))*255,p=0;for(let t=0;t<s.length;t+=3){let[m,h,g]=[s[t],s[t+1],s[t+2]],_=o[m*2]*c,v=o[m*2+1]*l,y=o[h*2]*c,b=o[h*2+1]*l,x=o[g*2]*c,S=o[g*2+1]*l,C=Math.abs((y-_)*(S-v)-(x-_)*(b-v))/2,w=Math.max(1,Math.min(Math.ceil(Math.sqrt(2*C)),Math.floor(Math.sqrt(2*n))));for(let t=0;t<=w;t++)for(let n=0;n<=w-t;n++){let o=t/w,s=n/w,C=1-o-s,T=C*_+o*y+s*x,E=C*v+o*b+s*S,D=(Math.floor(T)%c+c)%c,O=(Math.floor(E)%l+l)%l;if(p++,u[(O*c+D)*4+3]<d)continue;let k=C*a[m*3]+o*a[h*3]+s*a[g*3],A=C*a[m*3+1]+o*a[h*3+1]+s*a[g*3+1],j=C*a[m*3+2]+o*a[h*3+2]+s*a[g*3+2];for(let t of e.matrices)f(r,i,t[0]*k+t[4]*A+t[8]*j+t[12],t[1]*k+t[5]*A+t[9]*j+t[13],t[2]*k+t[6]*A+t[10]*j+t[14])}}return p}export{l as imageHeader,h as reportGLTF};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Mesh as MeshHandle } from '@margoengine/core';
|
|
2
|
+
import type { GLTFExtension, Model } from './index.js';
|
|
3
|
+
/** Where a camera or light sits: world space at load, with the model at its origin. */
|
|
4
|
+
export interface ScenePlacement {
|
|
5
|
+
/** The node's name, '' when it has none. */
|
|
6
|
+
name: string;
|
|
7
|
+
/** The node it hangs on; follows the model if it moves. */
|
|
8
|
+
node: MeshHandle;
|
|
9
|
+
/** World position. */
|
|
10
|
+
position: [number, number, number];
|
|
11
|
+
/** Unit vector the camera looks along, or the light shines along (the node's -Z). */
|
|
12
|
+
direction: [number, number, number];
|
|
13
|
+
/** Column-major world matrix. */
|
|
14
|
+
matrix: Float64Array;
|
|
15
|
+
}
|
|
16
|
+
/** A glTF camera. Distances are in metres, angles in radians. */
|
|
17
|
+
export type SceneCamera = ScenePlacement & ({
|
|
18
|
+
type: 'perspective';
|
|
19
|
+
/** Vertical field of view. */
|
|
20
|
+
yfov: number;
|
|
21
|
+
znear: number;
|
|
22
|
+
/** Absent for an infinite projection. */
|
|
23
|
+
zfar?: number;
|
|
24
|
+
/** Absent when the file leaves it to the canvas. */
|
|
25
|
+
aspectRatio?: number;
|
|
26
|
+
} | {
|
|
27
|
+
type: 'orthographic';
|
|
28
|
+
xmag: number;
|
|
29
|
+
ymag: number;
|
|
30
|
+
znear: number;
|
|
31
|
+
zfar: number;
|
|
32
|
+
});
|
|
33
|
+
/** A KHR_lights_punctual light with the extension's defaults filled in. */
|
|
34
|
+
export interface SceneLight extends ScenePlacement {
|
|
35
|
+
type: 'directional' | 'point' | 'spot';
|
|
36
|
+
/** Linear RGB. Default [1, 1, 1]. */
|
|
37
|
+
color: [number, number, number];
|
|
38
|
+
/** Lux for directional, candela for point and spot. Default 1. */
|
|
39
|
+
intensity: number;
|
|
40
|
+
/** Metres; absent means no limit. Absent on directional lights. */
|
|
41
|
+
range?: number;
|
|
42
|
+
/** Spot only. Default 0. */
|
|
43
|
+
innerConeAngle?: number;
|
|
44
|
+
/** Spot only. Default pi / 4. */
|
|
45
|
+
outerConeAngle?: number;
|
|
46
|
+
}
|
|
47
|
+
/** What {@link sceneData} returns: every camera and light on a node of the loaded scene. */
|
|
48
|
+
export interface SceneData {
|
|
49
|
+
cameras: SceneCamera[];
|
|
50
|
+
lights: SceneLight[];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Lets the light loader accept nodes with a camera or a `KHR_lights_punctual` light, instead of refusing them. Read
|
|
54
|
+
* them back with {@link sceneData}. Nothing is created in the engine: the app decides what to do with them.
|
|
55
|
+
* @example
|
|
56
|
+
* const model = await loadGLTF(engine, '/shot.glb', { extensions: [sceneExtras] });
|
|
57
|
+
* const { cameras, lights } = sceneData(model);
|
|
58
|
+
*/
|
|
59
|
+
export declare const sceneExtras: GLTFExtension;
|
|
60
|
+
/** The cameras and lights of a model loaded with {@link sceneExtras}; both empty for any other model. */
|
|
61
|
+
export declare function sceneData(model: Model): SceneData;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{n as e,t}from"./accessor-nVsu_f1L.js";import{composeMatrix as n,multiplyMatrices as r}from"@margoengine/math/mat4";var i=`KHR_lights_punctual`,a=e=>/* @__PURE__ */ TypeError(`Invalid glTF ${e}; the file is not valid glTF 2.0, re-export it and check it with the glTF Validator`),o=(e,t,n=-1/0)=>{if(typeof e!=`number`||!Number.isFinite(e)||e<n)throw a(t);return e},s=(e,t,n)=>{if(o(e,t)<=n)throw a(t);return e},c=(e,t)=>{if(o(e,t)===0)throw a(t);return e};function l(e){if(e.type===`perspective`&&e.perspective){let t=e.perspective;return{type:`perspective`,yfov:s(t.yfov,`camera yfov`,0),znear:s(t.znear,`camera znear`,0),...t.zfar===void 0?{}:{zfar:s(t.zfar,`camera zfar`,t.znear)},...t.aspectRatio===void 0?{}:{aspectRatio:s(t.aspectRatio,`camera aspect`,0)}}}if(e.type===`orthographic`&&e.orthographic){let t=e.orthographic;return{type:`orthographic`,xmag:c(t.xmag,`camera xmag`),ymag:c(t.ymag,`camera ymag`),znear:o(t.znear,`camera znear`,0),zfar:s(t.zfar,`camera zfar`,t.znear)}}throw a(`camera`)}function u(e){if(e.type!==`directional`&&e.type!==`point`&&e.type!==`spot`)throw a(`light type`);let t=e.color??[1,1,1];if(!Array.isArray(t)||t.length!==3)throw a(`light color`);let n={type:e.type,color:t.map(e=>o(e,`light color`,0)),intensity:o(e.intensity??1,`light intensity`,0),...e.range===void 0||e.type===`directional`?{}:{range:s(e.range,`light range`,0)}};if(e.type!==`spot`)return n;let r=o(e.spot?.innerConeAngle??0,`spot angle`,0),i=s(e.spot?.outerConeAngle??Math.PI/4,`spot angle`,r);if(i>Math.PI/2)throw a(`spot angle`);return{...n,innerConeAngle:r,outerConeAngle:i}}function d(e,t,i,a){let o=a.get(t);if(o)return o;let s=e.nodes[t],c=s.matrix?Float64Array.from(s.matrix):n(s.translation??[0,0,0],s.rotation??[0,0,0,1],s.scale??[1,1,1],/* @__PURE__ */ new Float64Array(16)),l=i[t]<0?c:r(d(e,i[t],i,a),c);return a.set(t,l),l}function f(e,t,n){let r=Math.hypot(n[8],n[9],n[10])||1;return{name:typeof e.name==`string`?e.name:``,node:t,position:[n[12],n[13],n[14]],direction:[-n[8]/r,-n[9]/r,-n[10]/r],matrix:n}}var p=/* @__PURE__ */ new WeakMap,m=/* @__PURE__ */ new WeakMap,h={handles:[i],allowEmptyScene:!0,read(e){let t=e.doc.nodes??[];m.set(e,t),e.doc={...e.doc,nodes:t.map(e=>{if(e?.camera===void 0&&!e?.extensions?.[i])return e;let{camera:t,extensions:n,...r}=e,{[i]:a,...o}=n??{};return Object.keys(o).length?{...r,extensions:o}:r})}},finish(n,r,a){let o={cameras:[],lights:[]},s=m.get(r),c=r.doc,h=c.extensions?.[i]?.lights,g=new Int32Array(c.nodes?.length??0).fill(-1),_=/* @__PURE__ */ new Map;c.nodes?.forEach((e,t)=>e.children?.forEach(e=>g[e]=t)),s.forEach((n,r)=>{let s=a.nodes[r],p=n?.extensions?.[i];if(!s||n.camera===void 0&&!p)return;let m=f(n,s,d(c,r,g,_));n.camera!==void 0&&o.cameras.push({...m,...l(t(c.cameras,n.camera,`camera`))}),p&&o.lights.push({...m,...u(t(h,e(p.light,`light`),`light`))})}),p.set(n,o)}};function g(e){return p.get(e)??{cameras:[],lights:[]}}export{g as sceneData,h as sceneExtras};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
var e=0,t=(t,n)=>{let r=n+e++;for(;t.plugins?.has(r);)r+=`:`;return r};function n(e){let n;if(typeof e?.sceneOf==`function`){for(let t of e.plugins?.values()??[])if(n=e.sceneOf(t))break;if(!n){let r={coreRequirement:/*@__PURE__*/__margoCoreRequirement("0.1.0",import.meta.url),name:t(e,`scene-scope:`),setup(){n=e.sceneOf(r)}};e.use(r),e.removePlugin(r.name)}}return r(e,n)}function r(e,n){return{scene:n,run(r){if(!n)return r();let i,a={coreRequirement:/*@__PURE__*/__margoCoreRequirement("0.1.0",import.meta.url),name:t(e,`scene-scope:`),setup(){i=r()}};return n.use(a),n.removePlugin(a.name),i}}}export{r as n,n as t};
|
package/dist/scene.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { EngineKernel, Geometry, Mesh } from '@margoengine/core';
|
|
2
|
+
import { type GeometryBuffers } from '@margoengine/core/extension';
|
|
3
|
+
/** Options of scene queries: whether hidden meshes are excluded (default true). */
|
|
4
|
+
export interface SceneQueryOptions {
|
|
5
|
+
/** Exclude meshes hidden by themselves or any ancestor. Defaults to true; ignores frustum culling. */
|
|
6
|
+
visibleOnly?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/** Options of fitting the camera to a scene: query options plus aspect ratio, duration and padding. */
|
|
9
|
+
export interface SceneFitOptions extends SceneQueryOptions {
|
|
10
|
+
/** Defaults to the canvas's displayed aspect ratio. */
|
|
11
|
+
aspect?: number;
|
|
12
|
+
duration?: number;
|
|
13
|
+
padding?: number;
|
|
14
|
+
}
|
|
15
|
+
interface SceneBufferBase extends Pick<GeometryBuffers, 'vertex' | 'index'> {
|
|
16
|
+
readonly node: Mesh;
|
|
17
|
+
/** Borrowed world matrix, updated with the owning mesh. */
|
|
18
|
+
readonly matrix: Float32Array;
|
|
19
|
+
/** Number of indices, not instances. */
|
|
20
|
+
readonly count: number;
|
|
21
|
+
readonly indexFormat: 'uint32';
|
|
22
|
+
}
|
|
23
|
+
/** The GPU buffers of a core mesh node, for custom passes. */
|
|
24
|
+
export interface CoreSceneBuffers extends SceneBufferBase {
|
|
25
|
+
readonly kind: 'core';
|
|
26
|
+
readonly stride: 24;
|
|
27
|
+
readonly geometry: Geometry;
|
|
28
|
+
/** Shared batch storage from the last render; does not identify this node's instance offset. */
|
|
29
|
+
readonly instance: GeometryBuffers['instance'];
|
|
30
|
+
}
|
|
31
|
+
/** The GPU buffers of a PBR model node, with its alpha texture and cutoff, for custom passes. */
|
|
32
|
+
export interface PBRSceneBuffers extends SceneBufferBase {
|
|
33
|
+
readonly kind: 'pbr';
|
|
34
|
+
readonly stride: 80;
|
|
35
|
+
readonly primitive?: object;
|
|
36
|
+
/** Byte offset of the primitive's first vertex in `vertex` (a deformer's shared buffer). Default 0. */
|
|
37
|
+
readonly vertexOffset?: number;
|
|
38
|
+
readonly alphaTexture?: GPUTextureView;
|
|
39
|
+
readonly alphaSampler?: GPUSampler;
|
|
40
|
+
readonly alpha?: number;
|
|
41
|
+
readonly cutoff?: number;
|
|
42
|
+
readonly texCoord?: 0 | 1;
|
|
43
|
+
readonly alphaTransform?: Float32Array;
|
|
44
|
+
readonly doubleSided?: boolean;
|
|
45
|
+
}
|
|
46
|
+
/** The GPU buffers of a scene node: a core mesh's or a PBR model's. */
|
|
47
|
+
export type SceneBuffers = CoreSceneBuffers | PBRSceneBuffers;
|
|
48
|
+
/**
|
|
49
|
+
* World bounds of selected meshes and their descendants. PBR positions are the
|
|
50
|
+
* CPU bind pose under current node transforms; this never reads back GPU deformation.
|
|
51
|
+
*/
|
|
52
|
+
export declare function getSceneBounds(instance: EngineKernel, roots: Iterable<Mesh>, options?: SceneQueryOptions): Float32Array;
|
|
53
|
+
/** Fit the existing camera to mixed core/PBR roots; returns the bounds used. */
|
|
54
|
+
export declare function fitScene(instance: EngineKernel, roots: Iterable<Mesh>, options?: SceneFitOptions): Float32Array;
|
|
55
|
+
/**
|
|
56
|
+
* Borrow existing GPU records, one per core node or PBR primitive. Requery after
|
|
57
|
+
* animation or device restoration; this does not encode deformation or own any
|
|
58
|
+
* buffer. Hidden roots are filtered by default. WebGL2 returns null.
|
|
59
|
+
*/
|
|
60
|
+
export declare function gatherSceneBuffers(instance: EngineKernel, roots: Iterable<Mesh>, options?: SceneQueryOptions): SceneBuffers[] | null;
|
|
61
|
+
export {};
|
package/dist/scene.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const r=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import{n as e}from"./errors-THVaUwlL.js";import{n as t}from"./mesh-source-DvU8YvMc.js";import{t as n}from"./visibility-C_Oqz9Sr.js";import{engineRuntime as __margoRuntime,meshRuntime as i}from"@margoengine/core/extension";function a(t,a,{visibleOnly:o=!0}){if(t.disposed)throw e(`disposed`,`Engine disposed; create a new engine`);if(typeof o!=`boolean`)throw TypeError(`Invalid visibleOnly; pass true or false`);let s=r(t),c=/* @__PURE__ */ new Set;for(let e of Array.from(a)){e.check();let t=i(e);if(t.engine!==s)throw TypeError(`Foreign mesh; pass meshes created by the same engine`);c.add(t.id)}s.store.tick(!1);let l=[];for(let e of s.meshes.values()){let t=!1,r=0;for(let n=e.id;n!==-1;n=s.store.parents[n]){if(++r>64)throw RangeError(`Parent depth: 64; flatten the mesh hierarchy`);c.has(n)&&(t=!0)}t&&(!o||!n(s.store,e.id))&&l.push(e)}return{engine:s,meshes:l}}function o(n,r,i={}){let{meshes:o}=a(n,r,i),s=[1/0,1/0,1/0,-1/0,-1/0,-1/0],c=(e,t,n)=>{for(let r=0;r<e.length;r+=t){let t=e[r],i=e[r+1],a=e[r+2];for(let e=0;e<3;e++){let r=n[e]*t+n[e+4]*i+n[e+8]*a+n[e+12];if(!Number.isFinite(Math.fround(r)))throw RangeError(`Scene positions must be finite`);s[e]=Math.min(s[e],r),s[e+3]=Math.max(s[e+3],r)}}};for(let e of o){let n=t(e);if(n?.kind===`core`)c(n.geometry.vertices,6,e.matrix);else if(n?.kind===`pbr`)for(let t of n.source.primitives)c(t.vertices,20,e.matrix)}if(!Number.isFinite(s[0]))throw e(`state`,`No geometry; select meshes with triangles`);return Float32Array.from(s)}function s(e,t,n={}){let r=o(e,t,n),i=e.canvas,a=n.aspect??Math.max(1,i.clientWidth||i.width)/Math.max(1,i.clientHeight||i.height);return e.camera.fitToBoundingBox([r[0],r[1],r[2]],[r[3],r[4],r[5]],{aspect:a,duration:n.duration,padding:n.padding}),r}function c(n,r,i={}){let{engine:o,meshes:s}=a(n,r,i);if(n.backend!==`webgpu`)return null;let c=[];for(let n of s){let r=t(n);if(r?.kind===`core`){let t=o.geometryBuffers(r.geometry);if(!t)throw e(`state`,`Geometry unavailable; render one frame first`);c.push({kind:`core`,node:n,matrix:n.matrix,geometry:r.geometry,vertex:t.vertex,index:t.index,instance:t.instance,count:r.geometry.indices.length,stride:24,indexFormat:`uint32`})}else if(r?.kind===`pbr`){let t=r.source.draws;if(r.source.primitives.length&&!t.length)throw e(`state`,`PBR geometry unavailable; render one frame first`);for(let e of t)c.push({...e,kind:`pbr`,matrix:n.matrix,stride:80,indexFormat:`uint32`})}}return c}export{s as fitScene,c as gatherSceneBuffers,o as getSceneBounds};
|