官网地址:https://threejs.org/docs/index.html#manual/zh/introduction/Creating-a-scene
官网demo:一个自动旋转的正方形
这个tree.js主要用的是3D模型图,图可是gitf,3D max制作3D模型
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<script src="../js/tree.js"></script>
</head>
<body>
<div id="test01" style="width: 50%;height: 50%;">
</div>
<script>
var scene = new THREE.Scene();
//创建一个透视相机:第一个参数:第一视角的角度,通常是指弧度,第二个参数:长宽比比如说,当你在一个宽屏电视上播放老电影时,可以看到图像仿佛是被压扁的
//第三个和第四参数:近截面(near)和远截面(far)
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
//web页面渲染器
var renderer = new THREE.WebGLRenderer();
//设置渲染器宽高
renderer.setSize( window.innerWidth, window.innerHeight );
//将渲染器添加到<body>下
document.body.appendChild( renderer.domElement );
//我将渲染器添加到<div>
// document.getElementById("test01").appendChild(renderer.domElement);
//创建一个立方体
var geometry = new THREE.BoxGeometry();
//创建一个材质:颜色绿色
var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
//创建一张网,放入立方体和材质
var cube = new THREE.Mesh( geometry, material );
//向摄像机添加物品
scene.add( cube );
//让相机z轴向前5像素:z轴越大,立方体距离越远(小)
camera.position.z = 5;
//通过循环调用让立方体动起来
var animate = function () {
//使用该方法让立方体动起来
requestAnimationFrame( animate );
//让立方体旋转:沿网格的x和y轴滚动
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render( scene, camera );
};
animate();
</script>
</body>
</html>
画的一条线
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>画线</title>
<script src="../js/tree.js"></script>
</head>
<body>
<script>
//创建web渲染器
var renderer = new THREE.WebGLRenderer();
//渲染器宽高(不受文档元素控制)
renderer.setSize( window.innerWidth, window.innerHeight );
//向body元素添加渲染器
document.body.appendChild( renderer.domElement );
//创建相机
var camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 500 );
camera.position.set( 0, 0, 100 );
camera.lookAt( 0, 0, 0 );
//创建
var scene = new THREE.Scene();
//new一个线材质对象并设置颜色
var material = new THREE.LineBasicMaterial( { color: 0x0000ff } );
//一个带有定点的几何体(下面这个画出来就是一个向上的箭头)根据x,y,z三轴
// var points = [];
// points.push( new THREE.Vector3( - 10, 0, 0 ) );
// points.push( new THREE.Vector3( 0, 10, 0 ) );
// points.push( new THREE.Vector3( 10, 0, 0 ) );
// var geometry = new THREE.BufferGeometry().setFromPoints( points );//创建几何体
//我自己画一个折线
var points = [];
points.push( new THREE.Vector3( - 10, 0, 0 ) );
points.push( new THREE.Vector3( 0, 10, 0 ) );
points.push( new THREE.Vector3( 10, 0, 0 ) );
points.push( new THREE.Vector3( 20, 10, 0 ) );
points.push( new THREE.Vector3( 30, 0, 0 ) );
points.push( new THREE.Vector3( 40, 10, 0 ) );
var geometry = new THREE.BufferGeometry().setFromPoints( points );//创建几何体
//将几何体和线材质对象组成一条线
var line = new THREE.Line( geometry, material );
//向场景添加线
scene.add( line );
//向渲染器提供场景和相机
renderer.render( scene, camera );
</script>
</body>
</html>