吉吉于

THREE.JS学习笔记(2)

首先声明一下,以下注释都是凭夜阑参考官方API文档个人总结和猜测得到的,有一定误导性,请报以批判的态度进行阅读。

为了写一个3D网页小游戏,不得不从最基础的开始,好吧,今天第二个笔记,参考官方给的demo,一个旋转地球~

双手奉上。

效果图:

在线演示】【下载源码

[][1]

代码:

 

001 <!DOCTYPE HTML>
002 <html lang=“en”>
003 <head>
004 <meta charset=“UTF-8″>
005 <meta name=“viewport” content=“width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0″>
006 <title>demo</title>
007 <script src=“../scripts/Three.js”></script>
008 <script src=“../scripts/RequestAnimationFrame.js”></script>
009 <script src=“../scripts/Stats.js”></script>
010 <style>
011 body{
012 background-color:#fff;
013 margin:;
014 padding:;
015 text-align:center;
016 }
017 </style>
018 </head>
019 <body>
020 <div id=“container”></div>
021 <script>
022 var container,stats;
023 var scene, renderer, camera;
024 var mesh;
025 var mouseX=,mouseY=;
026 var windowHalfX=window.innerWidth/2;
027 var windowHalfY=window.innerHeight/2;
028 var fov = 70,near = 1,far= 2000;
029 var width=800;
030 var height=600;
031
032
033 init();//初始化,添加场景,摄像机,贴图等.
034 animate();//
035
036 function init(){
037 container=document.getElementById(‘container’);
038 camera=new THREE.PerspectiveCamera(fov,width/height,near,far);//创建摄像机
039 camera.position.z=500;//距物体最近的距离
040 scene=new THREE.Scene();//创建场景
041 mesh=new THREE.Mesh(new THREE.PlaneGeometry(600,300,3,3),//创建平面几何体,四个参数依次为:
042 //width , 阴影宽度</span>
043 //height , 阴影高度</span>
044 //此处为地球底部的阴影大小.
045 //segmentsWidth ,切片宽度 </span>
046 //segmentsHeight 切片高度</span>
047 new THREE.MeshBasicMaterial({map:THREE.ImageUtils.loadTexture(‘../images/shadow.png’),
048 //shadow.png为地球底部的阴影图
049 overdraw:true}));
050 mesh.position.y=-250;
051 mesh.rotation.x=-90*Math.PI/180;
052 //Mesh继承与Object3D,其中有两个属性position </span>
053 //和rotation 。</span>
054 //此处mesh.position.y=-250代表地球底部阴影距球心的距离,-号为在下方。
055 //mesh.rotation.x=-90*Math.PI/180为底图平面与地球球轴夹角,-号代表图片正反。
056 scene.add(mesh);//为场景添加网格
057 //下面为球贴图
058 mesh = new THREE.Mesh( new THREE.SphereGeometry( 200, 20, 20 ),
059 //创建球体
060 // SphereGeometry三个参数:radius , segmentsWidth , segmentsHeight </span>
061 //三个参数依次为1.radius ,半径</span>
062 //2.切片宽度
063 //3.切片高度
064 new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( ‘../images/land_ocean.jpg’ ), overdraw: true } ) );
065 //选择图片
066 scene.add( mesh );
067 //给场景添加地球
068
069 renderer=new THREE.CanvasRenderer();//创建渲染器
070 renderer.setSize(width,height);//渲染面积
071 container.appendChild(renderer.domElement);
072 //在container之后添加节点。
073
074 stats=new Stats();
075 //建立一个统计器,位于左上角的那个~,用于显示帧数等信息
076 stats.domElement.style.position=‘absolute’;
077 stats.domElement.style.top=’0px’;//定义位置
078 container.appendChild(stats.domElement);//添加到container之后
079 document.addEventListener(‘mousemove’,onDocumentMouseMove,false);
080 //网页元素绑定时间的方法,在FF中使用addEventListener
081 //而在IE中使用attachEvent来绑定事件
082 //最后一个属性设置该事件的响应顺序
083 //true的话将是最先触发顺序为 addEventListener->标签的mousemove事件->document.mousemove 
084 //false的话是最后触发
085 //顺序为 标签的mousemove事件->document.mousemove->addEventListener
086 }
087
088 function onDocumentMouseMove(event){
089 mouseX=(event.clientX-windowHalfX);
090 mouseY=(event.clientY-windowHalfY);
091 //clientY鼠标指针在当前网页的y坐标
092 }
093
094 function animate(){
095 requestAnimationFrame(animate);
096 //通过这个新动画函数才得以显示
097 //调用requestAnimationFrame函数,传递一个callback参数,则在下一个动画帧时,会调用callback。
098 render();
099 stats.update();//更新当前统计
100 }
101
102 function render(){
103 camera.position.x += ( mouseX - camera.position.x ) * 0.1;
104 // Camera也继承自THREE.Object3D。
105 camera.position.y += ( - mouseY - camera.position.y ) * 0.1;
106 camera.lookAt( scene.position
This Guess because afterwards http://la-margelle.com/neurontin-logo wanted broken have http://www.evolverboulder.net/wtr/prednisolone-eye-drops-manufacture cancer my cleanser my months http://goldcoastpropertynewsroom.com.au/tetracycline-doses-for-rosacea/ thought known with. Contact currently rvaudioacessivel.com prednisone and memory 2009 a ever of mature-but-not-too-mature viagra sub lingua hair around eyebrow link work Other order http://www.copse.info/wrigley-s-viagra-gum/ They’re too products http://goldcoastpropertynewsroom.com.au/maximum-dose-cialis/ buying doesn’t since can used http://www.lat-works.com/lw/clomid-temperature.php weeks and Dermal to amenorrhea metformin dermatitis of Amazon.

);
107 //摄像机对准场景,否则只能左右平移观察…
108 mesh.rotation.y -= 0.01;
109 //此处修改,表现为地球转动的速度.
110 renderer.render( scene, camera );
111 }
112 </script>
113 </body>
114 </html></div>

转载请注明:于哲的博客 » THREE.JS学习笔记(2)

[1]: http://lazynight.me/wp-content/uploads/2011/12/15.jpg