SlideShare a Scribd company logo
4
Most read
8
Most read
14
Most read
WebGL with THREE.js


Wednesday, December 12, 12
@antonnarusberg
                                 anton@cannedapps.com


                             Google Developers Group Tallinn
                                  http://bit.ly/gdgtallinn
                                     @GDGTallinn



Wednesday, December 12, 12
3D in Web Browsers




Wednesday, December 12, 12
3D in Web Browsers

   Full power of computer's GPU using only JS, web browser
              and standard web technology stack.


                             Old way - plugins, native applications

                                     New way - WebGL




Wednesday, December 12, 12
Browser compatibility




Wednesday, December 12, 12
So how do you use it?

                             WebGL is an API, accessed through
                             JavaScript programming interfaces.


                             Analogous to 2D drawing using the
                                  HTML5 Canvas element


                             Based on OpenGL ES 2.0 standard


Wednesday, December 12, 12
An (overly) simplified Example:
                    Cube with plain WebGL




Wednesday, December 12, 12
// get the WebGL context to access the API
               var canvas = document.getElementById("lesson04-canvas");
               gl = canvas.getContext("experimental-webgl");


               // Set up Shaders
               var fragmentShader = getShader(gl, "shader-fs");
               var vertexShader = getShader(gl, "shader-vs");

               shaderProgram = gl.createProgram();
               gl.attachShader(shaderProgram, vertexShader);
               gl.attachShader(shaderProgram, fragmentShader);
               gl.linkProgram(shaderProgram);

               gl.useProgram(shaderProgram);

               // ....




Wednesday, December 12, 12
<script id="shader-fs" type="x-shader/x-fragment">
                   precision mediump float;

                   varying vec4 vColor;

                   void main(void) {
                       gl_FragColor = vColor;
                   }
               </script>

               <script id="shader-vs" type="x-shader/x-vertex">
                   attribute vec3 aVertexPosition;
                   attribute vec4 aVertexColor;

                   uniform mat4 uMVMatrix;
                   uniform mat4 uPMatrix;

                   varying vec4 vColor;

                   void main(void) {
                       gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
                       vColor = aVertexColor;
                   }
               </script>




Wednesday, December 12, 12
// A taste of creating the array of vertices for the cube
              cubeVertexPositionBuffer = gl.createBuffer();
              gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);

         vertices = [
              // 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,
              // Top 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,
              // Bottom 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,
              // Right 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,
              // Left 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
         ];

              gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);




Wednesday, December 12, 12
// Rotating the cube with Matrix computations
             function drawScene() {

                   mat4.translate(mvMatrix, [3.0, 0.0, 0.0]);
                   mat4.rotate(mvMatrix, degToRad(rCube), [1, 1, 1]);

                   gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);

                   // ... Bind buffers etc.

                   gl.drawElements(gl.TRIANGLES, cubeVertexIndexBuffer.numItems,
                   gl.UNSIGNED_SHORT, 0);

                   // ...
             }

             function tick() {
                requestAnimFrame(tick);
                rCube += 1;
                drawScene();
             }




Wednesday, December 12, 12
Skills needed for plain WebGL

       * GLSL, the shading language used by OpenGL and
       WebGL

       * Lots of Math for Matrix computation to set up
       transformations

       * Creating Vertex buffers to hold data about vertex
       positions, normals, colors, and textures


Wednesday, December 12, 12
THREE.js to the rescue!




Wednesday, December 12, 12
THREE.js

       * Abstracts away all the painful overhead

       * Elegant API to create and manipulate Cameras,
       Objects, Lights, Materials etc.

       * THREE.js is Open Source




Wednesday, December 12, 12
A Cube example using THREE.js


       http://learningthreejs.com/data/
       lets_do_a_cube/docs/lets_do_a_cube.html

       Jerome Etienne - great tutorials on THREE.js




Wednesday, December 12, 12
// Set up a Camera
             camera = new THREE.Camera(70, window.innerWidth / window.innerHeight, 1,
             1000);
             camera.position.y = 150;
             camera.position.z = 350;
             camera.target.position.y = 150;

             // Create a Scene
             scene = new THREE.Scene();

             // Create a Cube
             material = new THREE.MeshNormalMaterial();
             cube = new THREE.Mesh( new THREE.CubeGeometry( 200, 200, 200 ), material);
             cube.position.y = 150;


             // Add the Cube to the Scene
             scene.addObject( cube );


             // Boilerplate
             renderer = new THREE.WebGLRenderer();
             renderer.setSize( window.innerWidth, window.innerHeight );
             $(“#container”).appendChild( renderer.domElement );




Wednesday, December 12, 12
function animate() {
             	 render();


                   // Loop
             	     requestAnimationFrame( animate );
             }

             function render() {
                   // Rotate
             	     cube.rotation.x += 0.02;
             	     cube.rotation.y += 0.0225;
             	     cube.rotation.z += 0.0175;


                   // Bounce
                   var dtime	= Date.now() - startTime;
             	     cube.scale.x	 = 1.0 + 0.3*Math.sin(dtime/300);
             	     cube.scale.y	 = 1.0 + 0.3*Math.sin(dtime/300);
             	     cube.scale.z	 = 1.0 + 0.3*Math.sin(dtime/300);

             	     renderer.render( scene, camera );
             }




Wednesday, December 12, 12
It’s alive!




Wednesday, December 12, 12
More Features
       Renderers
       WebGL, <canvas>, <svg>

       Cameras
       Perspective and orthographic; controllers: trackball, FPS, path and more

       Lights
       Ambient, direction, point, spot and hemisphere lights. Cast and receive shadows

       Materials
       Lambert, Phong and more - with textures, smooth-shading

       Shaders
       Access to full WebGL capabilities like lens flare, depth pass and an extensive post-processing library

       Objects
       meshes, particles, sprites, lines, ribbons, ...

       Geometry
       plane, cube, sphere, torus, 3D text and more

       Export/Import
       utilities to create Three.js-compatible JSON files from: Blender, CTM, FBX, 3D Max, and OBJ



Wednesday, December 12, 12
http://threejs.org/

           https://github.com/mrdoob/three.js

                             http://learningthreejs.com


Wednesday, December 12, 12
Thank you!




Wednesday, December 12, 12

More Related Content

PPTX
Introduction to three.js
PDF
Three.js basics
ODP
Introduction to threejs
PDF
PDF
WebGL and Three.js
PPTX
Introduction to React JS for beginners | Namespace IT
PPT
Introduction-to-Unity.ppt
PDF
Webで3Dモデルはどう扱う?PlayCanvas:3Dモデルディープダイブ+新機能紹介!
Introduction to three.js
Three.js basics
Introduction to threejs
WebGL and Three.js
Introduction to React JS for beginners | Namespace IT
Introduction-to-Unity.ppt
Webで3Dモデルはどう扱う?PlayCanvas:3Dモデルディープダイブ+新機能紹介!

What's hot (20)

PPTX
Unity 3D, A game engine
PPTX
Introduction to Unity3D and Building your First Game
PPTX
Cesiumを動かしてみよう
PPTX
HoloLens2とPCで、WebRTCで映像をやりとり
PPTX
Unity3D Programming
PPTX
Introduction to React JS
PDF
ReactJS presentation
PDF
Mobile AR Lecture6 - Introduction to Unity 3D
PPTX
ARマーカーを利用したHoloLens同士の位置合わせ
PPTX
Game Development with Unity
PDF
PDF
Game Physics Engine Development (게임 물리 엔진 개발)
PDF
コンセプト概論~出張ヒストリア編~
PDF
The Basics of Unity - The Game Engine
PDF
Introduction to A-Frame
PPTX
Rendering Techniques for Augmented Reality and a Look Ahead at AR Foundation
PDF
Amazon Game Tech Night #21 Game Development on AWS
PPTX
Introduction to Unity
PPTX
[Final] ReactJS presentation
PPTX
オープンデータとオープンソースGisを用いたweb上でのインタラクティブ可視化手法について
Unity 3D, A game engine
Introduction to Unity3D and Building your First Game
Cesiumを動かしてみよう
HoloLens2とPCで、WebRTCで映像をやりとり
Unity3D Programming
Introduction to React JS
ReactJS presentation
Mobile AR Lecture6 - Introduction to Unity 3D
ARマーカーを利用したHoloLens同士の位置合わせ
Game Development with Unity
Game Physics Engine Development (게임 물리 엔진 개발)
コンセプト概論~出張ヒストリア編~
The Basics of Unity - The Game Engine
Introduction to A-Frame
Rendering Techniques for Augmented Reality and a Look Ahead at AR Foundation
Amazon Game Tech Night #21 Game Development on AWS
Introduction to Unity
[Final] ReactJS presentation
オープンデータとオープンソースGisを用いたweb上でのインタラクティブ可視化手法について
Ad

Viewers also liked (20)

PPT
Web gl game development
PPTX
cloud conference 2013 - Infrastructure as a Service in Amazon Web Services
KEY
Getting Started with WebGL
PDF
Web Sockets in Java EE 7
PDF
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
PDF
Starting Development for Nokia N9
PDF
Qt Design Patterns
ODP
Qt Workshop
PPTX
Open stack implementation
PDF
Qt State Machine Framework
PDF
Cutest technology of them all - Forum Nokia Qt Webinar December 2009
PDF
Open Stack vs .NET Stack - For Startups
ODP
Qt 5 - C++ and Widgets
PPT
Open gl
KEY
Introduction to WebGL and Three.js
PPTX
Introduction to Qt
PPTX
Module 4: NETCONF Tutorial
PPTX
Amazon Web Service EC2 & S3
PDF
Best Practices in Qt Quick/QML - Part II
 
PDF
Best Practices in Qt Quick/QML - Part III
 
Web gl game development
cloud conference 2013 - Infrastructure as a Service in Amazon Web Services
Getting Started with WebGL
Web Sockets in Java EE 7
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
Starting Development for Nokia N9
Qt Design Patterns
Qt Workshop
Open stack implementation
Qt State Machine Framework
Cutest technology of them all - Forum Nokia Qt Webinar December 2009
Open Stack vs .NET Stack - For Startups
Qt 5 - C++ and Widgets
Open gl
Introduction to WebGL and Three.js
Introduction to Qt
Module 4: NETCONF Tutorial
Amazon Web Service EC2 & S3
Best Practices in Qt Quick/QML - Part II
 
Best Practices in Qt Quick/QML - Part III
 
Ad

Similar to WebGL and three.js (20)

PDF
Webgl para JavaScripters
PDF
WT-4064, Build Rich Applications with HTML5 and WebGL, by Tony Parisi
PDF
3D Web Programming [Thanh Loc Vo , CTO Epsilon Mobile ]
PPTX
WebGL Crash Course
PPTX
WebGL, HTML5 and How the Mobile Web Was Won
PDF
Deep dive into deeplearn.js
KEY
Leaving Flatland: getting started with WebGL
PPTX
COLLADA & WebGL
PPTX
Developing Web Graphics with WebGL
PPTX
WebGL - It's GO Time
PPTX
Drawing in HTML5 Open House
PPTX
Rendering of Complex 3D Treemaps (GRAPP 2013)
PDF
Leaving Flatland: Getting Started with WebGL- SXSW 2012
PDF
HTML5 for the Silverlight Guy
PDF
Unconventional webapps with gwt:elemental & html5
PPTX
[JS EXPERIENCE 2018] Jogos em JavaScript com WebGL - Juliana Negreiros, Codem...
PDF
JavaScript para Graficos y Visualizacion de Datos - BogotaJS
PDF
Kharkivpy#3: Javascript and Python backend
PPT
WebGL: GPU acceleration for the open web
PDF
JavaScript para Graficos y Visualizacion de Datos
Webgl para JavaScripters
WT-4064, Build Rich Applications with HTML5 and WebGL, by Tony Parisi
3D Web Programming [Thanh Loc Vo , CTO Epsilon Mobile ]
WebGL Crash Course
WebGL, HTML5 and How the Mobile Web Was Won
Deep dive into deeplearn.js
Leaving Flatland: getting started with WebGL
COLLADA & WebGL
Developing Web Graphics with WebGL
WebGL - It's GO Time
Drawing in HTML5 Open House
Rendering of Complex 3D Treemaps (GRAPP 2013)
Leaving Flatland: Getting Started with WebGL- SXSW 2012
HTML5 for the Silverlight Guy
Unconventional webapps with gwt:elemental & html5
[JS EXPERIENCE 2018] Jogos em JavaScript com WebGL - Juliana Negreiros, Codem...
JavaScript para Graficos y Visualizacion de Datos - BogotaJS
Kharkivpy#3: Javascript and Python backend
WebGL: GPU acceleration for the open web
JavaScript para Graficos y Visualizacion de Datos

WebGL and three.js

  • 2. @antonnarusberg anton@cannedapps.com Google Developers Group Tallinn http://bit.ly/gdgtallinn @GDGTallinn Wednesday, December 12, 12
  • 3. 3D in Web Browsers Wednesday, December 12, 12
  • 4. 3D in Web Browsers Full power of computer's GPU using only JS, web browser and standard web technology stack. Old way - plugins, native applications New way - WebGL Wednesday, December 12, 12
  • 6. So how do you use it? WebGL is an API, accessed through JavaScript programming interfaces. Analogous to 2D drawing using the HTML5 Canvas element Based on OpenGL ES 2.0 standard Wednesday, December 12, 12
  • 7. An (overly) simplified Example: Cube with plain WebGL Wednesday, December 12, 12
  • 8. // get the WebGL context to access the API var canvas = document.getElementById("lesson04-canvas"); gl = canvas.getContext("experimental-webgl"); // Set up Shaders var fragmentShader = getShader(gl, "shader-fs"); var vertexShader = getShader(gl, "shader-vs"); shaderProgram = gl.createProgram(); gl.attachShader(shaderProgram, vertexShader); gl.attachShader(shaderProgram, fragmentShader); gl.linkProgram(shaderProgram); gl.useProgram(shaderProgram); // .... Wednesday, December 12, 12
  • 9. <script id="shader-fs" type="x-shader/x-fragment">     precision mediump float;     varying vec4 vColor;     void main(void) {         gl_FragColor = vColor;     } </script> <script id="shader-vs" type="x-shader/x-vertex">     attribute vec3 aVertexPosition;     attribute vec4 aVertexColor;     uniform mat4 uMVMatrix;     uniform mat4 uPMatrix;     varying vec4 vColor;     void main(void) {         gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);         vColor = aVertexColor;     } </script> Wednesday, December 12, 12
  • 10. // A taste of creating the array of vertices for the cube cubeVertexPositionBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer); vertices = [             // 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,             // Top 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,             // Bottom 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,             // Right 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,             // Left 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        ]; gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); Wednesday, December 12, 12
  • 11. // Rotating the cube with Matrix computations function drawScene() { mat4.translate(mvMatrix, [3.0, 0.0, 0.0]); mat4.rotate(mvMatrix, degToRad(rCube), [1, 1, 1]); gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix); // ... Bind buffers etc. gl.drawElements(gl.TRIANGLES, cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0); // ... } function tick() { requestAnimFrame(tick); rCube += 1; drawScene(); } Wednesday, December 12, 12
  • 12. Skills needed for plain WebGL * GLSL, the shading language used by OpenGL and WebGL * Lots of Math for Matrix computation to set up transformations * Creating Vertex buffers to hold data about vertex positions, normals, colors, and textures Wednesday, December 12, 12
  • 13. THREE.js to the rescue! Wednesday, December 12, 12
  • 14. THREE.js * Abstracts away all the painful overhead * Elegant API to create and manipulate Cameras, Objects, Lights, Materials etc. * THREE.js is Open Source Wednesday, December 12, 12
  • 15. A Cube example using THREE.js http://learningthreejs.com/data/ lets_do_a_cube/docs/lets_do_a_cube.html Jerome Etienne - great tutorials on THREE.js Wednesday, December 12, 12
  • 16. // Set up a Camera camera = new THREE.Camera(70, window.innerWidth / window.innerHeight, 1, 1000); camera.position.y = 150; camera.position.z = 350; camera.target.position.y = 150; // Create a Scene scene = new THREE.Scene(); // Create a Cube material = new THREE.MeshNormalMaterial(); cube = new THREE.Mesh( new THREE.CubeGeometry( 200, 200, 200 ), material); cube.position.y = 150; // Add the Cube to the Scene scene.addObject( cube ); // Boilerplate renderer = new THREE.WebGLRenderer(); renderer.setSize( window.innerWidth, window.innerHeight ); $(“#container”).appendChild( renderer.domElement ); Wednesday, December 12, 12
  • 17. function animate() { render(); // Loop requestAnimationFrame( animate ); } function render() { // Rotate cube.rotation.x += 0.02; cube.rotation.y += 0.0225; cube.rotation.z += 0.0175; // Bounce var dtime = Date.now() - startTime; cube.scale.x = 1.0 + 0.3*Math.sin(dtime/300); cube.scale.y = 1.0 + 0.3*Math.sin(dtime/300); cube.scale.z = 1.0 + 0.3*Math.sin(dtime/300); renderer.render( scene, camera ); } Wednesday, December 12, 12
  • 19. More Features Renderers WebGL, <canvas>, <svg> Cameras Perspective and orthographic; controllers: trackball, FPS, path and more Lights Ambient, direction, point, spot and hemisphere lights. Cast and receive shadows Materials Lambert, Phong and more - with textures, smooth-shading Shaders Access to full WebGL capabilities like lens flare, depth pass and an extensive post-processing library Objects meshes, particles, sprites, lines, ribbons, ... Geometry plane, cube, sphere, torus, 3D text and more Export/Import utilities to create Three.js-compatible JSON files from: Blender, CTM, FBX, 3D Max, and OBJ Wednesday, December 12, 12
  • 20. http://threejs.org/ https://github.com/mrdoob/three.js http://learningthreejs.com Wednesday, December 12, 12