本节从webgl中文网学习:http://www.hewebgl.com/article/getarticle/60
<!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);
document.getElementById('canvas-frame').appendChild(renderer.domElement);
renderer.setClearColor(0xFFFFFF, 1.0);
}
//相机
var camera;
function initCamera() {
camera = new THREE.PerspectiveCamera(45, width / height, 1, 10000);
camera.position.x = 600;
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.AmbientLight(0x00FF00); //环境光,影响整个环境,亮度一样
light.position.set(100, 100, 200);
scene.add(light);
//********* 平行光 **********
//light = new THREE.DirectionalLight(0xFF0000, 1); //平行光(颜色,强度),定向光,按照方向
//light.position.set(0, 0, 1);
//scene.add(light);
//********* 点光源 **********
//light = new THREE.PointLight(0x00FF00, 1, 400, 1); //点光源(颜色,强度,距离,衰变)
//light.position.set(0, 0, 0);
//scene.add(light);
//********* 聚光灯 **********
light = new THREE.SpotLight(0xFF0000, 3, 1000, 1); //聚光灯(颜色,强度,距离,衰变)
light.position.set(0, 0, 500);
scene.add(light);
}
//多维数据集
var cube;
function initObject() {
var geometry = new THREE.CubeGeometry( 200, 100, 50,4,4);
var material = new THREE.MeshLambertMaterial({ color: 0xFFFFFF });
var mesh = new THREE.Mesh( geometry,material);
mesh.position = new THREE.Vector3(0, 0, 0);
scene.add(mesh);
var mesh2 = new THREE.Mesh(geometry, material);
mesh2.position.set(-300, 0, 0);
scene.add(mesh2);
var mesh3 = new THREE.Mesh(geometry, material);
mesh3.position.set(0, -150, 0);
scene.add(mesh3);
var mesh4 = new THREE.Mesh(geometry, material);
mesh4.position.set(0, 150, 0);
scene.add(mesh4);
var mesh5 = new THREE.Mesh(geometry, material);
mesh5.position.set(300, 0, 0);
scene.add(mesh5);
var mesh6 = new THREE.Mesh(geometry, material);
mesh6.position.set(0, 0, -100);
scene.add(mesh6);
}
//启动函数
function threeStart() {
initThree();
initCamera();
initScene();
initLight();
initObject();
renderer.clear();
renderer.render(scene, camera);
}
</script>
</head>
<body οnlοad="threeStart();">
<div id="canvas-frame"></div>
</body>
</html>
//www.hewebgl.com/article/getarticle/60