SlideShare a Scribd company logo
BEGIN THREE.JS
Yi-Fan Liao
Special thanks to 3D modelers @shrimp & @yushen
http://mrdoob.github.com/three.js/
DEMO LINK
http://begeeben.github.io/begin-threejs/
SOURCE CODE
https://github.com/begeeben/begin-threejs
A SIMPLE EXAMPLE
RENDERER
var	
  renderer	
  =	
  new	
  THREE.WebGLRenderer();
renderer.setSize(	
  window.innerWidth,	
  window.innerHeight	
  );
document.body.appendChild(	
  renderer.domElement	
  );
SCENE
var	
  scene	
  =	
  new	
  THREE.Scene();
CAMERA
//	
  new	
  THREE.PerspectiveCamera(fov,	
  aspect,	
  near,	
  far)
var	
  camera	
  =	
  new	
  THREE.PerspectiveCamera(75,	
  
	
  	
  window.innerWidth/window.innerHeight,	
  0.1,	
  1000);
camera.position.set(0,	
  0,	
  5);
PERSPECTIVE	
  CAMERA
鏡頭與焦距與視⾓角
fov
far
near
aspect = width/height
OBJECT
var	
  geometry	
  =	
  new	
  THREE.CubeGeometry(1,1,1);
var	
  material	
  =	
  new	
  THREE.MeshBasicMaterial({
	
  	
  color:	
  0x199509
});
var	
  mesh	
  =	
  new	
  THREE.Mesh(geometry,	
  material);
scene.add(mesh);
RENDER()
function	
  render()	
  {
	
  	
  requestAnimationFrame(render);
	
  	
  mesh.rotation.x	
  +=	
  0.01;
	
  	
  mesh.rotation.y	
  +=	
  0.01;
	
  	
  renderer.render(scene,	
  camera);
}
render();
requestAnimationFrame for Smart Animating
Better JavaScript animations with requestAnimationFrame
CAMERAS
Cameras on OpenGL ES 2.x – The ModelViewProjection Matrix
ADD TEXTURE
TEXTURE	
  MAP
var	
  geometry	
  =	
  new	
  THREE.CubeGeometry(1,1,1);
//	
  Load	
  image	
  and	
  create	
  texture.
var	
  imgUrl	
  =	
  'images/jumper.jpg';
var	
  map	
  =	
  THREE.ImageUtils.loadTexture(imgUrl);
//	
  For	
  shiny	
  surfaces.
var	
  material	
  =	
  new	
  THREE.MeshPhongMaterial({	
  map:	
  map	
  });
var	
  mesh	
  =	
  new	
  THREE.Mesh(geometry,	
  material);
scene.add(mesh);
LIGHT
//	
  Directional	
  light	
  affects	
  objects	
  using	
  
MeshLambertMaterial	
  or	
  MeshPhongMaterial.
var	
  light	
  =	
  new	
  THREE.DirectionalLight(	
  0xffffff,	
  1	
  );
light.position.set(	
  0,	
  0,	
  1	
  );
scene.add(light);
LIGHTS
MATERIALS
Lights and Materials inThree.js
http://threejs.org/examples/webgl_materials.html
MeshBasicMaterial
MeshLambertMaterial
MeshPhongMaterial
LOAD A 3D MODEL
3D	
  MODEL
//	
  Load	
  ufo	
  image.
var	
  ufoMap	
  =	
  THREE.ImageUtils.loadTexture(
'models/ufo/textures/ufo.png');
var	
  ufoMaterial	
  =	
  new	
  THREE.MeshPhongMaterial({map:ufoMap});
//	
  Load	
  ufo	
  3D	
  JSON	
  model.
var	
  ufo;
var	
  loader	
  =	
  new	
  THREE.JSONLoader();
loader.load('models/ufo/ufo.js',	
  function(geometry)	
  {
	
  	
  ufo	
  =	
  new	
  THREE.Mesh(geometry,	
  ufoMaterial);
	
  	
  scene.add(ufo);
});
LOADER
ColladaLoader	
  for	
  Collada(.dae):	
  An	
  XML-­‐based	
  schema	
  to	
  make	
  it	
  
easy	
  to	
  transport	
  3D	
  assets	
  be-­‐tween	
  applications.
JSONLoader	
  for	
  JSON	
  Model	
  Format:	
  More	
  concise	
  and	
  loading	
  is	
  
faster	
  than	
  using	
  COLLADA	
  because	
  browsers	
  can	
  load	
  it	
  directly	
  
into	
  JavaScript	
  data	
  structures.
BinaryLoader	
  for	
  Binary	
  Model	
  Format:	
  The	
  binary	
  equivalent	
  to	
  
text-­‐based	
  JSON	
  format	
  but	
  smaller	
  in	
  size.
UTF8Loader	
  for	
  UTF-­‐8	
  Encoded	
  Format:	
  An	
  even	
  more	
  economical	
  
data	
  format.
SceneLoader	
  for	
  JSON	
  Scene	
  Format:	
  The	
  format	
  able	
  to	
  store	
  
multiple	
  models	
  or	
  an	
  entire	
  scene,	
  including	
  lights,	
  camera...	
  
etc.
MODEL CONVERSION
EXPORTERS
	
  Blender
	
  3DS	
  MAX
	
  Maya
CONVERTERS
To	
  JSON	
  format
	
  ctm
	
  Autodesk	
  Maya	
  .fbx
	
  Wavefront	
  .obj
To	
  utf8	
  format
	
  utf8
ANIMATE A 3D MODEL
MORPH	
  EXAMPLE
//	
  Load	
  3D	
  model.
var	
  loadCounter	
  =	
  2;
function	
  checkLoadingComplete()	
  {
	
  	
  loadCounter	
  -­‐=	
  1;
	
  	
  if(loadCounter	
  ===	
  0)	
  scene.add(boyMesh);
};
//	
  Textures.
//	
  How	
  the	
  image	
  is	
  applied	
  to	
  the	
  object.
var	
  mapping	
  =	
  new	
  THREE.UVMapping();
var	
  boyMap	
  =	
  THREE.ImageUtils.loadTexture(
'models/boy/textures/boy.png',	
  
mapping,	
  checkLoadingComplete());
MORPH	
  EXAMPLE
//	
  Body.
var	
  boyMesh;
var	
  loader	
  =	
  new	
  THREE.JSONLoader();
loader.load('models/boy/boy.js',	
  function(geometry)	
  {
	
  	
  geometry.computeMorphNormals();
	
  	
  var	
  materialBoy	
  =	
  new	
  THREE.MeshPhongMaterial({
	
  	
  	
  	
  color:	
  0xffffff,
	
  	
  	
  	
  specular:	
  0x111111,
	
  	
  	
  	
  shininess:	
  50,
	
  	
  	
  	
  map:	
  boyMap,
	
  	
  	
  	
  morphTargets:	
  true,
	
  	
  	
  	
  morphNormals:	
  true,
	
  	
  	
  	
  wrapAround:	
  true
	
  	
  });
	
  	
  boyMesh	
  =	
  new	
  THREE.MorphAnimMesh(geometry,	
  materialBoy);
	
  	
  boyMesh.castShadow	
  =	
  true;
	
  	
  boyMesh.receiveShadow	
  =	
  true;
	
  	
  //	
  Parse	
  animations	
  in	
  the	
  model	
  file.
	
  	
  boyMesh.parseAnimations();
	
  	
  boyMesh.playAnimation(	
  geometry.firstAnimation,	
  30	
  );
	
  	
  checkLoadingComplete();
});
ANIMATIONS
MORPH
Morph animation example on three.js website
SKELETAL	
  ANIMATION
Skinned animation example on three.js website
BUILD A 3D MENU
TEXT
<script	
  src="fonts/optimer_regular.typeface.js"></script>
var	
  textGeometry	
  =	
  new	
  THREE.TextGeometry(	
  text,	
  {	
  ...
MOUSE	
  EVENT
var	
  projector	
  =	
  new	
  THREE.Projector();
var	
  mouse	
  =	
  new	
  THREE.Vector2();
mouse.x	
  =	
  (	
  event.clientX	
  /	
  container.clientWidth	
  )	
  *	
  2	
  -­‐	
  1;
mouse.y	
  =	
  -­‐	
  (	
  event.clientY	
  /	
  container.clientHeight	
  )	
  *	
  2	
  +	
  1;
//	
  Unproject	
  the	
  screen	
  space	
  mouse	
  point	
  to	
  3D	
  world	
  space.
var	
  vector	
  =	
  new	
  THREE.Vector3(	
  mouse.x,	
  mouse.y,	
  0.5	
  );
//	
  Cast	
  a	
  ray	
  from	
  the	
  camera	
  according	
  to	
  the	
  vector.
var	
  raycaster	
  =	
  projector.pickingRay(	
  vector.clone(),	
  camera	
  );
//	
  Get	
  objects	
  that	
  intersects	
  the	
  ray.
var	
  intersects	
  =	
  raycaster.intersectObjects(	
  items,	
  true	
  );
if	
  (	
  intersects.length	
  >	
  0	
  )	
  {	
  ...
BLEND 3D CONTENT IN HTML
SAMPLE SLOT GAME
RESOURCES
	
  WebGL:	
  Up	
  and	
  Running	
  
Tools
	
  https://github.com/sole/tween.js
	
  three.js	
  boilerplate
	
  THREECONVERT	
  OSX	
  BATCH	
  EXPORTER	
  UTILITY	
  FOR	
  THREE.JS
	
  MD2	
  to	
  JSON	
  Converter
Show	
  Cases
	
  Verold	
  STUDIO
	
  Simple	
  facial	
  rigging	
  utilizing	
  morph	
  targets
	
  WebGL	
  -­‐	
  Three.js	
  +	
  impactJS
Tutorials
	
  Building	
  the	
  Game:	
  Part	
  3	
  -­‐	
  Skinning	
  &	
  Animation
	
  Verification	
  of	
  using	
  multiple	
  textures	
  with	
  three.js	
  
cubes
ABOUT
廖一帆
Front End Engineer
begeeben@gmail.com
github.com/begeeben
Yi-Fan Liao
begeeben.wordpress.com/
www.facebook.com/yifan.liao

More Related Content

ODP
Threejs使ってみた
PDF
UIWebViewでつくるUI
PDF
WebGL and three.js
PDF
Портируем существующее Web-приложение в виртуальную реальность / Денис Радин ...
PDF
HTML5 game dev with three.js - HexGL
PDF
The Ring programming language version 1.5.1 book - Part 53 of 180
PDF
PPTX
Introduction to three.js
Threejs使ってみた
UIWebViewでつくるUI
WebGL and three.js
Портируем существующее Web-приложение в виртуальную реальность / Денис Радин ...
HTML5 game dev with three.js - HexGL
The Ring programming language version 1.5.1 book - Part 53 of 180
Introduction to three.js

What's hot (8)

PDF
The Ring programming language version 1.8 book - Part 61 of 202
PPTX
WebGL and three.js - Web 3D Graphics
KEY
5 tips for your HTML5 games
PDF
The Ring programming language version 1.6 book - Part 59 of 189
PPT
Rotoscope inthebrowserppt billy
ODP
Introduction to threejs
KEY
Stupid Canvas Tricks
PDF
openFrameworks – 関数・クラス、オブジェクト指向プログラミング導入 - 多摩美メディアアートII
The Ring programming language version 1.8 book - Part 61 of 202
WebGL and three.js - Web 3D Graphics
5 tips for your HTML5 games
The Ring programming language version 1.6 book - Part 59 of 189
Rotoscope inthebrowserppt billy
Introduction to threejs
Stupid Canvas Tricks
openFrameworks – 関数・クラス、オブジェクト指向プログラミング導入 - 多摩美メディアアートII
Ad

Similar to Begin three.js.key (20)

PDF
Three.js basics
PDF
3D everywhere
PDF
WebGL 3D player
PDF
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
PDF
Augmented reality in web rtc browser
PDF
3D Web Programming [Thanh Loc Vo , CTO Epsilon Mobile ]
PDF
Creating Applications with WebGL and Three.js
PDF
Introduction to three.js & Leap Motion
PDF
ENEI16 - WebGL with Three.js
PDF
Neurotech Solutions Ltd: Рекомендации по Stage3D: выбор наиболее подходящего ...
PPT
OWC 2012 (Open Web Camp)
PPTX
Building Rich Internet Applications with HTML5 and WebGL
PPTX
COMP340 TOPIC 4 THREE.JS.pptx
PPTX
Rest3d BOF presentation at SigGraph 2013
PPT
Advanced Game Development with the Mobile 3D Graphics API
PPTX
WebGL - It's GO Time
PDF
Developing Interactive 3D Experiences in HTML5 with Carlos Ulloa
PDF
Getting started with Verold and Three.js
KEY
Getting Started with WebGL
PDF
Augmented Reality
Three.js basics
3D everywhere
WebGL 3D player
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
Augmented reality in web rtc browser
3D Web Programming [Thanh Loc Vo , CTO Epsilon Mobile ]
Creating Applications with WebGL and Three.js
Introduction to three.js & Leap Motion
ENEI16 - WebGL with Three.js
Neurotech Solutions Ltd: Рекомендации по Stage3D: выбор наиболее подходящего ...
OWC 2012 (Open Web Camp)
Building Rich Internet Applications with HTML5 and WebGL
COMP340 TOPIC 4 THREE.JS.pptx
Rest3d BOF presentation at SigGraph 2013
Advanced Game Development with the Mobile 3D Graphics API
WebGL - It's GO Time
Developing Interactive 3D Experiences in HTML5 with Carlos Ulloa
Getting started with Verold and Three.js
Getting Started with WebGL
Augmented Reality
Ad

Recently uploaded (20)

PPTX
TLE Review Electricity (Electricity).pptx
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
Hybrid model detection and classification of lung cancer
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
August Patch Tuesday
PPTX
A Presentation on Touch Screen Technology
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
cloud_computing_Infrastucture_as_cloud_p
PPTX
A Presentation on Artificial Intelligence
PDF
Encapsulation theory and applications.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Mushroom cultivation and it's methods.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
TLE Review Electricity (Electricity).pptx
Heart disease approach using modified random forest and particle swarm optimi...
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Hybrid model detection and classification of lung cancer
A comparative study of natural language inference in Swahili using monolingua...
August Patch Tuesday
A Presentation on Touch Screen Technology
Enhancing emotion recognition model for a student engagement use case through...
1 - Historical Antecedents, Social Consideration.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
cloud_computing_Infrastucture_as_cloud_p
A Presentation on Artificial Intelligence
Encapsulation theory and applications.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Mushroom cultivation and it's methods.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Building Integrated photovoltaic BIPV_UPV.pdf

Begin three.js.key

  • 1. BEGIN THREE.JS Yi-Fan Liao Special thanks to 3D modelers @shrimp & @yushen
  • 6. RENDERER var  renderer  =  new  THREE.WebGLRenderer(); renderer.setSize(  window.innerWidth,  window.innerHeight  ); document.body.appendChild(  renderer.domElement  );
  • 7. SCENE var  scene  =  new  THREE.Scene();
  • 8. CAMERA //  new  THREE.PerspectiveCamera(fov,  aspect,  near,  far) var  camera  =  new  THREE.PerspectiveCamera(75,      window.innerWidth/window.innerHeight,  0.1,  1000); camera.position.set(0,  0,  5);
  • 10. OBJECT var  geometry  =  new  THREE.CubeGeometry(1,1,1); var  material  =  new  THREE.MeshBasicMaterial({    color:  0x199509 }); var  mesh  =  new  THREE.Mesh(geometry,  material); scene.add(mesh);
  • 11. RENDER() function  render()  {    requestAnimationFrame(render);    mesh.rotation.x  +=  0.01;    mesh.rotation.y  +=  0.01;    renderer.render(scene,  camera); } render(); requestAnimationFrame for Smart Animating Better JavaScript animations with requestAnimationFrame
  • 12. CAMERAS Cameras on OpenGL ES 2.x – The ModelViewProjection Matrix
  • 14. TEXTURE  MAP var  geometry  =  new  THREE.CubeGeometry(1,1,1); //  Load  image  and  create  texture. var  imgUrl  =  'images/jumper.jpg'; var  map  =  THREE.ImageUtils.loadTexture(imgUrl); //  For  shiny  surfaces. var  material  =  new  THREE.MeshPhongMaterial({  map:  map  }); var  mesh  =  new  THREE.Mesh(geometry,  material); scene.add(mesh);
  • 15. LIGHT //  Directional  light  affects  objects  using   MeshLambertMaterial  or  MeshPhongMaterial. var  light  =  new  THREE.DirectionalLight(  0xffffff,  1  ); light.position.set(  0,  0,  1  ); scene.add(light);
  • 17. MATERIALS Lights and Materials inThree.js http://threejs.org/examples/webgl_materials.html MeshBasicMaterial MeshLambertMaterial MeshPhongMaterial
  • 18. LOAD A 3D MODEL
  • 19. 3D  MODEL //  Load  ufo  image. var  ufoMap  =  THREE.ImageUtils.loadTexture( 'models/ufo/textures/ufo.png'); var  ufoMaterial  =  new  THREE.MeshPhongMaterial({map:ufoMap}); //  Load  ufo  3D  JSON  model. var  ufo; var  loader  =  new  THREE.JSONLoader(); loader.load('models/ufo/ufo.js',  function(geometry)  {    ufo  =  new  THREE.Mesh(geometry,  ufoMaterial);    scene.add(ufo); });
  • 20. LOADER ColladaLoader  for  Collada(.dae):  An  XML-­‐based  schema  to  make  it   easy  to  transport  3D  assets  be-­‐tween  applications. JSONLoader  for  JSON  Model  Format:  More  concise  and  loading  is   faster  than  using  COLLADA  because  browsers  can  load  it  directly   into  JavaScript  data  structures. BinaryLoader  for  Binary  Model  Format:  The  binary  equivalent  to   text-­‐based  JSON  format  but  smaller  in  size. UTF8Loader  for  UTF-­‐8  Encoded  Format:  An  even  more  economical   data  format. SceneLoader  for  JSON  Scene  Format:  The  format  able  to  store   multiple  models  or  an  entire  scene,  including  lights,  camera...   etc.
  • 21. MODEL CONVERSION EXPORTERS  Blender  3DS  MAX  Maya CONVERTERS To  JSON  format  ctm  Autodesk  Maya  .fbx  Wavefront  .obj To  utf8  format  utf8
  • 22. ANIMATE A 3D MODEL
  • 23. MORPH  EXAMPLE //  Load  3D  model. var  loadCounter  =  2; function  checkLoadingComplete()  {    loadCounter  -­‐=  1;    if(loadCounter  ===  0)  scene.add(boyMesh); }; //  Textures. //  How  the  image  is  applied  to  the  object. var  mapping  =  new  THREE.UVMapping(); var  boyMap  =  THREE.ImageUtils.loadTexture( 'models/boy/textures/boy.png',   mapping,  checkLoadingComplete());
  • 24. MORPH  EXAMPLE //  Body. var  boyMesh; var  loader  =  new  THREE.JSONLoader(); loader.load('models/boy/boy.js',  function(geometry)  {    geometry.computeMorphNormals();    var  materialBoy  =  new  THREE.MeshPhongMaterial({        color:  0xffffff,        specular:  0x111111,        shininess:  50,        map:  boyMap,        morphTargets:  true,        morphNormals:  true,        wrapAround:  true    });    boyMesh  =  new  THREE.MorphAnimMesh(geometry,  materialBoy);    boyMesh.castShadow  =  true;    boyMesh.receiveShadow  =  true;    //  Parse  animations  in  the  model  file.    boyMesh.parseAnimations();    boyMesh.playAnimation(  geometry.firstAnimation,  30  );    checkLoadingComplete(); });
  • 26. MORPH Morph animation example on three.js website
  • 27. SKELETAL  ANIMATION Skinned animation example on three.js website
  • 28. BUILD A 3D MENU
  • 30. MOUSE  EVENT var  projector  =  new  THREE.Projector(); var  mouse  =  new  THREE.Vector2(); mouse.x  =  (  event.clientX  /  container.clientWidth  )  *  2  -­‐  1; mouse.y  =  -­‐  (  event.clientY  /  container.clientHeight  )  *  2  +  1; //  Unproject  the  screen  space  mouse  point  to  3D  world  space. var  vector  =  new  THREE.Vector3(  mouse.x,  mouse.y,  0.5  ); //  Cast  a  ray  from  the  camera  according  to  the  vector. var  raycaster  =  projector.pickingRay(  vector.clone(),  camera  ); //  Get  objects  that  intersects  the  ray. var  intersects  =  raycaster.intersectObjects(  items,  true  ); if  (  intersects.length  >  0  )  {  ...
  • 31. BLEND 3D CONTENT IN HTML
  • 33. RESOURCES  WebGL:  Up  and  Running   Tools  https://github.com/sole/tween.js  three.js  boilerplate  THREECONVERT  OSX  BATCH  EXPORTER  UTILITY  FOR  THREE.JS  MD2  to  JSON  Converter Show  Cases  Verold  STUDIO  Simple  facial  rigging  utilizing  morph  targets  WebGL  -­‐  Three.js  +  impactJS Tutorials  Building  the  Game:  Part  3  -­‐  Skinning  &  Animation  Verification  of  using  multiple  textures  with  three.js   cubes
  • 34. ABOUT 廖一帆 Front End Engineer begeeben@gmail.com github.com/begeeben Yi-Fan Liao begeeben.wordpress.com/ www.facebook.com/yifan.liao