SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

En vedette

Web3D - Semantic standards, WebGL, HCI
Web3D - Semantic standards, WebGL, HCIWeb3D - Semantic standards, WebGL, HCI
Web3D - Semantic standards, WebGL, HCIVictor Porof
 
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 JavaScriptRaphael Amorim
 
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 Pascal Rettig
 
ようこそ、HTML5裏APIの世界へ - HTML5 Conference 2013
ようこそ、HTML5裏APIの世界へ - HTML5 Conference 2013ようこそ、HTML5裏APIの世界へ - HTML5 Conference 2013
ようこそ、HTML5裏APIの世界へ - HTML5 Conference 2013Futomi Hatano
 
Introduction to webGL
Introduction to webGLIntroduction to webGL
Introduction to webGL志鴻 詹
 
WebGL - An Overview
WebGL - An OverviewWebGL - An Overview
WebGL - An OverviewJens Arps
 
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 webpjcozzi
 
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 2014Verold
 
Starting Development for Nokia N9
Starting Development for Nokia N9Starting Development for Nokia N9
Starting Development for Nokia N9tpyssysa
 
Qt Design Patterns
Qt Design PatternsQt Design Patterns
Qt Design PatternsYnon Perek
 
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 Frameworkaccount inactive
 
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 2009Nokia
 

En vedette (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
 

Plus de James Williams

Intro to HTML5 Game Programming
Intro to HTML5 Game ProgrammingIntro to HTML5 Game Programming
Intro to HTML5 Game ProgrammingJames 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 AppsJames Williams
 
Introduction to Griffon
Introduction to GriffonIntroduction to Griffon
Introduction to GriffonJames Williams
 
Griffon for the Enterprise
Griffon for the EnterpriseGriffon for the Enterprise
Griffon for the EnterpriseJames Williams
 
Game programming with Groovy
Game programming with GroovyGame programming with Groovy
Game programming with GroovyJames Williams
 
Java development with MongoDB
Java development with MongoDBJava development with MongoDB
Java development with MongoDBJames Williams
 
Porting legacy apps to Griffon
Porting legacy apps to GriffonPorting legacy apps to Griffon
Porting legacy apps to GriffonJames Williams
 
Using MongoDB With Groovy
Using MongoDB With GroovyUsing MongoDB With Groovy
Using MongoDB With GroovyJames Williams
 
Creating Voice Powered Apps with Ribbit
Creating Voice Powered Apps with RibbitCreating Voice Powered Apps with Ribbit
Creating Voice Powered Apps with RibbitJames Williams
 
Griffon: Swing just got fun again
Griffon: Swing just got fun againGriffon: Swing just got fun again
Griffon: Swing just got fun againJames Williams
 
Griffon: Swing just got fun again
Griffon: Swing just got fun againGriffon: Swing just got fun again
Griffon: Swing just got fun againJames 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 ApplicationsJames Williams
 
Boosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with GroovyBoosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with GroovyJames Williams
 
Griffon: Re-imaging Desktop Java Technology
Griffon: Re-imaging Desktop Java TechnologyGriffon: Re-imaging Desktop Java Technology
Griffon: Re-imaging Desktop Java TechnologyJames Williams
 

Plus de 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
 

Dernier

Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
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 AutomationSafe Software
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
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 WorkerThousandEyes
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
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 RobisonAnna Loughnan Colquhoun
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
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...Miguel Araújo
 
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 slidevu2urc
 
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 textsMaria Levchenko
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 

Dernier (20)

Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
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...
 
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
 
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
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 

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

Notes de l'éditeur

  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