UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 2.17 kB
import{Color as e}from"../../core/math/color.js";import{PIXELFORMAT_RGBA8 as r,FILTER_NEAREST as t,ADDRESS_CLAMP_TO_EDGE as s}from"../../platform/graphics/constants.js";import{GraphicsDevice as i}from"../../platform/graphics/graphics-device.js";import{RenderTarget as a}from"../../platform/graphics/render-target.js";import{Texture as h}from"../../platform/graphics/texture.js";import{Camera as o}from"../../scene/camera.js";import{Layer as n}from"../../scene/layer.js";import{getApplication as d}from"../globals.js";import{RenderPassPicker as c}from"./render-pass-picker.js";const p=new Set;class g{constructor(e,r,t){this.renderTarget=null,this.mapping=new Map,e instanceof i&&(e=d()),this.renderer=e.renderer,this.device=e.graphicsDevice,this.renderPass=new c(this.device,e.renderer),this.width=0,this.height=0,this.resize(r,t)}getSelection(e,r,t=1,s=1){const i=this.device;r=this.renderTarget.height-(r+s),e=Math.floor(e),r=Math.floor(r),t=Math.floor(Math.max(t,1)),s=Math.floor(Math.max(s,1)),i.setRenderTarget(this.renderTarget),i.updateBegin();const a=new Uint8Array(4*t*s);i.readPixels(e,r,t,s,a),i.updateEnd();const h=this.mapping;for(let e=0;e<t*s;e++){const r=a[4*e+0],t=a[4*e+1],s=a[4*e+2],i=a[4*e+3]<<24|r<<16|t<<8|s;-1!==i&&p.add(h.get(i))}const o=[];return p.forEach((e=>o.push(e))),p.clear(),o}allocateRenderTarget(){const e=new h(this.device,{format:r,width:this.width,height:this.height,mipmaps:!1,minFilter:t,magFilter:t,addressU:s,addressV:s,name:"pick"});this.renderTarget=new a({colorBuffer:e,depth:!0})}releaseRenderTarget(){this.renderTarget&&(this.renderTarget.destroyTextureBuffers(),this.renderTarget.destroy(),this.renderTarget=null)}prepare(r,t,s){r instanceof o&&(r=r.node.camera),s instanceof n&&(s=[s]),this.renderTarget&&this.width===this.renderTarget.width&&this.height===this.renderTarget.height||(this.releaseRenderTarget(),this.allocateRenderTarget()),this.mapping.clear();const i=this.renderPass;i.init(this.renderTarget),i.colorOps.clearValue=e.WHITE,i.colorOps.clear=!0,i.depthStencilOps.clearDepth=!0,i.update(r,t,s,this.mapping),i.render()}resize(e,r){this.width=Math.floor(e),this.height=Math.floor(r)}}export{g as Picker};