@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 10.1 kB
JavaScript
import{Color as t}from"../core/math/color.js";import{Mat4 as e}from"../core/math/mat4.js";import{Vec3 as r}from"../core/math/vec3.js";import{Vec4 as i}from"../core/math/vec4.js";import{math as s}from"../core/math/math.js";import{Frustum as a}from"../core/shape/frustum.js";import{ASPECT_AUTO as o,LAYERID_WORLD as h,LAYERID_DEPTH as n,LAYERID_SKYBOX as l,LAYERID_UI as c,LAYERID_IMMEDIATE as _,PROJECTION_PERSPECTIVE as p}from"./constants.js";import{RenderPassColorGrab as u}from"./graphics/render-pass-color-grab.js";import{RenderPassDepthGrab as f}from"./graphics/render-pass-depth-grab.js";import{RenderPassDepth as v}from"./graphics/render-pass-depth.js";const j=new r,P=new r,y=new r,C=new e,g=[new r,new r,new r,new r,new r,new r,new r,new r];class d{constructor(){this.shaderPassInfo=null,this.renderPassColorGrab=null,this.renderPassDepthGrab=null,this.renderPasses=[],this.jitter=0,this._aspectRatio=16/9,this._aspectRatioMode=o,this._calculateProjection=null,this._calculateTransform=null,this._clearColor=new t(.75,.75,.75,1),this._clearColorBuffer=!0,this._clearDepth=1,this._clearDepthBuffer=!0,this._clearStencil=0,this._clearStencilBuffer=!0,this._cullFaces=!0,this._farClip=1e3,this._flipFaces=!1,this._fov=45,this._frustumCulling=!0,this._horizontalFov=!1,this._layers=[h,n,l,c,_],this._layersSet=new Set(this._layers),this._nearClip=.1,this._node=null,this._orthoHeight=10,this._projection=p,this._rect=new i(0,0,1,1),this._renderTarget=null,this._scissorRect=new i(0,0,1,1),this._scissorRectClear=!1,this._aperture=16,this._shutter=.001,this._sensitivity=1e3,this._projMat=new e,this._projMatDirty=!0,this._projMatSkybox=new e,this._viewMat=new e,this._viewMatDirty=!0,this._viewProjMat=new e,this._viewProjMatDirty=!0,this._shaderMatricesVersion=0,this._viewProjInverse=new e,this._viewProjCurrent=null,this._viewProjPrevious=new e,this._jitters=[0,0,0,0],this.frustum=new a,this._xr=null,this._xrProperties={horizontalFov:this._horizontalFov,fov:this._fov,aspectRatio:this._aspectRatio,farClip:this._farClip,nearClip:this._nearClip}}destroy(){var t,e;null==(t=this.renderPassColorGrab)||t.destroy(),this.renderPassColorGrab=null,null==(e=this.renderPassDepthGrab)||e.destroy(),this.renderPassDepthGrab=null,this.renderPasses.length=0}_storeShaderMatrices(t,r,i,s){var a;this._shaderMatricesVersion!==s&&(this._shaderMatricesVersion=s,this._viewProjPrevious.copy(null!=(a=this._viewProjCurrent)?a:t),null!=this._viewProjCurrent||(this._viewProjCurrent=new e),this._viewProjCurrent.copy(t),this._viewProjInverse.invert(t),this._jitters[2]=this._jitters[0],this._jitters[3]=this._jitters[1],this._jitters[0]=r,this._jitters[1]=i)}get fullSizeClearRect(){const t=this._scissorRectClear?this.scissorRect:this._rect;return 0===t.x&&0===t.y&&1===t.z&&1===t.w}set aspectRatio(t){this._aspectRatio!==t&&(this._aspectRatio=t,this._projMatDirty=!0)}get aspectRatio(){var t;return null!=(t=this.xr)&&t.active?this._xrProperties.aspectRatio:this._aspectRatio}set aspectRatioMode(t){this._aspectRatioMode!==t&&(this._aspectRatioMode=t,this._projMatDirty=!0)}get aspectRatioMode(){return this._aspectRatioMode}set calculateProjection(t){this._calculateProjection=t,this._projMatDirty=!0}get calculateProjection(){return this._calculateProjection}set calculateTransform(t){this._calculateTransform=t}get calculateTransform(){return this._calculateTransform}set clearColor(t){this._clearColor.copy(t)}get clearColor(){return this._clearColor}set clearColorBuffer(t){this._clearColorBuffer=t}get clearColorBuffer(){return this._clearColorBuffer}set clearDepth(t){this._clearDepth=t}get clearDepth(){return this._clearDepth}set clearDepthBuffer(t){this._clearDepthBuffer=t}get clearDepthBuffer(){return this._clearDepthBuffer}set clearStencil(t){this._clearStencil=t}get clearStencil(){return this._clearStencil}set clearStencilBuffer(t){this._clearStencilBuffer=t}get clearStencilBuffer(){return this._clearStencilBuffer}set cullFaces(t){this._cullFaces=t}get cullFaces(){return this._cullFaces}set farClip(t){this._farClip!==t&&(this._farClip=t,this._projMatDirty=!0)}get farClip(){var t;return null!=(t=this.xr)&&t.active?this._xrProperties.farClip:this._farClip}set flipFaces(t){this._flipFaces=t}get flipFaces(){return this._flipFaces}set fov(t){this._fov!==t&&(this._fov=t,this._projMatDirty=!0)}get fov(){var t;return null!=(t=this.xr)&&t.active?this._xrProperties.fov:this._fov}set frustumCulling(t){this._frustumCulling=t}get frustumCulling(){return this._frustumCulling}set horizontalFov(t){this._horizontalFov!==t&&(this._horizontalFov=t,this._projMatDirty=!0)}get horizontalFov(){var t;return null!=(t=this.xr)&&t.active?this._xrProperties.horizontalFov:this._horizontalFov}set layers(t){this._layers=t.slice(0),this._layersSet=new Set(this._layers)}get layers(){return this._layers}get layersSet(){return this._layersSet}set nearClip(t){this._nearClip!==t&&(this._nearClip=t,this._projMatDirty=!0)}get nearClip(){var t;return null!=(t=this.xr)&&t.active?this._xrProperties.nearClip:this._nearClip}set node(t){this._node=t}get node(){return this._node}set orthoHeight(t){this._orthoHeight!==t&&(this._orthoHeight=t,this._projMatDirty=!0)}get orthoHeight(){return this._orthoHeight}set projection(t){this._projection!==t&&(this._projection=t,this._projMatDirty=!0)}get projection(){return this._projection}get projectionMatrix(){return this._evaluateProjectionMatrix(),this._projMat}set rect(t){this._rect.copy(t)}get rect(){return this._rect}set renderTarget(t){this._renderTarget=t}get renderTarget(){return this._renderTarget}set scissorRect(t){this._scissorRect.copy(t)}get scissorRect(){return this._scissorRect}get viewMatrix(){if(this._viewMatDirty){const t=this._node.getWorldTransform();this._viewMat.copy(t).invert(),this._viewMatDirty=!1}return this._viewMat}set aperture(t){this._aperture=t}get aperture(){return this._aperture}set sensitivity(t){this._sensitivity=t}get sensitivity(){return this._sensitivity}set shutter(t){this._shutter=t}get shutter(){return this._shutter}set xr(t){this._xr!==t&&(this._xr=t,this._projMatDirty=!0)}get xr(){return this._xr}clone(){return(new d).copy(this)}copy(t){return this._aspectRatio=t._aspectRatio,this._farClip=t._farClip,this._fov=t._fov,this._horizontalFov=t._horizontalFov,this._nearClip=t._nearClip,this._xrProperties.aspectRatio=t._xrProperties.aspectRatio,this._xrProperties.farClip=t._xrProperties.farClip,this._xrProperties.fov=t._xrProperties.fov,this._xrProperties.horizontalFov=t._xrProperties.horizontalFov,this._xrProperties.nearClip=t._xrProperties.nearClip,this.aspectRatioMode=t.aspectRatioMode,this.calculateProjection=t.calculateProjection,this.calculateTransform=t.calculateTransform,this.clearColor=t.clearColor,this.clearColorBuffer=t.clearColorBuffer,this.clearDepth=t.clearDepth,this.clearDepthBuffer=t.clearDepthBuffer,this.clearStencil=t.clearStencil,this.clearStencilBuffer=t.clearStencilBuffer,this.cullFaces=t.cullFaces,this.flipFaces=t.flipFaces,this.frustumCulling=t.frustumCulling,this.layers=t.layers,this.orthoHeight=t.orthoHeight,this.projection=t.projection,this.rect=t.rect,this.renderTarget=t.renderTarget,this.scissorRect=t.scissorRect,this.aperture=t.aperture,this.shutter=t.shutter,this.sensitivity=t.sensitivity,this.shaderPassInfo=t.shaderPassInfo,this.jitter=t.jitter,this._projMatDirty=!0,this}_enableRenderPassColorGrab(t,e){var r;e?this.renderPassColorGrab||(this.renderPassColorGrab=new u(t)):(null==(r=this.renderPassColorGrab)||r.destroy(),this.renderPassColorGrab=null)}_enableRenderPassDepthGrab(t,e,r){var i;r?this.renderPassDepthGrab||(this.renderPassDepthGrab=t.isWebGL1?new v(t,e,this):new f(t,this)):(null==(i=this.renderPassDepthGrab)||i.destroy(),this.renderPassDepthGrab=null)}_updateViewProjMat(){(this._projMatDirty||this._viewMatDirty||this._viewProjMatDirty)&&(this._viewProjMat.mul2(this.projectionMatrix,this.viewMatrix),this._viewProjMatDirty=!1)}worldToScreen(t,e,i,s=new r){this._updateViewProjMat(),this._viewProjMat.transformPoint(t,s);const a=this._viewProjMat.data,o=t.x*a[3]+t.y*a[7]+t.z*a[11]+1*a[15];return s.x=.5*(s.x/o+1)*e,s.y=.5*(1-s.y/o)*i,s}screenToWorld(t,i,s,a,o,h=new r){const n=this.farClip-this.nearClip;if(j.set(t/a,(o-i)/o,s/n),j.mulScalar(2),j.sub(r.ONE),this._projection===p){e._getPerspectiveHalfSize(P,this.fov,this.aspectRatio,this.nearClip,this.horizontalFov),P.x*=j.x,P.y*=j.y;const t=this._node.getWorldTransform();P.z=-this.nearClip,t.transformPoint(P,y);const r=this._node.getPosition();h.sub2(y,r),h.normalize(),h.mulScalar(s),h.add(r)}else this._updateViewProjMat(),C.copy(this._viewProjMat).invert(),C.transformPoint(j,h);return h}_evaluateProjectionMatrix(){if(this._projMatDirty){if(this._projection===p)this._projMat.setPerspective(this.fov,this.aspectRatio,this.nearClip,this.farClip,this.horizontalFov),this._projMatSkybox.copy(this._projMat);else{const t=this._orthoHeight,e=t*this.aspectRatio;this._projMat.setOrtho(-e,e,-t,t,this.nearClip,this.farClip),this._projMatSkybox.setPerspective(this.fov,this.aspectRatio,this.nearClip,this.farClip)}this._projMatDirty=!1}}getProjectionMatrixSkybox(){return this._evaluateProjectionMatrix(),this._projMatSkybox}getExposure(){const t=Math.log2(this._aperture*this._aperture/this._shutter*100/this._sensitivity);return 1/(1.2*Math.pow(2,t))}getScreenSize(t){if(this._projection===p){const e=this._node.getPosition().distance(t.center);if(e<t.radius)return 1;const r=Math.asin(t.radius/e),i=Math.tan(r),a=Math.tan(this.fov/2*s.DEG_TO_RAD);return Math.min(i/a,1)}return s.clamp(t.radius/this._orthoHeight,0,1)}getFrustumCorners(t=this.nearClip,e=this.farClip){const r=this.fov*Math.PI/180;let i=this._projection===p?Math.tan(r/2)*t:this._orthoHeight,s=i*this.aspectRatio;const a=g;return a[0].x=s,a[0].y=-i,a[0].z=-t,a[1].x=s,a[1].y=i,a[1].z=-t,a[2].x=-s,a[2].y=i,a[2].z=-t,a[3].x=-s,a[3].y=-i,a[3].z=-t,this._projection===p&&(i=Math.tan(r/2)*e,s=i*this.aspectRatio),a[4].x=s,a[4].y=-i,a[4].z=-e,a[5].x=s,a[5].y=i,a[5].z=-e,a[6].x=-s,a[6].y=i,a[6].z=-e,a[7].x=-s,a[7].y=-i,a[7].z=-e,a}setXrProperties(t){Object.assign(this._xrProperties,t),this._projMatDirty=!0}}export{d as Camera};