@multisetai/vps 1.1.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,93 +0,0 @@
1
- import*as h from'three';import {ARButton}from'three/addons/webxr/ARButton.js';import {DRACOLoader}from'three/examples/jsm/loaders/DRACOLoader.js';import {GLTFLoader}from'three/examples/jsm/loaders/GLTFLoader.js';import {TransformControls}from'three/examples/jsm/controls/TransformControls.js';function X(s,r){let t=s,e=(1-t[8])*r.width/2+r.x,i=(1-t[9])*r.height/2+r.y,a=r.width/2*t[0],o=r.height/2*t[5];return {fx:a,fy:o,px:e,py:i,width:r.width,height:r.height}}async function q(s,r,t,e=.8){let i=document.createElement("canvas");i.width=r,i.height=t;let a=i.getContext("2d");if(!a)return new Blob;let o=new ImageData(new Uint8ClampedArray(s),r,t);return a.putImageData(o,0,0),new Promise(c=>{i.toBlob(n=>c(n!=null?n:new Blob),"image/jpeg",e);})}async function O(s,r,t,e){let i=s.getContext();if(!i)return null;let a=i.createFramebuffer();if(!a)return null;let o;try{i.bindFramebuffer(i.FRAMEBUFFER,a),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,r,0),o=new Uint8Array(t*e*4),i.readPixels(0,0,t,e,i.RGBA,i.UNSIGNED_BYTE,o);}finally{i.bindFramebuffer(i.FRAMEBUFFER,null),i.deleteFramebuffer(a);}let c=new Uint8ClampedArray(o.length);for(let d=0;d<e;d+=1){let l=d*t*4,m=(e-d-1)*t*4;c.set(o.subarray(l,l+t*4),m);}let n=await q(c.buffer,t,e,.7);return n.size?{blob:n,width:t,height:e}:null}var F=class{constructor(r){this.renderer=new h.WebGLRenderer({canvas:r,antialias:true,alpha:true}),this.renderer.setSize(window.innerWidth,window.innerHeight),this.renderer.setPixelRatio(window.devicePixelRatio),this.renderer.xr.enabled=true,this.camera=new h.PerspectiveCamera(45,window.innerWidth/window.innerHeight,.2,1e4),this.scene=new h.Scene,this.setupLights();}setupLights(){let r=new h.HemisphereLight(16777215,16314623,1);r.position.set(.5,2,.25),this.scene.add(r);let t=new h.DirectionalLight("#7B2CBF");t.position.set(0,2,0),this.scene.add(t);}getScene(){return this.scene}getCamera(){return this.camera}getRenderer(){return this.renderer}resize(){this.camera.aspect=window.innerWidth/window.innerHeight,this.camera.updateProjectionMatrix(),this.renderer.setSize(window.innerWidth,window.innerHeight);}dispose(){this.renderer.dispose(),this.scene=null,this.camera=null,this.renderer=null;}};var K=`
2
- varying vec3 vWorldPosition;
3
- varying vec3 vWorldNormal;
4
-
5
- void main() {
6
- vec4 worldPos = modelMatrix * vec4(position, 1.0);
7
- vWorldPosition = worldPos.xyz;
8
- vWorldNormal = normalize(mat3(modelMatrix) * normal);
9
-
10
- gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
11
- }
12
- `,$=`
13
- uniform vec3 uColor;
14
- uniform float uOpacity;
15
-
16
- uniform vec3 uGridColor;
17
- uniform float uGridSize;
18
- uniform float uGridLineWidth;
19
- uniform float uShowGrid;
20
-
21
- uniform vec3 uCenter;
22
- uniform float uRadius;
23
- uniform float uProgress;
24
-
25
- uniform vec3 uGlowColor;
26
- uniform float uGlowWidth;
27
- uniform float uGlowIntensity;
28
-
29
- uniform vec3 uLightColor;
30
- uniform vec3 uLightDirection;
31
-
32
- varying vec3 vWorldPosition;
33
- varying vec3 vWorldNormal;
34
-
35
- float getGridFactor(vec3 worldPos, vec3 worldNormal, float gridSize, float lineWidth) {
36
- vec3 absNormal = abs(worldNormal);
37
- vec2 gridCoord;
38
-
39
- if (absNormal.y > absNormal.x && absNormal.y > absNormal.z) {
40
- gridCoord = worldPos.xz / gridSize;
41
- } else if (absNormal.x > absNormal.z) {
42
- gridCoord = worldPos.yz / gridSize;
43
- } else {
44
- gridCoord = worldPos.xy / gridSize;
45
- }
46
-
47
- vec2 gridFract = fract(abs(gridCoord));
48
- vec2 distToLine = min(gridFract, 1.0 - gridFract);
49
- float minDist = min(distToLine.x, distToLine.y);
50
- float gridFactor = 1.0 - smoothstep(0.0, lineWidth, minDist);
51
-
52
- return gridFactor;
53
- }
54
-
55
- void main() {
56
- vec3 baseColor = uColor;
57
- float baseAlpha = uOpacity;
58
-
59
- float distanceFromCenter = length(vWorldPosition - uCenter);
60
- float normalizedDistance = distanceFromCenter / max(uRadius, 0.001);
61
- float threshold = uProgress;
62
-
63
- if (threshold - normalizedDistance - 0.001 < 0.0) {
64
- discard;
65
- }
66
-
67
- float distanceFromEdge = threshold - normalizedDistance;
68
-
69
- float gridFactor = getGridFactor(vWorldPosition, normalize(vWorldNormal), uGridSize, uGridLineWidth);
70
-
71
- vec3 colorGridMixed = mix(baseColor, uGridColor, gridFactor * uShowGrid);
72
- float alphaGridMixed = max(baseAlpha, gridFactor * uShowGrid);
73
-
74
- float glowFactor = clamp(1.0 - clamp(distanceFromEdge / max(uGlowWidth, 0.001), 0.0, 1.0), 0.0, 1.0);
75
- glowFactor *= glowFactor;
76
-
77
- vec3 glowContribution = uGlowColor * glowFactor * uGlowIntensity;
78
-
79
- float alphaWithGlow = max(alphaGridMixed, glowFactor);
80
- alphaWithGlow = max(alphaWithGlow, 0.3);
81
-
82
- vec3 N = normalize(vWorldNormal);
83
- vec3 L = normalize(uLightDirection);
84
- float ndotl = max(0.3, dot(N, L));
85
- vec3 ambient = vec3(0.4);
86
- vec3 lighting = uLightColor * ndotl + ambient;
87
-
88
- vec3 litColor = colorGridMixed * lighting;
89
- vec3 finalColor = litColor + glowContribution * 0.5;
90
-
91
- gl_FragColor = vec4(finalColor, alphaWithGlow);
92
- }
93
- `;function _(s={}){var v,u,g,E,p,L,R,H,b,z,S,y,G,M;let r=(v=s.color)!=null?v:"#7B2CBF",t=(u=s.opacity)!=null?u:150/255,e=(g=s.gridColor)!=null?g:"#ffeb3b",i=(E=s.gridScale)!=null?E:2,a=(p=s.gridLineWidth)!=null?p:.02,o=(L=s.showGrid)!=null?L:1,c=(R=s.center)!=null?R:new h.Vector3(0,0,0),n=(H=s.radius)!=null?H:1,d=(b=s.progress)!=null?b:1,l=(z=s.glowColor)!=null?z:"#FF3A00",m=(S=s.glowWidth)!=null?S:.05,C=(y=s.glowIntensity)!=null?y:2,f=(G=s.lightColor)!=null?G:"#ffffff",w=(M=s.lightDirection)!=null?M:new h.Vector3(.3,1,.3).normalize();return new h.ShaderMaterial({vertexShader:K,fragmentShader:$,transparent:true,side:h.DoubleSide,uniforms:{uColor:{value:typeof r=="string"?new h.Color(r):r},uOpacity:{value:t},uGridColor:{value:typeof e=="string"?new h.Color(e):e},uGridSize:{value:i},uGridLineWidth:{value:a},uShowGrid:{value:o},uCenter:{value:c.clone()},uRadius:{value:n},uProgress:{value:d},uGlowColor:{value:typeof l=="string"?new h.Color(l):l},uGlowWidth:{value:m},uGlowIntensity:{value:C},uLightColor:{value:typeof f=="string"?new h.Color(f):f},uLightDirection:{value:w.clone()}}})}var U=50,W=class{constructor(r,t,e,i){this.scene=r;this.client=t;this.renderer=e;this.camera=i;this.gizmoControl=null;this.gizmoHelper=null;this.meshMaterial=null;this.localCenter=new h.Vector3;this.localRadius=0;this.radialDuration=4;this.radialRepeatInterval=20;this.idleTimeAfterReveal=0;this.lastViewerWorldPosition=new h.Vector3;this.meshGroup=new h.Group,this.meshGroup.visible=false,this.scene.add(this.meshGroup),this.dracoLoader=new DRACOLoader,this.dracoLoader.setDecoderPath("/draco/"),this.gltfLoader=new GLTFLoader,this.gltfLoader.setDRACOLoader(this.dracoLoader);}getMeshGroup(){return this.meshGroup}async ensureGizmoLoaded(){if(this.client.getConfig().showGizmo!==false){if(!this.gizmoControl){let t=new TransformControls(this.camera,this.renderer.domElement);t.setMode("translate"),t.setSpace("local"),t.enabled=false;let e=t.getHelper();this.scene.add(e),t.attach(this.meshGroup),this.gizmoControl=t,this.gizmoHelper=e;}this.gizmoHelper.visible=true;}}async ensureMeshLoaded(r){var i,a;if(this.scene.getObjectByName(r._id))return;let t=(a=(i=r.mapMesh)==null?void 0:i.rawMesh)==null?void 0:a.meshLink;if(!t)return;let e=await this.client.downloadFile(t);e&&await new Promise((o,c)=>{this.gltfLoader.load(e,n=>{let d=new h.Box3().setFromObject(n.scene),m=d.getSize(new h.Vector3).length(),C=d.getCenter(new h.Vector3);m>U&&(m=U),this.localCenter.copy(C),this.localRadius=Math.max(m/2,.001);let f=_({color:"#7B2CBF",opacity:.58,gridColor:"#ffeb3b",gridScale:2,gridLineWidth:.02,showGrid:1,center:this.localCenter.clone(),radius:this.localRadius,progress:0,glowColor:"#FF3A00",glowWidth:.05,glowIntensity:2});this.meshMaterial=f,n.scene.traverse(w=>{w.isMesh&&(w.material=f);}),n.scene.name=r._id,this.meshGroup.add(n.scene),o();},void 0,n=>{c(n instanceof Error?n:new Error(String(n)));});});}applyMeshTransform(r,t){let e=r.localizeData,i=new h.Vector3(e.position.x,e.position.y,e.position.z),a=new h.Quaternion(e.rotation.x,e.rotation.y,e.rotation.z,e.rotation.w),o=new h.Matrix4;o.compose(i,a,new h.Vector3(1,1,1));let c=o.clone().invert(),n=new h.Matrix4;n.multiplyMatrices(t,c);let d=new h.Vector3,l=new h.Quaternion,m=new h.Vector3;n.decompose(d,l,m),this.meshGroup.position.copy(d),this.meshGroup.quaternion.copy(l),this.meshGroup.scale.set(1,1,1),this.meshGroup.visible=true,this.meshGroup.updateMatrix(),this.meshMaterial&&this.meshMaterial.uniforms.uCenter.value.copy(this.localCenter).applyMatrix4(this.meshGroup.matrixWorld);}update(r,t){var o;if(!this.meshMaterial)return;t&&this.lastViewerWorldPosition.copy(t);let e=this.meshMaterial.uniforms;if(!e.uProgress)return;let i=(o=e.uProgress.value)!=null?o:0;if(i===0&&e.uCenter&&this.lastViewerWorldPosition.lengthSq()>0&&e.uCenter.value.copy(this.lastViewerWorldPosition),i>=1){this.idleTimeAfterReveal+=r,this.idleTimeAfterReveal>=this.radialRepeatInterval&&(e.uProgress.value=0,this.idleTimeAfterReveal=0);return}this.idleTimeAfterReveal=0;let a=Math.min(i+r/this.radialDuration,1);e.uProgress.value=a;}dispose(){this.meshMaterial=null,this.meshGroup.traverse(r=>{let t=r;t.isMesh&&(t.geometry.dispose(),Array.isArray(t.material)?t.material.forEach(e=>e.dispose()):t.material.dispose());}),this.scene.remove(this.meshGroup),this.dracoLoader.dispose(),this.gizmoHelper&&this.scene.remove(this.gizmoHelper),this.gizmoControl&&this.gizmoControl.dispose();}};var A=class{constructor(r,t,e,i){this.meshVisualizer=new W(r,t,e,i);}async ensureGizmoLoaded(){await this.meshVisualizer.ensureGizmoLoaded();}async ensureMeshLoaded(r){await this.meshVisualizer.ensureMeshLoaded(r);}applyMeshTransform(r,t){this.meshVisualizer.applyMeshTransform(r,t);}update(r,t){this.meshVisualizer.update(r,t);}dispose(){this.meshVisualizer.dispose();}};var ee=.2,re=.8,te=60,ie=1e4,oe=300,ne=10,B=class{constructor(r){this.options=r;this.experience=null;this.world=null;this.arButton=null;this.resizeHandler=null;this.isSessionActive=false;this.trackingLossFrames=0;this.hadTrackingLoss=false;this.isLocalizing=false;this.trackerSpace=null;this.xrWebGLBinding=null;}async initialize(r){var C,f,w,v;if(this.experience)return this.arButton;if(!window.isSecureContext)throw new Error("WebXR requires a secure context (HTTPS).");let t=(C=this.options.canvas)!=null?C:document.createElement("canvas");this.experience=new F(t);let e=this.experience.getRenderer(),i=this.experience.getCamera(),a=this.experience.getScene();this.world=new A(a,this.options.client,e,i),e.xr.addEventListener("sessionstart",()=>{var E,p;this.isSessionActive=true;let u=e.xr.getSession();if(u){let L=e.getContext(),R=XRWebGLBinding;this.xrWebGLBinding=new R(u,L);}(p=(E=this.options).onSessionStart)==null||p.call(E),this.options.client.getConfig().autoLocalize&&(u?u.requestAnimationFrame(()=>{this.localizeFrame();}):this.localizeFrame());}),e.xr.addEventListener("sessionend",()=>{var u,g;this.isSessionActive=false,this.xrWebGLBinding=null,(g=(u=this.options).onSessionEnd)==null||g.call(u);});let o=0,c=(u,g)=>{let E=o===0?0:(u-o)/1e3;o=u;let p=null;if(g){let R=e.xr.getReferenceSpace();if(R){let H=g.getViewerPose(R);if(H){let b=H.transform.position;p=new h.Vector3(b.x,b.y,b.z);}}}if(this.world&&E>0&&this.world.update(E,p),e.render(a,i),this.options.client.getConfig().relocalization&&g&&!this.isLocalizing){let R=e.xr.getReferenceSpace();R&&(g.getViewerPose(R)?this.hadTrackingLoss?(this.hadTrackingLoss=false,this.trackingLossFrames=0,this.localizeFrame()):this.trackingLossFrames=0:(this.trackingLossFrames+=1,this.trackingLossFrames>=te&&(this.hadTrackingLoss=true)));}};e.setAnimationLoop(c);let n=()=>{var u;(u=this.experience)==null||u.resize();};window.addEventListener("resize",n);let d=(f=this.options.overlayRoot)!=null?f:document.body,l=ARButton.createButton(e,{requiredFeatures:["camera-access","dom-overlay"],domOverlay:{root:d}}),m=r&&r instanceof HTMLElement?r:this.options.buttonContainer&&this.options.buttonContainer instanceof HTMLElement?this.options.buttonContainer:d;return m.contains(l)||m.appendChild(l),this.arButton=l,this.resizeHandler=n,(v=(w=this.options).onARButtonCreated)==null||v.call(w,l),l}getScene(){if(!this.experience)throw new Error("Scene: WebXR controller has not been initialized.");return this.experience.getScene()}getCamera(){if(!this.experience)throw new Error("Camera: WebXR controller has not been initialized.");return this.experience.getCamera()}getRenderer(){if(!this.experience)throw new Error("Renderer: WebXR controller has not been initialized.");return this.experience.getRenderer()}hasActiveSession(){var r;return this.isSessionActive&&((r=this.experience)==null?void 0:r.getRenderer().xr.isPresenting)===true}async localizeFrame(){var l,m,C,f,w,v,u,g,E;if(!this.experience)throw new Error("WebXR: WebXR controller has not been initialized.");if(!((m=(l=this.experience.getRenderer().xr).getSession)==null?void 0:m.call(l)))throw new Error("WebXR Session: No active WebXR session. Start AR before calling localizeFrame().");let e=this.options.client.getConfig(),i=(C=e.confidenceCheck)!=null?C:false,a=Math.max(ee,Math.min((f=e.confidenceThreshold)!=null?f:.5,re));(w=e.onLocalizationInit)==null||w.call(e),this.isLocalizing=true;let o=null,c;try{let p=await this.captureFrame();o=p.result,c=p.failureReason;}finally{this.isLocalizing=false;}if(o&&(!i||((v=o.localizeData.confidence)!=null?v:0)>=a)&&o){if((u=e.onLocalizationSuccess)==null||u.call(e,o),this.world&&this.trackerSpace){let p=e.showMesh&&!!o.mapDetails,L=e.showGizmo!==false;if(p||L)try{await this.world.ensureGizmoLoaded(),p&&await this.world.ensureMeshLoaded(o.mapDetails),this.world.applyMeshTransform(o,this.trackerSpace);}catch{}}return o}let d=c!=null?c:o?i?`Best confidence ${(g=o.localizeData.confidence)!=null?g:0} below threshold ${a}.`:void 0:"All attempts failed to produce a pose.";return (E=e.onLocalizationFailure)==null||E.call(e,d),null}async captureFrame(){var c,n,d;if(!this.experience)throw new Error("WebXR: WebXR controller has not been initialized.");let r=this.experience.getRenderer(),t=(n=(c=r.xr).getSession)==null?void 0:n.call(c);if(!t)throw new Error("WebXR Session: No active WebXR session. Start AR before capturing.");let e=r.xr.getReferenceSpace();if(!e)throw new Error("WebXR Reference Space: Unable to acquire XR reference space.");let i=r.getContext(),o=(d=this.options.client.getConfig().localizationTrackingTimeoutMs)!=null?d:ie;return new Promise((l,m)=>{let C=Date.now(),f=(w,v)=>{t.requestAnimationFrame(async(u,g)=>{var E,p,L,R,H;try{let b=Date.now()-C,z=g.getViewerPose(e);if(!z){if(b>=o||w+1>=oe){let y=(o/1e3).toFixed(1);l({result:null,failureReason:`Tracking unavailable: no viewer pose within ${y}s. Try moving the device or ensuring good lighting.`});}else f(w+1,v);return}let S=z.views;for(let y=0;y<S.length;y+=1){let G=S[y],M=G.camera;if(!M)continue;let V=(L=(p=(E=this.xrWebGLBinding)==null?void 0:E.getCameraImage)==null?void 0:p.call(E,M))!=null?L:null;if(!V)continue;let D=M.width,I=M.height;if(!D||!I)continue;let N=await O(r,V,D,I),k=X(G.projectionMatrix,{width:D,height:I,x:0,y:0});if(N&&k){this.trackerSpace=new h.Matrix4().fromArray(G.transform.matrix);let j=await this.options.client.localizeWithFrame(N,k);l({result:j});return}}v+1>=ne?l({result:null,failureReason:"Camera image not available. The device or browser may not support camera access in AR."}):f(w+1,v+1);}catch(b){m(b);}finally{i.bindFramebuffer(i.FRAMEBUFFER,(H=(R=t.renderState.baseLayer)==null?void 0:R.framebuffer)!=null?H:null);}});};f(0,0);})}dispose(){var r,t,e,i;this.resizeHandler&&window.removeEventListener("resize",this.resizeHandler),(r=this.experience)==null||r.getRenderer().setAnimationLoop(null),(t=this.experience)==null||t.dispose(),this.experience=null,(e=this.world)==null||e.dispose(),this.world=null,this.xrWebGLBinding=null,this.trackingLossFrames=0,this.hadTrackingLoss=false,(i=this.arButton)!=null&&i.parentElement&&this.arButton.parentElement.removeChild(this.arButton),this.arButton=null;}};export{B as WebxrController};