-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
material.ts
438 lines (407 loc) · 15.1 KB
/
material.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
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 material
*/
import { ccclass, property } from '../../core/data/class-decorator';
import { murmurhash2_32_gc } from '../../core/utils/murmurhash2_gc';
import { builtinResMgr } from '../3d/builtin';
import { RenderableComponent } from '../3d/framework/renderable-component';
import { GFXBindingType } from '../gfx/define';
import { GFXTextureView } from '../gfx/texture-view';
import { IDefineMap, Pass, PassOverrides } from '../renderer/core/pass';
import { samplerLib } from '../renderer/core/sampler-lib';
import { Asset } from './asset';
import { EffectAsset } from './effect-asset';
import { SpriteFrame } from './sprite-frame';
import { TextureBase } from './texture-base';
/**
* @zh
* 用来初始化材质的基本信息结构体。
*/
interface IMaterialInfo {
/**
* @zh
* 这个材质将使用的 EffectAsset,直接提供资源引用,和 effectName 至少要指定一个。
*/
effectAsset?: EffectAsset | null;
/**
* @zh
* 这个材质将使用的 EffectAsset,通过 effect 名指定,和 effectAsset 至少要指定一个。
*/
effectName?: string;
/**
* @zh
* 这个材质将使用第几个 technique,默认为 0。
*/
technique?: number;
/**
* @zh
* 这个材质定义的预处理宏,应与 shader 中的声明对应,默认全为 false。
*/
defines?: IDefineMap | IDefineMap[];
/**
* @zh
* 这个材质的自定义管线状态,将覆盖 effect 中的属性。<br>
* 注意在可能的情况下请尽量少的自定义管线状态,以减小对渲染效率的影响。
*/
states?: PassOverrides | PassOverrides[];
}
/**
* @zh
* 材质资源类,负责指定每个模型的材质信息。
*/
@ccclass('cc.Material')
export class Material extends Asset {
/**
* @zh
* 获取材质资源的实例,应当不会需要手动调用这个函数。
*/
public static getInstantiatedMaterial (mat: Material, rndCom: RenderableComponent, inEditor: boolean) {
if (mat._owner === rndCom) {
return mat;
} else {
const instance = new Material();
instance.copy(mat);
instance._native = mat._native + ' (Instance)';
instance._owner = rndCom;
if (inEditor) {
instance._uuid = mat._uuid;
}
return instance;
}
}
@property(EffectAsset)
protected _effectAsset: EffectAsset | null = null;
@property
protected _techIdx = 0;
@property
protected _defines: IDefineMap[] = [];
@property
protected _states: PassOverrides[] = [];
@property
protected _props: Array<Record<string, any>> = [];
protected _passes: Pass[] = [];
protected _owner: RenderableComponent | null = null;
protected _hash = 0;
/**
* @zh
* 当前使用的 EffectAsset 资源。
*/
get effectAsset () {
return this._effectAsset;
}
/**
* @zh
* 当前使用的 EffectAsset 资源名。
*/
get effectName () {
return this._effectAsset ? this._effectAsset.name : '';
}
/**
* @zh
* 当前的 technique 索引。
*/
get technique () {
return this._techIdx;
}
/**
* @zh
* 当前正在使用的 pass 数组。
*/
get passes () {
return this._passes;
}
/**
* @zh
* 材质的 hash。
*/
get hash () {
return this._hash;
}
constructor () {
super();
this.loaded = false;
}
/**
* @zh
* 根据所给信息初始化这个材质,初始化正常结束后材质即可立即用于渲染。
* @param info 初始化材质需要的基本信息。
*/
public reset (info: IMaterialInfo) {
if (!this._defines) { this._defines = []; }
if (!this._states) { this._states = []; }
if (!this._props) { this._props = []; }
if (info.technique !== undefined) { this._techIdx = info.technique; }
if (info.effectAsset) { this._effectAsset = info.effectAsset; }
else if (info.effectName) { this._effectAsset = EffectAsset.get(info.effectName); }
if (info.defines) { this._prepareInfo(info.defines, this._defines); }
if (info.states) { this._prepareInfo(info.states, this._states); }
this._update();
}
public initialize (info: IMaterialInfo) {
this.reset(info);
}
/**
* @zh
* 彻底销毁材质,注意销毁后无法重新初始化。<br>
* 如需重新初始化材质,不必先调用 destroy。
*/
public destroy () {
if (this._passes && this._passes.length) {
for (const pass of this._passes) {
pass.destroy();
}
}
this._passes = [];
this._effectAsset = null;
this._props = [];
this._defines = [];
this._states = [];
return super.destroy();
}
/**
* @zh
* 重置材质的所有 uniform 参数数据为 effect 默认初始值。
* @param clearPasses 是否同时重置当前正在用于渲染的 pass 数组内的信息
*/
public resetUniforms (clearPasses = true) {
this._props.length = this._passes.length;
this._props.fill({});
if (!clearPasses) { return; }
for (const pass of this._passes) {
pass.resetUBOs();
pass.resetTextures();
}
}
/**
* @zh
* 使用指定预处理宏重新编译当前 pass(数组)中的 shader。
* @param overrides 新增的预处理宏列表,会覆盖进当前列表。
* @param passIdx 要编译的 pass 索引,默认编译所有 pass。
*/
public recompileShaders (overrides: IDefineMap, passIdx?: number) {
if (!this._passes || !this._effectAsset) { return; }
if (passIdx === undefined) {
for (const pass of this._passes) {
pass.tryCompile(overrides);
}
} else {
this._passes[passIdx].tryCompile(overrides);
}
this._onPassesChange();
}
/**
* @zh
* 使用指定管线状态重载当前的 pass(数组)。
* @param overrides 新增的管线状态列表,会覆盖进当前列表。
* @param passIdx 要重载的 pass 索引,默认重载所有 pass。
*/
public overridePipelineStates (overrides: PassOverrides, passIdx?: number) {
if (!this._passes || !this._effectAsset) { return; }
const passInfos = this._effectAsset.techniques[this._techIdx].passes;
if (passIdx === undefined) {
for (const pass of this._passes) {
pass.overridePipelineStates(passInfos[pass.idxInTech], overrides);
}
} else {
this._passes[passIdx].overridePipelineStates(passInfos[passIdx], overrides);
}
this._onPassesChange();
}
/**
* @zh
* 通过 Loader 加载完成时的回调,将自动初始化材质资源。
*/
public onLoaded () {
this._update();
this.loaded = true;
this.emit('load');
}
/**
* @en
* Convenient property setter provided for quick material setup.<br>
* [[Pass.setUniform]] should be used instead<br>
* if you need to do per-frame property update.
* @zh
* 设置材质 uniform 参数的统一入口。<br>
* 注意如果需要每帧更新 uniform,建议使用 [[Pass.setUniform]] 以获得更好的性能。
* @param name 要设置的 uniform 名字。
* @param val 要设置的 uniform 值。
* @param passIdx 要设置的 pass 索引,默认设置所有 pass。
*/
public setProperty (name: string, val: any, passIdx?: number) {
let success = false;
if (passIdx === undefined) { // try set property for all applicable passes
const passes = this._passes;
const len = passes.length;
for (let i = 0; i < len; i++) {
const pass = passes[i];
if (this._uploadProperty(pass, name, val)) {
this._props[i][name] = val;
success = true;
}
}
} else {
if (passIdx >= this._passes.length) { console.warn(`illegal pass index: ${passIdx}.`); return; }
const pass = this._passes[passIdx];
if (this._uploadProperty(pass, name, val)) {
this._props[passIdx][name] = val;
success = true;
}
}
if (!success) {
console.warn(`illegal property name: ${name}.`);
return;
}
}
/**
* @zh
* 获取当前材质的指定 uniform 值。
* @param name 要获取的 uniform 名字。
* @param passIdx 要获取的源 pass 索引,默认遍历所有 pass,返回第一个找到指定名字的 uniform。
*/
public getProperty (name: string, passIdx?: number) {
if (passIdx === undefined) { // try get property in all possible passes
const propsArray = this._props;
const len = propsArray.length;
for (let i = 0; i < len; i++) {
const props = propsArray[i];
for (const p of Object.keys(props)) {
if (p === name) { return props[p]; }
}
}
} else {
if (passIdx >= this._props.length) { console.warn(`illegal pass index: ${passIdx}.`); return null; }
const props = this._props[passIdx];
for (const p of Object.keys(props)) {
if (p === name) { return props[p]; }
}
}
return null;
}
/**
* @zh
* 复制目标材质到当前实例。
* @param mat 要负责的目标材质。
*/
public copy (mat: Material) {
this._techIdx = mat._techIdx;
this._props.length = mat._props.length;
this._props.fill({});
for (let i = 0; i < mat._props.length; i++) {
Object.assign(this._props[i], mat._props[i]);
}
this._defines.length = mat._defines.length;
this._defines.fill({});
for (let i = 0; i < mat._defines.length; i++) {
Object.assign(this._defines[i], mat._defines[i]);
}
Object.assign(this._states, mat._states);
this._effectAsset = mat._effectAsset;
this._update();
}
protected _prepareInfo (patch: object | object[], cur: object[]) {
if (!Array.isArray(patch)) { // fill all the passes if not specified
const len = this._effectAsset ? this._effectAsset.techniques[this._techIdx].passes.length : 1;
patch = Array(len).fill(patch);
}
for (let i = 0; i < (patch as object[]).length; i++) {
Object.assign(cur[i] || (cur[i] = {}), patch[i]);
}
}
protected _update (keepProps: boolean = true) {
if (this._effectAsset) {
if (this._passes && this._passes.length) {
for (const pass of this._passes) {
pass.destroy();
}
}
this._passes = Pass.createPasses(this._effectAsset, {
techIdx: this._techIdx,
defines: this._defines,
states: this._states,
});
// handle property values
const totalPasses = this._effectAsset.techniques[this._techIdx].passes.length;
this._props.length = totalPasses;
if (keepProps) {
this._passes.forEach((pass, i) => {
let props = this._props[pass.idxInTech];
if (!props) { props = this._props[i] = {}; }
for (const p of Object.keys(props)) {
const val = props[p];
if (!val) { continue; }
this._uploadProperty(pass, p, props[p]);
}
});
} else {
this._props.fill({});
}
} else { // ugly yellow indicating missing effect
this._passes = builtinResMgr.get<Material>('missing-effect-material')._passes.slice();
}
this._onPassesChange();
}
protected _uploadProperty (pass: Pass, name: string, val: any) {
const handle = pass.getHandle(name);
if (handle === undefined) { return false; }
const bindingType = Pass.getBindingTypeFromHandle(handle);
if (bindingType === GFXBindingType.UNIFORM_BUFFER) {
if (Array.isArray(val)) {
pass.setUniformArray(handle, val);
} else {
pass.setUniform(handle, val);
}
} else if (bindingType === GFXBindingType.SAMPLER) {
const binding = Pass.getBindingFromHandle(handle);
if (val instanceof GFXTextureView) {
pass.bindTextureView(binding, val);
} else if (val instanceof TextureBase || val instanceof SpriteFrame) {
const textureView: GFXTextureView | null = val.getGFXTextureView();
if (!textureView || !textureView.texture.width || !textureView.texture.height) {
// console.warn(`material '${this._uuid}' received incomplete texture asset '${val._uuid}'`);
return false;
}
pass.bindTextureView(binding, textureView);
if (val instanceof TextureBase) {
pass.bindSampler(binding, samplerLib.getSampler(cc.game._gfxDevice, val.getSamplerHash()));
}
}
}
return true;
}
protected _onPassesChange () {
let str = '';
for (const pass of this._passes) {
str += pass.serializePipelineStates();
}
this._hash = murmurhash2_32_gc(str, 666);
if (this._owner) {
const comp = this._owner;
const index = comp.sharedMaterials.findIndex((m) => m === this);
// @ts-ignore
if (index >= 0) { comp._onRebuildPSO(index, this); }
}
}
}
cc.Material = Material;