laya-coreui-es
Version:
laya核心库、ui库es版本,剔除了媒体、TTF等功能。基于 LayaAir-release_2.12.0 修改
340 lines (339 loc) • 15 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseTexture = void 0;
const LayaGL_1 = require("../layagl/LayaGL");
const WebGLContext_1 = require("../webgl/WebGLContext");
const Bitmap_1 = require("./Bitmap");
const FilterMode_1 = require("./FilterMode");
const TextureFormat_1 = require("./TextureFormat");
const WrapMode_1 = require("./WrapMode");
class BaseTexture extends Bitmap_1.Bitmap {
get mipmap() {
return this._mipmap;
}
get format() {
return this._format;
}
get wrapModeU() {
return this._wrapModeU;
}
set wrapModeU(value) {
if (this._wrapModeU !== value) {
this._wrapModeU = value;
(this._width !== -1) && (this._setWarpMode(LayaGL_1.LayaGL.instance.TEXTURE_WRAP_S, value));
}
}
get wrapModeV() {
return this._wrapModeV;
}
set wrapModeV(value) {
if (this._wrapModeV !== value) {
this._wrapModeV = value;
(this._height !== -1) && (this._setWarpMode(LayaGL_1.LayaGL.instance.TEXTURE_WRAP_T, value));
}
}
get filterMode() {
return this._filterMode;
}
set filterMode(value) {
if (value !== this._filterMode) {
this._filterMode = value;
((this._width !== -1) && (this._height !== -1)) && (this._setFilterMode(value));
}
}
get anisoLevel() {
return this._anisoLevel;
}
set anisoLevel(value) {
if (value !== this._anisoLevel) {
this._anisoLevel = Math.max(1, Math.min(16, value));
((this._width !== -1) && (this._height !== -1)) && (this._setAnisotropy(value));
}
}
get mipmapCount() {
return this._mipmapCount;
}
get defaulteTexture() {
throw "BaseTexture:must override it.";
}
constructor(format, mipMap) {
super();
this._wrapModeU = WrapMode_1.WarpMode.Repeat;
this._wrapModeV = WrapMode_1.WarpMode.Repeat;
this._filterMode = FilterMode_1.FilterMode.Bilinear;
this._readyed = false;
this._width = -1;
this._height = -1;
this._format = format;
this._mipmap = mipMap;
this._anisoLevel = 1;
this._glTexture = LayaGL_1.LayaGL.instance.createTexture();
}
_getFormatByteCount() {
switch (this._format) {
case TextureFormat_1.TextureFormat.R8G8B8:
return 3;
case TextureFormat_1.TextureFormat.R8G8B8A8:
return 4;
case TextureFormat_1.TextureFormat.R5G6B5:
return 1;
case TextureFormat_1.TextureFormat.Alpha8:
return 1;
case TextureFormat_1.TextureFormat.R16G16B16A16:
return 2;
case TextureFormat_1.TextureFormat.R32G32B32A32:
return 4;
default:
throw "Texture2D: unknown format.";
}
}
_isPot(size) {
return (size & (size - 1)) === 0;
}
_getGLFormat() {
var glFormat;
var gl = LayaGL_1.LayaGL.instance;
var gpu = LayaGL_1.LayaGL.layaGPUInstance;
switch (this._format) {
case TextureFormat_1.TextureFormat.R8G8B8:
case TextureFormat_1.TextureFormat.R5G6B5:
glFormat = gl.RGB;
break;
case TextureFormat_1.TextureFormat.R8G8B8A8:
glFormat = gl.RGBA;
break;
case TextureFormat_1.TextureFormat.Alpha8:
glFormat = gl.ALPHA;
break;
case TextureFormat_1.TextureFormat.R32G32B32A32:
case TextureFormat_1.TextureFormat.R16G16B16A16:
glFormat = gl.RGBA;
break;
case TextureFormat_1.TextureFormat.DXT1:
if (gpu._compressedTextureS3tc)
glFormat = gpu._compressedTextureS3tc.COMPRESSED_RGB_S3TC_DXT1_EXT;
else
throw "BaseTexture: not support DXT1 format.";
break;
case TextureFormat_1.TextureFormat.DXT5:
if (gpu._compressedTextureS3tc)
glFormat = gpu._compressedTextureS3tc.COMPRESSED_RGBA_S3TC_DXT5_EXT;
else
throw "BaseTexture: not support DXT5 format.";
break;
case TextureFormat_1.TextureFormat.ETC1RGB:
if (gpu._compressedTextureEtc1)
glFormat = gpu._compressedTextureEtc1.COMPRESSED_RGB_ETC1_WEBGL;
else
throw "BaseTexture: not support ETC1RGB format.";
break;
case TextureFormat_1.TextureFormat.ETC2RGB:
if (gpu._compressedTextureETC)
glFormat = gpu._compressedTextureETC.COMPRESSED_RGB8_ETC2;
else
throw "BaseTexture: not support ETC2RGB format.";
break;
case TextureFormat_1.TextureFormat.ETC2RGBA:
if (gpu._compressedTextureETC)
glFormat = gpu._compressedTextureETC.COMPRESSED_RGBA8_ETC2_EAC;
else
throw "BaseTexture: not support ETC2RGBA format.";
break;
case TextureFormat_1.TextureFormat.ETC2RGB_Alpha8:
if (gpu._compressedTextureETC)
glFormat = gpu._compressedTextureETC.COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
else
throw "BaseTexture: not support ETC2SRGB_Alpha8 format.";
break;
case TextureFormat_1.TextureFormat.ETC2SRGB:
if (gpu._compressedTextureETC)
glFormat = gpu._compressedTextureETC.COMPRESSED_SRGB8_ETC2;
else
throw "BaseTexture: not support ETC2SRGB format.";
break;
case TextureFormat_1.TextureFormat.PVRTCRGB_2BPPV:
if (gpu._compressedTexturePvrtc)
glFormat = gpu._compressedTexturePvrtc.COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
else
throw "BaseTexture: not support PVRTCRGB_2BPPV format.";
break;
case TextureFormat_1.TextureFormat.PVRTCRGBA_2BPPV:
if (gpu._compressedTexturePvrtc)
glFormat = gpu._compressedTexturePvrtc.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
else
throw "BaseTexture: not support PVRTCRGBA_2BPPV format.";
break;
case TextureFormat_1.TextureFormat.PVRTCRGB_4BPPV:
if (gpu._compressedTexturePvrtc)
glFormat = gpu._compressedTexturePvrtc.COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
else
throw "BaseTexture: not support PVRTCRGB_4BPPV format.";
break;
case TextureFormat_1.TextureFormat.PVRTCRGBA_4BPPV:
if (gpu._compressedTexturePvrtc)
glFormat = gpu._compressedTexturePvrtc.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
else
throw "BaseTexture: not support PVRTCRGBA_4BPPV format.";
break;
case TextureFormat_1.TextureFormat.ASTC4x4:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_RGBA_ASTC_4x4_KHR;
else
throw "BaseTexture: not support ASTC4x4 format.";
break;
case TextureFormat_1.TextureFormat.ASTC4x4SRGB:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;
else
throw "BaseTexture: not support ASTC4x4_KHR format.";
break;
case TextureFormat_1.TextureFormat.ASTC6x6:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_RGBA_ASTC_6x6_KHR;
else
throw "BaseTexture: not support ASTC6x6 format.";
break;
case TextureFormat_1.TextureFormat.ASTC6x6SRGB:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR;
else
throw "BaseTexture: not support ASTC6x6_KHR format.";
break;
case TextureFormat_1.TextureFormat.ASTC8x8:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_RGBA_ASTC_8x8_KHR;
else
throw "BaseTexture: not support ASTC8x8 format.";
break;
case TextureFormat_1.TextureFormat.ASTC8x8SRGB:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR;
else
throw "BaseTexture: not support ASTC8x8 format.";
break;
case TextureFormat_1.TextureFormat.ASTC10x10:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_RGBA_ASTC_10x10_KHR;
else
throw "BaseTexture: not support ASTC10x10 format.";
break;
case TextureFormat_1.TextureFormat.ASTC10x10SRGB:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR;
else
throw "BaseTexture: not support ASTC10x10 format.";
break;
case TextureFormat_1.TextureFormat.ASTC12x12:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_RGBA_ASTC_12x12_KHR;
else
throw "BaseTexture: not support ASTC12x12 format.";
break;
case TextureFormat_1.TextureFormat.ASTC12x12SRGB:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR;
else
throw "BaseTexture: not support ASTC12x12 format.";
break;
default:
throw "BaseTexture: unknown texture format.";
}
return glFormat;
}
_setFilterMode(value) {
var gl = LayaGL_1.LayaGL.instance;
WebGLContext_1.WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture);
switch (value) {
case FilterMode_1.FilterMode.Point:
if (this._mipmap)
gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_NEAREST);
else
gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(this._glTextureType, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
break;
case FilterMode_1.FilterMode.Bilinear:
if (this._mipmap)
gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
else
gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(this._glTextureType, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
break;
case FilterMode_1.FilterMode.Trilinear:
if (this._mipmap)
gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
else
gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(this._glTextureType, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
break;
default:
throw new Error("BaseTexture:unknown filterMode value.");
}
}
_setWarpMode(orientation, mode) {
var gl = LayaGL_1.LayaGL.instance;
WebGLContext_1.WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture);
if (this._isPot(this._width) && this._isPot(this._height)) {
switch (mode) {
case WrapMode_1.WarpMode.Repeat:
gl.texParameteri(this._glTextureType, orientation, gl.REPEAT);
break;
case WrapMode_1.WarpMode.Clamp:
gl.texParameteri(this._glTextureType, orientation, gl.CLAMP_TO_EDGE);
break;
}
}
else {
gl.texParameteri(this._glTextureType, orientation, gl.CLAMP_TO_EDGE);
}
}
_setAnisotropy(value) {
var anisotropic = LayaGL_1.LayaGL.layaGPUInstance._extTextureFilterAnisotropic;
if (anisotropic) {
value = Math.max(value, 1);
var gl = LayaGL_1.LayaGL.instance;
WebGLContext_1.WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture);
value = Math.min(gl.getParameter(anisotropic.MAX_TEXTURE_MAX_ANISOTROPY_EXT), value);
gl.texParameterf(this._glTextureType, anisotropic.TEXTURE_MAX_ANISOTROPY_EXT, value);
}
}
_disposeResource() {
if (this._glTexture) {
LayaGL_1.LayaGL.instance.deleteTexture(this._glTexture);
this._glTexture = null;
this._setGPUMemory(0);
}
}
_getSource() {
if (this._readyed)
return this._glTexture;
else
return null;
}
generateMipmap() {
if (this._isPot(this.width) && this._isPot(this.height))
LayaGL_1.LayaGL.instance.generateMipmap(this._glTextureType);
}
}
exports.BaseTexture = BaseTexture;
BaseTexture._rgbmRange = 5.0;
BaseTexture.FORMAT_R8G8B8 = 0;
BaseTexture.FORMAT_R8G8B8A8 = 1;
BaseTexture.FORMAT_ALPHA8 = 2;
BaseTexture.FORMAT_DXT1 = 3;
BaseTexture.FORMAT_DXT5 = 4;
BaseTexture.FORMAT_ETC1RGB = 5;
BaseTexture.FORMAT_PVRTCRGB_2BPPV = 9;
BaseTexture.FORMAT_PVRTCRGBA_2BPPV = 10;
BaseTexture.FORMAT_PVRTCRGB_4BPPV = 11;
BaseTexture.FORMAT_PVRTCRGBA_4BPPV = 12;
BaseTexture.RENDERTEXTURE_FORMAT_RGBA_HALF_FLOAT = 14;
BaseTexture.FORMAT_R32G32B32A32 = 15;
BaseTexture.FORMAT_DEPTH_16 = 0;
BaseTexture.FORMAT_STENCIL_8 = 1;
BaseTexture.FORMAT_DEPTHSTENCIL_16_8 = 2;
BaseTexture.FORMAT_DEPTHSTENCIL_NONE = 3;
BaseTexture.FILTERMODE_POINT = 0;
BaseTexture.FILTERMODE_BILINEAR = 1;
BaseTexture.FILTERMODE_TRILINEAR = 2;
BaseTexture.WARPMODE_REPEAT = 0;
BaseTexture.WARPMODE_CLAMP = 1;