/** * @license * webvr-polyfill * Copyright (c) 2015-2017 Google * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @license * cardboard-vr-display * Copyright (c) 2015-2017 Google * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @license * webvr-polyfill-dpdb * Copyright (c) 2017 Google * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @license * wglu-preserve-state * Copyright (c) 2016, Brandon Jones. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ /** * @license * nosleep.js * Copyright (c) 2017, Rich Tibbett * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.WebVRPolyfill=t()}(this,function(){"use strict";var Be="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};var e,t,i=function(){return/Android/i.test(navigator.userAgent)||/iPhone|iPad|iPod/i.test(navigator.userAgent)},n=function(e,t){for(var i=0,r=e.length;ie.TEXTURE31){console.error("TEXTURE_BINDING_2D or TEXTURE_BINDING_CUBE_MAP must be followed by a valid texture unit"),r.push(null,null);break}n||(n=e.getParameter(e.ACTIVE_TEXTURE)),e.activeTexture(a),r.push(e.getParameter(s),null);break;case e.ACTIVE_TEXTURE:n=e.getParameter(e.ACTIVE_TEXTURE),r.push(null);break;default:r.push(e.getParameter(s))}}i(e);for(var A=0;Ae.TEXTURE31)break;e.activeTexture(a),e.bindTexture(e.TEXTURE_2D,o);break;case e.TEXTURE_BINDING_CUBE_MAP:var a=t[++A];if(ae.TEXTURE31)break;e.activeTexture(a),e.bindTexture(e.TEXTURE_CUBE_MAP,o);break;case e.VIEWPORT:e.viewport(o[0],o[1],o[2],o[3]);break;case e.BLEND:case e.CULL_FACE:case e.DEPTH_TEST:case e.SCISSOR_TEST:case e.STENCIL_TEST:o?e.enable(s):e.disable(s);break;default:console.log("No GL restore behavior for 0x"+s.toString(16))}n&&e.activeTexture(n)}}else i(e)},S=["attribute vec2 position;","attribute vec3 texCoord;","varying vec2 vTexCoord;","uniform vec4 viewportOffsetScale[2];","void main() {"," vec4 viewport = viewportOffsetScale[int(texCoord.z)];"," vTexCoord = (texCoord.xy * viewport.zw) + viewport.xy;"," gl_Position = vec4( position, 1.0, 1.0 );","}"].join("\n"),I=["precision mediump float;","uniform sampler2D diffuse;","varying vec2 vTexCoord;","void main() {"," gl_FragColor = texture2D(diffuse, vTexCoord);","}"].join("\n");function x(e,t,i,r){this.gl=e,this.cardboardUI=t,this.bufferScale=i,this.dirtySubmitFrameBindings=r,this.ctxAttribs=e.getContextAttributes(),this.meshWidth=20,this.meshHeight=20,this.bufferWidth=e.drawingBufferWidth,this.bufferHeight=e.drawingBufferHeight,this.realBindFramebuffer=e.bindFramebuffer,this.realEnable=e.enable,this.realDisable=e.disable,this.realColorMask=e.colorMask,this.realClearColor=e.clearColor,this.realViewport=e.viewport,h()||(this.realCanvasWidth=Object.getOwnPropertyDescriptor(e.canvas.__proto__,"width"),this.realCanvasHeight=Object.getOwnPropertyDescriptor(e.canvas.__proto__,"height")),this.isPatched=!1,this.lastBoundFramebuffer=null,this.cullFace=!1,this.depthTest=!1,this.blend=!1,this.scissorTest=!1,this.stencilTest=!1,this.viewport=[0,0,0,0],this.colorMask=[!0,!0,!0,!0],this.clearColor=[0,0,0,0],this.attribs={position:0,texCoord:1},this.program=y(e,S,I,this.attribs),this.uniforms=E(e,this.program),this.viewportOffsetScale=new Float32Array(8),this.setTextureBounds(),this.vertexBuffer=e.createBuffer(),this.indexBuffer=e.createBuffer(),this.indexCount=0,this.renderTarget=e.createTexture(),this.framebuffer=e.createFramebuffer(),this.depthStencilBuffer=null,this.depthBuffer=null,this.stencilBuffer=null,this.ctxAttribs.depth&&this.ctxAttribs.stencil?this.depthStencilBuffer=e.createRenderbuffer():this.ctxAttribs.depth?this.depthBuffer=e.createRenderbuffer():this.ctxAttribs.stencil&&(this.stencilBuffer=e.createRenderbuffer()),this.patch(),this.onResize()}x.prototype.destroy=function(){var e=this.gl;this.unpatch(),e.deleteProgram(this.program),e.deleteBuffer(this.vertexBuffer),e.deleteBuffer(this.indexBuffer),e.deleteTexture(this.renderTarget),e.deleteFramebuffer(this.framebuffer),this.depthStencilBuffer&&e.deleteRenderbuffer(this.depthStencilBuffer),this.depthBuffer&&e.deleteRenderbuffer(this.depthBuffer),this.stencilBuffer&&e.deleteRenderbuffer(this.stencilBuffer),this.cardboardUI&&this.cardboardUI.destroy()},x.prototype.onResize=function(){var e=this.gl,t=this,i=[e.RENDERBUFFER_BINDING,e.TEXTURE_BINDING_2D,e.TEXTURE0];C(e,i,function(e){t.realBindFramebuffer.call(e,e.FRAMEBUFFER,null),t.scissorTest&&t.realDisable.call(e,e.SCISSOR_TEST),t.realColorMask.call(e,!0,!0,!0,!0),t.realViewport.call(e,0,0,e.drawingBufferWidth,e.drawingBufferHeight),t.realClearColor.call(e,0,0,0,1),e.clear(e.COLOR_BUFFER_BIT),t.realBindFramebuffer.call(e,e.FRAMEBUFFER,t.framebuffer),e.bindTexture(e.TEXTURE_2D,t.renderTarget),e.texImage2D(e.TEXTURE_2D,0,t.ctxAttribs.alpha?e.RGBA:e.RGB,t.bufferWidth,t.bufferHeight,0,t.ctxAttribs.alpha?e.RGBA:e.RGB,e.UNSIGNED_BYTE,null),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,t.renderTarget,0),t.ctxAttribs.depth&&t.ctxAttribs.stencil?(e.bindRenderbuffer(e.RENDERBUFFER,t.depthStencilBuffer),e.renderbufferStorage(e.RENDERBUFFER,e.DEPTH_STENCIL,t.bufferWidth,t.bufferHeight),e.framebufferRenderbuffer(e.FRAMEBUFFER,e.DEPTH_STENCIL_ATTACHMENT,e.RENDERBUFFER,t.depthStencilBuffer)):t.ctxAttribs.depth?(e.bindRenderbuffer(e.RENDERBUFFER,t.depthBuffer),e.renderbufferStorage(e.RENDERBUFFER,e.DEPTH_COMPONENT16,t.bufferWidth,t.bufferHeight),e.framebufferRenderbuffer(e.FRAMEBUFFER,e.DEPTH_ATTACHMENT,e.RENDERBUFFER,t.depthBuffer)):t.ctxAttribs.stencil&&(e.bindRenderbuffer(e.RENDERBUFFER,t.stencilBuffer),e.renderbufferStorage(e.RENDERBUFFER,e.STENCIL_INDEX8,t.bufferWidth,t.bufferHeight),e.framebufferRenderbuffer(e.FRAMEBUFFER,e.STENCIL_ATTACHMENT,e.RENDERBUFFER,t.stencilBuffer)),!e.checkFramebufferStatus(e.FRAMEBUFFER)===e.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer incomplete!"),t.realBindFramebuffer.call(e,e.FRAMEBUFFER,t.lastBoundFramebuffer),t.scissorTest&&t.realEnable.call(e,e.SCISSOR_TEST),t.realColorMask.apply(e,t.colorMask),t.realViewport.apply(e,t.viewport),t.realClearColor.apply(e,t.clearColor)}),this.cardboardUI&&this.cardboardUI.onResize()},x.prototype.patch=function(){if(!this.isPatched){var n=this,t=this.gl.canvas,A=this.gl;h()||(t.width=g()*this.bufferScale,t.height=v()*this.bufferScale,Object.defineProperty(t,"width",{configurable:!0,enumerable:!0,get:function(){return n.bufferWidth},set:function(e){n.bufferWidth=e,n.realCanvasWidth.set.call(t,e),n.onResize()}}),Object.defineProperty(t,"height",{configurable:!0,enumerable:!0,get:function(){return n.bufferHeight},set:function(e){n.bufferHeight=e,n.realCanvasHeight.set.call(t,e),n.onResize()}})),this.lastBoundFramebuffer=A.getParameter(A.FRAMEBUFFER_BINDING),null==this.lastBoundFramebuffer&&(this.lastBoundFramebuffer=this.framebuffer,this.gl.bindFramebuffer(A.FRAMEBUFFER,this.framebuffer)),this.gl.bindFramebuffer=function(e,t){n.lastBoundFramebuffer=t||n.framebuffer,n.realBindFramebuffer.call(A,e,n.lastBoundFramebuffer)},this.cullFace=A.getParameter(A.CULL_FACE),this.depthTest=A.getParameter(A.DEPTH_TEST),this.blend=A.getParameter(A.BLEND),this.scissorTest=A.getParameter(A.SCISSOR_TEST),this.stencilTest=A.getParameter(A.STENCIL_TEST),A.enable=function(e){switch(e){case A.CULL_FACE:n.cullFace=!0;break;case A.DEPTH_TEST:n.depthTest=!0;break;case A.BLEND:n.blend=!0;break;case A.SCISSOR_TEST:n.scissorTest=!0;break;case A.STENCIL_TEST:n.stencilTest=!0}n.realEnable.call(A,e)},A.disable=function(e){switch(e){case A.CULL_FACE:n.cullFace=!1;break;case A.DEPTH_TEST:n.depthTest=!1;break;case A.BLEND:n.blend=!1;break;case A.SCISSOR_TEST:n.scissorTest=!1;break;case A.STENCIL_TEST:n.stencilTest=!1}n.realDisable.call(A,e)},this.colorMask=A.getParameter(A.COLOR_WRITEMASK),A.colorMask=function(e,t,i,r){n.colorMask[0]=e,n.colorMask[1]=t,n.colorMask[2]=i,n.colorMask[3]=r,n.realColorMask.call(A,e,t,i,r)},this.clearColor=A.getParameter(A.COLOR_CLEAR_VALUE),A.clearColor=function(e,t,i,r){n.clearColor[0]=e,n.clearColor[1]=t,n.clearColor[2]=i,n.clearColor[3]=r,n.realClearColor.call(A,e,t,i,r)},this.viewport=A.getParameter(A.VIEWPORT),A.viewport=function(e,t,i,r){n.viewport[0]=e,n.viewport[1]=t,n.viewport[2]=i,n.viewport[3]=r,n.realViewport.call(A,e,t,i,r)},this.isPatched=!0,M(t)}},x.prototype.unpatch=function(){if(this.isPatched){var e=this.gl,t=this.gl.canvas;h()||(Object.defineProperty(t,"width",this.realCanvasWidth),Object.defineProperty(t,"height",this.realCanvasHeight)),t.width=this.bufferWidth,t.height=this.bufferHeight,e.bindFramebuffer=this.realBindFramebuffer,e.enable=this.realEnable,e.disable=this.realDisable,e.colorMask=this.realColorMask,e.clearColor=this.realClearColor,e.viewport=this.realViewport,this.lastBoundFramebuffer==this.framebuffer&&e.bindFramebuffer(e.FRAMEBUFFER,null),this.isPatched=!1,setTimeout(function(){M(t)},1)}},x.prototype.setTextureBounds=function(e,t){e||(e=[0,0,.5,1]),t||(t=[.5,0,.5,1]),this.viewportOffsetScale[0]=e[0],this.viewportOffsetScale[1]=e[1],this.viewportOffsetScale[2]=e[2],this.viewportOffsetScale[3]=e[3],this.viewportOffsetScale[4]=t[0],this.viewportOffsetScale[5]=t[1],this.viewportOffsetScale[6]=t[2],this.viewportOffsetScale[7]=t[3]},x.prototype.submitFrame=function(){var e=this.gl,t=this,i=[];if(this.dirtySubmitFrameBindings||i.push(e.CURRENT_PROGRAM,e.ARRAY_BUFFER_BINDING,e.ELEMENT_ARRAY_BUFFER_BINDING,e.TEXTURE_BINDING_2D,e.TEXTURE0),C(e,i,function(e){t.realBindFramebuffer.call(e,e.FRAMEBUFFER,null),t.cullFace&&t.realDisable.call(e,e.CULL_FACE),t.depthTest&&t.realDisable.call(e,e.DEPTH_TEST),t.blend&&t.realDisable.call(e,e.BLEND),t.scissorTest&&t.realDisable.call(e,e.SCISSOR_TEST),t.stencilTest&&t.realDisable.call(e,e.STENCIL_TEST),t.realColorMask.call(e,!0,!0,!0,!0),t.realViewport.call(e,0,0,e.drawingBufferWidth,e.drawingBufferHeight),(t.ctxAttribs.alpha||h())&&(t.realClearColor.call(e,0,0,0,1),e.clear(e.COLOR_BUFFER_BIT)),e.useProgram(t.program),e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,t.indexBuffer),e.bindBuffer(e.ARRAY_BUFFER,t.vertexBuffer),e.enableVertexAttribArray(t.attribs.position),e.enableVertexAttribArray(t.attribs.texCoord),e.vertexAttribPointer(t.attribs.position,2,e.FLOAT,!1,20,0),e.vertexAttribPointer(t.attribs.texCoord,3,e.FLOAT,!1,20,8),e.activeTexture(e.TEXTURE0),e.uniform1i(t.uniforms.diffuse,0),e.bindTexture(e.TEXTURE_2D,t.renderTarget),e.uniform4fv(t.uniforms.viewportOffsetScale,t.viewportOffsetScale),e.drawElements(e.TRIANGLES,t.indexCount,e.UNSIGNED_SHORT,0),t.cardboardUI&&t.cardboardUI.renderNoState(),t.realBindFramebuffer.call(t.gl,e.FRAMEBUFFER,t.framebuffer),t.ctxAttribs.preserveDrawingBuffer||(t.realClearColor.call(e,0,0,0,0),e.clear(e.COLOR_BUFFER_BIT)),t.dirtySubmitFrameBindings||t.realBindFramebuffer.call(e,e.FRAMEBUFFER,t.lastBoundFramebuffer),t.cullFace&&t.realEnable.call(e,e.CULL_FACE),t.depthTest&&t.realEnable.call(e,e.DEPTH_TEST),t.blend&&t.realEnable.call(e,e.BLEND),t.scissorTest&&t.realEnable.call(e,e.SCISSOR_TEST),t.stencilTest&&t.realEnable.call(e,e.STENCIL_TEST),t.realColorMask.apply(e,t.colorMask),t.realViewport.apply(e,t.viewport),!t.ctxAttribs.alpha&&t.ctxAttribs.preserveDrawingBuffer||t.realClearColor.apply(e,t.clearColor)}),h()){var r=e.canvas;r.width==t.bufferWidth&&r.height==t.bufferHeight||(t.bufferWidth=r.width,t.bufferHeight=r.height,t.onResize())}},x.prototype.updateDeviceInfo=function(r){var e=this.gl,n=this,t=[e.ARRAY_BUFFER_BINDING,e.ELEMENT_ARRAY_BUFFER_BINDING];C(e,t,function(e){var t=n.computeMeshVertices_(n.meshWidth,n.meshHeight,r);if(e.bindBuffer(e.ARRAY_BUFFER,n.vertexBuffer),e.bufferData(e.ARRAY_BUFFER,t,e.STATIC_DRAW),!n.indexCount){var i=n.computeMeshIndices_(n.meshWidth,n.meshHeight);e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,n.indexBuffer),e.bufferData(e.ELEMENT_ARRAY_BUFFER,i,e.STATIC_DRAW),n.indexCount=i.length}})},x.prototype.computeMeshVertices_=function(e,t,i){for(var r=new Float32Array(2*e*t*5),n=i.getLeftEyeVisibleTanAngles(),A=i.getLeftEyeNoLensTanAngles(),s=i.getLeftEyeVisibleScreenRect(A),a=0,o=0;o<2;o++){for(var l=0;lt-42&&e.clientXn.clientHeight-42?i(e):e.clientX<42&&e.clientY<42&&r(e)},n.addEventListener("click",this.listener,!1)},G.prototype.onResize=function(){var e=this.gl,f=this,t=[e.ARRAY_BUFFER_BINDING];C(e,t,function(i){var A=[],s=i.drawingBufferWidth/2,e=Math.max(screen.width,screen.height)*window.devicePixelRatio,t=i.drawingBufferWidth/e,r=t*window.devicePixelRatio,n=4*r/2,a=42*r,o=28*r/2,l=14*r;function c(e,t){var i=(90-e)*N,r=Math.cos(i),n=Math.sin(i);A.push(Q*r*o+s,Q*n*o+o),A.push(t*r*o+s,t*n*o+o)}A.push(s-n,a),A.push(s-n,i.drawingBufferHeight),A.push(s+n,a),A.push(s+n,i.drawingBufferHeight),f.gearOffset=A.length/2;for(var h=0;h<=6;h++){var d=60*h;c(d,1),c(d+12,1),c(d+20,.75),c(d+40,.75),c(d+48,1)}function u(e,t){A.push(l+e,i.drawingBufferHeight-l-t)}f.gearVertexCount=A.length/2-f.gearOffset,f.arrowOffset=A.length/2;var p=n/Math.sin(45*N);u(0,o),u(o,0),u(o+p,p),u(p,o+p),u(p,o-p),u(0,o),u(o,2*o),u(o+p,2*o-p),u(p,o-p),u(0,o),u(p,o-n),u(28*r,o-n),u(p,o+n),u(28*r,o+n),f.arrowVertexCount=A.length/2-f.arrowOffset,i.bindBuffer(i.ARRAY_BUFFER,f.vertexBuffer),i.bufferData(i.ARRAY_BUFFER,new Float32Array(A),i.STATIC_DRAW)})},G.prototype.render=function(){var e=this.gl,t=this,i=[e.CULL_FACE,e.DEPTH_TEST,e.BLEND,e.SCISSOR_TEST,e.STENCIL_TEST,e.COLOR_WRITEMASK,e.VIEWPORT,e.CURRENT_PROGRAM,e.ARRAY_BUFFER_BINDING];C(e,i,function(e){e.disable(e.CULL_FACE),e.disable(e.DEPTH_TEST),e.disable(e.BLEND),e.disable(e.SCISSOR_TEST),e.disable(e.STENCIL_TEST),e.colorMask(!0,!0,!0,!0),e.viewport(0,0,e.drawingBufferWidth,e.drawingBufferHeight),t.renderNoState()})},G.prototype.renderNoState=function(){var e,t,i,r,n,A,s,a,o,l,c=this.gl;c.useProgram(this.program),c.bindBuffer(c.ARRAY_BUFFER,this.vertexBuffer),c.enableVertexAttribArray(this.attribs.position),c.vertexAttribPointer(this.attribs.position,2,c.FLOAT,!1,8,0),c.uniform4f(this.uniforms.color,1,1,1,1),e=this.projMat,t=0,i=c.drawingBufferWidth,r=0,n=c.drawingBufferHeight,a=1/(t-i),o=1/(r-n),l=1/((A=.1)-(s=1024)),e[0]=-2*a,e[1]=0,e[2]=0,e[3]=0,e[4]=0,e[5]=-2*o,e[6]=0,e[7]=0,e[8]=0,e[9]=0,e[10]=2*l,e[11]=0,e[12]=(t+i)*a,e[13]=(n+r)*o,e[14]=(s+A)*l,e[15]=1,c.uniformMatrix4fv(this.uniforms.projectionMat,!1,this.projMat),c.drawArrays(c.TRIANGLE_STRIP,0,4),c.drawArrays(c.TRIANGLE_STRIP,this.gearOffset,this.gearVertexCount),c.drawArrays(c.TRIANGLE_STRIP,this.arrowOffset,this.arrowVertexCount)},O.prototype.distortInverse=function(e){for(var t=0,i=1,r=e-this.distort(t);1e-4Math.abs(e.z)?z.set(-e.y,e.x,0):z.set(0,-e.z,e.y)):z.crossVectors(e,t),this.x=z.x,this.y=z.y,this.z=z.z,this.w=H,this.normalize(),this}};var Y=new X({widthMeters:.11,heightMeters:.062,bevelMeters:.004}),Z=new X({widthMeters:.1038,heightMeters:.0584,bevelMeters:.004}),j={CardboardV1:new J({id:"CardboardV1",label:"Cardboard I/O 2014",fov:40,interLensDistance:.06,baselineLensDistance:.035,screenLensDistance:.042,distortionCoefficients:[.441,.156],inverseCoefficients:[-.4410035,.42756155,-.4804439,.5460139,-.58821183,.5733938,-.48303202,.33299083,-.17573841,.0651772,-.01488963,.001559834]}),CardboardV2:new J({id:"CardboardV2",label:"Cardboard I/O 2015",fov:60,interLensDistance:.064,baselineLensDistance:.035,screenLensDistance:.039,distortionCoefficients:[.34,.55],inverseCoefficients:[-.33836704,-.18162185,.862655,-1.2462051,1.0560602,-.58208317,.21609078,-.05444823,.009177956,-.0009904169,6183535e-11,-16981803e-13]})};function K(e,t){this.viewer=j.CardboardV2,this.updateDeviceParams(e),this.distortion=new O(this.viewer.distortionCoefficients);for(var i=0;i 1"),0):2*Math.acos(r.w)),this.estimatedGravity.x.toFixed(1),this.estimatedGravity.y.toFixed(1),this.estimatedGravity.z.toFixed(1),this.measuredGravity.x.toFixed(1),this.measuredGravity.y.toFixed(1),this.measuredGravity.z.toFixed(1));var A=new W;A.copy(this.filterQ),A.multiply(n),this.filterQ.slerp(A,1-this.kFilter),this.previousFilterQ.copy(this.filterQ)},ie.prototype.getOrientation=function(){return this.filterQ},ie.prototype.accelToQuaternion_=function(e){var t=new V;t.copy(e),t.normalize();var i=new W;return i.setFromUnitVectors(new V(0,0,-1),t),i.inverse(),i},ie.prototype.gyroToQuaternionDelta_=function(e,t){var i=new W,r=new V;return r.copy(e),r.normalize(),i.setFromAxisAngle(r,e.length()*t),i},re.prototype.getPrediction=function(e,t,i){if(!this.previousTimestampS)return this.previousQ.copy(e),this.previousTimestampS=i,e;var r=new V;r.copy(t),r.normalize();var n=t.length();if(n<20*U)return this.isDebug&&console.log("Moving slowly, at %s deg/s: no prediction",(k*n).toFixed(1)),this.outQ.copy(e),this.previousQ.copy(e),this.outQ;var A=n*this.predictionTimeS;return this.deltaQ.setFromAxisAngle(r,A),this.outQ.copy(this.previousQ),this.outQ.multiply(this.deltaQ),this.previousQ.copy(e),this.previousTimestampS=i,this.outQ},ne.prototype.getPosition=function(){return null},ne.prototype.getOrientation=function(){var e=void 0;if(this.isWithoutDeviceMotion&&this._deviceOrientationQ){this.deviceOrientationFixQ=this.deviceOrientationFixQ||(r=(new W).setFromAxisAngle(new V(0,0,-1),0),n=new W,-90===window.orientation?n.setFromAxisAngle(new V(0,1,0),Math.PI/-2):n.setFromAxisAngle(new V(0,1,0),Math.PI/2),r.multiply(n)),this.deviceOrientationFilterToWorldQ=this.deviceOrientationFilterToWorldQ||((i=new W).setFromAxisAngle(new V(1,0,0),-Math.PI/2),i),e=this._deviceOrientationQ;var t=new W;return t.copy(e),t.multiply(this.deviceOrientationFilterToWorldQ),t.multiply(this.resetQ),t.multiply(this.worldToScreenQ),t.multiplyQuaternions(this.deviceOrientationFixQ,t),this.yawOnly&&(t.x=0,t.z=0,t.normalize()),this.orientationOut_[0]=t.x,this.orientationOut_[1]=t.y,this.orientationOut_[2]=t.z,this.orientationOut_[3]=t.w,this.orientationOut_}var i,r,n,A=this.filter.getOrientation();e=this.posePredictor.getPrediction(A,this.gyroscope,this.previousTimestampS);var t=new W;return t.copy(this.filterToWorldQ),t.multiply(this.resetQ),t.multiply(e),t.multiply(this.worldToScreenQ),this.yawOnly&&(t.x=0,t.z=0,t.normalize()),this.orientationOut_[0]=t.x,this.orientationOut_[1]=t.y,this.orientationOut_[2]=t.z,this.orientationOut_[3]=t.w,this.orientationOut_},ne.prototype.resetPose=function(){this.resetQ.copy(this.filter.getOrientation()),this.resetQ.x=0,this.resetQ.y=0,this.resetQ.z*=-1,this.resetQ.normalize(),m()&&this.resetQ.multiply(this.inverseWorldToScreenQ),this.resetQ.multiply(this.originalPoseAdjustQ)},ne.prototype.onDeviceOrientation_=function(e){this._deviceOrientationQ=this._deviceOrientationQ||new W;var t=e.alpha,i=e.beta,r=e.gamma;t=(t||0)*Math.PI/180,i=(i||0)*Math.PI/180,r=(r||0)*Math.PI/180,this._deviceOrientationQ.setFromEulerYXZ(i,t,-r)},ne.prototype.onDeviceMotion_=function(e){this.updateDeviceMotion_(e)},ne.prototype.updateDeviceMotion_=function(e){var t=e.accelerationIncludingGravity,i=e.rotationRate,r=e.timeStamp/1e3,n=r-this.previousTimestampS;return n<0?F("fusion-pose-sensor:invalid:non-monotonic","Invalid timestamps detected: non-monotonic timestamp from devicemotion"):n<=.001||1")};var ce="CardboardV1",he="WEBVR_CARDBOARD_VIEWER";function de(e){try{this.selectedKey=localStorage.getItem(he)}catch(e){console.error("Failed to load viewer profile: %s",e)}this.selectedKey||(this.selectedKey=e||ce),this.dialog=this.createDialog_(K.Viewers),this.root=null,this.onChangeCallbacks_=[]}de.prototype.show=function(e){(this.root=e).appendChild(this.dialog);var t=this.dialog.querySelector("#"+this.selectedKey);t.checked=!0,this.dialog.style.display="block"},de.prototype.hide=function(){this.root&&this.root.contains(this.dialog)&&this.root.removeChild(this.dialog),this.dialog.style.display="none"},de.prototype.getCurrentViewer=function(){return K.Viewers[this.selectedKey]},de.prototype.getSelectedKey_=function(){var e=this.dialog.querySelector("input[name=field]:checked");return e?e.id:null},de.prototype.onChange=function(e){this.onChangeCallbacks_.push(e)},de.prototype.fireOnChange_=function(e){for(var t=0;tc.capabilities.maxLayers)e(new Error("Invalid number of layers."));else{var i=o[0];if(i.source){var r=i.leftBounds||ge,n=i.rightBounds||ve;if(l){var A=c.layer_;A.source!==i.source&&(A.source=i.source);for(var s=0;s<4;s++)A.leftBounds[s]=r[s],A.rightBounds[s]=n[s];return c.wrapForFullscreen(c.layer_.source),c.updatePresent_(),void t()}if(c.layer_={predistorted:i.predistorted,source:i.source,leftBounds:r.slice(0),rightBounds:n.slice(0)},c.waitingForPresent_=!1,c.layer_&&c.layer_.source){var a=c.wrapForFullscreen(c.layer_.source);c.addFullscreenListeners_(a,function(){var e=document.fullscreenElement||document.webkitFullscreenElement||document.mozFullScreenElement||document.msFullscreenElement;c.isPresenting=a===e,c.isPresenting?(screen.orientation&&screen.orientation.lock&&screen.orientation.lock("landscape-primary").catch(function(e){console.error("screen.orientation.lock() failed due to",e.message)}),c.waitingForPresent_=!1,c.beginPresent_(),t()):(screen.orientation&&screen.orientation.unlock&&screen.orientation.unlock(),c.removeFullscreenWrapper(),c.disableWakeLock(),c.endPresent_(),c.removeFullscreenListeners_()),c.fireVRDisplayPresentChange_()},function(){c.waitingForPresent_&&(c.removeFullscreenWrapper(),c.removeFullscreenListeners_(),c.disableWakeLock(),c.waitingForPresent_=!1,c.isPresenting=!1,e(new Error("Unable to present.")))}),function(e){if(d())return!1;if(e.requestFullscreen)e.requestFullscreen();else if(e.webkitRequestFullscreen)e.webkitRequestFullscreen();else if(e.mozRequestFullScreen)e.mozRequestFullScreen();else{if(!e.msRequestFullscreen)return!1;e.msRequestFullscreen()}return!0}(a)?(c.enableWakeLock(),c.waitingForPresent_=!0):(h()||d())&&(c.enableWakeLock(),c.isPresenting=!0,c.beginPresent_(),c.fireVRDisplayPresentChange_(),t())}c.waitingForPresent_||h()||(w(),e(new Error("Unable to present.")))}else t()}else e(new Error("VRDisplay is not capable of presenting."))})},be.prototype.exitPresent=function(){var i=this.isPresenting,r=this;return this.isPresenting=!1,this.layer_=null,this.disableWakeLock(),new Promise(function(e,t){i?(!w()&&h()&&(r.endPresent_(),r.fireVRDisplayPresentChange_()),d()&&(r.removeFullscreenWrapper(),r.removeFullscreenListeners_(),r.endPresent_(),r.fireVRDisplayPresentChange_()),e()):t(new Error("Was not presenting to VRDisplay."))})},be.prototype.getLayers=function(){return this.layer_?[this.layer_]:[]},be.prototype.fireVRDisplayPresentChange_=function(){var e=new CustomEvent("vrdisplaypresentchange",{detail:{display:this}});window.dispatchEvent(e)},be.prototype.fireVRDisplayConnect_=function(){var e=new CustomEvent("vrdisplayconnect",{detail:{display:this}});window.dispatchEvent(e)},be.prototype.addFullscreenListeners_=function(e,t,i){this.removeFullscreenListeners_(),this.fullscreenEventTarget_=e,this.fullscreenChangeHandler_=t,this.fullscreenErrorHandler_=i,t&&(document.fullscreenEnabled?e.addEventListener("fullscreenchange",t,!1):document.webkitFullscreenEnabled?e.addEventListener("webkitfullscreenchange",t,!1):document.mozFullScreenEnabled?document.addEventListener("mozfullscreenchange",t,!1):document.msFullscreenEnabled&&e.addEventListener("msfullscreenchange",t,!1)),i&&(document.fullscreenEnabled?e.addEventListener("fullscreenerror",i,!1):document.webkitFullscreenEnabled?e.addEventListener("webkitfullscreenerror",i,!1):document.mozFullScreenEnabled?document.addEventListener("mozfullscreenerror",i,!1):document.msFullscreenEnabled&&e.addEventListener("msfullscreenerror",i,!1))},be.prototype.removeFullscreenListeners_=function(){if(this.fullscreenEventTarget_){var e=this.fullscreenEventTarget_;if(this.fullscreenChangeHandler_){var t=this.fullscreenChangeHandler_;e.removeEventListener("fullscreenchange",t,!1),e.removeEventListener("webkitfullscreenchange",t,!1),document.removeEventListener("mozfullscreenchange",t,!1),e.removeEventListener("msfullscreenchange",t,!1)}if(this.fullscreenErrorHandler_){var i=this.fullscreenErrorHandler_;e.removeEventListener("fullscreenerror",i,!1),e.removeEventListener("webkitfullscreenerror",i,!1),document.removeEventListener("mozfullscreenerror",i,!1),e.removeEventListener("msfullscreenerror",i,!1)}this.fullscreenEventTarget_=null,this.fullscreenChangeHandler_=null,this.fullscreenErrorHandler_=null}},be.prototype.enableWakeLock=function(){this.wakelock_&&this.wakelock_.enable()},be.prototype.disableWakeLock=function(){this.wakelock_&&this.wakelock_.disable()},be.prototype.beginPresent_=function(){},be.prototype.endPresent_=function(){},be.prototype.submitFrame=function(e){};var _e={ADDITIONAL_VIEWERS:[],DEFAULT_VIEWER:"",MOBILE_WAKE_LOCK:!0,DEBUG:!(be.prototype.getEyeParameters=function(e){return null}),DPDB_URL:"https://dpdb.webvr.rocks/dpdb.json",K_FILTER:.98,PREDICTION_TIME_S:.04,CARDBOARD_UI_DISABLED:!1,ROTATE_INSTRUCTIONS_DISABLED:!1,YAW_ONLY:!1,BUFFER_SCALE:.5,DIRTY_SUBMIT_FRAME_BINDINGS:!1},De={LEFT:"left",RIGHT:"right"};function Me(e){var t=D({},_e);e=D(t,e||{}),be.call(this,{wakelock:e.MOBILE_WAKE_LOCK}),this.config=e,this.displayName="Cardboard VRDisplay",this.capabilities=new Ee({hasPosition:!1,hasOrientation:!0,hasExternalDisplay:!1,canPresent:!0,maxLayers:1}),this.stageParameters=null,this.bufferScale_=this.config.BUFFER_SCALE,this.poseSensor_=new oe(this.config),this.distorter_=null,this.cardboardUI_=null,this.dpdb_=new $(this.config.DPDB_URL,this.onDeviceParamsUpdated_.bind(this)),this.deviceInfo_=new K(this.dpdb_.getDeviceParams(),e.ADDITIONAL_VIEWERS),this.viewerSelector_=new de(e.DEFAULT_VIEWER),this.viewerSelector_.onChange(this.onViewerChanged_.bind(this)),this.deviceInfo_.setViewer(this.viewerSelector_.getCurrentViewer()),this.config.ROTATE_INSTRUCTIONS_DISABLED||(this.rotateInstructions_=new le),h()&&window.addEventListener("resize",this.onResize_.bind(this))}return(Me.prototype=Object.create(be.prototype))._getPose=function(){return{position:null,orientation:this.poseSensor_.getOrientation(),linearVelocity:null,linearAcceleration:null,angularVelocity:null,angularAcceleration:null}},Me.prototype._resetPose=function(){this.poseSensor_.resetPose&&this.poseSensor_.resetPose()},Me.prototype._getFieldOfView=function(e){var t;if(e==De.LEFT)t=this.deviceInfo_.getFieldOfViewLeftEye();else{if(e!=De.RIGHT)return console.error("Invalid eye provided: %s",e),null;t=this.deviceInfo_.getFieldOfViewRightEye()}return t},Me.prototype._getEyeOffset=function(e){var t;if(e==De.LEFT)t=[.5*-this.deviceInfo_.viewer.interLensDistance,0,0];else{if(e!=De.RIGHT)return console.error("Invalid eye provided: %s",e),null;t=[.5*this.deviceInfo_.viewer.interLensDistance,0,0]}return t},Me.prototype.getEyeParameters=function(e){var t=this._getEyeOffset(e),i=this._getFieldOfView(e),r={offset:t,renderWidth:.5*this.deviceInfo_.device.width*this.bufferScale_,renderHeight:this.deviceInfo_.device.height*this.bufferScale_};return Object.defineProperty(r,"fieldOfView",{enumerable:!0,get:function(){return R("VRFieldOfView","VRFrameData's projection matrices"),i}}),r},Me.prototype.onDeviceParamsUpdated_=function(e){this.config.DEBUG&&console.log("DPDB reported that device params were updated."),this.deviceInfo_.updateDeviceParams(e),this.distorter_&&this.distorter_.updateDeviceInfo(this.deviceInfo_)},Me.prototype.updateBounds_=function(){this.layer_&&this.distorter_&&(this.layer_.leftBounds||this.layer_.rightBounds)&&this.distorter_.setTextureBounds(this.layer_.leftBounds,this.layer_.rightBounds)},Me.prototype.beginPresent_=function(){var e=this.layer_.source.getContext("webgl");e||(e=this.layer_.source.getContext("experimental-webgl")),e||(e=this.layer_.source.getContext("webgl2")),e&&(this.layer_.predistorted?this.config.CARDBOARD_UI_DISABLED||(e.canvas.width=g()*this.bufferScale_,e.canvas.height=v()*this.bufferScale_,this.cardboardUI_=new G(e)):(this.config.CARDBOARD_UI_DISABLED||(this.cardboardUI_=new G(e)),this.distorter_=new x(e,this.cardboardUI_,this.config.BUFFER_SCALE,this.config.DIRTY_SUBMIT_FRAME_BINDINGS),this.distorter_.updateDeviceInfo(this.deviceInfo_)),this.cardboardUI_&&this.cardboardUI_.listen(function(e){this.viewerSelector_.show(this.layer_.source.parentElement),e.stopPropagation(),e.preventDefault()}.bind(this),function(e){this.exitPresent(),e.stopPropagation(),e.preventDefault()}.bind(this)),this.rotateInstructions_&&(m()&&_()?this.rotateInstructions_.showTemporarily(3e3,this.layer_.source.parentElement):this.rotateInstructions_.update()),this.orientationHandler=this.onOrientationChange_.bind(this),window.addEventListener("orientationchange",this.orientationHandler),this.vrdisplaypresentchangeHandler=this.updateBounds_.bind(this),window.addEventListener("vrdisplaypresentchange",this.vrdisplaypresentchangeHandler),this.fireVRDisplayDeviceParamsChange_())},Me.prototype.endPresent_=function(){this.distorter_&&(this.distorter_.destroy(),this.distorter_=null),this.cardboardUI_&&(this.cardboardUI_.destroy(),this.cardboardUI_=null),this.rotateInstructions_&&this.rotateInstructions_.hide(),this.viewerSelector_.hide(),window.removeEventListener("orientationchange",this.orientationHandler),window.removeEventListener("vrdisplaypresentchange",this.vrdisplaypresentchangeHandler)},Me.prototype.updatePresent_=function(){this.endPresent_(),this.beginPresent_()},Me.prototype.submitFrame=function(e){if(this.distorter_)this.updateBounds_(),this.distorter_.submitFrame();else if(this.cardboardUI_&&this.layer_){var t=this.layer_.source.getContext("webgl").canvas;t.width==this.lastWidth&&t.height==this.lastHeight||this.cardboardUI_.onResize(),this.lastWidth=t.width,this.lastHeight=t.height,this.cardboardUI_.render()}},Me.prototype.onOrientationChange_=function(e){this.viewerSelector_.hide(),this.rotateInstructions_&&this.rotateInstructions_.update(),this.onResize_()},Me.prototype.onResize_=function(e){if(this.layer_){var t=this.layer_.source.getContext("webgl");t.canvas.setAttribute("style",["position: absolute","top: 0","left: 0","width: 100vw","height: 100vh","border: 0","margin: 0","padding: 0px","box-sizing: content-box"].join("; ")+";"),M(t.canvas)}},Me.prototype.onViewerChanged_=function(e){this.deviceInfo_.setViewer(e),this.distorter_&&this.distorter_.updateDeviceInfo(this.deviceInfo_),this.fireVRDisplayDeviceParamsChange_()},Me.prototype.fireVRDisplayDeviceParamsChange_=function(){var e=new CustomEvent("vrdisplaydeviceparamschange",{detail:{vrdisplay:this,deviceInfo:this.deviceInfo_}});window.dispatchEvent(e)},Me.VRFrameData=function(){this.leftProjectionMatrix=new Float32Array(16),this.leftViewMatrix=new Float32Array(16),this.rightProjectionMatrix=new Float32Array(16),this.rightViewMatrix=new Float32Array(16),this.pose=null},Me.VRDisplay=be,Me}()}(e={exports:{}},e.exports),e.exports),s=(t=A)&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t,a={ADDITIONAL_VIEWERS:[],DEFAULT_VIEWER:"",PROVIDE_MOBILE_VRDISPLAY:!0,MOBILE_WAKE_LOCK:!0,DEBUG:!1,DPDB_URL:"https://dpdb.webvr.rocks/dpdb.json",K_FILTER:.98,PREDICTION_TIME_S:.04,CARDBOARD_UI_DISABLED:!1,ROTATE_INSTRUCTIONS_DISABLED:!1,YAW_ONLY:!1,BUFFER_SCALE:.5,DIRTY_SUBMIT_FRAME_BINDINGS:!1};function o(e){this.config=r(r({},a),e),this.polyfillDisplays=[],this.enabled=!1,this.hasNative="getVRDisplays"in navigator,this.native={},this.native.getVRDisplays=navigator.getVRDisplays,this.native.VRFrameData=window.VRFrameData,this.native.VRDisplay=window.VRDisplay,(!this.hasNative||this.config.PROVIDE_MOBILE_VRDISPLAY&&i())&&(this.enable(),this.getVRDisplays().then(function(e){e&&e[0]&&e[0].fireVRDisplayConnect_&&e[0].fireVRDisplayConnect_()}))}o.prototype.getPolyfillDisplays=function(){if(this._polyfillDisplaysPopulated)return this.polyfillDisplays;if(i()){var e=new s({ADDITIONAL_VIEWERS:this.config.ADDITIONAL_VIEWERS,DEFAULT_VIEWER:this.config.DEFAULT_VIEWER,MOBILE_WAKE_LOCK:this.config.MOBILE_WAKE_LOCK,DEBUG:this.config.DEBUG,DPDB_URL:this.config.DPDB_URL,CARDBOARD_UI_DISABLED:this.config.CARDBOARD_UI_DISABLED,K_FILTER:this.config.K_FILTER,PREDICTION_TIME_S:this.config.PREDICTION_TIME_S,ROTATE_INSTRUCTIONS_DISABLED:this.config.ROTATE_INSTRUCTIONS_DISABLED,YAW_ONLY:this.config.YAW_ONLY,BUFFER_SCALE:this.config.BUFFER_SCALE,DIRTY_SUBMIT_FRAME_BINDINGS:this.config.DIRTY_SUBMIT_FRAME_BINDINGS});this.polyfillDisplays.push(e)}return this._polyfillDisplaysPopulated=!0,this.polyfillDisplays},o.prototype.enable=function(){if(this.enabled=!0,this.hasNative&&this.native.VRFrameData){var t=this.native.VRFrameData,i=new this.native.VRFrameData,r=this.native.VRDisplay.prototype.getFrameData;window.VRDisplay.prototype.getFrameData=function(e){e instanceof t?r.call(this,e):(r.call(this,i),e.pose=i.pose,n(i.leftProjectionMatrix,e.leftProjectionMatrix),n(i.rightProjectionMatrix,e.rightProjectionMatrix),n(i.leftViewMatrix,e.leftViewMatrix),n(i.rightViewMatrix,e.rightViewMatrix))}}navigator.getVRDisplays=this.getVRDisplays.bind(this),window.VRDisplay=s.VRDisplay,window.VRFrameData=s.VRFrameData},o.prototype.getVRDisplays=function(){var t=this;this.config;return this.hasNative?this.native.getVRDisplays.call(navigator).then(function(e){return 0