diff --git a/cocos/3d/assets/texture-cube.js b/cocos/3d/assets/texture-cube.ts
similarity index 87%
rename from cocos/3d/assets/texture-cube.js
rename to cocos/3d/assets/texture-cube.ts
index 8b22687e16..9854377f82 100644
--- a/cocos/3d/assets/texture-cube.js
+++ b/cocos/3d/assets/texture-cube.ts
@@ -23,10 +23,10 @@
THE SOFTWARE.
****************************************************************************/
// @ts-check
-import GFXTextureCube from '../../renderer/gfx/texture-cube';
+import ImageAsset from '../../assets/image-asset';
import TextureBase from '../../assets/texture-base';
import { ccclass, property } from '../../core/data/class-decorator';
-import ImageAsset from '../../assets/image-asset';
+import GFXTextureCube from '../../renderer/gfx/texture-cube';
/**
* @typedef {import("../../assets/texture-2d")} Texture2D
@@ -46,32 +46,18 @@ const FaceIndex = cc.Enum({
top: 2,
bottom: 3,
front: 4,
- back: 5
+ back: 5,
});
@ccclass('cc.TextureCube')
export default class TextureCube extends TextureBase {
- /**
- * @type {Mipmap[]}
- */
- @property
- _mipmaps = [];
-
- constructor() {
- super();
-
- /**
- * @type {GFXTextureCube}
- */
- this._texture = null;
- }
/**
* Gets the mipmap images.
* Note that the result do not contains the auto generated mipmaps.
* @type {Mipmap[]}
*/
- get mipmaps() {
+ get mipmaps () {
return this._mipmaps;
}
@@ -79,7 +65,7 @@ export default class TextureCube extends TextureBase {
* Sets the mipmaps images.
* @type {Mipmap[]}
*/
- set mipmaps(value) {
+ set mipmaps (value) {
this._setMipmaps(value);
}
@@ -87,7 +73,7 @@ export default class TextureCube extends TextureBase {
* Gets the mipmap image at level 0.
* @type {Mipmap}
*/
- get image() {
+ get image () {
return this._mipmaps.length === 0 ? null : this._mipmaps[0];
}
@@ -95,121 +81,15 @@ export default class TextureCube extends TextureBase {
* Sets the mipmap images as a single mipmap image.
* @type {Mipmap}
*/
- set image(value) {
+ set image (value) {
this.mipmaps = [value];
}
- /**
- * Gets the underlying texture object.
- */
- getImpl () {
- return this._texture;
- }
-
- /**
- * !#en
- * Destory this texture and immediately release its video memory. (Inherit from cc.Object.destroy)
- * After destroy, this object is not usable any more.
- * You can use cc.isValid(obj) to check whether the object is destroyed before accessing it.
- * !#zh
- * 销毁该贴图,并立即释放它对应的显存。(继承自 cc.Object.destroy)
- * 销毁后,该对象不再可用。您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。
- * @method destroy
- * @return {Boolean} inherit from the CCObject
- */
- destroy () {
- this.releaseTexture();
- // TODO cc.textureUtil ?
- // cc.textureCache.removeTextureForKey(this.url); // item.rawUrl || item.url
- return super.destroy();
- }
-
- /**
- * !#en
- * Release texture, please use destroy instead.
- * !#zh 释放纹理,请使用 destroy 替代。
- * @method releaseTexture
- * @deprecated since v2.0
- */
- releaseTexture () {
- this._setMipmaps([]);
- this._texture && this._texture.destroy();
- }
-
- /**
- * Sets the _mipmaps to specified value, and updates the width and height accordingly.
- * If available, synchronize the mipmap data to underlying texture.
- * @param {Mipmap[]} mipmaps
- */
- _setMipmaps(mipmaps) {
- this._mipmaps = mipmaps;
- if (mipmaps.length === 0) {
- super.update({
- width: 0,
- height: 0
- });
- } else {
- super.update({
- width: this._mipmaps[0].front.width,
- height: this._mipmaps[0].front.height,
- format: this._mipmaps[0].front._format
- });
- }
-
- let counter = 0;
- const inc = () => {
- ++counter;
- if (counter === this._mipmaps.length * 6) {
- this._updateMipmaps(this._mipmaps.map(mipmap => TextureCube._toMipmapArray(mipmap)));
- }
- };
- mipmaps.forEach(mipmap => {
- TextureCube._forEachFace(mipmap, face => {
- if (face.loaded) {
- inc();
- } else {
- face.addEventListener("load", () => inc());
- }
- });
- });
- }
-
- /**
- * Updates mipmaps of the underlying texture.
- * @param {ImageSource[][]} mipmapSources
- */
- _updateMipmaps(mipmapSources) {
- const opts = this._getOpts();
- opts.images = mipmapSources;
- if (!this._texture) {
- this._texture = new GFXTextureCube(cc.game._renderContext, opts);
- } else {
- this._texture.update(opts);
- }
- }
-
- /**
- * Update texture options, not available in Canvas render mode.
- * image, format, premultiplyAlpha can not be updated in native.
- * @method update
- */
- update (options) {
- if (!options) return;
-
- super.update(options);
-
- // TODO: options.image
-
- if (this._texture) {
- this._texture.update(options);
- }
- }
-
/**
* @param {Mipmap} mipmap
* @return {ImageSource[]}
*/
- static _toMipmapArray(mipmap) {
+ public static _toMipmapArray (mipmap) {
/** @type {ImageSource[]} */
const result = new Array();
result[FaceIndex.front] = mipmap.front.data;
@@ -225,8 +105,8 @@ export default class TextureCube extends TextureBase {
* @param {Mipmap} mipmap
* @param {(face: ImageAsset) => void} callback
*/
- static _forEachFace(mipmap, callback) {
- for (let face in mipmap) {
+ public static _forEachFace (mipmap, callback) {
+ for (const face in mipmap) {
callback(mipmap[face]);
}
}
@@ -234,34 +114,154 @@ export default class TextureCube extends TextureBase {
/**
* @param {Texture2D[]} textures
*/
- static fromTexture2DArray(textures, out) {
- let res = [], mips = textures.length / 6;
+ public static fromTexture2DArray (textures, out) {
+ const res = [], mips = textures.length / 6;
for (let i = 0; i < mips; i++) {
res.push(textures.splice(0, 6).reduce((acc, img, i) => {
acc[FaceIndex[i]] = img.image;
return acc;
}, {}));
}
- if (!out) out = new TextureCube();
+ if (!out) { out = new TextureCube(); }
out.mipmaps = res;
return out;
}
+ /**
+ * @type {Mipmap[]}
+ */
+ @property
+ public _mipmaps = [];
- _serialize() {
+ constructor () {
+ super();
+
+ /**
+ * @type {GFXTextureCube}
+ */
+ this._texture = null;
+ }
+
+ /**
+ * Gets the underlying texture object.
+ */
+ public getImpl () {
+ return this._texture;
+ }
+
+ /**
+ * !#en
+ * Destory this texture and immediately release its video memory. (Inherit from cc.Object.destroy)
+ * After destroy, this object is not usable any more.
+ * You can use cc.isValid(obj) to check whether the object is destroyed before accessing it.
+ * !#zh
+ * 销毁该贴图,并立即释放它对应的显存。(继承自 cc.Object.destroy)
+ * 销毁后,该对象不再可用。您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。
+ * @method destroy
+ * @return {Boolean} inherit from the CCObject
+ */
+ public destroy () {
+ this.releaseTexture();
+ // TODO cc.textureUtil ?
+ // cc.textureCache.removeTextureForKey(this.url); // item.rawUrl || item.url
+ return super.destroy();
+ }
+
+ /**
+ * !#en
+ * Release texture, please use destroy instead.
+ * !#zh 释放纹理,请使用 destroy 替代。
+ * @method releaseTexture
+ * @deprecated since v2.0
+ */
+ public releaseTexture () {
+ this._setMipmaps([]);
+ this._texture && this._texture.destroy();
+ }
+
+ /**
+ * Sets the _mipmaps to specified value, and updates the width and height accordingly.
+ * If available, synchronize the mipmap data to underlying texture.
+ * @param {Mipmap[]} mipmaps
+ */
+ public _setMipmaps (mipmaps) {
+ this._mipmaps = mipmaps;
+ if (mipmaps.length === 0) {
+ super.update({
+ width: 0,
+ height: 0,
+ });
+ } else {
+ super.update({
+ width: this._mipmaps[0].front.width,
+ height: this._mipmaps[0].front.height,
+ format: this._mipmaps[0].front._format,
+ });
+ }
+
+ let counter = 0;
+ const inc = () => {
+ ++counter;
+ if (counter === this._mipmaps.length * 6) {
+ this._updateMipmaps(this._mipmaps.map((mipmap) => TextureCube._toMipmapArray(mipmap)));
+ }
+ };
+ mipmaps.forEach((mipmap) => {
+ TextureCube._forEachFace(mipmap, (face) => {
+ if (face.loaded) {
+ inc();
+ } else {
+ face.addEventListener('load', () => inc());
+ }
+ });
+ });
+ }
+
+ /**
+ * Updates mipmaps of the underlying texture.
+ * @param {ImageSource[][]} mipmapSources
+ */
+ public _updateMipmaps (mipmapSources) {
+ const opts = this._getOpts();
+ opts.images = mipmapSources;
+ if (!this._texture) {
+ this._texture = new GFXTextureCube(cc.game._renderContext, opts);
+ } else {
+ this._texture.update(opts);
+ }
+ }
+
+ /**
+ * Update texture options, not available in Canvas render mode.
+ * image, format, premultiplyAlpha can not be updated in native.
+ * @method update
+ */
+ public update (options) {
+ if (!options) { return; }
+
+ super.update(options);
+
+ // TODO: options.image
+
+ if (this._texture) {
+ this._texture.update(options);
+ }
+ }
+
+ public _serialize () {
return {
base: super._serialize(),
- mipmaps: this._mipmaps.map(mipmap => {return {
+ mipmaps: this._mipmaps.map((mipmap) => {return {
front: mipmap.front._uuid,
back: mipmap.back._uuid,
left: mipmap.left._uuid,
right: mipmap.right._uuid,
top: mipmap.top._uuid,
bottom: mipmap.bottom._uuid,
- };})
+ }; }),
};
}
- _deserialize (data, handle) {
+ public _deserialize (data, handle) {
super._deserialize(data.base);
this._mipmaps = new Array(data.mipmaps.length);
@@ -277,7 +277,7 @@ export default class TextureCube extends TextureBase {
}
}
- onLoaded() {
+ public onLoaded () {
this.mipmaps = this._mipmaps;
}
}
diff --git a/cocos/assets/image-asset.js b/cocos/assets/image-asset.ts
similarity index 83%
rename from cocos/assets/image-asset.js
rename to cocos/assets/image-asset.ts
index 9644ebed06..89bdf0e92b 100644
--- a/cocos/assets/image-asset.js
+++ b/cocos/assets/image-asset.ts
@@ -22,8 +22,8 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
-//@ts-check
-import {ccclass, property} from "../core/data/class-decorator";
+// @ts-check
+import {ccclass, property} from '../core/data/class-decorator';
import EventTarget from '../core/event/event-target';
import {addon} from '../core/utils/js';
@@ -39,12 +39,12 @@ const CHAR_CODE_0 = 48; // '0'
@ccclass('cc.ImageAsset')
export default class ImageAsset extends cc.Asset {
- static extnames = ['.png', '.jpg', '.jpeg', '.bmp', '.webp', '.pvr', '.etc'];
+ public static extnames = ['.png', '.jpg', '.jpeg', '.bmp', '.webp', '.pvr', '.etc'];
/**
* @param {HTMLCanvasElement | HTMLImageElement | RawImageData} nativeAsset
*/
- constructor(nativeAsset = undefined) {
+ constructor (nativeAsset = undefined) {
super();
EventTarget.call(this);
@@ -58,7 +58,7 @@ export default class ImageAsset extends cc.Asset {
* @readonly
*/
// TODO - use nativeUrl directly
- this.url = "";
+ this.url = '';
/**
* @type {HTMLCanvasElement | HTMLImageElement | RawImageData}
@@ -68,7 +68,7 @@ export default class ImageAsset extends cc.Asset {
width: 0,
height: 0,
format: 0,
- _compressed: false
+ _compressed: false,
};
if (CC_EDITOR) {
@@ -79,12 +79,12 @@ export default class ImageAsset extends cc.Asset {
this._nativeAsset = nativeAsset;
}
}
-
+
/**
* @type {HTMLCanvasElement | HTMLImageElement | RawImageData}
*/
@property({
- override: true
+ override: true,
})
get _nativeAsset () {
// maybe returned to pool in webgl
@@ -101,14 +101,14 @@ export default class ImageAsset extends cc.Asset {
/**
* @type {HTMLCanvasElement | HTMLImageElement | ArrayBufferView}
*/
- get data() {
+ get data () {
return this._nativeData instanceof HTMLElement ? this._nativeData : this._nativeData._data;
}
/**
* @param {HTMLCanvasElement | HTMLImageElement | RawImageData} data
*/
- reset(data) {
+ public reset (data) {
if (!(data instanceof HTMLElement)) {
this._nativeData = Object.create(data);
this._onDataComplete();
@@ -128,15 +128,15 @@ export default class ImageAsset extends cc.Asset {
}
}
- get width() {
+ get width () {
return this._nativeData.width;
}
- get height() {
+ get height () {
return this._nativeData.height;
}
- get format() {
+ get format () {
if (this._nativeData instanceof HTMLElement) {
return undefined;
}
@@ -154,20 +154,20 @@ export default class ImageAsset extends cc.Asset {
// SERIALIZATION
- _serialize () {
- let extId = "";
+ public _serialize () {
+ let extId = '';
let exportedExts = this._exportedExts;
if (!exportedExts && this._native) {
exportedExts = [this._native];
}
if (exportedExts) {
- let exts = [];
+ const exts = [];
for (let i = 0; i < exportedExts.length; i++) {
- let extId = "";
- let ext = exportedExts[i];
+ let extId = '';
+ const ext = exportedExts[i];
if (ext) {
// ext@format
- let extFormat = ext.split('@');
+ const extFormat = ext.split('@');
extId = ImageAsset.extnames.indexOf(extFormat[0]);
if (extId < 0) {
extId = ext;
@@ -180,27 +180,27 @@ export default class ImageAsset extends cc.Asset {
}
extId = exts.join('_');
}
- return "" + extId;
+ return '' + extId;
}
- _deserialize (data, handle) {
- let fields = data.split(',');
+ public _deserialize (data, handle) {
+ const fields = data.split(',');
// decode extname
- var extIdStr = fields[0];
+ const extIdStr = fields[0];
if (extIdStr) {
- let extIds = extIdStr.split('_');
+ const extIds = extIdStr.split('_');
let extId = 999;
let ext = '';
let format = this._format;
- let SupportTextureFormats = cc.macro.SUPPORT_TEXTURE_FORMATS;
+ const SupportTextureFormats = cc.macro.SUPPORT_TEXTURE_FORMATS;
for (let i = 0; i < extIds.length; i++) {
- let extFormat = extIds[i].split('@');
+ const extFormat = extIds[i].split('@');
let tmpExt = extFormat[0];
tmpExt = tmpExt.charCodeAt(0) - CHAR_CODE_0;
tmpExt = ImageAsset.extnames[tmpExt] || extFormat;
- let index = SupportTextureFormats.indexOf(tmpExt);
+ const index = SupportTextureFormats.indexOf(tmpExt);
if (index !== -1 && index < extId) {
extId = index;
ext = tmpExt;
@@ -214,19 +214,19 @@ export default class ImageAsset extends cc.Asset {
}
// preset uuid to get correct nativeUrl
- let loadingItem = handle.customEnv;
- let uuid = loadingItem && loadingItem.uuid;
+ const loadingItem = handle.customEnv;
+ const uuid = loadingItem && loadingItem.uuid;
if (uuid) {
this._uuid = uuid;
- var url = this.nativeUrl;
+ const url = this.nativeUrl;
this.url = url;
}
}
}
- _onDataComplete() {
+ public _onDataComplete () {
this.loaded = true;
- this.emit("load");
+ this.emit('load');
}
}
@@ -240,4 +240,4 @@ export default class ImageAsset extends cc.Asset {
*/
addon(ImageAsset.prototype, EventTarget.prototype);
-cc.ImageAsset = ImageAsset;
\ No newline at end of file
+cc.ImageAsset = ImageAsset;
diff --git a/cocos/assets/render-texture.js b/cocos/assets/render-texture.ts
similarity index 75%
rename from cocos/assets/render-texture.js
rename to cocos/assets/render-texture.ts
index 5b3302620f..e63fdc39ec 100644
--- a/cocos/assets/render-texture.js
+++ b/cocos/assets/render-texture.ts
@@ -1,6 +1,6 @@
+import { ccclass } from '../core/data/class-decorator';
import gfx from '../renderer/gfx';
import Texture2D from './texture-2d';
-import { ccclass } from '../core/data/class-decorator';
/**
* Render textures are textures that can be rendered to.
@@ -25,44 +25,42 @@ export default class RenderTexture extends Texture2D {
* @param {Number} [depthStencilFormat]
* @method initWithSize
*/
- initWithSize (width, height, depthStencilFormat) {
+ public initWithSize (width, height, depthStencilFormat) {
this.width = Math.floor(width || cc.visibleRect.width);
this.height = Math.floor(height || cc.visibleRect.height);
this._resetUnderlyingMipmaps();
- let opts = {
+ const opts = {
colors: [ this._texture ],
};
if (depthStencilFormat) {
- if (this.depthStencilBuffer) this.depthStencilBuffer.destroy();
- let depthStencilBuffer = new gfx.RenderBuffer(cc.game._renderContext, depthStencilFormat, this.width, this.height);
+ if (this.depthStencilBuffer) { this.depthStencilBuffer.destroy(); }
+ const depthStencilBuffer = new gfx.RenderBuffer(cc.game._renderContext, depthStencilFormat, this.width, this.height);
if (depthStencilFormat === gfx.RB_FMT_D24S8) {
opts.depthStencil = depthStencilBuffer;
- }
- else if (depthStencilFormat === gfx.RB_FMT_S8) {
+ } else if (depthStencilFormat === gfx.RB_FMT_S8) {
opts.stencil = depthStencilBuffer;
- }
- else if (depthStencilFormat === gfx.RB_FMT_D16) {
+ } else if (depthStencilFormat === gfx.RB_FMT_D16) {
opts.depth = depthStencilBuffer;
}
this.depthStencilBuffer = depthStencilBuffer;
this.depthStencilFormat = depthStencilFormat;
}
- if (this._framebuffer) this._framebuffer.destroy();
+ if (this._framebuffer) { this._framebuffer.destroy(); }
this._framebuffer = new gfx.FrameBuffer(cc.game._renderContext, this.width, this.height, opts);
this.loaded = true;
- this.emit("load");
+ this.emit('load');
}
- updateSize(width, height) {
+ public updateSize (width, height) {
this.width = Math.floor(width || cc.visibleRect.width);
this.height = Math.floor(height || cc.visibleRect.height);
this._resetUnderlyingMipmaps();
- let rbo = this.depthStencilBuffer;
- if (rbo) rbo.update(this.width, this.height);
+ const rbo = this.depthStencilBuffer;
+ if (rbo) { rbo.update(this.width, this.height); }
this._framebuffer._width = width;
this._framebuffer._height = height;
}
@@ -74,8 +72,8 @@ export default class RenderTexture extends Texture2D {
* @param {Number} x
* @param {Number} y
*/
- drawTextureAt (texture, x, y) {
- if (!texture._image) return;
+ public drawTextureAt (texture, x, y) {
+ if (!texture._image) { return; }
this._texture.updateSubImage({
x, y,
@@ -84,8 +82,8 @@ export default class RenderTexture extends Texture2D {
height: texture.height,
level: 0,
flipY: false,
- premultiplyAlpha: texture._premultiplyAlpha
- })
+ premultiplyAlpha: texture._premultiplyAlpha,
+ });
}
/**
@@ -106,17 +104,17 @@ export default class RenderTexture extends Texture2D {
* @param {Number} [h]
* @return {Uint8Array}
*/
- readPixels (data, x, y, w, h) {
- if (!this._framebuffer || !this._texture) return data;
+ public readPixels (data, x, y, w, h) {
+ if (!this._framebuffer || !this._texture) { return data; }
x = x || 0;
y = y || 0;
- let width = w || this.width;
- let height = h || this.height;
+ const width = w || this.width;
+ const height = h || this.height;
data = data || new Uint8Array(width * height * 4);
- let gl = cc.game._renderContext._gl;
- let oldFBO = gl.getParameter(gl.FRAMEBUFFER_BINDING);
+ const gl = cc.game._renderContext._gl;
+ const oldFBO = gl.getParameter(gl.FRAMEBUFFER_BINDING);
gl.bindFramebuffer(gl.FRAMEBUFFER, this._framebuffer._glID);
gl.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, data);
gl.bindFramebuffer(gl.FRAMEBUFFER, oldFBO);
@@ -124,7 +122,7 @@ export default class RenderTexture extends Texture2D {
return data;
}
- destroy () {
+ public destroy () {
super.destroy();
if (this._framebuffer) {
this._framebuffer.destroy();
diff --git a/cocos/assets/texture-2d.js b/cocos/assets/texture-2d.ts
similarity index 87%
rename from cocos/assets/texture-2d.js
rename to cocos/assets/texture-2d.ts
index 544984835b..d9468ba218 100644
--- a/cocos/assets/texture-2d.js
+++ b/cocos/assets/texture-2d.ts
@@ -24,10 +24,10 @@
THE SOFTWARE.
****************************************************************************/
// @ts-check
-import GFXTexture2D from '../renderer/gfx/texture-2d';
import {ccclass, property} from '../core/data/class-decorator';
-import TextureBase from './texture-base';
+import GFXTexture2D from '../renderer/gfx/texture-2d';
import ImageAsset from './image-asset';
+import TextureBase from './texture-base';
/**
* @typedef {import("./texture-base").ImageSource} ImageSource
@@ -48,15 +48,15 @@ import ImageAsset from './image-asset';
@ccclass('cc.Texture2D')
export default class Texture2D extends TextureBase {
@property({
- override: true
+ override: true,
})
- _flipY = true;
-
+ public _flipY = true;
+
/**
* @type {ImageAsset[]}
*/
@property([ImageAsset])
- _mipmaps = [];
+ public _mipmaps = [];
constructor () {
super();
@@ -72,7 +72,7 @@ export default class Texture2D extends TextureBase {
* Note that the result do not contains the auto generated mipmaps.
* @return {ImageAsset[]}
*/
- get mipmaps() {
+ get mipmaps () {
return this._mipmaps;
}
@@ -80,7 +80,7 @@ export default class Texture2D extends TextureBase {
* Sets the mipmaps images.
* @param {ImageAsset[]} value
*/
- set mipmaps(value) {
+ set mipmaps (value) {
this._setMipmaps(value);
}
@@ -88,7 +88,7 @@ export default class Texture2D extends TextureBase {
* Gets the mipmap image at level 0.
* @return {ImageAsset}
*/
- get image() {
+ get image () {
return this._mipmaps.length === 0 ? null : this._mipmaps[0];
}
@@ -96,22 +96,22 @@ export default class Texture2D extends TextureBase {
* Sets the mipmap images as a single mipmap image.
* @param {ImageAsset} value
*/
- set image(value) {
+ set image (value) {
this.mipmaps = [value];
}
/**
* Gets the underlying texture object.
*/
- getImpl () {
+ public getImpl () {
return this._texture;
}
/**
* Returns the string representation of this texture.
*/
- toString () {
- return this._mipmaps.length !== 0 ? this._mipmaps[0].url : "";
+ public toString () {
+ return this._mipmaps.length !== 0 ? this._mipmaps[0].url : '';
}
/**
@@ -120,7 +120,7 @@ export default class Texture2D extends TextureBase {
* @method update
* @param {TextureUpdateOpts} options
*/
- update (options) {
+ public update (options) {
if (!options) {
return;
}
@@ -132,14 +132,14 @@ export default class Texture2D extends TextureBase {
}
if (options.images !== undefined && options.images.length !== 0) {
- this._setMipmaps(options.images.map(image => {
+ this._setMipmaps(options.images.map((image) => {
const mipmap = new ImageAsset();
mipmap.reset(image instanceof HTMLElement ? image : {
_data: image,
width: this.width,
height: this.height,
format: this._format,
- _compressed: false
+ _compressed: false,
});
return mipmap;
}));
@@ -154,7 +154,7 @@ export default class Texture2D extends TextureBase {
*
* @param {ImageSource} source
*/
- updateImage(source) {
+ public updateImage (source) {
this.updateMipmaps([source]);
}
@@ -166,7 +166,7 @@ export default class Texture2D extends TextureBase {
* If the range specified by [firstLevel, firstLevel + sources.length) exceeds
* the actually range of mipmaps this texture contains, only overlaped mipmaps are updated.
*/
- updateMipmaps(sources, firstLevel = 0) {
+ public updateMipmaps (sources, firstLevel = 0) {
if (firstLevel >= sources.length) {
return;
}
@@ -181,15 +181,15 @@ export default class Texture2D extends TextureBase {
width: mipmap.width,
height: mipmap.height,
format: mipmap._format,
- _compressed: false
+ _compressed: false,
});
this._texture.updateImage({
width: this.width,
height: this.height,
- level: level,
+ level,
image: sources[i],
flipY: false,
- premultiplyAlpha: false
+ premultiplyAlpha: false,
});
}
}
@@ -202,8 +202,8 @@ export default class Texture2D extends TextureBase {
* @return {HTMLImageElement|HTMLCanvasElement}
* @deprecated Use this.image.data instead.
*/
- getHtmlElementObj () {
- return (this._mipmaps[0] && (this._mipmaps[0].data instanceof HTMLElement)) ? this._mipmaps[0].data: null;
+ public getHtmlElementObj () {
+ return (this._mipmaps[0] && (this._mipmaps[0].data instanceof HTMLElement)) ? this._mipmaps[0].data : null;
}
/**
@@ -217,7 +217,7 @@ export default class Texture2D extends TextureBase {
* @method destroy
* @return {Boolean} inherit from the CCObject
*/
- destroy () {
+ public destroy () {
this.releaseTexture();
// TODO cc.textureUtil ?
// cc.textureCache.removeTextureForKey(this.url); // item.rawUrl || item.url
@@ -231,8 +231,8 @@ export default class Texture2D extends TextureBase {
* @method description
* @returns {String}
*/
- description () {
- return "";
+ public description () {
+ return '';
}
/**
@@ -242,7 +242,7 @@ export default class Texture2D extends TextureBase {
* @method releaseTexture
* @deprecated since v2.0, use destroy instead.
*/
- releaseTexture () {
+ public releaseTexture () {
this._setMipmaps([]);
this._texture && this._texture.destroy();
}
@@ -252,19 +252,19 @@ export default class Texture2D extends TextureBase {
* If available, synchronize the mipmap data to underlying texture.
* @param {ImageAsset[]} mipmaps
*/
- _setMipmaps(mipmaps) {
+ public _setMipmaps (mipmaps) {
this._mipmaps = mipmaps;
if (mipmaps.length === 0) {
super.update({
width: 0,
- height: 0
+ height: 0,
});
} else {
const level0 = this._mipmaps[0];
super.update({
width: level0.width,
height: level0.height,
- format: level0._format
+ format: level0._format,
});
}
@@ -272,14 +272,14 @@ export default class Texture2D extends TextureBase {
const inc = () => {
++counter;
if (counter === mipmaps.length) {
- this._resetUnderlyingMipmaps(mipmaps.map(mipmap => mipmap.data));
+ this._resetUnderlyingMipmaps(mipmaps.map((mipmap) => mipmap.data));
}
};
- mipmaps.forEach(mipmap => {
+ mipmaps.forEach((mipmap) => {
if (mipmap.loaded) {
inc();
} else {
- mipmap.addEventListener("load", () => {
+ mipmap.addEventListener('load', () => {
inc();
});
}
@@ -290,7 +290,7 @@ export default class Texture2D extends TextureBase {
* Resets sources and amount of the underlying texture's the mipmaps.
* @param {ImageSource[]} mipmapSources
*/
- _resetUnderlyingMipmaps(mipmapSources = [null]) {
+ public _resetUnderlyingMipmaps (mipmapSources = [null]) {
const opts = this._getOpts();
opts.images = mipmapSources;
if (!this._texture) {
@@ -300,14 +300,14 @@ export default class Texture2D extends TextureBase {
}
}
- _serialize() {
+ public _serialize () {
return {
base: super._serialize(),
- mipmaps: this._mipmaps.map(mipmap => mipmap._uuid)
+ mipmaps: this._mipmaps.map((mipmap) => mipmap._uuid),
};
}
- _deserialize (data, handle) {
+ public _deserialize (data, handle) {
super._deserialize(data.base);
this._mipmaps = new Array(data.mipmaps.length);
@@ -319,7 +319,7 @@ export default class Texture2D extends TextureBase {
}
}
- onLoaded() {
+ public onLoaded () {
this.mipmaps = this._mipmaps;
}
}
diff --git a/cocos/assets/texture-base.js b/cocos/assets/texture-base.ts
similarity index 87%
rename from cocos/assets/texture-base.js
rename to cocos/assets/texture-base.ts
index 3a9a578b40..1c726fefa7 100644
--- a/cocos/assets/texture-base.js
+++ b/cocos/assets/texture-base.ts
@@ -24,13 +24,13 @@
THE SOFTWARE.
****************************************************************************/
// @ts-check
+import {ccclass, property} from '../core/data/class-decorator';
import EventTarget from '../core/event/event-target';
import IDGenerator from '../core/utils/id-generator';
-import Enum from '../core/value-types/enum';
import {addon} from '../core/utils/js';
+import Enum from '../core/value-types/enum';
import {enums} from '../renderer/gfx/enums';
-import { Asset } from './asset';
-import {ccclass, property} from '../core/data/class-decorator';
+import Asset from './CCAsset';
/**
* @typedef {import("../renderer/gfx/texture-2d").HTMLImageSource} HTMLImageSource
@@ -42,7 +42,7 @@ import {ccclass, property} from '../core/data/class-decorator';
* @exports TextureUpdateOpts
*/
-let _images = [];
+const _images = [];
const CHAR_CODE_1 = 49; // '1'
@@ -52,7 +52,7 @@ const GL_REPEAT = 10497; // gl.REPEAT
const GL_CLAMP_TO_EDGE = 33071; // gl.CLAMP_TO_EDGE
const GL_MIRRORED_REPEAT = 33648; // gl.MIRRORED_REPEAT
-var idGenerator = new IDGenerator('Tex');
+const idGenerator = new IDGenerator('Tex');
/**
* The texture pixel format, default value is RGBA8888,
@@ -180,7 +180,7 @@ export const WrapMode = Enum({
* @type {Number}
* @readonly
*/
- MIRRORED_REPEAT: GL_MIRRORED_REPEAT
+ MIRRORED_REPEAT: GL_MIRRORED_REPEAT,
});
/**
@@ -201,7 +201,7 @@ export const Filter = Enum({
* @type {Number}
* @readonly
*/
- NEAREST: GL_NEAREST
+ NEAREST: GL_NEAREST,
});
const FilterIndex = {
@@ -212,7 +212,7 @@ const FilterIndex = {
/**
* @type TextureUpdateOpts
*/
-let _sharedOpts = {
+const _sharedOpts = {
width: undefined,
height: undefined,
minFilter: undefined,
@@ -226,10 +226,10 @@ let _sharedOpts = {
image: undefined,
flipY: undefined,
premultiplyAlpha: undefined,
- anisotropy: undefined
+ anisotropy: undefined,
};
function _getSharedOptions () {
- for (var key in _sharedOpts) {
+ for (const key in _sharedOpts) {
_sharedOpts[key] = undefined;
}
_images.length = 0;
@@ -240,49 +240,49 @@ function _getSharedOptions () {
@ccclass('cc.TextureBase')
export default class TextureBase extends Asset {
- @property
- _genMipmap = false;
- @property
- _format = PixelFormat.RGBA8888;
+ public static PixelFormat = PixelFormat;
- @property
- _premultiplyAlpha = false;
+ public static WrapMode = WrapMode;
- @property
- _flipY = false;
-
- @property
- _minFilter = Filter.LINEAR;
-
- @property
- _magFilter = Filter.LINEAR;
-
- @property
- _mipFilter = Filter.LINEAR;
-
- @property
- _wrapS = WrapMode.CLAMP_TO_EDGE;
-
- @property
- _wrapT = WrapMode.CLAMP_TO_EDGE;
-
- @property
- _anisotropy = 1;
-
- static PixelFormat = PixelFormat;
-
- static WrapMode = WrapMode;
-
- static Filter = Filter;
+ public static Filter = Filter;
/**
*
* @param {TextureBase} texture
*/
- static _isCompressed (texture) {
+ public static _isCompressed (texture) {
return texture._format >= PixelFormat.RGB_PVRTC_2BPPV1 && texture._format <= PixelFormat.RGBA_PVRTC_4BPPV1;
}
+ @property
+ public _genMipmap = false;
+
+ @property
+ public _format = PixelFormat.RGBA8888;
+
+ @property
+ public _premultiplyAlpha = false;
+
+ @property
+ public _flipY = false;
+
+ @property
+ public _minFilter = Filter.LINEAR;
+
+ @property
+ public _magFilter = Filter.LINEAR;
+
+ @property
+ public _mipFilter = Filter.LINEAR;
+
+ @property
+ public _wrapS = WrapMode.CLAMP_TO_EDGE;
+
+ @property
+ public _wrapT = WrapMode.CLAMP_TO_EDGE;
+
+ @property
+ public _anisotropy = 1;
constructor () {
super();
@@ -324,7 +324,7 @@ export default class TextureBase extends Asset {
this.height = 0;
}
- getId () {
+ public getId () {
return this._id;
}
@@ -333,7 +333,7 @@ export default class TextureBase extends Asset {
* @method update
* @param {TextureUpdateOpts} options
*/
- update (options) {
+ public update (options) {
if (!options) {
return;
}
@@ -379,8 +379,8 @@ export default class TextureBase extends Asset {
}
}
- _getOpts() {
- let opts = _getSharedOptions();
+ public _getOpts () {
+ const opts = _getSharedOptions();
opts.width = this.width;
opts.height = this.height;
opts.mipmap = this._genMipmap;
@@ -403,8 +403,8 @@ export default class TextureBase extends Asset {
* @method getPixelFormat
* @return {Number}
*/
- getPixelFormat () {
- //support only in WebGl rendering mode
+ public getPixelFormat () {
+ // support only in WebGl rendering mode
return this._format;
}
@@ -415,7 +415,7 @@ export default class TextureBase extends Asset {
* @method hasPremultipliedAlpha
* @return {Boolean}
*/
- hasPremultipliedAlpha () {
+ public hasPremultipliedAlpha () {
return this._premultiplyAlpha || false;
}
@@ -425,7 +425,7 @@ export default class TextureBase extends Asset {
* @method getAnisotropy
* @return {Number}
*/
- getAnisotropy() {
+ public getAnisotropy () {
return this._anisotropy;
}
@@ -436,7 +436,7 @@ export default class TextureBase extends Asset {
* @method hasMipmap
* @return {Boolean}
*/
- genMipmap () {
+ public genMipmap () {
return this._genMipmap || false;
}
@@ -449,9 +449,9 @@ export default class TextureBase extends Asset {
* @param {WrapMode} wrapS
* @param {WrapMode} wrapT
*/
- setWrapMode (wrapS, wrapT) {
+ public setWrapMode (wrapS, wrapT) {
if (this._wrapS !== wrapS || this._wrapT !== wrapT) {
- var opts = _getSharedOptions();
+ const opts = _getSharedOptions();
opts.wrapS = wrapS;
opts.wrapT = wrapT;
this.update(opts);
@@ -465,9 +465,9 @@ export default class TextureBase extends Asset {
* @param {Filter} minFilter
* @param {Filter} magFilter
*/
- setFilters (minFilter, magFilter) {
+ public setFilters (minFilter, magFilter) {
if (this._minFilter !== minFilter || this._magFilter !== magFilter) {
- var opts = _getSharedOptions();
+ const opts = _getSharedOptions();
opts.minFilter = minFilter;
opts.magFilter = magFilter;
this.update(opts);
@@ -480,9 +480,9 @@ export default class TextureBase extends Asset {
* @method setMipFilter
* @param {Filter} mipFilter
*/
- setMipFilter (mipFilter) {
+ public setMipFilter (mipFilter) {
if (this._mipFilter !== mipFilter) {
- var opts = _getSharedOptions();
+ const opts = _getSharedOptions();
opts.mipFilter = mipFilter;
this.update(opts);
}
@@ -495,9 +495,9 @@ export default class TextureBase extends Asset {
* @method setFlipY
* @param {Boolean} flipY
*/
- setFlipY (flipY) {
+ public setFlipY (flipY) {
if (this._flipY !== flipY) {
- var opts = _getSharedOptions();
+ const opts = _getSharedOptions();
opts.flipY = flipY;
this.update(opts);
}
@@ -510,9 +510,9 @@ export default class TextureBase extends Asset {
* @method setPremultiplyAlpha
* @param {Boolean} premultiply
*/
- setPremultiplyAlpha (premultiply) {
+ public setPremultiplyAlpha (premultiply) {
if (this._premultiplyAlpha !== premultiply) {
- var opts = _getSharedOptions();
+ const opts = _getSharedOptions();
opts.premultiplyAlpha = premultiply;
this.update(opts);
}
@@ -524,9 +524,9 @@ export default class TextureBase extends Asset {
* @method setAnisotropy
* @param {number} anisotropy
*/
- setAnisotropy(anisotropy) {
- if (this._anisotropy != anisotropy) {
- var opts = _getSharedOptions();
+ public setAnisotropy (anisotropy) {
+ if (this._anisotropy !== anisotropy) {
+ const opts = _getSharedOptions();
opts.anisotropy = anisotropy;
this.update(opts);
}
@@ -539,9 +539,9 @@ export default class TextureBase extends Asset {
* @method setGenerateMipmap
* @param {Boolean} mipmap
*/
- setGenMipmap (mipmap) {
+ public setGenMipmap (mipmap) {
if (this._genMipmap !== mipmap) {
- var opts = _getSharedOptions();
+ const opts = _getSharedOptions();
opts.mipmap = mipmap;
this.update(opts);
}
@@ -552,12 +552,12 @@ export default class TextureBase extends Asset {
/**
* @return {any}
*/
- _serialize () {
- return this._minFilter + "," + this._magFilter + "," +
- this._wrapS + "," + this._wrapT + "," +
- (this._premultiplyAlpha ? 1 : 0) + "," +
- this._mipFilter + "," + this._anisotropy + "," +
- (this._flipY ? 1 : 0) + "," +
+ public _serialize () {
+ return this._minFilter + ',' + this._magFilter + ',' +
+ this._wrapS + ',' + this._wrapT + ',' +
+ (this._premultiplyAlpha ? 1 : 0) + ',' +
+ this._mipFilter + ',' + this._anisotropy + ',' +
+ (this._flipY ? 1 : 0) + ',' +
(this._genMipmap ? 1 : 0);
}
@@ -565,9 +565,9 @@ export default class TextureBase extends Asset {
*
* @param {string} data
*/
- _deserialize (data) {
- let fields = data.split(',');
- fields.unshift("");
+ public _deserialize (data) {
+ const fields = data.split(',');
+ fields.unshift('');
if (fields.length >= 6) {
// decode filters
this._minFilter = parseInt(fields[1]);