<!DOCTYPE html>
<html>
<head>
<title>Example 01.02 - First Scene</title>
<script type="text/javascript" src="../libs/three.js"></script>
<style>
body {
/* set margin to 0 and overflow to hidden, to go fullscreen */
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<!-- Div which will hold the Output -->
<div id="WebGL-output">
</div>
<!-- Javascript code that runs our Three.js examples -->
<script type="text/javascript">
// once everything is loaded, we run our Three.js stuff.
function init() {
// create a scene, that will hold all our elements such as objects, cameras and lights.
var scene = new THREE.Scene(); //scene是一个容器,用来保存并跟踪所有我们想渲染的物体
// create a camera, which defines where we're looking at.
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
//camera决定了我们能在渲染好的scene里看到什么
// create a render and set the size
var renderer = new THREE.WebGLRenderer(); //创建一个webGLRenderer对象,使用计算上的显卡来渲染场景
renderer.setClearColorHex(0xEEEEEE);
//通过调用setClearColorHex()函数将renderer的背景色设置为白色
//或者可以这样写renderer.setClearColor(new THREE.Color(0xEEEEEE));
renderer.setSize(window.innerWidth, window.innerHeight);//setSize函数告诉renderer将scene渲染成多大尺寸
// show axes in the screen
var axes = new THREE.AxisHelper(20); //创建一个坐标轴对象
scene.add(axes); //使用scene.add()函数将坐标轴添加到场景中
// create the ground plane
var planeGeometry = new THREE.PlaneGeometry(60, 20);//定义平面的宽60,高20
var planeMaterial = new THREE.MeshBasicMaterial({color: 0xcccccc}); //创建一个基本的材质,并设定平面的颜色
var plane = new THREE.Mesh(planeGeometry, planeMaterial); //把上面两个对象合并到一个名为plane的Mesh对象中
// rotate and position the plane.在将变量添加到scene之前,先将它放到合适的位置
plane.rotation.x = -0.5 * Math.PI; //绕x轴转动90度
plane.position.x = 15; //position:决定该对象相对其父对象的位置,这里的父对象是THREE.Scene()
plane.position.y = 0;
plane.position.z = 0;
// add the plane to the scene
scene.add(plane);
var shape = new THREE.Shape();//创建Shape对象
var points = [//定位定点
new THREE.Vector2(50,60),
new THREE.Vector2(25,0),
new THREE.Vector2(50,-60)
];
shape.splineThru(points);//顶点带入样条插值计算函数
var splinePoints = shape.getPoints(20);//插值计算细分数20
var geometry = new THREE.LatheGeometry(splinePoints,30);//旋转造型
var material=new THREE.MeshPhongMaterial({
color:0x0000ff,//三角面颜色
side:THREE.DoubleSide//两面可见
});//材质对象
material.wireframe = true;//线条模式渲染(查看细分数)
var mesh=new THREE.Mesh(geometry,material);//旋转网格模型对象
scene.add(mesh);//旋转网格模型添加到场景中
// create a cube 创建一个方块并设置方块的位置
/*var cubeGeometry = new THREE.BoxGeometry(4, 4, 4);
var cubeMaterial = new THREE.MeshBasicMaterial({color: 0xff0000, wireframe: true});
var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
// position the cube
cube.position.x = -4;
cube.position.y = 3;
cube.position.z = 0;
// add the cube to the scene
scene.add(cube);
*/
// create a sphere 创建一个球体,同上方法
/* var sphereGeometry = new THREE.SphereGeometry(4, 20, 20);
var sphereMaterial = new THREE.MeshBasicMaterial({color: 0x7777ff, wireframe: true});
var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
// position the sphere
sphere.position.x = 20;
sphere.position.y = 4;
sphere.position.z = 2;
// add the sphere to the scene
scene.add(sphere);
*/
// position and point the camera to the center of the scene
camera.position.x = -30;
camera.position.y = 40;
camera.position.z = 30;
camera.lookAt(scene.position);//使用lookAt()函数,将相机对准场景的中心
// add the output of the renderer to the html element将renderer的输出挂到HTML页面中
document.getElementById("WebGL-output").appendChild(renderer.domElement);
// render the scene
renderer.render(scene, camera);
}
window.onload = init;
</script>
</body>
</html>
three.js 学习心得(一)
最新推荐文章于 2024-07-08 09:56:34 发布