import { Camera , CameraClearFlags } from "laya/d3/core/Camera" ; import { RenderContext3D } from "laya/d3/core/render/RenderContext3D" ; import { RenderableSprite3D } from "laya/d3/core/RenderableSprite3D" ; import { Scene3D } from "laya/d3/core/scene/Scene3D" ; import { Sprite3D } from "laya/d3/core/Sprite3D" ; import { Vector4 } from "laya/d3/math/Vector4" ; import { Viewport } from "laya/d3/math/Viewport" ; import { RenderTexture } from "laya/d3/resource/RenderTexture" ; import { RenderTextureDepthFormat , RenderTextureFormat } from "laya/resource/RenderTextureFormat" ; author miner
  export class EditPickUtil { public static selectSprite : Sprite3D ; public static editorCamera : Camera ; public static pickCamera : Camera ; public static editorScene : Scene3D ; public static pickPixel : Uint8Array = new Uint8Array ( 4 ) ; public static pickVector4 : Vector4 = new Vector4 ( ) ; public static oriClearFlag : CameraClearFlags = CameraClearFlags . Nothing ; public static oriClearColor : Vector4 = new Vector4 ( ) ; public static cameraClearColor : Vector4 = new Vector4 ( ) ; public static oriPipeLineMode : string ; param scene param camera public static setPickTarget ( scene , camera ) { EditPickUtil . editorCamera = camera ; EditPickUtil . editorScene = scene ; if ( ! EditPickUtil . pickCamera ) { EditPickUtil . pickCamera = new Camera ( ) ; EditPickUtil . pickCamera . name = "pick Camera" ; } EditPickUtil . oriPipeLineMode = RenderContext3D . _instance . configPipeLineMode ; } param posX param posY param texture private static pickRenderableSprite3D ( posX , posY , texture ) { var pixelArray = EditPickUtil . pickPixel ; var pickVec = EditPickUtil . pickVector4 ; var scene = EditPickUtil . editorScene ; texture . getData ( posX , posY , 1 , 1 , EditPickUtil . pickPixel ) ; console . log ( this . pickPixel ) ; pickVec . setValue ( pixelArray [ 0 ] , pixelArray [ 1 ] , pixelArray [ 2 ] , pixelArray [ 3 ] ) ; var id = scene . _searchIDByPickColor ( pickVec ) ; return scene . _pickIdToSprite [ id ] ; } static pickRenderTextureColor ( posX , posY , texture , outPixelArray , reck ) { texture . getData ( posX - reck , posY - reck , reck * 2 + 1 , reck * 2 + 1 , outPixelArray ) ; } param posX param posY param offsetX param offsetY param n public static pickSelectSprite3D ( posX , posY , offsetX = 0 , offsetY = 0 , n = 1 ) { var camera = EditPickUtil . editorCamera ; if ( posX < 0 || posY < 0 || ! camera ) return ; var context = RenderContext3D . _instance ; context . configPipeLineMode = "PickSprite" ; EditPickUtil . recordCameraData ( camera ) ; ( ! EditPickUtil . pickCamera ) && ( EditPickUtil . pickCamera . renderTarget = RenderTexture . createFromPool ( camera . renderTarget . width , camera . renderTarget . height , RenderTextureFormat . R8G8B8A8 , RenderTextureDepthFormat . DEPTH_16 ) ) ; var RT = Camera . drawRenderTextureByScene ( EditPickUtil . pickCamera , EditPickUtil . editorScene , EditPickUtil . pickCamera . renderTarget ) ; var renderableSprite = EditPickUtil . pickRenderableSprite3D ( posX - offsetX , posY - offsetY , RT ) ; if ( renderableSprite ) EditPickUtil . selectSprite = renderableSprite ; context . configPipeLineMode = EditPickUtil . oriPipeLineMode ; return renderableSprite ; } param camera public static recordCameraData ( camera ) { var pickCamera = EditPickUtil . pickCamera ; pickCamera . farPlane = camera . farPlane ; pickCamera . nearPlane = camera . nearPlane ; pickCamera . transform . worldMatrix = camera . transform . worldMatrix ; pickCamera . aspectRatio = camera . aspectRatio ; pickCamera . orthographic = camera . orthographic ; if ( pickCamera . orthographic ) { pickCamera . orthographicVerticalSize = camera . orthographicVerticalSize ; } pickCamera . clearFlag = CameraClearFlags . SolidColor ; pickCamera . clearColor = EditPickUtil . cameraClearColor ; } /**
     * 框选精灵队列
     */ public static pickSprite3DsByRect ( ) { return null ; } param camera param width param height public static EditorCameraSet ( camera , width , height , isOriCamraRenderTarget = true ) { var oriTargetTexture = camera . renderTarget ; var pickTargetTexture = EditPickUtil . pickCamera . renderTarget ; if ( ! oriTargetTexture ) { isOriCamraRenderTarget && ( oriTargetTexture = camera . renderTarget = new RenderTexture ( width , height , RenderTextureFormat . R8G8B8A8 , RenderTextureDepthFormat . DEPTH_16 ) ) ; if ( ! pickTargetTexture ) pickTargetTexture = EditPickUtil . pickCamera . renderTarget = new RenderTexture ( width , height , RenderTextureFormat . R8G8B8A8 , RenderTextureDepthFormat . DEPTH_16 ) ; } if ( oriTargetTexture && oriTargetTexture . width == width && oriTargetTexture . height == height ) { return ; } else { oriTargetTexture && oriTargetTexture . destroy ( ) ; pickTargetTexture && pickTargetTexture . destroy ( ) ; oriTargetTexture && ( camera . renderTarget = new RenderTexture ( width , height , RenderTextureFormat . R8G8B8A8 , RenderTextureDepthFormat . DEPTH_16 ) ) ; EditPickUtil . pickCamera . renderTarget = new RenderTexture ( width , height , RenderTextureFormat . R8G8B8A8 , RenderTextureDepthFormat . DEPTH_16 ) ; camera . render ( ) ; camera . viewport = new Viewport ( 0 , 0 , width , height ) ; } } }