Three.js lesson 1 建立一个球体
2013-01-10 09:40
253 查看
(文章来源一个日本鬼子)
<!DOCTYPEhtml> <html> <head> <metacharset="UTF-8"> <title>Three.jsLesson</title> <!--引入Three.js--> <scriptsrc="../src/Three.js"></script> <styletype="text/css"> div#canvas3d{ border:none; cursor:move; width:1400px; height:600px; background-color:#EEEEEE; } </style> <scripttype="text/javascript"defer="defer"> //开启Three.js渲染器 varrenderer; functioninitThree(){ width=document.getElementById('canvas3d').clientWidth; height=document.getElementById('canvas3d').clientHeight; //生成渲染器对象(属性:抗锯齿效果为设置有效) renderer=newTHREE.WebGLRenderer({antialias:true}); renderer.setSize(width,height); //追加【canvas】元素到【canvas3d】元素中。 document.getElementById('canvas3d').appendChild(renderer.domElement); renderer.setClearColorHex(0xFFFFFF,1.0); } //设置相机 varcamera; functioninitCamera(){ //设置透视投影的相机,默认情况下相机的上方向为Y轴,右方向为X轴,沿着Z轴朝里 camera=newTHREE.PerspectiveCamera(45,width/height,1,5000);//(视野角:fov纵横比:aspect相机离视体积最近的距离:near相机离视体积最远的距离:far) //位置坐标 camera.position.x=0; camera.position.y=50; camera.position.z=100; //轴方向 camera.up.x=0; camera.up.y=1; camera.up.z=0; //视野中心坐标 camera.lookAt({ x:0, y:0, z:0 }); } //设置场景 varscene; functioninitScene(){ scene=newTHREE.Scene(); } //设置光源 varlight; functioninitLight(){ light=newTHREE.DirectionalLight(0xff0000,1.0,0);//设置平行光源 light.position.set(200,200,200);//设置光源向量 scene.add(light);//追加光源到场景 } //设置物体 varsphere; functioninitObject(){ sphere=newTHREE.Mesh( newTHREE.SphereGeometry(20,20), newTHREE.MeshLambertMaterial({color:0xff0000})//材质设定 ); scene.add(sphere); sphere.position.set(0,0,0); } //执行 functionthreeStart(){ initThree(); initCamera(); initScene(); initLight(); initObject(); renderer.clear(); renderer.render(scene,camera); } </script> </head> <bodyonload='threeStart();'> <!--盛放canvas的容器--> <divid="canvas3d"></div> </body> </html>
设置相机
varcamera=NEWTHREE.PerspectiveCamera(fov,Aspect,near,FAR);
设置光源
包括聚光源spotlightsource和点光源pointsource,平行光parallellight是点光源的特例,例子里是平行光。
其他光源:
varlight=NEWTHREE.AmbientLight(hex);//ambientlight
varlight=NEWTHREE.PointLight(hex,Intensity,Distance);//pointlightsource
varlight=NEWTHREE.SpotLight(hex,Intensity,Distance,CastShadow);//lightsourceSpotLight
设置物体
THREE.CubeGeometry(width,height,depth,segmentsWidth,segmentsHeight,segmentsDepth,materials,sides);//立方体
THREE.CylinderGeometry(radiusTop,radiusBottom,height,segmentsRadius,segmentsHeight,openEnded);//圆锥体
THREE.OctahedronGeometry(radius,detail)//八面体
THREE.PlaneGeometry(width,height,segmentsWidth,segmentsHeight);//平面
THREE.SphereGeometry(radius,segmentsWidth,segmentsHeight,phiStart,phiLength,thetaStart,thetaLength);//球体
THREE.TorusGeometry(radius,tube,segmentsR,segmentsT,arc)type//圆环
相关文章推荐
- Three.js 第一篇:绘制一个静态的3D球体
- js入门:建立一个图片库
- three.js 创建一个立方体
- WebGL With Three.js – Lesson 6(转)
- three.js制作一个立体几何体
- 使用three.js画一个三角形
- three.js使用二:修复three.js的纹理路径多一个\的问题
- 学习用node.js建立一个简单的web服务器
- 用json方式实现在 js 中建立一个map
- 如何用webgl(three.js)搭建一个3D库房
- lesson 11:使用TCP协议编写一个网络程序,设置服务器端的监听端口是8002,当与客户端建立连接后,服务器端向客户端发送数据“Hello, world”,客户端收到数据后打印输出。
- <<node.js探秘>>(02).建立一个http站点服务.
- 构建一个简单的Three.js 示例
- 如何用webgl(three.js)搭建一个3D库房
- Three.js / DOC (一) 创建一个场景
- js结合拍php建立倒计时功能(一个页面一个或者多个)
- 使用React+Three.js 封装一个三维地球
- 如何用webgl(three.js)搭建一个3D库房
- 第一个Three.js程序——动手写一个简单的场景
- 【Android Training - 03】使用Fragments建立动态的UI [ Lesson 2 - 新建一个Fragment ]