three.js之投影矩阵

时间:2021-08-14 03:07:56

three.js之投影矩阵

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Three框架</title>
		<script src="../js/Three.js"></script>
		<style type="text/css">
			div#canvas-frame {
				border: none;
				cursor: pointer;
				width: 100%;
				height: 600px;
				background-color: #EEEEEE;
			}

		</style>
		<script>
			//创建渲染器
            var renderer;
            function initThree() {
                width = document.getElementById('canvas-frame').clientWidth;
                height = document.getElementById('canvas-frame').clientHeight;
                renderer = new THREE.WebGLRenderer({
                    antialias : true//开启抗锯齿
                });
                renderer.setSize(width, height);//设置渲染窗口大小
				//渲染结果在 id为 canvas-frame 的div中显示
                document.getElementById('canvas-frame').appendChild(renderer.domElement);
                renderer.setClearColorHex(0xFFFFFF, 1.0);
            }

            //初始化摄像机
            var camera;
            function initCamera() {
                camera = new THREE.PerspectiveCamera(45, width / height, 1, 10000);
				//camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, 10, 1000 );
                camera.position.x = 0;
                camera.position.y = 0;
                camera.position.z = 600;
                camera.up.x = 0;
                camera.up.y = 1;
                camera.up.z = 0;
                camera.lookAt({
                    x : 0,
                    y : 0,
                    z : 0
                });
            }

            var scene;
            function initScene() {
                scene = new THREE.Scene();
            }

            var light;
            function initLight() {
                light = new THREE.PointLight(0x00FF00);
                light.position.set(0, 0,300);
                scene.add(light);
            }

     		var cube;
            function initObject() {
				var geometry=new THREE.CylinderGeometry(70,100,200);
				var material=new THREE.MeshLambertMaterial({color:0xFFFFF});
				var mesh=new THREE.Mesh(geometry,material);
				mesh.position=new THREE.Vector3(0,-20,-20);
				scene.add(mesh);
            }

            function threeStart() {
                initThree();
                initCamera();
                initScene();
                initLight();
                initObject();
                animation();

            }

            function animation() {
				changeFov();
				renderer.render(scene,camera);
				requestAnimationFrame(animation)
            }

            //更新投影矩阵
            function setCameraFov(fov) {
				camera.fov=fov;
				camera.updateProjectionMatrix();
            }

            function changeFov() {
				var txtFov=document.getElementById("txtFov").value;
				var val=parseFloat(txtFov);
				setCameraFov(val);
            }
		</script>
	</head>

	<body onload="threeStart();">
		<div id="canvas-frame"></div>
		<div>
			Fov:<input type="text" value="45" id="txtFov"/>(0到180的值)
		</div>
	</body>
</html>