在网页显示出三维的效果,就要借助于视觉效果来实现,比如阴影、光照等。这里的Camera相当于人的眼睛,从坐标的那个视点去观察目标,相当于投影出立体感。这里首先说一下坐标。如下图所示:
z轴正方向指向屏幕外,x,y平面在屏幕上。坐标原点(0,0,0)默认为场景中心。照相机就是在这个坐标系中定义的。有两种投影方式:正交投影和透视投影。对于这两种投影具体介绍,请点击这里了解更多。
正交投影照相机(Orthographic Camera),构造函数:
OrthographicCamera( left, right, top, bottom, near, far )
//创建场景
var scene=new THREE.Scene();
//创建相机
var camera_Or=new THREE.OrthographicCamera(window.innerWidth/-2,window.innerWidth/2,window.innerHeight/2,window.innerHeight/-2,1,1000);
//camera_Or.position.z=5;
camera_Or.position.set(50,-20,50);
camera_Or.lookAt(new THREE.Vector3(0,0,0))
scene.add(camera_Or);
drawAxes(scene);
//创建渲染器
var renderer=new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth,window.innerHeight);
document.body.appendChild(renderer.domElement);
//创建立方体
var geometry=new THREE.BoxGeometry(100,100,100);
var material=new THREE.MeshBasicMaterial({color:0x00ff00
,wireframe:true
});
var cube=new THREE.Mesh(geometry,material);
scene.add(cube);
//渲染场景
var render=function(){
requestAnimationFrame(render);
renderer.render(scene,camera_Or);
}
render();
//创建场景
var scene=new THREE.Scene();
//创建相机
//var camera_Or=new THREE.OrthographicCamera(window.innerWidth/-2,window.innerWidth/2,window.innerHeight/2,window.innerHeight/-2,1,1000);
//camera_Or.position.z=5;
//camera_Or.position.set(50,-20,50);
//camera_Or.lookAt(new THREE.Vector3(0,0,0));
var camera_Pe = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );
//camera_Pe.position.x=5;
//camera_Pe.position.y=5;
//camera_Pe.position.z=10;
//设置照相机的位置
camera_Pe.position.set(3,5,5);
//照相机默认是沿着z轴负方向观察的,改为(10,10,10)这个观察点后肯定看到的是一片漆黑
//所以,应该指向原点才能看到物体
camera_Pe.lookAt(new THREE.Vector3(0,0,0));
//camera_Pe.view=(0,0,5);
scene.add(camera_Pe);
//drawAxes(scene);
//创建渲染器
var renderer=new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth,window.innerHeight);
document.body.appendChild(renderer.domElement);
//创建立方体
var geometry=new THREE.BoxGeometry(1,1,1);
var material=new THREE.MeshBasicMaterial({color:0x00ff00
,wireframe:true
});
var cube=new THREE.Mesh(geometry,material);
scene.add(cube);
//渲染场景
var render=function(){
requestAnimationFrame(render);
renderer.render(scene,camera_Pe);
}
render();
参考资料:
http://my.oschina.net/u/1455799/blog/213962
http://www.ituring.com.cn/article/48665