| 1 |
/* |
| 2 |
* Copyright (C) 2018 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 |
// Private utility that converts an asset string or Uint8Array into a low-level buffer descriptor. |
| 18 |
// Note that the low-level buffer descriptor must be manually deleted. |
| 19 |
function getBufferDescriptor(buffer) { |
| 20 |
if ('string' == typeof buffer || buffer instanceof String) { |
| 21 |
buffer = Filament.assets[buffer]; |
| 22 |
} |
| 23 |
if (buffer.buffer instanceof ArrayBuffer) { |
| 24 |
buffer = Filament.Buffer(buffer); |
| 25 |
} |
| 26 |
return buffer; |
| 27 |
} |
| 28 |
|
| 29 |
Filament.vectorToArray = function(vector) { |
| 30 |
const result = []; |
| 31 |
for (let i = 0; i < vector.size(); i++) { |
| 32 |
result.push(vector.get(i)); |
| 33 |
} |
| 34 |
return result; |
| 35 |
}; |
| 36 |
|
| 37 |
Filament.shadowOptions = function(overrides) { |
| 38 |
const options = { |
| 39 |
mapSize: 1024, |
| 40 |
shadowCascades: 1, |
| 41 |
constantBias: 0.001, |
| 42 |
normalBias: 1.0, |
| 43 |
shadowFar: 0.0, |
| 44 |
shadowNearHint: 1.0, |
| 45 |
shadowFarHint: 100.0, |
| 46 |
stable: false, |
| 47 |
polygonOffsetConstant: 0.5, |
| 48 |
polygonOffsetSlope: 2.0, |
| 49 |
screenSpaceContactShadows: false, |
| 50 |
stepCount: 8, |
| 51 |
maxShadowDistance: 0.3 |
| 52 |
}; |
| 53 |
return Object.assign(options, overrides); |
| 54 |
}; |
| 55 |
|
| 56 |
Filament.loadClassExtensions = function() { |
| 57 |
|
| 58 |
/// Engine ::core class:: |
| 59 |
|
| 60 |
/// create ::static method:: Creates an Engine instance for the given canvas. |
| 61 |
/// canvas ::argument:: the canvas DOM element |
| 62 |
/// options ::argument:: optional WebGL 2.0 context configuration |
| 63 |
/// ::retval:: an instance of [Engine] |
| 64 |
Filament.Engine.create = function(canvas, options) { |
| 65 |
const defaults = { |
| 66 |
majorVersion: 2, |
| 67 |
minorVersion: 0, |
| 68 |
antialias: false, |
| 69 |
depth: true, |
| 70 |
alpha: false |
| 71 |
}; |
| 72 |
options = Object.assign(defaults, options); |
| 73 |
|
| 74 |
// Create the WebGL 2.0 context. |
| 75 |
const ctx = canvas.getContext("webgl2", options); |
| 76 |
|
| 77 |
// Enable all desired extensions by calling getExtension on each one. |
| 78 |
ctx.getExtension('WEBGL_compressed_texture_s3tc'); |
| 79 |
ctx.getExtension('WEBGL_compressed_texture_astc'); |
| 80 |
ctx.getExtension('WEBGL_compressed_texture_etc'); |
| 81 |
|
| 82 |
// These transient globals are used temporarily during Engine construction. |
| 83 |
window.filament_glOptions = options; |
| 84 |
window.filament_glContext = ctx; |
| 85 |
|
| 86 |
// Register the GL context with emscripten and create the Engine. |
| 87 |
const engine = Filament.Engine._create(); |
| 88 |
|
| 89 |
// Annotate the engine with the GL context to support multiple canvases. |
| 90 |
engine.context = window.filament_glContext; |
| 91 |
engine.handle = window.filament_contextHandle; |
| 92 |
|
| 93 |
// Ensure that we do not pollute the global namespace. |
| 94 |
delete window.filament_glOptions; |
| 95 |
delete window.filament_glContext; |
| 96 |
delete window.filament_contextHandle; |
| 97 |
|
| 98 |
return engine; |
| 99 |
}; |
| 100 |
|
| 101 |
Filament.Engine.prototype.execute = function() { |
| 102 |
window.filament_contextHandle = this.handle; |
| 103 |
this._execute(); |
| 104 |
delete window.filament_contextHandle; |
| 105 |
}; |
| 106 |
|
| 107 |
/// createMaterial ::method:: |
| 108 |
/// package ::argument:: asset string, or Uint8Array, or [Buffer] with filamat contents |
| 109 |
/// ::retval:: an instance of [createMaterial] |
| 110 |
Filament.Engine.prototype.createMaterial = function(buffer) { |
| 111 |
buffer = getBufferDescriptor(buffer); |
| 112 |
const result = this._createMaterial(buffer); |
| 113 |
buffer.delete(); |
| 114 |
return result; |
| 115 |
}; |
| 116 |
|
| 117 |
/// createTextureFromKtx ::method:: Utility function that creates a [Texture] from a KTX file. |
| 118 |
/// buffer ::argument:: asset string, or Uint8Array, or [Buffer] with KTX file contents |
| 119 |
/// options ::argument:: Options dictionary. |
| 120 |
/// ::retval:: [Texture] |
| 121 |
Filament.Engine.prototype.createTextureFromKtx = function(buffer, options) { |
| 122 |
buffer = getBufferDescriptor(buffer); |
| 123 |
const result = Filament._createTextureFromKtx(buffer, this, options); |
| 124 |
buffer.delete(); |
| 125 |
return result; |
| 126 |
}; |
| 127 |
|
| 128 |
/// createIblFromKtx ::method:: Utility that creates an [IndirectLight] from a KTX file. |
| 129 |
/// NOTE: To prevent a leak, please be sure to destroy the associated reflections texture. |
| 130 |
/// buffer ::argument:: asset string, or Uint8Array, or [Buffer] with KTX file contents |
| 131 |
/// options ::argument:: Options dictionary. |
| 132 |
/// ::retval:: [IndirectLight] |
| 133 |
Filament.Engine.prototype.createIblFromKtx = function(buffer, options) { |
| 134 |
buffer = getBufferDescriptor(buffer); |
| 135 |
const result = Filament._createIblFromKtx(buffer, this, options); |
| 136 |
buffer.delete(); |
| 137 |
return result; |
| 138 |
}; |
| 139 |
|
| 140 |
/// createSkyFromKtx ::method:: Utility function that creates a [Skybox] from a KTX file. |
| 141 |
/// NOTE: To prevent a leak, please be sure to destroy the associated texture. |
| 142 |
/// buffer ::argument:: asset string, or Uint8Array, or [Buffer] with KTX file contents |
| 143 |
/// options ::argument:: Options dictionary. |
| 144 |
/// ::retval:: [Skybox] |
| 145 |
Filament.Engine.prototype.createSkyFromKtx = function(buffer, options) { |
| 146 |
const skytex = this.createTextureFromKtx(buffer, options); |
| 147 |
return Filament.Skybox.Builder().environment(skytex).build(this); |
| 148 |
}; |
| 149 |
|
| 150 |
/// createTextureFromPng ::method:: Creates a 2D [Texture] from the raw contents of a PNG file. |
| 151 |
/// buffer ::argument:: asset string, or Uint8Array, or [Buffer] with PNG file contents |
| 152 |
/// options ::argument:: object with optional `srgb`, `noalpha`, and `nomips` keys. |
| 153 |
/// ::retval:: [Texture] |
| 154 |
Filament.Engine.prototype.createTextureFromPng = function(buffer, options) { |
| 155 |
buffer = getBufferDescriptor(buffer); |
| 156 |
const result = Filament._createTextureFromImageFile(buffer, this, options); |
| 157 |
buffer.delete(); |
| 158 |
return result; |
| 159 |
}; |
| 160 |
|
| 161 |
/// createTextureFromJpeg ::method:: Creates a 2D [Texture] from the contents of a JPEG file. |
| 162 |
/// buffer ::argument:: asset string, or Uint8Array, or [Buffer] with JPEG file contents |
| 163 |
/// options ::argument:: JavaScript object with optional `srgb` and `nomips` keys. |
| 164 |
/// ::retval:: [Texture] |
| 165 |
Filament.Engine.prototype.createTextureFromJpeg = function(buffer, options) { |
| 166 |
buffer = getBufferDescriptor(buffer); |
| 167 |
const result = Filament._createTextureFromImageFile(buffer, this, options); |
| 168 |
buffer.delete(); |
| 169 |
return result; |
| 170 |
}; |
| 171 |
|
| 172 |
/// loadFilamesh ::method:: Consumes the contents of a filamesh file and creates a renderable. |
| 173 |
/// buffer ::argument:: asset string, or Uint8Array, or [Buffer] with filamesh contents |
| 174 |
/// definstance ::argument:: Optional default [MaterialInstance] |
| 175 |
/// matinstances ::argument:: Optional in-out object that gets populated with a \ |
| 176 |
/// name-to-[MaterialInstance] mapping. Clients can also optionally provide individual \ |
| 177 |
/// material instances using this argument. |
| 178 |
/// ::retval:: JavaScript object with keys `renderable`, `vertexBuffer`, and `indexBuffer`. \ |
| 179 |
/// These are of type [Entity], [VertexBuffer], and [IndexBuffer]. |
| 180 |
Filament.Engine.prototype.loadFilamesh = function(buffer, definstance, matinstances) { |
| 181 |
buffer = getBufferDescriptor(buffer); |
| 182 |
const result = Filament._loadFilamesh(this, buffer, definstance, matinstances); |
| 183 |
buffer.delete(); |
| 184 |
return result; |
| 185 |
}; |
| 186 |
|
| 187 |
/// createAssetLoader ::method:: |
| 188 |
/// ::retval:: an instance of [AssetLoader] |
| 189 |
/// Clients should create only one asset loader for the lifetime of their app, this prevents |
| 190 |
/// memory leaks and duplication of Material objects. |
| 191 |
Filament.Engine.prototype.createAssetLoader = function() { |
| 192 |
const materials = new Filament.gltfio$UbershaderLoader(this); |
| 193 |
return new Filament.gltfio$AssetLoader(this, materials); |
| 194 |
}; |
| 195 |
|
| 196 |
/// addEntities ::method:: |
| 197 |
/// entities ::argument:: array of entities |
| 198 |
/// This method is equivalent to calling `addEntity` on each item in the array. |
| 199 |
Filament.Scene.prototype.addEntities = function(entities) { |
| 200 |
const vector = new Filament.EntityVector(); |
| 201 |
for (const entity of entities) { |
| 202 |
vector.push_back(entity); |
| 203 |
} |
| 204 |
this._addEntities(vector); |
| 205 |
}; |
| 206 |
|
| 207 |
/// removeEntities ::method:: |
| 208 |
/// entities ::argument:: array of entities |
| 209 |
/// This method is equivalent to calling `remove` on each item in the array. |
| 210 |
Filament.Scene.prototype.removeEntities = function(entities) { |
| 211 |
const vector = new Filament.EntityVector(); |
| 212 |
for (const entity of entities) { |
| 213 |
vector.push_back(entity); |
| 214 |
} |
| 215 |
this._removeEntities(vector); |
| 216 |
}; |
| 217 |
|
| 218 |
/// setShadowOptions ::method:: |
| 219 |
/// instance ::argument:: Instance of a light component obtained from `getInstance`. |
| 220 |
/// overrides ::argument:: Dictionary with one or more of the following properties: \ |
| 221 |
/// mapSize, shadowCascades, constantBias, normalBias, shadowFar, shadowNearHint, \ |
| 222 |
/// shadowFarHint, stable, polygonOffsetConstant, polygonOffsetSlope, \ |
| 223 |
// screenSpaceContactShadows, stepCount, maxShadowDistance. |
| 224 |
Filament.LightManager.prototype.setShadowOptions = function(instance, overrides) { |
| 225 |
this._setShadowOptions(instance, Filament.shadowOptions(overrides)); |
| 226 |
}; |
| 227 |
|
| 228 |
/// setClearOptions ::method:: |
| 229 |
/// overrides ::argument:: Dictionary with one or more of the following properties: \ |
| 230 |
/// clearColor, clear, discard. |
| 231 |
Filament.Renderer.prototype.setClearOptions = function(overrides) { |
| 232 |
const options = { |
| 233 |
clearColor: [0, 0, 0, 0], |
| 234 |
clear: false, |
| 235 |
discard: true |
| 236 |
}; |
| 237 |
Object.assign(options, overrides); |
| 238 |
this._setClearOptions(options); |
| 239 |
}; |
| 240 |
|
| 241 |
/// setAmbientOcclusionOptions ::method:: |
| 242 |
/// overrides ::argument:: Dictionary with one or more of the following properties: \ |
| 243 |
/// radius, power, bias, resolution, intensity, quality. |
| 244 |
Filament.View.prototype.setAmbientOcclusionOptions = function(overrides) { |
| 245 |
const options = { |
| 246 |
radius: 0.3, |
| 247 |
power: 1.0, |
| 248 |
bias: 0.0005, |
| 249 |
resolution: 0.5, |
| 250 |
intensity: 1.0, |
| 251 |
quality: Filament.View$QualityLevel.LOW |
| 252 |
}; |
| 253 |
Object.assign(options, overrides); |
| 254 |
this._setAmbientOcclusionOptions(options); |
| 255 |
}; |
| 256 |
|
| 257 |
/// setDepthOfFieldOptions ::method:: |
| 258 |
/// overrides ::argument:: Dictionary with one or more of the following properties: \ |
| 259 |
/// focusDistance, cocScale, maxApertureDiameter, enabled. |
| 260 |
Filament.View.prototype.setDepthOfFieldOptions = function(overrides) { |
| 261 |
const options = { |
| 262 |
focusDistance: 10.0, |
| 263 |
cocScale: 1.0, |
| 264 |
maxApertureDiameter: 0.01, |
| 265 |
enabled: false |
| 266 |
}; |
| 267 |
Object.assign(options, overrides); |
| 268 |
this._setDepthOfFieldOptions(options); |
| 269 |
}; |
| 270 |
|
| 271 |
/// setBloomOptions ::method:: |
| 272 |
/// overrides ::argument:: Dictionary with one or more of the following properties: \ |
| 273 |
/// enabled, strength, resolution, anomorphism, levels, blendMode, threshold, highlight. |
| 274 |
/// NOTE: dirt texture is not yet supported in the JavaScript API. |
| 275 |
Filament.View.prototype.setBloomOptions = function(overrides) { |
| 276 |
const options = { |
| 277 |
dirtStrength: 0.2, |
| 278 |
strength: 0.10, |
| 279 |
resolution: 360, |
| 280 |
anamorphism: 1.0, |
| 281 |
levels: 6, |
| 282 |
blendMode: Filament.View$BloomOptions$BlendMode.ADD, |
| 283 |
threshold: true, |
| 284 |
enabled: false, |
| 285 |
highlight: 1000.0, |
| 286 |
lensFlare: false, |
| 287 |
starburst: true, |
| 288 |
chromaticAberration: 0.005, |
| 289 |
ghostCount: 4, |
| 290 |
ghostSpacing: 0.6, |
| 291 |
ghostThreshold: 10.0, |
| 292 |
haloThickness: 0.1, |
| 293 |
haloRadius: 0.4, |
| 294 |
haloThreshold: 10.0, |
| 295 |
dirt: null |
| 296 |
}; |
| 297 |
Object.assign(options, overrides); |
| 298 |
this._setBloomOptions(options); |
| 299 |
}; |
| 300 |
|
| 301 |
/// setFogOptions ::method:: |
| 302 |
/// overrides ::argument:: Dictionary with one or more of the following properties: \ |
| 303 |
/// distance, maximumOpacity, height, heightFalloff, color, density, inScatteringStart, |
| 304 |
/// inScatteringSize, fogColorFromIbl, enabled. |
| 305 |
Filament.View.prototype.setFogOptions = function(overrides) { |
| 306 |
const options = { |
| 307 |
distance: 0.0, |
| 308 |
maximumOpacity: 1.0, |
| 309 |
height: 0.0, |
| 310 |
heightFalloff: 1.0, |
| 311 |
color: .5, |
| 312 |
density: 0.1, |
| 313 |
inScatteringStart: 0.0, |
| 314 |
inScatteringSize: -1.0, |
| 315 |
fogColorFromIbl: false, |
| 316 |
enabled: false |
| 317 |
}; |
| 318 |
Object.assign(options, overrides); |
| 319 |
this._setFogOptions(options); |
| 320 |
}; |
| 321 |
|
| 322 |
/// setVignetteOptions ::method:: |
| 323 |
/// overrides ::argument:: Dictionary with one or more of the following properties: \ |
| 324 |
/// midPoint, roundness, feather, color, enabled. |
| 325 |
Filament.View.prototype.setVignetteOptions = function(overrides) { |
| 326 |
const options = { |
| 327 |
midPoint: 0.5, |
| 328 |
roundness: 0.5, |
| 329 |
feather: 0.5, |
| 330 |
color: [0, 0, 0, 1], |
| 331 |
enabled: false |
| 332 |
}; |
| 333 |
Object.assign(options, overrides); |
| 334 |
this._setVignetteOptions(options); |
| 335 |
}; |
| 336 |
|
| 337 |
/// BufferObject ::core class:: |
| 338 |
|
| 339 |
/// setBuffer ::method:: |
| 340 |
/// engine ::argument:: [Engine] |
| 341 |
/// buffer ::argument:: asset string, or Uint8Array, or [Buffer] |
| 342 |
/// byteOffset ::argument:: non-negative integer |
| 343 |
Filament.BufferObject.prototype.setBuffer = function(engine, buffer, byteOffset = 0) { |
| 344 |
buffer = getBufferDescriptor(buffer); |
| 345 |
this._setBuffer(engine, buffer, byteOffset); |
| 346 |
buffer.delete(); |
| 347 |
}; |
| 348 |
|
| 349 |
/// VertexBuffer ::core class:: |
| 350 |
|
| 351 |
/// setBufferAt ::method:: |
| 352 |
/// engine ::argument:: [Engine] |
| 353 |
/// bufferIndex ::argument:: non-negative integer |
| 354 |
/// buffer ::argument:: asset string, or Uint8Array, or [Buffer] |
| 355 |
/// byteOffset ::argument:: non-negative integer |
| 356 |
Filament.VertexBuffer.prototype.setBufferAt = function(engine, bufferIndex, buffer, byteOffset = 0) { |
| 357 |
buffer = getBufferDescriptor(buffer); |
| 358 |
this._setBufferAt(engine, bufferIndex, buffer, byteOffset); |
| 359 |
buffer.delete(); |
| 360 |
}; |
| 361 |
|
| 362 |
/// IndexBuffer ::core class:: |
| 363 |
|
| 364 |
/// setBuffer ::method:: |
| 365 |
/// engine ::argument:: [Engine] |
| 366 |
/// buffer ::argument:: asset string, or Uint8Array, or [Buffer] |
| 367 |
/// byteOffset ::argument:: non-negative integer |
| 368 |
Filament.IndexBuffer.prototype.setBuffer = function(engine, buffer, byteOffset = 0) { |
| 369 |
buffer = getBufferDescriptor(buffer); |
| 370 |
this._setBuffer(engine, buffer, byteOffset); |
| 371 |
buffer.delete(); |
| 372 |
}; |
| 373 |
|
| 374 |
Filament.LightManager$Builder.prototype.shadowOptions = function(overrides) { |
| 375 |
return this._shadowOptions(Filament.shadowOptions(overrides)); |
| 376 |
}; |
| 377 |
|
| 378 |
Filament.RenderableManager$Builder.prototype.build = |
| 379 |
Filament.LightManager$Builder.prototype.build = |
| 380 |
function(engine, entity) { |
| 381 |
const result = this._build(engine, entity); |
| 382 |
this.delete(); |
| 383 |
return result; |
| 384 |
}; |
| 385 |
|
| 386 |
Filament.ColorGrading$Builder.prototype.build = |
| 387 |
Filament.RenderTarget$Builder.prototype.build = |
| 388 |
Filament.VertexBuffer$Builder.prototype.build = |
| 389 |
Filament.IndexBuffer$Builder.prototype.build = |
| 390 |
Filament.Texture$Builder.prototype.build = |
| 391 |
Filament.IndirectLight$Builder.prototype.build = |
| 392 |
Filament.Skybox$Builder.prototype.build = |
| 393 |
function(engine) { |
| 394 |
const result = this._build(engine); |
| 395 |
this.delete(); |
| 396 |
return result; |
| 397 |
}; |
| 398 |
|
| 399 |
Filament.KtxBundle.prototype.getBlob = function(index) { |
| 400 |
const blob = this._getBlob(index); |
| 401 |
const result = blob.getBytes(); |
| 402 |
blob.delete(); |
| 403 |
return result; |
| 404 |
} |
| 405 |
|
| 406 |
Filament.KtxBundle.prototype.getCubeBlob = function(miplevel) { |
| 407 |
const blob = this._getCubeBlob(miplevel); |
| 408 |
const result = blob.getBytes(); |
| 409 |
blob.delete(); |
| 410 |
return result; |
| 411 |
} |
| 412 |
|
| 413 |
Filament.Texture.prototype.setImage = function(engine, level, pbd) { |
| 414 |
this._setImage(engine, level, pbd); |
| 415 |
pbd.delete(); |
| 416 |
} |
| 417 |
|
| 418 |
Filament.Texture.prototype.setImageCube = function(engine, level, pbd) { |
| 419 |
this._setImageCube(engine, level, pbd); |
| 420 |
pbd.delete(); |
| 421 |
} |
| 422 |
|
| 423 |
Filament.SurfaceOrientation$Builder.prototype.normals = function(buffer, stride = 0) { |
| 424 |
buffer = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength); |
| 425 |
this.norPointer = Filament._malloc(buffer.byteLength); |
| 426 |
Filament.HEAPU8.set(buffer, this.norPointer); |
| 427 |
this._normals(this.norPointer, stride); |
| 428 |
}; |
| 429 |
|
| 430 |
Filament.SurfaceOrientation$Builder.prototype.uvs = function(buffer, stride = 0) { |
| 431 |
buffer = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength); |
| 432 |
this.uvsPointer = Filament._malloc(buffer.byteLength); |
| 433 |
Filament.HEAPU8.set(buffer, this.uvsPointer); |
| 434 |
this._uvs(this.uvsPointer, stride); |
| 435 |
}; |
| 436 |
|
| 437 |
Filament.SurfaceOrientation$Builder.prototype.positions = function(buffer, stride = 0) { |
| 438 |
buffer = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength); |
| 439 |
this.posPointer = Filament._malloc(buffer.byteLength); |
| 440 |
Filament.HEAPU8.set(buffer, this.posPointer); |
| 441 |
this._positions(this.posPointer, stride); |
| 442 |
}; |
| 443 |
|
| 444 |
Filament.SurfaceOrientation$Builder.prototype.triangles16 = function(buffer, stride = 0) { |
| 445 |
buffer = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength); |
| 446 |
this.t16Pointer = Filament._malloc(buffer.byteLength); |
| 447 |
Filament.HEAPU8.set(buffer, this.t16Pointer); |
| 448 |
this._triangles16(this.t16Pointer, stride); |
| 449 |
}; |
| 450 |
|
| 451 |
Filament.SurfaceOrientation$Builder.prototype.triangles32 = function(buffer, stride = 0) { |
| 452 |
buffer = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength); |
| 453 |
this.t32Pointer = Filament._malloc(buffer.byteLength); |
| 454 |
Filament.HEAPU8.set(buffer, this.t32Pointer); |
| 455 |
this._triangles32(this.t32Pointer, stride); |
| 456 |
}; |
| 457 |
|
| 458 |
Filament.SurfaceOrientation$Builder.prototype.build = function() { |
| 459 |
const result = this._build(); |
| 460 |
this.delete(); |
| 461 |
if ('norPointer' in this) Filament._free(this.norPointer); |
| 462 |
if ('uvsPointer' in this) Filament._free(this.uvsPointer); |
| 463 |
if ('posPointer' in this) Filament._free(this.posPointer); |
| 464 |
if ('t16Pointer' in this) Filament._free(this.t16Pointer); |
| 465 |
if ('t32Pointer' in this) Filament._free(this.t32Pointer); |
| 466 |
return result; |
| 467 |
}; |
| 468 |
|
| 469 |
Filament.SurfaceOrientation.prototype.getQuats = function(nverts) { |
| 470 |
const attribType = Filament.VertexBuffer$AttributeType.SHORT4; |
| 471 |
const quatsBufferSize = 8 * nverts; |
| 472 |
const quatsBuffer = Filament._malloc(quatsBufferSize); |
| 473 |
this._getQuats(quatsBuffer, nverts, attribType); |
| 474 |
const arrayBuffer = Filament.HEAPU8.subarray(quatsBuffer, quatsBuffer + quatsBufferSize).slice().buffer; |
| 475 |
Filament._free(quatsBuffer); |
| 476 |
return new Int16Array(arrayBuffer); |
| 477 |
}; |
| 478 |
|
| 479 |
Filament.gltfio$AssetLoader.prototype.createAssetFromJson = function(buffer) { |
| 480 |
if ('string' == typeof buffer && buffer.endsWith('.glb')) { |
| 481 |
console.error('Please use createAssetFromBinary for glb files.'); |
| 482 |
} |
| 483 |
buffer = getBufferDescriptor(buffer); |
| 484 |
const result = this._createAssetFromJson(buffer); |
| 485 |
buffer.delete(); |
| 486 |
return result; |
| 487 |
}; |
| 488 |
|
| 489 |
Filament.gltfio$AssetLoader.prototype.createAssetFromBinary = function(buffer) { |
| 490 |
if ('string' == typeof buffer && buffer.endsWith('.gltf')) { |
| 491 |
console.error('Please use createAssetFromJson for gltf files.'); |
| 492 |
} |
| 493 |
buffer = getBufferDescriptor(buffer); |
| 494 |
const result = this._createAssetFromBinary(buffer); |
| 495 |
buffer.delete(); |
| 496 |
return result; |
| 497 |
}; |
| 498 |
|
| 499 |
Filament.gltfio$AssetLoader.prototype.createInstancedAsset = function(buffer, instances) { |
| 500 |
buffer = getBufferDescriptor(buffer); |
| 501 |
const asset = this._createInstancedAsset(buffer, instances.length); |
| 502 |
buffer.delete(); |
| 503 |
const instancesVector = asset._getAssetInstances(); |
| 504 |
for (let i = 0; i < instancesVector.size(); i++) { |
| 505 |
instances[i] = instancesVector.get(i); |
| 506 |
} |
| 507 |
return asset; |
| 508 |
}; |
| 509 |
|
| 510 |
// See the C++ documentation for ResourceLoader and AssetLoader. The JavaScript API differs in |
| 511 |
// that it takes two optional callbacks: |
| 512 |
// |
| 513 |
// - onDone is called after all resources have been downloaded and decoded. |
| 514 |
// - onFetched is called after each resource has finished downloading. |
| 515 |
// |
| 516 |
// Takes an optional base path for resolving the URI strings in the glTF file, which is |
| 517 |
// typically the path to the parent glTF file. The given base path cannot itself be a relative |
| 518 |
// URL, but clients can do the following to resolve a relative URL: |
| 519 |
// const basePath = '' + new URL(myRelativeUrl, document.location); |
| 520 |
// If the given base path is null, document.location is used as the base. |
| 521 |
// |
| 522 |
// The optional asyncInterval argument allows clients to control how decoding is amortized |
| 523 |
// over time. It represents the number of milliseconds between each texture decoding task. |
| 524 |
// |
| 525 |
// The optional config argument is an object with boolean fields `normalizeSkinningWeights` and |
| 526 |
// `recomputeBoundingBoxes`. |
| 527 |
Filament.gltfio$FilamentAsset.prototype.loadResources = function(onDone, onFetched, basePath, |
| 528 |
asyncInterval, config) { |
| 529 |
const asset = this; |
| 530 |
const engine = this.getEngine(); |
| 531 |
const names = this.getResourceUris(); |
| 532 |
const interval = asyncInterval || 30; |
| 533 |
const defaults = { |
| 534 |
normalizeSkinningWeights: true, |
| 535 |
recomputeBoundingBoxes: false |
| 536 |
}; |
| 537 |
config = Object.assign(defaults, config || {}); |
| 538 |
|
| 539 |
basePath = basePath || document.location; |
| 540 |
onFetched = onFetched || ((name) => {}); |
| 541 |
onDone = onDone || (() => {}); |
| 542 |
|
| 543 |
// Construct the set of URI strings to fetch. |
| 544 |
const urlset = new Set(); |
| 545 |
const urlToName = {}; |
| 546 |
for (let i = 0; i < names.size(); i++) { |
| 547 |
const name = names.get(i); |
| 548 |
if (name) { |
| 549 |
const url = '' + new URL(name, basePath); |
| 550 |
urlToName[url] = name; |
| 551 |
urlset.add(url); |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
// Construct a resource loader and start decoding after all textures are fetched. |
| 556 |
const resourceLoader = new Filament.gltfio$ResourceLoader(engine, |
| 557 |
config.normalizeSkinningWeights, |
| 558 |
config.recomputeBoundingBoxes); |
| 559 |
const onComplete = () => { |
| 560 |
resourceLoader.asyncBeginLoad(asset); |
| 561 |
|
| 562 |
// NOTE: This decodes in the wasm layer instead of using Canvas2D, which allows Filament |
| 563 |
// to have more control (handling of alpha, srgb, etc) and improves parity with native |
| 564 |
// platforms. In the future we may wish to offload this to web workers. |
| 565 |
|
| 566 |
// Decode a single PNG or JPG every 30 milliseconds, or at the specified interval. |
| 567 |
const timer = setInterval(() => { |
| 568 |
resourceLoader.asyncUpdateLoad(); |
| 569 |
const progress = resourceLoader.asyncGetLoadProgress(); |
| 570 |
if (progress >= 1) { |
| 571 |
clearInterval(timer); |
| 572 |
resourceLoader.delete(); |
| 573 |
onDone(); |
| 574 |
} |
| 575 |
}, interval); |
| 576 |
}; |
| 577 |
|
| 578 |
if (urlset.size == 0) { |
| 579 |
onComplete(); |
| 580 |
return; |
| 581 |
} |
| 582 |
|
| 583 |
// Begin downloading all external resources. |
| 584 |
Filament.fetch(Array.from(urlset), onComplete, function(url) { |
| 585 |
const buffer = getBufferDescriptor(url); |
| 586 |
const name = urlToName[url]; |
| 587 |
resourceLoader.addResourceData(name, buffer); |
| 588 |
buffer.delete(); |
| 589 |
onFetched(name); |
| 590 |
}); |
| 591 |
}; |
| 592 |
|
| 593 |
Filament.gltfio$FilamentAsset.prototype.getEntities = function() { |
| 594 |
return Filament.vectorToArray(this._getEntities()); |
| 595 |
}; |
| 596 |
|
| 597 |
Filament.gltfio$FilamentAsset.prototype.getEntitiesByName = function(name) { |
| 598 |
return Filament.vectorToArray(this._getEntitiesByName(name)); |
| 599 |
}; |
| 600 |
|
| 601 |
Filament.gltfio$FilamentAsset.prototype.getEntitiesByPrefix = function(prefix) { |
| 602 |
return Filament.vectorToArray(this._getEntitiesByPrefix(prefix)); |
| 603 |
}; |
| 604 |
|
| 605 |
Filament.gltfio$FilamentAsset.prototype.getLightEntities = function() { |
| 606 |
return Filament.vectorToArray(this._getLightEntities()); |
| 607 |
}; |
| 608 |
|
| 609 |
Filament.gltfio$FilamentAsset.prototype.getCameraEntities = function() { |
| 610 |
return Filament.vectorToArray(this._getCameraEntities()); |
| 611 |
}; |
| 612 |
}; |