@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 8.89 kB
JavaScript
import{LAYERID_UI as e,LAYERID_DEPTH as t,ASPECT_AUTO as r}from"../../../scene/constants.js";import{Camera as s}from"../../../scene/camera.js";import{ShaderPass as a}from"../../../scene/shader-pass.js";import{Component as i}from"../component.js";import{PostEffectQueue as o}from"./post-effect-queue.js";class c extends i{constructor(t,r){super(t,r),this.onPostprocessing=null,this.onPreRender=null,this.onPostRender=null,this._renderSceneDepthMap=0,this._renderSceneColorMap=0,this._sceneDepthMapRequested=!1,this._sceneColorMapRequested=!1,this._priority=0,this._disablePostEffectsLayer=e,this._camera=new s,this._evtLayersChanged=null,this._evtLayerAdded=null,this._evtLayerRemoved=null,this._camera.node=r,this._postEffects=new o(t.app,this)}setShaderPass(e){const t=a.get(this.system.app.graphicsDevice),r=e?t.allocate(e,{isForward:!0}):null;return this._camera.shaderPassInfo=r,r.index}getShaderPass(){var e;return null==(e=this._camera.shaderPassInfo)?void 0:e.name}set renderPasses(e){this._camera.renderPasses=e}get renderPasses(){return this._camera.renderPasses}set aperture(e){this._camera.aperture=e}get aperture(){return this._camera.aperture}set aspectRatio(e){this._camera.aspectRatio=e}get aspectRatio(){return this._camera.aspectRatio}set aspectRatioMode(e){this._camera.aspectRatioMode=e}get aspectRatioMode(){return this._camera.aspectRatioMode}set calculateProjection(e){this._camera.calculateProjection=e}get calculateProjection(){return this._camera.calculateProjection}set calculateTransform(e){this._camera.calculateTransform=e}get calculateTransform(){return this._camera.calculateTransform}get camera(){return this._camera}set clearColor(e){this._camera.clearColor=e}get clearColor(){return this._camera.clearColor}set clearColorBuffer(e){this._camera.clearColorBuffer=e,this.dirtyLayerCompositionCameras()}get clearColorBuffer(){return this._camera.clearColorBuffer}set clearDepthBuffer(e){this._camera.clearDepthBuffer=e,this.dirtyLayerCompositionCameras()}get clearDepthBuffer(){return this._camera.clearDepthBuffer}set clearStencilBuffer(e){this._camera.clearStencilBuffer=e,this.dirtyLayerCompositionCameras()}get clearStencilBuffer(){return this._camera.clearStencilBuffer}set cullFaces(e){this._camera.cullFaces=e}get cullFaces(){return this._camera.cullFaces}set disablePostEffectsLayer(e){this._disablePostEffectsLayer=e,this.dirtyLayerCompositionCameras()}get disablePostEffectsLayer(){return this._disablePostEffectsLayer}set farClip(e){this._camera.farClip=e}get farClip(){return this._camera.farClip}set flipFaces(e){this._camera.flipFaces=e}get flipFaces(){return this._camera.flipFaces}set fov(e){this._camera.fov=e}get fov(){return this._camera.fov}get frustum(){return this._camera.frustum}set frustumCulling(e){this._camera.frustumCulling=e}get frustumCulling(){return this._camera.frustumCulling}set horizontalFov(e){this._camera.horizontalFov=e}get horizontalFov(){return this._camera.horizontalFov}set layers(e){const t=this._camera.layers;for(let e=0;e<t.length;e++){const r=this.system.app.scene.layers.getLayerById(t[e]);r&&r.removeCamera(this)}if(this._camera.layers=e,this.enabled&&this.entity.enabled)for(let t=0;t<e.length;t++){const r=this.system.app.scene.layers.getLayerById(e[t]);r&&r.addCamera(this)}}get layers(){return this._camera.layers}get layersSet(){return this._camera.layersSet}set jitter(e){this._camera.jitter=e}get jitter(){return this._camera.jitter}set nearClip(e){this._camera.nearClip=e}get nearClip(){return this._camera.nearClip}set orthoHeight(e){this._camera.orthoHeight=e}get orthoHeight(){return this._camera.orthoHeight}get postEffects(){return this._postEffects}get postEffectsEnabled(){return this._postEffects.enabled}set priority(e){this._priority=e,this.dirtyLayerCompositionCameras()}get priority(){return this._priority}set projection(e){this._camera.projection=e}get projection(){return this._camera.projection}get projectionMatrix(){return this._camera.projectionMatrix}set rect(e){this._camera.rect=e,this.fire("set:rect",this._camera.rect)}get rect(){return this._camera.rect}set renderSceneColorMap(e){e&&!this._sceneColorMapRequested?(this.requestSceneColorMap(!0),this._sceneColorMapRequested=!0):this._sceneColorMapRequested&&(this.requestSceneColorMap(!1),this._sceneColorMapRequested=!1)}get renderSceneColorMap(){return this._renderSceneColorMap>0}set renderSceneDepthMap(e){e&&!this._sceneDepthMapRequested?(this.requestSceneDepthMap(!0),this._sceneDepthMapRequested=!0):this._sceneDepthMapRequested&&(this.requestSceneDepthMap(!1),this._sceneDepthMapRequested=!1)}get renderSceneDepthMap(){return this._renderSceneDepthMap>0}set renderTarget(e){this._camera.renderTarget=e,this.dirtyLayerCompositionCameras()}get renderTarget(){return this._camera.renderTarget}set scissorRect(e){this._camera.scissorRect=e}get scissorRect(){return this._camera.scissorRect}set sensitivity(e){this._camera.sensitivity=e}get sensitivity(){return this._camera.sensitivity}set shutter(e){this._camera.shutter=e}get shutter(){return this._camera.shutter}get viewMatrix(){return this._camera.viewMatrix}_enableDepthLayer(e){if(this.layers.find((e=>e===t))){const r=this.system.app.scene.layers.getLayerById(t);e?null==r||r.incrementCounter():null==r||r.decrementCounter()}else if(e)return!1;return!0}requestSceneColorMap(e){this._renderSceneColorMap+=e?1:-1,this._enableDepthLayer(e),this.camera._enableRenderPassColorGrab(this.system.app.graphicsDevice,this.renderSceneColorMap)}requestSceneDepthMap(e){this._renderSceneDepthMap+=e?1:-1,this._enableDepthLayer(e),this.camera._enableRenderPassDepthGrab(this.system.app.graphicsDevice,this.system.app.renderer,this.renderSceneDepthMap)}dirtyLayerCompositionCameras(){this.system.app.scene.layers._dirty=!0}screenToWorld(e,t,r,s){const a=this.system.app.graphicsDevice,i=a.clientRect.width,o=a.clientRect.height;return this._camera.screenToWorld(e,t,r,i,o,s)}worldToScreen(e,t){const r=this.system.app.graphicsDevice,s=r.clientRect.width,a=r.clientRect.height;return this._camera.worldToScreen(e,s,a,t)}onAppPrerender(){this._camera._viewMatDirty=!0,this._camera._viewProjMatDirty=!0}addCameraToLayers(){const e=this.layers;for(let t=0;t<e.length;t++){const r=this.system.app.scene.layers.getLayerById(e[t]);r&&r.addCamera(this)}}removeCameraFromLayers(){const e=this.layers;for(let t=0;t<e.length;t++){const r=this.system.app.scene.layers.getLayerById(e[t]);r&&r.removeCamera(this)}}onLayersChanged(e,t){this.addCameraToLayers(),e.off("add",this.onLayerAdded,this),e.off("remove",this.onLayerRemoved,this),t.on("add",this.onLayerAdded,this),t.on("remove",this.onLayerRemoved,this)}onLayerAdded(e){this.layers.indexOf(e.id)<0||e.addCamera(this)}onLayerRemoved(e){this.layers.indexOf(e.id)<0||e.removeCamera(this)}onEnable(){var e;const t=this.system.app.scene,r=t.layers;var s,a;(this.system.addCamera(this),null==(e=this._evtLayersChanged)||e.off(),this._evtLayersChanged=t.on("set:layers",this.onLayersChanged,this),r)&&(null==(s=this._evtLayerAdded)||s.off(),this._evtLayerAdded=r.on("add",this.onLayerAdded,this),null==(a=this._evtLayerRemoved)||a.off(),this._evtLayerRemoved=r.on("remove",this.onLayerRemoved,this));this.enabled&&this.entity.enabled&&this.addCameraToLayers(),this.postEffects.enable()}onDisable(){var e;const t=this.system.app.scene.layers;var r,s;(this.postEffects.disable(),this.removeCameraFromLayers(),null==(e=this._evtLayersChanged)||e.off(),this._evtLayersChanged=null,t)&&(null==(r=this._evtLayerAdded)||r.off(),this._evtLayerAdded=null,null==(s=this._evtLayerRemoved)||s.off(),this._evtLayerRemoved=null);this.system.removeCamera(this)}onRemove(){this.onDisable(),this.off(),this.camera.destroy()}calculateAspectRatio(e){const t=this.system.app.graphicsDevice,r=e?e.width:t.width,s=e?e.height:t.height;return r*this.rect.z/(s*this.rect.w)}frameUpdate(e){this.aspectRatioMode===r&&(this.aspectRatio=this.calculateAspectRatio(e))}startXr(e,t,r){this.system.app.xr.start(this,e,t,r)}endXr(e){this._camera.xr?this._camera.xr.end(e):e&&e(new Error("Camera is not in XR"))}copy(e){this.aperture=e.aperture,this.aspectRatio=e.aspectRatio,this.aspectRatioMode=e.aspectRatioMode,this.calculateProjection=e.calculateProjection,this.calculateTransform=e.calculateTransform,this.clearColor=e.clearColor,this.clearColorBuffer=e.clearColorBuffer,this.clearDepthBuffer=e.clearDepthBuffer,this.clearStencilBuffer=e.clearStencilBuffer,this.cullFaces=e.cullFaces,this.disablePostEffectsLayer=e.disablePostEffectsLayer,this.farClip=e.farClip,this.flipFaces=e.flipFaces,this.fov=e.fov,this.frustumCulling=e.frustumCulling,this.horizontalFov=e.horizontalFov,this.layers=e.layers,this.nearClip=e.nearClip,this.orthoHeight=e.orthoHeight,this.priority=e.priority,this.projection=e.projection,this.rect=e.rect,this.renderTarget=e.renderTarget,this.scissorRect=e.scissorRect,this.sensitivity=e.sensitivity,this.shutter=e.shutter}}export{c as CameraComponent};