import AbstractTexture from './AbstractTexture' ; import { TextureParameter , TextureParameterEnum , } from '../definitions/TextureParameter' ; import { PixelFormat , PixelFormatEnum } from '../definitions/PixelFormat' ; import { ComponentTypeEnum , ComponentType } from '../definitions/ComponentType' ; import IRenderable from './IRenderable' ; import CGAPIResourceRepository from '../renderer/CGAPIResourceRepository' ; import { Size , Index } from '../../types/CommonTypes' ; import FrameBuffer from '../renderer/FrameBuffer' ; import Vector4 from '../math/Vector4' ; export default class RenderTargetTexture extends AbstractTexture implements IRenderable { private __fbo ? : FrameBuffer ; constructor ( ) { super ( ) ; } create ( { width , height , level = 0 , internalFormat = PixelFormat . RGBA , format = PixelFormat . RGBA , type = ComponentType . UnsignedByte , magFilter = TextureParameter . Linear , minFilter = TextureParameter . Linear , wrapS = TextureParameter . ClampToEdge , wrapT = TextureParameter . ClampToEdge , } ) { this . __width = width ; this . __height = height ; this . __level = level ; this . __internalFormat = internalFormat ; this . __format = format ; this . __type = type ; this . __magFilter = magFilter ; this . __minFilter = minFilter ; this . __wrapS = wrapS ; this . __wrapT = wrapT ; this . __createRenderTargetTexture ( ) ; } set _fbo ( fbo ) { this . __fbo = fbo ; } get fbo ( ) { return this . __fbo ; } private __createRenderTargetTexture ( ) { const webGLResourceRepository = CGAPIResourceRepository . getWebGLResourceRepository ( ) ; const texture = webGLResourceRepository . createRenderTargetTexture ( { width : this . __width , height : this . __height , level : this . __level , internalFormat : this . __internalFormat , format : this . __format , type : this . __type , magFilter : this . __magFilter , minFilter : this . __minFilter , wrapS : this . __wrapS , wrapT : this . __wrapT , } ) ; this . cgApiResourceUid = texture ; AbstractTexture . __textureMap . set ( texture , this ) ; } resize ( width , height ) { this . destroy3DAPIResources ( ) ; this . __width = width ; this . __height = height ; this . __createRenderTargetTexture ( ) ; } destroy3DAPIResources ( ) { AbstractTexture . __textureMap . delete ( this . cgApiResourceUid ) ; const webGLResourceRepository = CGAPIResourceRepository . getWebGLResourceRepository ( ) ; webGLResourceRepository . deleteTexture ( this . cgApiResourceUid ) ; this . cgApiResourceUid = CGAPIResourceRepository . InvalidCGAPIResourceUid ; return true ; } getTexturePixelData ( ) { const webGLResourceRepository = CGAPIResourceRepository . getWebGLResourceRepository ( ) ; const glw = webGLResourceRepository . currentWebGLContextWrapper ; const gl = glw ! . getRawContext ( ) as WebGLRenderingContext ; const fbo = webGLResourceRepository . getWebGLResource ( this . __fbo ! . framebufferUID ) as WebGLFramebuffer ; gl . bindFramebuffer ( gl . FRAMEBUFFER , fbo ) ; const data = new Uint8Array ( this . width * this . height * 4 ) ; if ( ( gl as WebGL2RenderingContext ) . readBuffer != null ) { ( gl as WebGL2RenderingContext ) . readBuffer ( 36064 + this . __fbo ! . whichColorAttachment ( this ) ) ; } gl . readPixels ( 0 , 0 , this . width , this . height , gl . RGBA , gl . UNSIGNED_BYTE , data ) ; gl . bindFramebuffer ( gl . FRAMEBUFFER , null ) ; return data ; } param x param y param argByteArray returns getPixelValueAt ( x , y , argByteArray ? ) { let byteArray = argByteArray ; if ( ! byteArray ) { byteArray = this . getTexturePixelData ( ) ; } const color = Vector4 . fromCopyArray ( [ byteArray [ ( y * this . width + x ) * 4 + 0 ] , byteArray [ ( y * this . width + x ) * 4 + 1 ] , byteArray [ ( y * this . width + x ) * 4 + 2 ] , byteArray [ ( y * this . width + x ) * 4 + 3 ] ] ) ; return color ; } }