rgbd 0.0.11 → 0.0.13

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.
Files changed (2) hide show
  1. package/dist/rgbd.js +3 -3
  2. package/package.json +7 -4
package/dist/rgbd.js CHANGED
@@ -1,4 +1,4 @@
1
- const x=typeof window<"u"&&typeof HTMLElement<"u"&&typeof document<"u";let d,m,p;if(!x)d=class{},m=class{},p=class{};else{const S=`#version 300 es
1
+ import R from"./demux";const x=typeof window<"u"&&typeof HTMLElement<"u"&&typeof document<"u";let l,m,p;if(!x)l=class{},m=class{},p=class{};else{const A=`#version 300 es
2
2
 
3
3
  precision mediump float;
4
4
  precision mediump int;
@@ -79,9 +79,9 @@ const x=typeof window<"u"&&typeof HTMLElement<"u"&&typeof document<"u";let d,m,p
79
79
 
80
80
  fragColor = texture(u_colorImage, v_uv);
81
81
  }
82
- `;class v extends HTMLElement{constructor(){super(),this._params={GRID_SIZE:1e3,FOCAL_LENGTH:1200,DEPTH_MIN:.1,DEPTH_MAX:10,DISCARD_CUTOFF:0,CAM_RADIUS:1.5,CAM_MAX_THETA:Math.PI/40,CAM_MAX_PHI:Math.PI/64,CAM_FOV:Math.PI/4,CAM_SMOOTHNESS:.9,BOOMERANG_SPEED:.001,BACKGROUND_COLOR:[0,0,0]},this._shadowRoot=null,this._canvas=null,this._colorSource=null,this._depthSource=null,this._gl=null,this._shaderProgram=null,this._textures={},this._uniforms=null,this._mouse={x:0,y:0,hovered:!1},this._anim={theta:Math.PI,phi:0,radius:this._params.CAM_RADIUS},this._lastTime=0,this._animFrame=null,this._running=!1,this._userViewMat=null,this._userProjMat=null,this._onResizeHandler=null,this._onMouseMoveHandler=null,this._onMouseLeaveHandler=null,this._onRenderHandler=null,this._shadowRoot=this.attachShadow({mode:"open"});const t=document.createElement("div");t.style.position="relative",t.style.width="100%",t.style.height="100%",t.style.display="flex",t.style.alignItems="center",t.style.justifyContent="center",t.style.background="black",this._shadowRoot.appendChild(t);const e=document.createElement("style");e.textContent=`
82
+ `;class E extends HTMLElement{constructor(){super(),this.i={GRID_SIZE:250,FOCAL_LENGTH:1200,DEPTH_MIN:.1,DEPTH_MAX:10,DISCARD_CUTOFF:0,CAM_RADIUS:1.5,CAM_MAX_THETA:Math.PI/40,CAM_MAX_PHI:Math.PI/64,CAM_FOV:Math.PI/4,CAM_SMOOTHNESS:.9,BOOMERANG_SPEED:0,BACKGROUND_COLOR:[0,0,0]},this.g=null,this.e=null,this.t=null,this.h=null,this.n=null,this.E=null,this.l={},this.r=null,this.o={x:0,y:0,hovered:!1},this.s={theta:Math.PI,phi:0,radius:this.i.CAM_RADIUS},this._=0,this.v=null,this.M=!1,this.U=null,this.L=null,this.T=null,this.C=null,this.D=null,this.R=null,this.g=this.attachShadow({mode:"open"});const t=document.createElement("div");t.style.position="relative",t.style.width="100%",t.style.height="100%",t.style.display="flex",t.style.alignItems="center",t.style.justifyContent="center",t.style.background="black",this.g.appendChild(t);const e=document.createElement("style");e.textContent=`
83
83
  :host { display: inline-block; width: 300px; height: 200px; }
84
84
  :host([fullbleed]) { display:block; width:100%; height:100%; }
85
85
  canvas { touch-action: none; }
86
86
  .cover-img, .cover-video { position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
87
- `,this._shadowRoot.appendChild(e),this._canvas=document.createElement("canvas"),this._canvas.style.width="100%",this._canvas.style.height="100%",this._canvas.style.objectFit="contain",this._canvas.setAttribute("role","img"),this._canvas.setAttribute("aria-label","3D image"),t.appendChild(this._canvas),this._onResizeHandler=this._onResize.bind(this),this._onMouseMoveHandler=this._onMouseMove.bind(this),this._onMouseLeaveHandler=this._onMouseLeave.bind(this),this._onRenderHandler=this._onRender.bind(this)}connectedCallback(){this._readParams();let t;try{this._init(),t=!1}catch{t=!0}window.addEventListener("resize",this._onResizeHandler),window.addEventListener("mousemove",this._onMouseMoveHandler),document.addEventListener("mouseleave",this._onMouseLeaveHandler),this._loadSources(),t?this._createFallback():(this._onResize(),this._running=!0,this._lastTime=performance.now(),this._animFrame=requestAnimationFrame(this._onRenderHandler))}disconnectedCallback(){this._running=!1,cancelAnimationFrame(this._animFrame),window.removeEventListener("resize",this._onResizeHandler),this._canvas.removeEventListener("pointermove",this._onMouseMoveHandler),this._canvas.removeEventListener("pointerleave",this._onMouseLeaveHandler);try{if(this._gl){const t=this._gl;this._shaderProgram&&t.deleteProgram(this._shaderProgram);for(let e in this._textures)t.deleteTexture(this._textures[e])}}catch{}}setViewMatrix(t){this._userViewMat=t&&t.length===16?new Float32Array(t):null}setProjectionMatrix(t){this._userProjMat=t&&t.length===16?new Float32Array(t):null}get width(){return this._getSourceWidth(this._colorSource)}get height(){return this._getSourceHeight(this._colorSource)}_init(){const t=this._canvas.getContext("webgl2",{antialias:!0,alpha:!1});if(!t)throw new Error("WebGL not supported");this._gl=t;const e=this._createProgram(t,S,g);if(!e)return;this._shaderProgram=e,t.useProgram(e);const o=this._createTexture(t),i=this._createTexture(t);this._textures.color=o,this._textures.depth=i,this._updateTexture("color",this._colorSource),this._updateTexture("depth",this._depthSource);const r=t.getUniformLocation(e,"u_colorImage"),a=t.getUniformLocation(e,"u_depthImage");t.uniform1i(r,0),t.uniform1i(a,1),this._uniforms={uProjection:t.getUniformLocation(e,"u_projMat"),uView:t.getUniformLocation(e,"u_viewMat"),uImageSize:t.getUniformLocation(e,"u_imageSize"),uFocal:t.getUniformLocation(e,"u_focal"),uDepthMin:t.getUniformLocation(e,"u_depthMin"),uDepthMax:t.getUniformLocation(e,"u_depthMax"),uGridSegs:t.getUniformLocation(e,"u_grid"),uTargetRes:t.getUniformLocation(e,"u_targetRes"),uDiscardCutoff:t.getUniformLocation(e,"u_discardCutoff")}}_onRender(t){if(!this._running||!this._gl)return;const e=this._gl,o=t-this._lastTime;this._lastTime=t,this._updateTexturesIfNeeded(),e.clearColor(...this._params.BACKGROUND_COLOR,1),e.clear(e.COLOR_BUFFER_BIT|e.DEPTH_BUFFER_BIT),e.enable(e.DEPTH_TEST),this._mouse.hovered||(this._mouse.x=Math.sin(t*this._params.BOOMERANG_SPEED),this._mouse.y=Math.sin(t*this._params.BOOMERANG_SPEED*.5+Math.PI));const i=this._mouse.x*this._params.CAM_MAX_THETA+Math.PI,r=-this._mouse.y*this._params.CAM_MAX_PHI,a=this._mouse.hovered?this._params.CAM_RADIUS*.8:this._params.CAM_RADIUS;this._anim.theta=this._animate(i,this._anim.theta,o),this._anim.phi=this._animate(r,this._anim.phi,o),this._anim.radius=this._animate(a,this._anim.radius,o);const u=this._canvas.width/this._canvas.height,h=Math.max(1,Math.floor(this._params.GRID_SIZE)),n=Math.max(1,Math.round(h/u)),s=this._getSourceWidth(this._colorSource),c=this._getSourceHeight(this._colorSource);e.uniform2f(this._uniforms.uImageSize,s,c),e.uniform1f(this._uniforms.uFocal,this._params.FOCAL_LENGTH),e.uniform1f(this._uniforms.uDepthMin,this._params.DEPTH_MIN),e.uniform1f(this._uniforms.uDepthMax,this._params.DEPTH_MAX),e.uniform2i(this._uniforms.uGridSegs,h,n),e.uniform2f(this._uniforms.uTargetRes,this._canvas.width,this._canvas.height),e.uniform1f(this._uniforms.uDiscardCutoff,this._params.DISCARD_CUTOFF);const _=[this._anim.radius*Math.cos(this._anim.phi)*Math.sin(this._anim.theta),this._anim.radius*Math.sin(this._anim.phi),this._params.CAM_RADIUS+this._anim.radius*Math.cos(this._anim.phi)*Math.cos(this._anim.theta)],l=[0,0,this._params.CAM_RADIUS],T=[0,-1,0],M=this._userViewMat||new Float32Array(this._lookAt(_,l,T)),R=this._userProjMat||new Float32Array(this._perspective(this._params.CAM_FOV,u,.1,100));e.uniformMatrix4fv(this._uniforms.uView,!1,M),e.uniformMatrix4fv(this._uniforms.uProjection,!1,R),e.drawArrays(e.TRIANGLES,0,h*n*6),this._animFrame=requestAnimationFrame(this._onRenderHandler)}_onMouseMove(t){const e=this._canvas.getBoundingClientRect();this._mouse.hovered=!0,this._mouse.x=(t.clientX-e.left)/e.width*2-1,this._mouse.y=(t.clientY-e.top)/e.height*2-1}_onMouseLeave(){this._mouse.hovered=!1}_onResize(){const t=window.devicePixelRatio||1,e=this._canvas.parentElement;if(!e)return;const o=e.clientWidth,i=e.clientHeight;let r=this._getSourceWidth(this._colorSource)||1,a=this._getSourceHeight(this._colorSource)||1;const u=r/a;let h=o,n=h/u;n>i&&(n=i,h=n*u);const s=Math.max(1,Math.floor(h*t)),c=Math.max(1,Math.floor(n*t));this._canvas.width=s,this._canvas.height=c,this._gl&&this._gl.viewport(0,0,s,c)}_readParams(){const t=this._params;if(this.hasAttribute("grid-size")&&(t.GRID_SIZE=parseInt(this.getAttribute("grid-size"))??t.GRID_SIZE),this.hasAttribute("focal-length")&&(t.FOCAL_LENGTH=parseFloat(this.getAttribute("focal-length"))??t.FOCAL_LENGTH),this.hasAttribute("depth-min")&&(t.DEPTH_MIN=parseFloat(this.getAttribute("depth-min"))??t.DEPTH_MIN),this.hasAttribute("depth-max")&&(t.DEPTH_MAX=parseFloat(this.getAttribute("depth-max"))??t.DEPTH_MAX),this.hasAttribute("discard-cutoff")&&(t.DISCARD_CUTOFF=parseFloat(this.getAttribute("discard-cutoff"))??t.DISCARD_CUTOFF),this.hasAttribute("cam-radius")&&(t.CAM_RADIUS=parseFloat(this.getAttribute("cam-radius"))??t.CAM_RADIUS),this.hasAttribute("cam-max-theta")&&(t.CAM_MAX_THETA=parseFloat(this.getAttribute("cam-max-theta"))??t.CAM_MAX_THETA),this.hasAttribute("cam-max-phi")&&(t.CAM_MAX_PHI=parseFloat(this.getAttribute("cam-max-phi"))??t.CAM_MAX_PHI),this.hasAttribute("cam-fov")&&(t.CAM_FOV=parseFloat(this.getAttribute("cam-fov"))??t.CAM_FOV),this.hasAttribute("cam-smoothness")&&(t.CAM_SMOOTHNESS=parseFloat(this.getAttribute("cam-smoothness"))??t.CAM_SMOOTHNESS),this.hasAttribute("boomerang-speed")&&(t.BOOMERANG_SPEED=parseFloat(this.getAttribute("boomerang-speed"))??t.BOOMERANG_SPEED),this.hasAttribute("background-color"))try{const e=this.getAttribute("background-color").split(",").map(Number);e.length===3&&(t.BACKGROUND_COLOR=e)}catch{}}_createShader(t,e,o){const i=t.createShader(e);return t.shaderSource(i,o),t.compileShader(i),t.getShaderParameter(i,t.COMPILE_STATUS)?i:(console.error(t.getShaderInfoLog(i)),t.deleteShader(i),null)}_createProgram(t,e,o){const i=this._createShader(t,t.VERTEX_SHADER,e),r=this._createShader(t,t.FRAGMENT_SHADER,o);if(!i||!r)return null;const a=t.createProgram();return t.attachShader(a,i),t.attachShader(a,r),t.linkProgram(a),t.getProgramParameter(a,t.LINK_STATUS)?(t.deleteShader(i),t.deleteShader(r),a):(console.error(t.getProgramInfoLog(a)),t.deleteProgram(a),null)}_createTexture(t){const e=t.createTexture();return t.bindTexture(t.TEXTURE_2D,e),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.LINEAR),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.LINEAR),e}_updateTexture(t,e){if(!this._gl)return;const o=this._gl,i=this._textures[t];if(o.activeTexture(t==="color"?o.TEXTURE0:o.TEXTURE1),o.bindTexture(o.TEXTURE_2D,i),!this._isSourceReady(e)){o.texImage2D(o.TEXTURE_2D,0,o.RGBA,1,1,0,o.RGBA,o.UNSIGNED_BYTE,new Uint8Array([0,0,0,0]));return}try{o.pixelStorei(o.UNPACK_FLIP_Y_WEBGL,!1),o.texImage2D(o.TEXTURE_2D,0,o.RGBA,o.RGBA,o.UNSIGNED_BYTE,e)}catch{}}_perspective(t,e,o,i){const r=1/Math.tan(t/2);return[r/e,0,0,0,0,r,0,0,0,0,(i+o)/(o-i),-1,0,0,2*i*o/(o-i),0]}_lookAt(t,e,o){function i(s){const c=Math.hypot(...s)||1;return s.map(_=>_/c)}function r(s,c){return[s[1]*c[2]-s[2]*c[1],s[2]*c[0]-s[0]*c[2],s[0]*c[1]-s[1]*c[0]]}function a(s,c){return s.map((_,l)=>_-c[l])}const u=i(a(t,e)),h=i(r(o,u)),n=r(u,h);return[h[0],n[0],u[0],0,h[1],n[1],u[1],0,h[2],n[2],u[2],0,-(h[0]*t[0]+h[1]*t[1]+h[2]*t[2]),-(n[0]*t[0]+n[1]*t[1]+n[2]*t[2]),-(u[0]*t[0]+u[1]*t[1]+u[2]*t[2]),1]}_animate(t,e,o){const i=(1-this._params.CAM_SMOOTHNESS)*o/16.67,r=Math.sign(t-e);return e+=(t-e)*i,Math.sign(t-e)!==r&&(e=t),e}_loadSources(){throw new Error("Must implement _loadSources")}_createFallback(){throw new Error("Must implement _createFallback")}_updateTexturesIfNeeded(){}_isSourceReady(t){throw new Error("Must implement _isSourceReady")}_getSourceWidth(t){throw new Error("Must implement _getSourceWidth")}_getSourceHeight(t){throw new Error("Must implement _getSourceHeight")}}d=v;class A extends d{static get observedAttributes(){return["color-src","depth-src","color-id","depth-id","grid-size","focal-length","depth-min","depth-max","discard-cutoff","cam-radius","cam-max-theta","cam-max-phi","cam-fov","cam-smoothness","boomerang-speed","background-color"]}constructor(){super(),this._colorSource=new Image,this._depthSource=new Image,this._colorSource.crossOrigin="anonymous",this._depthSource.crossOrigin="anonymous"}attributeChangedCallback(t,e,o){if(e!==o)if(t==="color-src")this._colorSource.src=o||"";else if(t==="depth-src")this._depthSource.src=o||"";else if(t==="color-id"){const i=document.getElementById(o);i instanceof HTMLImageElement&&(this._colorSource=i,i.complete?this._updateTexture("color",i):i.addEventListener("load",()=>this._updateTexture("color",i)))}else if(t==="depth-id"){const i=document.getElementById(o);i instanceof HTMLImageElement&&(this._depthSource=i,i.complete?this._updateTexture("depth",i):i.addEventListener("load",()=>this._updateTexture("depth",i)))}else this._readParams()}_loadSources(){const t=this.getAttribute("color-id"),e=this.getAttribute("depth-id"),o=this.getAttribute("color-src"),i=this.getAttribute("depth-src");if(t){const r=document.getElementById(t);r instanceof HTMLImageElement&&r.complete?(this._colorSource=r,this._updateTexture("color",this._colorSource)):r&&(r.addEventListener("load",()=>this._updateTexture("color",r)),this._colorSource=r)}else o&&(this._colorSource.src=o,this._colorSource.onload=()=>this._updateTexture("color",this._colorSource));if(e){const r=document.getElementById(e);r instanceof HTMLImageElement&&r.complete?(this._depthSource=r,this._updateTexture("depth",this._depthSource)):r&&(r.addEventListener("load",()=>this._updateTexture("depth",r)),this._depthSource=r)}else i&&(this._depthSource.src=i,this._depthSource.onload=()=>this._updateTexture("depth",this._depthSource))}_createFallback(){this.getAttribute("color-id")&&(this._colorSource=this._colorSource.cloneNode(),this._colorSource.id+="_threedad-img"),this._colorSource.style.display="block",this._colorSource.classList.add("cover-img"),this._canvas.parentElement.appendChild(this._colorSource)}_isSourceReady(t){return t&&t.complete&&t.naturalWidth>0}_getSourceWidth(t){return t?t.width:1}_getSourceHeight(t){return t?t.height:1}}m=A;class E extends d{static get observedAttributes(){return["color-src","depth-src","color-id","depth-id","grid-size","focal-length","depth-min","depth-max","discard-cutoff","cam-radius","cam-max-theta","cam-max-phi","cam-fov","cam-smoothness","boomerang-speed","background-color","autoplay","loop","muted"]}constructor(){super(),this._colorSource=document.createElement("video"),this._depthSource=document.createElement("video"),this._colorSource.crossOrigin="anonymous",this._depthSource.crossOrigin="anonymous",this._colorSource.playsInline=!0,this._depthSource.playsInline=!0,this._colorSource.muted=!0,this._depthSource.muted=!0}attributeChangedCallback(t,e,o){if(e!==o)if(t==="color-src")this._colorSource.src=o||"";else if(t==="depth-src")this._depthSource.src=o||"";else if(t==="color-id"){const i=document.getElementById(o);i instanceof HTMLVideoElement&&(this._colorSource=i,i.readyState>=i.HAVE_CURRENT_DATA?this._updateTexture("color",i):i.addEventListener("loadeddata",()=>this._updateTexture("color",i)))}else if(t==="depth-id"){const i=document.getElementById(o);i instanceof HTMLVideoElement&&(this._depthSource=i,i.readyState>=i.HAVE_CURRENT_DATA?this._updateTexture("depth",i):i.addEventListener("loadeddata",()=>this._updateTexture("depth",i)))}else t==="autoplay"?(this._colorSource.autoplay=this.hasAttribute("autoplay"),this._depthSource.autoplay=this.hasAttribute("autoplay")):t==="loop"?(this._colorSource.loop=this.hasAttribute("loop"),this._depthSource.loop=this.hasAttribute("loop")):t==="muted"?(this._colorSource.muted=this.hasAttribute("muted"),this._depthSource.muted=this.hasAttribute("muted")):this._readParams()}play(){return Promise.all([this._colorSource?.play(),this._depthSource?.play()])}pause(){this._colorSource?.pause(),this._depthSource?.pause()}get paused(){return this._colorSource?this._colorSource.paused:!0}get currentTime(){return this._colorSource?this._colorSource.currentTime:0}set currentTime(t){console.log(t),this._colorSource&&(this._colorSource.currentTime=t),this._depthSource&&(this._depthSource.currentTime=t)}_loadSources(){const t=this.getAttribute("color-id"),e=this.getAttribute("depth-id"),o=this.getAttribute("color-src"),i=this.getAttribute("depth-src");if(this.hasAttribute("autoplay")&&(this._colorSource.autoplay=!0,this._depthSource.autoplay=!0),this.hasAttribute("loop")&&(this._colorSource.loop=!0,this._depthSource.loop=!0),this.hasAttribute("muted")&&(this._colorSource.muted=!0,this._depthSource.muted=!0),t){const r=document.getElementById(t);r instanceof HTMLVideoElement&&(this._colorSource=r)}else o&&(this._colorSource.src=o);if(e){const r=document.getElementById(e);r instanceof HTMLVideoElement&&(this._depthSource=r)}else i&&(this._depthSource.src=i);this.hasAttribute("autoplay")?this.play().then(()=>{this._onResize()}):this.play().then(()=>{this.pause(),this._onResize()})}_createFallback(){this.getAttribute("color-id")&&(this._colorSource=this._colorSource.cloneNode(!0),this._colorSource.id+="_threedad-video"),this._colorSource.style.display="block",this._colorSource.classList.add("cover-video"),this._colorSource.controls=!0,this._canvas.parentElement.appendChild(this._colorSource)}_updateTexturesIfNeeded(){this._isSourceReady(this._colorSource)&&this._updateTexture("color",this._colorSource),this._isSourceReady(this._depthSource)&&this._updateTexture("depth",this._depthSource),Math.abs(this._colorSource.currentTime-this._depthSource.currentTime)>.01&&(this._depthSource.currentTime=this._colorSource.currentTime,this._colorSource.paused!==this._depthSource.paused&&(this._colorSource.paused?this._depthSource.pause():this._depthSource.play()))}_isSourceReady(t){return t&&t.readyState>=t.HAVE_CURRENT_DATA}_getSourceWidth(t){return t?t.videoWidth:1}_getSourceHeight(t){return t?t.videoHeight:1}}p=E,customElements.define("rgbd-img",m),customElements.define("rgbd-video",p)}
87
+ `,this.g.appendChild(e),this.e=document.createElement("canvas"),this.e.style.width="100%",this.e.style.height="100%",this.e.style.objectFit="contain",this.e.setAttribute("role","img"),this.e.setAttribute("aria-label","3D image"),t.appendChild(this.e),this.T=this.F.bind(this),this.C=this.z.bind(this),this.D=this.W.bind(this),this.R=this.S.bind(this)}connectedCallback(){this.b();let t;try{this.V(),t=!1}catch{t=!0}window.addEventListener("resize",this.T),window.addEventListener("mousemove",this.C),document.addEventListener("mouseleave",this.D),t?this.I():(this.F(),this.M=!0,this._=performance.now(),this.v=requestAnimationFrame(this.R))}disconnectedCallback(){this.M=!1,cancelAnimationFrame(this.v),window.removeEventListener("resize",this.T),this.e.removeEventListener("pointermove",this.C),this.e.removeEventListener("pointerleave",this.D);try{if(this.n){const t=this.n;this.E&&t.deleteProgram(this.E);for(let e in this.l)t.deleteTexture(this.l[e])}}catch{}}setViewMatrix(t){this.U=t&&t.length===16?new Float32Array(t):null}setProjectionMatrix(t){this.L=t&&t.length===16?new Float32Array(t):null}get width(){return this.f(this.t)}get height(){return this.m(this.t)}V(){const t=this.e.getContext("webgl2",{antialias:!0,alpha:!1});if(!t)throw new Error("WebGL not supported");this.n=t;const e=this.j(t,A,g);if(!e)return;this.E=e,t.useProgram(e);const i=this.G(t),s=this.G(t);this.l.color=i,this.l.depth=s,this.c("color",this.t),this.c("depth",this.h);const r=t.getUniformLocation(e,"u_colorImage"),h=t.getUniformLocation(e,"u_depthImage");t.uniform1i(r,0),t.uniform1i(h,1),this.r={uProjection:t.getUniformLocation(e,"u_projMat"),uView:t.getUniformLocation(e,"u_viewMat"),uImageSize:t.getUniformLocation(e,"u_imageSize"),uFocal:t.getUniformLocation(e,"u_focal"),uDepthMin:t.getUniformLocation(e,"u_depthMin"),uDepthMax:t.getUniformLocation(e,"u_depthMax"),uGridSegs:t.getUniformLocation(e,"u_grid"),uTargetRes:t.getUniformLocation(e,"u_targetRes"),uDiscardCutoff:t.getUniformLocation(e,"u_discardCutoff")}}S(t){if(!this.M||!this.n)return;const e=this.n,i=t-this._;this._=t,this.H(),e.clearColor(...this.i.BACKGROUND_COLOR,1),e.clear(e.COLOR_BUFFER_BIT|e.DEPTH_BUFFER_BIT),e.enable(e.DEPTH_TEST),this.o.hovered||(this.o.x=Math.sin(t*this.i.BOOMERANG_SPEED),this.o.y=Math.sin(t*this.i.BOOMERANG_SPEED*.5+Math.PI));const s=this.o.x*this.i.CAM_MAX_THETA+Math.PI,r=-this.o.y*this.i.CAM_MAX_PHI,h=this.o.hovered?this.i.CAM_RADIUS*.8:this.i.CAM_RADIUS;this.s.theta=this.w(s,this.s.theta,i),this.s.phi=this.w(r,this.s.phi,i),this.s.radius=this.w(h,this.s.radius,i);const o=this.e.width/this.e.height,a=Math.max(1,Math.floor(this.i.GRID_SIZE)),c=Math.max(1,Math.round(a/o)),n=this.f(this.t),u=this.m(this.t);e.uniform2f(this.r.uImageSize,n,u),e.uniform1f(this.r.uFocal,this.i.FOCAL_LENGTH),e.uniform1f(this.r.uDepthMin,this.i.DEPTH_MIN),e.uniform1f(this.r.uDepthMax,this.i.DEPTH_MAX),e.uniform2i(this.r.uGridSegs,a,c),e.uniform2f(this.r.uTargetRes,this.e.width,this.e.height),e.uniform1f(this.r.uDiscardCutoff,this.i.DISCARD_CUTOFF);const d=[this.s.radius*Math.cos(this.s.phi)*Math.sin(this.s.theta),this.s.radius*Math.sin(this.s.phi),this.i.CAM_RADIUS+this.s.radius*Math.cos(this.s.phi)*Math.cos(this.s.theta)],f=[0,0,this.i.CAM_RADIUS],T=[0,-1,0],C=this.U||new Float32Array(this.K(d,f,T)),D=this.L||new Float32Array(this.q(this.i.CAM_FOV,o,.1,100));e.uniformMatrix4fv(this.r.uView,!1,C),e.uniformMatrix4fv(this.r.uProjection,!1,D),e.drawArrays(e.TRIANGLES,0,a*c*6),this.v=requestAnimationFrame(this.R)}z(t){const e=this.e.getBoundingClientRect();this.o.hovered=!0,this.o.x=(t.clientX-e.left)/e.width*2-1,this.o.y=(t.clientY-e.top)/e.height*2-1}W(){this.o.hovered=!1}F(){const t=window.devicePixelRatio||1,e=this.e.parentElement;if(!e)return;const i=e.clientWidth,s=e.clientHeight;let r=this.f(this.t)||1,h=this.m(this.t)||1;const o=r/h;let a=i,c=a/o;c>s&&(c=s,a=c*o);const n=Math.max(1,Math.floor(a*t)),u=Math.max(1,Math.floor(c*t));this.e.width=n,this.e.height=u,this.n&&this.n.viewport(0,0,n,u)}b(){const t=this.i;if(this.hasAttribute("grid-size")&&(t.GRID_SIZE=parseInt(this.getAttribute("grid-size"))??t.GRID_SIZE),this.hasAttribute("focal-length")&&(t.FOCAL_LENGTH=parseFloat(this.getAttribute("focal-length"))??t.FOCAL_LENGTH),this.hasAttribute("depth-min")&&(t.DEPTH_MIN=parseFloat(this.getAttribute("depth-min"))??t.DEPTH_MIN),this.hasAttribute("depth-max")&&(t.DEPTH_MAX=parseFloat(this.getAttribute("depth-max"))??t.DEPTH_MAX),this.hasAttribute("discard-cutoff")&&(t.DISCARD_CUTOFF=parseFloat(this.getAttribute("discard-cutoff"))??t.DISCARD_CUTOFF),this.hasAttribute("cam-radius")&&(t.CAM_RADIUS=parseFloat(this.getAttribute("cam-radius"))??t.CAM_RADIUS),this.hasAttribute("cam-max-theta")&&(t.CAM_MAX_THETA=parseFloat(this.getAttribute("cam-max-theta"))??t.CAM_MAX_THETA),this.hasAttribute("cam-max-phi")&&(t.CAM_MAX_PHI=parseFloat(this.getAttribute("cam-max-phi"))??t.CAM_MAX_PHI),this.hasAttribute("cam-fov")&&(t.CAM_FOV=parseFloat(this.getAttribute("cam-fov"))??t.CAM_FOV),this.hasAttribute("cam-smoothness")&&(t.CAM_SMOOTHNESS=parseFloat(this.getAttribute("cam-smoothness"))??t.CAM_SMOOTHNESS),this.hasAttribute("boomerang-speed")&&(t.BOOMERANG_SPEED=parseFloat(this.getAttribute("boomerang-speed"))??t.BOOMERANG_SPEED),this.hasAttribute("background-color"))try{const e=this.getAttribute("background-color").split(",").map(Number);e.length===3&&(t.BACKGROUND_COLOR=e)}catch{}}k(t,e,i){const s=t.createShader(e);return t.shaderSource(s,i),t.compileShader(s),t.getShaderParameter(s,t.COMPILE_STATUS)?s:(console.error(t.getShaderInfoLog(s)),t.deleteShader(s),null)}j(t,e,i){const s=this.k(t,t.VERTEX_SHADER,e),r=this.k(t,t.FRAGMENT_SHADER,i);if(!s||!r)return null;const h=t.createProgram();return t.attachShader(h,s),t.attachShader(h,r),t.linkProgram(h),t.getProgramParameter(h,t.LINK_STATUS)?(t.deleteShader(s),t.deleteShader(r),h):(console.error(t.getProgramInfoLog(h)),t.deleteProgram(h),null)}G(t){const e=t.createTexture();return t.bindTexture(t.TEXTURE_2D,e),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.LINEAR),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.LINEAR),e}c(t,e){if(!this.n)return;const i=this.n,s=this.l[t];if(i.activeTexture(t==="color"?i.TEXTURE0:i.TEXTURE1),i.bindTexture(i.TEXTURE_2D,s),!this.P(e)){i.texImage2D(i.TEXTURE_2D,0,i.RGBA,1,1,0,i.RGBA,i.UNSIGNED_BYTE,new Uint8Array([0,0,0,0]));return}try{i.pixelStorei(i.UNPACK_FLIP_Y_WEBGL,!1),i.texImage2D(i.TEXTURE_2D,0,i.RGBA,i.RGBA,i.UNSIGNED_BYTE,e)}catch{}}q(t,e,i,s){const r=1/Math.tan(t/2);return[r/e,0,0,0,0,r,0,0,0,0,(s+i)/(i-s),-1,0,0,2*s*i/(i-s),0]}K(t,e,i){function s(n){const u=Math.hypot(...n)||1;return n.map(d=>d/u)}function r(n,u){return[n[1]*u[2]-n[2]*u[1],n[2]*u[0]-n[0]*u[2],n[0]*u[1]-n[1]*u[0]]}function h(n,u){return n.map((d,f)=>d-u[f])}const o=s(h(t,e)),a=s(r(i,o)),c=r(o,a);return[a[0],c[0],o[0],0,a[1],c[1],o[1],0,a[2],c[2],o[2],0,-(a[0]*t[0]+a[1]*t[1]+a[2]*t[2]),-(c[0]*t[0]+c[1]*t[1]+c[2]*t[2]),-(o[0]*t[0]+o[1]*t[1]+o[2]*t[2]),1]}w(t,e,i){const s=(1-this.i.CAM_SMOOTHNESS)*i/16.67,r=Math.sign(t-e);return e+=(t-e)*s,Math.sign(t-e)!==r&&(e=t),e}I(){throw new Error("Must implement _createFallback")}H(){}P(t){throw new Error("Must implement _isSourceReady")}f(t){throw new Error("Must implement _getSourceWidth")}m(t){throw new Error("Must implement _getSourceHeight")}}l=E;class v extends l{static get observedAttributes(){return["color-src","depth-src","grid-size","focal-length","depth-min","depth-max","discard-cutoff","cam-radius","cam-max-theta","cam-max-phi","cam-fov","cam-smoothness","boomerang-speed","background-color"]}constructor(){super(),this.t=new Image,this.h=new Image,this.t.crossOrigin="anonymous",this.h.crossOrigin="anonymous",this.t.onload=()=>this.c("color",this.t),this.h.onload=()=>this.c("depth",this.h)}attributeChangedCallback(t,e,i){e!==i&&(t==="color-src"?this.t.src=i||"":t==="depth-src"?this.h.src=i||"":this.b())}I(){this.t.style.display="block",this.t.classList.add("cover-img"),this.e.parentElement.appendChild(this.t)}P(t){return t&&t.complete&&t.naturalWidth>0}f(t){return t?t.width:1}m(t){return t?t.height:1}}m=v;class M extends l{static get observedAttributes(){return["color-src","depth-src","grid-size","focal-length","depth-min","depth-max","discard-cutoff","cam-radius","cam-max-theta","cam-max-phi","cam-fov","cam-smoothness","boomerang-speed","background-color","autoplay","loop","muted"]}constructor(){super(),this.a=[],this.u=[],this.d=!1,this.O=!1,this.N=!0,this.p=0,this.Q=0}attributeChangedCallback(t,e,i){e!==i&&(t==="color-src"?(this.Y(),this.a=[this.A(i,0)]):t==="depth-src"?this.u=[this.A(i,0)]:t==="autoplay"?this.d=this.hasAttribute("autoplay"):t==="loop"?this.O=this.hasAttribute("loop"):t==="muted"?this.N=this.hasAttribute("muted"):this.b())}play(){this.d=!0}pause(){this.d=!1}enqueue(t,e,i){this.a.push(this.A(t,i)),this.u.push(this.A(e,i))}get paused(){return!this.d}get currentTime(){return this.p/1e3}set currentTime(t){throw new Error("Seeking not yet implemented!")}A(t,e){const i={startTime:e*1e3,demuxer:null,decoder:null,chunks:[],frames:[],curChunk:0};return i.decoder=new VideoDecoder({output(s){i.frames.push(s)},error(s){console.error("VideoDecoder encountered an error while decoding: ",s)}}),i.demuxer=new R(t,{onConfig(s){i.config=s,i.decoder?.configure(s)},onChunk(s){i.chunks?.push(s)},onEndOfStream(){i.demuxer=null}}),i}Z(t){const e=t.chunks[t.chunks.length-1];if(!e)return null;const i={startTime:t.startTime+e.timestamp+e.duration,demuxer:null,decoder:null,config:t.config,chunks:t.chunks,frames:[],curChunk:0};return i.decoder=new VideoDecoder({output(s){i.frames.push(s)},error(s){console.error("VideoDecoder encountered an error while decoding: ",s)}}),i.decoder.configure(t.config),i}S(t){this.d&&(this.p+=t-this._);const e=(s,r)=>{const h=s[r];for(;h.curChunk<h.chunks.length;){const o=h.chunks[h.curChunk];if(h.startTime+o.timestamp>(this.p+1e3)*1e3)break;h.decoder.decode(o),h.curChunk++}if(h.curChunk>=h.chunks.length&&!h.demuxer&&h.decoder){const o=h.decoder;o.flush().then(()=>{o.close(),h.done=!0}),h.decoder=null,this.O&&s.push(this.Z(h))}};for(let s=0;s<this.a.length;s++)e(this.a,s);for(let s=0;s<this.u.length;s++)e(this.u,s);const i=s=>s?!s.demuxer&&s.done&&s.frames.length==1:!1;i(this.a[0])&&i(this.u[0])&&(this.a.shift(),this.u.shift(),this.X=!0),super.S(t)}I(){this.t=document.createElement("video"),this.t.style.display="block",this.t.classList.add("cover-video"),this.t.muted=this.N,this.t.autoplay=this.d,this.t.loop=this.O,this.t.src=this.getAttribute("color-src"),this.e.parentElement.appendChild(this.t)}H(){const t=this.a[0],e=this.u[0];if(!t||!e)return;const i=this.p*1e3;let s=this.J??Number.MAX_VALUE,r=-1;for(let h=0;h<Math.min(t.frames.length,e.frames.length);h++){let o=Math.abs(i-(t.frames[h].timestamp+t.startTime));if(o<s)s=o,r=h;else break}if(!(r<0)){this.X&&(this.t.close(),this.h.close(),this.X=!1),this.t=t.frames[r],this.h=e.frames[r],this.J=this.t?.timestamp,this.t?.timestamp!==this.h?.timestamp&&console.error("Color and depth frames have mismatched PTS"),this.c("color",this.t),this.c("depth",this.h);for(let h=0;h<r;h++)t.frames.shift().close(),e.frames.shift().close()}}P(t){return t instanceof VideoFrame}f(t){return t?t.codedWidth:1}m(t){return t?t.codedHeight:1}B(t){t.decoder?.close(),t.decoder=null;for(let e=0;e<t.frames.length;e++)t.frames[e].close()}Y(){for(let t=0;t<this.a.length;t++)this.B(this.a[t]);for(let t=0;t<this.u.length;t++)this.B(this.u[t]);this.p=0}}p=M,customElements.define("rgbd-img",m),customElements.define("rgbd-video",p)}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rgbd",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "type": "module",
5
5
  "main": "dist/rgbd.js",
6
6
  "module": "dist/rgbd.js",
@@ -11,12 +11,15 @@
11
11
  "dist"
12
12
  ],
13
13
  "scripts": {
14
- "build": "esbuild src/rgbd.js --minify --outfile=dist/rgbd.js --format=esm",
15
- "dev": "npx serve .",
14
+ "build": "esbuild src/rgbd.js --minify --outfile=dist/rgbd.js --format=esm --mangle-props=^_",
15
+ "dev": "vite",
16
16
  "prepare": "npm run build"
17
17
  },
18
18
  "devDependencies": {
19
19
  "esbuild": "^0.27.0",
20
- "serve": "^14.2.5"
20
+ "vite": "^7.2.4"
21
+ },
22
+ "dependencies": {
23
+ "mp4box": "^0.5.4"
21
24
  }
22
25
  }