mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-04 18:42:40 +08:00
support compress texture (#3039)
* support compress texture * add cc.macro.SupportTextureFormats * fixed texture test * update api comment * reverse texture size * refine code * refine code * refine data
This commit is contained in:
@@ -116,7 +116,36 @@ const PixelFormat = cc.Enum({
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
AI8: gfx.TEXTURE_FMT_L8_A8
|
||||
AI8: gfx.TEXTURE_FMT_L8_A8,
|
||||
|
||||
/**
|
||||
* rgb 2 bpp pvrtc
|
||||
* @property RGB_PVRTC_2BPPV1
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
RGB_PVRTC_2BPPV1: gfx.TEXTURE_FMT_RGB_PVRTC_2BPPV1,
|
||||
/**
|
||||
* rgba 2 bpp pvrtc
|
||||
* @property RGBA_PVRTC_2BPPV1
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
RGBA_PVRTC_2BPPV1: gfx.TEXTURE_FMT_RGBA_PVRTC_2BPPV1,
|
||||
/**
|
||||
* rgb 4 bpp pvrtc
|
||||
* @property RGB_PVRTC_4BPPV1
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
RGB_PVRTC_4BPPV1: gfx.TEXTURE_FMT_RGB_PVRTC_4BPPV1,
|
||||
/**
|
||||
* rgba 4 bpp pvrtc
|
||||
* @property RGBA_PVRTC_4BPPV1
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
RGBA_PVRTC_4BPPV1: gfx.TEXTURE_FMT_RGBA_PVRTC_4BPPV1,
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -217,8 +246,13 @@ var Texture2D = cc.Class({
|
||||
// maybe returned to pool in webgl
|
||||
return this._image;
|
||||
},
|
||||
set (image) {
|
||||
this.initWithElement(image);
|
||||
set (data) {
|
||||
if (data._compressed && data._data) {
|
||||
this.initWithData(data._data, this._format, data.width, data.height);
|
||||
}
|
||||
else {
|
||||
this.initWithElement(data);
|
||||
}
|
||||
},
|
||||
override: true
|
||||
},
|
||||
@@ -237,7 +271,11 @@ var Texture2D = cc.Class({
|
||||
WrapMode: WrapMode,
|
||||
Filter: Filter,
|
||||
// predefined most common extnames
|
||||
extnames: ['.png', '.jpg', '.jpeg', '.bmp', '.webp']
|
||||
extnames: ['.png', '.jpg', '.jpeg', '.bmp', '.webp', '.pvr', '.etc'],
|
||||
|
||||
_isCompressed (texture) {
|
||||
return texture._format >= PixelFormat.RGB_PVRTC_2BPPV1 && texture._format <= PixelFormat.RGBA_PVRTC_4BPPV1;
|
||||
}
|
||||
},
|
||||
|
||||
ctor () {
|
||||
@@ -285,6 +323,10 @@ var Texture2D = cc.Class({
|
||||
this.height = 0;
|
||||
|
||||
this._texture = null;
|
||||
|
||||
if (CC_EDITOR) {
|
||||
this._exportedExts = null;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -423,10 +465,24 @@ var Texture2D = cc.Class({
|
||||
initWithData (data, pixelFormat, pixelsWidth, pixelsHeight) {
|
||||
var opts = _getSharedOptions();
|
||||
opts.image = data;
|
||||
// webgl texture 2d uses images
|
||||
opts.images = [opts.image];
|
||||
opts.hasMipmap = this._hasMipmap;
|
||||
opts.premultiplyAlpha = this._premultiplyAlpha;
|
||||
opts.flipY = this._flipY;
|
||||
opts.minFilter = FilterIndex[this._minFilter];
|
||||
opts.magFilter = FilterIndex[this._magFilter];
|
||||
opts.wrapS = this._wrapS;
|
||||
opts.wrapT = this._wrapT;
|
||||
opts.format = pixelFormat;
|
||||
opts.width = pixelsWidth;
|
||||
opts.height = pixelsHeight;
|
||||
this.update(opts);
|
||||
if (!this._texture) {
|
||||
this._texture = new renderer.Texture2D(renderer.device, opts);
|
||||
}
|
||||
else {
|
||||
this._texture.update(opts);
|
||||
}
|
||||
this.width = pixelsWidth;
|
||||
this.height = pixelsHeight;
|
||||
this.loaded = true;
|
||||
@@ -652,15 +708,29 @@ var Texture2D = cc.Class({
|
||||
|
||||
_serialize: (CC_EDITOR || CC_TEST) && function () {
|
||||
let extId = "";
|
||||
if (this._native) {
|
||||
// encode extname
|
||||
let ext = cc.path.extname(this._native);
|
||||
if (ext) {
|
||||
extId = Texture2D.extnames.indexOf(ext);
|
||||
if (extId < 0) {
|
||||
extId = ext;
|
||||
let exportedExts = this._exportedExts;
|
||||
if (!exportedExts && this._native) {
|
||||
exportedExts = [this._native];
|
||||
}
|
||||
if (exportedExts) {
|
||||
let exts = [];
|
||||
for (let i = 0; i < exportedExts.length; i++) {
|
||||
let extId = "";
|
||||
let ext = exportedExts[i]
|
||||
if (ext) {
|
||||
// ext@format
|
||||
let extFormat = ext.split('@');
|
||||
extId = Texture2D.extnames.indexOf(extFormat[0]);
|
||||
if (extId < 0) {
|
||||
extId = ext;
|
||||
}
|
||||
if (extFormat[1]) {
|
||||
extId += '@' + extFormat[1];
|
||||
}
|
||||
}
|
||||
exts.push(extId);
|
||||
}
|
||||
extId = exts.join('_');
|
||||
}
|
||||
let asset = "" + extId + "," +
|
||||
this._minFilter + "," + this._magFilter + "," +
|
||||
@@ -674,9 +744,30 @@ var Texture2D = cc.Class({
|
||||
// decode extname
|
||||
var extIdStr = fields[0];
|
||||
if (extIdStr) {
|
||||
let extId = extIdStr.charCodeAt(0) - CHAR_CODE_0;
|
||||
let ext = Texture2D.extnames[extId];
|
||||
this._setRawAsset(ext || extIdStr);
|
||||
let extIds = extIdStr.split('_');
|
||||
|
||||
let extId = 999;
|
||||
let ext = '';
|
||||
let format = this._format;
|
||||
let SupportTextureFormats = cc.macro.SUPPORT_TEXTURE_FORMATS;
|
||||
for (let i = 0; i < extIds.length; i++) {
|
||||
let extFormat = extIds[i].split('@');
|
||||
let tmpExt = extFormat[0];
|
||||
tmpExt = tmpExt.charCodeAt(0) - CHAR_CODE_0;
|
||||
tmpExt = Texture2D.extnames[tmpExt] || extFormat;
|
||||
|
||||
let index = SupportTextureFormats.indexOf(tmpExt);
|
||||
if (index !== -1 && index < extId) {
|
||||
extId = index;
|
||||
ext = tmpExt;
|
||||
format = extFormat[1] ? parseInt(extFormat[1]) : this._format;
|
||||
}
|
||||
}
|
||||
|
||||
if (ext) {
|
||||
this._setRawAsset(ext);
|
||||
this._format = format;
|
||||
}
|
||||
|
||||
// preset uuid to get correct nativeUrl
|
||||
let loadingItem = handle.customEnv;
|
||||
|
||||
@@ -216,6 +216,8 @@ var defaultMap = {
|
||||
'tiff' : downloadImage,
|
||||
'webp' : downloadWebp,
|
||||
'image' : downloadImage,
|
||||
'pvr': downloadBinary,
|
||||
'etc': downloadBinary,
|
||||
|
||||
// Audio
|
||||
'mp3' : downloadAudio,
|
||||
|
||||
@@ -108,6 +108,48 @@ function loadBinary (item) {
|
||||
}
|
||||
}
|
||||
|
||||
//===============//
|
||||
// PVR constants //
|
||||
//===============//
|
||||
// https://github.com/toji/texture-tester/blob/master/js/webgl-texture-util.js#L424
|
||||
const PVR_HEADER_LENGTH = 13; // The header length in 32 bit ints.
|
||||
const PVR_MAGIC = 0x03525650; //0x50565203;
|
||||
|
||||
// Offsets into the header array.
|
||||
const PVR_HEADER_MAGIC = 0;
|
||||
const PVR_HEADER_FORMAT = 2;
|
||||
const PVR_HEADER_HEIGHT = 6;
|
||||
const PVR_HEADER_WIDTH = 7;
|
||||
const PVR_HEADER_MIPMAPCOUNT = 11;
|
||||
const PVR_HEADER_METADATA = 12;
|
||||
|
||||
function loadCompressedTex (item) {
|
||||
// Get a view of the arrayBuffer that represents the DDS header.
|
||||
let header = new Int32Array(item.content.buffer, 0, PVR_HEADER_LENGTH);
|
||||
|
||||
// Do some sanity checks to make sure this is a valid DDS file.
|
||||
if(header[PVR_HEADER_MAGIC] != PVR_MAGIC) {
|
||||
return new Error("Invalid magic number in PVR header");
|
||||
}
|
||||
|
||||
// Gather other basic metrics and a view of the raw the DXT data.
|
||||
let width = header[PVR_HEADER_WIDTH];
|
||||
let height = header[PVR_HEADER_HEIGHT];
|
||||
let levels = header[PVR_HEADER_MIPMAPCOUNT];
|
||||
let dataOffset = header[PVR_HEADER_METADATA] + 52;
|
||||
let pvrtcData = new Uint8Array(item.content.buffer, dataOffset);
|
||||
|
||||
let pvrAsset = {
|
||||
_data: pvrtcData,
|
||||
_compressed: true,
|
||||
|
||||
width: width,
|
||||
height: height,
|
||||
};
|
||||
|
||||
return pvrAsset;
|
||||
}
|
||||
|
||||
var defaultMap = {
|
||||
// Images
|
||||
'png' : loadImage,
|
||||
@@ -119,6 +161,8 @@ var defaultMap = {
|
||||
'tiff' : loadImage,
|
||||
'webp' : loadImage,
|
||||
'image' : loadImage,
|
||||
'pvr' : loadCompressedTex,
|
||||
'etc' : loadCompressedTex,
|
||||
|
||||
// Audio
|
||||
'mp3' : loadAudioAsAsset,
|
||||
|
||||
@@ -345,6 +345,28 @@ cc.macro = {
|
||||
CLEANUP_IMAGE_CACHE: false,
|
||||
};
|
||||
|
||||
|
||||
let SUPPORT_TEXTURE_FORMATS = ['.webp', '.jpg', '.jpeg', '.bmp', '.png'];
|
||||
if (cc.sys.isMobile) {
|
||||
if (cc.sys.os === cc.sys.OS_IOS) {
|
||||
SUPPORT_TEXTURE_FORMATS = ['.pvr'].concat(SUPPORT_TEXTURE_FORMATS);
|
||||
}
|
||||
// else if (cc.sys.os === cc.sys.OS_ANDROID) {
|
||||
// SUPPORT_TEXTURE_FORMATS = ['.etc'].join(SUPPORT_TEXTURE_FORMATS);
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
* !en
|
||||
* The image format supported by the engine defaults, and the supported formats may differ in different build platforms and device types.
|
||||
* Currently all platform and device support ['.webp', '.jpg', '.jpeg', '.bmp', '.png'], ios mobile platform
|
||||
* !zh
|
||||
* 引擎默认支持的图片格式,支持的格式可能在不同的构建平台和设备类型上有所差别。
|
||||
* 目前所有平台和设备支持的格式有 ['.webp', '.jpg', '.jpeg', '.bmp', '.png']. The iOS mobile platform also supports the PVR format。
|
||||
* @property {[String]} SUPPORT_TEXTURE_FORMATS
|
||||
*/
|
||||
cc.macro.SUPPORT_TEXTURE_FORMATS = SUPPORT_TEXTURE_FORMATS;
|
||||
|
||||
js.getset(cc.macro, 'ENABLE_3D', function () {
|
||||
return cc._polyfill3D.enabled;
|
||||
}, function (enabled) {
|
||||
|
||||
@@ -9558,13 +9558,17 @@ Device.prototype._initExtensions = function _initExtensions (extensions) {
|
||||
for (var i = 0; i < extensions.length; ++i) {
|
||||
var name = extensions[i];
|
||||
|
||||
try {
|
||||
var ext = gl.getExtension(name);
|
||||
if (ext) {
|
||||
this$1._extensions[name] = ext;
|
||||
var vendorPrefixes = ["", "WEBKIT_", "MOZ_"];
|
||||
for (var j = 0; j < vendorPrefixes.length; j++) {
|
||||
try {
|
||||
var ext = gl.getExtension(vendorPrefixes[j] + name);
|
||||
if (ext) {
|
||||
this$1._extensions[name] = ext;
|
||||
break;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -108,7 +108,7 @@ let dynamicAtlasManager = {
|
||||
!spriteFrame || spriteFrame._original) return;
|
||||
|
||||
let texture = spriteFrame._texture;
|
||||
if (texture instanceof cc.RenderTexture) return;
|
||||
if (texture instanceof cc.RenderTexture || cc.Texture2D._isCompressed(texture)) return;
|
||||
|
||||
let w = texture.width, h = texture.height;
|
||||
let min = texture._minFilter, mag = texture._magFilter;
|
||||
|
||||
Reference in New Issue
Block a user