Share
Embed
WebGL Libs for WebApp Frameworks
all demos can be found here:
https://www.dropbox.com/sh/5l4nt9q6w6f7x8g/AABN93DZm2ryN49m8890mj9ka?dl=0
yomotsu
September 24, 2017
Transcript
67 slides
-
Presented by Akihiro Oyamada (@yomotsu) Sep 24, 2017 WebGL Libs
for WebApp Frameworks -
webpack three.js pixi.js etc React Vue.js Angular etc three.js logo:
https://github.com/mrdoob/three.js/issues/2789 -
Tips for development with frameworks + WebGL libs: 1. Import
three.js plugins in webpack. 2. Handle Canvas and Renderer as a WebApp view. 3. Remove Canvas and GL context appropriately. 4. Assets compressing. Agenda -
• OrbitControls • EffectComposer and Passes • CSS3DRenderer • Loaders
and others. Plugins for three.js -
import * as THREE from 'three'; THREE.OrbitControls = require( 'imports-loader?THREE!
exports-loader?THREE.OrbitControls!../node_modules/ three/examples/js/controls/OrbitControls.js' ); // snip const controls = new THREE.OrbitControls( camera ); -
import * as THREE from 'three'; THREE.OrbitControls = require( 'imports-loader?THREE!
exports-loader?THREE.OrbitControls!../node_modules/ three/examples/js/controls/OrbitControls.js' ); // snip const controls = new THREE.OrbitControls( camera ); -
import * as THREE from 'three'; THREE.OrbitControls = require( 'imports-loader?THREE!
exports-loader?THREE.OrbitControls!../node_modules/ three/examples/js/controls/OrbitControls.js' ); // snip const controls = new THREE.OrbitControls( camera ); -
import * as THREE from 'three'; THREE.OrbitControls = require( 'imports-loader?THREE!
exports-loader?THREE.OrbitControls!../node_modules/ three/examples/js/controls/OrbitControls.js' ); // snip const controls = new THREE.OrbitControls( camera ); -
import * as THREE from 'three'; THREE.OrbitControls = require( 'imports-loader?THREE!
exports-loader?THREE.OrbitControls!../node_modules/ three/examples/js/controls/OrbitControls.js' ); // snip const controls = new THREE.OrbitControls( camera ); -
import * as THREE from 'three'; THREE.OrbitControls = require( 'imports-loader?THREE!
exports-loader?THREE.OrbitControls!../node_modules/ three/examples/js/controls/OrbitControls.js' ); // snip const controls = new THREE.OrbitControls( camera ); -
import * as THREE from 'three'; THREE.OrbitControls = require( 'imports-loader?THREE!
exports-loader?THREE.OrbitControls!../node_modules/ three/examples/js/controls/OrbitControls.js' ); // snip const controls = new THREE.OrbitControls( camera ); -
<template> <canvas></canvas> </template> <script> import * as THREE from 'three';
export default { name: 'view-3d', data () { return { width: 200, height: 200 mount a canvas element -
} }, mounted() { const width = this.width; const height
= this.height; const loader = new THREE.JSONLoader(); // (snip) this.renderer = new THREE.WebGLRenderer( { canvas: this.$el, // <-- bind to mouted canvas antialias: true, stencil: false } ); -
<template> <canvas></canvas> </template> <script> import * as PIXI from 'pixi.js';
export default { data () { return {} }, mounted() { -
return {} }, mounted() { this.renderer = new PIXI.WebGLRenderer( {
width: 300, height: 300, view: this.$el, antialias: true, transparent: false, backgroundColor: 0xcccccc } ); -
tick() { if ( ! this.running ) return; requestAnimationFrame( ()
=> this.tick() ); this.mesh.rotation.y += 0.03; this.renderer.render( this.scene, this.camera ); } Your render loop -
beforeDestroy() { this.running = false; while ( this.scene.children.length > 0
) { const object = this.scene.children[ this.scene.children.length - 1 ]; deepDispose( object ); this.scene.remove( object ); } this.renderer.dispose(); this.renderer.forceContextLoss(); this.renderer.context = undefined; this.renderer.domElement = undefined; // until next garbage collection this.$el.width = 1; this.$el.height = 1; }, Dispose all objects -
beforeDestroy() { this.running = false; while ( this.scene.children.length > 0
) { const object = this.scene.children[ this.scene.children.length - 1 ]; deepDispose( object ); this.scene.remove( object ); } this.renderer.dispose(); this.renderer.forceContextLoss(); this.renderer.context = undefined; this.renderer.domElement = undefined; // until next garbage collection this.$el.width = 1; this.$el.height = 1; }, Children of scene consist of • Meshes • Lights • Object3d -
function deepDispose( object3D ) { object3D.traverse( object3D => dispose( object3D
) ); } function dispose ( object3D ) { if ( !! object3D.geometry ) { object3D.geometry.dispose(); object3D.geometry = undefined; } if ( !! object3D.material && object3D.material instanceof Array ) { object3D.material.forEach( material => disposeMaterial( material ) ); -
function deepDispose( object3D ) { object3D.traverse( object3D => dispose( object3D
) ); } function dispose ( object3D ) { if ( !! object3D.geometry ) { object3D.geometry.dispose(); object3D.geometry = undefined; } if ( !! object3D.material && object3D.material instanceof Array ) { object3D.material.forEach( material => disposeMaterial( material ) ); -
function dispose ( object3D ) { if ( !! object3D.geometry
) { object3D.geometry.dispose(); object3D.geometry = undefined; } if ( !! object3D.material && object3D.material instanceof Array ) { object3D.material.forEach( material => disposeMaterial( material ) ); } else if ( !! object3D.material ) { disposeMaterial( material ); } -
function disposeMaterial( material ) { if ( !! material.map )
{ material.map.dispose(); material.map = undefined; } // do that for normalMap, specularMap and bumpMap too material.dispose(); material = undefined; } -
Before unmount: • Stop render loop. • Dispose all geoms,
materials and textures recursively. • Release renderer and context. • Resize canvas to 1px * 1px. -
tick() { if ( ! this.running ) return; requestAnimationFrame( ()
=> this.tick() ); this.movieClip.rotation += 0.03; this.renderer.render( this.stage ); } -
beforeDestroy() { this.running = false; const destroyChildren = true; this.stage.destroy(
destroyChildren ); this.renderer.destroy(); this.renderer.view = undefined; // until next garbage collection this.$el.width = 1; this.$el.height = 1; }, -
Before unmount: • Stop render loop. • Destroy the stage
with deep option. • Release renderer and context. • Resize canvas to 1px * 1px. -
FYI: May not necessary to remove the canvas. It may
cause flickering. Consider to just hide it. -
const DRACO_PATH = './libs/'; const DRACO_TYPE = !! window.WebAssembly ?
'wasm' : 'js'; const dracoLoader = new THREE.DRACOLoader( DRACO_PATH, { type: DRACO_TYPE } ); dracoLoader.load( './model/rameses.drc', ( geometry ) => { const material = new THREE.MeshStandardMaterial( { map: texture } ); const mesh = new THREE.Mesh( geometry, material ); scene.add( mesh ); } ); -
const DRACO_PATH = './libs/'; const DRACO_TYPE = !! window.WebAssembly ?
'wasm' : 'js'; const dracoLoader = new THREE.DRACOLoader( DRACO_PATH, { type: DRACO_TYPE } ); dracoLoader.load( './model/rameses.drc', ( geometry ) => { const material = new THREE.MeshStandardMaterial( { map: texture } ); const mesh = new THREE.Mesh( geometry, material ); scene.add( mesh ); The decoder will be loaded from external file. You need set the path to it. -
const DRACO_PATH = './libs/'; const DRACO_TYPE = !! window.WebAssembly ?
'wasm' : 'js'; const dracoLoader = new THREE.DRACOLoader( DRACO_PATH, { type: DRACO_TYPE } ); dracoLoader.load( './model/rameses.drc', ( geometry ) => { const material = new THREE.MeshStandardMaterial( { map: texture } ); const mesh = new THREE.Mesh( geometry, material ); scene.add( mesh ); The recorder is available in both Wasm and JS. Use preferred version. -
const DRACO_PATH = './libs/'; const DRACO_TYPE = !! window.WebAssembly ?
'wasm' : 'js'; const dracoLoader = new THREE.DRACOLoader( DRACO_PATH, { type: DRACO_TYPE } ); dracoLoader.load( './model/rameses.drc', ( geometry ) => { const material = new THREE.MeshStandardMaterial( { map: texture } ); const mesh = new THREE.Mesh( geometry, material ); scene.add( mesh ); Make a loader instance for three.js -
const DRACO_PATH = './libs/'; const DRACO_TYPE = !! window.WebAssembly ?
'wasm' : 'js'; const dracoLoader = new THREE.DRACOLoader( DRACO_PATH, { type: DRACO_TYPE } ); dracoLoader.load( './model/rameses.drc', ( geometry ) => { const material = new THREE.MeshStandardMaterial( { map: texture } ); const mesh = new THREE.Mesh( geometry, material ); scene.add( mesh ); } ); Load a drc file -
const DRACO_PATH = './libs/'; const DRACO_TYPE = !! window.WebAssembly ?
'wasm' : 'js'; const dracoLoader = new THREE.DRACOLoader( DRACO_PATH, { type: DRACO_TYPE } ); dracoLoader.load( './model/rameses.drc', ( geometry ) => { const material = new THREE.MeshStandardMaterial( { map: texture } ); const mesh = new THREE.Mesh( geometry, material ); scene.add( mesh ); } ); -
• Pass renderer.render() if not needed. • Store as numbers:
colour, time etc… Other things -
three.js / pixi.js and others can be a part of
WebApp! Not only for artistic projects. Conclusion