from hello world to the interactive web with three.js: workshop at futurejs 2014

64
WebGL / Three.js Workshop at Futurejs 2014 Ross McKegney & Carlos Sanchez @verold

Upload: verold

Post on 28-Jan-2015

105 views

Category:

Software


0 download

DESCRIPTION

The first workshop at the first ever FutureJS conference in Barcelona. From Three.js Hello World to building your first interactive 3D app, to connecting your web app with the Internet of Things.

TRANSCRIPT

Page 1: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

WebGL / Three.js Workshop at Futurejs 2014

Ross McKegney & Carlos Sanchez

@verold

Page 2: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Agenda:

9:00 Introduction & Basics of Three.js10:00 Adding 3D content to your web app11:15 Three.js and the Internet of Things12:45 Wrap up

Page 3: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Gaming & Web are converging

Page 4: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Gaming & Web are converging

This era of the web belongs to creative coders

Page 5: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
Page 6: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
Page 7: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
Page 8: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Graphics WebGLProcessing WebCL / Workers / EmscriptenAudio Web AudioNetworking WebRTCReal-time + Devices Web Sockets

Page 9: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
Page 10: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

This is for you!The web is evolving, and it will reshape game development AND web design

Page 11: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
Page 12: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
Page 13: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Three.js

Page 14: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Global Arms Trade Datavizby Google

Small arms trade data from 1992-2010 is visualized on an interactive 3D globe.

Page 15: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Journey to Middle Earthby North Kingdom

An interactive tour of Middle Earth for desktop and mobile, using WebGL and HTML5.

Page 16: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

HexGLby Thibaut Despoutin

A simple racing game built for the web using Three.js.

Page 17: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

MODULE 1:Getting Started with Three.js

http://threejs.org/docs/index.html

Page 18: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Three.js starts with:

● Scene● Renderer (we’ll always use WebGL)● Camera, Objects, Materials, Lights

Page 19: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

var scene = new THREE.Scene();var camera =

new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

var renderer = new THREE.WebGLRenderer();renderer.setSize( window.innerWidth, window.innerHeight );document.body.appendChild( renderer.domElement );

var geometry = new THREE.CubeGeometry(1,1,1);var material = new THREE.MeshBasicMaterial(

{ color: 0x00ff00, wireframe: true } );var cube = new THREE.Mesh( geometry, material );scene.add( cube );

camera.position.z = 5;

function render() {requestAnimationFrame(render);renderer.render(scene, camera);

cube.rotation.x += 0.01;cube.position.z += 0.01;

}

render(); http://codepen.io/rossmckegney/full/lcAta

Page 20: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Tweak 1: MaterialsThree.js comes with a number of built-in materials and shaders: Basic, Lambert, Phong.

Page 21: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

MeshLambertMaterialFor non-shiny materials, lit per vertex

var geometry = new THREE.CubeGeometry(1,1,1);var material = new THREE.MeshLambertMaterial(

{ ambient: 'blue' } );var cube = new THREE.Mesh( geometry, material );cube.overdraw = true;scene.add( cube );

var ambientLight = new THREE.AmbientLight('white');scene.add(ambientLight);

http://codepen.io/rossmckegney/full/DaohB

Page 22: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

MeshPhongMaterialFor shiny materials, lit per pixel

var geometry = new THREE.CubeGeometry(1,1,1);var material = new THREE.MeshPhongMaterial(

{ color: 'blue' } );var cube = new THREE.Mesh( geometry, material );scene.add( cube );

var directionalLight = new THREE.DirectionalLight( 'white', 0.5 );directionalLight.position.set( 0, 0, 1 );scene.add( directionalLight );

http://codepen.io/rossmckegney/full/lkwnI

Page 23: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Tweak 2: Load modelThree.js comes with loaders for JSON, OBJ, Collada, STL, etc

Page 24: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

THREE.OBJLoaderThree.js extension, allows to load an OBJ file. Load the mesh and texture, with progress reporting. Watch out for CORS.

Page 25: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

var manager = new THREE.LoadingManager();manager.onProgress = function ( item, loaded, total ) {

console.log( item, loaded, total );};

var texture = new THREE.Texture();var loader = new THREE.ImageLoader( manager );loader.load( 'textures/UV_Grid_Sm.jpg', function ( image ) {

texture.image = image;texture.needsUpdate = true;

} );

var loader = new THREE.OBJLoader( manager );loader.load( 'obj/male02.obj', function ( object ) {

object.traverse( function ( child ) {if ( child instanceof THREE.Mesh ) {

child.material.map = texture;}

} );object.position.y = -90;scene.add( object );

} );

Page 26: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

MODULE 2: Overview of Verold Studio

Page 28: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Let’s be clear:Three.js is an easy-to-use graphics library supported by a large community of developers. It is not a game engine.

Page 29: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

So, we built an open-source game engine extending Three.js!

● Component Entity Model● Assisted Loading● Mobile Support and Fallbacks

Page 30: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Component Entity ModelGame logic is defined as modular components that can be attached to nodes on the scene graph.

Page 31: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Component Entity Model

Scene Graph

The scene graph holds your models, lights, cameras, etc

Page 32: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Component Entity Model

Scene Graph

Behaviours are defined by attaching modular components to nodes in the scene graph

Page 33: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Component Entity Model

Demo: Let’s light and rotate a cube!

Page 34: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Assisted LoadingIn a game engine (especially for the web) you need fine-grained control over loading

Page 35: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Assisted LoadingYou might load everything upfront (loading bar), or defer loading. We need a way to trigger asset/object loading and bind to different stages of load.

Page 36: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Assisted LoadingDemo: Loading scenarios

Page 37: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Mobile Support & FallbacksIdeally, HTML5 is write once, run anywhere. Not true in practice.

Page 38: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Mobile Support & FallbacksDevice constraints:● CPU & rendering performance● GPU capabilities● Network bandwidth

Page 39: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Mobile Support & FallbacksDemo: Thooloo.com

Page 40: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

MODULE 3:Adding 3D to your website

Page 41: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Animated Hero ImageGreat way to bringyour homepage to life!

http://verold.com

Page 42: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Product ConfiguratorsYou can easily swap geometries, change lighting,materials, etc..

http://vrld.co/ogUzZk

Page 43: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

3D ScansUseful not only formodelled 3D, but also for 3D scans.

http://www.juanvilla.es

http://bossfight.me

Page 44: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Plays well with others!Three.js and Verold worknicely for simple demos, or can be scaled up to robust frameworks likeAngular.js

http://brained.io

Page 45: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Offline too!Web can be used offlineor online. e.g. BPush kioskby James White designs.

http://ibptouch.dyndns.org/?range=26

Page 46: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

And of course games :)The Verold engine is well suited to simple games, seamlesslyintegrated with your website.

http://thooloo.com

Page 47: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

OK… so let’s add some 3D to a page!The goal for this exercise is to walk you through adding a 3D model to a canvas on your web app.

Page 48: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Steps:1. Upload 3D model2. Setup scene and materials3. Create your responsive web app4. Load the Verold engine and model5. Setup controls and widgets

Page 49: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

MODULE 4:Verold and the Internet of Things

Page 50: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

IoT -> Web

External Device

NativeHandler

bluetooth

Node.js

Emittersocket

Web Browser

Web Appweb socket

Web Server

e.g.NeuroSkyOculus VRRaspberry PiLeap Motion...

Desktop

Page 51: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

ex. Node-Neurosky

Page 52: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Connecting to Neurosky:this.port = opts.port || 13854this.host = opts.host || 'localhost'

if(typeof(opts.appName) !== 'string') throw new NodeNeuroSkyError('Must specify appName')if(typeof(opts.appKey) !== 'string') throw new NodeNeuroSkyError('Must specify appKey')

this.auth = {appName:opts.appName,appKey:opts.appKey

}

this.config = {enableRawOutput: false,format: "Json"

}

events.EventEmitter.call(this)

Page 53: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Listening:NeuroSkyClient.prototype.connect = function(){

...client.on('data',function(data){

if(!self.configSent){self.configSent = trueclient.write(JSON.stringify(self.config))

} else {try{

self.emit('data',JSON.parse(data.toString()))}catch(e){

self.emit('parse_error',data.toString())}

}})

Page 54: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Emitting:var express = require(‘express’), app = express(), server = require(‘http’).createServer(app),

io = require(‘socket.io’).listen(server, {log:false}), nodeThinkGear = require(‘../node-thinkgear’), mySocket = undefined;

var tgClient = nodeThinkGear.createClient({ appName: ‘NodeThinkGear’, appKey: ‘xxx’ });tgClient.connect();

io.sockets.on(‘connection’, function (socket) { mySocket = socket; });

tgClient.on(‘data, function(data) {if (data[‘poorSignalLevel’]) {

console.log(“connecting…”);}

if (mySocket) {mySocket.emit(‘think’, {data: data});

}});

Page 55: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

JS Client:var socket = io.connect(‘http://localhost:3000’);

socket.on(‘think’, function(data) {if (data[‘data’] && data[‘data’][‘eSense’]) {

attention = data[‘data’[‘eSense’][‘attention’];meditation = data[‘data’[‘eSense’][meditation];

}}

Page 56: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Exercise: Leap Motion and Three.jsLet’s integrate 3D gestures with our 3D scene!

Page 57: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Download the Leap Motion client app

https://www.leapmotion.com/setup

Page 58: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Get LeapJSLearn more about LeapJS at https://developer.leapmotion.com/leapjs/welcome

Page 59: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Verold Leap TemplateGrab the Verold Leap Template athttp://studio.verold.com/projects/5357f75685ce9f0200000058

Page 60: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

VeroldController ComponentAttach this to your Scene, and then it will establish a connection to Leap and pass the connection as the ‘leapReady’ event. // Connect to Leap var leap = new Leap.Controller({host: 'localhost', port: 6437}); leap.connect();

// This allows us to maintain the connection even whilst in an iFrame. leap.setBackground(true); // Pass the leap controller to all listeners this.events.trigger("leapReady", leap);

Page 61: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

VeroldListener ComponentSample component to show you how to listen to Leap events from the LeapController.

Component.prototype.init = function() { this.listenTo(this.events, "leapReady", this.initLeap);};

Component.prototype.initLeap = function(leap) { this.leap = leap;};

Page 62: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

Do Leap things!!!

LeapFrame = { hands: [ { palmPosition: [x,y,z] palmNormal: [x,y,z] direction: [x,y,z] roll() pitch() yaw() fingers: [ { tipPosition: [x,y,z] direction: [x,y,z] } ] } ]}

Page 63: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

What will you make?

Page 64: From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014

COMMUNICATE IN 3D

Ross McKegney, [email protected]

Carlos Sanchez, Senior Web [email protected]