@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 2.23 kB
JavaScript
import{FILTER_NEAREST as e,ADDRESS_CLAMP_TO_EDGE as r,PIXELFORMAT_DEPTHSTENCIL as t,PIXELFORMAT_DEPTH as s,PIXELFORMAT_R32F as a}from"../../platform/graphics/constants.js";import{RenderPass as d}from"../../platform/graphics/render-pass.js";import{RenderTarget as h}from"../../platform/graphics/render-target.js";import{Texture as i}from"../../platform/graphics/texture.js";const l=["uSceneDepthMap","uDepthMap"];class n extends d{constructor(e,r){super(e),this.depthRenderTarget=null,this.camera=null,this.camera=r}destroy(){super.destroy(),this.releaseRenderTarget(this.depthRenderTarget)}shouldReallocate(e,r){const t=(null==r?void 0:r.width)||this.device.width,s=(null==r?void 0:r.height)||this.device.height;return!e||t!==e.width||s!==e.height}allocateRenderTarget(t,s,a,d,n){const o=new i(a,{name:l[0],format:d,width:s?s.colorBuffer.width:a.width,height:s?s.colorBuffer.height:a.height,mipmaps:!1,minFilter:e,magFilter:e,addressU:r,addressV:r});return t?(t.destroyFrameBuffers(),n?t._depthBuffer=o:(t._colorBuffer=o,t._colorBuffers=[o])):t=new h({name:"DepthGrabRT",colorBuffer:n?null:o,depthBuffer:n?o:null,depth:!n,stencil:a.supportsStencil,autoResolve:!1}),t}releaseRenderTarget(e){e&&(e.destroyTextureBuffers(),e.destroy())}before(){var e,r,d,h;const i=this.camera,n=this.device,o=null!=(e=null==i?void 0:i.renderTarget)?e:n.backBuffer;let p=!0,f=o.stencil?t:s;if(n.isWebGPU){o.samples>1&&(f=a,p=!1)}const c=null!=(r=null==(d=i.renderTarget)?void 0:d.depthBuffer)?r:null==(h=i.renderTarget)?void 0:h.colorBuffer;this.shouldReallocate(this.depthRenderTarget,c)&&(this.releaseRenderTarget(this.depthRenderTarget),this.depthRenderTarget=this.allocateRenderTarget(this.depthRenderTarget,i.renderTarget,n,f,p));const u=p?this.depthRenderTarget.depthBuffer:this.depthRenderTarget.colorBuffer;l.forEach((e=>n.scope.resolve(e).setValue(u)))}execute(){const e=this.device;if(e.isWebGL2&&e.renderTarget.samples>1){const r=e.renderTarget.impl._glFrameBuffer,t=this.depthRenderTarget;e.renderTarget=t,e.updateBegin(),this.depthRenderTarget.impl.internalResolve(e,r,t.impl._glFrameBuffer,this.depthRenderTarget,e.gl.DEPTH_BUFFER_BIT)}else e.copyRenderTarget(e.renderTarget,this.depthRenderTarget,!1,!0)}}export{n as RenderPassDepthGrab};