@speridlabs/visus 1.0.2 → 1.0.4
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/dist/main.d.ts +328 -320
- package/dist/main.es.js +530 -671
- package/dist/main.umd.js +6 -6
- package/dist/react.es.js +606 -747
- package/package.json +1 -1
package/dist/main.umd.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
(function(
|
|
2
|
-
Please include <script src="https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.min.js"><\/script> before loading visus/main.umd.js`);function G(E){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(E){for(const t in E)if(t!=="default"){const o=Object.getOwnPropertyDescriptor(E,t);Object.defineProperty(e,t,o.get?o:{enumerable:!0,get:()=>E[t]})}}return e.default=E,Object.freeze(e)}const i=G(P);class ee{constructor(){p(this,"min",new i.Vector3(1/0,1/0,1/0));p(this,"max",new i.Vector3(-1/0,-1/0,-1/0));p(this,"center",new i.Vector3);p(this,"halfExtents",new i.Vector3)}reset(){this.min.set(1/0,1/0,1/0),this.max.set(-1/0,-1/0,-1/0),this.center.set(0,0,0),this.halfExtents.set(0,0,0)}expandByPoint(e){this.min.x=Math.min(this.min.x,e.x),this.min.y=Math.min(this.min.y,e.y),this.min.z=Math.min(this.min.z,e.z),this.max.x=Math.max(this.max.x,e.x),this.max.y=Math.max(this.max.y,e.y),this.max.z=Math.max(this.max.z,e.z),this.updateDerived()}expandByBox(e){this.min.x=Math.min(this.min.x,e.min.x),this.min.y=Math.min(this.min.y,e.min.y),this.min.z=Math.min(this.min.z,e.min.z),this.max.x=Math.max(this.max.x,e.max.x),this.max.y=Math.max(this.max.y,e.max.y),this.max.z=Math.max(this.max.z,e.max.z),this.updateDerived()}updateDerived(){this.center.addVectors(this.min,this.max).multiplyScalar(.5),this.halfExtents.subVectors(this.max,this.min).multiplyScalar(.5)}containsPoint(e){return e.x>=this.min.x&&e.x<=this.max.x&&e.y>=this.min.y&&e.y<=this.max.y&&e.z>=this.min.z&&e.z<=this.max.z}toBox3(){return new i.Box3().set(this.min,this.max)}clone(){const e=new ee;return e.min.copy(this.min),e.max.copy(this.max),e.center.copy(this.center),e.halfExtents.copy(this.halfExtents),e}}class ie{constructor(e=0){p(this,"numSplats",0);p(this,"positions");p(this,"rotations");p(this,"scales");p(this,"colors");p(this,"opacities");p(this,"centers");p(this,"boundingBox",new ee);this.numSplats=e,this.allocateBuffers(e)}allocateBuffers(e){this.positions=new Float32Array(e*3),this.rotations=new Float32Array(e*4),this.scales=new Float32Array(e*3),this.colors=new Float32Array(e*3),this.opacities=new Float32Array(e),this.centers=new Float32Array(e*3)}setSplat(e,t,o,r,n,s){if(e>=this.numSplats)throw new Error(`Splat index out of bounds: ${e} >= ${this.numSplats}`);const a=e*3,c=e*4,f=e*3,m=e*3;this.positions[a]=t.x,this.positions[a+1]=t.y,this.positions[a+2]=t.z,this.rotations[c]=o.x,this.rotations[c+1]=o.y,this.rotations[c+2]=o.z,this.rotations[c+3]=o.w,this.scales[f]=r.x,this.scales[f+1]=r.y,this.scales[f+2]=r.z,this.colors[m]=n.r,this.colors[m+1]=n.g,this.colors[m+2]=n.b,this.opacities[e]=s,this.centers[a]=t.x,this.centers[a+1]=t.y,this.centers[a+2]=t.z}getSplat(e){if(e>=this.numSplats)throw new Error(`Splat index out of bounds: ${e} >= ${this.numSplats}`);const t=e*3,o=e*4,r=e*3,n=e*3;return{position:new i.Vector3(this.positions[t],this.positions[t+1],this.positions[t+2]),rotation:new i.Quaternion(this.rotations[o],this.rotations[o+1],this.rotations[o+2],this.rotations[o+3]),scale:new i.Vector3(Math.exp(this.scales[r]),Math.exp(this.scales[r+1]),Math.exp(this.scales[r+2])),color:new i.Color(this.colors[n],this.colors[n+1],this.colors[n+2]),opacity:this.opacities[e]}}calculateBoundingBox(){this.boundingBox.reset();const e=new i.Vector3;for(let t=0;t<this.numSplats;t++){const o=t*3;e.set(this.positions[o],this.positions[o+1],this.positions[o+2]),this.boundingBox.expandByPoint(e)}return this.boundingBox}createDebugGeometry(){const e=new i.BufferGeometry;e.setAttribute("position",new i.BufferAttribute(this.positions,3));const t=new Float32Array(this.numSplats*3),o=new i.Quaternion,r=new i.Euler;for(let n=0;n<this.numSplats;n++){const s=n*4,a=n*3;o.set(this.rotations[s],this.rotations[s+1],this.rotations[s+2],this.rotations[s+3]),r.setFromQuaternion(o),t[a]=r.x,t[a+1]=r.y,t[a+2]=r.z}return e.setAttribute("rotation",new i.BufferAttribute(t,3)),e.setAttribute("scale",new i.BufferAttribute(this.scales,3)),e.setAttribute("color",new i.BufferAttribute(this.colors,3)),e.setAttribute("opacity",new i.BufferAttribute(this.opacities,1)),e}}class ae extends i.EventDispatcher{constructor(){super();p(this,"worker");p(this,"centers",null);p(this,"orderTexture",null);p(this,"chunks",null);p(this,"lastCameraPosition",new i.Vector3);p(this,"lastCameraDirection",new i.Vector3);const t=this.createWorkerCode(),o=new Blob([t],{type:"application/javascript"});this.worker=new Worker(URL.createObjectURL(o)),this.worker.onmessage=this.onWorkerMessage.bind(this)}onWorkerMessage(t){if(!this.orderTexture||!this.orderTexture.image)return console.error("SplatSorter: Order texture not initialized!");const{order:o,count:r}=t.data,n=this.orderTexture.image.data;if(!(n instanceof Uint32Array))return console.error("SplatSorter: Order texture data is not a Uint32Array!");n.set(new Uint32Array(o)),this.orderTexture.needsUpdate=!0;const s=n.buffer.slice(0),a={order:s};this.worker.postMessage(a,[s]),this.dispatchEvent({type:"updated",count:r})}init(t,o,r){if(!t||!(t.image.data instanceof Uint32Array))throw new Error("SplatSorter: Invalid orderTexture provided. Must be DataTexture with Uint32Array data.");if(!o||o.length%3!==0)throw new Error("SplatSorter: Invalid centers array provided. Length must be multiple of 3.");if(t.image.data.length<o.length/3)throw new Error("SplatSorter: orderTexture data buffer is smaller than the number of splats.");const n=o.length/3;this.orderTexture=t,this.centers=o.slice();const s=this.orderTexture.image.data;for(let x=0;x<n;x++)s[x]=x;this.orderTexture.needsUpdate=!0;const a=s.buffer.slice(0),c=this.centers.buffer.slice(0),f={order:a,centers:c},m=[a,c];if(r){this.chunks=r.slice();const x=this.chunks.buffer.slice(0);f.chunks=x,m.push(x)}this.worker.postMessage(f,m)}setMapping(t){if(!this.centers)return console.warn("SplatSorter: Cannot set mapping before initialization.");let o;const r=[];if(!t){const c=this.centers.buffer.slice(0);return o={centers:c,mapping:null},r.push(c),this.worker.postMessage(o,r)}const n=new Float32Array(t.length*3);for(let c=0;c<t.length;c++){const f=t[c];if(f*3+2>=this.centers.length){console.warn(`SplatSorter: Mapping index ${f} out of bounds.`);continue}const m=f*3,x=c*3;n[x+0]=this.centers[m+0],n[x+1]=this.centers[m+1],n[x+2]=this.centers[m+2]}const s=n.buffer.slice(0),a=t.buffer.slice(0);o={centers:s,mapping:a},r.push(s,a),this.worker.postMessage(o,r)}setCamera(t,o){const r=this.lastCameraPosition.distanceToSquared(t)>1e-7,n=this.lastCameraDirection.dot(o)<.9999;if(!r&&!n)return;this.lastCameraPosition.copy(t),this.lastCameraDirection.copy(o);const s={cameraPosition:{x:t.x,y:t.y,z:t.z},cameraDirection:{x:o.x,y:o.y,z:o.z}};this.worker.postMessage(s)}dispose(){this.worker&&this.worker.terminate(),this.orderTexture=null,this.centers=null,this.chunks=null}createWorkerCode(){return`(${(function(){let o=null,r=null,n=null,s=null,a=null,c=null,f=!1;const m={x:0,y:0,z:0},x={x:0,y:0,z:0},v={x:0,y:0,z:0},d={x:0,y:0,z:0};let S=null,u=null;const M=32,q=new Array(M).fill(0),j=new Array(M).fill(0),H=new Array(M).fill(0),L=(T,A,F)=>{for(;T<=A;){const C=A+T>>1,y=F(C);if(y>0)T=C+1;else if(y<0)A=C-1;else return C}return~T},Y=()=>{if(!o||!r||!a||!c)return;if(r.length===0){const g={order:o.buffer,count:0};self.postMessage(g,[o.buffer]),o=null;return}const T=a.x,A=a.y,F=a.z,C=c.x,y=c.y,z=c.z,_=1e-4,R=Math.abs(T-m.x)>_||Math.abs(A-m.y)>_||Math.abs(F-m.z)>_,B=Math.abs(C-x.x)>_||Math.abs(y-x.y)>_||Math.abs(z-x.z)>_;if(!f&&!R&&!B)return;f=!1,m.x=T,m.y=A,m.z=F,x.x=C,x.y=y,x.z=z;let l=1/0,h=-1/0;for(let g=0;g<8;++g){const D=g&1?v.x:d.x,O=g&2?v.y:d.y,b=g&4?v.z:d.z,k=D*C+O*y+b*z;l=Math.min(l,k),h=Math.max(h,k)}const w=r.length/3,W=h-l,U=(1<<Math.max(10,Math.min(20,Math.ceil(Math.log2(w/4)))))+1;if((!S||S.length!==w)&&(S=new Uint32Array(w)),!u||u.length!==U?u=new Uint32Array(U):u.fill(0),W<1e-7){for(let g=0;g<w;++g)S[g]=0;u[0]=w}else if(n&&n.length>0){const g=n.length/6;q.fill(0);for(let b=0;b<g;++b){const k=b*6,J=n[k+0],K=n[k+1],te=n[k+2],Z=n[k+3],X=J*C+K*y+te*z-l,re=X-Z,Q=X+Z,ne=Math.max(0,Math.floor(re*M/W)),se=Math.min(M,Math.ceil(Q*M/W));for(let $=ne;$<se;++$)q[$]++}let D=0;for(let b=0;b<M;++b)D+=q[b];H[0]=0,j[0]=0;for(let b=1;b<M;++b)H[b-1]=q[b-1]/D*U>>>0,j[b]=j[b-1]+H[b-1];H[M-1]=q[M-1]/D*U>>>0;const O=W/M;for(let b=0;b<w;++b){const k=b*3,J=r[k+0],K=r[k+1],te=r[k+2],Z=J*C+K*y+te*z,re=(h-Z)/O,Q=Math.max(0,Math.min(M-1,Math.floor(re))),ne=re-Q,se=j[Q]+H[Q]*ne>>>0,$=Math.min(se,U-1);S[b]=$,u[$]++}}else{const g=(U-1)/W;for(let D=0;D<w;++D){const O=D*3,b=r[O+0],k=r[O+1],J=r[O+2],K=b*C+k*y+J*z,Z=(h-K)*g>>>0,X=Math.min(Z,U-1);S[D]=X,u[X]++}}for(let g=1;g<U;g++)u[g]+=u[g-1];for(let g=w-1;g>=0;g--){const D=S[g],O=--u[D];o[O]=s?s[g]:g}const V=T*C+A*y+F*z,he=g=>{if(!o)return-1/0;const D=o[g],O=D;if(!r||O*3+2>=r.length)return-1/0;const b=O*3;return r[b]*C+r[b+1]*y+r[b+2]*z-V};let fe=w;if(w>0&&he(w-1)<0){const g=L(0,w-1,he);fe=g<0?~g:g}const Se={order:o.buffer,count:fe};self.postMessage(Se,[o.buffer]),o=null};self.onmessage=T=>{const A=T.data;A.order&&(o=new Uint32Array(A.order));let F=!1;if(A.centers&&(r=new Float32Array(A.centers),F=!0,f=!0),Object.prototype.hasOwnProperty.call(A,"mapping")&&(s=A.mapping?new Uint32Array(A.mapping):null,f=!0),A.chunks){if(n=new Float32Array(A.chunks),n.length>0&&n[3]>0)for(let C=0;C<n.length/6;++C){const y=C*6,z=n[y+0],_=n[y+1],R=n[y+2],B=n[y+3],l=n[y+4],h=n[y+5];n[y+0]=(z+B)*.5,n[y+1]=(_+l)*.5,n[y+2]=(R+h)*.5,n[y+3]=Math.sqrt((B-z)**2+(l-_)**2+(h-R)**2)*.5}f=!0}if(F&&r&&r.length>0){v.x=d.x=r[0],v.y=d.y=r[1],v.z=d.z=r[2];for(let C=1;C<r.length/3;C++){const y=C*3;v.x=Math.min(v.x,r[y+0]),d.x=Math.max(d.x,r[y+0]),v.y=Math.min(v.y,r[y+1]),d.y=Math.max(d.y,r[y+1]),v.z=Math.min(v.z,r[y+2]),d.z=Math.max(d.z,r[y+2])}}else F&&r&&r.length===0&&(v.x=d.x=v.y=d.y=v.z=d.z=0);A.cameraPosition&&(a=A.cameraPosition),A.cameraDirection&&(c=A.cameraDirection),Y()}}).toString()})();`}}const pe=(E,e)=>{const t=ce(E),o=ce(e);return t|o<<16};function ce(E){const e=new Float32Array([E]),o=new Int32Array(e.buffer)[0];let r=o>>16&32768,n=o>>12&2047;const s=o>>23&255;return s<103?r:s>142?(r|=31744,r|=(s===255?0:1)&&o&8388607,r):s<113?(n|=2048,r|=(n>>114-s)+(n>>113-s&1),r):(r|=s-112<<10|n>>1,r+=n&1,r)}const me=new ArrayBuffer(4),le=new DataView(me),xe=E=>(le.setUint32(0,E,!0),le.getFloat32(0,!0));class ue{constructor(e){p(this,"transformA");p(this,"transformB");p(this,"colorTexture");p(this,"orderTexture");p(this,"textureWidth");p(this,"textureHeight");const t=e.numSplats;this.textureWidth=Math.ceil(Math.sqrt(t)),this.textureHeight=Math.ceil(t/this.textureWidth),this.transformA=this.createTransformATexture(e),this.transformB=this.createTransformBTexture(e),this.colorTexture=this.createColorTexture(e),this.orderTexture=this.createOrderTexture(t)}createTransformATexture(e){const t=e.numSplats,o=new Float32Array(this.textureWidth*this.textureHeight*4);for(let n=0;n<t;n++){const s=n*4,a=n*3,c=n*4;o[s]=e.positions[a],o[s+1]=e.positions[a+1],o[s+2]=e.positions[a+2];const f=e.rotations[c+0],m=e.rotations[c+1],x=pe(f,m);o[s+3]=xe(x)}const r=new i.DataTexture(o,this.textureWidth,this.textureHeight,i.RGBAFormat,i.FloatType);return r.needsUpdate=!0,r.magFilter=i.NearestFilter,r.minFilter=i.NearestFilter,r}createTransformBTexture(e){const t=e.numSplats,o=new Float32Array(this.textureWidth*this.textureHeight*4);for(let n=0;n<t;n++){const s=n*4,a=n*3,c=n*4;o[s]=e.scales[a],o[s+1]=e.scales[a+1],o[s+2]=e.scales[a+2],o[s+3]=e.rotations[c+2]}const r=new i.DataTexture(o,this.textureWidth,this.textureHeight,i.RGBAFormat,i.FloatType);return r.needsUpdate=!0,r.magFilter=i.NearestFilter,r.minFilter=i.NearestFilter,r}createColorTexture(e){const t=e.numSplats,o=new Float32Array(this.textureWidth*this.textureHeight*4);for(let n=0;n<t;n++){const s=n*4,a=n*3;o[s]=e.colors[a],o[s+1]=e.colors[a+1],o[s+2]=e.colors[a+2],o[s+3]=e.opacities[n]}const r=new i.DataTexture(o,this.textureWidth,this.textureHeight,i.RGBAFormat,i.FloatType);return r.needsUpdate=!0,r}createOrderTexture(e){const t=new Uint32Array(this.textureWidth*this.textureHeight);for(let r=0;r<e;r++)t[r]=r;const o=new i.DataTexture(t,this.textureWidth,this.textureHeight,i.RedIntegerFormat,i.UnsignedIntType);return o.needsUpdate=!0,o}dispose(){this.transformA.dispose(),this.transformB.dispose(),this.colorTexture.dispose(),this.orderTexture.dispose()}}const ge=`
|
|
1
|
+
(function(A,P){typeof exports=="object"&&typeof module<"u"?P(exports,require("three")):typeof define=="function"&&define.amd?define(["exports","three"],P):(A=typeof globalThis<"u"?globalThis:A||self,P(A.Visus={},A.THREE))})(this,function(A,P){"use strict";var St=Object.defineProperty;var Ct=(A,P,L)=>P in A?St(A,P,{enumerable:!0,configurable:!0,writable:!0,value:L}):A[P]=L;var c=(A,P,L)=>Ct(A,typeof P!="symbol"?P+"":P,L);if(typeof THREE>"u")throw new Error(`Visus UMD build requires Three.js as a global "THREE".
|
|
2
|
+
Please include <script src="https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.min.js"><\/script> before loading visus/main.umd.js`);function L(T){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(T){for(const s in T)if(s!=="default"){const e=Object.getOwnPropertyDescriptor(T,s);Object.defineProperty(t,s,e.get?e:{enumerable:!0,get:()=>T[s]})}}return t.default=T,Object.freeze(t)}const o=L(P);class Q{constructor(){c(this,"min",new o.Vector3(1/0,1/0,1/0));c(this,"max",new o.Vector3(-1/0,-1/0,-1/0));c(this,"center",new o.Vector3);c(this,"halfExtents",new o.Vector3)}reset(){this.min.set(1/0,1/0,1/0),this.max.set(-1/0,-1/0,-1/0),this.center.set(0,0,0),this.halfExtents.set(0,0,0)}expandByPoint(t){this.min.x=Math.min(this.min.x,t.x),this.min.y=Math.min(this.min.y,t.y),this.min.z=Math.min(this.min.z,t.z),this.max.x=Math.max(this.max.x,t.x),this.max.y=Math.max(this.max.y,t.y),this.max.z=Math.max(this.max.z,t.z),this.updateDerived()}expandByBox(t){this.min.x=Math.min(this.min.x,t.min.x),this.min.y=Math.min(this.min.y,t.min.y),this.min.z=Math.min(this.min.z,t.min.z),this.max.x=Math.max(this.max.x,t.max.x),this.max.y=Math.max(this.max.y,t.max.y),this.max.z=Math.max(this.max.z,t.max.z),this.updateDerived()}updateDerived(){this.center.addVectors(this.min,this.max).multiplyScalar(.5),this.halfExtents.subVectors(this.max,this.min).multiplyScalar(.5)}containsPoint(t){return t.x>=this.min.x&&t.x<=this.max.x&&t.y>=this.min.y&&t.y<=this.max.y&&t.z>=this.min.z&&t.z<=this.max.z}toBox3(){return new o.Box3().set(this.min,this.max)}clone(){const t=new Q;return t.min.copy(this.min),t.max.copy(this.max),t.center.copy(this.center),t.halfExtents.copy(this.halfExtents),t}}class it{constructor(t=0){c(this,"numSplats",0);c(this,"positions");c(this,"rotations");c(this,"scales");c(this,"colors");c(this,"opacities");c(this,"centers");c(this,"boundingBox",new Q);this.numSplats=t,this.allocateBuffers(t)}allocateBuffers(t){this.positions=new Float32Array(t*3),this.rotations=new Float32Array(t*4),this.scales=new Float32Array(t*3),this.colors=new Float32Array(t*3),this.opacities=new Float32Array(t),this.centers=new Float32Array(t*3)}setSplat(t,s,e,r,n,i){if(t>=this.numSplats)throw new Error(`Splat index out of bounds: ${t} >= ${this.numSplats}`);const a=t*3,d=t*4,p=t*3,v=t*3;this.positions[a]=s.x,this.positions[a+1]=s.y,this.positions[a+2]=s.z,this.rotations[d]=e.x,this.rotations[d+1]=e.y,this.rotations[d+2]=e.z,this.rotations[d+3]=e.w,this.scales[p]=r.x,this.scales[p+1]=r.y,this.scales[p+2]=r.z,this.colors[v]=n.r,this.colors[v+1]=n.g,this.colors[v+2]=n.b,this.opacities[t]=i,this.centers[a]=s.x,this.centers[a+1]=s.y,this.centers[a+2]=s.z}getSplat(t){if(t>=this.numSplats)throw new Error(`Splat index out of bounds: ${t} >= ${this.numSplats}`);const s=t*3,e=t*4,r=t*3,n=t*3;return{position:new o.Vector3(this.positions[s],this.positions[s+1],this.positions[s+2]),rotation:new o.Quaternion(this.rotations[e],this.rotations[e+1],this.rotations[e+2],this.rotations[e+3]),scale:new o.Vector3(Math.exp(this.scales[r]),Math.exp(this.scales[r+1]),Math.exp(this.scales[r+2])),color:new o.Color(this.colors[n],this.colors[n+1],this.colors[n+2]),opacity:this.opacities[t]}}calculateBoundingBox(){this.boundingBox.reset();const t=new o.Vector3;for(let s=0;s<this.numSplats;s++){const e=s*3;t.set(this.positions[e],this.positions[e+1],this.positions[e+2]),this.boundingBox.expandByPoint(t)}return this.boundingBox}createDebugGeometry(){const t=new o.BufferGeometry;t.setAttribute("position",new o.BufferAttribute(this.positions,3));const s=new Float32Array(this.numSplats*3),e=new o.Quaternion,r=new o.Euler;for(let n=0;n<this.numSplats;n++){const i=n*4,a=n*3;e.set(this.rotations[i],this.rotations[i+1],this.rotations[i+2],this.rotations[i+3]),r.setFromQuaternion(e),s[a]=r.x,s[a+1]=r.y,s[a+2]=r.z}return t.setAttribute("rotation",new o.BufferAttribute(s,3)),t.setAttribute("scale",new o.BufferAttribute(this.scales,3)),t.setAttribute("color",new o.BufferAttribute(this.colors,3)),t.setAttribute("opacity",new o.BufferAttribute(this.opacities,1)),t}}class at extends o.EventDispatcher{constructor(){super();c(this,"worker");c(this,"centers",null);c(this,"orderTexture",null);c(this,"chunks",null);c(this,"lastCameraPosition",new o.Vector3);c(this,"lastCameraDirection",new o.Vector3);const s=this.createWorkerCode(),e=new Blob([s],{type:"application/javascript"});this.worker=new Worker(URL.createObjectURL(e)),this.worker.onmessage=this.onWorkerMessage.bind(this)}onWorkerMessage(s){if(!this.orderTexture||!this.orderTexture.image)return console.error("SplatSorter: Order texture not initialized!");const{order:e,count:r}=s.data,n=this.orderTexture.image.data;if(!(n instanceof Uint32Array))return console.error("SplatSorter: Order texture data is not a Uint32Array!");n.set(new Uint32Array(e)),this.orderTexture.needsUpdate=!0;const i=n.buffer.slice(0),a={order:i};this.worker.postMessage(a,[i]),this.dispatchEvent({type:"updated",count:r})}init(s,e,r){if(!s||!(s.image.data instanceof Uint32Array))throw new Error("SplatSorter: Invalid orderTexture provided. Must be DataTexture with Uint32Array data.");if(!e||e.length%3!==0)throw new Error("SplatSorter: Invalid centers array provided. Length must be multiple of 3.");if(s.image.data.length<e.length/3)throw new Error("SplatSorter: orderTexture data buffer is smaller than the number of splats.");const n=e.length/3;this.orderTexture=s,this.centers=e.slice();const i=this.orderTexture.image.data;for(let g=0;g<n;g++)i[g]=g;this.orderTexture.needsUpdate=!0;const a=i.buffer.slice(0),d=this.centers.buffer.slice(0),p={order:a,centers:d},v=[a,d];if(r){this.chunks=r.slice();const g=this.chunks.buffer.slice(0);p.chunks=g,v.push(g)}this.worker.postMessage(p,v),queueMicrotask(()=>{this.dispatchEvent({type:"updated",count:n})})}setMapping(s){if(!this.centers)return console.warn("SplatSorter: Cannot set mapping before initialization.");let e;const r=[];if(!s){const d=this.centers.buffer.slice(0);return e={centers:d,mapping:null},r.push(d),this.worker.postMessage(e,r)}const n=new Float32Array(s.length*3);for(let d=0;d<s.length;d++){const p=s[d];if(p*3+2>=this.centers.length){console.warn(`SplatSorter: Mapping index ${p} out of bounds.`);continue}const v=p*3,g=d*3;n[g+0]=this.centers[v+0],n[g+1]=this.centers[v+1],n[g+2]=this.centers[v+2]}const i=n.buffer.slice(0),a=s.buffer.slice(0);e={centers:i,mapping:a},r.push(i,a),this.worker.postMessage(e,r)}setCamera(s,e){const r=this.lastCameraPosition.distanceToSquared(s)>1e-7,n=this.lastCameraDirection.dot(e)<.9999;if(!r&&!n)return;this.lastCameraPosition.copy(s),this.lastCameraDirection.copy(e);const i={cameraPosition:{x:s.x,y:s.y,z:s.z},cameraDirection:{x:e.x,y:e.y,z:e.z}};this.worker.postMessage(i)}dispose(){this.worker&&this.worker.terminate(),this.orderTexture=null,this.centers=null,this.chunks=null}createWorkerCode(){return`(${(function(){let e=null,r=null,n=null,i=null,a=null,d=null,p=!1;const v={x:0,y:0,z:0},g={x:0,y:0,z:0},f={x:0,y:0,z:0},h={x:0,y:0,z:0};let l=null,b=null;const B=32,q=new Array(B).fill(0),W=new Array(B).fill(0),R=new Array(B).fill(0),j=(D,C,F)=>{for(;D<=C;){const M=C+D>>1,m=F(M);if(m>0)D=M+1;else if(m<0)C=M-1;else return M}return~D},N=()=>{if(!e||!r||!a||!d)return;if(r.length===0){const y={order:e.buffer,count:0};self.postMessage(y,[e.buffer]),e=null;return}const D=a.x,C=a.y,F=a.z,M=d.x,m=d.y,S=d.z,U=1e-4,E=Math.abs(D-v.x)>U||Math.abs(C-v.y)>U||Math.abs(F-v.z)>U,x=Math.abs(M-g.x)>U||Math.abs(m-g.y)>U||Math.abs(S-g.z)>U;if(!p&&!E&&!x)return;p=!1,v.x=D,v.y=C,v.z=F,g.x=M,g.y=m,g.z=S;let u=1/0,z=-1/0;for(let y=0;y<8;++y){const k=y&1?f.x:h.x,O=y&2?f.y:h.y,w=y&4?f.z:h.z,V=k*M+O*m+w*S;u=Math.min(u,V),z=Math.max(z,V)}const _=r.length/3,Y=z-u,I=(1<<Math.max(10,Math.min(20,Math.ceil(Math.log2(_/4)))))+1;if((!l||l.length!==_)&&(l=new Uint32Array(_)),!b||b.length!==I?b=new Uint32Array(I):b.fill(0),Y<1e-7){for(let y=0;y<_;++y)l[y]=0;b[0]=_}else if(n&&n.length>0){const y=n.length/6;q.fill(0);for(let w=0;w<y;++w){const V=w*6,$=n[V+0],J=n[V+1],tt=n[V+2],X=n[V+3],Z=$*M+J*m+tt*S-u,et=Z-X,K=Z+X,nt=Math.max(0,Math.floor(et*B/Y)),ot=Math.min(B,Math.ceil(K*B/Y));for(let G=nt;G<ot;++G)q[G]++}let k=0;for(let w=0;w<B;++w)k+=q[w];R[0]=0,W[0]=0;for(let w=1;w<B;++w)R[w-1]=q[w-1]/k*I>>>0,W[w]=W[w-1]+R[w-1];R[B-1]=q[B-1]/k*I>>>0;const O=Y/B;for(let w=0;w<_;++w){const V=w*3,$=r[V+0],J=r[V+1],tt=r[V+2],X=$*M+J*m+tt*S,et=(z-X)/O,K=Math.max(0,Math.min(B-1,Math.floor(et))),nt=et-K,ot=W[K]+R[K]*nt>>>0,G=Math.min(ot,I-1);l[w]=G,b[G]++}}else{const y=(I-1)/Y;for(let k=0;k<_;++k){const O=k*3,w=r[O+0],V=r[O+1],$=r[O+2],J=w*M+V*m+$*S,X=(z-J)*y>>>0,Z=Math.min(X,I-1);l[k]=Z,b[Z]++}}for(let y=1;y<I;y++)b[y]+=b[y-1];for(let y=_-1;y>=0;y--){const k=l[y],O=--b[k];e[O]=i?i[y]:y}const bt=D*M+C*m+F*S,dt=y=>{if(!e)return-1/0;const k=e[y],O=k;if(!r||O*3+2>=r.length)return-1/0;const w=O*3;return r[w]*M+r[w+1]*m+r[w+2]*S-bt};let ft=_;if(_>0&&dt(_-1)<0){const y=j(0,_-1,dt);ft=y<0?~y:y}const Mt={order:e.buffer,count:ft};self.postMessage(Mt,[e.buffer]),e=null};self.onmessage=D=>{const C=D.data;C.order&&(e=new Uint32Array(C.order));let F=!1;if(C.centers&&(r=new Float32Array(C.centers),F=!0,p=!0),Object.prototype.hasOwnProperty.call(C,"mapping")&&(i=C.mapping?new Uint32Array(C.mapping):null,p=!0),C.chunks){if(n=new Float32Array(C.chunks),n.length>0&&n[3]>0)for(let M=0;M<n.length/6;++M){const m=M*6,S=n[m+0],U=n[m+1],E=n[m+2],x=n[m+3],u=n[m+4],z=n[m+5];n[m+0]=(S+x)*.5,n[m+1]=(U+u)*.5,n[m+2]=(E+z)*.5,n[m+3]=Math.sqrt((x-S)**2+(u-U)**2+(z-E)**2)*.5}p=!0}if(F&&r&&r.length>0){f.x=h.x=r[0],f.y=h.y=r[1],f.z=h.z=r[2];for(let M=1;M<r.length/3;M++){const m=M*3;f.x=Math.min(f.x,r[m+0]),h.x=Math.max(h.x,r[m+0]),f.y=Math.min(f.y,r[m+1]),h.y=Math.max(h.y,r[m+1]),f.z=Math.min(f.z,r[m+2]),h.z=Math.max(h.z,r[m+2])}}else F&&r&&r.length===0&&(f.x=h.x=f.y=h.y=f.z=h.z=0);C.cameraPosition&&(a=C.cameraPosition),C.cameraDirection&&(d=C.cameraDirection),N()}}).toString()})();`}}const pt=(T,t)=>{const s=ct(T),e=ct(t);return s|e<<16};function ct(T){const t=new Float32Array([T]),e=new Int32Array(t.buffer)[0];let r=e>>16&32768,n=e>>12&2047;const i=e>>23&255;return i<103?r:i>142?(r|=31744,r|=(i===255?0:1)&&e&8388607,r):i<113?(n|=2048,r|=(n>>114-i)+(n>>113-i&1),r):(r|=i-112<<10|n>>1,r+=n&1,r)}const mt=new ArrayBuffer(4),lt=new DataView(mt),xt=T=>(lt.setUint32(0,T,!0),lt.getFloat32(0,!0));class ht{constructor(t){c(this,"transformA");c(this,"transformB");c(this,"colorTexture");c(this,"orderTexture");c(this,"textureWidth");c(this,"textureHeight");const s=t.numSplats;this.textureWidth=Math.ceil(Math.sqrt(s)),this.textureHeight=Math.ceil(s/this.textureWidth),this.transformA=this.createTransformATexture(t),this.transformB=this.createTransformBTexture(t),this.colorTexture=this.createColorTexture(t),this.orderTexture=this.createOrderTexture(s)}createTransformATexture(t){const s=t.numSplats,e=new Float32Array(this.textureWidth*this.textureHeight*4);for(let n=0;n<s;n++){const i=n*4,a=n*3,d=n*4;e[i]=t.positions[a],e[i+1]=t.positions[a+1],e[i+2]=t.positions[a+2];const p=t.rotations[d+0],v=t.rotations[d+1],g=pt(p,v);e[i+3]=xt(g)}const r=new o.DataTexture(e,this.textureWidth,this.textureHeight,o.RGBAFormat,o.FloatType);return r.needsUpdate=!0,r.magFilter=o.NearestFilter,r.minFilter=o.NearestFilter,r}createTransformBTexture(t){const s=t.numSplats,e=new Float32Array(this.textureWidth*this.textureHeight*4);for(let n=0;n<s;n++){const i=n*4,a=n*3,d=n*4;e[i]=t.scales[a],e[i+1]=t.scales[a+1],e[i+2]=t.scales[a+2],e[i+3]=t.rotations[d+2]}const r=new o.DataTexture(e,this.textureWidth,this.textureHeight,o.RGBAFormat,o.FloatType);return r.needsUpdate=!0,r.magFilter=o.NearestFilter,r.minFilter=o.NearestFilter,r}createColorTexture(t){const s=t.numSplats,e=new Float32Array(this.textureWidth*this.textureHeight*4);for(let n=0;n<s;n++){const i=n*4,a=n*3;e[i]=t.colors[a],e[i+1]=t.colors[a+1],e[i+2]=t.colors[a+2],e[i+3]=t.opacities[n]}const r=new o.DataTexture(e,this.textureWidth,this.textureHeight,o.RGBAFormat,o.FloatType);return r.needsUpdate=!0,r}createOrderTexture(t){const s=new Uint32Array(this.textureWidth*this.textureHeight);for(let r=0;r<t;r++)s[r]=r;const e=new o.DataTexture(s,this.textureWidth,this.textureHeight,o.RedIntegerFormat,o.UnsignedIntType);return e.needsUpdate=!0,e}dispose(){this.transformA.dispose(),this.transformB.dispose(),this.colorTexture.dispose(),this.orderTexture.dispose()}}const yt=`
|
|
3
3
|
precision highp float;
|
|
4
4
|
precision highp int;
|
|
5
5
|
precision highp usampler2D;
|
|
@@ -254,7 +254,7 @@ void main(void) {
|
|
|
254
254
|
vColor = vec4(splatColor, splatOpacity);
|
|
255
255
|
vUv = clippedCornerOffset;
|
|
256
256
|
}
|
|
257
|
-
`,
|
|
257
|
+
`,gt=`
|
|
258
258
|
precision highp float;
|
|
259
259
|
|
|
260
260
|
varying vec4 vColor; // Color and opacity passed from vertex shader
|
|
@@ -284,7 +284,7 @@ void main(void) {
|
|
|
284
284
|
// Premultiply color by alpha (required for correct blending)
|
|
285
285
|
gl_FragColor = vec4(vColor.rgb * alpha, alpha);
|
|
286
286
|
}
|
|
287
|
-
`;class
|
|
287
|
+
`;class ut extends o.ShaderMaterial{constructor(t={}){const s={transformA:{value:null},transformB:{value:null},splatColor:{value:null},splatOrder:{value:null},viewport:{value:new o.Vector2(1,1)},numSplats:{value:0}};super({vertexShader:yt,fragmentShader:gt,uniforms:s,transparent:!0,blending:o.CustomBlending,blendSrc:o.OneFactor,blendDst:o.OneMinusSrcAlphaFactor,blendSrcAlpha:o.OneFactor,blendDstAlpha:o.OneMinusSrcAlphaFactor,blendEquation:o.AddEquation,blendEquationAlpha:o.AddEquation,depthTest:!0,depthWrite:!1,side:o.DoubleSide,alphaTest:t.alphaTest!==void 0?t.alphaTest:0,toneMapped:t.toneMapped!==void 0?t.toneMapped:!1}),t.alphaHash&&(this.alphaHash=!0,this.depthWrite=!0,this.blending=o.NoBlending)}updateViewport(t,s){this.uniforms.viewport.value.set(t,s)}setTransformA(t){this.uniforms.transformA.value=t}setTransformB(t){this.uniforms.transformB.value=t}setColorTexture(t){this.uniforms.splatColor.value=t}setOrderTexture(t){this.uniforms.splatOrder.value=t}setNumSplats(t){this.uniforms.numSplats.value=t}}const H=class H extends o.Mesh{constructor(s,e={}){const r=new ut(e),n=H.createInstancedGeometry(s.numSplats,H.INSTANCE_SIZE);super(n,r);c(this,"sorter");c(this,"splatData");c(this,"options");c(this,"textureManager");c(this,"material");c(this,"geometry");c(this,"lastCameraPositionLocal",new o.Vector3);c(this,"lastCameraDirectionLocal",new o.Vector3);c(this,"invModelMatrix",new o.Matrix4);c(this,"_vpW",-1);c(this,"_vpH",-1);c(this,"_size",new o.Vector2);c(this,"_camPosW",new o.Vector3);c(this,"_camDirW",new o.Vector3);c(this,"_camPosL",new o.Vector3);c(this,"_camDirL",new o.Vector3);c(this,"onSorterUpdated",s=>{const e=s.count;this.geometry.instanceCount=Math.ceil(e/H.INSTANCE_SIZE),this.material.setNumSplats(e)});this.geometry=n,this.material=r,this.splatData=s,this.frustumCulled=!1,this.options={autoSort:!0,...e},this.textureManager=new ht(s),this.sorter=new at;let i=this.createChunks()||void 0;i===null&&console.warn("Visus: Could not create sorter chunks, bounding box might be invalid."),i=void 0,this.sorter.addEventListener("updated",this.onSorterUpdated),this.sorter.init(this.textureManager.orderTexture,this.splatData.centers,i??void 0),this.material.setTransformA(this.textureManager.transformA),this.material.setTransformB(this.textureManager.transformB),this.material.setColorTexture(this.textureManager.colorTexture),this.material.setOrderTexture(this.textureManager.orderTexture),this.material.setNumSplats(0),this.geometry.boundingBox=s.boundingBox.toBox3(),this.geometry.boundingSphere=new o.Sphere,this.geometry.boundingBox.getBoundingSphere(this.geometry.boundingSphere)}static createInstancedGeometry(s,e){const r=Math.ceil(s/e),n=new o.BufferGeometry,i=new Float32Array([-1,-1,0,1,-1,0,1,1,0,-1,1,0]),a=new Uint16Array([0,1,2,0,2,3]),d=new Float32Array(4*3*e);for(let f=0;f<e;f++){const h=f*4*3;for(let l=0;l<4;l++)d[h+l*3+0]=i[l*3+0],d[h+l*3+1]=i[l*3+1],d[h+l*3+2]=f}const p=new Uint32Array(6*e);for(let f=0;f<e;f++){const h=f*6,l=f*4;p[h+0]=a[0]+l,p[h+1]=a[1]+l,p[h+2]=a[2]+l,p[h+3]=a[3]+l,p[h+4]=a[4]+l,p[h+5]=a[5]+l}n.setAttribute("position",new o.BufferAttribute(d,3)),n.setIndex(new o.BufferAttribute(p,1));const v=new o.InstancedBufferGeometry;v.index=n.index,v.setAttribute("position",n.getAttribute("position"));const g=new Uint32Array(r);for(let f=0;f<r;f++)g[f]=f*e;return v.setAttribute("splatInstanceIndex",new o.InstancedBufferAttribute(g,1,!1)),v.instanceCount=0,v}createChunks(){const s=this.splatData.boundingBox;return s.min.x===1/0||s.max.x===-1/0?null:new Float32Array([s.min.x,s.min.y,s.min.z,s.max.x,s.max.y,s.max.z])}updateViewport(s,e){s===this._vpW&&e===this._vpH||(this._vpW=s,this._vpH=e,this.material.updateViewport(s,e))}sort(s){s.getWorldPosition(this._camPosW),s.getWorldDirection(this._camDirW),this.invModelMatrix.copy(this.matrixWorld).invert(),this._camPosL.copy(this._camPosW).applyMatrix4(this.invModelMatrix),this._camDirL.copy(this._camDirW).transformDirection(this.invModelMatrix);const e=this.lastCameraPositionLocal.distanceToSquared(this._camPosL)>1e-6,r=this.lastCameraDirectionLocal.dot(this._camDirL)<.999;this.options.autoSort&&(e||r)&&(this.lastCameraPositionLocal.copy(this._camPosL),this.lastCameraDirectionLocal.copy(this._camDirL),this.sorter.setCamera(this._camPosL,this._camDirL))}onBeforeRender(s,e,r){this.sort(r),s.getSize(this._size),this.updateViewport(this._size.x,this._size.y)}dispose(){this.sorter.removeEventListener("updated",this.onSorterUpdated),this.sorter.dispose(),this.geometry.dispose(),this.material.dispose(),this.textureManager.dispose()}};c(H,"INSTANCE_SIZE",128);let rt=H;class vt extends o.Loader{load(t,s,e,r){const n=new o.FileLoader(this.manager);n.setResponseType("arraybuffer"),n.setRequestHeader(this.requestHeader),n.setPath(this.path),n.setWithCredentials(this.withCredentials),n.load(t,i=>{try{if(s){const a=this.parse(i);s(a)}}catch(a){r?r(a):console.error(a),this.manager.itemError(t)}},e,r)}loadAsync(t,s){return new Promise((e,r)=>{const n=new o.FileLoader(this.manager);n.setResponseType("arraybuffer"),n.setRequestHeader(this.requestHeader),n.setPath(this.path),n.setWithCredentials(this.withCredentials),n.load(t,i=>{try{const a=this.parse(i);e(a)}catch(a){r(a),this.manager.itemError(t)}},s,i=>{r(i),this.manager.itemError(t)})})}parse(t){const s=new TextDecoder,e=new Uint8Array(t),r=[112,108,121,10],n=`
|
|
288
288
|
end_header
|
|
289
|
-
`;for(let
|
|
290
|
-
`),
|
|
289
|
+
`;for(let x=0;x<r.length;x++)if(e[x]!==r[x])throw new Error("Invalid PLY file: Missing magic bytes");let i=0;for(let x=0;x<e.length-n.length;x++){let u=!0;for(let z=0;z<n.length;z++)if(e[x+z]!==n.charCodeAt(z)){u=!1;break}if(u){i=x+n.length;break}}if(i===0)throw new Error("Invalid PLY file: Could not find end of header");const d=s.decode(e.subarray(0,i)).split(`
|
|
290
|
+
`),p=[];let v=null;for(let x=1;x<d.length;x++){const u=d[x].trim();if(u===""||u==="end_header")continue;const z=u.split(" ");switch(z[0]){case"format":v=z[1];break;case"element":p.push({name:z[1],count:parseInt(z[2],10),properties:[]});break;case"property":if(p.length===0)throw new Error("Invalid PLY file: Property without element");p[p.length-1].properties.push({type:z[1],name:z[2],storage:null});break}}if(v!=="binary_little_endian")throw new Error(`Unsupported PLY format: ${v}`);const g=p.find(x=>x.name==="vertex");if(!g)throw new Error("Invalid PLY file: No vertex element found");const f=new it(g.count),h=new DataView(t);let l=i;const b=x=>g.properties.findIndex(u=>u.name===x),B=b("x"),q=b("y"),W=b("z"),R=[b("rot_0"),b("rot_1"),b("rot_2"),b("rot_3")],j=[b("scale_0"),b("scale_1"),b("scale_2")],N=[b("f_dc_0"),b("f_dc_1"),b("f_dc_2")],D=b("opacity");if([B,q,W,...R,...j,...N,D].some(x=>x===-1))throw new Error("Invalid PLY file: Missing required properties");const F=.28209479177387814,M=x=>{if(x>0)return 1/(1+Math.exp(-x));const u=Math.exp(x);return u/(1+u)},m=new o.Vector3,S=new o.Quaternion,U=new o.Vector3,E=new o.Color;for(let x=0;x<g.count;x++){const u=[];for(let _=0;_<g.properties.length;_++){const st=g.properties[_].type;let I;switch(st){case"char":I=h.getInt8(l),l+=1;break;case"uchar":I=h.getUint8(l),l+=1;break;case"short":I=h.getInt16(l,!0),l+=2;break;case"ushort":I=h.getUint16(l,!0),l+=2;break;case"int":I=h.getInt32(l,!0),l+=4;break;case"uint":I=h.getUint32(l,!0),l+=4;break;case"float":I=h.getFloat32(l,!0),l+=4;break;case"double":I=h.getFloat64(l,!0),l+=8;break;default:throw new Error(`Unsupported property type: ${st}`)}u.push(I)}m.set(u[B],u[q],u[W]),S.set(u[R[1]],u[R[2]],u[R[3]],u[R[0]]).normalize(),S.w<0&&(S.x=-S.x,S.y=-S.y,S.z=-S.z,S.w=-S.w),U.set(u[j[0]],u[j[1]],u[j[2]]),E.set(.5+u[N[0]]*F,.5+u[N[1]]*F,.5+u[N[2]]*F),E.r=Math.max(0,Math.min(1,E.r)),E.g=Math.max(0,Math.min(1,E.g)),E.b=Math.max(0,Math.min(1,E.b));const z=M(u[D]);f.setSplat(x,m,S,U,E,z)}return f.calculateBoundingBox(),f}}const wt="0.3.0";A.BoundingBox=Q,A.PlyLoader=vt,A.SplatData=it,A.SplatMaterial=ut,A.SplatMesh=rt,A.SplatSorter=at,A.TextureManager=ht,A.VERSION=wt,Object.defineProperty(A,Symbol.toStringTag,{value:"Module"})});
|