@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 9.11 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.19/LICENSE.txt */
import{__decorate as e}from"tslib";import{releaseMaybe as r}from"../../../../../core/maybe.js";import{property as t,subclass as o}from"../../../../../core/accessorSupport/decorators.js";import{ZEROS as n}from"../../../../../core/libs/gl-matrix-2/factories/vec4f64.js";import{RenderNodeOutput as i}from"../../../webgl.js";import s from"../../../webgl/RenderNode.js";import{If as a,glsl as c}from"../../core/shaderModules/glsl.js";import{Pos2Locations as u}from"../../lib/DefaultVertexBufferLayouts.js";import{checkWebGLError as l}from"../../../../webgl/checkWebGLError.js";import{ColorAttachment0 as h,SizedPixelFormat as p,DataType as d}from"../../../../webgl/enums.js";let f=class extends s{constructor(e){super(e),this.destroyedCB=null,this.produces=i.FINAL,this.consumes={required:[i.FINAL]},this.clearColor=n,this._focusedFBOType=7,this._program=new Map}destroy(){this._program.forEach(e=>e.dispose()),this._program.clear(),this.destroyedCB()}render(e){const r=e.find(({name:e})=>e===i.FINAL);if(!this._focusedTexture)return r;const t=this.renderingContext;t.bindFramebuffer(r.fbo),t.setClearColor(0,0,0,0),t.clear(16384);const o=this._focusedTexture.attachment,n=[g(o)],s=this._ensureProgram(t,!1,n);return t.useProgram(s),t.bindTexture(o,0),s.setUniform1i("colorTex0",0),s.setUniform1i("inputType",this._focusedFBOType),3===this._focusedFBOType&&s.setUniform2fv("nearFar",this.camera.nearFar),t.screen.draw(),r}getDownscaledFBO(e,r,t,o,n){r&&(o=e.width,n=e.height);const i="linear depth"===t||t.includes("shadow"),s="overlay highlight"===t||"highlights"===t||"highlight mix"===t,a="highlight coverage"===t,c=e.colorTexture?.descriptor,u=c?.internalFormat,p=[],d=new Array,f=new Array;e.colorAttachments.forEach((r,t)=>{const o=e.getColorTexture(r);if(o){const e=g(o);p.push(e);const{descriptor:r}=o;"float"!==e||r.hasMipmap||r.isImmutable||(o.generateMipmap(),d[t]=!0)}if(t>0){const t=e.getColorTexture(r)?.descriptor.internalFormat;return void f.push(x(t))}f.push(i?0:s?1:a?2:x(u))});const m=this.renderingContext,{depthStencilTexture:v}=e,F=this._ensureProgram(m,null!=v,p),T=this.fboCache.acquire(o,n,"fbo visualizer");m.useProgram(F),m.bindFramebuffer(T.fbo);const b=[];e.colorAttachments.forEach((r,t)=>{const o=`colorTex${t.toFixed()}`;m.bindTexture(e.getColorTexture(r),t),F.setUniform1i(o,t),t>0&&T.acquireColor(h+t,5,"fbo visualizer"),b.push(r)});const C=v?.descriptor.linearFilterDepth??!1;if(v){const r=e.colorAttachments.length;v.setShadowFiltering(!1),m.bindTexture(v,r),F.setUniform1i("depthTex",r);const t=h+r;t>h&&T.acquireColor(t,5,"fbo visualizer"),b.push(t)}m.gl.drawBuffers(b),l(m.gl);const R=m.getViewport();return m.setViewport(0,0,o,n),m.setClearColor(0,0,0,0),m.clear(16384),m.setBlendingEnabled(!0),m.setBlendFunction(1,771),F.setUniform1iv("inputTypes",f),F.setUniform2fv("nearFar",this.camera.nearFar),m.screen.draw(),l(m.gl),m.bindFramebuffer(null),m.setViewport(R.x,R.y,R.width,R.height),r&&(this._focusedTexture?.release(),this._focusedTexture=T.getAttachment(h),this._focusedTexture?.retain(),this._focusedFBOType=f[0]),e.colorAttachments.forEach((r,t)=>{if(d[t]){const t=e.getColorTexture(r);t?.clearMipmap()}}),v&&v.setShadowFiltering(C),T}clearFocusedFBO(){this._focusedTexture=r(this._focusedTexture)}getPreviewContent(e,r,t,o){const n=this.renderingContext,i=o.includes("\ndepth"),s=this.fboCache.acquire(e,r,"fbo visualizer");n.bindFramebuffer(s?.fbo);const a=n.getViewport();n.setViewport(0,0,e,r),n.setClearColor(this.clearColor[0],this.clearColor[1],this.clearColor[2],this.clearColor[3]),n.clear(16384),n.setBlendingEnabled(!0),n.setBlendFunction(1,771);const c=t.attachment,u=this._ensureProgram(n,!1,[g(c)]);n.useProgram(u),n.bindTexture(c,0),u.setUniform1i("colorTex0",0),u.setUniform1iv("inputTypes",[i||c.descriptor.internalFormat!==p.RGBA16F?7:5]),n.screen.draw();const h=new ImageData(new Uint8ClampedArray(e*r*4),e,r);return n.gl.readPixels(0,0,e,r,6408,d.UNSIGNED_BYTE,new Uint8Array(h.data.buffer)),l(n.gl),n.bindFramebuffer(null),n.setViewport(a.x,a.y,a.width,a.height),s?.release(),h}_ensureProgram(e,r,t){const o=v({hasDepthAttachment:r,colorSamplerTypes:t}),n=this._program.get(o);if(n)return n;const i="#version 300 es\n in vec2 position;\n out vec2 uv;\n\n void main() {\n gl_Position = vec4(position, 0.0, 1.0);\n uv = position * 0.5 + vec2(0.5);\n }",s=e=>{let r="";for(let t=0;t<e.length;++t){const o=e[t];r+=`layout(location = ${c.int(t)}) out vec4 fragColor${c.int(t)};\n uniform ${m(o)} colorTex${c.int(t)};`}return r},l=e=>e.reduce((e,r,t)=>e+`{\n int inputType = inputTypes[${t}];\n if (inputType == ${3..toFixed()}) {\n fragColor${t} = vec4(vec3(texture(colorTex${t}, uv).r), 1.0);\n } else if (inputType == ${4..toFixed()}) {\n fragColor${t} = vec4(texture(colorTex${t}, uv).rg, 0.0, 1.0);\n } else if (inputType == ${(0).toFixed()}) {\n float depth = 1.0 - ((-linearDepthFromTexture(colorTex${t}, uv) - nearFar[0]) / (nearFar[1] - nearFar[0]));\n fragColor${t} = vec4(vec3(depth), depth >= 0.999 ? 0.0 : 1.0);\n } else if (inputType == ${5..toFixed()}) {\n fragColor${t} = vec4(texture(colorTex${t}, uv).rgb, 1.0);\n } else if (inputType == ${1..toFixed()}) {\n fragColor${t} = vec4(texture(colorTex${t}, uv)) * 255.0;\n fragColor${t} = vec4(fragColor${t}.rgb / fragColor${t}.a, fragColor${t}.a);\n } else if(inputType == ${2..toFixed()}) {\n vec2 texDim = vec2(textureSize(colorTex${t}, 0));\n ivec2 iuv = ivec2(uv*texDim);\n uvec2 hh = uvec2(texelFetch(colorTex${t},iuv,0).rg);\n fragColor${t} = vec4(\n ((hh & uvec2(0x55u)) != uvec2(0u)) ? 1.0 : 0.0,\n ((hh & uvec2(0xaau)) != uvec2(0u)) ? 1.0 : 0.0,\n 0.0,\n 1.0);\n } else {\n fragColor${t} = vec4(texture(colorTex${t}, uv));\n }\n }`,""),h=t.length,p=`#version 300 es\n precision highp float;\n precision highp usampler2D;\n\n in vec2 uv;\n ${s(t)}\n ${a(r,`layout(location = ${c.int(h)}) out vec4 fragDepth;\n uniform sampler2D depthTex;`)}\n\n uniform int inputTypes[${t.length}];\n uniform vec2 nearFar;\n\n // Factors to convert rgba back to float\n const vec4 RGBA_2_FLOAT_FACTORS = vec4(\n 255.0 / (256.0),\n 255.0 / (256.0 * 256.0),\n 255.0 / (256.0 * 256.0 * 256.0),\n 255.0 / (256.0 * 256.0 * 256.0 * 256.0)\n );\n\n\n float rgbaToFloat(vec4 rgba) {\n // Convert components from 0->1 back to 0->255 and then add the components together with their corresponding\n // fixed point factors, i.e. (256^1, 256^2, 256^3, 256^4)\n return dot(rgba, RGBA_2_FLOAT_FACTORS);\n }\n\n float linearDepthFromFloat(float depth) {\n depth = pow(depth, 0.2);\n return -(depth * (nearFar[1] - nearFar[0]) + nearFar[0]);\n }\n\n float linearDepthFromRGBA(vec4 depth) {\n return linearDepthFromFloat(rgbaToFloat(depth));\n }\n\n ${"float"===t[0]?"float linearDepthFromTexture(sampler2D depthTex, vec2 uv) {\n ivec2 iuv = ivec2(uv * vec2(textureSize(depthTex, 0)));\n return linearDepthFromRGBA(texelFetch(depthTex, iuv, 0));\n }":"float linearDepthFromTexture(usampler2D depthTex, vec2 uv) {\n ivec2 iuv = ivec2(uv * vec2(textureSize(depthTex, 0)));\n return linearDepthFromRGBA(vec4(texelFetch(depthTex, iuv, 0)) * 255.0);\n }"}\n\n void main() {\n ${l(t)}\n ${a(r,"float depth = 1.0 - pow(texture(depthTex, uv).r, 10.0);\n fragDepth = vec4(vec3(depth), depth < 0.000001 ? 0.0 : 1.0);")}\n }`,d=e.programCache.acquire(i,p,u);return this._program.set(o,d),d}};function m(e){switch(e){case"float":return"sampler2D";case"uint":return"usampler2D";case"int":return"isampler2D"}}function g(e){if(!e)return"float";const{internalFormat:r}=e.descriptor;switch(r){case p.R8I:case p.R16I:case p.R32I:case p.RG8I:case p.RG16I:case p.RG32I:case p.RGB8I:case p.RGB16I:case p.RGB32I:case p.RGBA8I:case p.RGBA16I:case p.RGBA32I:return"int";case p.R8UI:case p.R16UI:case p.R32UI:case p.RG8UI:case p.RG16UI:case p.RG32UI:case p.RGB8UI:case p.RGB16UI:case p.RGB32UI:case p.RGBA8UI:case p.RGBA16UI:case p.RGBA32UI:return"uint";default:return"float"}}function v(e){const{hasDepthAttachment:r,colorSamplerTypes:t}=e;return`${r?"Depth":""}${t.reduceRight((e,r)=>e+`|C${r}`,"")}`}function x(e){switch(e){case p.R16F:case p.R32F:case p.R8:return 3;case p.RG8:return 4;case p.RGBA16F:return 5;case p.RG8UI:return 6;default:return 7}}e([t()],f.prototype,"destroyedCB",void 0),e([t()],f.prototype,"produces",void 0),e([t()],f.prototype,"consumes",void 0),e([t()],f.prototype,"clearColor",void 0),f=e([o("esri.views.3d.webgl-engine.effects.debug.RenderNodeVisualizerNode")],f);export{f as RenderNodeVisualizerNode};