-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
texture-2d.ts
288 lines (254 loc) · 9.57 KB
/
texture-2d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* @category asset
*/
import { ccclass, type } from 'cc.decorator';
import { GFXTextureType } from '../gfx/define';
import { PixelFormat } from './asset-enum';
import { ImageAsset } from './image-asset';
import { PresumedGFXTextureInfo, SimpleTexture } from './simple-texture';
import { legacyCC } from '../global-exports';
/**
* @en The create information for [[Texture2D]]
* @zh 用来创建贴图的信息。
*/
export interface ITexture2DCreateInfo {
/**
* @en The pixel width
* @zh 像素宽度。
*/
width: number;
/**
* @en The pixel height
* @zh 像素高度。
*/
height: number;
/**
* @en The pixel format
* @zh 像素格式。
* @default PixelFormat.RGBA8888
*/
format?: PixelFormat;
/**
* @en The mipmap level count
* @zh mipmap 层级。
* @default 1
*/
mipmapLevel?: number;
}
/**
* @en The 2D texture asset. It supports mipmap, each level of mipmap use an [[ImageAsset]].
* @zh 二维贴图资源。二维贴图资源的每个 Mipmap 层级都为一张 [[ImageAsset]]。
*/
@ccclass('cc.Texture2D')
export class Texture2D extends SimpleTexture {
/**
* @en All levels of mipmap images, be noted, automatically generated mipmaps are not included.
* When setup mipmap, the size of the texture and pixel format could be modified.
* @zh 所有层级 Mipmap,注意,这里不包含自动生成的 Mipmap。
* 当设置 Mipmap 时,贴图的尺寸以及像素格式可能会改变。
*/
get mipmaps () {
return this._mipmaps;
}
set mipmaps (value) {
this._mipmaps = value;
this._setMipmapLevel(this._mipmaps.length);
if (this._mipmaps.length > 0) {
const imageAsset: ImageAsset = this._mipmaps[0];
this.reset({
width: imageAsset.width,
height: imageAsset.height,
format: imageAsset.format,
mipmapLevel: this._mipmaps.length,
});
this._mipmaps.forEach((mipmap, level) => {
this._assignImage(mipmap, level);
});
} else {
this.reset({
width: 0,
height: 0,
mipmapLevel: this._mipmaps.length,
});
}
}
/**
* @en Level 0 mipmap image.
* Be noted, `this.image = img` equals `this.mipmaps = [img]`,
* sets image will clear all previous mipmaps.
* @zh 0 级 Mipmap。
* 注意,`this.image = img` 等价于 `this.mipmaps = [img]`,
* 也就是说,通过 `this.image` 设置 0 级 Mipmap 时将隐式地清除之前的所有 Mipmap。
*/
get image () {
return this._mipmaps.length === 0 ? null : this._mipmaps[0];
}
set image (value) {
this.mipmaps = value ? [value] : [];
}
@type([ImageAsset])
public _mipmaps: ImageAsset[] = [];
public initialize () {
this.mipmaps = this._mipmaps;
}
public onLoaded () {
this.initialize();
}
/**
* @en Reset the current texture with given size, pixel format and mipmap images.
* After reset, the gfx resource will become invalid, you must use [[uploadData]] explicitly to upload the new mipmaps to GPU resources.
* @zh 将当前贴图重置为指定尺寸、像素格式以及指定 mipmap 层级。重置后,贴图的像素数据将变为未定义。
* mipmap 图像的数据不会自动更新到贴图中,你必须显式调用 [[uploadData]] 来上传贴图数据。
* @param info The create information
*/
public reset (info: ITexture2DCreateInfo) {
this._width = info.width;
this._height = info.height;
this._setGFXFormat(info.format);
this._setMipmapLevel(info.mipmapLevel || 1);
this._tryReset();
}
/**
* @en Reset the current texture with given size, pixel format and mipmap images.
* After reset, the gfx resource will become invalid, you must use [[uploadData]] explicitly to upload the new mipmaps to GPU resources.
* @zh 将当前贴图重置为指定尺寸、像素格式以及指定 mipmap 层级。重置后,贴图的像素数据将变为未定义。
* mipmap 图像的数据不会自动更新到贴图中,你必须显式调用 [[uploadData]] 来上传贴图数据。
* @param width Pixel width
* @param height Pixel height
* @param format Pixel format
* @param mipmapLevel Mipmap level count
* @deprecated since v1.0 please use [[reset]] instead
*/
public create (width: number, height: number, format = PixelFormat.RGBA8888, mipmapLevel = 1) {
this.reset({
width,
height,
format,
mipmapLevel,
});
}
public toString () {
return this._mipmaps.length !== 0 ? this._mipmaps[0].url : '';
}
public updateMipmaps (firstLevel: number = 0, count?: number) {
if (firstLevel >= this._mipmaps.length) {
return;
}
const nUpdate = Math.min(
count === undefined ? this._mipmaps.length : count,
this._mipmaps.length - firstLevel);
for (let i = 0; i < nUpdate; ++i) {
const level = firstLevel + i;
this._assignImage(this._mipmaps[level], level);
}
}
/**
* @en If the level 0 mipmap image is a HTML element, then return it, otherwise return null.
* @zh 若此贴图 0 级 Mipmap 的图像资源的实际源存在并为 HTML 元素则返回它,否则返回 `null`。
* @returns HTML element or `null`
* @deprecated Please use [[image.data]] instead
*/
public getHtmlElementObj () {
return (this._mipmaps[0] && (this._mipmaps[0].data instanceof HTMLElement)) ? this._mipmaps[0].data : null;
}
/**
* @en Destroy the current 2d texture, clear up all mipmap levels and the related GPU resources.
* @zh 销毁此贴图,清空所有 Mipmap 并释放占用的 GPU 资源。
*/
public destroy () {
this._mipmaps = [];
return super.destroy();
}
/**
* @en Gets the description of the 2d texture
* @zh 返回此贴图的描述。
* @returns The description
*/
public description () {
const url = this._mipmaps[0] ? this._mipmaps[0].url : '';
return `<cc.Texture2D | Name = ${url} | Dimension = ${this.width} x ${this.height}>`;
}
/**
* @en Release used GPU resources.
* @zh 释放占用的 GPU 资源。
* @deprecated please use [[destroy]] instead
*/
public releaseTexture () {
this.destroy();
}
public _serialize (exporting?: any): any {
return {
base: super._serialize(exporting),
mipmaps: this._mipmaps.map((mipmap) => {
if (!mipmap || !mipmap._uuid) {
return null;
}
if (exporting) {
return EditorExtends.UuidUtils.compressUuid(mipmap._uuid, true);
}
return mipmap._uuid;
}),
};
}
public _deserialize (serializedData: any, handle: any) {
const data = serializedData as ITexture2DSerializeData;
super._deserialize(data.base, handle);
this._mipmaps = new Array(data.mipmaps.length);
for (let i = 0; i < data.mipmaps.length; ++i) {
// Prevent resource load failed
this._mipmaps[i] = new ImageAsset();
if (!data.mipmaps[i]) {
continue;
}
const mipmapUUID = data.mipmaps[i];
handle.result.push(this._mipmaps, `${i}`, mipmapUUID);
this._mipmaps[i]._texture = this;
}
}
protected _getGfxTextureCreateInfo (presumed: PresumedGFXTextureInfo) {
return Object.assign({
type: GFXTextureType.TEX2D,
width: this._width,
height: this._height,
}, presumed);
}
protected _checkTextureLoaded () {
let ready = true;
for (let i = 0; i < this._mipmaps.length; ++i) {
const image = this._mipmaps[i];
if (!image.loaded){
ready = false;
break;
}
}
if (ready) {
super._textureReady();
}
}
}
legacyCC.Texture2D = Texture2D;
export interface ITexture2DSerializeData {
base: string;
mipmaps: string[];
}