您好,欢迎访问一九零五行业门户网

Three.js使用详解

这次给大家带来three.js使用详解,three.js使用的注意事项有哪些,下面就是实战案例,一起来看一下。
开场白
webgl可以让我们在canvas上实现3d效果。而three.js是一款webgl框架,由于其易用性被广泛应用。如果你要学习webgl,抛弃那些复杂的原生接口从这款框架入手是一个不错的选择。
博主目前也在学习three.js,发现相关资料非常稀少,甚至官方的api文档也非常粗糙,很多效果需要自己慢慢敲代码摸索。所以我写这个教程的目的一是自己总结,二是与大家分享。
本篇是系列教程的第一篇:入门篇。在这篇文章中,我将以一个简单的demo为例,阐述three.js的基本配置方法。学完这篇文章,你将学会如何在浏览器中绘制一个立体图形!
准备工作
在写代码之前,你首先要去下最新的three.js框架包,在你的页面里引入three.js,当然文件包里面也有不少的demo便于大家学习;
chrome是目前支持webgl最好的浏览器,firefow居其次,国内的遨游、猎豹经测试也可以运行。
从实例开始入门!
首先我们搭建html,如下:
<!doctype html> <html>  <head>  <meta charset="utf-8">  <title>lesson1-by-shawn.xie</title>  <!--引入three.js-->  <script src="three.js"></script>  <style type="text/css">  p#canvas-frame{   border: none;   cursor: move;   width: 1400px;   height: 600px;   background-color: #eeeeee;  }  </style>  </head>  <body>  <!--盛放canvas的容器-->  <p id="container"></p>  </body> </html>
准备和画布框大小一致的领域用于webgl绘制。 具体来说:
(1) body 标签中添加 id 为「canvas3d」的 p 元素。
(2) style 标签中指定 「canvas3d」的css样式。
需要注意的是,我们并不需要写一个<canvas>标签,我们只需要定义好盛放canvas的p就可以,canvas是three.js动态生成的!
下面我们开始写脚本,我们将通过以下五步构建一个简单的立体模型,这也是three.js的基本步骤:
1.设置three.js渲染器
三维空间里的物体映射到二维平面的过程被称为三维渲染。 一般来说我们都把进行渲染操作的软件叫做渲染器。 具体来说要进行下面这些处理。
(0) 声明全局变量(对象);
(1) 获取画布「canvas-frame」的高宽;
(2) 生成渲染器对象(属性:抗锯齿效果为设置有效);
(3) 指定渲染器的高宽(和画布框大小一致);
(4) 追加 【canvas】 元素到 【canvas3d】 元素中;
(5) 设置渲染器的清除色(clearcolor)。
//开启three.js渲染器 var renderer;//声明全局变量(对象) function initthree() {  width = document.getelementbyid('canvas3d').clientwidth;//获取画布「canvas3d」的宽  height = document.getelementbyid('canvas3d').clientheight;//获取画布「canvas3d」的高  renderer=new three.webglrenderer({antialias:true});//生成渲染器对象(属性:抗锯齿效果为设置有效)  renderer.setsize(width, height );//指定渲染器的高宽(和画布框大小一致)  document.getelementbyid('canvas3d').appendchild(renderer.domelement);//追加 【canvas】 元素到 【canvas3d】 元素中。  renderer.setclearcolorhex(0xffffff, 1.0);//设置canvas背景色(clearcolor) }
2.设置摄像机camera
opengl(webgl)中、三维空间中的物体投影到二维空间的方式中,存在透视投影和正投影两种相机。 透视投影就是、从视点开始越近的物体越大、远处的物体绘制的较小的一种方式、和日常生活中我们看物体的方式是一致的。 正投影就是不管物体和视点距离,都按照统一的大小进行绘制、在建筑和设计等领域需要从各个角度来绘制物体,因此这种投影被广泛应用。在 three.js 也能够指定透视投影和正投影两种方式的相机。 本文按照以下的步骤设置透视投影方式。
(0) 声明全局的变量(对象);
(1) 设置透视投影的相机;
(2) 设置相机的位置坐标;
(3) 设置相机的上为「z」轴方向;
(4) 设置视野的中心坐标。
//设置相机  var camera;  function initcamera() {   camera = new three.perspectivecamera( 45, width / height , 1 , 5000 );//设置透视投影的相机,默认情况下相机的上方向为y轴,右方向为x轴,沿着z轴朝里(视野角:fov 纵横比:aspect 相机离视体积最近的距离:near 相机离视体积最远的距离:far)  camera.position.x = 0;//设置相机的位置坐标  camera.position.y = 50;//设置相机的位置坐标  camera.position.z = 100;//设置相机的位置坐标  camera.up.x = 0;//设置相机的上为「x」轴方向  camera.up.y = 1;//设置相机的上为「y」轴方向  camera.up.z = 0;//设置相机的上为「z」轴方向  camera.lookat( {x:0, y:0, z:0 } );//设置视野的中心坐标  }
3.设置场景scene
场景就是一个三维空间。 用 [scene] 类声明一个叫 [scene] 的对象。
//设置场景  var scene;  function initscene() {   scene = new three.scene();  }
4.设置光源light
opengl(webgl)的三维空间中,存在点光源和聚光灯两种类型。 而且,作为点光源的一种特例还存在平行光源(无线远光源)。另外,作为光源的参数还可以进行 [环境光] 等设置。 作为对应, three.js中可以设置 [点光源(point light)] [聚光灯(spot light)] [平行光源(direction light)],和 [环境光(ambient light)]。 和opengl一样、在一个场景中可以设置多个光源。 基本上,都是环境光和其他几种光源进行组合。 如果不设置环境光,那么光线照射不到的面会变得过于黑暗。 本文中首先按照下面的步骤设置平行光源,在这之后还会追加环境光。
(0) 声明全局变量(对象)
(1) 设置平行光源
(2) 设置光源向量
(3) 追加光源到场景
这里我们用「directionallight」类声明一个叫 [light] 的对象来代表平行光源
//设置光源  var light;  function initlight() {   light = new three.directionallight(0xff0000, 1.0, 0);//设置平行光源  light.position.set( 200, 200, 200 );//设置光源向量  scene.add(light);// 追加光源到场景  }
5.设置物体object
这里,我们声明一个球模型
//设置物体  var sphere;  function initobject(){   sphere = new three.mesh(   new three.spheregeometry(20,20), //width,height,depth   new three.meshlambertmaterial({color: 0xff0000}) //材质设定  );  scene.add(sphere);  sphere.position.set(0,0,0);  }
最后,我们写一个主函数执行以上五步:
//执行  function threestart() {  initthree();  initcamera();  initscene();   initlight();  initobject();  renderer.clear();   renderer.render(scene, camera);  }
这时,测试以上程序,你会发现浏览器窗口中出现了你绘制的球形模型
总结
以上就是three.js的入门内容,我们核心的五步就是:
1.设置three.js渲染器
2.设置摄像机camera
3.设置场景scene
4.设置光源light
5.设置物体object
可能其中有些设置你还不太清楚,没关系,后面几篇文章会对以上五个主要步骤进行详细的讲解,敬请期待~~
本例完整代码:
<!doctype html> <html>  <head>  <meta charset="utf-8">  <title>lesson1-by-shawn.xie</title>  <!--引入three.js-->  <script src="three.js"></script>  <script type="text/javascript">  //开启three.js渲染器  var renderer;//声明全局变量(对象)  function initthree() {  width = document.getelementbyid('canvas3d').clientwidth;//获取画布「canvas3d」的宽  height = document.getelementbyid('canvas3d').clientheight;//获取画布「canvas3d」的高  renderer=new three.webglrenderer({antialias:true});//生成渲染器对象(属性:抗锯齿效果为设置有效)  renderer.setsize(width, height );//指定渲染器的高宽(和画布框大小一致)  document.getelementbyid('canvas3d').appendchild(renderer.domelement);//追加 【canvas】 元素到 【canvas3d】 元素中。  renderer.setclearcolorhex(0xffffff, 1.0);//设置canvas背景色(clearcolor)  }  //设置相机  var camera;  function initcamera() {   camera = new three.perspectivecamera( 45, width / height , 1 , 5000 );//设置透视投影的相机,默认情况下相机的上方向为y轴,右方向为x轴,沿着z轴朝里(视野角:fov 纵横比:aspect 相机离视体积最近的距离:near 相机离视体积最远的距离:far)  camera.position.x = 0;//设置相机的位置坐标  camera.position.y = 50;//设置相机的位置坐标  camera.position.z = 100;//设置相机的位置坐标  camera.up.x = 0;//设置相机的上为「x」轴方向  camera.up.y = 1;//设置相机的上为「y」轴方向  camera.up.z = 0;//设置相机的上为「z」轴方向  camera.lookat( {x:0, y:0, z:0 } );//设置视野的中心坐标  }  //设置场景  var scene;  function initscene() {   scene = new three.scene();  }  //设置光源  var light;  function initlight() {   light = new three.directionallight(0xff0000, 1.0, 0);//设置平行光源  light.position.set( 200, 200, 200 );//设置光源向量  scene.add(light);// 追加光源到场景  }  //设置物体  var sphere;  function initobject(){   sphere = new three.mesh(   new three.spheregeometry(20,20), //width,height,depth   new three.meshlambertmaterial({color: 0xff0000}) //材质设定  );  scene.add(sphere);  sphere.position.set(0,0,0);  }  //执行  function threestart() {  initthree();  initcamera();  initscene();   initlight();  initobject();  renderer.clear();   renderer.render(scene, camera);  }  </script>  <style type="text/css">  p#canvas3d{   border: none;   cursor: move;   width: 1400px;   height: 600px;   background-color: #eeeeee;  }  </style>  </head>  <body onload='threestart();'>  <!--盛放canvas的容器-->  <p id="canvas3d"></p>  </body> </html>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
angular js操作用户的方法
token怎么储存在客户端localstorage内
angularjs购物车结算时全选反选z
以上就是three.js使用详解的详细内容。
其它类似信息

推荐信息