laya-coreui-es
Version:
laya核心库、ui库es版本,剔除了媒体、TTF等功能。基于 LayaAir-release_2.12.0 修改
336 lines (335 loc) • 14.1 kB
JavaScript
import { LayaGL } from "../layagl/LayaGL";
import { WebGLContext } from "../webgl/WebGLContext";
import { Bitmap } from "./Bitmap";
import { FilterMode } from "./FilterMode";
import { TextureFormat } from "./TextureFormat";
import { WarpMode } from "./WrapMode";
export class BaseTexture extends 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.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.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 = WarpMode.Repeat;
this._wrapModeV = WarpMode.Repeat;
this._filterMode = FilterMode.Bilinear;
this._readyed = false;
this._width = -1;
this._height = -1;
this._format = format;
this._mipmap = mipMap;
this._anisoLevel = 1;
this._glTexture = LayaGL.instance.createTexture();
}
_getFormatByteCount() {
switch (this._format) {
case TextureFormat.R8G8B8:
return 3;
case TextureFormat.R8G8B8A8:
return 4;
case TextureFormat.R5G6B5:
return 1;
case TextureFormat.Alpha8:
return 1;
case TextureFormat.R16G16B16A16:
return 2;
case TextureFormat.R32G32B32A32:
return 4;
default:
throw "Texture2D: unknown format.";
}
}
_isPot(size) {
return (size & (size - 1)) === 0;
}
_getGLFormat() {
var glFormat;
var gl = LayaGL.instance;
var gpu = LayaGL.layaGPUInstance;
switch (this._format) {
case TextureFormat.R8G8B8:
case TextureFormat.R5G6B5:
glFormat = gl.RGB;
break;
case TextureFormat.R8G8B8A8:
glFormat = gl.RGBA;
break;
case TextureFormat.Alpha8:
glFormat = gl.ALPHA;
break;
case TextureFormat.R32G32B32A32:
case TextureFormat.R16G16B16A16:
glFormat = gl.RGBA;
break;
case TextureFormat.DXT1:
if (gpu._compressedTextureS3tc)
glFormat = gpu._compressedTextureS3tc.COMPRESSED_RGB_S3TC_DXT1_EXT;
else
throw "BaseTexture: not support DXT1 format.";
break;
case TextureFormat.DXT5:
if (gpu._compressedTextureS3tc)
glFormat = gpu._compressedTextureS3tc.COMPRESSED_RGBA_S3TC_DXT5_EXT;
else
throw "BaseTexture: not support DXT5 format.";
break;
case TextureFormat.ETC1RGB:
if (gpu._compressedTextureEtc1)
glFormat = gpu._compressedTextureEtc1.COMPRESSED_RGB_ETC1_WEBGL;
else
throw "BaseTexture: not support ETC1RGB format.";
break;
case TextureFormat.ETC2RGB:
if (gpu._compressedTextureETC)
glFormat = gpu._compressedTextureETC.COMPRESSED_RGB8_ETC2;
else
throw "BaseTexture: not support ETC2RGB format.";
break;
case TextureFormat.ETC2RGBA:
if (gpu._compressedTextureETC)
glFormat = gpu._compressedTextureETC.COMPRESSED_RGBA8_ETC2_EAC;
else
throw "BaseTexture: not support ETC2RGBA format.";
break;
case 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.ETC2SRGB:
if (gpu._compressedTextureETC)
glFormat = gpu._compressedTextureETC.COMPRESSED_SRGB8_ETC2;
else
throw "BaseTexture: not support ETC2SRGB format.";
break;
case 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.PVRTCRGBA_2BPPV:
if (gpu._compressedTexturePvrtc)
glFormat = gpu._compressedTexturePvrtc.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
else
throw "BaseTexture: not support PVRTCRGBA_2BPPV format.";
break;
case 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.PVRTCRGBA_4BPPV:
if (gpu._compressedTexturePvrtc)
glFormat = gpu._compressedTexturePvrtc.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
else
throw "BaseTexture: not support PVRTCRGBA_4BPPV format.";
break;
case TextureFormat.ASTC4x4:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_RGBA_ASTC_4x4_KHR;
else
throw "BaseTexture: not support ASTC4x4 format.";
break;
case 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.ASTC6x6:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_RGBA_ASTC_6x6_KHR;
else
throw "BaseTexture: not support ASTC6x6 format.";
break;
case 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.ASTC8x8:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_RGBA_ASTC_8x8_KHR;
else
throw "BaseTexture: not support ASTC8x8 format.";
break;
case TextureFormat.ASTC8x8SRGB:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR;
else
throw "BaseTexture: not support ASTC8x8 format.";
break;
case TextureFormat.ASTC10x10:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_RGBA_ASTC_10x10_KHR;
else
throw "BaseTexture: not support ASTC10x10 format.";
break;
case TextureFormat.ASTC10x10SRGB:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR;
else
throw "BaseTexture: not support ASTC10x10 format.";
break;
case TextureFormat.ASTC12x12:
if (gpu._compressedTextureASTC)
glFormat = gpu._compressedTextureASTC.COMPRESSED_RGBA_ASTC_12x12_KHR;
else
throw "BaseTexture: not support ASTC12x12 format.";
break;
case 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.instance;
WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture);
switch (value) {
case 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.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.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.instance;
WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture);
if (this._isPot(this._width) && this._isPot(this._height)) {
switch (mode) {
case WarpMode.Repeat:
gl.texParameteri(this._glTextureType, orientation, gl.REPEAT);
break;
case 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.layaGPUInstance._extTextureFilterAnisotropic;
if (anisotropic) {
value = Math.max(value, 1);
var gl = LayaGL.instance;
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.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.instance.generateMipmap(this._glTextureType);
}
}
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;