@margoengine/splats 0.1.0 → 1.0.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/package.json CHANGED
@@ -1,107 +1,107 @@
1
- {
2
- "name": "@margoengine/splats",
3
- "version": "0.1.0",
4
- "description": "Optional Gaussian splats with GPU covariance projection and stable compute radix sorting.",
5
- "keywords": [
6
- "margoengine",
7
- "webgpu",
8
- "3d",
9
- "gaussian-splatting"
10
- ],
11
- "homepage": "https://margoengine.com",
12
- "bugs": {
13
- "url": "https://github.com/scottcampbelldata/margoengine/issues"
14
- },
15
- "repository": {
16
- "type": "git",
17
- "url": "git+https://github.com/scottcampbelldata/margoengine.git",
18
- "directory": "packages/splats"
19
- },
20
- "license": "Apache-2.0",
21
- "author": "Scott Campbell",
22
- "type": "module",
23
- "publishConfig": {
24
- "access": "public"
25
- },
26
- "scripts": {
27
- "test:gpu": "cd ../.. && node packages/splats/test/gpu/check.mjs",
28
- "test": "node --test test/*.test.js ../../tests/modern-temporal.test.js ../../tests/modern-data-subpaths.test.js ../../examples/package-benchmarks/proofs/package-guard.mjs",
29
- "prepack": "npm --prefix ../.. run build:packages",
30
- "test:gpu:formats": "cd ../.. && node packages/splats/test/gpu/formats-check.mjs && node packages/splats/test/gpu/formats-check.mjs --compat",
31
- "test:gpu:lod-sog": "cd ../.. && node packages/splats/test/gpu/lod-sog-check.mjs",
32
- "test:gpu:modify": "cd ../.. && node packages/splats/test/gpu/modify-check.mjs",
33
- "test:gpu:scale": "cd ../.. && node packages/splats/test/gpu/scale-check.mjs"
34
- },
35
- "sideEffects": false,
36
- "exports": {
37
- ".": {
38
- "types": "./dist/index.d.ts",
39
- "import": "./dist/index.js"
40
- },
41
- "./depth": {
42
- "types": "./dist/depth.d.ts",
43
- "import": "./dist/depth.js"
44
- },
45
- "./lod": {
46
- "types": "./dist/lod.d.ts",
47
- "import": "./dist/lod.js"
48
- },
49
- "./lighting": {
50
- "types": "./dist/lighting.d.ts",
51
- "import": "./dist/lighting.js"
52
- },
53
- "./set": {
54
- "types": "./dist/set.d.ts",
55
- "import": "./dist/set.js"
56
- },
57
- "./session": {
58
- "types": "./dist/session.d.ts",
59
- "import": "./dist/session.js"
60
- },
61
- "./edit": {
62
- "types": "./dist/brush.d.ts",
63
- "import": "./dist/brush.js"
64
- },
65
- "./pick": {
66
- "types": "./dist/pick.d.ts",
67
- "import": "./dist/pick.js"
68
- },
69
- "./animate": {
70
- "types": "./dist/animate.d.ts",
71
- "import": "./dist/animate.js"
72
- },
73
- "./data": {
74
- "types": "./dist/data.d.ts",
75
- "import": "./dist/data.js"
76
- },
77
- "./progressive": {
78
- "types": "./dist/progressive.d.ts",
79
- "import": "./dist/progressive.js"
80
- },
81
- "./formats": {
82
- "types": "./dist/formats.d.ts",
83
- "import": "./dist/formats.js"
84
- },
85
- "./lod-sog": {
86
- "types": "./dist/lod-sog.d.ts",
87
- "import": "./dist/lod-sog.js"
88
- },
89
- "./modify": {
90
- "types": "./dist/modify.d.ts",
91
- "import": "./dist/modify.js"
92
- }
93
- },
94
- "files": [
95
- "dist",
96
- "NOTICE"
97
- ],
98
- "dependencies": {
99
- "@margoengine/math": "^0.1.0"
100
- },
101
- "peerDependencies": {
102
- "@margoengine/core": "^0.1.0"
103
- },
104
- "devDependencies": {
105
- "@margoengine/decoders": "^0.1.0"
106
- }
107
- }
1
+ {
2
+ "name": "@margoengine/splats",
3
+ "version": "1.0.0",
4
+ "description": "Optional Gaussian splats with GPU covariance projection and stable compute radix sorting.",
5
+ "keywords": [
6
+ "margoengine",
7
+ "webgpu",
8
+ "3d",
9
+ "gaussian-splatting"
10
+ ],
11
+ "homepage": "https://margoengine.com",
12
+ "bugs": {
13
+ "url": "https://github.com/scottcampbelldata/margoengine/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/scottcampbelldata/margoengine.git",
18
+ "directory": "packages/splats"
19
+ },
20
+ "license": "Apache-2.0",
21
+ "author": "Scott Campbell",
22
+ "type": "module",
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "scripts": {
27
+ "test:gpu": "cd ../.. && node packages/splats/test/gpu/check.mjs",
28
+ "test": "node --test test/*.test.js ../../tests/modern-temporal.test.js ../../tests/modern-data-subpaths.test.js ../../examples/package-benchmarks/proofs/package-guard.mjs",
29
+ "prepack": "npm --prefix ../.. run build:packages",
30
+ "test:gpu:formats": "cd ../.. && node packages/splats/test/gpu/formats-check.mjs && node packages/splats/test/gpu/formats-check.mjs --compat",
31
+ "test:gpu:lod-sog": "cd ../.. && node packages/splats/test/gpu/lod-sog-check.mjs",
32
+ "test:gpu:modify": "cd ../.. && node packages/splats/test/gpu/modify-check.mjs",
33
+ "test:gpu:scale": "cd ../.. && node packages/splats/test/gpu/scale-check.mjs"
34
+ },
35
+ "sideEffects": false,
36
+ "exports": {
37
+ ".": {
38
+ "types": "./dist/index.d.ts",
39
+ "import": "./dist/index.js"
40
+ },
41
+ "./depth": {
42
+ "types": "./dist/depth.d.ts",
43
+ "import": "./dist/depth.js"
44
+ },
45
+ "./lod": {
46
+ "types": "./dist/lod.d.ts",
47
+ "import": "./dist/lod.js"
48
+ },
49
+ "./lighting": {
50
+ "types": "./dist/lighting.d.ts",
51
+ "import": "./dist/lighting.js"
52
+ },
53
+ "./set": {
54
+ "types": "./dist/set.d.ts",
55
+ "import": "./dist/set.js"
56
+ },
57
+ "./session": {
58
+ "types": "./dist/session.d.ts",
59
+ "import": "./dist/session.js"
60
+ },
61
+ "./edit": {
62
+ "types": "./dist/brush.d.ts",
63
+ "import": "./dist/brush.js"
64
+ },
65
+ "./pick": {
66
+ "types": "./dist/pick.d.ts",
67
+ "import": "./dist/pick.js"
68
+ },
69
+ "./animate": {
70
+ "types": "./dist/animate.d.ts",
71
+ "import": "./dist/animate.js"
72
+ },
73
+ "./data": {
74
+ "types": "./dist/data.d.ts",
75
+ "import": "./dist/data.js"
76
+ },
77
+ "./progressive": {
78
+ "types": "./dist/progressive.d.ts",
79
+ "import": "./dist/progressive.js"
80
+ },
81
+ "./formats": {
82
+ "types": "./dist/formats.d.ts",
83
+ "import": "./dist/formats.js"
84
+ },
85
+ "./lod-sog": {
86
+ "types": "./dist/lod-sog.d.ts",
87
+ "import": "./dist/lod-sog.js"
88
+ },
89
+ "./modify": {
90
+ "types": "./dist/modify.d.ts",
91
+ "import": "./dist/modify.js"
92
+ }
93
+ },
94
+ "files": [
95
+ "dist",
96
+ "NOTICE"
97
+ ],
98
+ "dependencies": {
99
+ "@margoengine/math": "^1.0.0"
100
+ },
101
+ "peerDependencies": {
102
+ "@margoengine/core": "^1.0.0"
103
+ },
104
+ "devDependencies": {
105
+ "@margoengine/decoders": "^1.0.0"
106
+ }
107
+ }
@@ -1,2 +0,0 @@
1
- import{SRGBToLinear as e}from"@margoengine/math/color";import{MargoError as t}from"@margoengine/math/errors";var n=Object.defineProperty,r=(e,t)=>{let r={};for(var i in e)n(r,i,{get:e[i],enumerable:!0});return t||n(r,Symbol.toStringTag,{value:`Module`}),r},i=/* @__PURE__ */ r({attributesFromSplats:()=>g,decomposeCovariance:()=>f,encodeSplat:()=>h,parsePly:()=>c,splatsFromAttributes:()=>l}),a=.2820947918,o={char:[1,(e,t)=>e.getInt8(t)],int8:[1,(e,t)=>e.getInt8(t)],uchar:[1,(e,t)=>e.getUint8(t)],uint8:[1,(e,t)=>e.getUint8(t)],short:[2,(e,t)=>e.getInt16(t,!0)],int16:[2,(e,t)=>e.getInt16(t,!0)],ushort:[2,(e,t)=>e.getUint16(t,!0)],uint16:[2,(e,t)=>e.getUint16(t,!0)],int:[4,(e,t)=>e.getInt32(t,!0)],int32:[4,(e,t)=>e.getInt32(t,!0)],uint:[4,(e,t)=>e.getUint32(t,!0)],uint32:[4,(e,t)=>e.getUint32(t,!0)],float:[4,(e,t)=>e.getFloat32(t,!0)],float32:[4,(e,t)=>e.getFloat32(t,!0)],double:[8,(e,t)=>e.getFloat64(t,!0)],float64:[8,(e,t)=>e.getFloat64(t,!0)]},s=e=>{let t=Math.min(1,Math.max(0,e));return t<=.04045?1/12.92:2.4/1.055*((t+.055)/1.055)**1.4};function c(e){if(!(e instanceof ArrayBuffer)&&!ArrayBuffer.isView(e))throw TypeError(`Expected .ply bytes; pass an ArrayBuffer or typed array of the file`);let n=e instanceof ArrayBuffer?new Uint8Array(e):new Uint8Array(e.buffer,e.byteOffset,e.byteLength),r=Math.min(n.length,65536),i=-1,a=new TextDecoder(`latin1`).decode(n.subarray(0,r)),s=a.indexOf(`end_header`);if(a.startsWith(`ply`)&&s>=0&&(i=a.indexOf(`
2
- `,s)+1),i<=0)throw RangeError(`Not a PLY file: it must start with "ply" and have an end_header line.`);let c=a.slice(0,i).split(/\r?\n/),u=``,d=-1,f=!1,p=[];for(let e of c){let n=e.trim().split(/\s+/);if(n[0]===`format`)u=n[1];else if(n[0]===`element`){if(f=n[1]===`vertex`,f)d=Number(n[2]);else if(n[1]===`chunk`)throw new t(`unsupported`,`Packed (chunked) splat PLY files are not supported; export an uncompressed Gaussian-splat PLY.`)}else if(n[0]===`property`&&f){if(n[1]===`list`)throw RangeError(`Splat PLY vertex properties must be scalars, not lists.`);if(!o[n[1]])throw RangeError(`Unknown PLY property type ${n[1]}; use float, double or an integer type.`);p.push({type:n[1],name:n[2]})}}if(u===`binary_big_endian`)throw new t(`unsupported`,`Big-endian PLY files are not supported; export binary_little_endian or ascii.`);if(u!==`binary_little_endian`&&u!==`ascii`)throw RangeError(`Unknown PLY format ${u}; use binary_little_endian or ascii.`);if(!Number.isInteger(d)||d<1||d>3e6)throw RangeError(`A splat PLY needs 1 to 3,000,000 vertices.`);let m=new Map(p.map((e,t)=>[e.name,t])),h=[`x`,`y`,`z`,`f_dc_0`,`f_dc_1`,`f_dc_2`,`opacity`,`scale_0`,`scale_1`,`scale_2`,`rot_0`,`rot_1`,`rot_2`,`rot_3`].filter(e=>!m.has(e));if(h.length)throw RangeError(`Not a Gaussian-splat PLY: missing ${h.join(`, `)}; export the splat attributes.`);let g=0;for(;m.has(`f_rest_${g}`);)g++;let _=g/3+1;if(g%3||![1,4,9,16,25].includes(_))throw RangeError(`Splat PLY f_rest_* must hold 3, 8, 15 or 24 coefficients per colour channel (SH degree 1-4).`);let v=p.length,y=new Float64Array(d*v);if(u===`ascii`){let e=new TextDecoder(`latin1`).decode(n.subarray(i)).trim().split(/\s+/);if(e.length<d*v)throw RangeError(`The ascii PLY has fewer values than its header promises; check that the file is complete.`);for(let t=0;t<d*v;t++)y[t]=Number(e[t])}else{let e=[],t=0;for(let n of p)e.push(t),t+=o[n.type][0];if(n.length-i<d*t)throw RangeError(`The PLY file is shorter than its header promises; check that it downloaded completely.`);let r=new DataView(n.buffer,n.byteOffset+i,d*t),a=p.map(e=>o[e.type][1]);for(let n=0;n<d;n++)for(let i=0;i<v;i++)y[n*v+i]=a[i](r,n*t+e[i])}let b=e=>m.get(e),x=b(`x`),S=b(`f_dc_0`),C=b(`opacity`),w=b(`scale_0`),T=b(`rot_0`),E=g?b(`f_rest_0`):0,D=_-1,O={count:d,positions:new Float32Array(d*3),logScales:new Float32Array(d*3),rotations:new Float32Array(d*4),dc:new Float32Array(d*3),alphas:new Float32Array(d),rest:D?new Float32Array(d*D*3):void 0};for(let e=0;e<d;e++){let t=t=>y[e*v+t];if(O.positions.set([t(x),t(b(`y`)),t(b(`z`))],e*3),O.rotations.set([t(T),t(T+1),t(T+2),t(T+3)],e*4),O.logScales.set([t(w),t(w+1),t(w+2)],e*3),O.dc.set([t(S),t(S+1),t(S+2)],e*3),O.alphas[e]=1/(1+Math.exp(-t(C))),O.rest)for(let n=0;n<3;n++)for(let r=0;r<D;r++)O.rest[(e*D+r)*3+n]=t(E+n*D+r)}return l(O)}function l(t,{linear:n=!1}={}){let{count:r}=t,i=t.rest?t.rest.length/r/3:0,o=i+1;if(!Number.isInteger(i)||![0,3,8,15,24].includes(i))throw RangeError(`Splat spherical harmonics must hold 3, 8, 15 or 24 coefficients per colour channel (degree 1-4).`);let c=new Float32Array(r*6),l=new Float32Array(r*4),u=i?new Float32Array(r*o*3):void 0;for(let f=0;f<r;f++){let r=t.rotations[f*4],p=t.rotations[f*4+1],m=t.rotations[f*4+2],h=t.rotations[f*4+3],g=Math.hypot(r,p,m,h);if(!(g>0))throw RangeError(`A splat rotation is the zero quaternion; check the file.`);r/=g,p/=g,m/=g,h/=g,d(c,f,[p,m,h,r],[Math.exp(t.logScales[f*3]),Math.exp(t.logScales[f*3+1]),Math.exp(t.logScales[f*3+2])]),l[f*4+3]=t.alphas[f];for(let r=0;r<3;r++){let c=.5+a*t.dc[f*3+r],d=Math.min(1,Math.max(0,c)),p=n?d:e(d);if(l[f*4+r]=p,!u)continue;let m=n?1:s(c);u[f*o*3+r]=(p-.5)/a;for(let e=1;e<o;e++)u[(f*o+e)*3+r]=t.rest[(f*i+e-1)*3+r]*m}}let f=u?{positions:t.positions,covariances:c,colors:l,sphericalHarmonics:u}:{positions:t.positions,covariances:c,colors:l};return j(f),f}var u=2**-20;function d(e,t,n,r){let[i,a,o,s]=n,c=[1-2*(a*a+o*o),2*(i*a-o*s),2*(i*o+a*s),2*(i*a+o*s),1-2*(i*i+o*o),2*(a*o-i*s),2*(i*o-a*s),2*(a*o+i*s),1-2*(i*i+a*a)],l=[r[0]*r[0],r[1]*r[1],r[2]*r[2]],d=Math.max(Math.max(...l)*u,1e-30),f=l.map(e=>Math.max(e,d)),p=0;for(let n=0;n<3;n++)for(let r=n;r<3;r++)e[t*6+p++]=c[n*3]*c[r*3]*f[0]+c[n*3+1]*c[r*3+1]*f[1]+c[n*3+2]*c[r*3+2]*f[2]}function f(e,t=0){let n=[[e[t],e[t+1],e[t+2]],[e[t+1],e[t+3],e[t+4]],[e[t+2],e[t+4],e[t+5]]],r=[[1,0,0],[0,1,0],[0,0,1]];for(let e=0;e<32&&!(Math.abs(n[0][1])+Math.abs(n[0][2])+Math.abs(n[1][2])<1e-30*(Math.abs(n[0][0])+Math.abs(n[1][1])+Math.abs(n[2][2])));e++)for(let[e,t]of[[0,1],[0,2],[1,2]]){if(n[e][t]===0)continue;let i=(n[t][t]-n[e][e])/(2*n[e][t]),a=Math.sign(i||1)/(Math.abs(i)+Math.sqrt(i*i+1)),o=1/Math.sqrt(a*a+1),s=a*o;for(let r=0;r<3;r++){let i=n[r][e],a=n[r][t];n[r][e]=o*i-s*a,n[r][t]=s*i+o*a}for(let r=0;r<3;r++){let i=n[e][r],a=n[t][r];n[e][r]=o*i-s*a,n[t][r]=s*i+o*a}for(let n=0;n<3;n++){let i=r[n][e],a=r[n][t];r[n][e]=o*i-s*a,r[n][t]=s*i+o*a}}if(r[0][0]*(r[1][1]*r[2][2]-r[1][2]*r[2][1])-r[0][1]*(r[1][0]*r[2][2]-r[1][2]*r[2][0])+r[0][2]*(r[1][0]*r[2][1]-r[1][1]*r[2][0])<0)for(let e=0;e<3;e++)r[e][2]=-r[e][2];return{scale:[0,1,2].map(e=>Math.sqrt(Math.max(n[e][e],0))),rotation:p(r)}}function p(e){let t=e[0][0]+e[1][1]+e[2][2],n,r,i,a;if(t>0){let o=Math.sqrt(t+1)*2;a=o/4,n=(e[2][1]-e[1][2])/o,r=(e[0][2]-e[2][0])/o,i=(e[1][0]-e[0][1])/o}else if(e[0][0]>e[1][1]&&e[0][0]>e[2][2]){let t=Math.sqrt(1+e[0][0]-e[1][1]-e[2][2])*2;a=(e[2][1]-e[1][2])/t,n=t/4,r=(e[0][1]+e[1][0])/t,i=(e[0][2]+e[2][0])/t}else if(e[1][1]>e[2][2]){let t=Math.sqrt(1+e[1][1]-e[0][0]-e[2][2])*2;a=(e[0][2]-e[2][0])/t,n=(e[0][1]+e[1][0])/t,r=t/4,i=(e[1][2]+e[2][1])/t}else{let t=Math.sqrt(1+e[2][2]-e[0][0]-e[1][1])*2;a=(e[1][0]-e[0][1])/t,n=(e[0][2]+e[2][0])/t,r=(e[1][2]+e[2][1])/t,i=t/4}let o=Math.hypot(n,r,i,a);return[n/o,r/o,i/o,a/o]}var m=e=>{let t=Math.min(1,Math.max(0,e));return t<=.0031308?t*12.92:1.055*t**(1/2.4)-.055};function h(e){let t=j(e),n=new Uint8Array(t*32),r=new DataView(n.buffer);if(e.colors.some((e,t)=>t%4==3&&e>1))throw RangeError(`The .splat format stores opacity through 1; use SOG for this cloud`);for(let i=0;i<t;i++){let t=i*32;for(let n=0;n<3;n++)r.setFloat32(t+n*4,e.positions[i*3+n],!0);let{scale:a,rotation:o}=f(e.covariances,i*6);for(let e=0;e<3;e++)r.setFloat32(t+12+e*4,Math.max(a[e],1e-12),!0);for(let r=0;r<3;r++)n[t+24+r]=Math.round(m(e.colors[i*4+r])*255);n[t+27]=Math.round(e.colors[i*4+3]*255);let[s,c,l,u]=o;[u,s,c,l].forEach((e,r)=>n[t+28+r]=Math.min(255,Math.max(0,Math.round(e*128+128))))}return n}function g(e){let t=j(e),n=e.sphericalHarmonics,r=n?n.length/t/3:1,i={count:t,positions:e.positions.slice(),logScales:new Float32Array(t*3),rotations:new Float32Array(t*4),dc:new Float32Array(t*3),alphas:new Float32Array(t),...n?{rest:new Float32Array(t*(r-1)*3)}:{}};for(let o=0;o<t;o++){let{scale:t,rotation:c}=f(e.covariances,o*6),[l,u,d,p]=c;for(let e=0;e<3;e++)i.logScales[o*3+e]=Math.log(Math.max(t[e],1e-30));i.rotations.set([p,l,u,d],o*4),i.alphas[o]=e.colors[o*4+3];for(let t=0;t<3;t++){let c=m(n?.5+a*n[o*r*3+t]:e.colors[o*4+t]),l=s(c);i.dc[o*3+t]=(c-.5)/a;for(let e=1;e<r;e++)i.rest[(o*(r-1)+e-1)*3+t]=n[(o*r+e)*3+t]/l}}return i}async function _(e,t){return(await import(`./spz-BTy6CPTp.js`).then(e=>e.n)).parseSpz(e,t)}async function v(e,t){return(await import(`./sog-D0LYo48N.js`).then(e=>e.n)).parseSog(e,t)}async function y(e,t){return(await import(`./encode-BA6xu973.js`)).encodeSpz(e,t)}async function b(e){return(await import(`./encode-BA6xu973.js`)).encodeSog(e)}var x=(e,t)=>e.sphericalHarmonics?e.sphericalHarmonics.length/t/3:0;function S(e,t){let n=j(e);if(typeof t!=`function`)throw TypeError(`filterSplats needs a keep(index) function`);let r=[];for(let i=0;i<n;i++)t(i,e)&&r.push(i);return C(e,r,x(e,n))}function C(e,t,n){if(!t.length)throw RangeError(`No splats are left; widen the region or the filter.`);let r=t.length,i=new Float32Array(r*3),a=new Float32Array(r*6),o=new Float32Array(r*4),s=n?new Float32Array(r*n*3):void 0;return t.forEach((t,r)=>{i.set(e.positions.subarray(t*3,t*3+3),r*3),a.set(e.covariances.subarray(t*6,t*6+6),r*6),o.set(e.colors.subarray(t*4,t*4+4),r*4),s&&s.set(e.sphericalHarmonics.subarray(t*n*3,(t+1)*n*3),r*n*3)}),s?{positions:i,covariances:a,colors:o,sphericalHarmonics:s}:{positions:i,covariances:a,colors:o}}function w(e,t,n={}){let r=n.invert??!1,i=e=>Array.isArray(e)&&e.length===3&&e.every(e=>typeof e==`number`&&Number.isFinite(e)),a;if(t&&`box`in t&&i(t.box?.min)&&i(t.box?.max)){let{min:e,max:n}=t.box;a=(t,r,i)=>t>=e[0]&&t<=n[0]&&r>=e[1]&&r<=n[1]&&i>=e[2]&&i<=n[2]}else if(t&&`sphere`in t&&i(t.sphere?.center)&&t.sphere.radius>=0){let{center:e,radius:n}=t.sphere;a=(t,r,i)=>(t-e[0])**2+(r-e[1])**2+(i-e[2])**2<=n*n}else throw RangeError(`Invalid crop region; pass {box: {min: [x, y, z], max: [x, y, z]}} or {sphere: {center: [x, y, z], radius}}`);let o=e?.positions;return S(e,e=>a(o[e*3],o[e*3+1],o[e*3+2])!==r)}function T(e,t,n,r){return r[0]=.2820947918,r[1]=-.4886025119*t,r[2]=.4886025119*n,r[3]=-.4886025119*e,r[4]=1.0925484306*e*t,r[5]=-1.0925484306*t*n,r[6]=.3153915653*(3*n*n-1),r[7]=-1.0925484306*e*n,r[8]=.5462742153*(e*e-t*t),r[9]=-.5900435899*t*(3*e*e-t*t),r[10]=2.8906114426*e*t*n,r[11]=-.4570457995*t*(5*n*n-1),r[12]=.3731763326*n*(5*n*n-3),r[13]=-.4570457995*e*(5*n*n-1),r[14]=1.4453057213*n*(e*e-t*t),r[15]=-.5900435899*e*(e*e-3*t*t),r[16]=2.5033429418*e*t*(e*e-t*t),r[17]=-1.7701307698*t*n*(3*e*e-t*t),r[18]=.9461746958*e*t*(7*n*n-1),r[19]=-.6690465436*t*n*(7*n*n-3),r[20]=.1057855469*(35*n**4-30*n*n+3),r[21]=-.6690465436*e*n*(7*n*n-3),r[22]=.4730873479*(e*e-t*t)*(7*n*n-1),r[23]=-1.7701307698*e*n*(e*e-3*t*t),r[24]=.6258357354*(e**4-6*e*e*t*t+t**4),r}function E(e,t){let n=new Float64Array(t),r=[],i=[];for(let a=0;a<128;a++){let o=1-2*(a+.5)/128,s=Math.sqrt(1-o*o),c=a*Math.PI*(3-Math.sqrt(5)),l=[Math.cos(c)*s,o,Math.sin(c)*s];r.push(T(l[0],l[1],l[2],new Float64Array(t)).slice(0,t));let u=[e[0]*l[0]+e[3]*l[1]+e[6]*l[2],e[1]*l[0]+e[4]*l[1]+e[7]*l[2],e[2]*l[0]+e[5]*l[1]+e[8]*l[2]];i.push(T(u[0],u[1],u[2],n).slice(0,t))}let a=new Float64Array(t*t),o=new Float64Array(t*t);for(let e=0;e<128;e++)for(let n=0;n<t;n++)for(let s=0;s<t;s++)a[n*t+s]+=r[e][n]*r[e][s],o[n*t+s]+=r[e][n]*i[e][s];for(let e=0;e<t;e++){let n=e;for(let r=e+1;r<t;r++)Math.abs(a[r*t+e])>Math.abs(a[n*t+e])&&(n=r);for(let r=0;r<t;r++)[a[e*t+r],a[n*t+r]]=[a[n*t+r],a[e*t+r]],[o[e*t+r],o[n*t+r]]=[o[n*t+r],o[e*t+r]];for(let n=0;n<t;n++){if(n===e)continue;let r=a[n*t+e]/a[e*t+e];if(r)for(let i=0;i<t;i++)a[n*t+i]-=r*a[e*t+i],o[n*t+i]-=r*o[e*t+i]}}for(let e=0;e<t;e++)for(let n=0;n<t;n++)o[e*t+n]/=a[e*t+e];return o}function D(e,t){let n=j(e);if(!t||t.length!==16||!Array.from(t).every(Number.isFinite))throw RangeError(`Invalid splat transform; pass a 4x4 column-major matrix of 16 finite numbers`);let r=t,i=[r[0],r[4],r[8],r[1],r[5],r[9],r[2],r[6],r[10]],a=i[0]*(i[4]*i[8]-i[5]*i[7])-i[1]*(i[3]*i[8]-i[5]*i[6])+i[2]*(i[3]*i[7]-i[4]*i[6]);if(!(Math.abs(a)>1e-12))throw RangeError(`A splat transform needs an invertible linear part (no zero scale); fix the matrix.`);let o=new Float32Array(n*3),s=new Float32Array(n*6),c=e.colors.slice();for(let t=0;t<n;t++){let n=e.positions[t*3],a=e.positions[t*3+1],c=e.positions[t*3+2];for(let e=0;e<3;e++)o[t*3+e]=i[e*3]*n+i[e*3+1]*a+i[e*3+2]*c+r[12+e];let l=e.covariances.subarray(t*6,t*6+6),u=[l[0],l[1],l[2],l[1],l[3],l[4],l[2],l[4],l[5]],d=/* @__PURE__ */ new Float64Array(9);for(let e=0;e<3;e++)for(let t=0;t<3;t++)d[e*3+t]=i[e*3]*u[t]+i[e*3+1]*u[3+t]+i[e*3+2]*u[6+t];let f=0;for(let e=0;e<3;e++)for(let n=e;n<3;n++)s[t*6+f++]=d[e*3]*i[n*3]+d[e*3+1]*i[n*3+1]+d[e*3+2]*i[n*3+2]}let l={positions:o,covariances:s,colors:c},u=e.sphericalHarmonics;if(u){let t=x(e,n),r=E(O(i),t),a=new Float32Array(u.length);for(let e=0;e<n;e++)for(let n=0;n<3;n++)for(let i=0;i<t;i++){let o=0;for(let a=0;a<t;a++)o+=r[i*t+a]*u[(e*t+a)*3+n];a[(e*t+i)*3+n]=o}l.sphericalHarmonics=a}return j(l),l}function O(e){let t=e.slice();for(let e=0;e<64;e++){let e=t[0]*(t[4]*t[8]-t[5]*t[7])-t[1]*(t[3]*t[8]-t[5]*t[6])+t[2]*(t[3]*t[7]-t[4]*t[6]),n=[t[4]*t[8]-t[5]*t[7],t[5]*t[6]-t[3]*t[8],t[3]*t[7]-t[4]*t[6],t[2]*t[7]-t[1]*t[8],t[0]*t[8]-t[2]*t[6],t[1]*t[6]-t[0]*t[7],t[1]*t[5]-t[2]*t[4],t[2]*t[3]-t[0]*t[5],t[0]*t[4]-t[1]*t[3]],r=t.map((t,r)=>(t+n[r]/e)/2),i=r.reduce((e,n,r)=>e+Math.abs(n-t[r]),0);if(t=r,i<1e-14)break}return t}function k(...e){if(!e.length)throw RangeError(`mergeSplats needs at least one cloud`);let t=e.map(e=>j(e)),n=new Set(e.map((e,n)=>x(e,t[n])));if(n.size>1)throw RangeError(`Merged clouds need the same spherical-harmonics degree; drop sphericalHarmonics from all or give all the same degree.`);let r=t.reduce((e,t)=>e+t,0),i=[...n][0],a=new Float32Array(r*3),o=new Float32Array(r*6),s=new Float32Array(r*4),c=i?new Float32Array(r*i*3):void 0,l=0;e.forEach((e,n)=>{a.set(e.positions,l*3),o.set(e.covariances,l*6),s.set(e.colors,l*4),c&&c.set(e.sphericalHarmonics,l*i*3),l+=t[n]});let u=c?{positions:a,covariances:o,colors:s,sphericalHarmonics:c}:{positions:a,covariances:o,colors:s};return j(u),u}function A(e,t){let n=j(e),r=t?.cellSize;if(!(r>0&&r<1/0))throw RangeError(`decimateSplats needs a cellSize above 0, world units`);let i=x(e,n),a=/* @__PURE__ */ new Map,o=e.positions;for(let e=0;e<n;e++){let t=`${Math.floor(o[e*3]/r)},${Math.floor(o[e*3+1]/r)},${Math.floor(o[e*3+2]/r)}`,n=a.get(t);n||a.set(t,n=[]),n.push(e)}let s=a.size,c=new Float32Array(s*3),l=new Float32Array(s*6),u=new Float32Array(s*4),d=i?new Float32Array(s*i*3):void 0,f=0;for(let t of a.values()){let n=t.map(t=>{let n=e.covariances.subarray(t*6,t*6+6),r=n[0]*(n[3]*n[5]-n[4]*n[4])-n[1]*(n[1]*n[5]-n[4]*n[2])+n[2]*(n[1]*n[4]-n[3]*n[2]);return Math.max(e.colors[t*4+3],1e-6)*Math.sqrt(Math.max(r,0))}),r=n.reduce((e,t)=>e+t,0)||1,a=[0,0,0];t.forEach((e,t)=>{for(let i=0;i<3;i++)a[i]+=n[t]/r*o[e*3+i]});let s=/* @__PURE__ */ new Float64Array(6);t.forEach((t,c)=>{let l=n[c]/r,p=[o[t*3]-a[0],o[t*3+1]-a[1],o[t*3+2]-a[2]],m=e.covariances.subarray(t*6,t*6+6),h=0;for(let e=0;e<3;e++)for(let t=e;t<3;t++,h++)s[h]+=l*(m[h]+p[e]*p[t]);for(let n=0;n<4;n++)u[f*4+n]+=l*e.colors[t*4+n];if(d)for(let n=0;n<i*3;n++)d[f*i*3+n]+=l*e.sphericalHarmonics[t*i*3+n]}),c.set(a,f*3),l.set(s,f*6),f++}let p=d?{positions:c,covariances:l,colors:u,sphericalHarmonics:d}:{positions:c,covariances:l,colors:u};for(let e=0;e<s*4;e++)p.colors[e]=Math.min(e%4==3?1.5625:1,Math.max(0,p.colors[e]));return j(p),p}function j(e){if(!e||!(e.positions instanceof Float32Array)||!(e.covariances instanceof Float32Array)||!(e.colors instanceof Float32Array))throw TypeError(`Splat data must use Float32 arrays`);let t=e.positions.length/3,n=e.sphericalHarmonics;if(!Number.isInteger(t)||t<1||t>3e6||e.covariances.length!==t*6||e.colors.length!==t*4||n!==void 0&&(!(n instanceof Float32Array)||![4,9,16,25].includes(n.length/t/3)))throw RangeError(`Splat data needs 1 to 3,000,000 splats, covariances of 6 and colors of 4 per splat, and spherical harmonics of 4, 9, 16 or 25 coefficients per channel; fix the array lengths.`);for(let t of[e.positions,e.covariances,e.colors,...n?[n]:[]])for(let e of t)if(!Number.isFinite(e))throw RangeError(`Splat attributes must be finite`);for(let n=0;n<t;n++){let t=n*6,r=e.covariances[t],i=e.covariances[t+1],a=e.covariances[t+2],o=e.covariances[t+3],s=e.covariances[t+4],c=e.covariances[t+5];if(r<=0||r*o-i*i<=0||r*(o*c-s*s)-i*(i*c-a*s)+a*(i*s-a*o)<=0)throw RangeError(`Splat covariance must be positive definite`);for(let t=0;t<4;t++)if(e.colors[n*4+t]<0||e.colors[n*4+t]>(t===3?1.5625:1))throw RangeError(`Splat colors must be 0..1 and opacity 0..1.5625; fix the attribute values`)}return t}function M(t){if(!(t instanceof ArrayBuffer)&&!ArrayBuffer.isView(t))throw TypeError(`Expected .splat bytes`);let n=t instanceof ArrayBuffer?new Uint8Array(t):new Uint8Array(t.buffer,t.byteOffset,t.byteLength),r=n.length/32;if(!Number.isInteger(r)||r<1||r>3e6)throw RangeError(`A .splat file must hold 1 to 3,000,000 splats of 32 bytes each; check that the file is complete.`);let i=new DataView(n.buffer,n.byteOffset,n.byteLength),a=new Float32Array(r*3),o=new Float32Array(r*6),s=new Float32Array(r*4);for(let t=0;t<r;t++){let r=t*32;for(let e=0;e<3;e++)a[t*3+e]=i.getFloat32(r+e*4,!0);let c=i.getFloat32(r+12,!0),l=i.getFloat32(r+16,!0),u=i.getFloat32(r+20,!0);if(![c,l,u].every(e=>Number.isFinite(e)&&e>0))throw RangeError(`A .splat scale must be finite and above 0; check that the file is a valid .splat.`);let d=(n[r+28]-128)/128,f=(n[r+29]-128)/128,p=(n[r+30]-128)/128,m=(n[r+31]-128)/128,h=Math.hypot(d,f,p,m);if(!h)throw RangeError(`A .splat rotation is the zero quaternion (all four bytes are 128); check that the file is a valid .splat.`);d/=h,f/=h,p/=h,m/=h;let g=[1-2*(p*p+m*m),2*(f*p-m*d),2*(f*m+p*d),2*(f*p+m*d),1-2*(f*f+m*m),2*(p*m-f*d),2*(f*m-p*d),2*(p*m+f*d),1-2*(f*f+p*p)],_=[c*c,l*l,u*u],v=0;for(let e=0;e<3;e++)for(let n=e;n<3;n++)o[t*6+v++]=g[e*3]*g[n*3]*_[0]+g[e*3+1]*g[n*3+1]*_[1]+g[e*3+2]*g[n*3+2]*_[2];for(let i=0;i<3;i++)s[t*4+i]=e(n[r+24+i]/255);s[t*4+3]=n[r+27]/255}let c={positions:a,covariances:o,colors:s};return j(c),c}export{l as _,S as a,b as c,_ as d,g as f,i as g,c as h,A as i,y as l,h as m,j as n,k as o,f as p,w as r,D as s,M as t,v as u,r as v};
@@ -1,48 +0,0 @@
1
- // @margo/core-built 0.1.0
2
- import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
- const p=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
4
- import{n as e}from"./data-D31W24Wu.js";import{t}from"./view-DyJeX8rJ.js";import{c as n,d as r,f as i,l as a,o,p as s,r as c,s as l,t as u,u as d}from"./shader-B7zZyYz-.js";import{t as f}from"./view-rect-G-t2gTqO.js";import{engineRuntime as __margoRuntime}from"@margoengine/core/extension";import{MargoError as m}from"@margoengine/math/errors";function h(e){if(e.length!==2||!e.every(Number.isFinite)||e[0]<0||e[1]>1||e[0]>e[1])throw RangeError(`Dither fade needs 0 <= lower <= upper <= 1; correct the coverage interval`)}var g=`fn margoDitherRank(p:vec2f)->u32 {
5
- let x=u32(p.x)&3u;let y=u32(p.y)&3u;
6
- return 4u*(2u*((x&1u)^(y&1u))+(y&1u))+2u*(((x>>1u)&1u)^((y>>1u)&1u))+((y>>1u)&1u);
7
- }`,_=`struct Sort {count:u32,groups:u32,blocks:u32,shift:u32}
8
- @group(0) @binding(0) var<uniform> sort:Sort;`,v=_+`
9
- @group(0) @binding(1) var<storage,read> input:array<vec2u>;
10
- @group(0) @binding(2) var<storage,read_write> histograms:array<u32>;
11
- var<workgroup> bins:array<atomic<u32>,16>;
12
- @compute @workgroup_size(256) fn main(@builtin(global_invocation_id) id:vec3u,@builtin(local_invocation_index) lane:u32,@builtin(workgroup_id) group:vec3u){
13
- if(lane<16){atomicStore(&bins[lane],0);}workgroupBarrier();
14
- if(id.x<sort.count){atomicAdd(&bins[(input[id.x].x>>sort.shift)&15],1);}workgroupBarrier();
15
- if(lane<16){histograms[lane*sort.groups+group.x]=atomicLoad(&bins[lane]);}}`,y=_+`
16
- @group(0) @binding(1) var<storage,read> histograms:array<u32>;
17
- @group(0) @binding(2) var<storage,read_write> offsets:array<u32>;
18
- @group(0) @binding(3) var<storage,read_write> totals:array<u32>;
19
- var<workgroup> values:array<u32,256>;
20
- @compute @workgroup_size(256) fn main(@builtin(local_invocation_index) lane:u32,@builtin(workgroup_id) group:vec3u){
21
- let index=group.x*256+lane;values[lane]=0;if(index<sort.groups){values[lane]=histograms[group.y*sort.groups+index];}workgroupBarrier();
22
- for(var stride=1u;stride<256;stride*=2){let at=(lane+1)*stride*2-1;if(at<256){values[at]+=values[at-stride];}workgroupBarrier();}
23
- if(lane==0){totals[group.y*sort.blocks+group.x]=values[255];values[255]=0;}workgroupBarrier();
24
- for(var stride=128u;stride>0;stride/=2){let at=(lane+1)*stride*2-1;if(at<256){let left=values[at-stride];values[at-stride]=values[at];values[at]+=left;}workgroupBarrier();}
25
- if(index<sort.groups){offsets[group.y*sort.groups+index]=values[lane];}}`,b=_+`
26
- @group(0) @binding(1) var<storage,read> totals:array<u32>;
27
- @group(0) @binding(2) var<storage,read_write> bases:array<u32>;
28
- var<workgroup> values:array<u32,1024>;
29
- @compute @workgroup_size(256) fn main(@builtin(local_invocation_index) lane:u32){
30
- for(var i=lane;i<1024;i+=256){values[i]=0;if(i<sort.blocks*16){values[i]=totals[i];}}workgroupBarrier();
31
- for(var stride=1u;stride<1024;stride*=2){for(var i=lane;i<1024/(stride*2);i+=256){let at=(i+1)*stride*2-1;values[at]+=values[at-stride];}workgroupBarrier();}
32
- if(lane==0){values[1023]=0;}workgroupBarrier();
33
- for(var stride=512u;stride>0;stride/=2){for(var i=lane;i<1024/(stride*2);i+=256){let at=(i+1)*stride*2-1;let left=values[at-stride];values[at-stride]=values[at];values[at]+=left;}workgroupBarrier();}
34
- for(var i=lane;i<sort.blocks*16;i+=256){bases[i]=values[i];}}`,x=_+`
35
- @group(0) @binding(1) var<storage,read> input:array<vec2u>;
36
- @group(0) @binding(2) var<storage,read_write> output:array<vec2u>;
37
- @group(0) @binding(3) var<storage,read> offsets:array<u32>;
38
- @group(0) @binding(4) var<storage,read> bases:array<u32>;
39
- var<workgroup> masks:array<atomic<u32>,128>;
40
- @compute @workgroup_size(256) fn main(@builtin(global_invocation_id) id:vec3u,@builtin(local_invocation_index) lane:u32,@builtin(workgroup_id) group:vec3u){
41
- if(lane<128){atomicStore(&masks[lane],0);}workgroupBarrier();
42
- var item=vec2u(0);var digit=0u;let word=lane/32;let bit=lane%32;
43
- if(id.x<sort.count){item=input[id.x];digit=(item.x>>sort.shift)&15;atomicOr(&masks[digit*8+word],1u<<bit);}workgroupBarrier();
44
- if(id.x<sort.count){var rank=countOneBits(atomicLoad(&masks[digit*8+word])&((1u<<bit)-1));
45
- for(var i=0u;i<word;i++){rank+=countOneBits(atomicLoad(&masks[digit*8+i]));}
46
- let destination=bases[digit*sort.blocks+group.x/256]+offsets[digit*sort.groups+group.x]+rank;output[destination]=item;}}`;function S(e){return e?.adapterInfo?.vendor===`margo-webgl2`?`webgl2`:`native`}function C(e,t){return S(e)===`webgl2`?t():void 0}var w=e=>typeof e==`string`&&e.endsWith(`float`),T=0,E=class _{coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);constructor(t){this.name=`splats:`+T++,this.buffers=[],this.inputs=[],this.keys=[],this.projectionBuffers=[],this.fogSource=null,this.fogData=/* @__PURE__ */ new Float32Array(12),this.sortPipelines=[],this.sortGroups=[],this.renders=/* @__PURE__ */ new Map,this.disposed=!1,this.mip=!1,this.writes=0,this.transparency=d(),this.attachment={view:void 0,loadOp:`load`,storeOp:`store`},this.plainDepth={view:void 0,depthReadOnly:!0},this.stencilDepth={view:void 0,depthReadOnly:!0,stencilReadOnly:!0},this.presentation={colorAttachments:[this.attachment],depthStencilAttachment:this.plainDepth},this.values=/* @__PURE__ */ new Float32Array(48),this.integers=new Uint32Array(this.values.buffer),this.view=this.values.subarray(16,32),this.count=e(t),this.positions=new Float32Array(this.count*4),this.covariances=new Float32Array(this.count*8),this.colors=t.colors.slice(),this.sh=t.sphericalHarmonics?.slice()??/* @__PURE__ */ new Float32Array(4);for(let e=0;e<this.count;e++)this.positions.set(t.positions.subarray(e*3,e*3+3),e*4),this.positions[e*4+3]=t.colors[e*4+3],this.covariances.set(t.covariances.subarray(e*6,e*6+6),e*8)}static reserve(e,t=0){if(![0,4,9,16,25].includes(t))throw RangeError(`Splat SH coefficients must be 0, 4, 9, 16 or 25`);if(!Number.isInteger(e)||e<1||e>3e6)throw RangeError(`Splats.reserve needs 1 to 3,000,000 splats`);let n=new Float32Array(e*6);for(let t=0;t<e;t++)n[t*6]=n[t*6+3]=n[t*6+5]=1;return new _({positions:new Float32Array(e*3),covariances:n,colors:new Float32Array(e*4),...t?{sphericalHarmonics:new Float32Array(e*t*3)}:{}})}get coefficients(){return this.sh.length===4?0:this.sh.length/this.count/3}write(t,n){let r=e(n),i=this.coefficients;if((n.sphericalHarmonics?n.sphericalHarmonics.length/r/3:0)!==i)throw RangeError(`Splats.write needs ${i} SH coefficients per channel, as the cloud has`);this.range(t,r);for(let e=0;e<r;e++)this.positions.set(n.positions.subarray(e*3,e*3+3),(t+e)*4),this.positions[(t+e)*4+3]=n.colors[e*4+3],this.covariances.set(n.covariances.subarray(e*6,e*6+6),(t+e)*8);return this.colors.set(n.colors,t*4),i&&this.sh.set(n.sphericalHarmonics,t*i*3),this.upload(t,r,!0)}clear(e,t){this.range(e,t);for(let n=e;n<e+t;n++)this.positions[n*4+3]=this.colors[n*4+3]=0;return this.upload(e,t,!1)}setOpacities(e,t,n=1){if(this.range(e,t.length),!(n>=0&&n<=1))throw RangeError(`Splats.setOpacities scale must be 0 to 1`);for(let r=0;r<t.length;r++)this.positions[(e+r)*4+3]=this.colors[(e+r)*4+3]=t[r]*n;return this.upload(e,t.length,!1)}range(e,t){if(this.disposed)throw new m(`disposed`,`The splats were disposed; write to a live cloud`);if(!Number.isInteger(e)||!Number.isInteger(t)||e<0||t<0||e+t>this.count)throw RangeError(`Splats ${e} to ${e+t-1} are outside this cloud of ${this.count}; keep first + count within the cloud`)}upload(e,t,n){this.writes++;let[r,i,a,o]=this.inputs,s=this.device?.queue,c=this.coefficients;return!s||!r||!t||(s.writeBuffer(r,e*16,this.positions,e*4,t*4),s.writeBuffer(a,e*16,this.colors,e*4,t*4),n&&s.writeBuffer(i,e*32,this.covariances,e*8,t*8),n&&c&&s.writeBuffer(o,e*c*12,this.sh,e*c*3,t*c*3)),this}get ditherFadeBytes(){return this.fadeData?.byteLength??0}setDitherFade(e,t,n=0,r=1){this.range(e,t),h([n,r]),this.fadeData||(this.fadeData=new Uint32Array(this.count).fill(4096),this.record=void 0,this.renders.clear());let i=Math.ceil(Math.fround(n)*16-.5),a=Math.ceil(Math.fround(r)*16-.5),o=Math.max(0,i)|Math.max(0,a)<<8;return this.fadeData.fill(o,e,e+t),this.device&&(this.fadeBuffer?t&&this.device.queue.writeBuffer(this.fadeBuffer,e*4,this.fadeData,e,t):this.prepareFade()),this}prepareFade(){if(!this.fadeData||!this.device||this.fadeBuffer)return;let e=this.device;this.fadeBuffer=e.createBuffer({size:this.fadeData.byteLength,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_DST}),this.buffers.push(this.fadeBuffer),e.queue.writeBuffer(this.fadeBuffer,0,this.fadeData)}async read(){this.range(0,0);let e=this.device,[t,n,r,i]=this.inputs;for(let a=-1;e&&t&&a!==this.writes;){a=this.writes;let o=[[t,this.positions],[n,this.covariances],[r,this.colors],[i,this.sh]],s=e.createCommandEncoder(),c=o.map(([t,n])=>{let r=e.createBuffer({size:n.byteLength,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST});return s.copyBufferToBuffer(t,0,r,0,n.byteLength),r});e.queue.submit([s.finish()]);try{await Promise.all(c.map(e=>e.mapAsync(GPUMapMode.READ))),a===this.writes&&c.forEach((e,t)=>o[t][1].set(new Float32Array(e.getMappedRange())))}finally{for(let e of c)e.destroy()}}let a=this.count,o={positions:new Float32Array(a*3),covariances:new Float32Array(a*6),colors:this.colors.slice()};for(let e=0;e<a;e++)o.positions.set(this.positions.subarray(e*4,e*4+3),e*3),o.covariances.set(this.covariances.subarray(e*8,e*8+6),e*6);return this.coefficients&&(o.sphericalHarmonics=this.sh.slice()),o}get antialias(){return this.mip}set antialias(e){this.mip=!!e}get fog(){return this.fogSource}setFog(e){return this.fogSource=r(e),this}static attach(e,t){__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));let n=new _(t);return p(e).use(n),n}setup(e){if(this.disposed||!e.gpu)throw new m(this.disposed?`disposed`:`unsupported`,`Splats require WebGPU and a live cloud; attach a new Splats to a WebGPU engine; see https://margoengine.com/errors#webgpu`);this.engine=e,this.device=e.gpu;let t=this.device,n=Math.ceil(this.count/256),r=Math.ceil(n/256);if(r*16>1024||n>t.limits.maxComputeWorkgroupsPerDimension||t.limits.maxComputeInvocationsPerWorkgroup<256||t.limits.maxComputeWorkgroupSizeX<256||t.limits.maxStorageBuffersPerShaderStage<7)throw RangeError(`Splat compute exceeds device limits; use fewer splats or a device with larger compute limits.`);for(let e of[this.positions.byteLength,this.covariances.byteLength,this.colors.byteLength,this.sh.byteLength,this.count*32,this.count*16,this.count*8,n*16*4])if(e>t.limits.maxBufferSize||e>t.limits.maxStorageBufferBindingSize)throw RangeError(`Splat attributes exceed device storage limits; use fewer splats.`);let i=(e,n,r)=>{let i=t.createBuffer({size:e,usage:n|GPUBufferUsage.COPY_DST});return this.buffers.push(i),r&&t.queue.writeBuffer(i,0,r),i};this.prepareFade(),this.uniform=i(192,GPUBufferUsage.UNIFORM),this.index=i(24,GPUBufferUsage.INDEX,new Uint32Array([0,1,2,2,1,3]));let a=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC,o=i(this.positions.byteLength,a,this.positions),s=i(this.covariances.byteLength,a,this.covariances),l=i(this.colors.byteLength,a,this.colors),u=i(this.sh.byteLength,a,this.sh);this.inputs=[o,s,l,u],this.projected=i(this.count*32,GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC),this.evaluated=i(this.count*16,GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC),this.keys=[0,1].map(()=>i(this.count*8,GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC));let d=i(n*16*4,GPUBufferUsage.STORAGE),f=i(n*16*4,GPUBufferUsage.STORAGE),p=i(r*16*4,GPUBufferUsage.STORAGE),h=i(r*16*4,GPUBufferUsage.STORAGE),g=e=>t.createComputePipeline({layout:`auto`,compute:{module:t.createShaderModule({code:e}),entryPoint:`main`}}),_=(e,n)=>t.createBindGroup({layout:e.getBindGroupLayout(0),entries:n.map((e,t)=>({binding:t,resource:{buffer:e}}))});this.projection=g(c),this.projectionBuffers=[this.uniform,o,s,l,u,this.projected,this.evaluated,this.keys[0]],this.projectionGroup=_(this.projection,this.projectionBuffers),this.sortPipelines=[v,y,b,x].map(g);let S=[];for(let e=0;e<8;e++){let t=i(16,GPUBufferUsage.UNIFORM,new Uint32Array([this.count,n,r,e*4])),a=e%2;S.push(t),this.sortGroups.push([_(this.sortPipelines[0],[t,this.keys[a],d]),_(this.sortPipelines[1],[t,d,f,p]),_(this.sortPipelines[2],[t,p,h]),_(this.sortPipelines[3],[t,this.keys[a],this.keys[1-a],f,h])])}let w=this.keys;C(t,()=>import(`./compat-CDoDsXE8.js`))?.then(({splatCompatSort:e})=>{this.keys===w&&(this.compatSort=e(t,S[0],w))})}registerPasses(e){e.add({name:this.name+`:project-sort`,order:-7,execute:e=>{e.backend===`webgpu`&&this.sort(e)}}),e.add({name:this.name+`:draw`,order:3,execute:e=>{e.backend===`webgpu`&&this.draw(e)}})}sort(e){this.values.set(e.camera.matrix),t(this.view,e.camera),this.values.set(e.camera.position,32);let n=e.height/(2*Math.tan(e.camera.fov*Math.PI/360)),r=this.values,a=this.integers,o=e.encoder;r[36]=e.width,r[37]=e.height,r[38]=r[39]=n,r[40]=e.camera.near,r[41]=e.camera.far,r[42]=+w(e.format),r[43]=0;let c=s(e.camera.matrix,e.camera.position);c&&(r[38]=c.sx*e.width/2,r[39]=c.sy*e.height/2,r[40]=c.near,r[41]=c.far,r[43]=1),a[44]=this.count,a[45]=this.sh.length===4?0:this.sh.length/this.count/3,a[46]=+this.mip,a[47]=0,e.device.queue.writeBuffer(this.uniform,0,this.values);let l=this.fogSource&&this.fogResources(e.device);l&&(i(this.fogSource,e.camera.position,this.fogData),e.device.queue.writeBuffer(l.buffer,0,this.fogData));let u=Math.ceil(this.count/256),d=Math.ceil(u/256),f=o.beginComputePass();try{if(l?this.dispatch(f,l.pipeline,l.group,u,1):this.dispatch(f,this.projection,this.projectionGroup,u,1),this.transparency.hook)return;if(this.compatSort)this.dispatch(f,this.compatSort.pipeline,this.compatSort.group,1,1);else for(let e=0;e<8;e++){let t=this.sortGroups[e];this.dispatch(f,this.sortPipelines[0],t[0],u,1),this.dispatch(f,this.sortPipelines[1],t[1],d,16),this.dispatch(f,this.sortPipelines[2],t[2],1,1),this.dispatch(f,this.sortPipelines[3],t[3],u,1)}}finally{f.end()}}fogResources(e){if(this.fogged)return this.fogged;let t=e.createBuffer({size:48,usage:GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST});this.buffers.push(t);let n=e.createComputePipeline({layout:`auto`,compute:{module:e.createShaderModule({code:u()}),entryPoint:`main`}});return this.fogged={buffer:t,pipeline:n,group:e.createBindGroup({layout:n.getBindGroupLayout(0),entries:[...this.projectionBuffers,t].map((e,t)=>({binding:t,resource:{buffer:e}}))})}}dispatch(e,t,n,r,i){e.setPipeline(t),e.setBindGroup(0,n),e.dispatchWorkgroups(r,i)}fadeShader(e){return this.fadeData?g+`
47
- @group(0) @binding(4) var<storage,read> fadeRanges:array<u32>;
48
- `+e.replace(`@location(1) color:vec4f}`,`@location(1) color:vec4f,@location(3) @interpolate(flat) margoFade:u32}`).replace(`@location(1) color:vec4f,@location(2) margoDepth:f32}`,`@location(1) color:vec4f,@location(2) margoDepth:f32,@location(3) @interpolate(flat) margoFade:u32}`).replace(`o.corner=corner;`,`o.corner=corner;o.margoFade=fadeRanges[index];`).replace(`let power=-4.5*dot(i.corner,i.corner);`,`let fade=margoDitherRank(i.clip.xy);if(fade<(i.margoFade&255u)||fade>=(i.margoFade>>8u)){discard;}let power=-4.5*dot(i.corner,i.corner);`):e}pipeline(e,t,r){t+=this.fadeData?`|dither`:``;let i=this.renders.get(t);if(i)return i;let a=r?.surface(1),s=e.device.createShaderModule({code:this.fadeShader(a?l(a):o)}),c=e.device.createRenderPipeline({layout:`auto`,vertex:{module:s,entryPoint:`vertex`},fragment:{module:s,entryPoint:r?`transparent`:`fragment`,...r?.blends?{constants:{margoKind:0}}:{},targets:r?[...r.targets]:[{format:e.format,blend:n}]},multisample:e.descriptor.multisample,depthStencil:{format:e.descriptor.depthStencil.format,depthWriteEnabled:!1,depthCompare:`greater-equal`}});return i={pipeline:c,group:e.device.createBindGroup({layout:c.getBindGroupLayout(0),entries:[this.uniform,this.projected,this.evaluated,this.keys[0],...this.fadeBuffer?[this.fadeBuffer]:[]].map((e,t)=>({binding:t,resource:{buffer:e}}))})},this.renders.set(t,i),i}draw(e){if(this.transparency.hook)return;let t=e.descriptor.multisample?.count,n=e.descriptor.depthStencil.format,r=this.record;(!r||this.recordSamples!==t||this.recordFormat!==e.format||this.recordDepth!==n)&&(r=this.pipeline(e,t+`:`+e.format+`:`+n),this.record=r,this.recordSamples=t,this.recordFormat=e.format,this.recordDepth=n,this.presentation.depthStencilAttachment=n.includes(`stencil`)?this.stencilDepth:this.plainDepth),this.attachment.view=e.color,this.presentation.depthStencilAttachment.view=e.depth;let i=e.encoder.beginRenderPass(this.presentation);f(i,e),i.setPipeline(r.pipeline),i.setBindGroup(0,r.group),i.setIndexBuffer(this.index,`uint32`),i.drawIndexed(6,this.count),i.end(),this.engine.stats.drawCalls++}setTransparency(e,t){return this.transparency.set(e,t)}drawTransparent(e,t){if(!this.uniform)return;let n=t.surface(1),{pipeline:r,group:i}=this.pipeline(e,e.descriptor.multisample?.count+`:`+e.format+`:`+e.descriptor.depthStencil.format+n,t),o=a(e.device,t,r,i);o.setIndexBuffer(this.index,`uint32`),o.drawIndexed(6,this.count),this.engine.stats.drawCalls++}get attached(){return!!this.projected}get gpuState(){if(!this.projected||!this.evaluated||!this.keys.length)throw new m(`state`,`Splats are not attached; attach them to an engine first`);let[e,t,n,r]=this.inputs;return{sorted:this.keys[0],ellipses:this.projected,colors:this.evaluated,positions:e,covariances:t,inputColors:n,sh:r}}release(){for(let e of this.buffers)e.destroy();this.buffers=[],this.inputs=[],this.keys=[],this.sortGroups=[],this.sortPipelines=[],this.compatSort=void 0,this.renders.clear(),this.record=void 0,this.fadeBuffer=void 0,this.uniform=this.index=this.projected=this.evaluated=void 0,this.projection=void 0,this.projectionGroup=void 0,this.projectionBuffers=[],this.fogged=void 0}onDeviceRestored(e){this.release(),this.setup(e)}dispose(){this.disposed||(this.disposed=!0,this.transparency.released?.(),this.engine?.sceneOf(this)?.removePlugin(this.name),this.release(),this.engine=this.device=void 0)}[Symbol.dispose??Symbol()](){this.dispose()}destroy(){this.dispose()}};export{E as t};
@@ -1 +0,0 @@
1
- import{MargoError as e}from"@margoengine/math/errors";var t={deflate:[120,156,3,0,0,0,0,1],"deflate-raw":[3,0],gzip:[31,139,8,0,0,0,0,0,0,3,3,0,0,0,0,0,0,0,0,0]},n=/* @__PURE__ */ new Map;async function r(e,t){try{return await new Response(new Blob([e]).stream().pipeThrough(new DecompressionStream(t))).arrayBuffer(),!0}catch{return!1}}function i(e){if(!(e in t))throw TypeError(`format must be deflate, deflate-raw or gzip (got ${e})`);let i=n.get(e);return i||(i=r(new Uint8Array([...t[e],0]),e),n.set(e,i)),i}async function a(e,t){return!await i(t)||e.byteLength===0?!1:r(e.subarray(0,e.byteLength-1),t)}async function o(t,n,r,i){if(r?.throwIfAborted(),typeof DecompressionStream>`u`)throw new e(`unsupported`,`This runtime has no DecompressionStream; use a current browser or Node 18+.`);let o=new Blob([t]).stream().pipeThrough(new DecompressionStream(n)).getReader(),s=[],c=0;try{for(;;){r?.throwIfAborted();let{done:e,value:t}=await o.read();if(e||(s.push(t),c+=t.length,c>i))break}}catch(e){throw r?.aborted?r.reason:RangeError(`The compressed data is damaged (${e.message}); check that the file is complete.`)}if(c>i)throw await o.cancel().catch(()=>{}),RangeError(`The compressed data expands past ${i} bytes, more than the file can hold; check that the file is complete and not damaged.`);if(await a(t,n))throw RangeError(`The compressed data is damaged (data after the end of the stream); check that the file is complete.`);let l=new Uint8Array(c),u=0;for(let e of s)l.set(e,u),u+=e.length;return l}async function s(t,n){let r=new DataView(t.buffer,t.byteOffset,t.byteLength),i=e=>{throw RangeError(`Not a readable ZIP archive: ${e}; check that the file is complete.`)},a=-1;for(let e=t.length-22;e>=Math.max(0,t.length-22-65535);e--)if(r.getUint32(e,!0)===101010256){a=e;break}a<0&&i(`no end-of-directory record`);let s=r.getUint16(a+10,!0),c=r.getUint32(a+16,!0);if(s===65535||c===4294967295)throw new e(`unsupported`,`ZIP64 archives are not supported; re-export the SOG.`);let l=/* @__PURE__ */ new Map;for(let a=0;a<s;a++){(c+46>t.length||r.getUint32(c,!0)!==33639248)&&i(`a damaged central directory`);let a=r.getUint16(c+10,!0),s=r.getUint32(c+20,!0),u=r.getUint32(c+24,!0),d=r.getUint16(c+28,!0),f=r.getUint16(c+30,!0),p=r.getUint16(c+32,!0),m=r.getUint32(c+42,!0),h=new TextDecoder().decode(t.subarray(c+46,c+46+d));if(c+=46+d+f+p,h.endsWith(`/`))continue;(m+30>t.length||r.getUint32(m,!0)!==67324752)&&i(`a damaged local header`);let g=m+30+r.getUint16(m+26,!0)+r.getUint16(m+28,!0);g+s>t.length&&i(`an entry that runs past the end of the file`);let _=t.subarray(g,g+s),v;if(a===0)v=_;else if(a===8)v=await o(_,`deflate-raw`,n,u);else throw new e(`unsupported`,`ZIP entry ${h} uses compression method ${a}; only stored and deflate are supported.`);v.length!==u&&i(`entry ${h} has the wrong size`),l.set(h,v)}return l}export{s as n,o as t};