UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 6.62 kB
import{isSome as e}from"../../../../core/maybe.js";import{RenderTargetHelper as t}from"./RenderTargetHelper.js";import{TransparencyPassType as r}from"./TransparencyPassType.js";import{AlphaMode as i}from"../shaders/CompositingTechniqueConfiguration.js";import{ContextType as o}from"../../../webgl/context-util.js";import{TextureSamplingMode as s,SizedPixelFormat as a,PixelFormat as h,PixelType as n,ClearBufferBit as l}from"../../../webgl/enums.js";class g{constructor(e,r){this._rctx=e,this._compositingHelper=r,this._mainColorTarget=0,this._dimensions={width:4,height:4},this._background={type:"color",color:[0,0,0,1]};const i=e.type===o.WEBGL2;this._renderTargetHelper=new t(e);const l=this._renderTargetHelper;this._mainColorTargets=[l.registerColorTarget({name:"mainColorTarget0"}),l.registerColorTarget({name:"mainColorTarget1"})],this.frontFaceTarget=l.registerColorTarget({name:"frontFaceTarget"});const g=e=>l.registerColorTarget({name:e,dataType:n.FLOAT,internalFormat:i?a.RGBA32F:h.RGBA,samplingMode:s.NEAREST});this.colorFloatTarget=g("colorFloatTarget"),this.alphaFloatTarget=g("alphaFloatTarget"),this.mainDepth=l.registerDepthTarget({name:"mainDepth"}),this.linearDepth=l.registerColorTarget({name:"linearDepth",samplingMode:s.NEAREST}),this.terrainLinearDepth=l.registerColorTarget({name:"terrainLinearDepth"}),this.geometryLinearDepth=l.registerColorTarget({name:"geometryLinearDepth"}),this.normal=l.registerColorTarget({name:"normal"}),this.highlight=l.registerColorTarget({name:"highlight",internalFormat:i?a.RGBA4:h.RGBA,dataType:n.UNSIGNED_SHORT_4_4_4_4}),this.hudVisibility=l.registerColorTarget({name:"hudVisibility",internalFormat:i?a.RGBA4:h.RGBA,dataType:n.UNSIGNED_SHORT_4_4_4_4}),this.tmpColor=l.registerColorTarget({name:"tmpColor"}),this.tmpDepth=l.registerDepthTarget({name:"tmpDepth"}),this.hudColor=l.registerColorTarget({name:"hudColor"})}dispose(){this._renderTargetHelper.dispose()}get width(){return this._dimensions.width}get height(){return this._dimensions.height}set background(e){this._background=e}get background(){return this._background}get currentColorTarget(){return this._mainColorTargets[this._mainColorTarget]}get previousColorTarget(){return this._mainColorTargets[1-this._mainColorTarget]}get framebuffer(){return this.getFramebuffer(this.currentColorTarget,this.mainDepth)}getFramebuffer(e,t){return this._renderTargetHelper.getFramebuffer(this._dimensions,e,t)}get colorTexture(){return this._renderTargetHelper.getAllocatedColorTexture(this.currentColorTarget)}get depthTexture(){return this._renderTargetHelper.getAllocatedDepthTexture(this.mainDepth)}get linearDepthTexture(){return this._renderTargetHelper.getAllocatedColorTexture(this.linearDepth)}get terrainLinearDepthTexture(){return this._renderTargetHelper.getAllocatedColorTexture(this.terrainLinearDepth)}get geometryLinearDepthTexture(){return this._renderTargetHelper.getAllocatedColorTexture(this.geometryLinearDepth)}get lastFrameColorTexture(){return this._renderTargetHelper.getAllocatedColorTexture(this.previousColorTarget)}get normalTexture(){return this._renderTargetHelper.getAllocatedColorTexture(this.normal)}get highlightTexture(){return this._renderTargetHelper.getAllocatedColorTexture(this.highlight)}get hudVisibilityTexture(){return this._getColorTexture(this.hudVisibility)}get tmpColorTexture(){return this._getColorTexture(this.tmpColor)}get hudColorTexture(){return this._getColorTexture(this.hudColor)}get mainColorTexture(){return this._getColorTexture(this.currentColorTarget)}setupRenderTarget(e){e||(this._mainColorTarget=0,this.disposeTarget(this._mainColorTargets[1])),this._mainColorTarget=0===this._mainColorTarget&&e?1:0}initializeFrame(e){const t=this._rctx;this._dimensions.width=e.fullWidth,this._dimensions.height=e.fullHeight,this.bindTarget(this.currentColorTarget,this.mainDepth),t.setClearStencil(0);const r=this._background.color;t.setClearColor(r[0]*r[3],r[1]*r[3],r[2]*r[3],r[3]),t.clearSafe(l.COLOR_BUFFER_BIT|l.DEPTH_BUFFER_BIT|l.STENCIL_BUFFER_BIT)}composite(t){e(this.colorTexture)&&this._compositingHelper.composite(t,this.colorTexture,i.None)}renderTmpAndCompositeToMain(e,t,r,i=!1){this.renderToTargets(e,this.tmpColor,i?this.tmpDepth:this.mainDepth,T),this._compositingHelper.composite(t,this._getColorTexture(this.tmpColor),r)}renderHUDVisibility(e,t=!1){this.renderToTargets(e,this.hudVisibility,t?this.tmpDepth:this.mainDepth,p)}compositeTransparentTerrainOntoHUDVisibility(e){this.renderToTargets((()=>this._compositingHelper.compositeHUD(e,this._getColorTexture(this.tmpColor))),this.hudVisibility,this.tmpDepth)}renderOITPass(e,t,i){let o,s;switch(t){case r.Color:o=this.colorFloatTarget,s=[0,0,0,0];break;case r.Alpha:o=this.alphaFloatTarget,s=[1,1,1,1];break;case r.FrontFace:o=this.frontFaceTarget,s=[0,0,0,0]}i?this.renderToTargets(e,o,this.tmpDepth,s,!0,!0):this.renderToTargets(e,o,this.mainDepth,s,!1)}compositeTransparentTerrainOntoMain(e){this.bindFramebuffer(),this._compositingHelper.composite(e,this._getColorTexture(this.tmpColor),i.PremultipliedAlpha)}compositeOccludedOntoMain(e,t){this.bindFramebuffer(),this._compositingHelper.composite(e,this._getColorTexture(this.tmpColor),i.PremultipliedAlpha,t)}compositeTransparentOntoOpaque(e,t){t?(this.bindTarget(this.hudColor,this.tmpDepth),this._rctx.setClearColor(0,0,0,1e-13),this._rctx.clearSafe(l.COLOR_BUFFER_BIT)):this.bindFramebuffer(),this._compositingHelper.compositeOIT(e,this._getColorTexture(this.colorFloatTarget),this._getColorTexture(this.alphaFloatTarget),this._getColorTexture(this.frontFaceTarget))}bindFramebuffer(){this._rctx.bindFramebuffer(this.framebuffer)}renderDepthDetached(e){this.bindTarget(this.currentColorTarget),e(),this.bindTarget(this.currentColorTarget,this.mainDepth)}disposeTarget(e){this._renderTargetHelper.disposeTargetResource(e)}renderToFBO(e,t,r=!1,i=!1){const o=this._rctx;let s=0;if(t){const e=1e-13,r=Math.max(e,t[3]);o.setClearColor(t[0],t[1],t[2],r),s|=l.COLOR_BUFFER_BIT}r&&(s|=l.DEPTH_BUFFER_BIT),!1===i?i=0:(!0===i&&(i=255),s|=l.STENCIL_BUFFER_BIT),s&&o.clearSafe(s,i),e(),o.gl.flush(),this.bindTarget(this.currentColorTarget,this.mainDepth)}renderToTargets(e,t,r,i,o=!1,s=!1){const a=this.bindTarget(t,r);return this.renderToFBO(e,i,o,s),a}bindTarget(e,t){const r=this._renderTargetHelper.getFramebuffer(this._dimensions,e,t);return this._rctx.bindFramebuffer(r),r}_getColorTexture(e){return this._renderTargetHelper.getColorTexture(e,this._dimensions)}get gpuMemoryUsage(){let e=0;return this._renderTargetHelper&&(e+=this._renderTargetHelper.gpuMemoryUsage),e}}const T=[0,0,0,0],p=[0,1,0,1];export{g as OffscreenRendering};