Slide 1

Slide 1 text

WebGL:
 The graphics API
 for web developers Presented by Akihiro Oyamada (@yomotsu) Jun 18, 2015

Slide 2

Slide 2 text

Frontend Engineer at PixelGrid, Inc. Akihiro Oyamada @yomotsu

Slide 3

Slide 3 text

Limitations of the Web 3D

Slide 4

Slide 4 text

4 www.nike.com/gb/en_gb/c/nikeid

Slide 5

Slide 5 text

5 http://www2.asics.co.jp/baseball/3dorder

Slide 6

Slide 6 text

6 http://www2.asics.co.jp/io/help/help.html They also provide
 an installation guide…

Slide 7

Slide 7 text

There were
 no graphics APIs
 for the Web

Slide 8

Slide 8 text

The history
 of the web 3d

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

• A Web Standerd • A JavaScript feature • Great performance • Programable Shaders in GLSL

Slide 11

Slide 11 text

Click a link,
 Just it will open!

Slide 12

Slide 12 text

Maintained by Khronos Group, not W3C

Slide 13

Slide 13 text

13 http://caniuse.com/#feat=webgl

Slide 14

Slide 14 text

Most of browsers support WebGL

Slide 15

Slide 15 text

Hardware-
 accelerated

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

vec3(-1, -1, 0) vec3( 1, -1, 0) vec3(-1, 1, 0) vec3(-1, 1, 0) vec3( 1, -1, 0) vec3( 1, 1, 0) ... 1 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 ✕ position matrix

Slide 19

Slide 19 text

for ( var i = 0, l = position.length; i < l; i ++ ) { ! matrix.multiplyVector( position[ i ] ); ! }

Slide 20

Slide 20 text

Scalar Operation SIMD Operation

Slide 21

Slide 21 text

Scalar Operation SIMD Operation

Slide 22

Slide 22 text

Scalar Operation SIMD Operation

Slide 23

Slide 23 text

CPU GPU

Slide 24

Slide 24 text

CPU GPU

Slide 25

Slide 25 text

Super fast! because of the parallel processing

Slide 26

Slide 26 text

• Tech definitions • Verbose initialization of WebGL • Shaders in GLSL • Math: Linear algebra, physics, etc… :PVTUJMMIBWFUPMFBSO

Slide 27

Slide 27 text

three.js PIXI.js TWGL

Slide 28

Slide 28 text

three.js

Slide 29

Slide 29 text

29 http://threejs.org/

Slide 30

Slide 30 text

30 http://threejs.org/docs/

Slide 31

Slide 31 text

31 http://www.apple.com/macbook/design/

Slide 32

Slide 32 text

32 https://www.ana-flightconnections.com/

Slide 33

Slide 33 text

33 https://www.ana-flightconnections.com/

Slide 34

Slide 34 text

34 http://fr.special-t.com/media/ogreen/en/

Slide 35

Slide 35 text

35 https://yomotsu.github.io/walkthrough

Slide 36

Slide 36 text

make a scene

Slide 37

Slide 37 text

make a camera

Slide 38

Slide 38 text

add a light

Slide 39

Slide 39 text

add an object

Slide 40

Slide 40 text

http://localhost:8000/threejs/threejs_0_basic.html

Slide 41

Slide 41 text

=^.^= var width = window.innerWidth, height = window.innerHeight; ! var scene = new THREE.Scene(); ! var camera = new THREE.PerspectiveCamera( 40, width / height, 1, 1000 ); camera.position.set( 0, 0, 3 ); ! var renderer = new THREE.WebGLRenderer(); renderer.setSize( width, height ); document.body.appendChild( renderer.domElement ); !

Slide 42

Slide 42 text

=^.^= var width = window.innerWidth, height = window.innerHeight; ! var scene = new THREE.Scene(); ! var camera = new THREE.PerspectiveCamera( 40, width / height, 1, 1000 ); camera.position.set( 0, 0, 3 ); ! var renderer = new THREE.WebGLRenderer(); renderer.setSize( width, height ); document.body.appendChild( renderer.domElement ); ! load pixi.js

Slide 43

Slide 43 text

var width = window.innerWidth, height = window.innerHeight; ! var scene = new THREE.Scene(); ! var camera = new THREE.PerspectiveCamera( 40, width / height, 1, 1000 ); camera.position.set( 0, 0, 3 ); ! var renderer = new THREE.WebGLRenderer(); renderer.setSize( width, height ); document.body.appendChild( renderer.domElement ); ! var directionalLight = new THREE.DirectionalLight( 0xffffff ); directionalLight.position.set( 0, 1, 1 ).normalize(); scene.add( directionalLight ); ! var box = new THREE.Mesh( new THREE.BoxGeometry( 1, 1, 1 ), new THREE.MeshPhongMaterial( { color: 0xff0000 } ) ); make a scene

Slide 44

Slide 44 text

var width = window.innerWidth, height = window.innerHeight; ! var scene = new THREE.Scene(); ! var camera = new THREE.PerspectiveCamera( 40, width / height, 1, 1000 ); camera.position.set( 0, 0, 3 ); ! var renderer = new THREE.WebGLRenderer(); renderer.setSize( width, height ); document.body.appendChild( renderer.domElement ); ! var directionalLight = new THREE.DirectionalLight( 0xffffff ); directionalLight.position.set( 0, 1, 1 ).normalize(); scene.add( directionalLight ); ! var box = new THREE.Mesh( new THREE.BoxGeometry( 1, 1, 1 ), new THREE.MeshPhongMaterial( { color: 0xff0000 } ) ); make a camera

Slide 45

Slide 45 text

var scene = new THREE.Scene(); ! var camera = new THREE.PerspectiveCamera( 40, width / height, 1, 1000 ); camera.position.set( 0, 0, 3 ); ! var renderer = new THREE.WebGLRenderer(); renderer.setSize( width, height ); document.body.appendChild( renderer.domElement ); ! var directionalLight = new THREE.DirectionalLight( 0xffffff ); directionalLight.position.set( 0, 1, 1 ).normalize(); scene.add( directionalLight ); ! var box = new THREE.Mesh( new THREE.BoxGeometry( 1, 1, 1 ), new THREE.MeshPhongMaterial( { color: 0xff0000 } ) ); scene.add( box ); ! ( function renderLoop () { ! make a renderer

Slide 46

Slide 46 text

! var renderer = new THREE.WebGLRenderer(); renderer.setSize( width, height ); document.body.appendChild( renderer.domElement ); ! var directionalLight = new THREE.DirectionalLight( 0xffffff ); directionalLight.position.set( 0, 1, 1 ).normalize(); scene.add( directionalLight ); ! var box = new THREE.Mesh( new THREE.BoxGeometry( 1, 1, 1 ), new THREE.MeshPhongMaterial( { color: 0xff0000 } ) ); scene.add( box ); ! ( function renderLoop () { ! requestAnimationFrame( renderLoop ); box.rotation.y += 0.01; renderer.render( scene, camera ); ! make a light

Slide 47

Slide 47 text

var directionalLight = new THREE.DirectionalLight( 0xffffff ); directionalLight.position.set( 0, 1, 1 ).normalize(); scene.add( directionalLight ); ! var box = new THREE.Mesh( new THREE.BoxGeometry( 1, 1, 1 ), new THREE.MeshPhongMaterial( { color: 0xff0000 } ) ); scene.add( box ); ! ( function renderLoop () { ! requestAnimationFrame( renderLoop ); box.rotation.y += 0.01; renderer.render( scene, camera ); ! } )(); !

Slide 48

Slide 48 text

scene.add( directionalLight ); ! var box = new THREE.Mesh( new THREE.BoxGeometry( 1, 1, 1 ), new THREE.MeshPhongMaterial( { color: 0xff0000 } ) ); scene.add( box ); ! ( function renderLoop () { ! requestAnimationFrame( renderLoop ); box.rotation.y += 0.01; renderer.render( scene, camera ); ! } )(); !

Slide 49

Slide 49 text

http://localhost:8000/threejs/threejs_0_basic.html

Slide 50

Slide 50 text

3D Models http://localhost:8000/threejs/threejs_1_3dmodel.html

Slide 51

Slide 51 text

skeletal animation http://localhost:8000/threejs/threejs_2_animation.html

Slide 52

Slide 52 text

events http://localhost:8000/threejs/threejs_3_event.html

Slide 53

Slide 53 text

custom shader http://yomotsu.github.io/VolumetricFire/examples/example2.html

Slide 54

Slide 54 text

• 3D model import • Animations • Math functions ( such as vector and matrix ) • Build-in Shaders • Post processing effects and more!

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

56 http://www.pixijs.com/

Slide 57

Slide 57 text

57 http://pixijs.github.io/docs/

Slide 58

Slide 58 text

58 http://www.html5gamedevs.com/forum/15-pixijs/

Slide 59

Slide 59 text

59 http://www.goodboydigital.com/runpixierun/

Slide 60

Slide 60 text

60 http://kaatje.ketnet.be/html/

Slide 61

Slide 61 text

61 http://www.wolverineunleashed.com/

Slide 62

Slide 62 text

62 http://archive.revolutionsinsound.com/#/explore/clubnights/bugged-out

Slide 63

Slide 63 text

http://localhost:8000/pixijs/pixi_0_basic.html

Slide 64

Slide 64 text

=^.^= body{margin: 0;} canvas{display: block;} ! ! ! var renderer = new PIXI.autoDetectRenderer( 600, 400, { backgroundColor : 0xCCCCCC } ); document.body.appendChild( renderer.view ); !

Slide 65

Slide 65 text

body{margin: 0;} canvas{display: block;} ! ! ! var renderer = new PIXI.autoDetectRenderer( 600, 400, { backgroundColor : 0xCCCCCC } ); document.body.appendChild( renderer.view ); ! var stage = new PIXI.Container(); ! var monster = new PIXI.Sprite.fromImage( 'monster.png' ); monster.position.set( 100, 50 ); stage.addChild( monster ); load pixi.js

Slide 66

Slide 66 text

! ! var renderer = new PIXI.autoDetectRenderer( 600, 400, { backgroundColor : 0xCCCCCC } ); document.body.appendChild( renderer.view ); ! var stage = new PIXI.Container(); ! var monster = new PIXI.Sprite.fromImage( 'monster.png' ); monster.position.set( 100, 50 ); stage.addChild( monster ); ! var animate = ( function animate() { ! requestAnimationFrame( animate ); renderer.render( stage ); ! } )(); make a renderer

Slide 67

Slide 67 text

backgroundColor : 0xCCCCCC } ); document.body.appendChild( renderer.view ); ! var stage = new PIXI.Container(); ! var monster = new PIXI.Sprite.fromImage( 'monster.png' ); monster.position.set( 100, 50 ); stage.addChild( monster ); ! var animate = ( function animate() { ! requestAnimationFrame( animate ); renderer.render( stage ); ! } )(); ! !

Slide 68

Slide 68 text

backgroundColor : 0xCCCCCC } ); document.body.appendChild( renderer.view ); ! var stage = new PIXI.Container(); ! var monster = new PIXI.Sprite.fromImage( 'monster.png' ); monster.position.set( 100, 50 ); stage.addChild( monster ); ! var animate = ( function animate() { ! requestAnimationFrame( animate ); renderer.render( stage ); ! } )(); ! !

Slide 69

Slide 69 text

var stage = new PIXI.Container(); ! var monster = new PIXI.Sprite.fromImage( 'monster.png' ); monster.position.set( 100, 50 ); stage.addChild( monster ); ! var animate = ( function animate() { ! requestAnimationFrame( animate ); renderer.render( stage ); ! } )(); ! !

Slide 70

Slide 70 text

http://localhost:8000/pixijs/pixi_0_basic.html

Slide 71

Slide 71 text

filters http://localhost:8000/pixijs/pixi_1_blend.html

Slide 72

Slide 72 text

sprite animations http://localhost:8000/pixijs/pixi_2_sprite.html

Slide 73

Slide 73 text

events http://localhost:8000/pixijs/pixi_3_event.html

Slide 74

Slide 74 text

shaders http://localhost:8000/pixijs/pixi_4_shader.html

Slide 75

Slide 75 text

custom shaders http://localhost:8000/pixijs/pixi_5_shader2.html

Slide 76

Slide 76 text

• Sprite and spine animations • Mouse and touch events • Pixel manipulation with WebGL • Custom shaders and more

Slide 77

Slide 77 text

• Stage has been deprecated, now container • Name spaces has been changed • EventListener has been supported • Color matrix has been changed to 5x4 from 4x4 and many changes %JGGFSFODFTCFUXFFO7BOE7

Slide 78

Slide 78 text

TWGL.js by the author of WebGL Fundamentals

Slide 79

Slide 79 text

79 http://twgljs.org/

Slide 80

Slide 80 text

http://localhost:8000/twgljs/twgl_1.html

Slide 81

Slide 81 text

=^.^= body{margin: 0;} canvas{display: block;} ! ! ! ! var canvas = document.getElementById( 'canvas' ); canvas.width = window.innerWidth; canvas.height = window.innerHeight;

Slide 82

Slide 82 text

! ! ! ! var canvas = document.getElementById( 'canvas' ); canvas.width = window.innerWidth; canvas.height = window.innerHeight; ! var gl = twgl.getWebGLContext( canvas ); ! var vs = [ 'attribute vec3 position;', 'attribute vec2 texcoord;', 'uniform mat4 projectionMatrix;', 'uniform mat4 viewMatrix;', prepare a canvas

Slide 83

Slide 83 text

! ! ! ! var canvas = document.getElementById( 'canvas' ); canvas.width = window.innerWidth; canvas.height = window.innerHeight; ! var gl = twgl.getWebGLContext( canvas ); ! var vs = [ 'attribute vec3 position;', 'attribute vec2 texcoord;', 'uniform mat4 projectionMatrix;', 'uniform mat4 viewMatrix;', load twgl.js

Slide 84

Slide 84 text

! ! var canvas = document.getElementById( 'canvas' ); canvas.width = window.innerWidth; canvas.height = window.innerHeight; ! var gl = twgl.getWebGLContext( canvas ); ! var vs = [ 'attribute vec3 position;', 'attribute vec2 texcoord;', 'uniform mat4 projectionMatrix;', 'uniform mat4 viewMatrix;', 'uniform mat4 modelMatrix;', 'varying vec2 vUv;', 'void main () {', get gl context

Slide 85

Slide 85 text

! var vs = [ 'attribute vec3 position;', 'attribute vec2 texcoord;', 'uniform mat4 projectionMatrix;', 'uniform mat4 viewMatrix;', 'uniform mat4 modelMatrix;', 'varying vec2 vUv;', 'void main () {', 'gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0 );', 'vUv = texcoord;', ! '}' ].join( '\n' ); ! var fs = [ prepare VS source

Slide 86

Slide 86 text

].join( '\n' ); ! var fs = [ 'precision mediump float;', ! 'uniform sampler2D diffuse;', ! 'varying vec2 vUv;', ! 'void main () {', ! 'vec4 diffuseColor = texture2D( diffuse, vUv );', 'gl_FragColor = diffuseColor;', // 'gl_FragColor = vec4( 0, 0, 0, 1 );', ! '}' ].join( '\n' ); ! var programInfo = twgl.createProgramInfo( gl, [ vs, fs ] ); ! var arrays = { position: { prepare FS source

Slide 87

Slide 87 text

! '}' ].join( '\n' ); ! var programInfo = twgl.createProgramInfo( gl, [ vs, fs ] ); ! var arrays = { position: { numComponents: 3, data: [ -1, -1, 0, 1, -1, 0, -1, 1, 0, ! -1, 1, 0, 1, -1, 0, 1, 1, 0 ] }, indices: { numComponents: 3, data: [ compile those and
 create a program

Slide 88

Slide 88 text

! var arrays = { position: { numComponents: 3, data: [ -1, -1, 0, 1, -1, 0, -1, 1, 0, ! -1, 1, 0, 1, -1, 0, 1, 1, 0 ] }, indices: { numComponents: 3, data: [ 0, 1, 2, 3, 4, 5 ] }, prepare attributes

Slide 89

Slide 89 text

}, indices: { numComponents: 3, data: [ 0, 1, 2, 3, 4, 5 ] }, texcoord: { numComponents: 2, data: [ 0, 1, 1, 1, 0, 0, ! 0, 0, 1, 1, 1, 0 ] } }; !

Slide 90

Slide 90 text

}; ! var bufferInfo = twgl.createBufferInfoFromArrays( gl, arrays ); ! ! var projectionMatrix = twgl.m4.perspective( 30 * Math.PI / 180, gl.canvas.clientWidth / gl.canvas.clientHeight, 1, 1000 ); var viewMatrix = twgl.m4.translate( twgl.m4.identity(), [ 0, 0 ,-5 ] ); var modelMatrix = twgl.m4.rotationY( 1 ); ! ! var tex = twgl.createTexture( gl, { min: gl.NEAREST, mag: gl.NEAREST, src: './uv.png' }, function () {} ); ! make a buffer

Slide 91

Slide 91 text

! var projectionMatrix = twgl.m4.perspective( 30 * Math.PI / 180, gl.canvas.clientWidth / gl.canvas.clientHeight, 1, 1000 ); var viewMatrix = twgl.m4.translate( twgl.m4.identity(), [ 0, 0 ,-5 ] ); var modelMatrix = twgl.m4.rotationY( 1 ); ! ! var tex = twgl.createTexture( gl, { min: gl.NEAREST, mag: gl.NEAREST, src: './uv.png' }, function () {} ); ! var uniforms = { projectionMatrix: projectionMatrix, viewMatrix : viewMatrix, modelMatrix : modelMatrix, prepare unifroms

Slide 92

Slide 92 text

}, function () {} ); ! var uniforms = { projectionMatrix: projectionMatrix, viewMatrix : viewMatrix, modelMatrix : modelMatrix, diffuse : tex }; ! var render = function ( time ) { ! twgl.resizeCanvasToDisplaySize( gl.canvas ); gl.viewport( 0, 0, gl.canvas.width, gl.canvas.height ); uniforms.modelMatrix = twgl.m4.rotationY( time * 0.001 ); ! gl.useProgram( programInfo.program ); twgl.setBuffersAndAttributes( gl, programInfo, bufferInfo ); twgl.setUniforms( programInfo, uniforms ); gl.drawElements( gl.TRIANGLES, bufferInfo.numElements, gl.UNSIGNED_SHORT, 0 ); !

Slide 93

Slide 93 text

! var render = function ( time ) { ! twgl.resizeCanvasToDisplaySize( gl.canvas ); gl.viewport( 0, 0, gl.canvas.width, gl.canvas.height ); uniforms.modelMatrix = twgl.m4.rotationY( time * 0.001 ); ! gl.useProgram( programInfo.program ); twgl.setBuffersAndAttributes( gl, programInfo, bufferInfo ); twgl.setUniforms( programInfo, uniforms ); gl.drawElements( gl.TRIANGLES, bufferInfo.numElements, gl.UNSIGNED_SHORT, 0 ); ! requestAnimationFrame( render ); ! }; ! render( 0 ); ! ! render it!

Slide 94

Slide 94 text

http://localhost:8000/twgljs/twgl_1.html

Slide 95

Slide 95 text

http://localhost:8000/twgljs/twgl_2.html

Slide 96

Slide 96 text

• Less verbose initialization • Math functions That’s it!

Slide 97

Slide 97 text

WebGL is not PS4

Slide 98

Slide 98 text

• WebUI for 3D contents
 Still not exist a lot • Multiple devices
 High-End Gaming PC, Laptop, Mobile and others • Internet Connection 8IBUXFOFFEUPUIJOLBCPVU

Slide 99

Slide 99 text

• Number of polygons
 esp. file size • Texture size • Resolution • Number of Draw-calls 8IBUXFOFFEUPDBSFBCPVU

Slide 100

Slide 100 text

Like 3DS, PSP, PSVita FYI: Playable characters in FINAL FANTASY TYPE-0 consist of
 about 1000 tris and 76KB textures http://hexadrive.jp/lab/case/1573/

Slide 101

Slide 101 text

Video games could be
 great knowledge

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

WebVR

Slide 105

Slide 105 text

d = navigator.
 getVRDevices()

Slide 106

Slide 106 text

d = navigator.
 getVRDevices() •d[ i ].VRDevice (Type of the device) •d[ i ].VREyeParameters •FOV •eyeTranslation •d[ i ].VRPositionState •position •velocity •orientation •angularVelocity othres...

Slide 107

Slide 107 text

vrHMD.getEyeTranslation( ‘left' ); vrHMD.getEyeTranslation( 'right' ); vrHMD.getRecommendedEyeFieldOfView( 'left' ); vrHMD.getRecommendedEyeFieldOfView( 'right' );

Slide 108

Slide 108 text

Chromium WebVR Builds Firefox Nightly Builds WebVR Oculus Rift Enabler as of Jun, 2015

Slide 109

Slide 109 text

Virtuix Omni Locomotion HMD Cyberith Virtualizer OCULUS Rift SONY Morpheus HTC Vine • Leap Motion • PrioVR • Control VR • Sixense Stem • Razer Hydra Others

Slide 110

Slide 110 text

110 https://www.youtube.com/watch?v=aTtfAQEeAJI

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

Drop-in Phone
 Viewers

Slide 113

Slide 113 text

Drop-in Phone
 Viewers • Different APIs
 (It will use DeviceOrientation API instead) • Limited performance
 (Stereo rendering on mobile device)

Slide 114

Slide 114 text

Conclusion

Slide 115

Slide 115 text

WebGL is
 a cross-platform,
 web standard, fast,
 powerful graphics API

Slide 116

Slide 116 text

Several JS libraries
 are available
 for different purposes

Slide 117

Slide 117 text

WebGL is still expanding WebGL2, WebVR and others

Slide 118

Slide 118 text

gl.finish(); @yomotsu