forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture.js
More file actions
320 lines (276 loc) · 13.2 KB
/
texture.js
File metadata and controls
320 lines (276 loc) · 13.2 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
pc.extend(pc, function () {
'use strict';
var JSON_ADDRESS_MODE = {
"repeat": pc.ADDRESS_REPEAT,
"clamp": pc.ADDRESS_CLAMP_TO_EDGE,
"mirror": pc.ADDRESS_MIRRORED_REPEAT
};
var JSON_FILTER_MODE = {
"nearest": pc.FILTER_NEAREST,
"linear": pc.FILTER_LINEAR,
"nearest_mip_nearest": pc.FILTER_NEAREST_MIPMAP_NEAREST,
"linear_mip_nearest": pc.FILTER_LINEAR_MIPMAP_NEAREST,
"nearest_mip_linear": pc.FILTER_NEAREST_MIPMAP_LINEAR,
"linear_mip_linear": pc.FILTER_LINEAR_MIPMAP_LINEAR
};
function arrayBufferCopy(src, dst, dstByteOffset, numBytes) {
var i;
var dst32Offset = dstByteOffset / 4;
var tail = (numBytes % 4);
var src32 = new Uint32Array(src.buffer, 0, (numBytes - tail) / 4);
var dst32 = new Uint32Array(dst.buffer);
for (i = 0; i < src32.length; i++) {
dst32[dst32Offset + i] = src32[i];
}
for (i = numBytes - tail; i < numBytes; i++) {
dst[dstByteOffset + i] = src[i];
}
}
var TextureHandler = function (device, assets, loader) {
this._device = device;
this._assets = assets;
this._loader = loader;
// by default don't try cross-origin, because some browsers send different cookies (e.g. safari) if this is set.
this.crossOrigin = undefined;
if (assets.prefix) {
// ensure we send cookies if we load images.
this.crossOrigin = 'anonymous';
}
};
TextureHandler.prototype = {
load: function (url, callback) {
var self = this;
var image;
var urlWithoutParams = url.indexOf('?') >= 0 ? url.split('?')[0] : url;
var ext = pc.path.getExtension(urlWithoutParams).toLowerCase();
if ((ext === '.dds') || (ext === '.crn')) {
var options = {
cache: true,
responseType: "arraybuffer"
};
pc.http.get(url, options, function (err, response) {
if (!err) {
callback(null, response);
} else {
callback(err);
}
});
} else if ((ext === '.jpg') || (ext === '.jpeg') || (ext === '.gif') || (ext === '.png')) {
image = new Image();
// only apply cross-origin setting if this is an absolute URL, relative URLs can never be cross-origin
if (self.crossOrigin !== undefined && pc.ABSOLUTE_URL.test(url)) {
image.crossOrigin = self.crossOrigin;
}
// Call success callback after opening Texture
image.onload = function () {
callback(null, image);
};
// Call error callback with details.
image.onerror = function (event) {
callback(pc.string.format("Error loading Texture from: '{0}'", url));
};
image.src = url;
} else {
var blobStart = urlWithoutParams.indexOf("blob:");
if (blobStart >= 0) {
urlWithoutParams = urlWithoutParams.substr(blobStart);
url = urlWithoutParams;
image = new Image();
// Call success callback after opening Texture
image.onload = function () {
callback(null, image);
};
// Call error callback with details.
image.onerror = function (event) {
callback(pc.string.format("Error loading Texture from: '{0}'", url));
};
image.src = url;
} else {
// Unsupported texture extension
// Use timeout because asset events can be hooked up after load gets called in some
// cases. For example, material loads a texture on 'add' event.
setTimeout(function () {
callback(pc.string.format("Error loading Texture: format not supported: '{0}'", ext));
}, 0);
}
}
},
open: function (url, data) {
if (! url)
return;
var texture;
var ext = pc.path.getExtension(url).toLowerCase();
var format = null;
// Every browser seems to pass data as an Image type. For some reason, the XDK
// passes an HTMLImageElement. TODO: figure out why!
// DDS textures are ArrayBuffers
if ((data instanceof Image) || (data instanceof HTMLImageElement)) { // PNG, JPG or GIF
var img = data;
format = (ext === ".jpg" || ext === ".jpeg") ? pc.PIXELFORMAT_R8_G8_B8 : pc.PIXELFORMAT_R8_G8_B8_A8;
texture = new pc.Texture(this._device, {
// #ifdef PROFILER
profilerHint: pc.TEXHINT_ASSET,
// #endif
width: img.width,
height: img.height,
format: format
});
texture.setSource(img);
} else if (data instanceof ArrayBuffer) { // DDS or CRN
if (ext === ".crn") {
// Copy loaded file into Emscripten-managed memory
var srcSize = data.byteLength;
var bytes = new Uint8Array(data);
var src = Module._malloc(srcSize);
arrayBufferCopy(bytes, Module.HEAPU8, src, srcSize);
// Decompress CRN to DDS (minus the header)
var dst = Module._crn_decompress_get_data(src, srcSize);
var dstSize = Module._crn_decompress_get_size(src, srcSize);
data = Module.HEAPU8.buffer.slice(dst, dst + dstSize);
}
// DDS loading
var header = new Uint32Array(data, 0, 128 / 4);
var width = header[4];
var height = header[3];
var mips = Math.max(header[7], 1);
var isFourCc = header[20] === 4;
var fcc = header[21];
var bpp = header[22];
var isCubemap = header[28] === 65024; // TODO: check by bitflag
var FCC_DXT1 = 827611204; // DXT1
var FCC_DXT5 = 894720068; // DXT5
var FCC_FP32 = 116; // RGBA32f
// non standard
var FCC_ETC1 = 826496069;
var FCC_PVRTC_2BPP_RGB_1 = 825438800;
var FCC_PVRTC_2BPP_RGBA_1 = 825504336;
var FCC_PVRTC_4BPP_RGB_1 = 825439312;
var FCC_PVRTC_4BPP_RGBA_1 = 825504848;
var compressed = false;
var floating = false;
var etc1 = false;
var pvrtc2 = false;
var pvrtc4 = false;
if (isFourCc) {
if (fcc===FCC_DXT1) {
format = pc.PIXELFORMAT_DXT1;
compressed = true;
} else if (fcc===FCC_DXT5) {
format = pc.PIXELFORMAT_DXT5;
compressed = true;
} else if (fcc===FCC_FP32) {
format = pc.PIXELFORMAT_RGBA32F;
floating = true;
} else if (fcc===FCC_ETC1) {
format = pc.PIXELFORMAT_ETC1;
compressed = true;
etc1 = true;
} else if (fcc===FCC_PVRTC_2BPP_RGB_1 || fcc===FCC_PVRTC_2BPP_RGBA_1) {
format = fcc===FCC_PVRTC_2BPP_RGB_1? pc.PIXELFORMAT_PVRTC_2BPP_RGB_1 : pc.PIXELFORMAT_PVRTC_2BPP_RGBA_1;
compressed = true;
pvrtc2 = true;
} else if (fcc===FCC_PVRTC_4BPP_RGB_1 || fcc===FCC_PVRTC_4BPP_RGBA_1) {
format = fcc===FCC_PVRTC_4BPP_RGB_1? pc.PIXELFORMAT_PVRTC_4BPP_RGB_1 : pc.PIXELFORMAT_PVRTC_4BPP_RGBA_1;
compressed = true;
pvrtc4 = true;
}
} else {
if (bpp===32) {
format = pc.PIXELFORMAT_R8_G8_B8_A8;
}
}
if (! format) {
// #ifdef DEBUG
console.error("This DDS pixel format is currently unsupported. Empty texture will be created instead.");
// #endif
texture = new pc.Texture(this._device, {
width: 4,
height: 4,
format: pc.PIXELFORMAT_R8_G8_B8
});
return texture;
}
var texOptions = {
// #ifdef PROFILER
profilerHint: pc.TEXHINT_ASSET,
// #endif
width: width,
height: height,
format: format,
cubemap: isCubemap
};
texture = new pc.Texture(this._device, texOptions);
if (isCubemap) {
texture.addressU = pc.ADDRESS_CLAMP_TO_EDGE;
texture.addressV = pc.ADDRESS_CLAMP_TO_EDGE;
}
var offset = 128;
var faces = isCubemap? 6 : 1;
var mipSize;
var DXT_BLOCK_WIDTH = 4;
var DXT_BLOCK_HEIGHT = 4;
var blockSize = fcc===FCC_DXT1? 8 : 16;
var numBlocksAcross, numBlocksDown, numBlocks;
for(var face=0; face<faces; face++) {
var mipWidth = width;
var mipHeight = height;
for(var i=0; i<mips; i++) {
if (compressed) {
if (etc1) {
mipSize = Math.floor((mipWidth + 3) / 4) * Math.floor((mipHeight + 3) / 4) * 8;
} else if (pvrtc2) {
mipSize = Math.max(mipWidth, 16) * Math.max(mipHeight, 8) / 4;
} else if (pvrtc4) {
mipSize = Math.max(mipWidth, 8) * Math.max(mipHeight, 8) / 2;
} else {
numBlocksAcross = Math.floor((mipWidth + DXT_BLOCK_WIDTH - 1) / DXT_BLOCK_WIDTH);
numBlocksDown = Math.floor((mipHeight + DXT_BLOCK_HEIGHT - 1) / DXT_BLOCK_HEIGHT);
numBlocks = numBlocksAcross * numBlocksDown;
mipSize = numBlocks * blockSize;
}
} else {
mipSize = mipWidth * mipHeight * 4;
}
var mipBuff = floating? new Float32Array(data, offset, mipSize) : new Uint8Array(data, offset, mipSize);
if (!isCubemap) {
texture._levels[i] = mipBuff;
} else {
if (!texture._levels[i]) texture._levels[i] = [];
texture._levels[i][face] = mipBuff;
}
offset += floating? mipSize * 4 : mipSize;
mipWidth = Math.max(mipWidth * 0.5, 1);
mipHeight = Math.max(mipHeight * 0.5, 1);
}
}
texture.upload();
}
return texture;
},
patch: function (asset, assets) {
var texture = asset.resource;
if (! texture)
return;
if (texture.name !== asset.name)
texture.name = asset.name;
if (asset.data.hasOwnProperty('minfilter') && texture.minFilter !== JSON_FILTER_MODE[asset.data.minfilter])
texture.minFilter = JSON_FILTER_MODE[asset.data.minfilter];
if (asset.data.hasOwnProperty('magfilter') && texture.magFilter !== JSON_FILTER_MODE[asset.data.magfilter])
texture.magFilter = JSON_FILTER_MODE[asset.data.magfilter];
if (asset.data.hasOwnProperty('addressu') && texture.addressU !== JSON_ADDRESS_MODE[asset.data.addressu])
texture.addressU = JSON_ADDRESS_MODE[asset.data.addressu];
if (asset.data.hasOwnProperty('addressv') && texture.addressV !== JSON_ADDRESS_MODE[asset.data.addressv])
texture.addressV = JSON_ADDRESS_MODE[asset.data.addressv];
if (asset.data.hasOwnProperty('mipmaps') && texture.mipmaps !== asset.data.mipmaps)
texture.mipmaps = asset.data.mipmaps;
if (asset.data.hasOwnProperty('anisotropy') && texture.anisotropy !== asset.data.anisotropy)
texture.anisotropy = asset.data.anisotropy;
var rgbm = !!asset.data.rgbm;
if (asset.data.hasOwnProperty('rgbm') && texture.rgbm !== rgbm)
texture.rgbm = rgbm;
}
};
return {
TextureHandler: TextureHandler,
};
}());