@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 2.23 kB
JavaScript
import{FILTER_LINEAR_MIPMAP_LINEAR as e,FILTER_LINEAR as r,ADDRESS_CLAMP_TO_EDGE as t}from"../../platform/graphics/constants.js";import{RenderPass as o}from"../../platform/graphics/render-pass.js";import{RenderTarget as s}from"../../platform/graphics/render-target.js";import{Texture as i}from"../../platform/graphics/texture.js";const l=["uSceneColorMap","texture_grabPass"];class a extends o{constructor(...e){super(...e),this.colorRenderTarget=null,this.source=null}destroy(){super.destroy(),this.releaseRenderTarget(this.colorRenderTarget)}shouldReallocate(e,r,t){if((null==e?void 0:e.colorBuffer.format)!==t)return!0;const o=(null==r?void 0:r.width)||this.device.width,s=(null==r?void 0:r.height)||this.device.height;return!e||o!==e.width||s!==e.height}allocateRenderTarget(o,a,c,d){const n=c.isWebGL2,h=new i(c,{name:l[0],format:d,width:a?a.colorBuffer.width:c.width,height:a?a.colorBuffer.height:c.height,mipmaps:n,minFilter:n?e:r,magFilter:r,addressU:t,addressV:t});return o?(o.destroyFrameBuffers(),o._colorBuffer=h,o._colorBuffers=[h]):o=new s({name:"ColorGrabRT",colorBuffer:h,depth:!1,stencil:!1,autoResolve:!1}),o}releaseRenderTarget(e){e&&(e.destroyTextureBuffers(),e.destroy())}frameUpdate(){var e;const r=this.device,t=this.source,o=null!=(e=null==t?void 0:t.colorBuffer.format)?e:this.device.backBufferFormat;this.shouldReallocate(this.colorRenderTarget,null==t?void 0:t.colorBuffer,o)&&(this.releaseRenderTarget(this.colorRenderTarget),this.colorRenderTarget=this.allocateRenderTarget(this.colorRenderTarget,t,r,o));const s=this.colorRenderTarget.colorBuffer;l.forEach((e=>r.scope.resolve(e).setValue(s)))}execute(){const e=this.device,r=this.source,t=this.colorRenderTarget.colorBuffer;if(e.isWebGPU)e.copyRenderTarget(r,this.colorRenderTarget,!0,!1),e.mipmapRenderer.generate(this.colorRenderTarget.colorBuffer.impl);else if(e.isWebGL2)e.copyRenderTarget(r,this.colorRenderTarget,!0,!1),e.activeTexture(e.maxCombinedTextures-1),e.bindTexture(t),e.gl.generateMipmap(t.impl._glTarget);else{t.impl._glTexture||t.impl.initialize(e,t),e.bindTexture(t);const r=e.gl;r.copyTexImage2D(r.TEXTURE_2D,0,t.impl._glFormat,0,0,t.width,t.height,0),t._needsUpload=!1,t._needsMipmapsUpload=!1}}}export{a as RenderPassColorGrab};