SlideShare a Scribd company logo
1 of 43
Introduction to WebGL and Three.js

James Williams

James.L.Williams@gmail.com - Rearden Commerce
About Me
• Author of Learning HTML5
  Game Programming
• Google+: +JamesWilliams
• Twitter: @ecspike
• http://jameswilliams.be/blog


                                                         James Williams   2
                         James.L.Williams@gmail.com – Rearden Commerce
Agenda - WebGL
•   What are WebGL and Three.js?
•   Lighting, Shaders, and Materials
•   Creating Meshes
•   GLSL
•   Animation
•   Debugging
                                                           James Williams   3
                           James.L.Williams@gmail.com – Rearden Commerce
What is WebGL?
•   Low-level 3D graphics context
•   Hardware accelerated
•   Supported by most modern browsers
•   Syntax is based on OpenGL ES 2.0



                                                         James Williams   4
                         James.L.Williams@gmail.com – Rearden Commerce
Getting a WebGLContext
• Hook to draw on the canvas
 var canvas = document.getElementById(“c”);
 var ctx = c.getContext(“experimental-webgl”);




                                                          James Williams   5
                          James.L.Williams@gmail.com – Rearden Commerce
What is Three.js?
• Abstraction layer over WebGL
• 3D scenegraph library
• Capable of rendering to
  – Canvas2D, WebGL, or SVG
• Exporters for popular 3D formats
• http://github.com/mrdoob/three.js

                                                         James Williams   6
                         James.L.Williams@gmail.com – Rearden Commerce
Initializing Three.js
function init() {
    var HEIGHT = 480, WIDTH = 640;
    // create a renderer, camera, and scene
     renderer = new THREE.WebGLRenderer();
     renderer.setSize (WIDTH, HEIGHT);
     camera = /* truncated */
     // create scene
     scene = new THREE.Scene();
     // add objects to scene
    elem.appendChild(render.domElement);
}
                                                                  James Williams   7
                                  James.L.Williams@gmail.com – Rearden Commerce
Camera
•   Eye point
•   Field of vision
•   Near / Far planes
•   Target(LookAt) point
•   Up vector
•   Advanced Camera types
                                                        James Williams   8
                        James.L.Williams@gmail.com – Rearden Commerce
Creating Meshes
• Geometry vs Mesh
• Built-in geometries
• Vertex
 new THREE.Vertex(
    new Three.Vector3(0.0, 1.0, 0.0)
 );
• Face
 new THREE.Face3(0,1,2);

                                                           James Williams   9
                           James.L.Williams@gmail.com – Rearden Commerce
Creating Meshes
geometry = new THREE.Geometry();
geometry.vertices.push(vertex1);
geometry.vertices.push(vertex2);
geometry.vertices.push(vertex3);
geometry.faces.push(face1);
var triangle = new THREE.Mesh(geometry,
     new THREE.MeshBasicMaterial( {
      color: 0x00ff00 }
   )
);


                                                               James Williams   10
                               James.L.Williams@gmail.com – Rearden Commerce
Creating Meshes
plane = new THREE.Mesh(
   new THREE.PlaneGeometry( 200, 200 ),
   new THREE.MeshBasicMaterial( { color:
     0xe0e0e0 }
   )
);
scene.add( plane );
scene.add(triangle);




                                                               James Williams   11
                               James.L.Williams@gmail.com – Rearden Commerce
Demo




                                  James Williams   12
  James.L.Williams@gmail.com – Rearden Commerce
Lighting
•   Ambient
•   Point
•   Directional
•   SpotLight
    new THREE.AmbientLight(hexColor);
    new THREE.PointLight(hexColor, [intensity], [distance]);




                                                                   James Williams   13
                                   James.L.Williams@gmail.com – Rearden Commerce
Shading
•   Flat
•   Lambertian
•   Gouraud
•   Phong



                                                     James Williams   14
                     James.L.Williams@gmail.com – Rearden Commerce
Materials
•   MeshBasicMaterial
•   MeshLambertMaterial
•   MeshPhongMaterial
•   MeshShaderMaterial



                                                          James Williams   15
                          James.L.Williams@gmail.com – Rearden Commerce
Demo




                                  James Williams   16
  James.L.Williams@gmail.com – Rearden Commerce
Texturing
• Can load from images or use canvas data
• Basic shapes precalculate texture coordinates




                                                         James Williams   17
                         James.L.Williams@gmail.com – Rearden Commerce
2D Texture Coordinates
(0,0)                                                      (1,1)




(0,1)                                                      (1,0)
                                                   James Williams   18
                   James.L.Williams@gmail.com – Rearden Commerce
Texturing Example
var texture = THREE.ImageUtils.loadTexture(
   "200407-bluemarble.jpg" );
var material = new THREE.MeshBasicMaterial(
   { color: 0xFFFFFF,
     ambient: 0xFFFFFF, map:texture } );


sphere = new THREE.Mesh(
   new THREE.SphereGeometry(32, 32, 32), material
);


scene.add(sphere);
                                                               James Williams   19
                               James.L.Williams@gmail.com – Rearden Commerce
Demo




                                  James Williams   20
  James.L.Williams@gmail.com – Rearden Commerce
Loading Models
function drawCube() {
    var loader = new THREE.JSONLoader();
    loader.load( {model: "cube.js", callback: createScene1 });
}


function createScene1(obj) {
    obj.materials[0][0].shading = THREE.FlatShading;
    mesh = THREE.SceneUtils.addMesh( scene, obj, 250, 400, 0,
0, 0, 0, 0,obj.materials[0] );
}


                                                               James Williams   21
                               James.L.Williams@gmail.com – Rearden Commerce
Demo




                                  James Williams   22
  James.L.Williams@gmail.com – Rearden Commerce
What is GLSL?
• High-level language with C-like syntax
• Targets the GPU and graphics pipeline
• A GLSL program consists of
  – fragment shader
  – vertex shader
• Content of shaders passed around as strings
• Shaders can be generated at run-time
                                                         James Williams   23
                         James.L.Williams@gmail.com – Rearden Commerce
Vertex Shaders
• Run once per vertex in a mesh
• Can alter color, position, texels, etc at that
  vertex




                                                            James Williams   24
                            James.L.Williams@gmail.com – Rearden Commerce
Example Vertex Shader
<script id="shader-vs" type="x-shader/x-vertex">
    void main(void) {
       gl_Position = projectionMatrix *
       modelViewMatrix *
         vec4(position, 1.0);
    }
</script>




                                                               James Williams   25
                               James.L.Williams@gmail.com – Rearden Commerce
Fragment(Pixel) Shaders
• Run once per pixel in a mesh
• Can produce effects such as bump and env
  mapping




                                                       James Williams   26
                       James.L.Williams@gmail.com – Rearden Commerce
Example Fragment(Pixel) Shader
<script id="shader-vs" type="x-shader/x-fragment">
    void main(void) {
       gl_FragColor = vec4(
       0.0, 1.0, 0.0, 1.0
      );
    }
</script>




                                                               James Williams   27
                               James.L.Williams@gmail.com – Rearden Commerce
Shader Demo Code
 shaderMaterial = new
     THREE.MeshShaderMaterial({
     vertexShader: $('#v').get(0).innerHTML,
     fragmentShader:$('#f').get(0).innerHTML,
     vertexColors: true
 });
/* truncated */
var triangle = new THREE.Mesh(
   geometry, shaderMaterial
);



                                                                James Williams   28
                                James.L.Williams@gmail.com – Rearden Commerce
Shader Variables
•   uniform
•   varying
•   attribute
•   Types



                                                        James Williams   29
                        James.L.Williams@gmail.com – Rearden Commerce
Constructing New Shader Types
struct MyMaterial {
    vec4 ambient;
    vec4 diffuse;
     vec4 specular;
     float shininess;
};




                                                        James Williams   30
                        James.L.Williams@gmail.com – Rearden Commerce
Communicating with Shaders
var uniforms;
uniforms = {
    time: {type:"f", value:0.0}
}
shaderMaterial = new THREE.MeshShaderMaterial({
      uniforms: uniforms,
      vertexShader:$('#v').get(0).innerHTML,
      fragmentShader:$('#f').get(0).innerHTML,
});



                                                                  James Williams   31
                                  James.L.Williams@gmail.com – Rearden Commerce
Custom Shader
uniform float time;


void main(){
    float r = cos(time);
    float g = sin(time);
    float b = tan(time);


    gl_FragColor = vec4(r, 1.0 - g , b, 1.0);
}



                                                                 James Williams   32
                                 James.L.Williams@gmail.com – Rearden Commerce
Vertex Shader for 2D texturing
/* Vertex Shader */
attribute vec4 a_position;
attribute vec2 a_texCoord;
varying vec2 v_texCoord;


void main() {
    gl_Position = a_position;
    v_texCoord = a_texCoord;
}



                                                                James Williams   33
                                James.L.Williams@gmail.com – Rearden Commerce
Fragment Shader for 2D texturing
/* Fragment Shader */
precision mediump float;
varying vec2 v_texCoord;
uniform sampler2D s_texture;


void main() {
    gl_FragColor = texture2D(s_texture, v_texCoord);
}




                                                               James Williams   34
                               James.L.Williams@gmail.com – Rearden Commerce
Animation
  Armature - 3D representation of bones, ligaments, and
  tendons
• Forward kinematics
• Inverse kinematics
• Keyframes/Morph targets




                                                             James Williams   35
                             James.L.Williams@gmail.com – Rearden Commerce
MorphTargets
var time = new Date().getTime() % duration;
var keyframe = Math.floor(time / interpol )
  + offset;
if ( keyframe != currentKeyframe ) {
    mesh.morphTargetInfluences[lastFrame]=0;
    mesh.morphTargetInfluences[currentFrame]
      =1;
    mesh.morphTargetInfluences[keyframe]=0;
    lastFrame = currentFrame;
    currentFrame = keyframe;
}

                                                                 James Williams   36
                                 James.L.Williams@gmail.com – Rearden Commerce
MorphTargets
mesh.morphTargetInfluences[ keyframe ]
 = ( time % interpol ) / interpolation;

mesh.morphTargetInfluences[ lastFrame ]
= 1 - mesh.morphTargetInfluences[keyframe];




                                                               James Williams   37
                               James.L.Williams@gmail.com – Rearden Commerce
Demo




                                  James Williams   38
  James.L.Williams@gmail.com – Rearden Commerce
Debugging WebGL




                                       James Williams   39
       James.L.Williams@gmail.com – Rearden Commerce
Stats.js
• Measures:
   – FPS - frames per second
   – MS - millis to render frame
   – MB - allocated MB
• Github:https://github.com/mrdoob/stats.js


                                                                   James Williams   40
                                   James.L.Williams@gmail.com – Rearden Commerce
Stats.js code
   var stats = new Stats()
$("body").append(stats.domElement);

//... in your render function
stats.update();




                                                                 James Williams   41
                                 James.L.Williams@gmail.com – Rearden Commerce
WebGL Inspector
•   Allows you to step through rendering
•   View assets and programs
•   Capture individual frames
•   Github: http://benvanik.github.com/WebGL-Inspector



                                                                James Williams   42
                                James.L.Williams@gmail.com – Rearden Commerce
Questions?




                                     James Williams   43
     James.L.Williams@gmail.com – Rearden Commerce

More Related Content

Viewers also liked

Web3D - Semantic standards, WebGL, HCI
Web3D - Semantic standards, WebGL, HCIWeb3D - Semantic standards, WebGL, HCI
Web3D - Semantic standards, WebGL, HCI
Victor Porof
 
Three.js 一場從2D變成3D的冒險
Three.js  一場從2D變成3D的冒險Three.js  一場從2D變成3D的冒險
Three.js 一場從2D變成3D的冒險
智遠 成
 

Viewers also liked (17)

Web3D - Semantic standards, WebGL, HCI
Web3D - Semantic standards, WebGL, HCIWeb3D - Semantic standards, WebGL, HCI
Web3D - Semantic standards, WebGL, HCI
 
The Power of WebGL - Hackeando sua GPU com JavaScript
The Power of WebGL - Hackeando sua GPU com JavaScriptThe Power of WebGL - Hackeando sua GPU com JavaScript
The Power of WebGL - Hackeando sua GPU com JavaScript
 
3D in the Browser via WebGL: It's Go Time
3D in the Browser via WebGL: It's Go Time 3D in the Browser via WebGL: It's Go Time
3D in the Browser via WebGL: It's Go Time
 
ようこそ、HTML5裏APIの世界へ - HTML5 Conference 2013
ようこそ、HTML5裏APIの世界へ - HTML5 Conference 2013ようこそ、HTML5裏APIの世界へ - HTML5 Conference 2013
ようこそ、HTML5裏APIの世界へ - HTML5 Conference 2013
 
Introduction to webGL
Introduction to webGLIntroduction to webGL
Introduction to webGL
 
WebGL - An Overview
WebGL - An OverviewWebGL - An Overview
WebGL - An Overview
 
WebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open webWebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open web
 
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
 
Starting Development for Nokia N9
Starting Development for Nokia N9Starting Development for Nokia N9
Starting Development for Nokia N9
 
Qt Design Patterns
Qt Design PatternsQt Design Patterns
Qt Design Patterns
 
Qt Workshop
Qt WorkshopQt Workshop
Qt Workshop
 
Three.js 一場從2D變成3D的冒險
Three.js  一場從2D變成3D的冒險Three.js  一場從2D變成3D的冒險
Three.js 一場從2D變成3D的冒險
 
Qt State Machine Framework
Qt State Machine FrameworkQt State Machine Framework
Qt State Machine Framework
 
Cutest technology of them all - Forum Nokia Qt Webinar December 2009
Cutest technology of them all - Forum Nokia Qt Webinar December 2009Cutest technology of them all - Forum Nokia Qt Webinar December 2009
Cutest technology of them all - Forum Nokia Qt Webinar December 2009
 
WebGL and three.js
WebGL and three.jsWebGL and three.js
WebGL and three.js
 
Qt 5 - C++ and Widgets
Qt 5 - C++ and WidgetsQt 5 - C++ and Widgets
Qt 5 - C++ and Widgets
 
Introduction to Qt
Introduction to QtIntroduction to Qt
Introduction to Qt
 

More from James Williams

Ratpack - Classy and Compact Groovy Web Apps
Ratpack - Classy and Compact Groovy Web AppsRatpack - Classy and Compact Groovy Web Apps
Ratpack - Classy and Compact Groovy Web Apps
James Williams
 
Creating Voice Powered Apps with Ribbit
Creating Voice Powered Apps with RibbitCreating Voice Powered Apps with Ribbit
Creating Voice Powered Apps with Ribbit
James Williams
 
Extending Groovys Swing User Interface in Builder to Build Richer Applications
Extending Groovys Swing User Interface in Builder to Build Richer ApplicationsExtending Groovys Swing User Interface in Builder to Build Richer Applications
Extending Groovys Swing User Interface in Builder to Build Richer Applications
James Williams
 
Boosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with GroovyBoosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with Groovy
James Williams
 

More from James Williams (17)

Intro to HTML5 Game Programming
Intro to HTML5 Game ProgrammingIntro to HTML5 Game Programming
Intro to HTML5 Game Programming
 
Ratpack - Classy and Compact Groovy Web Apps
Ratpack - Classy and Compact Groovy Web AppsRatpack - Classy and Compact Groovy Web Apps
Ratpack - Classy and Compact Groovy Web Apps
 
Introduction to Griffon
Introduction to GriffonIntroduction to Griffon
Introduction to Griffon
 
Griffon for the Enterprise
Griffon for the EnterpriseGriffon for the Enterprise
Griffon for the Enterprise
 
Game programming with Groovy
Game programming with GroovyGame programming with Groovy
Game programming with Groovy
 
Java development with MongoDB
Java development with MongoDBJava development with MongoDB
Java development with MongoDB
 
Enterprise Griffon
Enterprise GriffonEnterprise Griffon
Enterprise Griffon
 
Porting legacy apps to Griffon
Porting legacy apps to GriffonPorting legacy apps to Griffon
Porting legacy apps to Griffon
 
Using MongoDB With Groovy
Using MongoDB With GroovyUsing MongoDB With Groovy
Using MongoDB With Groovy
 
Creating Voice Powered Apps with Ribbit
Creating Voice Powered Apps with RibbitCreating Voice Powered Apps with Ribbit
Creating Voice Powered Apps with Ribbit
 
Griffon: Swing just got fun again
Griffon: Swing just got fun againGriffon: Swing just got fun again
Griffon: Swing just got fun again
 
Griffon: Swing just got fun again
Griffon: Swing just got fun againGriffon: Swing just got fun again
Griffon: Swing just got fun again
 
Extending Groovys Swing User Interface in Builder to Build Richer Applications
Extending Groovys Swing User Interface in Builder to Build Richer ApplicationsExtending Groovys Swing User Interface in Builder to Build Richer Applications
Extending Groovys Swing User Interface in Builder to Build Richer Applications
 
Boosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with GroovyBoosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with Groovy
 
Griffon: Re-imaging Desktop Java Technology
Griffon: Re-imaging Desktop Java TechnologyGriffon: Re-imaging Desktop Java Technology
Griffon: Re-imaging Desktop Java Technology
 
Android Development
Android DevelopmentAndroid Development
Android Development
 
SVCC Intro to Grails
SVCC Intro to GrailsSVCC Intro to Grails
SVCC Intro to Grails
 

Recently uploaded

Recently uploaded (20)

Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
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...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
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
 

Introduction to WebGL and Three.js

  • 1. Introduction to WebGL and Three.js James Williams James.L.Williams@gmail.com - Rearden Commerce
  • 2. About Me • Author of Learning HTML5 Game Programming • Google+: +JamesWilliams • Twitter: @ecspike • http://jameswilliams.be/blog James Williams 2 James.L.Williams@gmail.com – Rearden Commerce
  • 3. Agenda - WebGL • What are WebGL and Three.js? • Lighting, Shaders, and Materials • Creating Meshes • GLSL • Animation • Debugging James Williams 3 James.L.Williams@gmail.com – Rearden Commerce
  • 4. What is WebGL? • Low-level 3D graphics context • Hardware accelerated • Supported by most modern browsers • Syntax is based on OpenGL ES 2.0 James Williams 4 James.L.Williams@gmail.com – Rearden Commerce
  • 5. Getting a WebGLContext • Hook to draw on the canvas var canvas = document.getElementById(“c”); var ctx = c.getContext(“experimental-webgl”); James Williams 5 James.L.Williams@gmail.com – Rearden Commerce
  • 6. What is Three.js? • Abstraction layer over WebGL • 3D scenegraph library • Capable of rendering to – Canvas2D, WebGL, or SVG • Exporters for popular 3D formats • http://github.com/mrdoob/three.js James Williams 6 James.L.Williams@gmail.com – Rearden Commerce
  • 7. Initializing Three.js function init() { var HEIGHT = 480, WIDTH = 640; // create a renderer, camera, and scene renderer = new THREE.WebGLRenderer(); renderer.setSize (WIDTH, HEIGHT); camera = /* truncated */ // create scene scene = new THREE.Scene(); // add objects to scene elem.appendChild(render.domElement); } James Williams 7 James.L.Williams@gmail.com – Rearden Commerce
  • 8. Camera • Eye point • Field of vision • Near / Far planes • Target(LookAt) point • Up vector • Advanced Camera types James Williams 8 James.L.Williams@gmail.com – Rearden Commerce
  • 9. Creating Meshes • Geometry vs Mesh • Built-in geometries • Vertex new THREE.Vertex( new Three.Vector3(0.0, 1.0, 0.0) ); • Face new THREE.Face3(0,1,2); James Williams 9 James.L.Williams@gmail.com – Rearden Commerce
  • 10. Creating Meshes geometry = new THREE.Geometry(); geometry.vertices.push(vertex1); geometry.vertices.push(vertex2); geometry.vertices.push(vertex3); geometry.faces.push(face1); var triangle = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial( { color: 0x00ff00 } ) ); James Williams 10 James.L.Williams@gmail.com – Rearden Commerce
  • 11. Creating Meshes plane = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) ); scene.add( plane ); scene.add(triangle); James Williams 11 James.L.Williams@gmail.com – Rearden Commerce
  • 12. Demo James Williams 12 James.L.Williams@gmail.com – Rearden Commerce
  • 13. Lighting • Ambient • Point • Directional • SpotLight new THREE.AmbientLight(hexColor); new THREE.PointLight(hexColor, [intensity], [distance]); James Williams 13 James.L.Williams@gmail.com – Rearden Commerce
  • 14. Shading • Flat • Lambertian • Gouraud • Phong James Williams 14 James.L.Williams@gmail.com – Rearden Commerce
  • 15. Materials • MeshBasicMaterial • MeshLambertMaterial • MeshPhongMaterial • MeshShaderMaterial James Williams 15 James.L.Williams@gmail.com – Rearden Commerce
  • 16. Demo James Williams 16 James.L.Williams@gmail.com – Rearden Commerce
  • 17. Texturing • Can load from images or use canvas data • Basic shapes precalculate texture coordinates James Williams 17 James.L.Williams@gmail.com – Rearden Commerce
  • 18. 2D Texture Coordinates (0,0) (1,1) (0,1) (1,0) James Williams 18 James.L.Williams@gmail.com – Rearden Commerce
  • 19. Texturing Example var texture = THREE.ImageUtils.loadTexture( "200407-bluemarble.jpg" ); var material = new THREE.MeshBasicMaterial( { color: 0xFFFFFF, ambient: 0xFFFFFF, map:texture } ); sphere = new THREE.Mesh( new THREE.SphereGeometry(32, 32, 32), material ); scene.add(sphere); James Williams 19 James.L.Williams@gmail.com – Rearden Commerce
  • 20. Demo James Williams 20 James.L.Williams@gmail.com – Rearden Commerce
  • 21. Loading Models function drawCube() { var loader = new THREE.JSONLoader(); loader.load( {model: "cube.js", callback: createScene1 }); } function createScene1(obj) { obj.materials[0][0].shading = THREE.FlatShading; mesh = THREE.SceneUtils.addMesh( scene, obj, 250, 400, 0, 0, 0, 0, 0,obj.materials[0] ); } James Williams 21 James.L.Williams@gmail.com – Rearden Commerce
  • 22. Demo James Williams 22 James.L.Williams@gmail.com – Rearden Commerce
  • 23. What is GLSL? • High-level language with C-like syntax • Targets the GPU and graphics pipeline • A GLSL program consists of – fragment shader – vertex shader • Content of shaders passed around as strings • Shaders can be generated at run-time James Williams 23 James.L.Williams@gmail.com – Rearden Commerce
  • 24. Vertex Shaders • Run once per vertex in a mesh • Can alter color, position, texels, etc at that vertex James Williams 24 James.L.Williams@gmail.com – Rearden Commerce
  • 25. Example Vertex Shader <script id="shader-vs" type="x-shader/x-vertex"> void main(void) { gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); } </script> James Williams 25 James.L.Williams@gmail.com – Rearden Commerce
  • 26. Fragment(Pixel) Shaders • Run once per pixel in a mesh • Can produce effects such as bump and env mapping James Williams 26 James.L.Williams@gmail.com – Rearden Commerce
  • 27. Example Fragment(Pixel) Shader <script id="shader-vs" type="x-shader/x-fragment"> void main(void) { gl_FragColor = vec4( 0.0, 1.0, 0.0, 1.0 ); } </script> James Williams 27 James.L.Williams@gmail.com – Rearden Commerce
  • 28. Shader Demo Code shaderMaterial = new THREE.MeshShaderMaterial({ vertexShader: $('#v').get(0).innerHTML, fragmentShader:$('#f').get(0).innerHTML, vertexColors: true }); /* truncated */ var triangle = new THREE.Mesh( geometry, shaderMaterial ); James Williams 28 James.L.Williams@gmail.com – Rearden Commerce
  • 29. Shader Variables • uniform • varying • attribute • Types James Williams 29 James.L.Williams@gmail.com – Rearden Commerce
  • 30. Constructing New Shader Types struct MyMaterial { vec4 ambient; vec4 diffuse; vec4 specular; float shininess; }; James Williams 30 James.L.Williams@gmail.com – Rearden Commerce
  • 31. Communicating with Shaders var uniforms; uniforms = { time: {type:"f", value:0.0} } shaderMaterial = new THREE.MeshShaderMaterial({ uniforms: uniforms, vertexShader:$('#v').get(0).innerHTML, fragmentShader:$('#f').get(0).innerHTML, }); James Williams 31 James.L.Williams@gmail.com – Rearden Commerce
  • 32. Custom Shader uniform float time; void main(){ float r = cos(time); float g = sin(time); float b = tan(time); gl_FragColor = vec4(r, 1.0 - g , b, 1.0); } James Williams 32 James.L.Williams@gmail.com – Rearden Commerce
  • 33. Vertex Shader for 2D texturing /* Vertex Shader */ attribute vec4 a_position; attribute vec2 a_texCoord; varying vec2 v_texCoord; void main() { gl_Position = a_position; v_texCoord = a_texCoord; } James Williams 33 James.L.Williams@gmail.com – Rearden Commerce
  • 34. Fragment Shader for 2D texturing /* Fragment Shader */ precision mediump float; varying vec2 v_texCoord; uniform sampler2D s_texture; void main() { gl_FragColor = texture2D(s_texture, v_texCoord); } James Williams 34 James.L.Williams@gmail.com – Rearden Commerce
  • 35. Animation Armature - 3D representation of bones, ligaments, and tendons • Forward kinematics • Inverse kinematics • Keyframes/Morph targets James Williams 35 James.L.Williams@gmail.com – Rearden Commerce
  • 36. MorphTargets var time = new Date().getTime() % duration; var keyframe = Math.floor(time / interpol ) + offset; if ( keyframe != currentKeyframe ) { mesh.morphTargetInfluences[lastFrame]=0; mesh.morphTargetInfluences[currentFrame] =1; mesh.morphTargetInfluences[keyframe]=0; lastFrame = currentFrame; currentFrame = keyframe; } James Williams 36 James.L.Williams@gmail.com – Rearden Commerce
  • 37. MorphTargets mesh.morphTargetInfluences[ keyframe ] = ( time % interpol ) / interpolation; mesh.morphTargetInfluences[ lastFrame ] = 1 - mesh.morphTargetInfluences[keyframe]; James Williams 37 James.L.Williams@gmail.com – Rearden Commerce
  • 38. Demo James Williams 38 James.L.Williams@gmail.com – Rearden Commerce
  • 39. Debugging WebGL James Williams 39 James.L.Williams@gmail.com – Rearden Commerce
  • 40. Stats.js • Measures: – FPS - frames per second – MS - millis to render frame – MB - allocated MB • Github:https://github.com/mrdoob/stats.js James Williams 40 James.L.Williams@gmail.com – Rearden Commerce
  • 41. Stats.js code var stats = new Stats() $("body").append(stats.domElement); //... in your render function stats.update(); James Williams 41 James.L.Williams@gmail.com – Rearden Commerce
  • 42. WebGL Inspector • Allows you to step through rendering • View assets and programs • Capture individual frames • Github: http://benvanik.github.com/WebGL-Inspector James Williams 42 James.L.Williams@gmail.com – Rearden Commerce
  • 43. Questions? James Williams 43 James.L.Williams@gmail.com – Rearden Commerce

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n