Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

3D is hard

Slide 3

Slide 3 text

We’re building a videogame

Slide 4

Slide 4 text

We’re building a videogame

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

(You are being a smartass)

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

WHAT?

Slide 11

Slide 11 text

WebGL is A JavaScript API to render graphics accessing directly the GPU

Slide 12

Slide 12 text

WebGL is not An API for 3D graphics

Slide 13

Slide 13 text

WebGL is not An OpenGL Wrapper

Slide 14

Slide 14 text

angle angle OpenGL ES 2.0 to DirectX 9 translation

Slide 15

Slide 15 text

angle angle OpenGL ES 2.0 to DirectX 9 translation

Slide 16

Slide 16 text

WHERE?

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

DIV

Slide 19

Slide 19 text

CANVAS DIV

Slide 20

Slide 20 text

WebGL context CANVAS “webgl” .getContext(“webgl”) WebGL context the

Slide 21

Slide 21 text

WebGL context WebGL context(s) a b a.getContext(“webg b.getContext(“webg

Slide 22

Slide 22 text

WHEN?

Slide 23

Slide 23 text

DRAW DRAW DRAW DRAW DRAW DRAW DRAW DRAW Not when you want to

Slide 24

Slide 24 text

When the browser tells you to.

Slide 25

Slide 25 text

When the browser tells you to. requestAnimationFrame

Slide 26

Slide 26 text

BRO WHAT ABOUT DOUBLE BUFFERING

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Tearing

Slide 29

Slide 29 text

Buffer A Buffer B

Slide 30

Slide 30 text

Buffer A Buffer B

Slide 31

Slide 31 text

Buffer A Buffer B

Slide 32

Slide 32 text

JavaScript doesn’t execute while the DOM is being rendered!

Slide 33

Slide 33 text

JavaScript doesn’t execute while the DOM is being rendered!

Slide 34

Slide 34 text

HOW?

Slide 35

Slide 35 text

Introducing... Jimmy the Vertex

Slide 36

Slide 36 text

I’m not looking forward to this...

Slide 37

Slide 37 text

(x, y, z)

Slide 38

Slide 38 text

+x +y +z RIGHT HAND REPRESENT

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Local Space

Slide 43

Slide 43 text

World Space

Slide 44

Slide 44 text

Camera Space

Slide 45

Slide 45 text

Camera Space

Slide 46

Slide 46 text

Screen Space

Slide 47

Slide 47 text

xx xy xz Yx Yy Yz Zx Zy Zz { } World Space Local Space Model Matrix

Slide 48

Slide 48 text

xx xy xz Yx Yy Yz Zx Zy Zz { } Camera Space World Space View Matrix

Slide 49

Slide 49 text

xx xy xz Yx Yy Yz Zx Zy Zz { } Screen Space Camera Space Projection Matrix

Slide 50

Slide 50 text

We could do the matrix operations in JavaScript

Slide 51

Slide 51 text

We could do the matrix operations in JavaScript BUT WE DON’T

Slide 52

Slide 52 text

SHADER is a program that runs on the GPU. SHADER a

Slide 53

Slide 53 text

(THAT WAS EASY)

Slide 54

Slide 54 text

N N It begins with an...

Slide 55

Slide 55 text

OT JAVASCRIPT N N It begins with an...

Slide 56

Slide 56 text

GLSL GLSL OpenGL Shader Language

Slide 57

Slide 57 text

precision highp float; varying vec2 t; varying vec2 pos; uniform float time; uniform vec2 mouse; uniform int mouseLeft; uniform sampler2D tex0; void main() { float v1 = (sin(t.s+time) + 1.0) / 2.0; float v2 = (cos(t.t+time) + 1.0) / 2.0; float d = distance(mouse, pos); vec2 tt = vec2(t.s+sin(time/10.0), t.t+cos(time/10.0)); vec4 c1 = texture2D(tex0, tt) * 1.1; float avg = (c1.r+c1.g+c1.b)/3.0; float r = c1.r+v1*pow(avg,4.0) - pow(d,pow(avg,2.0) +float(mouseLeft)*avg); float g = c1.g+v2*pow(avg,4.0) - pow(d,pow(avg,2.0) +float(mouseLeft)*avg); float b = c1.g - pow(d,pow(avg,2.0) +float(mouseLeft)*avg); gl_FragColor = vec4(r, g, b, 1.0); }

Slide 58

Slide 58 text

precision highp float; varying vec2 t; varying vec2 pos; uniform float time; uniform vec2 mouse; uniform int mouseLeft; uniform sampler2D tex0; void main() { float v1 = (sin(t.s+time) + 1.0) / 2.0; float v2 = (cos(t.t+time) + 1.0) / 2.0; float d = distance(mouse, pos); vec2 tt = vec2(t.s+sin(time/10.0), t.t+cos(time/10.0)); vec4 c1 = texture2D(tex0, tt) * 1.1; float avg = (c1.r+c1.g+c1.b)/3.0; float r = c1.r+v1*pow(avg,4.0) - pow(d,pow(avg,2.0) +float(mouseLeft)*avg); float g = c1.g+v2*pow(avg,4.0) - pow(d,pow(avg,2.0) +float(mouseLeft)*avg); float b = c1.g - pow(d,pow(avg,2.0) +float(mouseLeft)*avg); gl_FragColor = vec4(r, g, b, 1.0); } WELL THIS KINDA LOOKS LIKE JAVASCRIPT WELL THIS KINDA LOOKS LIKE JAVASCRIPT

Slide 59

Slide 59 text

precision highp float; varying vec2 t; varying vec2 pos; uniform float time; uniform vec2 mouse; uniform int mouseLeft; uniform sampler2D tex0; void main() { float v1 = (sin(t.s+time) + 1.0) / 2.0; float v2 = (cos(t.t+time) + 1.0) / 2.0; float d = distance(mouse, pos); vec2 tt = vec2(t.s+sin(time/10.0), t.t+cos(time/10.0)); vec4 c1 = texture2D(tex0, tt) * 1.1; float avg = (c1.r+c1.g+c1.b)/3.0; float r = c1.r+v1*pow(avg,4.0) - pow(d,pow(avg,2.0) +float(mouseLeft)*avg); float g = c1.g+v2*pow(avg,4.0) - pow(d,pow(avg,2.0) +float(mouseLeft)*avg); float b = c1.g - pow(d,pow(avg,2.0) +float(mouseLeft)*avg); gl_FragColor = vec4(r, g, b, 1.0); }

Slide 60

Slide 60 text

precision highp float; varying vec2 t; varying vec2 pos; uniform float time; uniform vec2 mouse; uniform int mouseLeft; uniform sampler2D tex0; void main() { float v1 = (sin(t.s+time) + 1.0) / 2.0; float v2 = (cos(t.t+time) + 1.0) / 2.0; float d = distance(mouse, pos); vec2 tt = vec2(t.s+sin(time/10.0), t.t+cos(time/10.0)); vec4 c1 = texture2D(tex0, tt) * 1.1; float avg = (c1.r+c1.g+c1.b)/3.0; float r = c1.r+v1*pow(avg,4.0) - pow(d,pow(avg,2.0) +float(mouseLeft)*avg); float g = c1.g+v2*pow(avg,4.0) - pow(d,pow(avg,2.0) +float(mouseLeft)*avg); float b = c1.g - pow(d,pow(avg,2.0) +float(mouseLeft)*avg); gl_FragColor = vec4(r, g, b, 1.0); } “

Slide 61

Slide 61 text

precision highp float; varying vec2 t; varying vec2 pos; uniform float time; uniform vec2 mouse; uniform int mouseLeft; uniform sampler2D tex0; void main() { float v1 = (sin(t.s+time) + 1.0) / 2.0; float v2 = (cos(t.t+time) + 1.0) / 2.0; float d = distance(mouse, pos); vec2 tt = vec2(t.s+sin(time/10.0), t.t+cos(time/10.0)); vec4 c1 = texture2D(tex0, tt) * 1.1; float avg = (c1.r+c1.g+c1.b)/3.0; float r = c1.r+v1*pow(avg,4.0) - pow(d,pow(avg,2.0) +float(mouseLeft)*avg); float g = c1.g+v2*pow(avg,4.0) - pow(d,pow(avg,2.0) +float(mouseLeft)*avg); float b = c1.g - pow(d,pow(avg,2.0) +float(mouseLeft)*avg); gl_FragColor = vec4(r, g, b, 1.0); }

Slide 62

Slide 62 text

document.getElementById document.getElementById

Slide 63

Slide 63 text

GLSL GLSL GLSL compile compile compile link

Slide 64

Slide 64 text

xx xy xz Yx Yy Yz Zx Zy Zz { } xx xy xz Yx Yy Yz Zx Zy Zz { } Perspective Matrix Model View Matrix

Slide 65

Slide 65 text

attribute vec3 aVertexPosition; uniform mat4 uMVMatrix; uniform mat4 uPMatrix; void main(void) { gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0); }

Slide 66

Slide 66 text

attribute vec3 aVertexPosition; uniform mat4 uMVMatrix; uniform mat4 uPMatrix; void main(void) { gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0); } Output

Slide 67

Slide 67 text

attribute vec3 aVertexPosition; uniform mat4 uMVMatrix; uniform mat4 uPMatrix; void main(void) { gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0); } Output Uniforms

Slide 68

Slide 68 text

attribute vec3 aVertexPosition; uniform mat4 uMVMatrix; uniform mat4 uPMatrix; void main(void) { gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0); } Output Uniforms Attribute

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

xx xy xz Yx Yy Yz Zx Zy Zz { } xx xy xz Yx Yy Yz Zx Zy Zz { } Passing Uniforms var ptr = gl.getUniformLocation(shaderProgram, "uPMatrix") gl.uniformMatrix4fv( ptr, false, new Float32Array(vertexes));

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

-1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, Here come the vertices

Slide 74

Slide 74 text

-1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, Here come the vertices

Slide 75

Slide 75 text

-1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, Here come the vertices

Slide 76

Slide 76 text

-1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, Here come the vertices

Slide 77

Slide 77 text

-1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, Here come the vertices

Slide 78

Slide 78 text

cubeVertices = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertices); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertices); gl.vertexAttribPointer(vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0); 0,1,0 1,0,0 0,1,1 1,0,1 1,1,1 Passing Vertices

Slide 79

Slide 79 text

cubeVertices = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertices); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertices); gl.vertexAttribPointer(vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0); 0,1,0 1,0,0 0,1,1 1,0,1 1,1,1 Passing Vertices

Slide 80

Slide 80 text

xx xy xz Yx Yy Yz Zx Zy Zz { } xx xy xz Yx Yy Yz Zx Zy Zz { } Uniforms gl_Position 0,1,0 1,0,0 0,1,1 1,0,1 1,1,1 [aVertexPosition] drawArrays*

Slide 81

Slide 81 text

xx xy xz Yx Yy Yz Zx Zy Zz { } xx xy xz Yx Yy Yz Zx Zy Zz { } Uniforms gl_Position 0,1,0 1,0,0 0,1,1 1,0,1 1,1,1 [aVertexPosition] drawArrays*

Slide 82

Slide 82 text

xx xy xz Yx Yy Yz Zx Zy Zz { } xx xy xz Yx Yy Yz Zx Zy Zz { } Uniforms gl_Position 0,1,0 1,0,0 0,1,1 1,0,1 1,1,1 [aVertexPosition] drawArrays*

Slide 83

Slide 83 text

xx xy xz Yx Yy Yz Zx Zy Zz { } xx xy xz Yx Yy Yz Zx Zy Zz { } Uniforms gl_Position 0,1,0 1,0,0 0,1,1 1,0,1 1,1,1 [aVertexPosition] drawArrays*

Slide 84

Slide 84 text

xx xy xz Yx Yy Yz Zx Zy Zz { } xx xy xz Yx Yy Yz Zx Zy Zz { } Uniforms gl_Position 0,1,0 1,0,0 0,1,1 1,0,1 1,1,1 [aVertexPosition] drawArrays*

Slide 85

Slide 85 text

Rasterization

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

Fragments

Slide 89

Slide 89 text

Fragment Shader varying lowp vec4 vColor; void main(void) { gl_FragColor = vColor; }

Slide 90

Slide 90 text

Fragment Shader varying lowp vec4 vColor; void main(void) { gl_FragColor = vColor; } Output

Slide 91

Slide 91 text

Fragment Shader varying lowp vec4 vColor; void main(void) { gl_FragColor = vColor; } Output Variant

Slide 92

Slide 92 text

Variants Vertex Shader Fragment Shader

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

Color

Slide 96

Slide 96 text

Color Texture

Slide 97

Slide 97 text

Color Texture Lighting

Slide 98

Slide 98 text

Color Texture Lighting Specularity

Slide 99

Slide 99 text

Color Texture Lighting Specularity Normal Mapping

Slide 100

Slide 100 text

Color Texture Lighting Specularity Normal Mapping ...

Slide 101

Slide 101 text

Clear Canvas Load Perspective M atrixes Load O bject M atrixes Render O bject

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

Three.js https://github.com/mrdoob/three.js A.K.A. use

Slide 104

Slide 104 text

3D is hard

Slide 105

Slide 105 text

3D is hard hard to understand

Slide 106

Slide 106 text

3D is hard hard to understand hard to learn

Slide 107

Slide 107 text

Hard to get right

Slide 108

Slide 108 text

But we need to try

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

twitter.com/vmg github.com/vmg

Slide 111

Slide 111 text

With demos from: http://wagerfield.github.io/flat-surface-shader/ http://lab.aerotwist.com/webgl/surface/ http://www.mrdoob.com/lab/javascript/beachballs/ http://plumegraph.org/