UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 5.86 kB
import{EventHandler as t}from"../../core/event-handler.js";import{Texture as e}from"../../platform/graphics/texture.js";import{Vec4 as i}from"../../core/math/vec4.js";import{Mat3 as r}from"../../core/math/mat3.js";import{Mat4 as h}from"../../core/math/mat4.js";import{PIXELFORMAT_RGB8 as s,ADDRESS_CLAMP_TO_EDGE as a,FILTER_LINEAR as _}from"../../platform/graphics/constants.js";class o extends t{constructor(t,o,f){super(),this._manager=void 0,this._xrView=void 0,this._positionData=new Float32Array(3),this._viewport=new i,this._projMat=new h,this._projViewOffMat=new h,this._viewMat=new h,this._viewOffMat=new h,this._viewMat3=new r,this._viewInvMat=new h,this._viewInvOffMat=new h,this._xrCamera=null,this._textureColor=null,this._textureDepth=null,this._depthInfo=null,this._emptyDepthBuffer=new Uint8Array(32),this._depthMatrix=new h,this._manager=t,this._xrView=o;const n=this._manager.app.graphicsDevice;if(this._manager.views.supportedColor&&(this._xrCamera=this._xrView.camera,this._manager.views.availableColor&&this._xrCamera&&(this._textureColor=new e(n,{format:s,mipmaps:!1,addressU:a,addressV:a,minFilter:_,magFilter:_,width:this._xrCamera.width,height:this._xrCamera.height,name:`XrView-${this._xrView.eye}-Color`}))),this._manager.views.supportedDepth&&this._manager.views.availableDepth){this._textureDepth=new e(n,{format:this._manager.views.depthPixelFormat,arrayLength:1===f?0:f,mipmaps:!1,addressU:a,addressV:a,minFilter:_,magFilter:_,width:4,height:4,name:`XrView-${this._xrView.eye}-Depth`});for(let t=0;t<this._textureDepth._levels.length;t++)this._textureDepth._levels[t]=this._emptyDepthBuffer}(this._textureColor||this._textureDepth)&&n.on("devicelost",this._onDeviceLost,this)}get textureColor(){return this._textureColor}get textureDepth(){return this._textureDepth}get depthUvMatrix(){return this._depthMatrix}get depthValueToMeters(){var t;return(null==(t=this._depthInfo)?void 0:t.rawValueToMeters)||0}get eye(){return this._xrView.eye}get viewport(){return this._viewport}get projMat(){return this._projMat}get projViewOffMat(){return this._projViewOffMat}get viewOffMat(){return this._viewOffMat}get viewInvOffMat(){return this._viewInvOffMat}get viewMat3(){return this._viewMat3}get positionData(){return this._positionData}update(t,e){this._xrView=e,this._manager.views.availableColor&&(this._xrCamera=this._xrView.camera);const i=t.session.renderState.baseLayer.getViewport(this._xrView);this._viewport.x=i.x,this._viewport.y=i.y,this._viewport.z=i.width,this._viewport.w=i.height,this._projMat.set(this._xrView.projectionMatrix),this._viewMat.set(this._xrView.transform.inverse.matrix),this._viewInvMat.set(this._xrView.transform.matrix),this._updateTextureColor(),this._updateDepth(t)}_updateTextureColor(){if(!this._manager.views.availableColor||!this._xrCamera||!this._textureColor)return;const t=this._manager.webglBinding;if(!t)return;const e=t.getCameraImage(this._xrCamera);if(!e)return;const i=this._manager.app.graphicsDevice,r=i.gl;if(this._frameBufferSource){var h,s;const t=i.isWebGL2?r.COLOR_ATTACHMENT0:null!=(h=null==(s=i.extDrawBuffers)?void 0:s.COLOR_ATTACHMENT0_WEBGL)?h:r.COLOR_ATTACHMENT0,a=this._xrCamera.width,_=this._xrCamera.height;i.setFramebuffer(this._frameBufferSource),r.framebufferTexture2D(r.FRAMEBUFFER,t,r.TEXTURE_2D,e,0),i.setFramebuffer(this._frameBuffer),r.framebufferTexture2D(r.FRAMEBUFFER,t,r.TEXTURE_2D,this._textureColor.impl._glTexture,0),r.bindFramebuffer(r.READ_FRAMEBUFFER,this._frameBufferSource),r.bindFramebuffer(r.DRAW_FRAMEBUFFER,this._frameBuffer),r.blitFramebuffer(0,_,a,0,0,0,a,_,r.COLOR_BUFFER_BIT,r.NEAREST)}else this._frameBufferSource=r.createFramebuffer(),this._frameBuffer=r.createFramebuffer()}_updateDepth(t){var e,i;if(!this._manager.views.availableDepth||!this._textureDepth)return;const r=this._manager.views.depthGpuOptimized,h=r?this._manager.webglBinding:t;if(!h)return void(this._depthInfo=null);const s=h.getDepthInformation(this._xrView);if(!s)return void(this._depthInfo=null);let a=!this._depthInfo!=!s;this._depthInfo=s;const _=(null==(e=this._depthInfo)?void 0:e.width)||4,o=(null==(i=this._depthInfo)?void 0:i.height)||4;let f=!1;this._textureDepth.width===_&&this._textureDepth.height===o||(this._textureDepth._width=_,this._textureDepth._height=o,a=!0,f=!0),a&&(this._depthInfo?this._depthMatrix.data.set(this._depthInfo.normDepthBufferFromNormView.matrix):this._depthMatrix.setIdentity()),this._depthInfo?r?this._depthInfo.texture&&(this._textureDepth.impl._glTexture=this._depthInfo.texture):(this._textureDepth._levels[0]=new Uint8Array(this._depthInfo.data),this._textureDepth.upload()):(this._textureDepth._levels[0]=this._emptyDepthBuffer,this._textureDepth.upload()),f&&this.fire("depth:resize",_,o)}updateTransforms(t){t?(this._viewInvOffMat.mul2(t,this._viewInvMat),this.viewOffMat.copy(this._viewInvOffMat).invert()):(this._viewInvOffMat.copy(this._viewInvMat),this.viewOffMat.copy(this._viewMat)),this._viewMat3.setFromMat4(this._viewOffMat),this._projViewOffMat.mul2(this._projMat,this._viewOffMat),this._positionData[0]=this._viewInvOffMat.data[12],this._positionData[1]=this._viewInvOffMat.data[13],this._positionData[2]=this._viewInvOffMat.data[14]}_onDeviceLost(){this._frameBufferSource=null,this._frameBuffer=null,this._depthInfo=null}getDepth(t,e){var i,r;return this._manager.views.depthGpuOptimized?null:null!=(i=null==(r=this._depthInfo)?void 0:r.getDepthInMeters(t,e))?i:null}destroy(){if(this._depthInfo=null,this._textureColor&&(this._textureColor.destroy(),this._textureColor=null),this._textureDepth&&(this._textureDepth.destroy(),this._textureDepth=null),this._frameBufferSource){const t=this._manager.app.graphicsDevice.gl;t.deleteFramebuffer(this._frameBufferSource),this._frameBufferSource=null,t.deleteFramebuffer(this._frameBuffer),this._frameBuffer=null}}}o.EVENT_DEPTHRESIZE="depth:resize";export{o as XrView};