UNPKG

laya-coreui-es

Version:

laya核心库、ui库es版本,剔除了媒体、TTF等功能。基于 LayaAir-release_2.12.0 修改

205 lines (204 loc) 9.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RenderTexture2D = void 0; const Texture2D_1 = require("./Texture2D"); const LayaGL_1 = require("../layagl/LayaGL"); const BaseTexture_1 = require("./BaseTexture"); const WebGLContext_1 = require("../webgl/WebGLContext"); const BaseShader_1 = require("../webgl/shader/BaseShader"); const RenderState2D_1 = require("../webgl/utils/RenderState2D"); const ILaya_1 = require("../../ILaya"); const RenderTextureFormat_1 = require("./RenderTextureFormat"); class RenderTexture2D extends BaseTexture_1.BaseTexture { static get currentActive() { return RenderTexture2D._currentActive; } get depthStencilFormat() { return this._depthStencilFormat; } get defaulteTexture() { return Texture2D_1.Texture2D.grayTexture; } getIsReady() { return true; } get sourceWidth() { return this._width; } get sourceHeight() { return this._height; } get offsetX() { return 0; } get offsetY() { return 0; } constructor(width, height, format = RenderTextureFormat_1.RenderTextureFormat.R8G8B8, depthStencilFormat = RenderTextureFormat_1.RenderTextureDepthFormat.DEPTH_16) { super(format, false); this._mgrKey = 0; this._glTextureType = LayaGL_1.LayaGL.instance.TEXTURE_2D; this._width = width; this._height = height; this._depthStencilFormat = depthStencilFormat; this._create(width, height); this.lock = true; } _create(width, height) { var gl = LayaGL_1.LayaGL.instance; this._frameBuffer = gl.createFramebuffer(); WebGLContext_1.WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture); var glFormat = this._getGLFormat(); gl.texImage2D(this._glTextureType, 0, glFormat, width, height, 0, glFormat, gl.UNSIGNED_BYTE, null); this._setGPUMemory(width * height * 4); gl.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer); gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this._glTexture, 0); if (this._depthStencilFormat !== RenderTextureFormat_1.RenderTextureDepthFormat.DEPTHSTENCIL_NONE) { this._depthStencilBuffer = gl.createRenderbuffer(); gl.bindRenderbuffer(gl.RENDERBUFFER, this._depthStencilBuffer); switch (this._depthStencilFormat) { case RenderTextureFormat_1.RenderTextureDepthFormat.DEPTH_16: gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height); gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, this._depthStencilBuffer); break; case RenderTextureFormat_1.RenderTextureDepthFormat.STENCIL_8: gl.renderbufferStorage(gl.RENDERBUFFER, gl.STENCIL_INDEX8, width, height); gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, gl.RENDERBUFFER, this._depthStencilBuffer); break; case RenderTextureFormat_1.RenderTextureDepthFormat.DEPTHSTENCIL_24_8: gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_STENCIL, width, height); gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this._depthStencilBuffer); break; default: } } gl.bindFramebuffer(gl.FRAMEBUFFER, null); gl.bindRenderbuffer(gl.RENDERBUFFER, null); this._setWarpMode(gl.TEXTURE_WRAP_S, this._wrapModeU); this._setWarpMode(gl.TEXTURE_WRAP_T, this._wrapModeV); this._setFilterMode(this._filterMode); this._setAnisotropy(this._anisoLevel); this._readyed = true; this._activeResource(); } generateMipmap() { if (this._isPot(this.width) && this._isPot(this.height)) { this._mipmap = true; LayaGL_1.LayaGL.instance.generateMipmap(this._glTextureType); this._setFilterMode(this._filterMode); this._setGPUMemory(this.width * this.height * 4 * (1 + 1 / 3)); } else { this._mipmap = false; this._setGPUMemory(this.width * this.height * 4); } } static pushRT() { RenderTexture2D.rtStack.push({ rt: RenderTexture2D._currentActive, w: RenderState2D_1.RenderState2D.width, h: RenderState2D_1.RenderState2D.height }); } static popRT() { var gl = LayaGL_1.LayaGL.instance; var top = RenderTexture2D.rtStack.pop(); if (top) { if (RenderTexture2D._currentActive != top.rt) { LayaGL_1.LayaGL.instance.bindFramebuffer(gl.FRAMEBUFFER, top.rt ? top.rt._frameBuffer : null); RenderTexture2D._currentActive = top.rt; } gl.viewport(0, 0, top.w, top.h); RenderState2D_1.RenderState2D.width = top.w; RenderState2D_1.RenderState2D.height = top.h; } } start() { var gl = LayaGL_1.LayaGL.instance; LayaGL_1.LayaGL.instance.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer); this._lastRT = RenderTexture2D._currentActive; RenderTexture2D._currentActive = this; this._readyed = true; gl.viewport(0, 0, this._width, this._height); this._lastWidth = RenderState2D_1.RenderState2D.width; this._lastHeight = RenderState2D_1.RenderState2D.height; RenderState2D_1.RenderState2D.width = this._width; RenderState2D_1.RenderState2D.height = this._height; BaseShader_1.BaseShader.activeShader = null; } end() { var gl = LayaGL_1.LayaGL.instance; gl.bindFramebuffer(gl.FRAMEBUFFER, null); RenderTexture2D._currentActive = null; this._readyed = true; } restore() { var gl = LayaGL_1.LayaGL.instance; if (this._lastRT != RenderTexture2D._currentActive) { LayaGL_1.LayaGL.instance.bindFramebuffer(gl.FRAMEBUFFER, this._lastRT ? this._lastRT._frameBuffer : null); RenderTexture2D._currentActive = this._lastRT; } this._readyed = true; gl.viewport(0, 0, this._lastWidth, this._lastHeight); RenderState2D_1.RenderState2D.width = this._lastWidth; RenderState2D_1.RenderState2D.height = this._lastHeight; BaseShader_1.BaseShader.activeShader = null; } clear(r = 0.0, g = 0.0, b = 0.0, a = 1.0) { var gl = LayaGL_1.LayaGL.instance; gl.clearColor(r, g, b, a); var clearFlag = gl.COLOR_BUFFER_BIT; switch (this._depthStencilFormat) { case gl.DEPTH_COMPONENT16: clearFlag |= gl.DEPTH_BUFFER_BIT; break; case gl.STENCIL_INDEX8: clearFlag |= gl.STENCIL_BUFFER_BIT; break; case gl.DEPTH_STENCIL: clearFlag |= gl.DEPTH_BUFFER_BIT; clearFlag |= gl.STENCIL_BUFFER_BIT; break; } gl.clear(clearFlag); } getData(x, y, width, height) { if (ILaya_1.ILaya.Render.isConchApp && window.conchConfig.threadMode == 2) { throw "native 2 thread mode use getDataAsync"; } var gl = LayaGL_1.LayaGL.instance; gl.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer); var canRead = (gl.checkFramebufferStatus(gl.FRAMEBUFFER) === gl.FRAMEBUFFER_COMPLETE); if (!canRead) { gl.bindFramebuffer(gl.FRAMEBUFFER, null); return null; } var pixels = new Uint8Array(this._width * this._height * 4); var glFormat = this._getGLFormat(); gl.readPixels(x, y, width, height, glFormat, gl.UNSIGNED_BYTE, pixels); gl.bindFramebuffer(gl.FRAMEBUFFER, null); return pixels; } getDataAsync(x, y, width, height, callBack) { var gl = LayaGL_1.LayaGL.instance; gl.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer); gl.readPixelsAsync(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, function (data) { callBack(new Uint8Array(data)); }); gl.bindFramebuffer(gl.FRAMEBUFFER, null); } recycle() { } _disposeResource() { if (this._frameBuffer) { var gl = LayaGL_1.LayaGL.instance; gl.deleteTexture(this._glTexture); gl.deleteFramebuffer(this._frameBuffer); gl.deleteRenderbuffer(this._depthStencilBuffer); this._glTexture = null; this._frameBuffer = null; this._depthStencilBuffer = null; this._setGPUMemory(0); } } } exports.RenderTexture2D = RenderTexture2D; RenderTexture2D.rtStack = []; RenderTexture2D.defuv = [0, 0, 1, 0, 1, 1, 0, 1]; RenderTexture2D.flipyuv = [0, 1, 1, 1, 1, 0, 0, 0];