| 1 |
/* |
| 2 |
* Copyright (C) 2021 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 |
// If you are bundling this with rollup, webpack, or esbuild, the following URL should be trimmed. |
| 18 |
import { LitElement, html, css } from "https://unpkg.com/lit-element?module"; |
| 19 |
|
| 20 |
// To allow the DOM to render before the Filament WASM module is ready, we maintain a little |
| 21 |
// queue of tasks that get invoked as soon as the module is done loading. |
| 22 |
class FilamentTasks { |
| 23 |
constructor() { |
| 24 |
this.tasks = [] |
| 25 |
if (!Filament.Engine) { |
| 26 |
Filament.init([], () => { for (const task of this.tasks) task(); }); |
| 27 |
} |
| 28 |
} |
| 29 |
add(callback) { |
| 30 |
if (!Filament.Engine) { |
| 31 |
this.tasks.push(callback); |
| 32 |
} else { |
| 33 |
callback(); |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
// FilamentViewer is a fairly limited web component for showing glTF models with Filament. |
| 39 |
// |
| 40 |
// To embed a 3D viewer in your web page, simply add something like the following to your HTML, |
| 41 |
// similar to an <img> element. |
| 42 |
// |
| 43 |
// <filament-viewer src="FlightHelmet.gltf" /> |
| 44 |
// |
| 45 |
// In addition to the src URL, attributes can be used to set up an optional IBL and skybox. |
| 46 |
// The documentation for these attributes is in the FilamentViewer constructor. |
| 47 |
// |
| 48 |
// MISSING FEATURES |
| 49 |
// ---------------- |
| 50 |
// None of the following features are implemented. They would be easy to add. |
| 51 |
// - Replace gltumble and Trackball with camutils Manipulator (i.e. enable scroll-to-zoom) |
| 52 |
// - Write a documentation page (might be neat if the doc page has instances of the actual viewer) |
| 53 |
// - Fix the import at the top of the file to support webpack / rollup / esbuild |
| 54 |
// - Expose more animation properties (e.g. enable / disable, selected index) |
| 55 |
// - Expose camera properties, glTF camera selection, and clear color |
| 56 |
// - Expose more IBL properties |
| 57 |
// - Expose directional light properties |
| 58 |
// - Optional turntable animation |
| 59 |
// |
| 60 |
class FilamentViewer extends LitElement { |
| 61 |
constructor() { |
| 62 |
super(); |
| 63 |
|
| 64 |
// LitElement properties: |
| 65 |
this.src = null; // Path to glTF file. |
| 66 |
this.alt = null; // Alternate canvas content. |
| 67 |
this.ibl = null; // Path to image based light ktx. |
| 68 |
this.sky = null; // Path to skybox ktx. |
| 69 |
this.enableDrop = null; // Enables drag and drop. |
| 70 |
this.intensity = 30000; // Intensity of the image based light. |
| 71 |
|
| 72 |
// Private properties: |
| 73 |
this.filamentTasks = new FilamentTasks(); |
| 74 |
this.canvasId = "filament-viewer-canvas"; |
| 75 |
this.overlayId = "filament-viewer-overlay"; |
| 76 |
this.srcBlob = null; |
| 77 |
} |
| 78 |
|
| 79 |
static get properties() { |
| 80 |
return { |
| 81 |
src: { type: String }, |
| 82 |
alt: { type: String }, |
| 83 |
ibl: { type: String }, |
| 84 |
sky: { type: String }, |
| 85 |
enableDrop: { type: Boolean }, |
| 86 |
intensity: { type: Number }, |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
firstUpdated() { |
| 91 |
// At this point in the lit-element lifecycle, the "render" has taken place, which simply |
| 92 |
// means the canvas element now exists. However the Filament wasm module may or may not be |
| 93 |
// fully loaded, which is why we use the task manager. |
| 94 |
const canvas = this.shadowRoot.getElementById(this.canvasId); |
| 95 |
if (canvas.parentNode.host.parentElement.tagName === "FILAMENT-VIEWER") { |
| 96 |
console.error("Do not nest FilamentViewer, this is unsupported."); |
| 97 |
console.error("Try placing each viewer in a wrapper element."); |
| 98 |
return; |
| 99 |
} |
| 100 |
this.filamentTasks.add(this._startFilament.bind(this)); |
| 101 |
|
| 102 |
const overlay = this.shadowRoot.getElementById(this.overlayId); |
| 103 |
["dragenter", "dragover", "dragleave", "drop"].forEach(eventName => { |
| 104 |
overlay.addEventListener(eventName, e => { e.preventDefault(); e.stopPropagation() }, false) |
| 105 |
}); |
| 106 |
if (this.enableDrop) { |
| 107 |
overlay.addEventListener("drop", this._dropHandler.bind(this), false); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
updated(props) { |
| 112 |
if (props.has("src")) this.filamentTasks.add(this._loadAsset.bind(this)); |
| 113 |
if (props.has("ibl")) this.filamentTasks.add(this._loadIbl.bind(this)); |
| 114 |
if (props.has("sky")) this.filamentTasks.add(this._loadSky.bind(this)); |
| 115 |
if (props.has("enableDrop")) this._updateOverlay(); |
| 116 |
if (props.has("intensity") && this.indirectLight) { |
| 117 |
this.indirectLight.setIntensity(this.intensity); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
static get styles() { |
| 122 |
return css` |
| 123 |
:host { |
| 124 |
display: inline-block; |
| 125 |
width: 300px; |
| 126 |
height: 300px; |
| 127 |
border: solid 1px black; |
| 128 |
position: relative; |
| 129 |
} |
| 130 |
canvas { |
| 131 |
background: #D9D9D9; /* This is consistent with the default clear color. */ |
| 132 |
} |
| 133 |
canvas, .overlay { |
| 134 |
width: 100%; |
| 135 |
height: 100%; |
| 136 |
position: absolute; |
| 137 |
} |
| 138 |
.overlay { |
| 139 |
text-align: center; |
| 140 |
padding: 10px; |
| 141 |
}`; |
| 142 |
} |
| 143 |
|
| 144 |
render() { |
| 145 |
return html` |
| 146 |
<canvas part="canvas" alt="${this.alt}" id="${this.canvasId}"></canvas> |
| 147 |
<div class="overlay" part="overlay" id="${this.overlayId}"></div> |
| 148 |
`; |
| 149 |
} |
| 150 |
|
| 151 |
_dropHandler(dragEvent) { |
| 152 |
if (!dragEvent.dataTransfer) return; |
| 153 |
this.srcBlob = null; |
| 154 |
this.srcBlobResources = {}; |
| 155 |
for (const file of dragEvent.dataTransfer.files) { |
| 156 |
if (file.name.endsWith(".glb") || file.name.endsWith(".gltf")) { |
| 157 |
this.srcBlob = file; |
| 158 |
} else { |
| 159 |
this.srcBlobResources[file.name] = file; |
| 160 |
} |
| 161 |
} |
| 162 |
if (this.srcBlob) { |
| 163 |
this._loadAsset(); |
| 164 |
} else { |
| 165 |
console.error("Please include a glTF file."); |
| 166 |
} |
| 167 |
}; |
| 168 |
|
| 169 |
_updateOverlay() { |
| 170 |
const overlay = this.shadowRoot.getElementById(this.overlayId); |
| 171 |
if (!this.enableDrop || this.asset) { |
| 172 |
overlay.innerHTML = ""; |
| 173 |
} else { |
| 174 |
overlay.innerHTML = "Drop glb or file set here."; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
_startFilament() { |
| 179 |
const LightType = Filament.LightManager$Type; |
| 180 |
|
| 181 |
const canvas = this.shadowRoot.getElementById(this.canvasId); |
| 182 |
const overlay = this.shadowRoot.getElementById(this.overlayId); |
| 183 |
|
| 184 |
this.engine = Filament.Engine.create(canvas); |
| 185 |
this.scene = this.engine.createScene(); |
| 186 |
this.sunlight = Filament.EntityManager.get().create(); |
| 187 |
this.scene.addEntity(this.sunlight); |
| 188 |
this.loader = this.engine.createAssetLoader(); |
| 189 |
this.cameraEntity = Filament.EntityManager.get().create(); |
| 190 |
this.camera = this.engine.createCamera(this.cameraEntity); |
| 191 |
this.swapChain = this.engine.createSwapChain(); |
| 192 |
this.renderer = this.engine.createRenderer(); |
| 193 |
this.view = this.engine.createView(); |
| 194 |
this.view.setVignetteOptions({ midPoint: 0.7, enabled: true }); |
| 195 |
this.view.setCamera(this.camera); |
| 196 |
this.view.setScene(this.scene); |
| 197 |
|
| 198 |
// If gltumble has been loaded, use it. |
| 199 |
if (window.Trackball) { |
| 200 |
this.trackball = new Trackball(overlay, { startSpin: 0.0 }); |
| 201 |
} |
| 202 |
|
| 203 |
// This color is consistent with the default CSS background color. |
| 204 |
this.renderer.setClearOptions({ clearColor: [0.8, 0.8, 0.8, 1.0], clear: true }); |
| 205 |
|
| 206 |
Filament.LightManager.Builder(LightType.SUN) |
| 207 |
.direction([0, -.7, -.7]) |
| 208 |
.sunAngularRadius(1.9) |
| 209 |
.castShadows(true) |
| 210 |
.sunHaloSize(10.0) |
| 211 |
.sunHaloFalloff(80.0) |
| 212 |
.build(this.engine, this.sunlight); |
| 213 |
|
| 214 |
// TODO: if ResizeObserver is not supported, then we should call _onResized and |
| 215 |
// pass (canvas.clientWidth * window.devicePixelRatio) for the dimensions. |
| 216 |
var ro = new ResizeObserver(entries => { |
| 217 |
const width = entries[0].devicePixelContentBoxSize[0].inlineSize; |
| 218 |
const height = entries[0].devicePixelContentBoxSize[0].blockSize; |
| 219 |
this._onResized(width, height); |
| 220 |
}); |
| 221 |
ro.observe(canvas); |
| 222 |
|
| 223 |
window.requestAnimationFrame(this._renderFrame.bind(this)); |
| 224 |
} |
| 225 |
|
| 226 |
_onResized(width, height) { |
| 227 |
const Fov = Filament.Camera$Fov; |
| 228 |
const canvas = this.shadowRoot.getElementById(this.canvasId); |
| 229 |
canvas.width = width; |
| 230 |
canvas.height = height; |
| 231 |
this.view.setViewport([0, 0, width, height]); |
| 232 |
const y = -0.125, eye = [0, y, 1.5], center = [0, y, 0], up = [0, 1, 0]; |
| 233 |
this.camera.lookAt(eye, center, up); |
| 234 |
const aspect = width / height; |
| 235 |
const fov = aspect < 1 ? Fov.HORIZONTAL : Fov.VERTICAL; |
| 236 |
this.camera.setProjectionFov(25, aspect, 1.0, 10.0, fov); |
| 237 |
} |
| 238 |
|
| 239 |
_loadIbl() { |
| 240 |
if (!this.ibl) { |
| 241 |
return; |
| 242 |
} |
| 243 |
if (this.indirectLight) { |
| 244 |
console.info("FilamentViewer does not allow the IBL to be changed."); |
| 245 |
return; |
| 246 |
} |
| 247 |
fetch(this.ibl).then(response => { |
| 248 |
return response.arrayBuffer(); |
| 249 |
}).then(arrayBuffer => { |
| 250 |
const ktxData = new Uint8Array(arrayBuffer); |
| 251 |
this.indirectLight = this.engine.createIblFromKtx(ktxData); |
| 252 |
this.indirectLight.setIntensity(this.intensity); |
| 253 |
this.scene.setIndirectLight(this.indirectLight); |
| 254 |
}); |
| 255 |
} |
| 256 |
|
| 257 |
_loadSky() { |
| 258 |
if (!this.sky) { |
| 259 |
return; |
| 260 |
} |
| 261 |
if (this.skybox) { |
| 262 |
console.info("FilamentViewer does not allow the skybox to be changed."); |
| 263 |
return; |
| 264 |
} |
| 265 |
fetch(this.sky).then(response => { |
| 266 |
return response.arrayBuffer(); |
| 267 |
}).then(arrayBuffer => { |
| 268 |
const ktxData = new Uint8Array(arrayBuffer); |
| 269 |
this.skybox = this.engine.createSkyFromKtx(ktxData); |
| 270 |
this.scene.setSkybox(this.skybox); |
| 271 |
}); |
| 272 |
} |
| 273 |
|
| 274 |
_loadAsset() { |
| 275 |
const zoffset = 4; |
| 276 |
|
| 277 |
if (this.asset) { |
| 278 |
this.scene.removeEntities(this.asset.getEntities()); |
| 279 |
this.animator = null; |
| 280 |
this.asset = null; |
| 281 |
} |
| 282 |
|
| 283 |
// If we have neither a URL nor a dropped file, leave early. |
| 284 |
if (!this.src && !this.srcBlob) { |
| 285 |
this._updateOverlay(); |
| 286 |
return; |
| 287 |
} |
| 288 |
|
| 289 |
// Dropping a glb file is simple because there are no external resources. |
| 290 |
if (this.srcBlob && this.srcBlob.name.endsWith(".glb")) { |
| 291 |
this.srcBlob.arrayBuffer().then(buffer => { |
| 292 |
this.asset = this.loader.createAssetFromBinary(new Uint8Array(buffer)); |
| 293 |
const aabb = this.asset.getBoundingBox(); |
| 294 |
this.assetRoot = this.asset.getRoot(); |
| 295 |
this.unitCubeTransform = Filament.fitIntoUnitCube(aabb, zoffset); |
| 296 |
this.asset.loadResources(); |
| 297 |
this.animator = this.asset.getAnimator(); |
| 298 |
this.animationStartTime = Date.now(); |
| 299 |
this._updateOverlay(); |
| 300 |
}); |
| 301 |
return; |
| 302 |
} |
| 303 |
|
| 304 |
// Dropping a fileset requires pushing each resource to ResourceLoader. |
| 305 |
if (this.srcBlob && this.srcBlob.name.endsWith(".gltf")) { |
| 306 |
|
| 307 |
const config = { |
| 308 |
normalizeSkinningWeights: true, |
| 309 |
recomputeBoundingBoxes: false, |
| 310 |
asyncInterval: 30 |
| 311 |
}; |
| 312 |
|
| 313 |
const doneAddingResources = resourceLoader => { |
| 314 |
this.srcBlobResources = {}; |
| 315 |
resourceLoader.asyncBeginLoad(this.asset); |
| 316 |
const timer = setInterval(() => { |
| 317 |
resourceLoader.asyncUpdateLoad(); |
| 318 |
const progress = resourceLoader.asyncGetLoadProgress(); |
| 319 |
if (progress >= 1) { |
| 320 |
clearInterval(timer); |
| 321 |
resourceLoader.delete(); |
| 322 |
this.animator = this.asset.getAnimator(); |
| 323 |
this.animationStartTime = Date.now(); |
| 324 |
} |
| 325 |
}, config.asyncInterval); |
| 326 |
}; |
| 327 |
|
| 328 |
this.srcBlob.arrayBuffer().then(buffer => { |
| 329 |
this.asset = this.loader.createAssetFromJson(new Uint8Array(buffer)); |
| 330 |
const aabb = this.asset.getBoundingBox(); |
| 331 |
this.assetRoot = this.asset.getRoot(); |
| 332 |
this.unitCubeTransform = Filament.fitIntoUnitCube(aabb, zoffset); |
| 333 |
|
| 334 |
const resourceLoader = new Filament.gltfio$ResourceLoader(this.engine, |
| 335 |
config.normalizeSkinningWeights, |
| 336 |
config.recomputeBoundingBoxes); |
| 337 |
|
| 338 |
let remaining = Object.keys(this.srcBlobResources).length; |
| 339 |
for (const name in this.srcBlobResources) { |
| 340 |
this.srcBlobResources[name].arrayBuffer().then(buffer => { |
| 341 |
const desc = getBufferDescriptor(new Uint8Array(buffer)); |
| 342 |
resourceLoader.addResourceData(name, getBufferDescriptor(desc)); |
| 343 |
if (--remaining === 0) { |
| 344 |
doneAddingResources(resourceLoader); |
| 345 |
} |
| 346 |
}); |
| 347 |
} |
| 348 |
|
| 349 |
this._updateOverlay(); |
| 350 |
}); |
| 351 |
|
| 352 |
return; |
| 353 |
} |
| 354 |
|
| 355 |
// If we reach this point, we are loading from a src URL rather than drag-and-drop. |
| 356 |
|
| 357 |
fetch(this.src).then(response => { |
| 358 |
return response.arrayBuffer(); |
| 359 |
}).then(arrayBuffer => { |
| 360 |
const modelData = new Uint8Array(arrayBuffer); |
| 361 |
if (this.src.endsWith(".glb")) { |
| 362 |
this.asset = this.loader.createAssetFromBinary(modelData); |
| 363 |
} else { |
| 364 |
this.asset = this.loader.createAssetFromJson(modelData); |
| 365 |
} |
| 366 |
|
| 367 |
const aabb = this.asset.getBoundingBox(); |
| 368 |
this.assetRoot = this.asset.getRoot(); |
| 369 |
this.unitCubeTransform = Filament.fitIntoUnitCube(aabb, zoffset); |
| 370 |
|
| 371 |
const basePath = '' + new URL(this.src, document.location); |
| 372 |
|
| 373 |
this.asset.loadResources(() => { |
| 374 |
this.animator = this.asset.getAnimator(); |
| 375 |
this.animationStartTime = Date.now(); |
| 376 |
}, null, basePath); |
| 377 |
|
| 378 |
this._updateOverlay(); |
| 379 |
}); |
| 380 |
} |
| 381 |
|
| 382 |
_updateAsset() { |
| 383 |
// Invoke the first glTF animation if it exists. |
| 384 |
if (this.animator && this.animator.getAnimationCount() > 0) { |
| 385 |
const ms = Date.now() - this.animationStartTime; |
| 386 |
this.animator.applyAnimation(0, ms / 1000); |
| 387 |
this.animator.updateBoneMatrices(); |
| 388 |
} |
| 389 |
|
| 390 |
// Apply the root transform of the model. |
| 391 |
const tcm = this.engine.getTransformManager(); |
| 392 |
const inst = tcm.getInstance(this.assetRoot); |
| 393 |
let rootTransform = this.unitCubeTransform; |
| 394 |
if (this.trackball) { |
| 395 |
rootTransform = Filament.multiplyMatrices(rootTransform, this.trackball.getMatrix()); |
| 396 |
} |
| 397 |
tcm.setTransform(inst, rootTransform); |
| 398 |
inst.delete(); |
| 399 |
|
| 400 |
// Add renderable entities to the scene as they become ready. |
| 401 |
while (true) { |
| 402 |
const entity = this.asset.popRenderable(); |
| 403 |
if (entity.getId() == 0) { |
| 404 |
entity.delete(); |
| 405 |
break; |
| 406 |
} |
| 407 |
this.scene.addEntity(entity); |
| 408 |
entity.delete(); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
_renderFrame() { |
| 413 |
// Apply transforms and add entities to the scene. |
| 414 |
if (this.asset) { |
| 415 |
this._updateAsset(); |
| 416 |
} |
| 417 |
|
| 418 |
// Render the view and update the Filament engine. |
| 419 |
if (this.renderer.beginFrame(this.swapChain)) { |
| 420 |
this.renderer.renderView(this.view); |
| 421 |
this.renderer.endFrame(); |
| 422 |
} |
| 423 |
this.engine.execute(); |
| 424 |
|
| 425 |
window.requestAnimationFrame(this._renderFrame.bind(this)); |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
customElements.define("filament-viewer", FilamentViewer); |