SlideShare a Scribd company logo
1 of 24
an introduction to
WebGL
tony parisi
january 23, 2014
about me
serial entrepreneur
founder, Vizi
co-organizer, SF WebGL Meetup
co-creator, VRML and X3D web standards
designer, glTF
SF WebGL Meetup
author
http://www.meetup.com/WebGL-Developers-Meetup/
contact information
tparisi@gmail.com
skype: auradeluxe
http://twitter.com/auradeluxe
p://www.tonyparisi.com/
http://www.learningwebgl.com/

book source code
https://github.com/tparisi/WebGLBook
https://github.com/tparisi/Programming3DApplications
htt

get the books!
WebGL: Up and Running
http://www.amazon.com/dp/144932357X
Programming 3D Applications with HTML and WebGL
http://www.amazon.com/Programming-ApplicationsHTML5-WebGL-Visualization/dp/1449362966

http://www.tonyparisi.com

1/22/20
14
we live in a 3D world

image: http://www.clermonttaichi.com/

http://www.tonyparisi.com

1/22/20
14
our media is also 3D

(though it’s usually
rendered on flat screens)

http://www.tonyparisi.com

1/22/20
14
built with dedicated computers
and expensive software…

http://www.tonyparisi.com

1/22/20
14
until now.

100,000 Stars Google Experiment
http://workshop.chromeexperiments.com/stars/

http://www.tonyparisi.com

1/22/20
14
breakthrough applications

60FPS

ported in 5 days Unreal native C++ engine -> JavaScriptEmscripten + asm.js
http://www.tonyparisi.com

1/22/20
14
the 3D API standard for the
web
OpenGL ES™ in a browser
JavaScript API bindings
shipped since early 2011
supported in all modern browsers
•
•
•
•
•
•

desktop Safari, Firefox, Chrome, Opera, Internet Explorer
iOS mobile Safari – iAds only
Android – mobile Chrome, mobile Firefox
Amazon Silk Browser, Kindle Fire OS
Blackberry, Tizen, Firefox OS
Surface (Windows 8.1)

over 1B served
http://www.tonyparisi.com

1/22/20
14
how WebGL works
JavaScript drawing API
draw to a canvas element using a special context
low-level drawing – buffers, primitives, textures and shaders
accelerated by graphics hardware (GPU)
can draw 2D as well as 3D graphics

there is no file format; no markup language; no DOM.

http://www.tonyparisi.com

1/22/20
14
a simple WebGL program
1. create a <canvas> element
2. obtain a drawing context
3. initialize the viewport

4. create one or more buffers
5. create one or more matrices
6. create one or more shaders

7. initialize the shaders
8. draw one or more primitives

http://www.tonyparisi.com

1/22/20
14
create the canvas, context and
viewport
function initWebGL(canvas) {
var gl = null;
var msg = "Your browser does not support WebGL, " +
"or it is not enabled by default.";
try
{
gl = canvas.getContext(“webgl");
}
catch (e)
{
msg = "Error creating WebGL Context!: " + e.toString();
}

detect WebGL

if (!gl)
{
alert(msg);
throw new Error(msg);
}
return gl;
}
function initViewport(gl, canvas)
{
gl.viewport(0, 0, canvas.width, canvas.height);
}

set WebGL drawing
region

http://www.tonyparisi.com

1/22/20
14
buffers and typed arrays
var vertexBuffer;
vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
var verts = [
// Front face
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
// Back face
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
…

WebGL drawing functions
use buffers of data

new low-level data type
stores arrays of floats
and ints compactly

];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(verts), gl.STATIC_DRAW);
http://www.tonyparisi.com

1/22/20
14
shaders
var vertexShaderSource =
"
"
"
"
"
"
"
"
"
"
"
"

the vertex shader
attribute vec3 vertexPos;n" +
attribute vec2 texCoord;n" +
transforms model-space
uniform mat4 modelViewMatrix;n" +
positions into screen
uniform mat4 projectionMatrix;n" +
space
varying vec2 vTexCoord;n" +
void main(void) {n" +
// Return the transformed and projected vertex valuen" +
gl_Position = projectionMatrix * modelViewMatrix * n" +
vec4(vertexPos, 1.0);n" +
// Output the texture coordinate in vTexCoordn" +
vTexCoord = texCoord;n" +
}n";
the fragment shader
outputs a color value for
each pixel

var fragmentShaderSource =
" precision mediump float;n" +
" varying vec2 vTexCoord;n" +
" uniform sampler2D uSampler;n" +
" void main(void) {n" +
" // Return the pixel color: always output whiten" +
" gl_FragColor = texture2D(uSampler, vec2(vTexCoord.s, vTexCoord.t));n" +
"}n";
http://www.tonyparisi.com

1/22/20
14
drawing
function draw(gl, obj) {
// clear the background (with black)
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.DEPTH_TEST);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

clear the canvas

// set the shader to use
gl.useProgram(shaderProgram);

set the shader

// connect up the shader parameters: vertex position, texture coordinate,
// projection/model matrices and texture
// set up the buffers
gl.bindBuffer(gl.ARRAY_BUFFER, obj.buffer);
gl.vertexAttribPointer(shaderVertexPositionAttribute, obj.vertSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, obj.texCoordBuffer);
gl.vertexAttribPointer(shaderTexCoordAttribute, obj.texCoordSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, obj.indices);
gl.uniformMatrix4fv(shaderProjectionMatrixUniform, false, projectionMatrix);
gl.uniformMatrix4fv(shaderModelViewMatrixUniform, false, modelViewMatrix);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, webGLTexture);
gl.uniform1i(shaderSamplerUniform, 0);
// draw the object
gl.drawElements(obj.primtype, obj.nIndices, gl.UNSIGNED_SHORT, 0);
}

set up buffers for
vertices and
texture
coordinates
pass transform
and projection
matrices
to the shader
set the texture and pass to
the shader
draw the object

http://www.tonyparisi.com

1/22/20
14
WT
F

?
http://www.tonyparisi.com

1/22/20
14
engines and frameworks
game engines/IDEs
Goo
Enginehttp://www.gootechnologies.c
om/

rendering libraries/frameworks
Three.js
http://threejs.org/
SceneJS

Verold http://verold.com/
Turbulenz https://turbulenz.com/
PlayCanvas
http://www.playcanvas.com/
Artillery Engine
https://artillery.com/

http://scenejs.org/
BabylonJS
http://www.babylonjs.com/
Vizi
https://github.com/tparisi/Vizi
Voodoo.js
http://www.voodoojs.com/

PhiloGL
Sketchfab https://sketchfab.com/

http://www.senchalabs.org/philogl/

Unreal… ?

tQuery
http://jeromeetienne.github.io/tquery/

Unity… !?
http://www.tonyparisi.com

1/22/20
14
three.js:
a JavaScript 3D engine
https://github.com/mrdoob/three.js/

the most popular WebGL library
renderer = new THREE.WebGLRenderer( { canvas: canvas, antialias: true } );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 45, canvas.width /
canvas.height, 1, 4000 );
scene.add(camera);
var light = new THREE.DirectionalLight( 0xffffff, 1.5);
scene.add( light );
var mapUrl ="../images/webgl-logo-256.jpg“;
var map = THREE.ImageUtils.loadTexture(mapUrl );
var material = new THREE.MeshPhongMaterial({ map: map });
var geometry = new THREE.CubeGeometry(2, 2, 2);
cube = new THREE.Mesh(geometry, material);
scene.add( cube );

can render to
WebGL,
2D canvas, SVG
and CSS3
represents
WebGL at a
high level using
standard
3D graphics
concepts
http://www.tonyparisi.com

1/22/20
14
three.js flagship projects

http://www.tonyparisi.com

1/22/20
14
3D animation
requestAnimationFrame()
http://www.paulirish.com/2011/requestanimationframe-forsmart-animating/

with JavaScript we can animate anything:
materials, lights, textures…. shaders
three.js has support for key frames, morphs and skins

Tween.js – simple tweening library
https://github.com/sole/tween.js/
var tween = new TWEEN.Tween( { x: 50, y: 0 } )
.to( { x: 400 }, 2000 )
.easing( TWEEN.Easing.Elastic.InOut )
.start();
function animate() {
requestAnimationFrame( animate );
TWEEN.update();
}

create and
start tween

call TWEEN.update()
in your run loop
http://www.tonyparisi.com

1/22/20
14
the content pipeline
still pretty crude – tools and converters under
development
sample work flows
A. OBJ  Three.js JSON with Python command-line tool
•

load into Three.js application; hand-layout, hand-light, handanimate

B. MD2/MD5  Three.js JSON with online tool
•

load into Three.js application; hand-layout, hand-light

C. COLLADA (full scene)
1. export to from Maya, 3ds Max, Blender, Sketchup
2. files contain scene layout, cameras, lights and animations – no
need to do it by hand
3. import into Three.js application and use
4. but COLLADA files are big to download and slow to parse
1/22/20
http://www.tonyparisi.com

14
glTF: a “JPEG for 3D”
full-featured: scene layout, cameras, lights, animations

JSON for scene structure; binary buffers for model data

model from 3drt.com
http://www.tonyparisi.com

1/22/20
14
cross-browser WebGL
desktop
• Safari, Firefox, Chrome, Opera,
Internet Explorer

Building a game or app?
use Ludei
http://ludei.com/

mobile
• iOS - mobile Safari – iAds only
• Android – mobile Chrome
• Amazon Silk, Kindle Fire OS
• Blackberry, Tizen, Firefox OS

http://www.tonyparisi.com

1/22/20
14
2014: the tipping point
Microsoft is fully supporting WebGL
Kindle Fire HDX: at $229, the 7” is probably the best
multimedia device deal on the planet… thanks in part to
WebGL.
Sony built the whole PS4 user interface out of WebGL.
4.2M seats in one whack… and growing.
the 2013 NORAD Tracks Santa site saw 48.8% WebGL
success across all browsers & platforms for 20M
visitors, an increase of 146% over 2012.
Opera Devices SDK – WebGL coming soon to a Bang &
Olufsen TV near you!
tonight: one of the best-ever-attended SFHTML5
meetups. We broke the group’s record for wait list (over
500)!
http://www.tonyparisi.com

1/22/20
14
stay in touch…
contact information
tparisi@gmail.com
skype: auradeluxe
http://twitter.com/auradeluxe
p://www.tonyparisi.com/
http://www.learningwebgl.com/

SF WebGL Meetup
http://www.meetup.com/WebGL-Developers-Meetup/
htt

book source code
https://github.com/tparisi/WebGLBook
https://github.com/tparisi/Programming3DApplications

get Vizi
https://github.com/tparisi/Vizi
get the books!
WebGL: Up and Running
http://www.amazon.com/dp/144932357X
Programming 3D Applications with HTML and WebGL
http://www.amazon.com/Programming-ApplicationsHTML5-WebGL-Visualization/dp/1449362966
http://www.tonyparisi.com

1/22/20
14

More Related Content

Viewers also liked

Computação Gráfica - Introdução ao OpenGL
Computação Gráfica - Introdução ao OpenGLComputação Gráfica - Introdução ao OpenGL
Computação Gráfica - Introdução ao OpenGL
Tony Alexander Hild
 
Slides prakti gsvc finals-110518
Slides prakti gsvc finals-110518Slides prakti gsvc finals-110518
Slides prakti gsvc finals-110518
jdoughertydc
 
Progetto involucro. Prof.Iovino
Progetto involucro.  Prof.IovinoProgetto involucro.  Prof.Iovino
Progetto involucro. Prof.Iovino
Antonella0_91
 
Rpl 7 ppl dan metrik proyek (2)
Rpl 7 ppl dan metrik proyek (2)Rpl 7 ppl dan metrik proyek (2)
Rpl 7 ppl dan metrik proyek (2)
Komang Yogi
 
2nd sem portfolio
2nd sem portfolio2nd sem portfolio
2nd sem portfolio
nutcrackar1
 
προσομοίωση πειράματος φυσικής σε γλώσσα Scratch
προσομοίωση πειράματος φυσικής σε γλώσσα Scratchπροσομοίωση πειράματος φυσικής σε γλώσσα Scratch
προσομοίωση πειράματος φυσικής σε γλώσσα Scratch
georgefyttas
 
Decisions in communication
Decisions in communicationDecisions in communication
Decisions in communication
TalinChirinian
 

Viewers also liked (17)

Introduction to Webgl by Rachel Prudden
Introduction to Webgl by Rachel PruddenIntroduction to Webgl by Rachel Prudden
Introduction to Webgl by Rachel Prudden
 
Desenvolvimento de ambientes 3D para Web usando Three JS
Desenvolvimento de ambientes 3D para Web usando Three JSDesenvolvimento de ambientes 3D para Web usando Three JS
Desenvolvimento de ambientes 3D para Web usando Three JS
 
3D Image visualization
3D Image visualization3D Image visualization
3D Image visualization
 
Introduction to WebGL and Three.js
Introduction to WebGL and Three.jsIntroduction to WebGL and Three.js
Introduction to WebGL and Three.js
 
Computação Gráfica - Introdução ao OpenGL
Computação Gráfica - Introdução ao OpenGLComputação Gráfica - Introdução ao OpenGL
Computação Gráfica - Introdução ao OpenGL
 
Slides prakti gsvc finals-110518
Slides prakti gsvc finals-110518Slides prakti gsvc finals-110518
Slides prakti gsvc finals-110518
 
BillingViews Facebook Success Index
BillingViews Facebook Success IndexBillingViews Facebook Success Index
BillingViews Facebook Success Index
 
Email Advertising
Email AdvertisingEmail Advertising
Email Advertising
 
Portafolio
PortafolioPortafolio
Portafolio
 
Progetto involucro. Prof.Iovino
Progetto involucro.  Prof.IovinoProgetto involucro.  Prof.Iovino
Progetto involucro. Prof.Iovino
 
Proyecto investigación
Proyecto investigación Proyecto investigación
Proyecto investigación
 
Rpl 7 ppl dan metrik proyek (2)
Rpl 7 ppl dan metrik proyek (2)Rpl 7 ppl dan metrik proyek (2)
Rpl 7 ppl dan metrik proyek (2)
 
2nd sem portfolio
2nd sem portfolio2nd sem portfolio
2nd sem portfolio
 
προσομοίωση πειράματος φυσικής σε γλώσσα Scratch
προσομοίωση πειράματος φυσικής σε γλώσσα Scratchπροσομοίωση πειράματος φυσικής σε γλώσσα Scratch
προσομοίωση πειράματος φυσικής σε γλώσσα Scratch
 
X22686506
X22686506X22686506
X22686506
 
History lesson
History lessonHistory lesson
History lesson
 
Decisions in communication
Decisions in communicationDecisions in communication
Decisions in communication
 

More from Tony Parisi

More from Tony Parisi (20)

The New Fine Arts
The New Fine ArtsThe New Fine Arts
The New Fine Arts
 
Face the Future: Computing in an Augmented World
Face the Future: Computing in an Augmented WorldFace the Future: Computing in an Augmented World
Face the Future: Computing in an Augmented World
 
Powering the VR/AR Ecosystem 2017-01-17
Powering the VR/AR Ecosystem 2017-01-17Powering the VR/AR Ecosystem 2017-01-17
Powering the VR/AR Ecosystem 2017-01-17
 
WebVR: Developing for the Immersive Web
WebVR: Developing for the Immersive WebWebVR: Developing for the Immersive Web
WebVR: Developing for the Immersive Web
 
Introduction to WebVR Autodesk Forge 2016
Introduction to WebVR Autodesk Forge 2016Introduction to WebVR Autodesk Forge 2016
Introduction to WebVR Autodesk Forge 2016
 
WebVR Ecosystem and API Update
WebVR Ecosystem and API UpdateWebVR Ecosystem and API Update
WebVR Ecosystem and API Update
 
Foundations of the Immersive Web
Foundations of the Immersive WebFoundations of the Immersive Web
Foundations of the Immersive Web
 
The Immersive Web
The Immersive WebThe Immersive Web
The Immersive Web
 
Virtually Anyone
Virtually AnyoneVirtually Anyone
Virtually Anyone
 
React-VR: An Early Experiment with React and WebGL for VR Development
React-VR: An Early Experiment with React and WebGL for VR DevelopmentReact-VR: An Early Experiment with React and WebGL for VR Development
React-VR: An Early Experiment with React and WebGL for VR Development
 
WebGL: The Next Generation
WebGL:  The Next GenerationWebGL:  The Next Generation
WebGL: The Next Generation
 
Vrml, or There and Back Again
Vrml, or There and Back AgainVrml, or There and Back Again
Vrml, or There and Back Again
 
The Coming Distribution War
The Coming Distribution WarThe Coming Distribution War
The Coming Distribution War
 
Browser-Based Virtual Reality April 2015
Browser-Based Virtual Reality April 2015Browser-Based Virtual Reality April 2015
Browser-Based Virtual Reality April 2015
 
glTF and the WebGL Art Pipeline March 2015
glTF and the WebGL Art Pipeline March 2015glTF and the WebGL Art Pipeline March 2015
glTF and the WebGL Art Pipeline March 2015
 
An Introduction to Web VR January 2015
An Introduction to Web VR January 2015An Introduction to Web VR January 2015
An Introduction to Web VR January 2015
 
Up And Running With Web VR Fall 2014
Up And Running With Web VR Fall 2014Up And Running With Web VR Fall 2014
Up And Running With Web VR Fall 2014
 
Hacking Reality: Browser-Based VR with HTML5
Hacking Reality: Browser-Based VR with HTML5Hacking Reality: Browser-Based VR with HTML5
Hacking Reality: Browser-Based VR with HTML5
 
WebGL, WebVR and the Metaverse
WebGL, WebVR and the MetaverseWebGL, WebVR and the Metaverse
WebGL, WebVR and the Metaverse
 
And Who Shall Control the Metaverse?
And Who Shall Control the Metaverse?And Who Shall Control the Metaverse?
And Who Shall Control the Metaverse?
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 

An Introduction to WebGL - SFHTML5 Talk January 2014

  • 1. an introduction to WebGL tony parisi january 23, 2014
  • 2. about me serial entrepreneur founder, Vizi co-organizer, SF WebGL Meetup co-creator, VRML and X3D web standards designer, glTF SF WebGL Meetup author http://www.meetup.com/WebGL-Developers-Meetup/ contact information tparisi@gmail.com skype: auradeluxe http://twitter.com/auradeluxe p://www.tonyparisi.com/ http://www.learningwebgl.com/ book source code https://github.com/tparisi/WebGLBook https://github.com/tparisi/Programming3DApplications htt get the books! WebGL: Up and Running http://www.amazon.com/dp/144932357X Programming 3D Applications with HTML and WebGL http://www.amazon.com/Programming-ApplicationsHTML5-WebGL-Visualization/dp/1449362966 http://www.tonyparisi.com 1/22/20 14
  • 3. we live in a 3D world image: http://www.clermonttaichi.com/ http://www.tonyparisi.com 1/22/20 14
  • 4. our media is also 3D (though it’s usually rendered on flat screens) http://www.tonyparisi.com 1/22/20 14
  • 5. built with dedicated computers and expensive software… http://www.tonyparisi.com 1/22/20 14
  • 6. until now. 100,000 Stars Google Experiment http://workshop.chromeexperiments.com/stars/ http://www.tonyparisi.com 1/22/20 14
  • 7. breakthrough applications 60FPS ported in 5 days Unreal native C++ engine -> JavaScriptEmscripten + asm.js http://www.tonyparisi.com 1/22/20 14
  • 8. the 3D API standard for the web OpenGL ES™ in a browser JavaScript API bindings shipped since early 2011 supported in all modern browsers • • • • • • desktop Safari, Firefox, Chrome, Opera, Internet Explorer iOS mobile Safari – iAds only Android – mobile Chrome, mobile Firefox Amazon Silk Browser, Kindle Fire OS Blackberry, Tizen, Firefox OS Surface (Windows 8.1) over 1B served http://www.tonyparisi.com 1/22/20 14
  • 9. how WebGL works JavaScript drawing API draw to a canvas element using a special context low-level drawing – buffers, primitives, textures and shaders accelerated by graphics hardware (GPU) can draw 2D as well as 3D graphics there is no file format; no markup language; no DOM. http://www.tonyparisi.com 1/22/20 14
  • 10. a simple WebGL program 1. create a <canvas> element 2. obtain a drawing context 3. initialize the viewport 4. create one or more buffers 5. create one or more matrices 6. create one or more shaders 7. initialize the shaders 8. draw one or more primitives http://www.tonyparisi.com 1/22/20 14
  • 11. create the canvas, context and viewport function initWebGL(canvas) { var gl = null; var msg = "Your browser does not support WebGL, " + "or it is not enabled by default."; try { gl = canvas.getContext(“webgl"); } catch (e) { msg = "Error creating WebGL Context!: " + e.toString(); } detect WebGL if (!gl) { alert(msg); throw new Error(msg); } return gl; } function initViewport(gl, canvas) { gl.viewport(0, 0, canvas.width, canvas.height); } set WebGL drawing region http://www.tonyparisi.com 1/22/20 14
  • 12. buffers and typed arrays var vertexBuffer; vertexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); var verts = [ // Front face -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, // Back face -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, … WebGL drawing functions use buffers of data new low-level data type stores arrays of floats and ints compactly ]; gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(verts), gl.STATIC_DRAW); http://www.tonyparisi.com 1/22/20 14
  • 13. shaders var vertexShaderSource = " " " " " " " " " " " " the vertex shader attribute vec3 vertexPos;n" + attribute vec2 texCoord;n" + transforms model-space uniform mat4 modelViewMatrix;n" + positions into screen uniform mat4 projectionMatrix;n" + space varying vec2 vTexCoord;n" + void main(void) {n" + // Return the transformed and projected vertex valuen" + gl_Position = projectionMatrix * modelViewMatrix * n" + vec4(vertexPos, 1.0);n" + // Output the texture coordinate in vTexCoordn" + vTexCoord = texCoord;n" + }n"; the fragment shader outputs a color value for each pixel var fragmentShaderSource = " precision mediump float;n" + " varying vec2 vTexCoord;n" + " uniform sampler2D uSampler;n" + " void main(void) {n" + " // Return the pixel color: always output whiten" + " gl_FragColor = texture2D(uSampler, vec2(vTexCoord.s, vTexCoord.t));n" + "}n"; http://www.tonyparisi.com 1/22/20 14
  • 14. drawing function draw(gl, obj) { // clear the background (with black) gl.clearColor(0.0, 0.0, 0.0, 1.0); gl.enable(gl.DEPTH_TEST); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); clear the canvas // set the shader to use gl.useProgram(shaderProgram); set the shader // connect up the shader parameters: vertex position, texture coordinate, // projection/model matrices and texture // set up the buffers gl.bindBuffer(gl.ARRAY_BUFFER, obj.buffer); gl.vertexAttribPointer(shaderVertexPositionAttribute, obj.vertSize, gl.FLOAT, false, 0, 0); gl.bindBuffer(gl.ARRAY_BUFFER, obj.texCoordBuffer); gl.vertexAttribPointer(shaderTexCoordAttribute, obj.texCoordSize, gl.FLOAT, false, 0, 0); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, obj.indices); gl.uniformMatrix4fv(shaderProjectionMatrixUniform, false, projectionMatrix); gl.uniformMatrix4fv(shaderModelViewMatrixUniform, false, modelViewMatrix); gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_2D, webGLTexture); gl.uniform1i(shaderSamplerUniform, 0); // draw the object gl.drawElements(obj.primtype, obj.nIndices, gl.UNSIGNED_SHORT, 0); } set up buffers for vertices and texture coordinates pass transform and projection matrices to the shader set the texture and pass to the shader draw the object http://www.tonyparisi.com 1/22/20 14
  • 16. engines and frameworks game engines/IDEs Goo Enginehttp://www.gootechnologies.c om/ rendering libraries/frameworks Three.js http://threejs.org/ SceneJS Verold http://verold.com/ Turbulenz https://turbulenz.com/ PlayCanvas http://www.playcanvas.com/ Artillery Engine https://artillery.com/ http://scenejs.org/ BabylonJS http://www.babylonjs.com/ Vizi https://github.com/tparisi/Vizi Voodoo.js http://www.voodoojs.com/ PhiloGL Sketchfab https://sketchfab.com/ http://www.senchalabs.org/philogl/ Unreal… ? tQuery http://jeromeetienne.github.io/tquery/ Unity… !? http://www.tonyparisi.com 1/22/20 14
  • 17. three.js: a JavaScript 3D engine https://github.com/mrdoob/three.js/ the most popular WebGL library renderer = new THREE.WebGLRenderer( { canvas: canvas, antialias: true } ); scene = new THREE.Scene(); camera = new THREE.PerspectiveCamera( 45, canvas.width / canvas.height, 1, 4000 ); scene.add(camera); var light = new THREE.DirectionalLight( 0xffffff, 1.5); scene.add( light ); var mapUrl ="../images/webgl-logo-256.jpg“; var map = THREE.ImageUtils.loadTexture(mapUrl ); var material = new THREE.MeshPhongMaterial({ map: map }); var geometry = new THREE.CubeGeometry(2, 2, 2); cube = new THREE.Mesh(geometry, material); scene.add( cube ); can render to WebGL, 2D canvas, SVG and CSS3 represents WebGL at a high level using standard 3D graphics concepts http://www.tonyparisi.com 1/22/20 14
  • 19. 3D animation requestAnimationFrame() http://www.paulirish.com/2011/requestanimationframe-forsmart-animating/ with JavaScript we can animate anything: materials, lights, textures…. shaders three.js has support for key frames, morphs and skins Tween.js – simple tweening library https://github.com/sole/tween.js/ var tween = new TWEEN.Tween( { x: 50, y: 0 } ) .to( { x: 400 }, 2000 ) .easing( TWEEN.Easing.Elastic.InOut ) .start(); function animate() { requestAnimationFrame( animate ); TWEEN.update(); } create and start tween call TWEEN.update() in your run loop http://www.tonyparisi.com 1/22/20 14
  • 20. the content pipeline still pretty crude – tools and converters under development sample work flows A. OBJ  Three.js JSON with Python command-line tool • load into Three.js application; hand-layout, hand-light, handanimate B. MD2/MD5  Three.js JSON with online tool • load into Three.js application; hand-layout, hand-light C. COLLADA (full scene) 1. export to from Maya, 3ds Max, Blender, Sketchup 2. files contain scene layout, cameras, lights and animations – no need to do it by hand 3. import into Three.js application and use 4. but COLLADA files are big to download and slow to parse 1/22/20 http://www.tonyparisi.com 14
  • 21. glTF: a “JPEG for 3D” full-featured: scene layout, cameras, lights, animations JSON for scene structure; binary buffers for model data model from 3drt.com http://www.tonyparisi.com 1/22/20 14
  • 22. cross-browser WebGL desktop • Safari, Firefox, Chrome, Opera, Internet Explorer Building a game or app? use Ludei http://ludei.com/ mobile • iOS - mobile Safari – iAds only • Android – mobile Chrome • Amazon Silk, Kindle Fire OS • Blackberry, Tizen, Firefox OS http://www.tonyparisi.com 1/22/20 14
  • 23. 2014: the tipping point Microsoft is fully supporting WebGL Kindle Fire HDX: at $229, the 7” is probably the best multimedia device deal on the planet… thanks in part to WebGL. Sony built the whole PS4 user interface out of WebGL. 4.2M seats in one whack… and growing. the 2013 NORAD Tracks Santa site saw 48.8% WebGL success across all browsers & platforms for 20M visitors, an increase of 146% over 2012. Opera Devices SDK – WebGL coming soon to a Bang & Olufsen TV near you! tonight: one of the best-ever-attended SFHTML5 meetups. We broke the group’s record for wait list (over 500)! http://www.tonyparisi.com 1/22/20 14
  • 24. stay in touch… contact information tparisi@gmail.com skype: auradeluxe http://twitter.com/auradeluxe p://www.tonyparisi.com/ http://www.learningwebgl.com/ SF WebGL Meetup http://www.meetup.com/WebGL-Developers-Meetup/ htt book source code https://github.com/tparisi/WebGLBook https://github.com/tparisi/Programming3DApplications get Vizi https://github.com/tparisi/Vizi get the books! WebGL: Up and Running http://www.amazon.com/dp/144932357X Programming 3D Applications with HTML and WebGL http://www.amazon.com/Programming-ApplicationsHTML5-WebGL-Visualization/dp/1449362966 http://www.tonyparisi.com 1/22/20 14