<!DOCTYPE html><html>
<head>
<meta charset="UTF-8">
<title>lesson1-by-shawn.xie</title>
<!--引入Three.js-->
<script src="Three.js"></script>
<style type="text/css">
div#canvas-frame{
border: none;
cursor: move;
width: 1400px;
height: 600px;
background-color: #EEEEEE;
}
</style>
</head>
<body>
<!--盛放canvas的容器-->
<div id="container"></div>
</body></html>准备和画布框大小一致的领域用于WebGL绘制。 具体来说:
(1) body 标签中添加 id 为「canvas3d」的 div 元素。
(2) style 标签中指定 「canvas3d」的CSS样式。
需要注意的是,我们并不需要写一个<canvas>标签,我们只需要定义好盛放canvas的div就可以,canvas是three.js动态生成的!
下面我们开始写脚本,我们将通过以下五步构建一个简单的立体模型,这也是three.js的基本步骤:
1.设置three.js渲染器三维空间里的物体映射到二维平面的过程被称为三维渲染。 一般来说我们都把进行渲染操作的软件叫做渲染器。 具体来说要进行下面这些处理。
(0) 声明全局变量(对象);
(1) 获取画布「canvas-frame」的高宽;
(2) 生成渲染器对象(属性:抗锯齿效果为设置有效);
(3) 指定渲染器的高宽(和画布框大小一致);
(4) 追加 【canvas】 元素到 【canvas3d】 元素中;
(5) 设置渲染器的清除色(clearColor)。
//开启Three.js渲染器var renderer;//声明全局变量(对象)function initThree() {
width = document.getElementById('canvas3d').clientWidth;//获取画布「canvas3d」的宽
height = document.getElementById('canvas3d').clientHeight;//获取画布「canvas3d」的高
renderer=new THREE.WebGLRenderer({antialias:true});//生成渲染器对象(属性:抗锯齿效果为设置有效)
renderer.setSize(width, height );//指定渲染器的高宽(和画布框大小一致)
document.getElementById('canvas3d').appendChild(renderer.domElement);//追加 【canvas】 元素到 【canvas3d】 元素中。
renderer.setClearColorHex(0xFFFFFF, 1.0);//设置canvas背景色(clearColor)} 2.设置摄像机camera //设置相机
var camera; function initCamera() {
camera = new THREE.PerspectiveCamera( 45, width / height , 1 , 5000 );//设置透视投影的相机,默认情况下相机的上方向为Y轴,右方向为X轴,沿着Z轴朝里(视野角:fov 纵横比:aspect 相机离视体积最近的距离:near 相机离视体积最远的距离:far)
camera.position.x = 0;//设置相机的位置坐标
camera.position.y = 50;//设置相机的位置坐标
camera.position.z = 100;//设置相机的位置坐标
camera.up.x = 0;//设置相机的上为「x」轴方向
camera.up.y = 1;//设置相机的上为「y」轴方向
camera.up.z = 0;//设置相机的上为「z」轴方向
camera.lookAt( {x:0, y:0, z:0 } );//设置视野的中心坐标
}
场景就是一个三维空间。 用 [Scene] 类声明一个叫 [scene] 的对象。
//设置场景
var scene; function initScene() {
scene = new THREE.Scene();
} //设置光源
var light; function initLight() {
light = new THREE.DirectionalLight(0xff0000, 1.0, 0);//设置平行光源
light.position.set( 200, 200, 200 );//设置光源向量
scene.add(light);// 追加光源到场景
} //设置物体
var sphere; function initObject(){
sphere = new THREE.Mesh( new THREE.SphereGeometry(20,20), //width,height,depth
new THREE.MeshLambertMaterial({color: 0xff0000}) //材质设定 );
scene.add(sphere);
sphere.position.set(0,0,0);
} //执行
function threeStart() {
initThree();
initCamera();
initScene();
initLight();
initObject();
renderer.clear();
renderer.render(scene, camera);
}<!DOCTYPE html><html>
<head>
<meta charset="UTF-8">
<title>lesson1-by-shawn.xie</title>
<!--引入Three.js-->
<script src="Three.js"></script>
<script type="text/javascript">
//开启Three.js渲染器
var renderer;//声明全局变量(对象)
function initThree() {
width = document.getElementById('canvas3d').clientWidth;//获取画布「canvas3d」的宽 height = document.getElementById('canvas3d').clientHeight;//获取画布「canvas3d」的高 renderer=new THREE.WebGLRenderer({antialias:true});//生成渲染器对象(属性:抗锯齿效果为设置有效) renderer.setSize(width, height );//指定渲染器的高宽(和画布框大小一致) document.getElementById('canvas3d').appendChild(renderer.domElement);//追加 【canvas】 元素到 【canvas3d】 元素中。 renderer.setClearColorHex(0xFFFFFF, 1.0);//设置canvas背景色(clearColor) } //设置相机
var camera; function initCamera() {
camera = new THREE.PerspectiveCamera( 45, width / height , 1 , 5000 );//设置透视投影的相机,默认情况下相机的上方向为Y轴,右方向为X轴,沿着Z轴朝里(视野角:fov 纵横比:aspect 相机离视体积最近的距离:near 相机离视体积最远的距离:far) camera.position.x = 0;//设置相机的位置坐标 camera.position.y = 50;//设置相机的位置坐标 camera.position.z = 100;//设置相机的位置坐标 camera.up.x = 0;//设置相机的上为「x」轴方向 camera.up.y = 1;//设置相机的上为「y」轴方向 camera.up.z = 0;//设置相机的上为「z」轴方向 camera.lookAt( {x:0, y:0, z:0 } );//设置视野的中心坐标 } //设置场景
var scene; function initScene() {
scene = new THREE.Scene();
} //设置光源
var light; function initLight() {
light = new THREE.DirectionalLight(0xff0000, 1.0, 0);//设置平行光源 light.position.set( 200, 200, 200 );//设置光源向量 scene.add(light);// 追加光源到场景 } //设置物体
var sphere; function initObject(){
sphere = new THREE.Mesh( new THREE.SphereGeometry(20,20), //width,height,depth
new THREE.MeshLambertMaterial({color: 0xff0000}) //材质设定 );
scene.add(sphere);
sphere.position.set(0,0,0);
} //执行
function threeStart() {
initThree();
initCamera();
initScene();
initLight();
initObject();
renderer.clear();
renderer.render(scene, camera);
} </script>
<style type="text/css">
div#canvas3d{
border: none;
cursor: move;
width: 1400px;
height: 600px;
background-color: #EEEEEE;
}
</style>
</head>
<body onload='threeStart();'>
<!--盛放canvas的容器-->
<div id="canvas3d"></div>
</body></html>
查看更多关于【three.js详解之一】入门教程的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did31444