forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaterial.js
More file actions
322 lines (263 loc) · 10.7 KB
/
material.js
File metadata and controls
322 lines (263 loc) · 10.7 KB
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
import { http } from '../net/http.js';
import { PIXELFORMAT_R8_G8_B8_A8 } from '../graphics/constants.js';
import { Texture } from '../graphics/texture.js';
import { SPECULAR_PHONG } from '../scene/constants.js';
import { standardMaterialCubemapParameters, standardMaterialTextureParameters } from '../scene/materials/standard-material-parameters.js';
import { AssetReference } from '../asset/asset-reference.js';
import { JsonStandardMaterialParser } from './parser/material/json-standard-material.js';
var PLACEHOLDER_MAP = {
aoMap: 'white',
diffuseMap: 'gray',
specularMap: 'gray',
metalnessMap: 'black',
glossMap: 'gray',
emissiveMap: 'gray',
normalMap: 'normal',
heightMap: 'gray',
opacityMap: 'gray',
sphereMap: 'gray',
lightMap: 'white'
};
/**
* @class
* @name MaterialHandler
* @implements {ResourceHandler}
* @classdesc Resource handler used for loading {@link Material} resources.
* @param {Application} app - The running {@link Application}.
*/
class MaterialHandler {
constructor(app) {
this._assets = app.assets;
this._device = app.graphicsDevice;
this._placeholderTextures = null;
this._parser = new JsonStandardMaterialParser();
this.maxRetries = 0;
}
load(url, callback) {
if (typeof url === 'string') {
url = {
load: url,
original: url
};
}
// Loading from URL (engine-only)
http.get(url.load, {
retry: this.maxRetries > 0,
maxRetries: this.maxRetries
}, function (err, response) {
if (!err) {
if (callback) {
response._engine = true;
callback(null, response);
}
} else {
if (callback) {
callback("Error loading material: " + url.original + " [" + err + "]");
}
}
});
}
open(url, data) {
var material = this._parser.parse(data);
// temp storage for engine-only as we need this during patching
if (data._engine) {
material._data = data;
delete data._engine;
}
return material;
}
// creates placeholders for textures
// that are used while texture is loading
_createPlaceholders() {
this._placeholderTextures = {};
var textures = {
white: [255, 255, 255, 255],
gray: [128, 128, 128, 255],
black: [0, 0, 0, 255],
normal: [128, 128, 255, 255]
};
for (var key in textures) {
if (!textures.hasOwnProperty(key))
continue;
// create texture
this._placeholderTextures[key] = new Texture(this._device, {
width: 2,
height: 2,
format: PIXELFORMAT_R8_G8_B8_A8
});
this._placeholderTextures[key].name = 'placeholder';
// fill pixels with color
var pixels = this._placeholderTextures[key].lock();
for (var i = 0; i < 4; i++) {
for (var c = 0; c < 4; c++) {
pixels[i * 4 + c] = textures[key][c];
}
}
this._placeholderTextures[key].unlock();
}
}
patch(asset, assets) {
// in an engine-only environment we manually copy the source data into the asset
if (asset.resource._data) {
asset._data = asset.resource._data; // use _data to avoid firing events
delete asset.resource._data; // remove from temp storage
}
// patch the name of the asset over the material name property
asset.data.name = asset.name;
asset.resource.name = asset.name;
this._bindAndAssignAssets(asset, assets);
asset.off('unload', this._onAssetUnload, this);
asset.on('unload', this._onAssetUnload, this);
}
_onAssetUnload(asset) {
// remove the parameter block we created which includes texture references
delete asset.data.parameters;
delete asset.data.chunks;
delete asset.data.name;
}
_assignTexture(parameterName, materialAsset, texture) {
materialAsset.data[parameterName] = texture;
materialAsset.resource[parameterName] = texture;
}
// assign a placeholder texture while waiting for one to load
// placeholder textures do not replace the data[parameterName] value
// in the asset.data thus preserving the final asset id until it is loaded
_assignPlaceholderTexture(parameterName, materialAsset) {
// create placeholder textures on-demand
if (!this._placeholderTextures) {
this._createPlaceholders();
}
var placeholder = PLACEHOLDER_MAP[parameterName];
var texture = this._placeholderTextures[placeholder];
materialAsset.resource[parameterName] = texture;
}
_onTextureLoad(parameterName, materialAsset, textureAsset) {
this._assignTexture(parameterName, materialAsset, textureAsset.resource);
materialAsset.resource.update();
}
_onTextureAdd(parameterName, materialAsset, textureAsset) {
this._assets.load(textureAsset);
}
_onTextureRemove(parameterName, materialAsset, textureAsset) {
var material = materialAsset.resource;
if (material) {
if (material[parameterName] === textureAsset.resource) {
this._assignTexture(parameterName, materialAsset, null);
material.update();
}
}
}
_assignCubemap(parameterName, materialAsset, textures) {
materialAsset.data[parameterName] = textures[0]; // the primary cubemap texture
if (textures.length === 7) {
// the prefiltered textures
materialAsset.data.prefilteredCubeMap128 = textures[1];
materialAsset.data.prefilteredCubeMap64 = textures[2];
materialAsset.data.prefilteredCubeMap32 = textures[3];
materialAsset.data.prefilteredCubeMap16 = textures[4];
materialAsset.data.prefilteredCubeMap8 = textures[5];
materialAsset.data.prefilteredCubeMap4 = textures[6];
}
}
_onCubemapLoad(parameterName, materialAsset, cubemapAsset) {
this._assignCubemap(parameterName, materialAsset, cubemapAsset.resources);
this._parser.initialize(materialAsset.resource, materialAsset.data);
}
_onCubemapAdd(parameterName, materialAsset, cubemapAsset) {
// phong based - so ensure we load individual faces
if (materialAsset.data.shadingModel === SPECULAR_PHONG) {
materialAsset.loadFaces = true;
}
this._assets.load(cubemapAsset);
}
_onCubemapRemove(parameterName, materialAsset, cubemapAsset) {
var material = materialAsset.resource;
if (material[parameterName] === cubemapAsset.resource) {
this._assignCubemap(parameterName, materialAsset, [null, null, null, null, null, null, null]);
material.update();
}
}
_bindAndAssignAssets(materialAsset, assets) {
// always migrate before updating material from asset data
var data = this._parser.migrate(materialAsset.data);
var material = materialAsset.resource;
var pathMapping = (data.mappingFormat === "path");
var TEXTURES = standardMaterialTextureParameters;
var i, name, assetReference;
// iterate through all texture parameters
for (i = 0; i < TEXTURES.length; i++) {
name = TEXTURES[i];
assetReference = material._assetReferences[name];
// data[name] contains an asset id for a texture
if (data[name] && !(data[name] instanceof Texture)) {
if (!assetReference) {
assetReference = new AssetReference(name, materialAsset, assets, {
load: this._onTextureLoad,
add: this._onTextureAdd,
remove: this._onTextureRemove
}, this);
material._assetReferences[name] = assetReference;
}
if (pathMapping) {
// texture paths are measured from the material directory
assetReference.url = materialAsset.getAbsoluteUrl(data[name]);
} else {
assetReference.id = data[name];
}
if (assetReference.asset) {
if (assetReference.asset.resource) {
// asset is already loaded
this._assignTexture(name, materialAsset, assetReference.asset.resource);
} else {
this._assignPlaceholderTexture(name, materialAsset);
}
assets.load(assetReference.asset);
}
} else {
if (assetReference) {
// texture has been removed
if (pathMapping) {
assetReference.url = null;
} else {
assetReference.id = null;
}
} else {
// no asset reference and no data field
// do nothing
}
}
}
var CUBEMAPS = standardMaterialCubemapParameters;
// iterate through all cubemap parameters
for (i = 0; i < CUBEMAPS.length; i++) {
name = CUBEMAPS[i];
assetReference = material._assetReferences[name];
// data[name] contains an asset id for a cubemap
if (data[name] && !(data[name] instanceof Texture)) {
if (!assetReference) {
assetReference = new AssetReference(name, materialAsset, assets, {
load: this._onCubemapLoad,
add: this._onCubemapAdd,
remove: this._onCubemapRemove
}, this);
material._assetReferences[name] = assetReference;
}
if (pathMapping) {
assetReference.url = data[name];
} else {
assetReference.id = data[name];
}
if (assetReference.asset) {
if (assetReference.asset.loaded) {
// asset loaded
this._assignCubemap(name, materialAsset, assetReference.asset.resources);
}
assets.load(assetReference.asset);
}
}
}
// call to re-initialize material after all textures assigned
this._parser.initialize(material, data);
}
}
export { MaterialHandler };