UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 4.15 kB
import{disposeMaybe as e}from"../../../../core/maybe.js";import{createResolver as t}from"../../../../core/promiseUtils.js";import{d as r}from"../../../../chunks/vec4f64.js";import{RenderRequestType as s,Decorations as i}from"../lib/basicInterfaces.js";import{resampleHermite as o}from"../../../support/screenshotUtils.js";import{PixelFormat as n,DataType as a,TargetType as h,DepthStencilTargetType as c}from"../../../webgl/enums.js";import{FramebufferObject as l}from"../../../webgl/FramebufferObject.js";import{createEmptyImageData as d}from"../../../../core/imageUtils.js";class f{constructor(e,t){this.viewCamera=e,this.frameHasDecorations=t}}class u{constructor(e,t,r,s){this._rctx=e,this._renderFunctions=t,this._forceCameraHook=r,this._disposeOffscreenBuffers=s,this.supersample=!0,this._screenshotQueue=new Array}destroy(){this._rctx=null}async takeScreenshot(e){await this._renderFunctions.prepareOverlay(),this._renderFunctions.requestRenderScene(s.BACKGROUND);const r=t();return this._screenshotQueue.push({settings:e,resolver:r}),r.promise}update(e,t){for(const r of this._screenshotQueue){if(null==this._rctx){r.resolver.reject();continue}const s={...r.settings,pixelRatio:r.settings.pixelRatio*e.viewCamera.pixelRatio},i=this._renderScreenshot(e,s,t);r.resolver(i)}this._screenshotQueue.length=0}_renderScreenshotOverlay(e,t,r){e.width=t.width,e.height=t.height;const s=e.getContext("2d"),i=r.pixelRatio;return s.save(),s.translate(0,t.height),s.scale(1,-1),r.region&&s.translate(-r.region.x,-r.region.y),s.scale(i,i),t=this._renderFunctions.renderOverlay(e,t),s.restore(),t}_readbackScreenshot(e,t){return e.resample?this._readbackScreenshotResampled({...e,resample:e.resample},t):this._readbackScreenshotImmediate(e,t)}_readbackScreenshotResampled(e,t){const{framebufferWidth:r,framebufferHeight:s,region:i,resample:h}=e,c=this._ensureScreenshotEncodeCanvas();let l=d(r,s,c);this._rctx.gl.readPixels(0,0,r,s,n.RGBA,a.UNSIGNED_BYTE,new Uint8Array(l.data.buffer)),t(),l=this._renderScreenshotOverlay(c,l,{...e,region:void 0});const f=d(i.width,i.height,c);return o(l,f,!0,h.region.x,s-(h.region.y+h.region.height),h.region.width,h.region.height)}_readbackScreenshotImmediate(e,t){const{framebufferHeight:r,region:s}=e,i=this._ensureScreenshotEncodeCanvas(),o=d(s.width,s.height,i);return this._rctx.gl.readPixels(s.x,r-(s.y+s.height),s.width,s.height,n.RGBA,a.UNSIGNED_BYTE,new Uint8Array(o.data.buffer)),t(),this._renderScreenshotOverlay(i,o,e)}_renderScreenshot(t,s,o){let n=null,a=null;const d=t.viewCamera,{framebufferWidth:f,framebufferHeight:u}=s;let _=!1;const m=s.disableDecorations&&t.frameHasDecorations,g=f!==d.fullWidth||u!==d.fullHeight,p=s.ignorePadding&&d.pixelRatio!==s.pixelRatio,b=g||m||p||s.objectAndLayerIdColor;if(s.objectAndLayerIdColor&&(a=new l(this._rctx,{width:f,height:u,colorTarget:h.TEXTURE,depthStencilTarget:c.DEPTH_STENCIL_RENDER_BUFFER})),b){const e=d.clone();if(s.ignorePadding){const t=r(e.padding);for(let r=0;r<4;r++)t[r]=Math.round(t[r]/e.pixelRatio*s.pixelRatio);e.padding=t}e.fullWidth=f,e.fullHeight=u,e.pixelRatio=s.pixelRatio;const t=d.fovX-e.fovX,m=d.fovY-e.fovY;t<0&&t<m?e.fovX=d.fovX:m<0&&m<t&&(e.fovY=d.fovY),this._forceCameraHook(e),_=!0,n=new l(this._rctx,{width:e.fullWidth,height:e.fullHeight,colorTarget:h.TEXTURE,depthStencilTarget:c.DEPTH_STENCIL_RENDER_BUFFER}),this._renderFunctions.renderScene(n,a,e,s.disableDecorations?i.OFF:i.ON,o),this._disposeOffscreenBuffers()}const x=()=>{this._rctx.bindFramebuffer(null),e(n)},v=this._readbackScreenshot(s,x);x();let E=null;if(s.objectAndLayerIdColor){const t=()=>{this._rctx.bindFramebuffer(null),e(a)};this._rctx.bindFramebuffer(a),E=this._readbackScreenshot(s,t),this._rctx.bindFramebuffer(null),t()}if(b&&!this._rctx.contextAttributes.alpha)for(let e=3;e<v.data.length;e+=4)v.data[e]=255;if(E&&!this._rctx.contextAttributes.alpha)for(let e=3;e<E.data.length;e+=4)E.data[e]=255;return _&&this._forceCameraHook(d),[v,E]}_ensureScreenshotEncodeCanvas(){return this._screenshotEncodeCanvas||(this._screenshotEncodeCanvas=document.createElement("canvas")),this._screenshotEncodeCanvas}}export{f as ScreenshotContext,u as ScreenshotManager};