Debian Codegraph @

web/filament-js/utilities.js

raw
1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // ---------------
18 // Buffer Wrappers
19 // ---------------
20
21 // These wrappers make it easy for JavaScript clients to pass large swaths of data to Filament. They
22 // copy the contents of the given typed array into the WASM heap, then return a low-level buffer
23 // descriptor object. If the given array was taken from the WASM heap, then they create a temporary
24 // copy because the input pointer becomes invalidated after allocating heap memory for the buffer
25 // descriptor.
26
27 /// Buffer ::function:: Constructs a [BufferDescriptor] by copying a typed array into the WASM heap.
28 /// typedarray ::argument:: Data to consume (e.g. Uint8Array, Uint16Array, Float32Array)
29 /// ::retval:: [BufferDescriptor]
30 Filament.Buffer = function(typedarray) {
31 console.assert(typedarray.buffer instanceof ArrayBuffer);
32 console.assert(typedarray.byteLength > 0);
33 if (Filament.HEAPU32.buffer == typedarray.buffer) {
34 typedarray = new Uint8Array(typedarray);
35 }
36 const ta = typedarray;
37 const bd = new Filament.driver$BufferDescriptor(ta.byteLength);
38 const uint8array = new Uint8Array(ta.buffer, ta.byteOffset, ta.byteLength);
39 bd.getBytes().set(uint8array);
40 return bd;
41 };
42
43 /// PixelBuffer ::function:: Constructs a [PixelBufferDescriptor] by copying a typed array into \
44 /// the WASM heap.
45 /// typedarray ::argument:: Data to consume (e.g. Uint8Array, Uint16Array, Float32Array)
46 /// format ::argument:: [PixelDataFormat]
47 /// datatype ::argument:: [PixelDataType]
48 /// ::retval:: [PixelBufferDescriptor]
49 Filament.PixelBuffer = function(typedarray, format, datatype) {
50 console.assert(typedarray.buffer instanceof ArrayBuffer);
51 console.assert(typedarray.byteLength > 0);
52 if (Filament.HEAPU32.buffer == typedarray.buffer) {
53 typedarray = new Uint8Array(typedarray);
54 }
55 const ta = typedarray;
56 const bd = new Filament.driver$PixelBufferDescriptor(ta.byteLength, format, datatype);
57 const uint8array = new Uint8Array(ta.buffer, ta.byteOffset, ta.byteLength);
58 bd.getBytes().set(uint8array);
59 return bd;
60 };
61
62 /// CompressedPixelBuffer ::function:: Constructs a [PixelBufferDescriptor] for compressed texture
63 /// data by copying a typed array into the WASM heap.
64 /// typedarray ::argument:: Data to consume (e.g. Uint8Array, Uint16Array, Float32Array)
65 /// cdatatype ::argument:: [CompressedPixelDataType]
66 /// faceSize ::argument:: Number of bytes in each face (cubemaps only)
67 /// ::retval:: [PixelBufferDescriptor]
68 Filament.CompressedPixelBuffer = function(typedarray, cdatatype, faceSize) {
69 console.assert(typedarray.buffer instanceof ArrayBuffer);
70 console.assert(typedarray.byteLength > 0);
71 faceSize = faceSize || typedarray.byteLength;
72 if (Filament.HEAPU32.buffer == typedarray.buffer) {
73 typedarray = new Uint8Array(typedarray);
74 }
75 const ta = typedarray;
76 const bd = new Filament.driver$PixelBufferDescriptor(ta.byteLength, cdatatype, faceSize, true);
77 const uint8array = new Uint8Array(ta.buffer, ta.byteOffset, ta.byteLength);
78 bd.getBytes().set(uint8array);
79 return bd;
80 };
81
82 Filament._loadFilamesh = function(engine, buffer, definstance, matinstances) {
83 matinstances = matinstances || {};
84 const registry = new Filament.MeshReader$MaterialRegistry();
85 for (let key in matinstances) {
86 registry.set(key, matinstances[key]);
87 }
88 if (definstance) {
89 registry.set("DefaultMaterial", definstance);
90 }
91 const mesh = Filament.MeshReader.loadMeshFromBuffer(engine, buffer, registry);
92 const keys = registry.keys();
93 for (let i = 0; i < keys.size(); i++) {
94 const key = keys.get(i);
95 const minstance = registry.get(key);
96 matinstances[key] = minstance;
97 }
98 return {
99 "renderable": mesh.renderable(),
100 "vertexBuffer": mesh.vertexBuffer(),
101 "indexBuffer": mesh.indexBuffer(),
102 }
103 }
104
105 // ------------------
106 // Geometry Utilities
107 // ------------------
108
109 /// IcoSphere ::class:: Utility class for constructing spheres (requires glMatrix).
110 ///
111 /// The constructor takes an integer subdivision level, with 0 being an icosahedron.
112 ///
113 /// Exposes three arrays as properties:
114 ///
115 /// - `icosphere.vertices` Float32Array of XYZ coordinates.
116 /// - `icosphere.tangents` Uint16Array (interpreted as half-floats) encoding the surface orientation
117 /// as quaternions.
118 /// - `icosphere.triangles` Uint16Array with triangle indices.
119 ///
120 Filament.IcoSphere = function(nsubdivs) {
121 const X = .525731112119133606;
122 const Z = .850650808352039932;
123 const N = 0.;
124 this.vertices = new Float32Array([
125 -X, +N, +Z, +X, +N, +Z, -X, +N, -Z, +X, +N, -Z ,
126 +N, +Z, +X, +N, +Z, -X, +N, -Z, +X, +N, -Z, -X ,
127 +Z, +X, +N, -Z, +X, +N, +Z, -X, +N, -Z, -X, +N ,
128 ]);
129 this.triangles = new Uint16Array([
130 1, 4, 0, 4, 9, 0, 4, 5, 9, 8, 5, 4 , 1, 8, 4 ,
131 1, 10, 8, 10, 3, 8, 8, 3, 5, 3, 2, 5 , 3, 7, 2 ,
132 3, 10, 7, 10, 6, 7, 6, 11, 7, 6, 0, 11 , 6, 1, 0 ,
133 10, 1, 6, 11, 0, 9, 2, 11, 9, 5, 2, 9 , 11, 2, 7 ,
134 ]);
135
136 nsubdivs = nsubdivs || 0;
137 while (nsubdivs-- > 0) {
138 this.subdivide();
139 }
140
141 const nverts = this.vertices.length / 3;
142
143 // This is a unit sphere, so normals = positions.
144 const normals = this.vertices;
145
146 // Perform computations.
147 const sob = new Filament.SurfaceOrientation$Builder();
148 sob.vertexCount(nverts);
149 sob.normals(normals, 0)
150 const orientation = sob.build();
151
152 // Copy the results out of the helper.
153 this.tangents = orientation.getQuats(nverts);
154
155 // Free up the surface orientation helper now that we're done with it.
156 orientation.delete();
157 }
158
159 Filament.IcoSphere.prototype.subdivide = function() {
160 const srctris = this.triangles;
161 const srcverts = this.vertices;
162 const nsrctris = srctris.length / 3;
163 const ndsttris = nsrctris * 4;
164 const nsrcverts = srcverts.length / 3;
165 const ndstverts = nsrcverts + nsrctris * 3;
166 const dsttris = new Uint16Array(ndsttris * 3);
167 const dstverts = new Float32Array(ndstverts * 3);
168 dstverts.set(srcverts);
169 let srcind = 0, dstind = 0, i3 = nsrcverts * 3, i4 = i3 + 3, i5 = i4 + 3;
170 for (let tri = 0; tri < nsrctris; tri++, i3 += 9, i4 += 9, i5 += 9) {
171 const i0 = srctris[srcind++] * 3;
172 const i1 = srctris[srcind++] * 3;
173 const i2 = srctris[srcind++] * 3;
174 const v0 = srcverts.subarray(i0, i0 + 3);
175 const v1 = srcverts.subarray(i1, i1 + 3);
176 const v2 = srcverts.subarray(i2, i2 + 3);
177 const v3 = dstverts.subarray(i3, i3 + 3);
178 const v4 = dstverts.subarray(i4, i4 + 3);
179 const v5 = dstverts.subarray(i5, i5 + 3);
180 vec3.normalize(v3, vec3.add(v3, v0, v1));
181 vec3.normalize(v4, vec3.add(v4, v1, v2));
182 vec3.normalize(v5, vec3.add(v5, v2, v0));
183 dsttris[dstind++] = i0 / 3;
184 dsttris[dstind++] = i3 / 3;
185 dsttris[dstind++] = i5 / 3;
186 dsttris[dstind++] = i3 / 3;
187 dsttris[dstind++] = i1 / 3;
188 dsttris[dstind++] = i4 / 3;
189 dsttris[dstind++] = i5 / 3;
190 dsttris[dstind++] = i3 / 3;
191 dsttris[dstind++] = i4 / 3;
192 dsttris[dstind++] = i2 / 3;
193 dsttris[dstind++] = i5 / 3;
194 dsttris[dstind++] = i4 / 3;
195 }
196 this.triangles = dsttris;
197 this.vertices = dstverts;
198 }
199
200 // ---------------
201 // Math Extensions
202 // ---------------
203
204 function clamp(v, least, most) {
205 return Math.max(Math.min(most, v), least);
206 }
207
208 /// packSnorm16 ::function:: Converts a float in [-1, +1] into a half-float.
209 /// value ::argument:: float
210 /// ::retval:: half-float
211 Filament.packSnorm16 = function(value) {
212 return Math.round(clamp(value, -1.0, 1.0) * 32767.0);
213 }
214
215 /// loadMathExtensions ::function:: Extends the [glMatrix](http://glmatrix.net/) math library.
216 /// Filament does not require its clients to use glMatrix, but if its usage is detected then
217 /// the [init] function will automatically call `loadMathExtensions`.
218 /// This defines the following functions:
219 /// - **vec4.packSnorm16** can be used to create half-floats (see [packSnorm16])
220 /// - **mat3.fromRotation** now takes an arbitrary axis
221 Filament.loadMathExtensions = function() {
222 vec4.packSnorm16 = function(out, src) {
223 out[0] = Filament.packSnorm16(src[0]);
224 out[1] = Filament.packSnorm16(src[1]);
225 out[2] = Filament.packSnorm16(src[2]);
226 out[3] = Filament.packSnorm16(src[3]);
227 return out;
228 }
229 // In gl-matrix, mat3 rotation assumes rotation about the Z axis, so here we add a function
230 // to allow an arbitrary axis.
231 const fromRotationZ = mat3.fromRotation;
232 mat3.fromRotation = function(out, radians, axis) {
233 if (axis) {
234 return mat3.fromMat4(out, mat4.fromRotation(mat4.create(), radians, axis));
235 }
236 return fromRotationZ(out, radians);
237 };
238 };
239
240 // ---------------
241 // Texture helpers
242 // ---------------
243
244 Filament._createTextureFromKtx = function(ktxdata, engine, options) {
245 options = options || {};
246 const ktx = options['ktx'] || new Filament.KtxBundle(ktxdata);
247 const srgb = !!options['srgb'];
248 return Filament.ktx$createTexture(engine, ktx, srgb);
249 };
250
251 Filament._createIblFromKtx = function(ktxdata, engine, options) {
252 options = options || {};
253 const iblktx = options['ktx'] = new Filament.KtxBundle(ktxdata);
254
255 const format = iblktx.info().glInternalFormat;
256 if (format != this.ctx.R11F_G11F_B10F && format != this.ctx.RGB16F && format != this.ctx.RGB32F) {
257 console.warn('IBL texture format is 0x' + format.toString(16) +
258 ' which is not an expected floating-point format. Please use cmgen to generate IBL.');
259 }
260
261 const ibltex = Filament._createTextureFromKtx(ktxdata, engine, options);
262 const shstring = iblktx.getMetadata("sh");
263 const ibl = Filament.IndirectLight.Builder()
264 .reflections(ibltex)
265 .build(engine);
266 ibl.shfloats = shstring.split(/\s/, 9 * 3).map(parseFloat);
267 return ibl;
268 };
269
270 Filament._createTextureFromImageFile = function(fileContents, engine, options) {
271 const Sampler = Filament.Texture$Sampler;
272 const TextureFormat = Filament.Texture$InternalFormat;
273 const PixelDataFormat = Filament.PixelDataFormat;
274
275 options = options || {};
276 const srgb = !!options['srgb'];
277 const noalpha = !!options['noalpha'];
278 const nomips = !!options['nomips'];
279
280 const decodedImage = Filament.decodeImage(fileContents, noalpha ? 3 : 4);
281
282 let texformat, pbformat, pbtype;
283 if (noalpha) {
284 texformat = srgb ? TextureFormat.SRGB8 : TextureFormat.RGB8;
285 pbformat = PixelDataFormat.RGB;
286 pbtype = Filament.PixelDataType.UBYTE;
287 } else {
288 texformat = srgb ? TextureFormat.SRGB8_A8 : TextureFormat.RGBA8;
289 pbformat = PixelDataFormat.RGBA;
290 pbtype = Filament.PixelDataType.UBYTE;
291 }
292
293 const tex = Filament.Texture.Builder()
294 .width(decodedImage.width)
295 .height(decodedImage.height)
296 .levels(nomips ? 1 : 0xff)
297 .sampler(Sampler.SAMPLER_2D)
298 .format(texformat)
299 .build(engine);
300
301 const pixelbuffer = Filament.PixelBuffer(decodedImage.data.getBytes(), pbformat, pbtype);
302 tex.setImage(engine, 0, pixelbuffer);
303 if (!nomips) {
304 tex.generateMipmaps(engine);
305 }
306 return tex;
307 };
308
309 /// getSupportedFormats ::function:: Queries WebGL to check which compressed formats are supported.
310 /// ::retval:: object with boolean values and the following keys: s3tc, astc, etc
311 Filament.getSupportedFormats = function() {
312 if (Filament.supportedFormats) {
313 return Filament.supportedFormats;
314 }
315 const options = { majorVersion: 2, minorVersion: 0 };
316 let ctx = document.createElement('canvas').getContext('webgl2', options);
317 const result = {
318 s3tc: false,
319 astc: false,
320 etc: false,
321 }
322 let exts = ctx.getSupportedExtensions(), nexts = exts.length, i;
323 for (i = 0; i < nexts; i++) {
324 let ext = exts[i];
325 if (ext == "WEBGL_compressed_texture_s3tc") {
326 result.s3tc = true;
327 } else if (ext == "WEBGL_compressed_texture_astc") {
328 result.astc = true;
329 } else if (ext == "WEBGL_compressed_texture_etc") {
330 result.etc = true;
331 }
332 }
333 return Filament.supportedFormats = result;
334 }
335
336 /// getSupportedFormatSuffix ::function:: Generate a file suffix according to the texture format.
337 /// Consumes a string describing desired formats and produces a file suffix depending on
338 /// which (if any) of the formats are actually supported by the WebGL implementation. This is
339 /// useful for compressed textures. For example, some platforms accept ETC and others accept S3TC.
340 /// desiredFormats ::argument:: space-delimited string of desired formats
341 /// ::retval:: empty string if there is no intersection of supported and desired formats.
342 Filament.getSupportedFormatSuffix = function(desiredFormats) {
343 desiredFormats = desiredFormats.split(' ');
344 let exts = Filament.getSupportedFormats();
345 for (let key in exts) {
346 if (exts[key] && desiredFormats.includes(key)) {
347 return '_' + key;
348 }
349 }
350 return '';
351 }