function Curtains(t,e){return this.planes=[],this._drawStack=[],this._drawingEnabled=!0,this._forceRender=!1,this.container=document.getElementById(t||"canvas"),this.productionMode=e||!1,this.container?(this._init(),this):(this.productionMode||console.warn("You must specify a valid container ID"),void(this._onErrorCallback&&this._onErrorCallback()))}Curtains.prototype._init=function(){if(this.glCanvas=document.createElement("canvas"),this.glContext=this.glCanvas.getContext("webgl",{alpha:!0})||this.glCanvas.getContext("experimental-webgl"),!this.glContext)return this.productionMode||console.warn("WebGL context could not be created"),void(this._onErrorCallback&&this._onErrorCallback());var t=window.pixelRatio||1;this.setPixelRatio(t),this._loseContextExtension=this.glContext.getExtension("WEBGL_lose_context"),this._contextLostHandler=this._contextLost.bind(this),this.glCanvas.addEventListener("webglcontextlost",this._contextLostHandler,!1),this._contextRestoredHandler=this._contextRestored.bind(this),this.glCanvas.addEventListener("webglcontextrestored",this._contextRestoredHandler,!1),this._resizeHandler=this.resize.bind(this),window.addEventListener("resize",this._resizeHandler,!1),this._readyToDraw()},Curtains.prototype.setPixelRatio=function(t){this.pixelRatio=parseFloat(Math.max(t,1))||1,this.resize()},Curtains.prototype._setSize=function(){var t=this.container.getBoundingClientRect();this._boundingRect={width:t.width*this.pixelRatio,height:t.height*this.pixelRatio,top:t.top*this.pixelRatio,left:t.left*this.pixelRatio},this.glCanvas.style.width=Math.floor(this._boundingRect.width/this.pixelRatio)+"px",this.glCanvas.style.height=Math.floor(this._boundingRect.height/this.pixelRatio)+"px",this.glCanvas.width=Math.floor(this._boundingRect.width),this.glCanvas.height=Math.floor(this._boundingRect.height),this.glContext.viewport(0,0,this.glContext.drawingBufferWidth,this.glContext.drawingBufferHeight)},Curtains.prototype.resize=function(){this._setSize();for(var t=0;t0;)this.removePlane(this.planes[0]);var t=this,e=setInterval(function(){0===t.planes.length&&(clearInterval(e),t.glContext.clear(t.glContext.DEPTH_BUFFER_BIT|t.glContext.COLOR_BUFFER_BIT),window.cancelAnimationFrame(t._animationFrameID),window.removeEventListener("resize",t._resizeHandler,!1),t.glCanvas.removeEventListener("webglcontextlost",t._contextLostHandler,!1),t.glCanvas.removeEventListener("webglcontextrestored",t._contextRestoredHandler,!1),t.glContext&&t._loseContextExtension&&t._loseContextExtension.loseContext(),t.container.removeChild(t.glCanvas))},100)},Curtains.prototype._createPlane=function(t,e){return new Curtains.Plane(this,t,e)},Curtains.prototype.addPlane=function(t,e){return this.glContext?t&&0!==t.length?this._createPlane(t,e):(this.productionMode||console.warn("The html element you specified does not currently exists in the DOM"),this._onErrorCallback&&this._onErrorCallback(),!1):(this.productionMode||console.warn("Unable to create a plane. The WebGl context couldn't be created"),this._onErrorCallback&&this._onErrorCallback(),null)},Curtains.prototype.removePlane=function(t){t._canDraw=!1;for(var e,i=this._drawStack,r=0;r0&&this.loadSources(n);var s=[];for(o=0;o0&&this.loadSources(s);var h=[];for(o=0;o0&&this.loadSources(h),this._loadingManager.initSourcesToLoad=n.length+s.length+h.length}0!==this._loadingManager.initSourcesToLoad||a.productionMode||console.warn("This plane does not contain any image, video or canvas element. You may want to add some later with the loadSource() or loadSources() method.");var l=this;r=setInterval(function(){l._loadingManager.sourcesLoaded>=l._loadingManager.initSourcesToLoad&&(clearInterval(r),l._onReadyCallback&&l._onReadyCallback())},16)}},Curtains.Plane.prototype._setInitParams=function(t){var e=this._wrapper;this._canDraw=!1,this.alwaysDraw=t.alwaysDraw||!1,this._shouldDraw=!0,this._definition={width:parseInt(t.widthSegments)||1,height:parseInt(t.heightSegments)||1},!t.mimicCSS&&!1!==t.mimicCSS||e.productionMode||console.warn("mimicCSS property is deprecated since v2.0 as the planes will always copy their html elements sizes and positions."),this.imageCover=t.imageCover||!1,this.imageCover&&!e.productionMode&&console.warn("imageCover property is deprecated. Please use texture matrix in your shader instead."),this.autoloadSources=t.autoloadSources,null!==this.autoloadSources&&void 0!==this.autoloadSources||(this.autoloadSources=!0),this.crossOrigin=t.crossOrigin||"anonymous",this._fov=t._fov||75,this._shouldUseDepthTest=!0,t.uniforms||(e.productionMode||console.warn("You are setting a plane without uniforms, you won't be able to interact with it. Please check your addPlane method for : ",this.htmlElement),t.uniforms={}),this.uniforms={};var i=this;t.uniforms&&Object.keys(t.uniforms).map(function(e,r){var a=t.uniforms[e];i.uniforms[e]={name:a.name,type:a.type,value:a.value}})},Curtains.Plane.prototype._setupShaders=function(t){var e,i,r=this._wrapper,a=t.vertexShaderID||this.htmlElement.getAttribute("data-vs-id"),n=t.fragmentShaderID||this.htmlElement.getAttribute("data-fs-id");t.vertexShader||(a&&document.getElementById(a)?e=document.getElementById(a).innerHTML:(r.productionMode||console.warn("No vertex shader provided, will use a default one"),e="#ifdef GL_ES\nprecision mediump float;\n#endif\nattribute vec3 aVertexPosition;attribute vec2 aTextureCoord;uniform mat4 uMVMatrix;uniform mat4 uPMatrix;varying vec3 vVertexPosition;varying vec2 vTextureCoord;void main() {vTextureCoord = aTextureCoord;vVertexPosition = aVertexPosition;gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);}")),t.fragmentShader||(n&&document.getElementById(n)?i=document.getElementById(n).innerHTML:(r.productionMode||console.warn("No fragment shader provided, will use a default one"),i="#ifdef GL_ES\nprecision mediump float;\n#endif\nvarying vec3 vVertexPosition;varying vec2 vTextureCoord;void main( void ) {gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);}")),this._shaders={vertexShaderCode:t.vertexShader||e,fragmentShaderCode:t.fragmentShader||i}},Curtains.Plane.prototype._setupPlaneProgram=function(){var t=!0,e=this._wrapper,i=e.glContext;return this._program=i.createProgram(),this._shaders.vertexShader=e._createShader(this._shaders.vertexShaderCode,i.VERTEX_SHADER),this._shaders.fragmentShader=e._createShader(this._shaders.fragmentShaderCode,i.FRAGMENT_SHADER),this._shaders.vertexShader&&this._shaders.fragmentShader||e.productionMode||(e.productionMode||console.warn("Unable to find or compile the vertex or fragment shader"),this._onErrorCallback&&this._onErrorCallback(),t=!1),t&&(i.attachShader(this._program,this._shaders.vertexShader),i.attachShader(this._program,this._shaders.fragmentShader),i.linkProgram(this._program),i.getProgramParameter(this._program,i.LINK_STATUS)||(e.productionMode||console.warn("Unable to initialize the shader program."),this._onErrorCallback&&this._onErrorCallback(),t=!1),this._matrices={mvMatrix:{name:"uMVMatrix",matrix:new Float32Array([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]),location:i.getUniformLocation(this._program,"uMVMatrix")},pMatrix:{name:"uPMatrix",matrix:new Float32Array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]),location:i.getUniformLocation(this._program,"uPMatrix")}}),t},Curtains.Plane.prototype._setDocumentSizes=function(){var t=this._wrapper,e=this.htmlElement.getBoundingClientRect();0===e.width&&0===e.height&&(e=t._boundingRect),this._boundingRect={document:{width:e.width*t.pixelRatio,height:e.height*t.pixelRatio,top:e.top*t.pixelRatio,left:e.left*t.pixelRatio}}},Curtains.Plane.prototype._setComputedSizes=function(){var t=this._wrapper,e=this._boundingRect.document.width/2+this._boundingRect.document.left,i=this._boundingRect.document.height/2+this._boundingRect.document.top,r=t._boundingRect.width/2+t._boundingRect.left,a=t._boundingRect.height/2+t._boundingRect.top;this._boundingRect.computed={width:this._boundingRect.document.width/t._boundingRect.width,height:this._boundingRect.document.height/t._boundingRect.height,top:(a-i)/t._boundingRect.height,left:(e-r)/t._boundingRect.height}},Curtains.Plane.prototype._restoreContext=function(){if(this._canDraw=!1,this._shaders.vertexShader=null,this._shaders.fragmentShader=null,this._program=null,this._matrices=null,this._attributes=null,this._geometry.bufferInfos=null,this._material.bufferInfos=null,this._setupPlaneProgram()){this._setAttributes(),this._setUniforms(this.uniforms),this._initializeBuffers();for(var t=0;t180&&(r=180);var a=parseFloat(e)||.1,n=parseFloat(i)||100;this._matrices&&(this._matrices.pMatrix.matrix=this._setPerspectiveMatrix(r,a,n),this._wrapper.glContext.useProgram(this._program),this._wrapper.glContext.uniformMatrix4fv(this._matrices.pMatrix.location,!1,this._matrices.pMatrix.matrix),this._setMVMatrix())},Curtains.Plane.prototype._setMVMatrix=function(){var t=this._wrapper,e=new Float32Array([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]),i=new Float32Array([1,0,0,0,0,1,0,0,0,0,1,0,this._translation.x,this._translation.y,this._translation.z-this._fov/2,1]),r=new Float32Array([1,0,0,0,0,Math.cos(this.rotation.x),Math.sin(this.rotation.x),0,0,-Math.sin(this.rotation.x),Math.cos(this.rotation.x),0,0,0,0,1]),a=new Float32Array([Math.cos(this.rotation.y),0,-Math.sin(this.rotation.y),0,0,1,0,0,Math.sin(this.rotation.y),0,Math.cos(this.rotation.y),0,0,0,0,1]),n=new Float32Array([Math.cos(this.rotation.z),Math.sin(this.rotation.z),0,0,-Math.sin(this.rotation.z),Math.cos(this.rotation.z),0,0,0,0,1,0,0,0,0,1]),o={x:this.scale.x*(t._boundingRect.width/t._boundingRect.height*this._boundingRect.computed.width/2),y:this.scale.y*this._boundingRect.computed.height/2},s=new Float32Array([o.x,0,0,0,0,o.y,0,0,0,0,1,0,0,0,0,1]),h=this._multiplyMatrix(e,i);h=this._multiplyMatrix(h,r),h=this._multiplyMatrix(h,a),h=this._multiplyMatrix(h,n),h=this._multiplyMatrix(h,s),this._matrices&&(this._matrices.mvMatrix.matrix=h,t.glContext.useProgram(this._program),t.glContext.uniformMatrix4fv(this._matrices.mvMatrix.location,!1,this._matrices.mvMatrix.matrix))},Curtains.Plane.prototype.setScale=function(t,e){t=parseFloat(t)||1,t=Math.max(t,.001),e=parseFloat(e)||1,e=Math.max(e,.001),this.scale={x:t,y:e},this.alwaysDraw||this._shouldDrawCheck(),this._setMVMatrix();for(var i=0;ithis._wrapper._boundingRect.width+0||a<-0||i>this._wrapper._boundingRect.height+0?this._shouldDraw&&(this._shouldDraw=!1,setTimeout(function(){o._onLeaveViewCallback&&o._onLeaveViewCallback()},0)):(this._shouldDraw||setTimeout(function(){o._onReEnterViewCallback&&o._onReEnterViewCallback()},0),this._shouldDraw=!0)},Curtains.Plane.prototype._applyCSSPositions=function(){this._setComputedSizes(),this._setTranslation()},Curtains.Plane.prototype.updatePosition=function(){this._setDocumentSizes(),this._applyCSSPositions()},Curtains.Plane.prototype.enableDepthTest=function(t){this._shouldUseDepthTest=t},Curtains.Plane.prototype.moveToFront=function(){this.enableDepthTest(!1);for(var t=this._wrapper._drawStack,e=0;e=e.HAVE_FUTURE_DATA&&i._onSourceLoaded(e),e.load(),this.videos.push(e)},Curtains.Plane.prototype.loadCanvas=function(t){var e=t;e.sampler=t.getAttribute("data-sampler")||null;var i=this.createTexture(e.sampler);this.canvases.push(e),i._onSourceLoaded(e)},Curtains.Plane.prototype.loadImages=function(t){for(var e=0;ea?s=Math.min(0,e-t*(1/a)):n=this.source.HAVE_CURRENT_DATA&&(this._willUpdate=!this._willUpdate),this._willUpdate&&this.shouldUpdate&&this._update()},Curtains.Texture.prototype._dispose=function(){"video"===this.type?(this.source.removeEventListener("canplaythrough",this._onSourceLoadedHandler,!1),this.source.pause(),this.source.removeAttribute("src"),this.source.load(),this.source.updateInterval&&clearInterval(this.source.updateInterval)):"canvas"===this.type?this.source.width=this.source.width:"image"===this.type&&this.source.removeEventListener("load",this._onSourceLoadedHandler,!1),this.source=null;var t=this._wrapper.glContext;t&&(t.activeTexture(t.TEXTURE0+this.index),t.bindTexture(t.TEXTURE_2D,null),t.deleteTexture(this._sampler.texture)),this._plane._loadingManager.sourcesLoaded--};