<script type="text/javascript"> functioninit(){ //渲染器 var renderer = new THREE.WebGLRenderer({ canvas:document.getElementById('mainCanvas') }); renderer.setClearColor(0x000000); //场景 var scene = new THREE.Scene(); //照相机 var camera = new THREE.OrthographicCamera(-10, 10, 7.5, -7.5, 0.1, 100); camera.position.set(-5, 25, 25); camera.lookAt(new THREE.Vector3(0, 0, 0)); scene.add(camera);
//光照 var light = new THREE.PointLight(0xffffff, 1, 1000); light.position.set(15,25,20); scene.add(light);
//材质 // var texture = THREE.ImageUtils.loadTexture('img',{},function(){ // renderer.render(scene,camera); // });
var materials = []; for(var i = 0;i<6;++i){ materials.push(new THREE.MeshBasicMaterial({ map: THREE.ImageUtils.loadTexture('img/'+i+'.png',{},function(){ renderer.render(scene,camera); }), overdraw: true
})); }
var cube = new THREE.Mesh(new THREE.CubeGeometry(5,5,5), new THREE.MeshFaceMaterial(materials)); scene.add(cube);
<script type="text/javascript"> functioninit(){ //渲染器 var renderer = new THREE.WebGLRenderer({ canvas:document.getElementById('mainCanvas') }); renderer.setClearColor(0x000000); //场景 var scene = new THREE.Scene(); //照相机 var camera = new THREE.OrthographicCamera(-10, 10, 7.5, -7.5, 0.1, 100); camera.position.set(0, 0, 25); camera.lookAt(new THREE.Vector3(0, 0, 0)); scene.add(camera);
//光照 var light = new THREE.PointLight(0xffffff, 1, 1000); light.position.set(15,25,20); scene.add(light);
<script type="text/javascript"> functioninit(){ //渲染器 var renderer = new THREE.WebGLRenderer({ canvas:document.getElementById('mainCanvas') }); renderer.setClearColor(0x000000); //场景 var scene = new THREE.Scene(); //照相机 var camera = new THREE.OrthographicCamera(-2.5, 2.5, 1.875, -1.875, 0.1, 100); camera.position.set(5, 5, 20); camera.lookAt(new THREE.Vector3(1, 0, 0)); scene.add(camera);
var material = new THREE.MeshBasicMaterial({ color: 0xffff00 });
var loader = new THREE.FontLoader(); loader.load('gothical_Regular.json', function(font) { var mesh = new THREE.Mesh(new THREE.TextGeometry('Hello', { font: font, size: 1, height: 1 }), material); scene.add(mesh); //渲染 renderer.render(scene,camera); }); } </script>
<script type="text/javascript"> functioninit(){ //渲染器 var renderer = new THREE.WebGLRenderer({ canvas:document.getElementById('mainCanvas') }); renderer.setClearColor(0x000000); //场景 var scene = new THREE.Scene(); //照相机 var camera = new THREE.OrthographicCamera(-2, 2, 1.5, -1.5, 1, 10); camera.position.set(4, -3, 5); camera.lookAt(new THREE.Vector3(0, 0, 0)); scene.add(camera); //创建边长为1的红色正方体,wireframe(线框)材质,以便看到正方体后方的边 var cube = new THREE.Mesh(new THREE.CubeGeometry(1,1,1), new THREE.MeshBasicMaterial({ color:0xff0000, wireframe:true }) ); scene.add(cube); //渲染 renderer.render(scene,camera); } </script>
6.透视投影照相机(Perspective Camera)
THREE.PerspectiveCamera(fov, aspect, near, far)
fov — 相机视锥体竖直方向上的张角(是角度制而非弧度制) aspect — 相机视锥体宽高比width / height,通常设为Canvas 的横纵比例 near — 相机视锥体近裁剪面 far — 相机视锥体远裁剪面
<script type="text/javascript"> functioninit(){ //渲染器 var renderer = new THREE.WebGLRenderer({ canvas:document.getElementById('mainCanvas') }); renderer.setClearColor(0x000000); //场景 var scene = new THREE.Scene(); //照相机 var camera = new THREE.PerspectiveCamera(60, 400 / 300, 1, 10); camera.position.set(0, 0, 5); scene.add(camera); //创建边长为1的红色正方体,wireframe(线框)材质,以便看到正方体后方的边 var cube = new THREE.Mesh(new THREE.CubeGeometry(1,1,1), new THREE.MeshBasicMaterial({ color:0xff0000, wireframe:true }) ); scene.add(cube); //渲染 renderer.render(scene,camera); } </script>