enei16 - webgl with three.js

110
WebGL with three.js ENEI 2016 José Ferrão

Upload: jose-ferrao

Post on 05-Apr-2017

207 views

Category:

Software


1 download

TRANSCRIPT

Page 1: ENEI16 - WebGL with Three.js

WebGL with three.jsENEI 2016

José Ferrão

Page 2: ENEI16 - WebGL with Three.js

SetupText editor (sublime, emacs, vim, brackets, notepad, ...)

Web Browser (Firefox, chrome, edge, ...)

Phyton 3.0+

2

FilesFiles for the workshop available at

github.com/tentone/enei2016

Page 3: ENEI16 - WebGL with Three.js

PresentationJosé Manuel Miranda Ferrão

MIECT, Aveiro University

3

[email protected]

github.com/tentone

twitter.com/tentonej

Page 4: ENEI16 - WebGL with Three.js

4

IDKwGL (github.com/tentone/idkwgl)

• WebGL based library for 3D graphics

•My first contact with WebGL

Page 5: ENEI16 - WebGL with Three.js

5

nunuStudio (github.com/tentone/nunuStudio)

• IDE for web 3D and VR applications development

• Based on three.js

Page 6: ENEI16 - WebGL with Three.js

6

Page 7: ENEI16 - WebGL with Three.js

DANGER!

7

FOURTH

DIMENSION

in use

Page 8: ENEI16 - WebGL with Three.js

Objectives• Introduce base concepts about graphic programming

• Explore the three.js library

• Introduce WebVR with three.js

8

Page 9: ENEI16 - WebGL with Three.js

Javascript• Originally developed by Brendan Eich in 1995

• JavaScript is standardized by the ECMAScript specifications

• A browser reads the code and runs it directly

9

• Javascript is getting faster

• But still ... stop using it for everything ... please

Page 10: ENEI16 - WebGL with Three.js

Javascript

10

Page 11: ENEI16 - WebGL with Three.js

WebGL• WebGL is a Javascript API for rendering 3D graphics using the computer GPU

• No plugins are required

• WebGL specification is close the to the OpenGL ES 2.0 specification

• Graphics programming can be hard

• Mathematics

• GLSL

11

Page 12: ENEI16 - WebGL with Three.js

How to draw an Owl

12

“A fun and create guide for beginners”

First stepDraw 2 circles

Second stepDraw the rest of the damn Owl

Page 13: ENEI16 - WebGL with Three.js

Rendering pipeline

13

“From data to screen”

Page 14: ENEI16 - WebGL with Three.js

Triangles

14

Page 15: ENEI16 - WebGL with Three.js

Pixel graphics

15

Page 16: ENEI16 - WebGL with Three.js

Sub-pixel accuracy

16

Page 17: ENEI16 - WebGL with Three.js

Sub-pixel accuracy

17

Page 18: ENEI16 - WebGL with Three.js

Anti-Aliasing

18

Page 19: ENEI16 - WebGL with Three.js

Depth Buffer

19

Page 20: ENEI16 - WebGL with Three.js

Depth Buffer (http://orbides.org/apps/superslow.html)

20

Page 21: ENEI16 - WebGL with Three.js

GLSL

GLSL is a C-like language that runs in the GPU to draw images

21

Fragment shader

Operate at pixel level and are used to control how the objects surface is draw

Vertex shader

Operate on the vertex level and can be used to deform objects

Page 22: ENEI16 - WebGL with Three.js

GLSL

22

Page 24: ENEI16 - WebGL with Three.js

Rendering pipeline

24

Page 25: ENEI16 - WebGL with Three.js

three.js

25

Page 26: ENEI16 - WebGL with Three.js

• Cross-browser high level library for WebGL graphics

• User all over the internet

• Compreensive API for beginners

26

three.js (www.threejs.org)

Released by Ricardo Cabello to GitHub in 2010

github.com/mrdoob

twitter.com/mrdoob

Page 28: ENEI16 - WebGL with Three.js

Lifecycle

28

Initialize

Update

Render

Resize

Page 29: ENEI16 - WebGL with Three.js

Lifecycle

29

Page 30: ENEI16 - WebGL with Three.js

Hello world

30

Page 31: ENEI16 - WebGL with Three.js

Can we make it move?

31

Page 32: ENEI16 - WebGL with Three.js

Transformations

32

Page 33: ENEI16 - WebGL with Three.js

Scale

33

ቊx′ = 2 ∙ xy′ = 2 ∙ y

Page 34: ENEI16 - WebGL with Three.js

Rotation

34

ቊx′ = cos 45 ∙ x − sin(45) ∙ y

y′ = sin 45 ∙ x + cos(45) ∙ y

Page 35: ENEI16 - WebGL with Three.js

Translation

35

ቐ𝑥′ = 𝑥 + 𝑤 ∙ 10

𝑦′ = 𝑦𝑤 = 1

Page 36: ENEI16 - WebGL with Three.js

Matrix Transformations

𝑥′ = 𝑎 ∙ 𝑥 + 𝑏 ∙ 𝑦 + 𝑐 ∙ 𝑧 + 𝑑 ∙ 𝑤

𝑦′ = 𝑒 ∙ 𝑥 + 𝑓 ∙ 𝑦 + 𝑔 ∙ 𝑧 + ℎ ∙ 𝑤

𝑧′ = 𝑖 ∙ 𝑥 + 𝑗 ∙ 𝑦 + 𝑘 ∙ 𝑧 + 𝑚 ∙ 𝑤

𝑤′ = 𝑛 ∙ 𝑥 + 𝑜 ∙ 𝑦 + 𝑝 ∙ 𝑧 + 𝑞 ∙ 𝑤

36

𝑥′ 𝑦′ 𝑧′ 𝑤′ =

𝑎 𝑏 𝑐 𝑑𝑒 𝑓 𝑔 ℎ𝑖 𝑗 𝑘 𝑚𝑛 𝑜 𝑝 𝑞

𝑥𝑦𝑧1

• Most of the graphic API’s apply linear transformations using transformation matrixes

• A transformation matrix is multiplied by the model vertices to get the transformated vertices

Page 37: ENEI16 - WebGL with Three.js

3D Objects• Objects are represented in a 3D space• Position

• Scale

• Rotation

• Objects are organized in a tree structure

• Parents properties affect the children objects

Page 38: ENEI16 - WebGL with Three.js

Hello world!

38

Page 39: ENEI16 - WebGL with Three.js

39

Geometries

Page 40: ENEI16 - WebGL with Three.js

Geometries

40

Page 41: ENEI16 - WebGL with Three.js

41

Input

Page 42: ENEI16 - WebGL with Three.js

Mouse and keyboard input

42

Page 43: ENEI16 - WebGL with Three.js

Cameras

43

Page 44: ENEI16 - WebGL with Three.js

Cameras

44Perspective Orthographic

Page 45: ENEI16 - WebGL with Three.js

Cameras

45OrthographicPerspective

Page 46: ENEI16 - WebGL with Three.js

Lights

46

Page 47: ENEI16 - WebGL with Three.js

Lights

Ambient Diffuse Specular Combined

+ + =

Page 48: ENEI16 - WebGL with Three.js

Lights

Diffuse Specular

Page 49: ENEI16 - WebGL with Three.js

Lights

49

Page 50: ENEI16 - WebGL with Three.js

Ambient Lights

50

Page 51: ENEI16 - WebGL with Three.js

Point Lights

51

Page 52: ENEI16 - WebGL with Three.js

Spot Lights

52

Page 53: ENEI16 - WebGL with Three.js

Directional Lights

53

Page 54: ENEI16 - WebGL with Three.js

Shading

54Smooth shading

(per pixel)

Flat shading

(per vertex)

Page 55: ENEI16 - WebGL with Three.js

Shading

55Smooth shading

(per pixel)

Flat shading

(per vertex)

Page 56: ENEI16 - WebGL with Three.js

Materials

56

Page 57: ENEI16 - WebGL with Three.js

Materials

57

Page 58: ENEI16 - WebGL with Three.js

Materials

58

Standard

(PBR)

Phong Lambert Basic

Page 59: ENEI16 - WebGL with Three.js

Basic Material

59

•Simple material

• Supports coloring

• Transparency

• Alpha maps

• No lighting

Page 60: ENEI16 - WebGL with Three.js

Lambert Material

60

• Everything that is supported in basic material

• Per vertex lighting

• Environment mapping

Page 61: ENEI16 - WebGL with Three.js

Phong Material

61

• Per pixel lighting

• Specular component

• Normal maps

• Specular maps

• Displacement maps

Page 62: ENEI16 - WebGL with Three.js

PBR Material

62

• Metalness and roughness

• Based on a physical model

• Allows designers to collect material characteristics from the real world

• Clear coat

Page 63: ENEI16 - WebGL with Three.js

External files

63

Page 64: ENEI16 - WebGL with Three.js

Textures

64

Page 65: ENEI16 - WebGL with Three.js

Textures

65

Page 66: ENEI16 - WebGL with Three.js

UV Mapping

66

UV mapping is the process of projecting a 2D image to a 3D model surface

Page 67: ENEI16 - WebGL with Three.js

Normal Material

67

Page 68: ENEI16 - WebGL with Three.js

Normal Mapping

68

Page 69: ENEI16 - WebGL with Three.js

Normal Mapping

69

Page 70: ENEI16 - WebGL with Three.js

Normal Mapping

70Original w/ Normal map

Page 71: ENEI16 - WebGL with Three.js

Normal Mapping

71Original

4M Triangles

Simplified

500 Triangles

Simplified

+ Normal map

Page 72: ENEI16 - WebGL with Three.js

Specular mapping

72

Page 73: ENEI16 - WebGL with Three.js

Specular mapping

73

Page 74: ENEI16 - WebGL with Three.js

Displacement mapping

74

Page 75: ENEI16 - WebGL with Three.js

Displacement mapping

75

=+

Page 76: ENEI16 - WebGL with Three.js

Displacement mapping

76

Original Normal

Mapping

Displacement

Mapping

Page 77: ENEI16 - WebGL with Three.js

Displacement mapping

77

Page 78: ENEI16 - WebGL with Three.js

Tesselation

78

Page 79: ENEI16 - WebGL with Three.js

Tesselation

79

• Consists in repeatedly subdividing the a geometry into a finer mesh

• The GPU generates more vertices and triangles dynamically

• Used with displacement mapping to improve object quality dynamically

Page 80: ENEI16 - WebGL with Three.js

Tesselation

80

Page 81: ENEI16 - WebGL with Three.js

Tesselation

81

Page 82: ENEI16 - WebGL with Three.js

Video Textures

82

Page 83: ENEI16 - WebGL with Three.js

Video Textures

83

Page 84: ENEI16 - WebGL with Three.js

Shadows

84

Page 85: ENEI16 - WebGL with Three.js

Shadows

85

Page 86: ENEI16 - WebGL with Three.js

Shadow map (threejs.org/examples/webgl_shadowmap.html)

86

Page 87: ENEI16 - WebGL with Three.js

ShadowsPoint, directional and spot ligths can cast shadows

87

Page 88: ENEI16 - WebGL with Three.js

Cube mapping

88

Page 89: ENEI16 - WebGL with Three.js

Cube mapping

89

Page 90: ENEI16 - WebGL with Three.js

Cube mapping

90

Page 91: ENEI16 - WebGL with Three.js

Reflections

91

Page 92: ENEI16 - WebGL with Three.js

Reflections

92

Page 93: ENEI16 - WebGL with Three.js

Refractions

93

Page 94: ENEI16 - WebGL with Three.js

Refractions

94

Page 95: ENEI16 - WebGL with Three.js

Cube cameras

95

Page 96: ENEI16 - WebGL with Three.js

Raycaster

96

Page 97: ENEI16 - WebGL with Three.js

Raycaster

97

Page 98: ENEI16 - WebGL with Three.js

Raycaster

98

Page 99: ENEI16 - WebGL with Three.js

WebVR

99

Page 100: ENEI16 - WebGL with Three.js

WebVR (w3c.github.io/webvr/)

• WebVR is an JavaScript API that provides access to Virtual Reality devices, in your browser.

• WebVR is still an experimental feature

• Special builds of browsers are required for now

100

Page 101: ENEI16 - WebGL with Three.js

WebVR

101

Page 102: ENEI16 - WebGL with Three.js

WebVR

102

Microsoft Edge

(Announced)

Google Chrome

WebVR 1.1

Mozilla Firefox

WebVR 1.1

Samsung Gear VR Browser

Page 103: ENEI16 - WebGL with Three.js

WebVR

103

Page 104: ENEI16 - WebGL with Three.js

WebVR

104

Page 105: ENEI16 - WebGL with Three.js

Performance tips• Polygon count

• Reduce polygon count and use normal maps

• Buffered Geometry vs Geometry

• Material performance

• Basic and Lambert materials peform better

• Object count

• Merge geometries when possible

105

Page 106: ENEI16 - WebGL with Three.js

Whats next?

106

Page 107: ENEI16 - WebGL with Three.js

A-Frame• VR Web Apps written in HTML

• Entity-component ecosystem

• Built on top of three.js

107

Page 108: ENEI16 - WebGL with Three.js

WebGL 2.0• Based on OpenGL 3 ES

108

Page 109: ENEI16 - WebGL with Three.js

Questions?

109

Page 110: ENEI16 - WebGL with Three.js

ReferencesTHREE.JS - http://threejs.org/ (September 18, 2016)

AlteredQualia - http://alteredqualia.com/ (September 18, 2016)

Open.gl - https://open.gl/drawing (October 04, 2016)

OpenGL Tutorial - http://www.opengl-tutorial.org/ (October 04, 2016)

FlatIcon - http://www.flaticon.com/ (October 04, 2016)

nunuStudio - https://github.com/tentone/nunustudio (October 05, 2016)

Acko.net - https://acko.net/ (October 05, 2016)

Oculus - https://developer.oculus.com/webvr/ (October 05, 2016)

NVIDIA - http://www.nvidia.com/object/tessellation.html (October 05 2016)

110