(function(c,E){typeof exports=="object"&&typeof module<"u"?E(exports):typeof define=="function"&&define.amd?define(["exports"],E):(c=typeof globalThis<"u"?globalThis:c||self,E(c["panorama-renderer"]={}))})(this,function(c){"use strict";class E{gl;_program;vs;fs;initialized=!1;ext;type=0;vsSource="";fsSource="";uniformLocations={};attributeLocations={};_shaderCompiled=!1;constructor(t,e){this.gl=t;const s=t.context;this.ext=s.getExtension("KHR_parallel_shader_compile"),this._program=s.createProgram(),this.vs=s.createShader(s.VERTEX_SHADER),this.fs=s.createShader(s.FRAGMENT_SHADER),this.type=this.detectType(e),this.vsSource=this.getVertexShader(this.type),s.shaderSource(this.vs,this.vsSource),s.compileShader(this.vs),this.fsSource=`${this.getFragmentShader(this.type)}${e}`,s.shaderSource(this.fs,this.fsSource),s.compileShader(this.fs),s.attachShader(this._program,this.vs),s.attachShader(this._program,this.fs),s.linkProgram(this._program)}get program(){if(this.initialized)return this._program;this.initialized=!0;const t=this.gl.context;let e=t.getShaderParameter(this.vs,t.COMPILE_STATUS);if(!e)throw console.table(this.vsSource.split(` `)),new Error(`ImageEffectRenderer: Vertex shader compilation failed: ${t.getShaderInfoLog(this.vs)}`);if(e=t.getShaderParameter(this.fs,t.COMPILE_STATUS),!e)throw console.table(this.fsSource.split(` `)),new Error(`ImageEffectRenderer: Shader compilation failed: ${t.getShaderInfoLog(this.fs)}`);if(e=t.getProgramParameter(this._program,t.LINK_STATUS),!e)throw new Error(`ImageEffectRenderer: Program linking failed: ${t.getProgramInfoLog(this._program)}`);return this._program}get shaderCompiled(){return this._shaderCompiled=this._shaderCompiled||!this.ext||this.gl.context.getProgramParameter(this._program,this.ext.COMPLETION_STATUS_KHR),this._shaderCompiled}use(){this.gl.context.useProgram(this.program)}getUniformLocation(t){return this.uniformLocations[t]!==void 0?this.uniformLocations[t]:this.uniformLocations[t]=this.gl.context.getUniformLocation(this._program,t)}getAttributeLocation(t){return this.attributeLocations[t]!==void 0?this.attributeLocations[t]:(this.gl.context.useProgram(this.program),this.attributeLocations[t]=this.gl.context.getAttribLocation(this._program,t))}detectType(t){const e=/mainImage/gmi,s=/^#version[\s]+300[\s]+es[\s]+/gmi;return e.exec(t)?this.gl.isWebGL2?1:0:s.exec(t)?3:2}getFragmentShader(t){switch(t){case 0:return`precision highp float; ${this.getUniformShader()} varying vec2 vUV0; void mainImage(out vec4, vec2); vec4 texture(sampler2D tex, vec2 uv) { return texture2D(tex, uv); } void main(void) { gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); mainImage(gl_FragColor, vUV0 * iResolution.xy); } `;case 1:return`#version 300 es precision highp float; ${this.getUniformShader()} in vec2 vUV0; out vec4 outFragColor; void mainImage(out vec4, vec2); vec4 texture2D(sampler2D tex, vec2 uv) { return texture(tex, uv); } void main(void) { outFragColor = vec4(0.0, 0.0, 0.0, 1.0); mainImage(outFragColor, vUV0 * iResolution.xy); } `;default:return""}}getVertexShader(t){const e=`#version 300 es in vec3 aPos; in vec2 aUV; uniform float iAspect; out vec2 vScreen; out vec2 vUV0; void main(void) { vUV0 = aUV; vScreen = aPos.xy; vScreen.x *= iAspect; gl_Position = vec4(aPos, 1.0); }`,s=`attribute vec3 aPos; attribute vec2 aUV; uniform float iAspect; varying vec2 vScreen; varying vec2 vUV0; void main(void) { vUV0 = aUV; vScreen = aPos.xy; vScreen.x *= iAspect; gl_Position = vec4(aPos, 1.0); }`,r=`attribute vec2 aPos; attribute vec2 aUV; varying vec2 vUV0; void main(void) { vUV0 = aUV; gl_Position = vec4(aPos, 0.0, 1.0); } `,o=`#version 300 es in vec2 aPos; in vec2 aUV; out vec2 vUV0; void main(void) { vUV0 = aUV; gl_Position = vec4(aPos, 0.0, 1.0); } `;switch(t){case 0:return r;case 1:return o;case 2:return s;case 3:default:return e}}getUniformShader(){return` uniform vec2 iResolution; uniform float iTime; uniform float iGlobalTime; uniform float iAspect; uniform int iFrame; uniform vec4 iMouse; uniform highp sampler2D iChannel0; uniform highp sampler2D iChannel1; uniform highp sampler2D iChannel2; uniform highp sampler2D iChannel3; uniform highp sampler2D iChannel4; uniform highp sampler2D iChannel5; uniform highp sampler2D iChannel6; uniform highp sampler2D iChannel7; uniform vec2 iChannelResolution0; uniform vec2 iChannelResolution1; uniform vec2 iChannelResolution2; uniform vec2 iChannelResolution3; uniform vec2 iChannelResolution4; uniform vec2 iChannelResolution5; uniform vec2 iChannelResolution6; uniform vec2 iChannelResolution7; `}}var p=(i=>(i[i.INT=0]="INT",i[i.FLOAT=1]="FLOAT",i[i.VEC2=2]="VEC2",i[i.VEC3=3]="VEC3",i[i.VEC4=4]="VEC4",i[i.MATRIX=5]="MATRIX",i))(p||{});class ft{type;name;x=0;y=0;z=0;w=0;matrix;constructor(t,e){this.type=t,this.name=e}}class J{isWebGL2=!0;context;canvas;quadVBO;lastQuadVBO=void 0;sharedPrograms={};sharedTextures={};constructor(t=void 0){this.canvas=t||document.createElement("canvas");const e={premultipliedAlpha:!0,alpha:!0,preserveDrawingBuffer:!1,antialias:!1,depth:!1,stencil:!1};this.context=this.canvas.getContext("webgl2",e),this.context||(this.context=this.canvas.getContext("webgl",e),this.isWebGL2=!1),this.context.getExtension("WEBGL_color_buffer_float"),this.context.getExtension("EXT_color_buffer_float"),this.context.getExtension("OES_texture_float"),this.context.getExtension("OES_texture_float_linear"),this.context.getExtension("KHR_parallel_shader_compile"),this.context.clearColor(0,0,0,0),this.context.clear(this.context.COLOR_BUFFER_BIT),this.context.enable(this.context.BLEND),this.context.blendFunc(this.context.ONE,this.context.ONE_MINUS_SRC_ALPHA),this.quadVBO=this.generateQuad()}generateQuad(){const t=this.context,e=new Float32Array([-1,1,0,1,-1,-1,0,0,1,1,1,1,1,-1,1,0]),s=t.createBuffer();return t.bindBuffer(t.ARRAY_BUFFER,s),t.bufferData(t.ARRAY_BUFFER,e,t.STATIC_DRAW),s}drawQuad(t,e){const s=this.context;this.lastQuadVBO!==this.quadVBO&&(this.lastQuadVBO=this.quadVBO,s.bindBuffer(s.ARRAY_BUFFER,this.quadVBO),s.enableVertexAttribArray(t),s.vertexAttribPointer(t,2,s.FLOAT,!1,4*4,0),s.enableVertexAttribArray(e),s.vertexAttribPointer(e,2,s.FLOAT,!1,4*4,2*4)),s.drawArrays(s.TRIANGLE_STRIP,0,4)}getCachedTexture(t,e){const s=`${t}_${e.clampX}_${e.clampY}_${e.useMipmap}`;return this.sharedTextures[t]?this.sharedTextures[s]:this.sharedTextures[s]=this.context.createTexture()}compileShader(t){return this.sharedPrograms[t]?this.sharedPrograms[t]:this.sharedPrograms[t]=new E(this,t)}setTextureParameter(t,e){const s=this.context;s.bindTexture(s.TEXTURE_2D,t),s.texParameteri(s.TEXTURE_2D,s.TEXTURE_WRAP_S,e.clampX?s.CLAMP_TO_EDGE:s.REPEAT),s.texParameteri(s.TEXTURE_2D,s.TEXTURE_WRAP_T,e.clampY?s.CLAMP_TO_EDGE:s.REPEAT),s.texParameteri(s.TEXTURE_2D,s.TEXTURE_MAG_FILTER,e.magFilterLinear?s.LINEAR:s.NEAREST),e.useMipmap?(s.texParameteri(s.TEXTURE_2D,s.TEXTURE_MIN_FILTER,s.LINEAR_MIPMAP_LINEAR),s.generateMipmap(s.TEXTURE_2D)):s.texParameteri(s.TEXTURE_2D,s.TEXTURE_MIN_FILTER,e.minFilterLinear?s.LINEAR:s.NEAREST)}bindTextures(t){const e=this.context;for(let s=0;s<8;s++){e.activeTexture(e.TEXTURE0+s);const r=t[s];r&&r.buffer?e.bindTexture(e.TEXTURE_2D,r.buffer.src.texture):r&&r.texture?e.bindTexture(e.TEXTURE_2D,r.texture):e.bindTexture(e.TEXTURE_2D,null)}}setUniforms(t,e){const s=this.context;Object.values(t).forEach(r=>{const o=e.getUniformLocation(r.name);if(o!==null)switch(r.type){case p.INT:s.uniform1i(o,r.x);break;case p.FLOAT:s.uniform1f(o,r.x);break;case p.VEC2:s.uniform2f(o,r.x,r.y);break;case p.VEC3:s.uniform3f(o,r.x,r.y,r.z);break;case p.VEC4:s.uniform4f(o,r.x,r.y,r.z,r.w);break;case p.MATRIX:s.uniformMatrix4fv(o,!1,r.matrix);break}})}}class R{width=0;height=0;program;main;gl;frame=0;static defaultImageOptions={clampX:!0,clampY:!0,flipY:!1,useMipmap:!0,useCache:!0,minFilterLinear:!0,magFilterLinear:!0};uniforms={};textures=[];constructor(t){this.gl=t}setImage(t,e,s={}){if(t>=8)throw new Error("ImageEffectRenderer: A maximum of 8 slots is available, slotIndex is out of bounds.");this.setUniformInt(`iChannel${t}`,t),this.setUniformVec2(`iChannelResolution${t}`,e.width,e.height);const r=this.gl.context,o=this.textures[t];if(e instanceof R){o&&o.texture&&!o.cached&&r.deleteTexture(o.texture);const n={...e.options,...s};this.textures[t]={texture:void 0,buffer:e,cached:!1},this.gl.setTextureParameter(e.src.texture,n),this.gl.setTextureParameter(e.dest.texture,n)}else{const n={...R.defaultImageOptions,...s};n.useCache=n.useCache&&e instanceof HTMLImageElement,n.useCache&&o&&o.texture&&!o.cached&&(r.deleteTexture(o.texture),o.texture=void 0);let a=o&&o.texture;n.useCache&&e instanceof HTMLImageElement&&(a=this.gl.getCachedTexture(e.src,n)),a||(a=r.createTexture()),this.textures[t]={texture:a,buffer:void 0,cached:n.useCache},r.bindTexture(r.TEXTURE_2D,a),r.pixelStorei(r.UNPACK_FLIP_Y_WEBGL,s.flipY?1:0),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,r.RGBA,r.UNSIGNED_BYTE,e),this.gl.setTextureParameter(a,n)}}setUniformFloat(t,e){this.setUniform(t,p.FLOAT,e,0,0,0,void 0)}setUniformInt(t,e){this.setUniform(t,p.INT,e,0,0,0,void 0)}setUniformVec2(t,e,s){this.setUniform(t,p.VEC2,e,s,0,0,void 0)}setUniformVec3(t,e,s,r){this.setUniform(t,p.VEC3,e,s,r,0,void 0)}setUniformVec4(t,e,s,r,o){this.setUniform(t,p.VEC4,e,s,r,o,void 0)}setUniformMatrix(t,e){this.setUniform(t,p.MATRIX,0,0,0,0,e)}draw(t=0,e,s){this.width=e|0,this.height=s|0,this.program.use(),this.setUniformFloat("iGlobalTime",t),this.setUniformFloat("iTime",t),this.setUniformInt("iFrame",this.frame),this.setUniformFloat("iAspect",e/s),this.setUniformVec2("iResolution",e,s),this.gl.setUniforms(this.uniforms,this.program),this.gl.bindTextures(this.textures),this.gl.drawQuad(this.program.getAttributeLocation("aPos"),this.program.getAttributeLocation("aUV")),this.frame++}get shaderCompiled(){return this.program.shaderCompiled}setUniform(t,e,s,r,o,n,a){let h=this.uniforms[t];h||(h=this.uniforms[t]=new ft(e,t)),h.x=s,h.y=r,h.z=o,h.w=n,h.matrix=a}destruct(){this.textures.forEach(t=>t.texture&&!t.cached&&this.gl.context.deleteTexture(t.texture)),this.textures=[],this.uniforms={}}}class q{width=0;height=0;texture;frameBuffer;gl;format=WebGLRenderingContext.RGBA;internalFormat=WebGLRenderingContext.RGBA;type=WebGLRenderingContext.UNSIGNED_BYTE;constructor(t,e=WebGLRenderingContext.UNSIGNED_BYTE){if(this.gl=t,this.type=e,t.isWebGL2)switch(e){case WebGLRenderingContext.UNSIGNED_BYTE:this.internalFormat=WebGL2RenderingContext.RGBA8;break;case WebGLRenderingContext.FLOAT:this.internalFormat=WebGL2RenderingContext.RGBA32F;break}else this.internalFormat=this.format;const s=t.context;this.texture=s.createTexture(),this.resize(16,16),this.frameBuffer=s.createFramebuffer(),s.bindFramebuffer(s.FRAMEBUFFER,this.frameBuffer),s.framebufferTexture2D(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0,s.TEXTURE_2D,this.texture,0),s.bindFramebuffer(s.FRAMEBUFFER,null)}resize(t,e){if(this.width===(t|0)&&this.height===(e|0))return;this.width=t|0,this.height=e|0;const s=this.gl.context;s.bindTexture(s.TEXTURE_2D,this.texture),s.pixelStorei(s.UNPACK_FLIP_Y_WEBGL,0),this.gl.isWebGL2?s.texImage2D(s.TEXTURE_2D,0,this.internalFormat,this.width,this.height,0,this.format,this.type,null):s.texImage2D(s.TEXTURE_2D,0,this.format,this.width,this.height,0,this.format,this.type,null)}destruct(){const t=this.gl.context;this.frameBuffer&&t.deleteFramebuffer(this.frameBuffer),this.texture&&t.deleteTexture(this.texture)}}class W extends R{static defaultBufferOptions={...R.defaultImageOptions,useMipmap:!1,useCache:!1,type:5121};options;frameBuffer0;frameBuffer1;constructor(t,e={}){super(t),this.options={...W.defaultBufferOptions,...e},this.frameBuffer0=new q(t,this.options.type),this.frameBuffer1=new q(t,this.options.type)}draw(t=0,e,s){if(e<=0||s<=0)return;const r=this.gl.context,o=this.dest;o.resize(e,s),r.bindFramebuffer(r.FRAMEBUFFER,o.frameBuffer),r.clear(r.COLOR_BUFFER_BIT),super.draw(t,e,s),r.bindFramebuffer(r.FRAMEBUFFER,null)}get src(){return this.frame%2===0?this.frameBuffer0:this.frameBuffer1}get dest(){return this.frame%2===1?this.frameBuffer0:this.frameBuffer1}destruct(){super.destruct(),this.frameBuffer0.destruct(),this.frameBuffer1.destruct()}}class B extends R{static index=0;canvas;buffers=[];options;time=0;index;tickFuncs=[];readyFuncs=[];startTime=-1;drawOneFrame=!1;container;animationRequestId=0;resizeObserver;_ready=!1;constructor(t,e,s,r){if(super(t),this.options={...r},this.index=B.index++,this.container=e,this.main=this,this.options.useSharedContext){this.canvas=document.createElement("canvas");const o=this.canvas.getContext("2d");o.fillStyle="#00000000",o.clearRect(0,0,this.canvas.width,this.canvas.height)}else this.canvas=this.gl.canvas;this.canvas.style.inset="0",this.canvas.style.width="100%",this.canvas.style.height="100%",this.canvas.style.margin="0",this.canvas.style.display="block",this.container.appendChild(this.canvas),this.program=new E(this.gl,s),this.resizeObserver=new ResizeObserver(()=>{this.options.autoResize&&this.updateSize()}),this.resizeObserver.observe(e),this.options.useSharedContext||this.drawingLoop(0)}play(){this.options.loop=!0}stop(){this.options.loop=!1}createBuffer(t,e,s={}){const r=this.buffers[t];r&&r.destruct();const o=new W(this.gl,s);return o.program=this.gl.compileShader(e),o.main=this,this.buffers[t]=o}tick(t){this.tickFuncs.push(t)}ready(t){this.readyFuncs.push(t)}drawFrame(t=0){this.time=t/1e3,this.drawOneFrame=!0}get drawThisFrame(){return(this.options.loop||this.drawOneFrame)&&this.width>0&&this.height>0&&(!this.options.asyncCompile||this.allShadersCompiled)}drawInstance(t){const e=this.gl.context;this.drawOneFrame||(this.time+=t),this.tickFuncs.forEach(s=>s(t)),this.buffers.forEach(s=>{s&&(e.viewport(0,0,this.width,this.height),s.draw(this.time,this.canvas.width,this.canvas.height))}),e.viewport(0,0,this.width,this.height),e.clear(e.COLOR_BUFFER_BIT),this.draw(this.time,this.canvas.width,this.canvas.height),this.drawOneFrame=!1}get allShadersCompiled(){return this.shaderCompiled&&this.buffers.every(t=>t&&t.shaderCompiled)}update(t){this.allShadersCompiled&&(this._ready||(this._ready=!0,this.readyFuncs.forEach(e=>e()),this.readyFuncs=[]))}updateSize(){this.width=this.container.offsetWidth*this.options.pixelRatio|0,this.height=this.container.offsetHeight*this.options.pixelRatio|0,(this.width!==this.canvas.width||this.height!==this.canvas.height)&&(this.canvas.width=this.width,this.canvas.height=this.height,this.drawOneFrame=!0)}drawingLoop(t=0){this.animationRequestId=window.requestAnimationFrame(s=>this.drawingLoop(s)),t/=1e3;const e=this.startTime<0?1/60:t-this.startTime;this.startTime=t>0?t:-1,this.update(e),this.drawThisFrame&&this.drawInstance(e)}destruct(){cancelAnimationFrame(this.animationRequestId),super.destruct(),this.resizeObserver.disconnect(),this.container.removeChild(this.canvas),this.canvas.replaceWith(this.canvas.cloneNode(!0)),this.buffers.forEach(t=>{t.destruct()}),this.buffers=[],this.tickFuncs=[]}copyCanvas(){const t=this.gl.canvas,e=this.canvas.getContext("2d");e.clearRect(0,0,this.width,this.height),e.drawImage(t,0,t.height-this.height,this.width,this.height,0,0,this.width,this.height)}}class l{static defaultOptions={loop:!1,autoResize:!0,pixelRatio:typeof window<"u"?window.devicePixelRatio:1,useSharedContext:!1,asyncCompile:!0};static poolInUse=[];static poolWebGLInstance=[];static sharedInstance;static sharedTime=-1;constructor(){throw new Error("Use ImageEffectRenderer.createTemporary to create an ImageEffectRenderer")}static createTemporary(t,e,s={}){const r={...l.defaultOptions,...s};if(r.useSharedContext){l.sharedInstance||(l.sharedInstance=new J,this.drawInstances(0));const o=new B(l.sharedInstance,t,e,r);return this.poolInUse.push(o),o}else{const o=l.poolWebGLInstance.pop()||new J;return new B(o,t,e,r)}}static releaseTemporary(t){t.options.useSharedContext||this.poolWebGLInstance.push(t.gl),t.stop(),t.destruct();const e=l.poolInUse.indexOf(t);e>-1&&l.poolInUse.splice(e,1)}static drawInstances(t=0){window.requestAnimationFrame(h=>this.drawInstances(h)),t/=1e3;const e=l.sharedTime<0?1/60:t-l.sharedTime;l.sharedTime=t;const s=l.sharedInstance.canvas,r=l.sharedInstance.context,o=l.poolInUse;let n=0,a=0;o.forEach(h=>{h.update(e)}),o.forEach(h=>{h.drawThisFrame&&(n=Math.max(n,h.width),a=Math.max(a,h.height))}),(n>s.width||a>s.height)&&(s.width=n,s.height=a),r.clear(r.COLOR_BUFFER_BIT),o.forEach(h=>{h.drawThisFrame&&(h.drawInstance(e),h.copyCanvas())})}}class Y{press=!1;down=!1;downTime=0}class mt{canvas;mousePos={x:0,y:0};previousMousePos={x:0,y:0};mouseVelocity={x:0,y:0};normalized={x:0,y:0};mouseClickCallbacks=[];buttons=[new Y,new Y,new Y];resetSpeed=!1;constructor(t){this.canvas=t,this.canvas.addEventListener("touchstart",this.onMouseStart.bind(this),!1),this.canvas.addEventListener("touchmove",this.touchMoveListener.bind(this),!1),this.canvas.addEventListener("touchend",this.endListener.bind(this),!1),this.canvas.addEventListener("touchcancel",this.endListener.bind(this),!1),this.canvas.addEventListener("mousedown",this.onMouseStart.bind(this),!1),this.canvas.addEventListener("mousemove",this.mouseMoveListener.bind(this),!1),this.canvas.addEventListener("mouseup",this.endListener.bind(this),!1),this.canvas.addEventListener("mousecancel",this.endListener.bind(this),!1),this.canvas.addEventListener("mouseout",this.endListener.bind(this),!1)}get normalizedVelocity(){return{...this.mouseVelocity}}get mouseDown(){return this.buttons[0].press}click(t){this.mouseClickCallbacks.push(t)}update(t){this.normalized.x=this.mousePos.x/this.canvas.clientWidth,this.normalized.y=this.mousePos.y/this.canvas.clientHeight,this.resetSpeed?(this.resetSpeed=!1,this.mouseVelocity.x=0,this.mouseVelocity.y=0):(this.mouseVelocity.x=this.normalized.x-this.previousMousePos.x,this.mouseVelocity.y=this.normalized.y-this.previousMousePos.y),this.previousMousePos.x=this.normalized.x,this.previousMousePos.y=this.normalized.y,this.buttons.forEach(e=>{e.down=!1,e.press?(e.downTime===0&&(e.down=!0),e.downTime+=t):e.downTime=0})}destruct(){this.canvas&&(this.canvas.removeEventListener("touchstart",this.onMouseStart,!1),this.canvas.removeEventListener("touchmove",this.touchMoveListener,!1),this.canvas.removeEventListener("touchend",this.endListener,!1),this.canvas.removeEventListener("touchcancel",this.endListener,!1),this.canvas.removeEventListener("mousedown",this.onMouseStart,!1),this.canvas.removeEventListener("mousemove",this.mouseMoveListener,!1),this.canvas.removeEventListener("mouseend",this.endListener,!1),this.canvas.removeEventListener("mousecancel",this.endListener,!1),this.canvas.removeEventListener("mouseout",this.endListener,!1))}touchMoveListener(t){this.setMouse(t.targetTouches[0])}mouseMoveListener(t){this.setMouse(t)}endListener(){this.buttons[0].press=!1}onMouseStart(t){t.preventDefault();let e=!1;t instanceof TouchEvent?(e=!0,this.setMouse(t.targetTouches[0])):this.setMouse(t),this.resetSpeed=!0,this.buttons[e?0:t.which-1].press=!0,this.mouseClickCallbacks.forEach(s=>{s()})}setMouse(t){this.mousePos.x=t.pageX,this.mousePos.y=t.pageY}}function pt(i,t,e){return Math.max(t,Math.min(e,i))}function tt(i){return Math.max(0,Math.min(1,i))}function O(i,t,e){return i+(t-i)*e}function et(i){return i=tt(i),i*i*(3-2*i)}const xt=1e-9;function vt(i,t){return{x:i.x+t.x,y:i.y+t.y,z:i.z+t.z}}function st(i,t){return{x:i.x-t.x,y:i.y-t.y,z:i.z-t.z}}function j(i,t){return{x:i.y*t.z-i.z*t.y,y:i.z*t.x-i.x*t.z,z:i.x*t.y-i.y*t.x}}function X(i){const t=Math.sqrt(i.x*i.x+i.y*i.y+i.z*i.z);return{x:i.x/t,y:i.y/t,z:i.z/t}}function H(i,t){const e=t[0]*i.x+t[4]*i.y+t[8]*i.z+t[12]*i.w,s=t[1]*i.x+t[5]*i.y+t[9]*i.z+t[13]*i.w,r=t[2]*i.x+t[6]*i.y+t[10]*i.z+t[14]*i.w,o=t[3]*i.x+t[7]*i.y+t[11]*i.z+t[15]*i.w;return{x:e,y:s,z:r,w:o}}function it(i,t,e,s){const r=1/Math.tan(i/2);let o;const n=Array(16).fill(0);return n[0]=r/t,n[5]=r,n[11]=-1,s!==null&&s!==1/0?(o=1/(e-s),n[10]=(s+e)*o,n[14]=2*s*e*o):(n[10]=-1,n[14]=-2*e),n}function rt(i,t){const e=Array(16).fill(0);for(let s=0;s<4;s++)for(let r=0;r<4;r++)for(let o=0;o<4;o++)e[s*4+r]+=i[s*4+o]*t[o*4+r];return e}function L(){return[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]}function ot(i){const t=Array(16).fill(0),[e,s,r,o,n,a,h,m,d,f,x,v,g,w,y,T]=i,I=e*a-s*n,U=e*h-r*n,z=e*m-o*n,M=s*h-r*a,S=s*m-o*a,b=r*m-o*h,F=d*w-f*g,A=d*y-x*g,P=d*T-v*g,_=f*y-x*w,V=f*T-v*w,D=x*T-v*y;let u=I*D-U*V+z*_+M*P-S*A+b*F;return u&&(u=1/u,t[0]=(a*D-h*V+m*_)*u,t[1]=(r*V-s*D-o*_)*u,t[2]=(w*b-y*S+T*M)*u,t[3]=(x*S-f*b-v*M)*u,t[4]=(h*P-n*D-m*A)*u,t[5]=(e*D-r*P+o*A)*u,t[6]=(y*z-g*b-T*U)*u,t[7]=(d*b-x*z+v*U)*u,t[8]=(n*V-a*P+m*F)*u,t[9]=(s*P-e*V-o*F)*u,t[10]=(g*S-w*z+T*I)*u,t[11]=(f*z-d*S-v*I)*u,t[12]=(a*A-n*_-h*F)*u,t[13]=(e*_-s*A+r*F)*u,t[14]=(w*U-g*M-y*I)*u,t[15]=(d*M-f*U+x*I)*u),t}function nt(i,t,e){const s=X(st(i,t)),r=X(j(e,s)),o=X(j(s,r));return[r.x,o.x,s.x,r.y,o.y,s.y,r.z,o.z,s.z]}function Q(i){const t=i[0]+i[4]+i[8];let e,s,r,o;if(t>0){let n=Math.sqrt(t+1);o=.5*n,n=.5/n,e=(i[5]-i[7])*n,s=(i[6]-i[2])*n,r=(i[1]-i[3])*n}else{let n=0;i[4]>i[0]&&(n=1),i[8]>i[n*3+n]&&(n=2);const a=(n+1)%3,h=(n+2)%3,m=Math.sqrt(i[n*3+n]-i[a*3+a]-i[h*3+h]+1),d=.5*m,f=.5/m;o=(i[a*3+h]-i[h*3+a])*f;const x=(i[a*3+n]+i[n*3+a])*f,v=(i[h*3+n]+i[n*3+h])*f;e=n==0?d:a==0?x:v,s=n==1?d:a==1?x:v,r=n==2?d:a==2?x:v}return{x:e,y:s,z:r,w:o}}function $(i){return[i[0],i[1],i[2],0,i[3],i[4],i[5],0,i[6],i[7],i[8],0,0,0,0,1]}function C(){return{w:1,x:0,y:0,z:0}}function N(i,t){const e=i.w*t.w-i.x*t.x-i.y*t.y-i.z*t.z,s=i.w*t.x+i.x*t.w+i.y*t.z-i.z*t.y,r=i.w*t.y-i.x*t.z+i.y*t.w+i.z*t.x,o=i.w*t.z+i.x*t.y-i.y*t.x+i.z*t.w;return{w:e,x:s,y:r,z:o}}function at(i,t){const e=Math.sin(t/2),r={w:Math.cos(t/2),x:e,y:0,z:0};return N(i,r)}function ht(i,t){const e=Math.sin(t/2),r={w:Math.cos(t/2),x:0,y:e,z:0};return N(i,r)}function ct(i,t){const e=Math.sin(t/2),r={w:Math.cos(t/2),x:0,y:0,z:e};return N(i,r)}function K(i){const t=Z(i),e=t[0],s=t[3];t[6];const r=t[1],o=t[4];t[7];const n=t[2],a=t[5],h=t[8];let m=0,d=0,f=0;return m=Math.asin(pt(a,-1,1)),Math.abs(a)<.9999999?(d=Math.atan2(-n,h),f=Math.atan2(-s,o)):(d=0,f=Math.atan2(r,e)),{x:m,y:d,z:f}}function ut(i){let t=C();return t=at(t,i.x),t=ht(t,i.y),t=ct(t,i.z),t}function lt(i){const t=Math.sqrt(i.w*i.w+i.x*i.x+i.y*i.y+i.z*i.z);return t===0?{w:1,x:0,y:0,z:0}:{w:i.w/t,x:i.x/t,y:i.y/t,z:i.z/t}}function dt(i,t,e){let s=i.x*t.x+i.y*t.y+i.z*t.z+i.w*t.w;s<0&&(s=-s,t.x=-t.x,t.y=-t.y,t.z=-t.z,t.w=-t.w);let r,o;if(1-s>xt){const n=Math.acos(s),a=Math.sin(n);r=Math.sin((1-e)*n)/a,o=Math.sin(e*n)/a}else r=1-e,o=e;return lt({x:r*i.x+o*t.x,y:r*i.y+o*t.y,z:r*i.z+o*t.z,w:r*i.w+o*t.w})}function Z(i){const t=Array(9).fill(0),{x:e,y:s,z:r,w:o}=i,n=e+e,a=s+s,h=r+r,m=e*n,d=s*n,f=s*a,x=r*n,v=r*a,g=r*h,w=o*n,y=o*a,T=o*h;return t[0]=1-f-g,t[3]=d-T,t[6]=x+y,t[1]=d+T,t[4]=1-m-g,t[7]=v-w,t[2]=x-y,t[5]=v+w,t[8]=1-m-f,t}class G{static defaultOptions={inertia:.5,slowDownTime:.5,clampXRotation:[-.5,.5],clampYRotation:void 0,userInteractions:!0};options={...G.defaultOptions};renderer;mouseListener;lastUserRotateSpeed={x:0,y:0};currentRotateSpeed={x:0,y:0};slowDownTimer=0;euler={x:0,y:0,z:0};init(t,e){this.renderer=t,this.options={...G.defaultOptions,...e},this.mouseListener=new mt(this.renderer.canvas)}update(t,e,s){this.mouseListener.update(t);const r=this.renderer.aspectRatio,o=.5/Math.tan(this.renderer.fov*.5),n=Math.atan2(r*.5,o)*2;if(this.mouseListener.mouseDown){const h=this.mouseListener.normalizedVelocity;this.lastUserRotateSpeed.x=O(-h.x*n*(1/t),this.currentRotateSpeed.x,this.options.inertia),this.lastUserRotateSpeed.y=O(h.y*this.renderer.fov*(1/t),this.currentRotateSpeed.y,this.options.inertia),this.slowDownTimer=this.options.slowDownTime}const a=this.options.slowDownTime>0?this.slowDownTimer/this.options.slowDownTime:0;if(this.currentRotateSpeed.x=O(0,this.lastUserRotateSpeed.x,a),this.currentRotateSpeed.y=O(0,this.lastUserRotateSpeed.y,a),this.slowDownTimer=Math.max(0,this.slowDownTimer-t),this.options.userInteractions&&!s){this.euler=K(e);const h=this.euler;return h.x-=this.currentRotateSpeed.y*t,h.y+=this.currentRotateSpeed.x*t,h.z=0,this.options.clampXRotation&&(h.x=Math.min(Math.max(h.x,this.options.clampXRotation[0]),this.options.clampXRotation[1])),this.options.clampYRotation&&(h.y=Math.min(Math.max(h.y,this.options.clampYRotation[0]),this.options.clampYRotation[1])),ut(h)}else return this.euler=K(e),e}destruct(){this.mouseListener.destruct()}}class k{options;static defaultOptions={loop:!0,fov:1,barrelDistortion:.1,shader:!1,renderer:!1,rotationController:!1,controlledRendererInstance:!1,rotationControllerOptions:{}};renderer;transitionProgress=1;transitionDuration=1;transitionEase=et;rotationController;rotation=C();rotationStart=C();rotationEnd=C();projection=L();view=L();viewProjection=L();invViewProjection=L();constructor(t,e,s={}){this.options={...k.defaultOptions,...s},this.options.renderer!==!1?this.renderer=this.options.renderer:this.options.controlledRendererInstance?this.renderer=this.options.controlledRendererInstance.main:this.renderer=l.createTemporary(t,this.shader,{useSharedContext:!1,...s}),this.options.rotationController?this.rotationController=this.options.rotationController:this.rotationController=new G,this.rotationController.init(this,this.options.rotationControllerOptions),e&&this.setImage(0,e,{flipY:!0,clampX:!1,clampY:!0,useMipmap:!0}),this.renderer.tick(r=>this.drawingLoop(r))}get fov(){return this.options.fov}set fov(t){this.options.fov=t}get barrelDistortion(){return this.options.barrelDistortion}set barrelDistortion(t){this.options.barrelDistortion=t}get aspectRatio(){return this.canvas.width/this.canvas.height}tick(t){this.renderer.tick(t)}worldToScreen(t){let e={...t,w:1};e=H(e,this.viewProjection),e.x/=e.w,e.y/=e.w;const s=Math.sqrt(e.x*e.x+e.y*e.y),r=this.barrelDistortion;if(r*s>0){const o=Math.pow(9*r*r*s+Math.sqrt(3)*Math.sqrt(27*r*r*r*r*s*s+4*r*r*r),.3333333333333333);let n=o/(Math.pow(2,1/3)*Math.pow(3,2/3)*r);n-=Math.pow(2/3,1/3)/o;const a=n/s;e.x*=a,e.y*=a}return e.x=e.x*.5+.5,e.y=e.y*-.5+.5,{x:e.x,y:e.y,z:e.z}}lookAt(t,e=0,s=et){const r={...t};this.transitionEase=s;let o=nt({x:0,y:0,z:0},r,{x:0,y:1,z:0});this.view=$(o),e>0?(this.transitionProgress=0,this.rotationStart={...this.rotation},this.rotationEnd=Q(o)):this.rotation=Q(o)}get canvas(){return this.renderer.canvas}screenToWorld(t){let e=t.x*2-1,s=1-t.y;s=s*2-1;const r=e*e+s*s,o=1+this.barrelDistortion*r;e*=o,s*=o;const a=H({x:e,y:s,z:1,w:1},this.invViewProjection);return{x:a.x,y:a.y,z:a.z}}play(){this.renderer.play()}stop(){this.renderer.stop()}setImage(t,e,s={}){this.renderer.setImage(t,e,s)}drawingLoop(t){this.update(t),this.draw()}update(t){if(this.rotation=this.rotationController.update(t,this.rotation,this.transitionProgress<1),this.transitionProgress<1){this.transitionProgress+=t/this.transitionDuration;const e=this.transitionEase(tt(this.transitionProgress));this.rotation=dt(this.rotationStart,this.rotationEnd,e)}this.updateViewProjection()}draw(){const t=this.options.controlledRendererInstance?this.options.controlledRendererInstance:this.renderer;t.setUniformMatrix("uInvViewProjection",new Float32Array(this.invViewProjection)),t.setUniformFloat("uBarrelDistortion",this.barrelDistortion)}updateViewProjection(){this.projection=it(this.fov,this.aspectRatio,.01,100),this.view=$(Z(this.rotation)),this.viewProjection=rt(this.view,this.projection),this.invViewProjection=ot(this.viewProjection)}get shader(){return this.options.shader?this.options.shader:k.defaultShader}static defaultShader=` uniform mat4 uInvViewProjection; uniform float uBarrelDistortion; vec2 getEqUV(vec3 rd) { vec2 uv = vec2(atan(rd.z, rd.x), asin(rd.y)); uv *= vec2(0.15915494309, 0.31830988618); uv.y += 0.5; return fract(uv); } void mainImage( out vec4 c, vec2 p ) { vec2 uv = vUV0 * 2. - 1.; float r2 = dot(uv,uv); uv.xy *= 1.0 + uBarrelDistortion * r2; vec4 rd = vec4(uv, 1., 1.); rd = uInvViewProjection * rd; rd.xyz = normalize(rd.xyz); vec2 uv1 = getEqUV(rd.xyz); vec3 col1 = texture(iChannel0, uv1).xyz; vec2 uv2 = uv1; uv2.x = fract(uv2.x + 0.5) - 0.5; vec3 col2 = texture(iChannel0, uv2).xyz; c.xyz = mix(col1, col2, step(abs(uv2.x), 0.25)); c.w = 1.; }`;destruct(){this.renderer&&l.releaseTemporary(this.renderer)}}c.ImageEffectRenderer=l,c.PanoramaRenderer=k,c.eulerToQuat=ut,c.mat3LookAt=nt,c.mat3ToMat4=$,c.mat3ToQuat=Q,c.mat4Identity=L,c.mat4Inverse=ot,c.mat4Multiply=rt,c.mat4Perspective=it,c.quatIdentity=C,c.quatMultiply=N,c.quatNormalize=lt,c.quatRotateX=at,c.quatRotateY=ht,c.quatRotateZ=ct,c.quatSlerp=dt,c.quatToEuler=K,c.quatToMat3=Z,c.vec3Add=vt,c.vec3Cross=j,c.vec3Normalize=X,c.vec3Sub=st,c.vec4Transform=H,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});