UNPKG

@babylonjs/viewer

Version:

The Babylon Viewer aims to simplify a specific but common Babylon.js use case: loading, viewing, and interacting with a 3D model.

3 lines 64.5 kB
import{S as e,H as t,aF as s,aG as n,aH as r,aI as i,m as o,aJ as a,A as c,aK as l,r as u,W as h,N as _,X as d,y as p,R as f,aL as m,aM as x,g as v,M as S,V as y,f as g,aN as C,aO as w,L as T,G as E,aP as A,aQ as P,b as z,e as b,C as D,aR as H,aC as M,j as I,Q as R,aB as U,i as F,aS as V,B,aT as L,aA as k}from"./index-yNtOWHHe.esm.min.js";import"./logDepthDeclaration-Y3dkPPwd.esm.min.js";import"./fogFragment-rzKA2Lan.esm.min.js";import"./sceneUboDeclaration-DxcaNt9h.esm.min.js";import"./meshUboDeclaration-BGCOdaY7.esm.min.js";import"./logDepthVertex-CX8EMs5c.esm.min.js";import"./helperFunctions-9xvgSBwn.esm.min.js";import"./clipPlaneFragment-C7FZ856n.esm.min.js";import"./logDepthDeclaration-BV4L8eT_.esm.min.js";import"./fogFragment-C6r2MTD6.esm.min.js";import"./sceneUboDeclaration-CK_BUki9.esm.min.js";import"./meshUboDeclaration-B1_1y_od.esm.min.js";import"./helperFunctions-gnVTkT4Y.esm.min.js";import"./clipPlaneVertex-DIlDDAxK.esm.min.js";import"./logDepthVertex-CaGrjmVd.esm.min.js";import{R as O}from"./rawTexture-BOCcQuA1.esm.min.js";import"./thinInstanceMesh-S2BjT3Bu.esm.min.js";import{A as N}from"./assetContainer-Dkk4zkgP.esm.min.js";import{Ray as G}from"./ray-CFND3lN5.esm.min.js";import{S as W}from"./standardMaterial-DbW5onZc.esm.min.js";const X="gaussianSplattingFragmentDeclaration";e.IncludesShadersStore[X]||(e.IncludesShadersStore[X]="vec4 gaussianColor(vec4 inColor)\n{float A=-dot(vPosition,vPosition);if (A<-4.0) discard;float B=exp(A)*inColor.a;\n#include<logDepthFragment>\nvec3 color=inColor.rgb;\n#ifdef FOG\n#include<fogFragment>\n#endif\nreturn vec4(color,B);}\n");const Z="gaussianSplattingPixelShader",j="#include<clipPlaneFragmentDeclaration>\n#include<logDepthDeclaration>\n#include<fogFragmentDeclaration>\nvarying vec4 vColor;varying vec2 vPosition;\n#include<gaussianSplattingFragmentDeclaration>\nvoid main () { \n#include<clipPlaneFragment>\ngl_FragColor=gaussianColor(vColor);}\n";e.ShadersStore[Z]||(e.ShadersStore[Z]=j);const Y={name:Z,shader:j};var K=Object.freeze({__proto__:null,gaussianSplattingPixelShader:Y});const q="gaussianSplattingVertexDeclaration";e.IncludesShadersStore[q]||(e.IncludesShadersStore[q]="attribute vec2 position;uniform mat4 view;uniform mat4 projection;uniform mat4 world;uniform vec4 vEyePosition;");const Q="gaussianSplattingUboDeclaration";e.IncludesShadersStore[Q]||(e.IncludesShadersStore[Q]="#include<sceneUboDeclaration>\n#include<meshUboDeclaration>\nattribute vec2 position;");const J="gaussianSplatting";e.IncludesShadersStore[J]||(e.IncludesShadersStore[J]="#if !defined(WEBGL2) && !defined(WEBGPU) && !defined(NATIVE)\nmat3 transpose(mat3 matrix) {return mat3(matrix[0][0],matrix[1][0],matrix[2][0],\nmatrix[0][1],matrix[1][1],matrix[2][1],\nmatrix[0][2],matrix[1][2],matrix[2][2]);}\n#endif\nvec2 getDataUV(float index,vec2 textureSize) {float y=floor(index/textureSize.x);float x=index-y*textureSize.x;return vec2((x+0.5)/textureSize.x,(y+0.5)/textureSize.y);}\n#if SH_DEGREE>0\nivec2 getDataUVint(float index,vec2 textureSize) {float y=floor(index/textureSize.x);float x=index-y*textureSize.x;return ivec2(uint(x+0.5),uint(y+0.5));}\n#endif\nstruct Splat {vec4 center;vec4 color;vec4 covA;vec4 covB;\n#if SH_DEGREE>0\nuvec4 sh0; \n#endif\n#if SH_DEGREE>1\nuvec4 sh1;\n#endif\n#if SH_DEGREE>2\nuvec4 sh2;\n#endif\n};Splat readSplat(float splatIndex)\n{Splat splat;vec2 splatUV=getDataUV(splatIndex,dataTextureSize);splat.center=texture2D(centersTexture,splatUV);splat.color=texture2D(colorsTexture,splatUV);splat.covA=texture2D(covariancesATexture,splatUV)*splat.center.w;splat.covB=texture2D(covariancesBTexture,splatUV)*splat.center.w;\n#if SH_DEGREE>0\nivec2 splatUVint=getDataUVint(splatIndex,dataTextureSize);splat.sh0=texelFetch(shTexture0,splatUVint,0);\n#endif\n#if SH_DEGREE>1\nsplat.sh1=texelFetch(shTexture1,splatUVint,0);\n#endif\n#if SH_DEGREE>2\nsplat.sh2=texelFetch(shTexture2,splatUVint,0);\n#endif\nreturn splat;}\n#if defined(WEBGL2) || defined(WEBGPU) || defined(NATIVE)\nvec3 computeColorFromSHDegree(vec3 dir,const vec3 sh[16])\n{const float SH_C0=0.28209479;const float SH_C1=0.48860251;float SH_C2[5];SH_C2[0]=1.092548430;SH_C2[1]=-1.09254843;SH_C2[2]=0.315391565;SH_C2[3]=-1.09254843;SH_C2[4]=0.546274215;float SH_C3[7];SH_C3[0]=-0.59004358;SH_C3[1]=2.890611442;SH_C3[2]=-0.45704579;SH_C3[3]=0.373176332;SH_C3[4]=-0.45704579;SH_C3[5]=1.445305721;SH_C3[6]=-0.59004358;vec3 result=/*SH_C0**/sh[0];\n#if SH_DEGREE>0\nfloat x=dir.x;float y=dir.y;float z=dir.z;result+=- SH_C1*y*sh[1]+SH_C1*z*sh[2]-SH_C1*x*sh[3];\n#if SH_DEGREE>1\nfloat xx=x*x,yy=y*y,zz=z*z;float xy=x*y,yz=y*z,xz=x*z;result+=\nSH_C2[0]*xy*sh[4] +\nSH_C2[1]*yz*sh[5] +\nSH_C2[2]*(2.0*zz-xx-yy)*sh[6] +\nSH_C2[3]*xz*sh[7] +\nSH_C2[4]*(xx-yy)*sh[8];\n#if SH_DEGREE>2\nresult+=\nSH_C3[0]*y*(3.0*xx-yy)*sh[9] +\nSH_C3[1]*xy*z*sh[10] +\nSH_C3[2]*y*(4.0*zz-xx-yy)*sh[11] +\nSH_C3[3]*z*(2.0*zz-3.0*xx-3.0*yy)*sh[12] +\nSH_C3[4]*x*(4.0*zz-xx-yy)*sh[13] +\nSH_C3[5]*z*(xx-yy)*sh[14] +\nSH_C3[6]*x*(xx-3.0*yy)*sh[15];\n#endif\n#endif\n#endif\nreturn result;}\nvec4 decompose(uint value)\n{vec4 components=vec4(\nfloat((value ) & 255u),\nfloat((value>>uint( 8)) & 255u),\nfloat((value>>uint(16)) & 255u),\nfloat((value>>uint(24)) & 255u));return components*vec4(2./255.)-vec4(1.);}\nvec3 computeSH(Splat splat,vec3 dir)\n{vec3 sh[16];sh[0]=vec3(0.,0.,0.);\n#if SH_DEGREE>0\nvec4 sh00=decompose(splat.sh0.x);vec4 sh01=decompose(splat.sh0.y);vec4 sh02=decompose(splat.sh0.z);sh[1]=vec3(sh00.x,sh00.y,sh00.z);sh[2]=vec3(sh00.w,sh01.x,sh01.y);sh[3]=vec3(sh01.z,sh01.w,sh02.x);\n#endif\n#if SH_DEGREE>1\nvec4 sh03=decompose(splat.sh0.w);vec4 sh04=decompose(splat.sh1.x);vec4 sh05=decompose(splat.sh1.y);sh[4]=vec3(sh02.y,sh02.z,sh02.w);sh[5]=vec3(sh03.x,sh03.y,sh03.z);sh[6]=vec3(sh03.w,sh04.x,sh04.y);sh[7]=vec3(sh04.z,sh04.w,sh05.x);sh[8]=vec3(sh05.y,sh05.z,sh05.w);\n#endif\n#if SH_DEGREE>2\nvec4 sh06=decompose(splat.sh1.z);vec4 sh07=decompose(splat.sh1.w);vec4 sh08=decompose(splat.sh2.x);vec4 sh09=decompose(splat.sh2.y);vec4 sh10=decompose(splat.sh2.z);vec4 sh11=decompose(splat.sh2.w);sh[9]=vec3(sh06.x,sh06.y,sh06.z);sh[10]=vec3(sh06.w,sh07.x,sh07.y);sh[11]=vec3(sh07.z,sh07.w,sh08.x);sh[12]=vec3(sh08.y,sh08.z,sh08.w);sh[13]=vec3(sh09.x,sh09.y,sh09.z);sh[14]=vec3(sh09.w,sh10.x,sh10.y);sh[15]=vec3(sh10.z,sh10.w,sh11.x); \n#endif\nreturn computeColorFromSHDegree(dir,sh);}\n#else\nvec3 computeSH(Splat splat,vec3 dir)\n{return vec3(0.,0.,0.);}\n#endif\nvec4 gaussianSplatting(vec2 meshPos,vec3 worldPos,vec2 scale,vec3 covA,vec3 covB,mat4 worldMatrix,mat4 viewMatrix,mat4 projectionMatrix)\n{mat4 modelView=viewMatrix*worldMatrix;vec4 camspace=viewMatrix*vec4(worldPos,1.);vec4 pos2d=projectionMatrix*camspace;float bounds=1.2*pos2d.w;if (pos2d.z<-pos2d.w || pos2d.x<-bounds || pos2d.x>bounds\n|| pos2d.y<-bounds || pos2d.y>bounds) {return vec4(0.0,0.0,2.0,1.0);}\nmat3 Vrk=mat3(\ncovA.x,covA.y,covA.z,\ncovA.y,covB.x,covB.y,\ncovA.z,covB.y,covB.z\n);mat3 J=mat3(\nfocal.x/camspace.z,0.,-(focal.x*camspace.x)/(camspace.z*camspace.z),\n0.,focal.y/camspace.z,-(focal.y*camspace.y)/(camspace.z*camspace.z),\n0.,0.,0.\n);mat3 invy=mat3(1,0,0,0,-1,0,0,0,1);mat3 T=invy*transpose(mat3(modelView))*J;mat3 cov2d=transpose(T)*Vrk*T;\n#if COMPENSATION\nfloat c00=cov2d[0][0];float c11=cov2d[1][1];float c01=cov2d[0][1];float detOrig=c00*c11-c01*c01;\n#endif\ncov2d[0][0]+=kernelSize;cov2d[1][1]+=kernelSize;\n#if COMPENSATION\nvec3 c2d=vec3(cov2d[0][0],c01,cov2d[1][1]);float detBlur=c2d.x*c2d.z-c2d.y*c2d.y;float compensation=sqrt(max(0.,detOrig/detBlur));vColor.w*=compensation;\n#endif\nfloat mid=(cov2d[0][0]+cov2d[1][1])/2.0;float radius=length(vec2((cov2d[0][0]-cov2d[1][1])/2.0,cov2d[0][1]));float epsilon=0.0001;float lambda1=mid+radius+epsilon,lambda2=mid-radius+epsilon;if (lambda2<0.0)\n{return vec4(0.0,0.0,2.0,1.0);}\nvec2 diagonalVector=normalize(vec2(cov2d[0][1],lambda1-cov2d[0][0]));vec2 majorAxis=min(sqrt(2.0*lambda1),1024.0)*diagonalVector;vec2 minorAxis=min(sqrt(2.0*lambda2),1024.0)*vec2(diagonalVector.y,-diagonalVector.x);vec2 vCenter=vec2(pos2d);return vec4(\nvCenter \n+ ((meshPos.x*majorAxis\n+ meshPos.y*minorAxis)*invViewport*pos2d.w)*scale,pos2d.zw);}");const $="gaussianSplattingVertexShader",ee="#include<__decl__gaussianSplattingVertex>\n#ifdef LOGARITHMICDEPTH\n#extension GL_EXT_frag_depth : enable\n#endif\n#include<clipPlaneVertexDeclaration>\n#include<fogVertexDeclaration>\n#include<logDepthDeclaration>\n#include<helperFunctions>\nattribute float splatIndex;uniform vec2 invViewport;uniform vec2 dataTextureSize;uniform vec2 focal;uniform float kernelSize;uniform vec3 eyePosition;uniform vec3 viewDirectionFactor;uniform sampler2D covariancesATexture;uniform sampler2D covariancesBTexture;uniform sampler2D centersTexture;uniform sampler2D colorsTexture;\n#if SH_DEGREE>0\nuniform highp usampler2D shTexture0;\n#endif\n#if SH_DEGREE>1\nuniform highp usampler2D shTexture1;\n#endif\n#if SH_DEGREE>2\nuniform highp usampler2D shTexture2;\n#endif\nvarying vec4 vColor;varying vec2 vPosition;\n#include<gaussianSplatting>\nvoid main () {Splat splat=readSplat(splatIndex);vec3 covA=splat.covA.xyz;vec3 covB=vec3(splat.covA.w,splat.covB.xy);vec4 worldPos=world*vec4(splat.center.xyz,1.0);vColor=splat.color;vPosition=position;\n#if SH_DEGREE>0\nmat3 worldRot=mat3(world);mat3 normWorldRot=inverseMat3(worldRot);vec3 dir=normalize(normWorldRot*(worldPos.xyz-eyePosition));dir*=viewDirectionFactor;vColor.xyz=splat.color.xyz+computeSH(splat,dir);\n#endif\ngl_Position=gaussianSplatting(position,worldPos.xyz,vec2(1.,1.),covA,covB,world,view,projection);\n#include<clipPlaneVertex>\n#include<fogVertex>\n#include<logDepthVertex>\n}\n";e.ShadersStore[$]||(e.ShadersStore[$]=ee);const te={name:$,shader:ee};var se=Object.freeze({__proto__:null,gaussianSplattingVertexShader:te});const ne="gaussianSplattingFragmentDeclaration";e.IncludesShadersStoreWGSL[ne]||(e.IncludesShadersStoreWGSL[ne]="fn gaussianColor(inColor: vec4f,inPosition: vec2f)->vec4f\n{var A : f32=-dot(inPosition,inPosition);if (A>-4.0)\n{var B: f32=exp(A)*inColor.a;\n#include<logDepthFragment>\nvar color: vec3f=inColor.rgb;\n#ifdef FOG\n#include<fogFragment>\n#endif\nreturn vec4f(color,B);} else {return vec4f(0.0);}}\n");const re="gaussianSplattingPixelShader",ie="#include<clipPlaneFragmentDeclaration>\n#include<logDepthDeclaration>\n#include<fogFragmentDeclaration>\nvarying vColor: vec4f;varying vPosition: vec2f;\n#include<gaussianSplattingFragmentDeclaration>\n@fragment\nfn main(input: FragmentInputs)->FragmentOutputs {\n#include<clipPlaneFragment>\nfragmentOutputs.color=gaussianColor(input.vColor,input.vPosition);}\n";e.ShadersStoreWGSL[re]||(e.ShadersStoreWGSL[re]=ie);const oe={name:re,shader:ie};var ae=Object.freeze({__proto__:null,gaussianSplattingPixelShaderWGSL:oe});const ce="gaussianSplatting";e.IncludesShadersStoreWGSL[ce]||(e.IncludesShadersStoreWGSL[ce]="fn getDataUV(index: f32,dataTextureSize: vec2f)->vec2<f32> {let y: f32=floor(index/dataTextureSize.x);let x: f32=index-y*dataTextureSize.x;return vec2f((x+0.5),(y+0.5));}\nstruct Splat {center: vec4f,\ncolor: vec4f,\ncovA: vec4f,\ncovB: vec4f,\n#if SH_DEGREE>0\nsh0: vec4<u32>,\n#endif\n#if SH_DEGREE>1\nsh1: vec4<u32>,\n#endif\n#if SH_DEGREE>2\nsh2: vec4<u32>,\n#endif\n};fn readSplat(splatIndex: f32,dataTextureSize: vec2f)->Splat {var splat: Splat;let splatUV=getDataUV(splatIndex,dataTextureSize);let splatUVi32=vec2<i32>(i32(splatUV.x),i32(splatUV.y));splat.center=textureLoad(centersTexture,splatUVi32,0);splat.color=textureLoad(colorsTexture,splatUVi32,0);splat.covA=textureLoad(covariancesATexture,splatUVi32,0)*splat.center.w;splat.covB=textureLoad(covariancesBTexture,splatUVi32,0)*splat.center.w;\n#if SH_DEGREE>0\nsplat.sh0=textureLoad(shTexture0,splatUVi32,0);\n#endif\n#if SH_DEGREE>1\nsplat.sh1=textureLoad(shTexture1,splatUVi32,0);\n#endif\n#if SH_DEGREE>2\nsplat.sh2=textureLoad(shTexture2,splatUVi32,0);\n#endif\nreturn splat;}\nfn computeColorFromSHDegree(dir: vec3f,sh: array<vec3<f32>,16>)->vec3f\n{let SH_C0: f32=0.28209479;let SH_C1: f32=0.48860251;var SH_C2: array<f32,5>=array<f32,5>(\n1.092548430,\n-1.09254843,\n0.315391565,\n-1.09254843,\n0.546274215\n);var SH_C3: array<f32,7>=array<f32,7>(\n-0.59004358,\n2.890611442,\n-0.45704579,\n0.373176332,\n-0.45704579,\n1.445305721,\n-0.59004358\n);var result: vec3f=/*SH_C0**/sh[0];\n#if SH_DEGREE>0\nlet x: f32=dir.x;let y: f32=dir.y;let z: f32=dir.z;result+=-SH_C1*y*sh[1]+SH_C1*z*sh[2]-SH_C1*x*sh[3];\n#if SH_DEGREE>1\nlet xx: f32=x*x;let yy: f32=y*y;let zz: f32=z*z;let xy: f32=x*y;let yz: f32=y*z;let xz: f32=x*z;result+=\nSH_C2[0]*xy*sh[4] +\nSH_C2[1]*yz*sh[5] +\nSH_C2[2]*(2.0f*zz-xx-yy)*sh[6] +\nSH_C2[3]*xz*sh[7] +\nSH_C2[4]*(xx-yy)*sh[8];\n#if SH_DEGREE>2\nresult+=\nSH_C3[0]*y*(3.0f*xx-yy)*sh[9] +\nSH_C3[1]*xy*z*sh[10] +\nSH_C3[2]*y*(4.0f*zz-xx-yy)*sh[11] +\nSH_C3[3]*z*(2.0f*zz-3.0f*xx-3.0f*yy)*sh[12] +\nSH_C3[4]*x*(4.0f*zz-xx-yy)*sh[13] +\nSH_C3[5]*z*(xx-yy)*sh[14] +\nSH_C3[6]*x*(xx-3.0f*yy)*sh[15];\n#endif\n#endif\n#endif\nreturn result;}\nfn decompose(value: u32)->vec4f\n{let components : vec4f=vec4f(\nf32((value ) & 255u),\nf32((value>>u32( 8)) & 255u),\nf32((value>>u32(16)) & 255u),\nf32((value>>u32(24)) & 255u));return components*vec4f(2./255.)-vec4f(1.);}\nfn computeSH(splat: Splat,dir: vec3f)->vec3f\n{var sh: array<vec3<f32>,16>;sh[0]=vec3f(0.,0.,0.);\n#if SH_DEGREE>0\nlet sh00: vec4f=decompose(splat.sh0.x);let sh01: vec4f=decompose(splat.sh0.y);let sh02: vec4f=decompose(splat.sh0.z);sh[1]=vec3f(sh00.x,sh00.y,sh00.z);sh[2]=vec3f(sh00.w,sh01.x,sh01.y);sh[3]=vec3f(sh01.z,sh01.w,sh02.x);\n#endif\n#if SH_DEGREE>1\nlet sh03: vec4f=decompose(splat.sh0.w);let sh04: vec4f=decompose(splat.sh1.x);let sh05: vec4f=decompose(splat.sh1.y);sh[4]=vec3f(sh02.y,sh02.z,sh02.w);sh[5]=vec3f(sh03.x,sh03.y,sh03.z);sh[6]=vec3f(sh03.w,sh04.x,sh04.y);sh[7]=vec3f(sh04.z,sh04.w,sh05.x);sh[8]=vec3f(sh05.y,sh05.z,sh05.w);\n#endif\n#if SH_DEGREE>2\nlet sh06: vec4f=decompose(splat.sh1.z);let sh07: vec4f=decompose(splat.sh1.w);let sh08: vec4f=decompose(splat.sh2.x);let sh09: vec4f=decompose(splat.sh2.y);let sh10: vec4f=decompose(splat.sh2.z);let sh11: vec4f=decompose(splat.sh2.w);sh[9]=vec3f(sh06.x,sh06.y,sh06.z);sh[10]=vec3f(sh06.w,sh07.x,sh07.y);sh[11]=vec3f(sh07.z,sh07.w,sh08.x);sh[12]=vec3f(sh08.y,sh08.z,sh08.w);sh[13]=vec3f(sh09.x,sh09.y,sh09.z);sh[14]=vec3f(sh09.w,sh10.x,sh10.y);sh[15]=vec3f(sh10.z,sh10.w,sh11.x); \n#endif\nreturn computeColorFromSHDegree(dir,sh);}\nfn gaussianSplatting(\nmeshPos: vec2<f32>,\nworldPos: vec3<f32>,\nscale: vec2<f32>,\ncovA: vec3<f32>,\ncovB: vec3<f32>,\nworldMatrix: mat4x4<f32>,\nviewMatrix: mat4x4<f32>,\nprojectionMatrix: mat4x4<f32>,\nfocal: vec2f,\ninvViewport: vec2f,\nkernelSize: f32\n)->vec4f {let modelView=viewMatrix*worldMatrix;let camspace=viewMatrix*vec4f(worldPos,1.0);let pos2d=projectionMatrix*camspace;let bounds=1.2*pos2d.w;if (pos2d.z<0. || pos2d.x<-bounds || pos2d.x>bounds || pos2d.y<-bounds || pos2d.y>bounds) {return vec4f(0.0,0.0,2.0,1.0);}\nlet Vrk=mat3x3<f32>(\ncovA.x,covA.y,covA.z,\ncovA.y,covB.x,covB.y,\ncovA.z,covB.y,covB.z\n);let J=mat3x3<f32>(\nfocal.x/camspace.z,0.0,-(focal.x*camspace.x)/(camspace.z*camspace.z),\n0.0,focal.y/camspace.z,-(focal.y*camspace.y)/(camspace.z*camspace.z),\n0.0,0.0,0.0\n);let invy=mat3x3<f32>(\n1.0,0.0,0.0,\n0.0,-1.0,0.0,\n0.0,0.0,1.0\n);let T=invy*transpose(mat3x3<f32>(\nmodelView[0].xyz,\nmodelView[1].xyz,\nmodelView[2].xyz))*J;var cov2d=transpose(T)*Vrk*T;\n#if COMPENSATION\nlet c00: f32=cov2d[0][0];let c11: f32=cov2d[1][1];let c01: f32=cov2d[0][1];let detOrig: f32=c00*c11-c01*c01;\n#endif\ncov2d[0][0]+=kernelSize;cov2d[1][1]+=kernelSize;\n#if COMPENSATION\nlet c2d: vec3f=vec3f(cov2d[0][0],c01,cov2d[1][1]);let detBlur: f32=c2d.x*c2d.z-c2d.y*c2d.y;let compensation: f32=sqrt(max(0.,detOrig/detBlur));vertexOutputs.vColor.w*=compensation;\n#endif\nlet mid=(cov2d[0][0]+cov2d[1][1])/2.0;let radius=length(vec2<f32>((cov2d[0][0]-cov2d[1][1])/2.0,cov2d[0][1]));let lambda1=mid+radius;let lambda2=mid-radius;if (lambda2<0.0) {return vec4f(0.0,0.0,2.0,1.0);}\nlet diagonalVector=normalize(vec2<f32>(cov2d[0][1],lambda1-cov2d[0][0]));let majorAxis=min(sqrt(2.0*lambda1),1024.0)*diagonalVector;let minorAxis=min(sqrt(2.0*lambda2),1024.0)*vec2<f32>(diagonalVector.y,-diagonalVector.x);let vCenter=vec2<f32>(pos2d.x,pos2d.y);return vec4f(\nvCenter+((meshPos.x*majorAxis+meshPos.y*minorAxis)*invViewport*pos2d.w)*scale,\npos2d.z,\npos2d.w\n);}\n");const le="gaussianSplattingVertexShader",ue="#include<sceneUboDeclaration>\n#include<meshUboDeclaration>\n#include<helperFunctions>\n#include<clipPlaneVertexDeclaration>\n#include<fogVertexDeclaration>\n#include<logDepthDeclaration>\nattribute splatIndex: f32;attribute position: vec2f;uniform invViewport: vec2f;uniform dataTextureSize: vec2f;uniform focal: vec2f;uniform kernelSize: f32;uniform eyePosition: vec3f;uniform viewDirectionFactor: vec3f;var covariancesATexture: texture_2d<f32>;var covariancesBTexture: texture_2d<f32>;var centersTexture: texture_2d<f32>;var colorsTexture: texture_2d<f32>;\n#if SH_DEGREE>0\nvar shTexture0: texture_2d<u32>;\n#endif\n#if SH_DEGREE>1\nvar shTexture1: texture_2d<u32>;\n#endif\n#if SH_DEGREE>2\nvar shTexture2: texture_2d<u32>;\n#endif\nvarying vColor: vec4f;varying vPosition: vec2f;\n#include<gaussianSplatting>\n@vertex\nfn main(input : VertexInputs)->FragmentInputs {var splat: Splat=readSplat(input.splatIndex,uniforms.dataTextureSize);var covA: vec3f=splat.covA.xyz;var covB: vec3f=vec3f(splat.covA.w,splat.covB.xy);let worldPos: vec4f=mesh.world*vec4f(splat.center.xyz,1.0);vertexOutputs.vPosition=input.position;\n#if SH_DEGREE>0\nlet worldRot: mat3x3f= mat3x3f(mesh.world[0].xyz,mesh.world[1].xyz,mesh.world[2].xyz);let normWorldRot: mat3x3f=inverseMat3(worldRot);var dir: vec3f=normalize(normWorldRot*(worldPos.xyz-uniforms.eyePosition.xyz));dir*=uniforms.viewDirectionFactor;vertexOutputs.vColor=vec4f(splat.color.xyz+computeSH(splat,dir),splat.color.w);\n#else\nvertexOutputs.vColor=splat.color;\n#endif\nvertexOutputs.position=gaussianSplatting(input.position,worldPos.xyz,vec2f(1.0,1.0),covA,covB,mesh.world,scene.view,scene.projection,uniforms.focal,uniforms.invViewport,uniforms.kernelSize);\n#include<clipPlaneVertex>\n#include<fogVertex>\n#include<logDepthVertex>\n}\n";e.ShadersStoreWGSL[le]||(e.ShadersStoreWGSL[le]=ue);const he={name:le,shader:ue};var _e=Object.freeze({__proto__:null,gaussianSplattingVertexShaderWGSL:he});class de extends p{constructor(){super(),this.FOG=!1,this.THIN_INSTANCES=!0,this.LOGARITHMICDEPTH=!1,this.CLIPPLANE=!1,this.CLIPPLANE2=!1,this.CLIPPLANE3=!1,this.CLIPPLANE4=!1,this.CLIPPLANE5=!1,this.CLIPPLANE6=!1,this.SH_DEGREE=0,this.COMPENSATION=!1,this.rebuild()}}class pe extends t{constructor(e,t){super(e,t),this.kernelSize=pe.KernelSize,this._compensation=pe.Compensation,this._isDirty=!1,this.backFaceCulling=!1}set compensation(e){this._isDirty=this._isDirty!=e,this._compensation=e}get compensation(){return this._compensation}get hasRenderTargetTextures(){return!1}needAlphaTesting(){return!1}needAlphaBlending(){return!0}isReadyForSubMesh(e,t){const l=!0,u=t._drawWrapper;let h=t.materialDefines;if(h&&this._isDirty&&h.markAsUnprocessed(),u.effect&&this.isFrozen&&u._wasPreviouslyReady&&u._wasPreviouslyUsingInstances===l)return!0;t.materialDefines||(h=t.materialDefines=new de);const _=this.getScene();if(this._isReadyForSubMesh(t))return!0;const d=_.getEngine(),p=e;s(e,_,this._useLogarithmicDepth,this.pointsCloud,this.fogEnabled,!1,h),n(_,d,this,h,l,null,!0),r(e,h,!1,!1),(d.version>1||d.isWebGPU)&&(h.SH_DEGREE=p.shDegree);const f=p.material;if(h.COMPENSATION=f&&f.compensation?f.compensation:pe.Compensation,h.isDirty){h.markAsProcessed(),_.resetCachedMaterial();const e=[o.PositionKind,"splatIndex"];i(e,h);const s=["world","view","projection","vFogInfos","vFogColor","logarithmicDepthConstant","invViewport","dataTextureSize","focal","eyePosition","kernelSize","viewDirectionFactor"],n=["covariancesATexture","covariancesBTexture","centersTexture","colorsTexture","shTexture0","shTexture1","shTexture2"],r=["Scene","Mesh"];a({uniformsNames:s,uniformBuffersNames:r,samplers:n,defines:h}),c(s);const l=h.toString(),u=_.getEngine().createEffect("gaussianSplatting",{attributes:e,uniformsNames:s,uniformBuffersNames:r,samplers:n,defines:l,onCompiled:this.onCompiled,onError:this.onError,indexParameters:{},shaderLanguage:this._shaderLanguage,extraInitializationsAsync:async()=>{1===this._shaderLanguage?await Promise.all([Promise.resolve().then((function(){return ae})),Promise.resolve().then((function(){return _e}))]):await Promise.all([Promise.resolve().then((function(){return K})),Promise.resolve().then((function(){return se}))])}},d);t.setEffect(u,h,this._materialContext)}return!(!t.effect||!t.effect.isReady())&&(h._renderId=_.getRenderId(),u._wasPreviouslyReady=!0,u._wasPreviouslyUsingInstances=l,this._isDirty=!1,!0)}static BindEffect(e,t,s){const n=s.getEngine(),r=s.activeCamera,i=n.getRenderWidth(),o=n.getRenderHeight(),a=e,c=a.material,u=r?.rigParent?.rigCameras.length||1;t.setFloat2("invViewport",1/(i/u),1/o);let h=1e3;if(r){const e=r.getProjectionMatrix().m[5];h=r.fovMode==l.FOVMODE_VERTICAL_FIXED?o*e/2:i*e/2}if(t.setFloat2("focal",h,h),t.setVector3("viewDirectionFactor",a.viewDirectionFactor),t.setFloat("kernelSize",c&&c.kernelSize?c.kernelSize:pe.KernelSize),s.bindEyePosition(t,"eyePosition",!0),a.covariancesATexture){const e=a.covariancesATexture.getSize();if(t.setFloat2("dataTextureSize",e.width,e.height),t.setTexture("covariancesATexture",a.covariancesATexture),t.setTexture("covariancesBTexture",a.covariancesBTexture),t.setTexture("centersTexture",a.centersTexture),t.setTexture("colorsTexture",a.colorsTexture),a.shTextures)for(let e=0;e<a.shTextures?.length;e++)t.setTexture(`shTexture${e}`,a.shTextures[e])}}bindForSubMesh(e,t,s){const n=this.getScene(),r=s.materialDefines;if(!r)return;const i=s.effect;if(!i)return;this._activeEffect=i,t.getMeshUniformBuffer().bindToEffect(i,"Mesh"),t.transferToEffect(e);this._mustRebind(n,i,s,t.visibility)?(this.bindView(i),this.bindViewProjection(i),pe.BindEffect(t,this._activeEffect,n),u(i,this,n)):n.getEngine()._features.needToAlwaysBindUniformBuffers&&(this._needToBindSceneUbo=!0),h(n,t,i),this.useLogarithmicDepth&&_(r,i,n),this._afterBind(t,this._activeEffect,s)}clone(e){return d.Clone((()=>new pe(e,this.getScene())),this)}serialize(){const e=super.serialize();return e.customType="BABYLON.GaussianSplattingMaterial",e}getClassName(){return"GaussianSplattingMaterial"}static Parse(e,t,s){return d.Parse((()=>new pe(e.name,t)),e,t,s)}}pe.KernelSize=.3,pe.Compensation=!1,f("BABYLON.GaussianSplattingMaterial",pe);const fe=x,me={...m,TwoPi:2*Math.PI,Sign:Math.sign,Log2:Math.log2,HCF:fe},xe=(e,t)=>{const s=(1<<t)-1;return(e&s)/s},ve=(e,t)=>{t.x=xe(e>>>21,11),t.y=xe(e>>>11,10),t.z=xe(e,11)},Se=(e,t)=>{t[0]=255*xe(e>>>24,8),t[1]=255*xe(e>>>16,8),t[2]=255*xe(e>>>8,8),t[3]=255*xe(e,8)},ye=(e,t)=>{const s=1/(.5*Math.sqrt(2)),n=(xe(e>>>20,10)-.5)*s,r=(xe(e>>>10,10)-.5)*s,i=(xe(e,10)-.5)*s,o=Math.sqrt(1-(n*n+r*r+i*i));switch(e>>>30){case 0:t.set(o,n,r,i);break;case 1:t.set(n,o,r,i);break;case 2:t.set(n,r,o,i);break;case 3:t.set(n,r,i,o)}};var ge,Ce,we,Te;!function(e){e[e.FLOAT=0]="FLOAT",e[e.INT=1]="INT",e[e.UINT=2]="UINT",e[e.DOUBLE=3]="DOUBLE",e[e.UCHAR=4]="UCHAR",e[e.UNDEFINED=5]="UNDEFINED"}(ge||(ge={})),function(e){e[e.MIN_X=0]="MIN_X",e[e.MIN_Y=1]="MIN_Y",e[e.MIN_Z=2]="MIN_Z",e[e.MAX_X=3]="MAX_X",e[e.MAX_Y=4]="MAX_Y",e[e.MAX_Z=5]="MAX_Z",e[e.MIN_SCALE_X=6]="MIN_SCALE_X",e[e.MIN_SCALE_Y=7]="MIN_SCALE_Y",e[e.MIN_SCALE_Z=8]="MIN_SCALE_Z",e[e.MAX_SCALE_X=9]="MAX_SCALE_X",e[e.MAX_SCALE_Y=10]="MAX_SCALE_Y",e[e.MAX_SCALE_Z=11]="MAX_SCALE_Z",e[e.PACKED_POSITION=12]="PACKED_POSITION",e[e.PACKED_ROTATION=13]="PACKED_ROTATION",e[e.PACKED_SCALE=14]="PACKED_SCALE",e[e.PACKED_COLOR=15]="PACKED_COLOR",e[e.X=16]="X",e[e.Y=17]="Y",e[e.Z=18]="Z",e[e.SCALE_0=19]="SCALE_0",e[e.SCALE_1=20]="SCALE_1",e[e.SCALE_2=21]="SCALE_2",e[e.DIFFUSE_RED=22]="DIFFUSE_RED",e[e.DIFFUSE_GREEN=23]="DIFFUSE_GREEN",e[e.DIFFUSE_BLUE=24]="DIFFUSE_BLUE",e[e.OPACITY=25]="OPACITY",e[e.F_DC_0=26]="F_DC_0",e[e.F_DC_1=27]="F_DC_1",e[e.F_DC_2=28]="F_DC_2",e[e.F_DC_3=29]="F_DC_3",e[e.ROT_0=30]="ROT_0",e[e.ROT_1=31]="ROT_1",e[e.ROT_2=32]="ROT_2",e[e.ROT_3=33]="ROT_3",e[e.MIN_COLOR_R=34]="MIN_COLOR_R",e[e.MIN_COLOR_G=35]="MIN_COLOR_G",e[e.MIN_COLOR_B=36]="MIN_COLOR_B",e[e.MAX_COLOR_R=37]="MAX_COLOR_R",e[e.MAX_COLOR_G=38]="MAX_COLOR_G",e[e.MAX_COLOR_B=39]="MAX_COLOR_B",e[e.SH_0=40]="SH_0",e[e.SH_1=41]="SH_1",e[e.SH_2=42]="SH_2",e[e.SH_3=43]="SH_3",e[e.SH_4=44]="SH_4",e[e.SH_5=45]="SH_5",e[e.SH_6=46]="SH_6",e[e.SH_7=47]="SH_7",e[e.SH_8=48]="SH_8",e[e.SH_9=49]="SH_9",e[e.SH_10=50]="SH_10",e[e.SH_11=51]="SH_11",e[e.SH_12=52]="SH_12",e[e.SH_13=53]="SH_13",e[e.SH_14=54]="SH_14",e[e.SH_15=55]="SH_15",e[e.SH_16=56]="SH_16",e[e.SH_17=57]="SH_17",e[e.SH_18=58]="SH_18",e[e.SH_19=59]="SH_19",e[e.SH_20=60]="SH_20",e[e.SH_21=61]="SH_21",e[e.SH_22=62]="SH_22",e[e.SH_23=63]="SH_23",e[e.SH_24=64]="SH_24",e[e.SH_25=65]="SH_25",e[e.SH_26=66]="SH_26",e[e.SH_27=67]="SH_27",e[e.SH_28=68]="SH_28",e[e.SH_29=69]="SH_29",e[e.SH_30=70]="SH_30",e[e.SH_31=71]="SH_31",e[e.SH_32=72]="SH_32",e[e.SH_33=73]="SH_33",e[e.SH_34=74]="SH_34",e[e.SH_35=75]="SH_35",e[e.SH_36=76]="SH_36",e[e.SH_37=77]="SH_37",e[e.SH_38=78]="SH_38",e[e.SH_39=79]="SH_39",e[e.SH_40=80]="SH_40",e[e.SH_41=81]="SH_41",e[e.SH_42=82]="SH_42",e[e.SH_43=83]="SH_43",e[e.SH_44=84]="SH_44",e[e.UNDEFINED=85]="UNDEFINED"}(Ce||(Ce={}));class Ee extends v{get viewDirectionFactor(){return this._viewDirectionFactor}get shDegree(){return this._shDegree}get splatsData(){return this._splatsData}get covariancesATexture(){return this._covariancesATexture}get covariancesBTexture(){return this._covariancesBTexture}get centersTexture(){return this._centersTexture}get colorsTexture(){return this._colorsTexture}get shTextures(){return this._shTextures}set material(e){this._material=e,this._material.backFaceCulling=!0,this._material.cullBackFaces=!1,e.resetDrawCache()}get material(){return this._material}constructor(e,t=null,s=null,n=!1){super(e,s),this._vertexCount=0,this._worker=null,this._frameIdLastUpdate=-1,this._modelViewMatrix=S.Identity(),this._canPostToWorker=!0,this._readyToDisplay=!1,this._covariancesATexture=null,this._covariancesBTexture=null,this._centersTexture=null,this._colorsTexture=null,this._splatPositions=null,this._splatIndex=null,this._shTextures=null,this._splatsData=null,this._sh=null,this._keepInRam=!1,this._delayedTextureUpdate=null,this._oldDirection=new y,this._useRGBACovariants=!1,this._material=null,this._tmpCovariances=[0,0,0,0,0,0],this._sortIsDirty=!1,this._shDegree=0,this._viewDirectionFactor=new y(1,1,-1);const r=new g;r.positions=[-3,-2,0,3,-2,0,0,4,0],r.indices=[0,1,2],r.applyToMesh(this),this.subMeshes=[],new C(0,0,3,0,3,this),this.setEnabled(!1),this._useRGBACovariants=!this.getEngine().isWebGPU&&1===this.getEngine().version,this._keepInRam=n,t&&this.loadFileAsync(t),this._material=new pe(this.name+"_material",this._scene)}getClassName(){return"GaussianSplattingMesh"}getTotalVertices(){return this._vertexCount}isReady(e=!1){return!!super.isReady(e,!0)&&(!!this._readyToDisplay||(this._postToWorker(!0),!1))}_postToWorker(e=!1){const t=this.getScene().getFrameId();if((e||t!==this._frameIdLastUpdate)&&this._worker&&this._scene.activeCamera&&this._canPostToWorker){const s=this._scene.activeCamera.getViewMatrix();this.getWorldMatrix().multiplyToRef(s,this._modelViewMatrix),s.invertToRef(w.Matrix[0]),this.getWorldMatrix().multiplyToRef(w.Matrix[0],w.Matrix[1]),y.TransformNormalToRef(y.Forward(this._scene.useRightHandedSystem),w.Matrix[1],w.Vector3[2]),w.Vector3[2].normalize();const n=y.Dot(w.Vector3[2],this._oldDirection);(e||Math.abs(n-1)>=.01)&&(this._oldDirection.copyFrom(w.Vector3[2]),this._frameIdLastUpdate=t,this._canPostToWorker=!1,this._worker.postMessage({view:this._modelViewMatrix.m,depthMix:this._depthMix,useRightHandedSystem:this._scene.useRightHandedSystem},[this._depthMix.buffer]))}}render(e,t,s){return this._postToWorker(),super.render(e,t,s)}static _TypeNameToEnum(e){switch(e){case"float":return 0;case"int":return 1;case"uint":return 2;case"double":return 3;case"uchar":return 4}return 5}static _ValueNameToEnum(e){switch(e){case"min_x":return 0;case"min_y":return 1;case"min_z":return 2;case"max_x":return 3;case"max_y":return 4;case"max_z":return 5;case"min_scale_x":return 6;case"min_scale_y":return 7;case"min_scale_z":return 8;case"max_scale_x":return 9;case"max_scale_y":return 10;case"max_scale_z":return 11;case"packed_position":return 12;case"packed_rotation":return 13;case"packed_scale":return 14;case"packed_color":return 15;case"x":return 16;case"y":return 17;case"z":return 18;case"scale_0":return 19;case"scale_1":return 20;case"scale_2":return 21;case"diffuse_red":case"red":return 22;case"diffuse_green":case"green":return 23;case"diffuse_blue":case"blue":return 24;case"f_dc_0":return 26;case"f_dc_1":return 27;case"f_dc_2":return 28;case"f_dc_3":return 29;case"opacity":return 25;case"rot_0":return 30;case"rot_1":return 31;case"rot_2":return 32;case"rot_3":return 33;case"min_r":return 34;case"min_g":return 35;case"min_b":return 36;case"max_r":return 37;case"max_g":return 38;case"max_b":return 39;case"f_rest_0":return 40;case"f_rest_1":return 41;case"f_rest_2":return 42;case"f_rest_3":return 43;case"f_rest_4":return 44;case"f_rest_5":return 45;case"f_rest_6":return 46;case"f_rest_7":return 47;case"f_rest_8":return 48;case"f_rest_9":return 49;case"f_rest_10":return 50;case"f_rest_11":return 51;case"f_rest_12":return 52;case"f_rest_13":return 53;case"f_rest_14":return 54;case"f_rest_15":return 55;case"f_rest_16":return 56;case"f_rest_17":return 57;case"f_rest_18":return 58;case"f_rest_19":return 59;case"f_rest_20":return 60;case"f_rest_21":return 61;case"f_rest_22":return 62;case"f_rest_23":return 63;case"f_rest_24":return 64;case"f_rest_25":return 65;case"f_rest_26":return 66;case"f_rest_27":return 67;case"f_rest_28":return 68;case"f_rest_29":return 69;case"f_rest_30":return 70;case"f_rest_31":return 71;case"f_rest_32":return 72;case"f_rest_33":return 73;case"f_rest_34":return 74;case"f_rest_35":return 75;case"f_rest_36":return 76;case"f_rest_37":return 77;case"f_rest_38":return 78;case"f_rest_39":return 79;case"f_rest_40":return 80;case"f_rest_41":return 81;case"f_rest_42":return 82;case"f_rest_43":return 83;case"f_rest_44":return 84}return 85}static ParseHeader(e){const t=new Uint8Array(e),s=(new TextDecoder).decode(t.slice(0,10240)),n="end_header\n",r=s.indexOf(n);if(r<0||!s)return null;const i=parseInt(/element vertex (\d+)\n/.exec(s)[1]),o=/element chunk (\d+)\n/.exec(s);let a=0;o&&(a=parseInt(o[1]));let c=0,l=0;const u={double:8,int:4,uint:4,float:4,short:2,ushort:2,uchar:1,list:0};let h;!function(e){e[e.Vertex=0]="Vertex",e[e.Chunk=1]="Chunk",e[e.SH=2]="SH"}(h||(h={}));let _=1;const d=[],p=[],f=s.slice(0,r).split("\n");let m=0;for(const e of f)if(e.startsWith("property ")){const[,t,s]=e.split(" "),n=Ee._ValueNameToEnum(s);n>=84?m=3:n>=64?m=2:n>=48&&(m=1);const r=Ee._TypeNameToEnum(t);1==_?(p.push({value:n,type:r,offset:l}),l+=u[t]):0==_?(d.push({value:n,type:r,offset:c}),c+=u[t]):2==_&&d.push({value:n,type:r,offset:c}),u[t]||T.Warn(`Unsupported property type: ${t}.`)}else if(e.startsWith("element ")){const[,t]=e.split(" ");"chunk"==t?_=1:"vertex"==t?_=0:"sh"==t&&(_=2)}const x=new DataView(e,r+11),v=new ArrayBuffer(Ee._RowOutputLength*i);let S=null,y=0;if(m){y=3*((m+1)*(m+1)-1),S=new ArrayBuffer(y*i)}return{vertexCount:i,chunkCount:a,rowVertexLength:c,rowChunkLength:l,vertexProperties:d,chunkProperties:p,dataView:x,buffer:v,shDegree:m,shCoefficientCount:y,shBuffer:S}}static _GetCompressedChunks(e,t){if(!e.chunkCount)return null;const s=e.dataView,n=new Array(e.chunkCount);for(let r=0;r<e.chunkCount;r++){const i={min:new y,max:new y,minScale:new y,maxScale:new y,minColor:new y(0,0,0),maxColor:new y(1,1,1)};n[r]=i;for(let n=0;n<e.chunkProperties.length;n++){const r=e.chunkProperties[n];let o;if(0===r.type)switch(o=s.getFloat32(r.offset+t.value,!0),r.value){case 0:i.min.x=o;break;case 1:i.min.y=o;break;case 2:i.min.z=o;break;case 3:i.max.x=o;break;case 4:i.max.y=o;break;case 5:i.max.z=o;break;case 6:i.minScale.x=o;break;case 7:i.minScale.y=o;break;case 8:i.minScale.z=o;break;case 9:i.maxScale.x=o;break;case 10:i.maxScale.y=o;break;case 11:i.maxScale.z=o;break;case 34:i.minColor.x=o;break;case 35:i.minColor.y=o;break;case 36:i.minColor.z=o;break;case 37:i.maxColor.x=o;break;case 38:i.maxColor.y=o;break;case 39:i.maxColor.z=o}}t.value+=e.rowChunkLength}return n}static _GetSplat(e,t,s,n){const r=w.Quaternion[0],i=w.Vector3[0],o=Ee._RowOutputLength,a=e.buffer,c=e.dataView,l=new Float32Array(a,t*o,3),u=new Float32Array(a,t*o+12,3),h=new Uint8ClampedArray(a,t*o+24,4),_=new Uint8ClampedArray(a,t*o+28,4);let d=null;e.shBuffer&&(d=new Uint8ClampedArray(e.shBuffer,t*e.shCoefficientCount,e.shCoefficientCount));const p=t>>8;let f=255,m=0,x=0,v=0;const S=[];for(let o=0;o<e.vertexProperties.length;o++){const a=e.vertexProperties[o];let _;switch(a.type){case 0:_=c.getFloat32(n.value+a.offset,!0);break;case 1:_=c.getInt32(n.value+a.offset,!0);break;case 2:_=c.getUint32(n.value+a.offset,!0);break;case 3:_=c.getFloat64(n.value+a.offset,!0);break;case 4:_=c.getUint8(n.value+a.offset);break;default:continue}switch(a.value){case 12:{const e=s[p];ve(_,i),l[0]=me.Lerp(e.min.x,e.max.x,i.x),l[1]=me.Lerp(e.min.y,e.max.y,i.y),l[2]=me.Lerp(e.min.z,e.max.z,i.z)}break;case 13:ye(_,r),f=r.x,m=r.y,x=r.z,v=r.w;break;case 14:{const e=s[p];ve(_,i),u[0]=Math.exp(me.Lerp(e.minScale.x,e.maxScale.x,i.x)),u[1]=Math.exp(me.Lerp(e.minScale.y,e.maxScale.y,i.y)),u[2]=Math.exp(me.Lerp(e.minScale.z,e.maxScale.z,i.z))}break;case 15:{const e=s[p];Se(_,h),h[0]=255*me.Lerp(e.minColor.x,e.maxColor.x,h[0]/255),h[1]=255*me.Lerp(e.minColor.y,e.maxColor.y,h[1]/255),h[2]=255*me.Lerp(e.minColor.z,e.maxColor.z,h[2]/255)}break;case 16:l[0]=_;break;case 17:l[1]=_;break;case 18:l[2]=_;break;case 19:u[0]=Math.exp(_);break;case 20:u[1]=Math.exp(_);break;case 21:u[2]=Math.exp(_);break;case 22:h[0]=_;break;case 23:h[1]=_;break;case 24:h[2]=_;break;case 26:h[0]=255*(.5+Ee._SH_C0*_);break;case 27:h[1]=255*(.5+Ee._SH_C0*_);break;case 28:h[2]=255*(.5+Ee._SH_C0*_);break;case 29:h[3]=255*(.5+Ee._SH_C0*_);break;case 25:h[3]=1/(1+Math.exp(-_))*255;break;case 30:f=_;break;case 31:m=_;break;case 32:x=_;break;case 33:v=_}if(d&&a.value>=40&&a.value<=84){const s=a.value-40;if(4==a.type&&e.chunkCount){const n=c.getUint8(e.rowChunkLength*e.chunkCount+e.vertexCount*e.rowVertexLength+t*e.shCoefficientCount+s);S[s]=127.5*(n*(8/255)-4)+127.5}else{const e=me.Clamp(127.5*_+127.5,0,255);S[s]=e}}}if(d){const t=1==e.shDegree?3:2==e.shDegree?8:15;for(let e=0;e<t;e++)d[3*e+0]=S[e],d[3*e+1]=S[e+t],d[3*e+2]=S[e+2*t]}r.set(m,x,v,f),r.normalize(),_[0]=127.5*r.w+127.5,_[1]=127.5*r.x+127.5,_[2]=127.5*r.y+127.5,_[3]=127.5*r.z+127.5,n.value+=e.rowVertexLength}static*ConvertPLYWithSHToSplat(e,t=!1){const s=Ee.ParseHeader(e);if(!s)return{buffer:e};const n={value:0},r=Ee._GetCompressedChunks(s,n);for(let e=0;e<s.vertexCount;e++)Ee._GetSplat(s,e,r,n),e%Ee._PlyConversionBatchSize===0&&t&&(yield);let i=null;if(s.shDegree&&s.shBuffer){const e=Math.ceil(s.shCoefficientCount/16);let t=0;const n=new Uint8Array(s.shBuffer);i=[];const r=s.vertexCount,o=E.LastCreatedEngine;if(o){const a=o.getCaps().maxTextureSize,c=Math.ceil(r/a);for(let t=0;t<e;t++){const e=new Uint8Array(c*a*4*4);i.push(e)}for(let e=0;e<r;e++)for(let r=0;r<s.shCoefficientCount;r++){const s=n[t++];i[Math.floor(r/16)][r%16+16*e]=s}}}return{buffer:s.buffer,sh:i}}static*ConvertPLYToSplat(e,t=!1){const s=Ee.ParseHeader(e);if(!s)return e;const n={value:0},r=Ee._GetCompressedChunks(s,n);for(let e=0;e<s.vertexCount;e++)Ee._GetSplat(s,e,r,n),e%Ee._PlyConversionBatchSize===0&&t&&(yield);return s.buffer}static async ConvertPLYToSplatAsync(e){return await A(Ee.ConvertPLYToSplat(e,!0),P())}static async ConvertPLYWithSHToSplatAsync(e){return await A(Ee.ConvertPLYWithSHToSplat(e,!0),P())}async loadDataAsync(e){return await this.updateDataAsync(e)}async loadFileAsync(e){const t=await z.LoadFileAsync(e,!0),s=await Ee.ConvertPLYWithSHToSplatAsync(t);await this.updateDataAsync(s.buffer,s.sh)}dispose(e){if(this._covariancesATexture?.dispose(),this._covariancesBTexture?.dispose(),this._centersTexture?.dispose(),this._colorsTexture?.dispose(),this._shTextures)for(const e of this._shTextures)e.dispose();this._covariancesATexture=null,this._covariancesBTexture=null,this._centersTexture=null,this._colorsTexture=null,this._shTextures=null,this._worker?.terminate(),this._worker=null,super.dispose(e,!0)}_copyTextures(e){if(this._covariancesATexture=e.covariancesATexture?.clone(),this._covariancesBTexture=e.covariancesBTexture?.clone(),this._centersTexture=e.centersTexture?.clone(),this._colorsTexture=e.colorsTexture?.clone(),e._shTextures){this._shTextures=[];for(const e of this._shTextures)this._shTextures?.push(e.clone())}}clone(e=""){const t=new Ee(e,void 0,this.getScene());t._copySource(this),t.makeGeometryUnique(),t._vertexCount=this._vertexCount,t._copyTextures(this),t._modelViewMatrix=S.Identity(),t._splatPositions=this._splatPositions,t._readyToDisplay=!1,t._instanciateWorker();const s=this.getBoundingInfo();return t.getBoundingInfo().reConstruct(s.minimum,s.maximum,this.getWorldMatrix()),t.forcedInstanceCount=t._vertexCount,t.setEnabled(!0),t}_makeSplat(e,t,s,n,r,i,o,a){const c=w.Matrix[0],l=w.Matrix[1],u=w.Quaternion[0],h=this._useRGBACovariants?4:2,_=t[8*e+0],d=-t[8*e+1],p=t[8*e+2];this._splatPositions[4*e+0]=_,this._splatPositions[4*e+1]=d,this._splatPositions[4*e+2]=p,o.minimizeInPlaceFromFloats(_,d,p),a.maximizeInPlaceFromFloats(_,d,p),u.set((s[32*e+28+1]-127.5)/127.5,(s[32*e+28+2]-127.5)/127.5,(s[32*e+28+3]-127.5)/127.5,-(s[32*e+28+0]-127.5)/127.5),u.toRotationMatrix(c),S.ScalingToRef(2*t[8*e+3+0],2*t[8*e+3+1],2*t[8*e+3+2],l);const f=c.multiplyToRef(l,w.Matrix[0]).m,m=this._tmpCovariances;m[0]=f[0]*f[0]+f[1]*f[1]+f[2]*f[2],m[1]=f[0]*f[4]+f[1]*f[5]+f[2]*f[6],m[2]=f[0]*f[8]+f[1]*f[9]+f[2]*f[10],m[3]=f[4]*f[4]+f[5]*f[5]+f[6]*f[6],m[4]=f[4]*f[8]+f[5]*f[9]+f[6]*f[10],m[5]=f[8]*f[8]+f[9]*f[9]+f[10]*f[10];let x=-1e4;for(let e=0;e<6;e++)x=Math.max(x,Math.abs(m[e]));this._splatPositions[4*e+3]=x;const v=x;n[4*e+0]=b(m[0]/v),n[4*e+1]=b(m[1]/v),n[4*e+2]=b(m[2]/v),n[4*e+3]=b(m[3]/v),r[e*h+0]=b(m[4]/v),r[e*h+1]=b(m[5]/v),i[4*e+0]=s[32*e+24+0],i[4*e+1]=s[32*e+24+1],i[4*e+2]=s[32*e+24+2],i[4*e+3]=s[32*e+24+3]}_updateTextures(e,t,s,n){const r=this._getTextureSize(this._vertexCount),i=(e,t,s,n)=>new O(e,t,s,n,this._scene,!1,!1,D.TEXTURE_BILINEAR_SAMPLINGMODE,D.TEXTURETYPE_FLOAT),o=(e,t,s,n)=>new O(e,t,s,n,this._scene,!1,!1,D.TEXTURE_BILINEAR_SAMPLINGMODE,D.TEXTURETYPE_UNSIGNED_BYTE),a=(e,t,s,n)=>new O(e,t,s,n,this._scene,!1,!1,D.TEXTURE_NEAREST_SAMPLINGMODE,D.TEXTURETYPE_UNSIGNED_INTEGER),c=(e,t,s,n)=>new O(e,t,s,n,this._scene,!1,!1,D.TEXTURE_BILINEAR_SAMPLINGMODE,D.TEXTURETYPE_HALF_FLOAT);if(this._covariancesATexture){this._delayedTextureUpdate={covA:e,covB:t,colors:s,centers:this._splatPositions,sh:n};const r=Float32Array.from(this._splatPositions),i=this._vertexCount;this._worker.postMessage({positions:r,vertexCount:i},[r.buffer]),this._postToWorker(!0)}else{if(this._covariancesATexture=c(e,r.x,r.y,D.TEXTUREFORMAT_RGBA),this._covariancesBTexture=c(t,r.x,r.y,this._useRGBACovariants?D.TEXTUREFORMAT_RGBA:D.TEXTUREFORMAT_RG),this._centersTexture=i(this._splatPositions,r.x,r.y,D.TEXTUREFORMAT_RGBA),this._colorsTexture=o(s,r.x,r.y,D.TEXTUREFORMAT_RGBA),n){this._shTextures=[];for(const e of n){const t=a(new Uint32Array(e.buffer),r.x,r.y,D.TEXTUREFORMAT_RGBA_INTEGER);t.wrapU=D.TEXTURE_CLAMP_ADDRESSMODE,t.wrapV=D.TEXTURE_CLAMP_ADDRESSMODE,this._shTextures.push(t)}}this._instanciateWorker()}}*_updateData(e,t,s){this._covariancesATexture||(this._readyToDisplay=!1);const n=new Uint8Array(e),r=new Float32Array(n.buffer);this._keepInRam&&(this._splatsData=e,s&&(this._sh=s));const i=n.length/Ee._RowOutputLength;i!=this._vertexCount&&this._updateSplatIndexBuffer(i),this._vertexCount=i,this._shDegree=s?s.length:0;const o=this._getTextureSize(i),a=o.x*o.y,c=Ee.ProgressiveUpdateAmount??o.y,l=o.x*c;this._splatPositions=new Float32Array(4*a);const u=new Uint16Array(4*a),h=new Uint16Array((this._useRGBACovariants?4:2)*a),_=new Uint8Array(4*a),d=new y(Number.MAX_VALUE,Number.MAX_VALUE,Number.MAX_VALUE),p=new y(-Number.MAX_VALUE,-Number.MAX_VALUE,-Number.MAX_VALUE);if(Ee.ProgressiveUpdateAmount){this._updateTextures(u,h,_,s),this.setEnabled(!0);const e=Math.ceil(o.y/c);for(let s=0;s<e;s++){const e=s*c,i=e*o.x;for(let e=0;e<l;e++)this._makeSplat(i+e,r,n,u,h,_,d,p);this._updateSubTextures(this._splatPositions,u,h,_,e,Math.min(c,o.y-e)),this.getBoundingInfo().reConstruct(d,p,this.getWorldMatrix()),t&&(yield)}const i=Float32Array.from(this._splatPositions),a=this._vertexCount;this._worker.postMessage({positions:i,vertexCount:a},[i.buffer]),this._sortIsDirty=!0}else{for(let e=0;e<i;e++)this._makeSplat(e,r,n,u,h,_,d,p),t&&e%Ee._SplatBatchSize===0&&(yield);this._updateTextures(u,h,_,s),this.getBoundingInfo().reConstruct(d,p,this.getWorldMatrix()),this.setEnabled(!0)}this._postToWorker(!0)}async updateDataAsync(e,t){return await A(this._updateData(e,!0,t),P())}updateData(e,t){H(this._updateData(e,!1,t))}refreshBoundingInfo(){return this.thinInstanceRefreshBoundingInfo(!1),this}_updateSplatIndexBuffer(e){(!this._splatIndex||e>this._splatIndex.length)&&(this._splatIndex=new Float32Array(e),this.thinInstanceSetBuffer("splatIndex",this._splatIndex,1,!1)),this.forcedInstanceCount=e}_updateSubTextures(e,t,s,n,r,i,o){const a=(e,t,s,n,r)=>{this.getEngine().updateTextureData(e.getInternalTexture(),t,0,n,s,r,0,0,!1)},c=this._getTextureSize(this._vertexCount),l=this._useRGBACovariants?4:2,u=r*c.x,h=i*c.x,_=new Uint16Array(t.buffer,4*u*Uint16Array.BYTES_PER_ELEMENT,4*h),d=new Uint16Array(s.buffer,u*l*Uint16Array.BYTES_PER_ELEMENT,h*l),p=new Uint8Array(n.buffer,4*u,4*h),f=new Float32Array(e.buffer,4*u*Float32Array.BYTES_PER_ELEMENT,4*h);if(a(this._covariancesATexture,_,c.x,r,i),a(this._covariancesBTexture,d,c.x,r,i),a(this._centersTexture,f,c.x,r,i),a(this._colorsTexture,p,c.x,r,i),o)for(let e=0;e<o.length;e++){const t=4,s=new Uint8Array(this._sh[e].buffer,u*t,h*t);a(this._shTextures[e],s,c.x,r,i)}}_instanciateWorker(){if(!this._vertexCount)return;this._updateSplatIndexBuffer(this._vertexCount),this._worker?.terminate(),this._worker=new Worker(URL.createObjectURL(new Blob(["(",Ee._CreateWorker.toString(),")(self)"],{type:"application/javascript"}))),this._depthMix=new BigInt64Array(this._vertexCount);const e=Float32Array.from(this._splatPositions),t=this._vertexCount;this._worker.postMessage({positions:e,vertexCount:t},[e.buffer]),this._worker.onmessage=e=>{this._depthMix=e.data.depthMix;const s=new Uint32Array(e.data.depthMix.buffer);if(this._splatIndex)for(let e=0;e<this._vertexCount;e++)this._splatIndex[e]=s[2*e];if(this._delayedTextureUpdate){const e=this._getTextureSize(t);this._updateSubTextures(this._delayedTextureUpdate.centers,this._delayedTextureUpdate.covA,this._delayedTextureUpdate.covB,this._delayedTextureUpdate.colors,0,e.y,this._delayedTextureUpdate.sh),this._delayedTextureUpdate=null}this.thinInstanceBufferUpdated("splatIndex"),this._canPostToWorker=!0,this._readyToDisplay=!0,this._sortIsDirty&&(this._postToWorker(!0),this._sortIsDirty=!1)}}_getTextureSize(e){const t=this._scene.getEngine(),s=t.getCaps().maxTextureSize;let n=1;if(1!==t.version||t.isWebGPU)n=Math.ceil(e/s);else for(;s*n<e;)n*=2;return n>s&&(T.Error("GaussianSplatting texture size: ("+s+", "+n+"), maxTextureSize: "+s),n=s),new M(s,n)}}Ee._RowOutputLength=32,Ee._SH_C0=.28209479177387814,Ee._SplatBatchSize=327680,Ee._PlyConversionBatchSize=32768,Ee.ProgressiveUpdateAmount=0,Ee._CreateWorker=function(e){let t,s,n,r,i=0;e.onmessage=o=>{if(o.data.positions)t=o.data.positions,i=o.data.vertexCount;else{const a=o.data.view;if(!t||!a)throw new Error("positions or view is not defined!");s=o.data.depthMix,n=new Uint32Array(s.buffer),r=new Float32Array(s.buffer);for(let e=0;e<i;e++)n[2*e]=e;let c=-1;o.data.useRightHandedSystem&&(c=1);for(let e=0;e<i;e++)r[2*e+1]=1e4+(a[2]*t[4*e+0]+a[6]*t[4*e+1]+a[10]*t[4*e+2])*c;s.sort(),e.postMessage({depthMix:s},[s.buffer])}}};class Ae{constructor(e,t,s,n,r){this.idx=0,this.color=new I(1,1,1,1),this.position=y.Zero(),this.rotation=y.Zero(),this.uv=new M(0,0),this.velocity=y.Zero(),this.pivot=y.Zero(),this.translateFromPivot=!1,this._pos=0,this._ind=0,this.groupId=0,this.idxInGroup=0,this._stillInvisible=!1,this._rotationMatrix=[1,0,0,0,1,0,0,0,1],this.parentId=null,this._globalPosition=y.Zero(),this.idx=e,this._group=t,this.groupId=s,this.idxInGroup=n,this._pcs=r}get size(){return this.size}set size(e){this.size=e}get quaternion(){return this.rotationQuaternion}set quaternion(e){this.rotationQuaternion=e}intersectsMesh(e,t){if(!e.hasBoundingInfo)return!1;if(!this._pcs.mesh)throw new Error("Point Cloud System doesnt contain the Mesh");if(t)return e.getBoundingInfo().boundingSphere.intersectsPoint(this.position.add(this._pcs.mesh.position));const s=e.getBoundingInfo().boundingBox,n=s.maximumWorld.x,r=s.minimumWorld.x,i=s.maximumWorld.y,o=s.minimumWorld.y,a=s.maximumWorld.z,c=s.minimumWorld.z,l=this.position.x+this._pcs.mesh.position.x,u=this.position.y+this._pcs.mesh.position.y,h=this.position.z+this._pcs.mesh.position.z;return r<=l&&l<=n&&o<=u&&u<=i&&c<=h&&h<=a}getRotationMatrix(e){let t;if(this.rotationQuaternion)t=this.rotationQuaternion;else{t=w.Quaternion[0];const e=this.rotation;R.RotationYawPitchRollToRef(e.y,e.x,e.z,t)}t.toRotationMatrix(e)}}class Pe{get groupID(){return this.groupId}set groupID(e){this.groupId=e}constructor(e,t){this.groupId=e,this._positionFunction=t}}!function(e){e[e.Color=2]="Color",e[e.UV=1]="UV",e[e.Random=0]="Random",e[e.Stated=3]="Stated"}(we||(we={}));class ze{get positions(){return this._positions32}get colors(){return this._colors32}get uvs(){return this._uvs32}constructor(e,t,s,n){this.particles=new Array,this.nbParticles=0,this.counter=0,this.vars={},this._promises=[],this._positions=new Array,this._indices=new Array,this._normals=new Array,this._colors=new Array,this._uvs=new Array,this._updatable=!0,this._isVisibilityBoxLocked=!1,this._alwaysVisible=!1,this._groups=new Array,this._groupCounter=0,this._computeParticleColor=!0,this._computeParticleTexture=!0,this._computeParticleRotation=!0,this._computeBoundingBox=!1,this._isReady=!1,this.name=e,this._size=t,this._scene=s||E.LastCreatedScene,n&&void 0!==n.updatable?this._updatable=n.updatable:this._updatable=!0}async buildMeshAsync(e){return await Promise.all(this._promises),this._isReady=!0,await this._buildMeshAsync(e)}async _buildMeshAsync(e){0===this.nbParticles&&this.addPoints(1),this._positions32=new Float32Array(this._positions),this._uvs32=new Float32Array(this._uvs),this._colors32=new Float32Array(this._colors);const t=new g;t.set(this._positions32,o.PositionKind),this._uvs32.length>0&&t.set(this._uvs32,o.UVKind);let s=0;this._colors32.length>0&&(s=1,t.set(this._colors32,o.ColorKind));const n=new v(this.name,this._scene);t.applyToMesh(n,this._updatable),this.mesh=n,this._positions=null,this._uvs=null,this._colors=null,this._updatable||(this.particles.length=0);let r=e;return r||(r=new W("point cloud material",this._scene),r.emissiveColor=new U(s,s,s),r.disableLighting=!0,r.pointsCloud=!0,r.pointSize=this._size),n.material=r,n}_addParticle(e,t,s,n){const r=new Ae(e,t,s,n,this);return this.particles.push(r),r}_randomUnitVector(e){e.position=new y(Math.random(),Math.random(),Math.random()),e.color=new I(1,1,1,1)}_getColorIndicesForCoord(e,t,s,n){const r=e._groupImageData,i=s*(4*n)+4*t,o=[i,i+1,i+2,i+3],a=o[1],c=o[2],l=o[3],u=r[o[0]],h=r[a],_=r[c],d=r[l];return new I(u/255,h/255,_/255,d)}_setPointsColorOrUV(e,t,s,n,r,i,a,c){c=c??0,s&&e.updateFacetData();const l=2*e.getBoundingInfo().boundingSphere.radius;let u=e.getVerticesData(o.PositionKind);const h=e.getIndices(),_=e.getVerticesData(o.UVKind+(c?c+1:"")),d=e.getVerticesData(o.ColorKind),p=y.Zero();e.computeWorldMatrix();const f=e.getWorldMatrix();if(!f.isIdentity()){u=u.slice(0);for(let e=0;e<u.length/3;e++)y.TransformCoordinatesFromFloatsToRef(u[3*e],u[3*e+1],u[3*e+2],f,p),u[3*e]=p.x,u[3*e+1]=p.y,u[3*e+2]=p.z}let m=0,x=0,v=0,S=0,g=0,C=0,w=0,T=0,E=0,A=0,P=0,z=0,b=0;const D=y.Zero(),H=y.Zero(),R=y.Zero(),B=y.Zero(),L=y.Zero();let k=0,O=0,N=0,W=0,X=0,Z=0;const j=M.Zero(),Y=M.Zero(),K=M.Zero(),q=M.Zero(),Q=M.Zero();let J=0,$=0,ee=0,te=0,se=0,ne=0,re=0,ie=0,oe=0,ae=0,ce=0,le=0;const ue=F.Zero(),he=F.Zero(),_e=F.Zero(),de=F.Zero(),pe=F.Zero();let fe,me,xe=0,ve=0;a=a||0;let Se=new F(0,0,0,0),ye=y.Zero(),ge=y.Zero(),Ce=y.Zero(),we=0,Te=y.Zero(),Ee=0,Ae=0;const Pe=new G(y.Zero(),new y(1,0,0));let ze,be=y.Zero();for(let o=0;o<h.length/3;o++){let c,p,f,M,F,G,De,He;x=h[3*o],v=h[3*o+1],S=h[3*o+2],g=u[3*x],C=u[3*x+1],w=u[3*x+2],T=u[3*v],E=u[3*v+1],A=u[3*v+2],P=u[3*S],z=u[3*S+1],b=u[3*S+2],D.set(g,C,w),H.set(T,E,A),R.set(P,z,b),H.subtractToRef(D,B),R.subtractToRef(H,L),_&&(k=_[2*x],O=_[2*x+1],N=_[2*v],W=_[2*v+1],X=_[2*S],Z=_[2*S+1],j.set(k,O),Y.set(N,W),K.set(X,Z),Y.subtractToRef(j,q),K.subtractToRef(Y,Q)),d&&n&&(J=d[4*x],$=d[4*x+1],ee=d[4*x+2],te=d[4*x+3],se=d[4*v],ne=d[4*v+1],re=d[4*v+2],ie=d[4*v+3],oe=d[4*S],ae=d[4*S+1],ce=d[4*S+2],le=d[4*S+3],ue.set(J,$,ee,te),he.set(se,ne,re,ie),_e.set(oe,ae,ce,le),he.subtractToRef(ue,de),_e.subtractToRef(he,pe));const Me=new U(0,0,0),Ie=new U(0,0,0);let Re,Ue;for(let u=0;u<t._groupDensity[o];u++)m=this.particles.length,this._addParticle(m,t,this._groupCounter,o+u),Ue=this.particles[m],xe=Math.sqrt(V(0,1)),ve=V(0,1),fe=D.add(B.scale(xe)).add(L.scale(xe*ve)),s&&(ye=e.getFacetNormal(o).normalize().scale(-1),ge=B.clone().normalize(),Ce=y.Cross(ye,ge),we=V(0,2*Math.PI),Te=ge.scale(Math.cos(we)).add(Ce.scale(Math.sin(we))),we=V(.1,Math.PI/2),be=Te.scale(Math.cos(we)).add(ye.scale(Math.sin(we))),Pe.origin=fe.add(be.scale(1e-5)),Pe.direction=be,Pe.length=l,ze=Pe.intersectsMesh(e),ze.hit&&(Ae=ze.pickedPoint.subtract(fe).length(),Ee=V(0,1)*Ae,fe.addInPlace(be.scale(Ee)))),Ue.position=fe.clone(),this._positions.push(Ue.position.x,Ue.position.y,Ue.position.z),void 0!==n?_&&(me=j.add(q.scale(xe)).add(Q.scale(xe*ve)),n?r&&null!==t._groupImageData?(c=t._groupImgWidth,p=t._groupImgHeight,Re=this._getColorIndicesForCoord(t,Math.round(me.x*c),Math.round(me.y*p),c),Ue.color=Re,this._colors.push(Re.r,Re.g,Re.b,Re.a)):d?(Se=ue.add(de.scale(xe)).add(pe.scale(xe*ve)),Ue.color=new I(Se.x,Se.y,Se.z,Se.w),this._colors.push(Se.x,Se.y,Se.z,Se.w)):(Se=ue.set(Math.random(),Math.random(),Math.random(),1),Ue.color=new I(Se.x,Se.y,Se.z,Se.w),this._colors.push(Se.x,Se.y,Se.z,Se.w)):(Ue.uv=me.clone(),this._uvs.push(Ue.uv.x,Ue.uv.y))):(i?(Me.set(i.r,i.g,i.b),f=V(-a,a),M=V(-a,a),He=Me.toHSV(),F=He.r,G=He.g+f,De=He.b+M,G<0&&(G=0),G>1&&(G=1),De<0&&(De=0),De>1&&(De=1),U.HSVtoRGBToRef(F,G,De,Ie),Se.set(Ie.r,Ie.g,Ie.b,1)):Se=ue.set(Math.random(),Math.random(),Math.random(),1),Ue.color=new I(Se.x,Se.y,Se.z,Se.w),this._colors.push(Se.x,Se.y,Se.z,Se.w))}}_colorFromTexture(e,t,s){if(null===e.material)return T.Warn(e.name+"has no material."),t._groupImageData=null,void this._setPointsColorOrUV(e,t,s,!0,!1);const n=e.material.getActiveTextures();if(0===n.length)return T.Warn(e.name+"has no usable texture."),t._groupImageData=null,void this._setPointsColorOrUV(e,t,s,!0,!1);const r=e.clone();r.setEnabled(!1),this._promises.push(new Promise((e=>{B.WhenAllReady(n,(()=>{let i=t._textureNb;i<0&&(i=0),i>n.length-1&&(i=n.length-1);const o=()=>{t._groupImgWidth=n[i].getSize().width,t._groupImgHeight=n[i].getSize().height,this._setPointsColorOrUV(r,t,s,!0,!0,void 0,void 0,n[i].coordinatesIndex),r.dispose(),e()};t._groupImageData=null;const a=n[i].readPixels();a?a.then((e=>{t._groupImageData=e,o()})):o()}))})))}_calculateDensity(e,t,s){let n,r,i,o,a,c,l,u,h,_,d,p;const f=y.Zero(),m=y.Zero(),x=y.Zero(),v=y.Zero(),S=y.Zero(),g=y.Zero();let C;const w=[];let T=0;const E=s.length/3;for(let e=0;e<E;e++)n=s[3*e],r=s[3*e+1],i=s[3*e+2],o=t[3*n],a=t[3*n+1],c=t[3*n+2],l=t[3*r],u=t[3*r+1],h=t[3*r+2],_=t[3*i],d=t[3*i+1],p=t[3*i+2],f.set(o,a,c),m.set(l,u,h),x.set(_,d,p),m.subtractToRef(f,v),x.subtractToRef(m,S),y.CrossToRef(v,S,g),C=.5*g.length(),T+=C,w[e]=T;const A=new Array(E);let P=e;for(let e=E-1;e>0;e--){const t=w[e];if(0===t)A[e]=0;else{const s=(t-w[e-1])/t*P,n=Math.floor(s),r=s-n,i=n+Number(Math.random()<r);A[e]=i,P-=i}}return A[0]=P,A}addPoints(e,t=this._randomUnitVector){const s=new Pe(this._groupCounter,t);let n,r=this.nbParticles;for(let t=0;t<e;t++)n=this._addParticle(r,s,this._groupCounter,t),s&&s._positionFunction&&s._positionFunction(n,r,t),this._positions.push(n.position.x,n.position.y,n.position.z),n.color&&this._colors.push(n.color.r,n.color.g,n.color.b,n.color.a),n.uv&&this._uvs.push(n.uv.x,n.uv.y),r++;return this.nbParticles+=e,this._groupCounter++,this._groupCounter}addSurfacePoints(e,t,s,n,r){let i=s||0;(isNaN(i)||i<0||i>3)&&(i=0);const a=e.getVerticesData(o.PositionKind),c=e.getIndices();this._groups.push(this._groupCounter);const l=new Pe(this._groupCounter,null);switch(l._groupDensity=this._calculateDensity(t,a,c),2===i?l._textureNb=n||0:n=n||new I(1,1,1,1),i){case 2:this._colorFromTexture(e,l,!1);break;case 1:this._setPointsColorOrUV(e,l,!1,!1,!1);break;case 0:this._setPointsColorOrUV(e,l,!1);break;case 3:this._setPointsColorOrUV(e,l,!1,void 0,void 0,n,r)}return this.nbParticles+=t,this._groupCounter++,this._groupCounter-1}addVolumePoints(e,t,s,n,r){let i=s||0;(isNaN(i)||i<0||i>3)&&(i=0);const a=e.getVerticesData(o.PositionKind),c=e.getIndices();this._groups.push(this._groupCounter);const l=new Pe(this._groupCounter,null);switch(l._groupDensity=this._calculateDensity(t,a,c),2===i?l._textureNb=n||0:n=n||new I(1,1,1,1),i){case 2:this._colorFromTexture(e,l,!0);break;case 1:this._setPointsColorOrUV(e,l,!0,!1,!1);break;case 0:this._setPointsColorOrUV(e,l,!0);break;case 3:this._setPointsColorOrUV(e,l,!0,void 0,void 0,n,r)}return this.nbParticles+=t,this._groupCounter++,this._groupCounter-1}setParticles(e=0,t=this.nbParticles-1,s=!0){if(!this._updatable||!this._isReady)return this;this.beforeUpdateParticles(e,t,s);const n=w.Matrix[0],r=this.mesh,i=this._colors32,a=this._positions32,c=this._uvs32,l=w.Vector3,u=l[5].copyFromFloats(1,0,0),h=l[6].copyFromFloats(0,1,0),_=l[7].copyFromFloats(0,0,1),d=l[8].setAll(Number.MAX_VALUE),p=l[9].setAll(-Number.MAX_VALUE);S.IdentityToRef(n);let f=0;if(this.mesh?.isFacetDataEnabled&&(this._computeBoundingBox=!0),t=t>=this.nbParticles?this.nbParticles-1:t,this._computeBoundingBox&&(0!=e||t!=this.nbParticles-1)){const e=this.mesh?.getBoundingInfo();e&&(d.copyFrom(e.minimum),p.copyFrom(e.maximum))}f=0;let m=0,x=0,v=0;for(let s=e;s<=t;s++){const e=this.particles[s];f=e.idx,m=3*f,x=4*f,v=2*f,this.updateParticle(e);const t=e._rotationMatrix,r=e.position,i=e._globalPosition;this._computeParticleRotation&&e.getRotationMatrix(n);if(null!==e.parentId){const s=this.particles[e.parentId],o=s._rotationMatrix,a=s._globalPosition,c=r.x*o[1]+r.y*o[4]+r.z*o[7],l=r.x*o[0]+r.y*o[3]+r.z*o[6],u=r.x*o[2]+r.y*o[5]+r.z*o[8];if(i.x=a.x+l,i.y=a.y+c,i.z=a.z+u,this._computeParticleRotation){const e=n.m;t[0]=e[0]*o[0]+e[1]*o[3]+e[2]*o[6],t[1]=e[0]*o[1]+e[1]*o[4]+e[2]*o[7],t[2]=e[0]*o[2]+e[1]*o[5]+e[2]*o[8],t[3]=e[4]*o[0]+e[5]*o[3]+e[6]*o[6],t[4]=e[4]*o[1]+e[5]*o[4]+e[6]*o[7],t[5]=e[4]*o[2]+e[5]*o[5]+e[6]*o[8],t[6]=e[8]*o[0]+e[9]*o[3]+e[10]*o[6],t[7]=e[8]*o[1]+e[9]*o[4]+e[10]*o[7],t[8]=e[8]*o[2]+e[9]*o[5]+e[10]*o[8]}}else if(i.x=0,i.y=0,i.z=0,this._computeParticleRotation){const e=n.m;t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=e[4],t[4]=e[5],t[5]=e[6],t[6]=e[8],t[7]=e[9],t[8]=e[10]}const o=l[11];e.translateFromPivot?o.setAll(0):o.copyFrom(e.pivot);const c=l[0];c.copyFrom(e.position);const S=c.x-e.pivot.x,y=c.y-e.pivot.y,g=c.z-e.pivot.z;let C=S*t[0]+y*t[3]+g*t[6],w=S*t[1]+y*t[4]+g*t[7],T=S*t[2]+y*t[5]+g*t[8];C+=o.x,w+=o.y,T+=o.z;const E=a[m]=i.x+u.x*C+h.x*w+_.x*T,A=a[m+1]=i.y+u.y*C+h.y*w+_.y*T,P=a[m+2]=i.z+u.z*C+h.z*w+_.z*T;if(this._computeBoundingBox&&(d.minimizeInPlaceFromFloats(E,A,P),p.maximizeInPlaceFromFloats(E,A,P)),this._computeParticleColor&&e.color){const t=e.color,s=this._colors32;s[x]=t.r,s[x+1]=t.g,s[x+2]=t.b,s[x+3]=t.a}if(this._computeParticleTexture&&e.uv){const t=e.uv,s=this._uvs32;s[v]=t.x,s[v+1]=t.y}}return r&&(s&&(this._computeParticleColor&&r.updateVerticesData(o.ColorKind,i,!1,!1),this._computeParticleTexture&&r.updateVerticesData(o.UVKind,c,!1,!1),r.updateVerticesData(o.PositionKind,a,!1,!1)),this._computeBoundingBox&&(r.hasBoundingInfo?r.getBoundingInfo().reConstruct(d,p,r._worldMatrix):r.buildBoundingInfo(d,p,r._worldMatrix))),this.afterUpdateParticles(e,t,s),this}dispose(){this.mesh?.dispose(),this.vars=null,this._positions=null,this._indices=null,this._normals=null,this._uvs=null,this._colors=null,this._indices32=null,this._positions32=null,this._uvs32=null,this._colors32=null}refreshVisibleSize(){return this._isVisibilityBoxLocked||this.mesh?.refreshBoundingInfo(),this}setVisibilityBox(e){if(!this.mesh)return;const t=e/2;this.mesh.buildBoundingInfo(new y(-t,-t,-t),new y(t,t,t))}get isAlwaysVisible(){return this._alwaysVisible}set isAlwaysVisible(e){this.mesh&&(this._alwaysVisible=e,this.mesh.alwaysSelectAsActiveMesh=e)}set computeParticleRotation(e){this._computeParticleRotation=e}set computeParticleColor(e){this._computeParticleColor=e}set computeParticleTexture(e){this._computeParticleTexture=e}get computeParticleColor(){return this._computeParticleColor}get computeParticleTexture(){return this._computeParticleTexture}set computeBoundingBox(e){this._computeBoundingBox=e}get computeBoundingBox(){return this._computeBoundingBox}initParticles(){}recycleParticle(e){return e}updateParticle(e){return e}beforeUpdateParticles(e,t,s){}afterUpdateParticles(e,t,s){}}!function(e){e[e.Splat=0]="Splat",e[e.PointCloud=1]="PointCloud",e[e.Mesh=2]="Mesh",e[e.Reject=3]="Reject"}(Te||(Te={}));class be{constructor(e=be._DefaultLoadingOptions){this.name=L.name,this._assetContainer=null,this.extensions=L.extensions,this._loadingOptions=e}createPlugin(e){return new be(e[L.name])}async importMeshAsync(e,t,s,n,r,i){return await this._parseAsync(e,t,s,n).then((e=>({meshes:e,particleSystems:[],skeletons:[],animationGroups:[],transformNodes:[],geometries:[],lights:[],spriteManagers:[]})))}static _BuildPointCloud(e,t){if(!t.byteLength)return!1;const s=new Uint8Array(t),n=new Float32Array(t),r=s.length/32;return e.addPoints(r,(function(e,t){const r=n[8*t+0],i=n[8*t+1],o=n[8*t+2];e.position=new y(r,i,o);const a=s[32*t+24+0]/255,c=s[32*t+24+1]/255,l=s[32*t+24+2]/255;e.color=new I(a,c,l,1)})),!0}static _BuildMesh(e,t){const s=new v("PLYMesh",e),n=new Uint8Array(t.data),r=new Float32Array(t.data),i=n.length/32,o=[],a=new g;for(let e=0;e<i;e++){const t=r[8*e+0],s=r[8*e+1],n=r[8*e+2];o.push(t,s,n)}if(t.hasVertexColors){const e=new Float32Array(4*i);for(let t=0;t<i;t++){const s=n[32*t+24+0]/255,r=n[32*t+24+1]/255,i=n[32*t+24+2]/255;e[4*t+0]=s,e[4*t+1]=r,e[4*t+2]=i,e[4*t+3]=1}a.colors=e}return a.positions=o,a.indices=t.faces,a.applyToMesh(s),s}_parseSPZAsync(e,t){const s=new Uint8Array(e),n=new Uint32Array(e.slice(0,12)),r=n[2],i=s[12],o=s[13],a=s[14],c=s[15],l=n[1];if(c||1347635022!=n[0]||2!=l&&3!=l)return new Promise((e=>{e({mode:3,data:u,hasVertexColors:!1})}));const u=new ArrayBuffer(32*r),h=1/(1<<o),_=new Int32Array(1),d=new Uint8Array(_.buffer),p=function(e,t){return d[0]=e[t+0],d[1]=e[t+1],d[2]=e[t+2],d[3]=128&e[t+2]?255:0,_[0]*h};let f=16;const m=new Float32Array(u),x=new Float32Array(u),v=new Uint8ClampedArray(u),S=new Uint8ClampedArray(u);let y=1,g=0;this._loadingOptions.flipY||(y=-1,g=255);for(let e=0;e<r;e++)m[8*e+0]=p(s,f+0),m[8*e+1]=y*p(s,f+3),m[8*e+2]=y*p(s,f+6),f+=9;for(let e=0;e<r;e++){for(let t=0;t<3;t++){const n=(s[f+r+3*e+t]-127.5)/38.25;v[32*e+24+t]=me.Clamp(255*(.5+.282*n),0,255)}v[32*e+24+3]=s[f+e]}f+=4*r;for(let e=0;e<r;e++)x[8*e+3+0]=Math.exp(s[f+0]/16-10),x[8*e+3+1]=Math.exp(s[f+1]/16-10),x[8*e+3+2]=Math.exp(s[f+2]/16-10),f+=3;if(l>=3){const e=Math.SQRT1_2;for(let t=0;t<r;t++){const n=[s[f+0],s[f+1],s[f+2],s[f+3]],r=n[0]+(n[1]<<8)+(n[2]<<16)+(n[3]<<24),i=511,o=[],a=r>>>30;let c=r,l=0;for(let t=3;t>=0;--t)if(t!==a){const s=c&i,n=c>>>9&1;c>>>=10,o[t]=e*(s/i),1===n&&(o[t]=-o[t]),l+=o[t]*o[t]}const u=1-l;o[a]=Math.sqrt(Math.max(u,0)),o[1]*=y,o[2]*=y;const h=[3,0,1,2];for(let e=0;e<4;e++)S[32*t+28+e]=Math.round(127.5+127.5*o[h[e]]);f+=4}}else for(let e=0;e<r;e++){const t=s[f+0],n=s[f+1]*y+g,r=s[f+2]*y+g,i=t/127.5-1,o=n/127.5-1,a=r/127.5-1;S[32*e+28+1]=t,S[32*e+28+2]=n,S[32*e+28+3]=r;const c=1-(i*i+o*o+a*a);S[32*e+28+0]=127.5+127.5*Math.sqrt(c<0?0:c),f+=3}if(i){const e=3*((i+1)*(i+1)-1),n=Math.ceil(e/16);let o=f;const c=[],l=t.getEngine().getCaps().maxTextureSize,h=Math.ceil(r/l);for(let e=0;e<n;e++){const e=new Uint8Array(h*l*4*4);c.push(e)}for(let t=0;t<r;t++)for(let n=0;n<e;n++){const e=s[o++],r=Math.floor(n/16);c[r][n%16+16*t]=e}return new Promise((e=>{e({mode:0,data:u,hasVertexColors:!1,sh:c,trainedWithAntialiasing:!!a})}))}return new Promise((e=>{e({mode:0,data:u,hasVertexColors:!1,trainedWithAntialiasing:!!a})}))}_parseAsync(e,t,s,n){const r=[],i=new ReadableStream({start(e){e.enqueue(new Uint8Array(s)),e.close()}}),o=new DecompressionStream("gzip"),a=i.pipeThrough(o);return new Promise((e=>{new Response(a).arrayBuffer().then((s=>{this._parseSPZAsync(s,t).then((s=>{t._blockEntityCollection=!!this._assetContainer;const n=new Ee("GaussianSplatting",null,t,this._loadingOptions.keepInRam);if(s.trainedWithAntialiasing){const e=n.material;e.kernelSize=.1,e.compensation=!0}n._parentContainer=this._assetContainer,r.push(n),n.updateData(s.data,s.sh),t._blockEntityCollection=!1,e(r)}))})).catch((()=>{be._ConvertPLYToSplat(s).then((async s=>{switch(t._blockEntityCollection=!!this._assetContainer,s.mode){case 0:{const e=new Ee("GaussianSplatting",null,t,this._loadingOptions.keepInRam);e._parentContainer=this._assetContainer,r.push(e),e.updateData(s.data,s.sh),!s.compressed&&s.rawSplat||e.viewDirectionFactor.set(-1,-1,1)}break;case 1:{const e=new ze("PointCloud",1,t);be._BuildPointCloud(e,s.data)?await e.buildMeshAsync().then((e=>{r.push(e)})):e.dispose()}break;case 2:if(!s.faces)throw new Error("PLY mesh doesn't contain face informations.");r.push(be._BuildMesh(t,s));break;default:throw new Error("Unsupported Splat mode")}t._blockEntityCollection=!1,e(r)}))}))}))}loadAssetContainerAsync(e,t,s){const n=new N(e);return this._assetContainer=n,this.importMeshAsync(null,e,t,s).then((e=>{for(const t of e.meshes)n.meshes.push(t);return this._assetContainer=null,n})).catch((e=>{throw this._assetContainer=null,e}))}loadAsync(e,t,s){return this.importMeshAsync(null,e,t,s).then((()=>{}))}static _ConvertPLYToSplat(e){const t=new Uint8Array(e),s=(new TextDecoder).decode(t.slice(0,10240)),n="end_header\n",r=s.indexOf(n);if(r<0||!s)return new Promise((t=>{t({mode:0,data:e,rawSplat:!0})}));const i=parseInt(/element vertex (\d+)\n/.exec(s)[1]),o=/element face (\d+)\n/.exec(s);let a=0;o&&(a=parseInt(o[1]));const c=/element chunk (\d+)\n/.exec(s);let l=0;c&&(l=parseInt(c[1]));let u=0,h=0;const _={double:8,int:4,uint:4,float:4,short:2,ushort:2,uchar:1,list:0};let d;!function(e){e[e.Vertex=0]="Vertex",e[e.Chunk=1]="Chunk",e[e.SH=2]="SH"}(d||(d={}));let p=1;const f=[],m=s.slice(0,r).split("\n");for(const e of m)if(e.startsWith("property ")){const[,t,s]=e.split(" ");1==p?h+=_[t]:0==p?(f.push({name:s,type:t,offset:u}),u+=_[t]):2==p&&f.push({name:s,type:t,offset:u}),_[t]||T.Warn(`Unsupported property type: ${t}.`)}else if(e.startsWith("element ")){const[,t]=e.split(" ");"chunk"==t?p=1:"vertex"==t?p=0:"sh"==t&&(p=2)}const x=u,v=h;return Ee.ConvertPLYWithSHToSplatAsync(e).then((async t=>{const s=new DataView(e,r+11);let n=v*l+x*i;const o=[];if(a)for(let e=0;e<a;e++){const e=s.getUint8(n);if(3==e){n+=1;for(let t=0;t<e;t++){const e=s.getUint32(n+4*(2-t),!0);o.push(e)}n+=12}}if(l)return await new Promise((e=>{e({mode:0,data:t.buffer,sh:t.sh,faces:o,hasVertexColors:!1,compressed:!0,rawSplat:!1})}));let c=0,u=0;const h=["x","y","z","scale_0","scale_1","scale_2","opacity","rot_0","rot_1","rot_2","rot_3"],_=["red","green","blue","f_dc_0","f_dc_1","f_dc_2"];for(let e=0;e<f.length;e++){const t=f[e];h.includes(t.name)&&c++,_.includes(t.name)&&u++}const d=c==h.length&&3==u,p=a?2:d?0:1;return await new Promise((e=>{e({mode:p,data:t.buffer,sh:t.sh,faces:o,hasVertexColors:!!u,compressed:!1,rawSplat:!1})}))}))}}be._DefaultLoadingOptions={keepInRam:!1,flipY:!1},k(new be);export{be as SPLATFileLoader};
//# sourceMappingURL=splatFileLoader-BLOxN-VM.esm.min.js.map