@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 3.87 kB
JavaScript
import{LAYERID_SKYBOX as s,LAYERID_IMMEDIATE as e}from"../../scene/constants.js";import{PIXELFORMAT_RGBA8 as a,FILTER_LINEAR as t,ADDRESS_CLAMP_TO_EDGE as r,PIXELFORMAT_DEPTH as o,FILTER_NEAREST as i}from"../../platform/graphics/constants.js";import{Texture as n}from"../../platform/graphics/texture.js";import{RenderPass as l}from"../../platform/graphics/render-pass.js";import{RenderPassColorGrab as p}from"../../scene/graphics/render-pass-color-grab.js";import{RenderPassForward as h}from"../../scene/renderer/render-pass-forward.js";import{RenderTarget as c}from"../../platform/graphics/render-target.js";import{RenderPassBloom as d}from"./render-pass-bloom.js";import{RenderPassCompose as m}from"./render-pass-compose.js";import{RenderPassTAA as b}from"./render-pass-taa.js";import{RenderPassPrepass as P}from"./render-pass-prepass.js";class f extends l{constructor(s,e={}){super(s.graphicsDevice),this.app=void 0,this.prePass=void 0,this.scenePass=void 0,this.composePass=void 0,this.bloomPass=void 0,this.taaPass=void 0,this._bloomEnabled=!0,this._renderTargetScale=1,this._rt=null,this.app=s,this.options=this.sanitizeOptions(e),this.setupRenderPasses(this.options)}destroy(){this._rt&&(this._rt.destroyTextureBuffers(),this._rt.destroy(),this._rt=null),this.beforePasses.forEach((s=>s.destroy())),this.beforePasses=null}sanitizeOptions(a){const t={camera:null,samples:2,sceneColorMap:!0,lastGrabLayerId:s,lastGrabLayerIsTransparent:!1,lastSceneLayerId:e,lastSceneLayerIsTransparent:!0,taaEnabled:!1};return Object.assign({},t,a)}set renderTargetScale(s){this._renderTargetScale=s,this.scenePass&&(this.scenePass.options.scaleX=s,this.scenePass.options.scaleY=s)}get renderTargetScale(){return this._renderTargetScale}set bloomEnabled(s){this._bloomEnabled!==s&&(this._bloomEnabled=s,this.composePass.bloomTexture=s?this.bloomPass.bloomTexture:null,this.bloomPass.enabled=s)}get bloomEnabled(){return this._bloomEnabled}set lastMipLevel(s){this.bloomPass.lastMipLevel=s}get lastMipLevel(){return this.bloomPass.lastMipLevel}setupRenderPasses(s){const{app:e,device:l}=this,{scene:f,renderer:u}=e,g=f.layers,T=s.camera,y=T.renderTarget,S=l.getRenderableHdrFormat()||a,v=new n(l,{name:"SceneColor",width:4,height:4,format:S,mipmaps:!1,minFilter:t,magFilter:t,addressU:r,addressV:r}),L=new n(l,{name:"SceneDepth",width:4,height:4,format:o,mipmaps:!1,minFilter:i,magFilter:i,addressU:r,addressV:r}),E=new c({colorBuffer:v,depthBuffer:L,samples:s.samples});this._rt=E;const w={resizeSource:y,scaleX:this.renderTargetScale,scaleY:this.renderTargetScale};s.prepassEnabled&&(this.prePass=new P(l,f,u,T,L,w)),this.scenePass=new h(l,g,f,u),this.scenePass.init(E,w),s.prepassEnabled&&(this.scenePass.noDepthClear=!0,this.scenePass.depthStencilOps.storeDepth=!0);const _=s.sceneColorMap?s.lastGrabLayerId:s.lastSceneLayerId,j=s.sceneColorMap?s.lastGrabLayerIsTransparent:s.lastSceneLayerIsTransparent;let x,I,M=!0,D=0;D=this.scenePass.addLayers(g,T,D,M,_,j),M=!1,s.sceneColorMap&&(x=new p(l),x.source=E,I=new h(l,g,f,u),I.init(E),D=I.addLayers(g,T,D,M,s.lastSceneLayerId,s.lastSceneLayerIsTransparent),s.prepassEnabled&&(I.depthStencilOps.storeDepth=!0));let C=v;s.taaEnabled&&(this.taaPass=new b(l,v,T),C=this.taaPass.historyTexture),this.bloomPass=new d(e.graphicsDevice,C,S),this.composePass=new m(e.graphicsDevice),this.composePass.bloomTexture=this.bloomPass.bloomTexture,this.composePass.taaEnabled=s.taaEnabled,this.composePass.init(y);const F=new h(l,g,f,u);F.init(y),F.addLayers(g,T,D,M);const O=[this.prePass,this.scenePass,x,I,this.taaPass,this.bloomPass,this.composePass,F];this.beforePasses=O.filter((s=>void 0!==s))}frameUpdate(){var s,e;super.frameUpdate();const a=null!=(s=null==(e=this.taaPass)?void 0:e.update())?s:this._rt.colorBuffer;this.composePass.sceneTexture=a,this.bloomEnabled&&(this.bloomPass.sourceTexture=a)}}export{f as RenderPassCameraFrame};