three.js 小例子分析

three.js 小例子分析_第1张图片

这是一个官方的小例子就是分析学习下。
https://threejs.org/examples/#webgl_geometry_extrude_shapes

开头的引入js没什么好说



//这是鼠标控制的工具

接着分析代码

var camera, scene, renderer, controls;

init();
animate();

开头简单例子就帮忙总结了一下three.js 大舞台搭建

  1. camera   摄像机 
  2. scene     场景(画板)
  3. renderer  渲染器 (画笔)
  4. controls   这里就是场景画面的控制器
  5. init() 初始化上面的几个元素
  6. animate()  动画,场景变换 

下面开始看看init()到底都做了些什么

function init() {
                 //初始化画笔
	             renderer = new THREE.WebGLRenderer();

                //设置设备像素比。通常用于避免HiDPI设备上绘图模糊
                //不懂的PixelRatio的同学可以看下这篇博客
                //https://www.cnblogs.com/timejs/p/5409533.html
				renderer.setPixelRatio( window.devicePixelRatio );
                
                //将输出canvas的大小调整为(width, height)并考虑设备像素比
				renderer.setSize( window.innerWidth, window.innerHeight );
				document.body.appendChild( renderer.domElement );


                //初始化场景
				scene = new THREE.Scene();
				scene.background = new THREE.Color( 0x222222 );

                //初始化摄像机
				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
				camera.position.set( 0, 0, 500 );

                //控制器
				controls = new THREE.TrackballControls( camera, renderer.domElement );
				controls.minDistance = 200;
				controls.maxDistance = 500;

                //设置环境光 (此灯全局均匀地照亮场景中的所有对象)
				scene.add( new THREE.AmbientLight( 0x222222 ) );

                //点光源
				var light = new THREE.PointLight( 0xffffff );
				light.position.copy( camera.position );
				scene.add( light );

				//使用Catmull-Rom算法从一系列点创建平滑的三维样条曲线 。
				var closedSpline = new THREE.CatmullRomCurve3( [
					new THREE.Vector3( - 60, - 100, 60 ),
					new THREE.Vector3( - 60, 20, 60 ),
					new THREE.Vector3( - 60, 120, 60 ),
					new THREE.Vector3( 60, 20, - 60 ),
					new THREE.Vector3( 60, - 100, - 60 )
				] );

                //曲线的类型
				closedSpline.curveType = 'catmullrom';
                //是否闭合
				closedSpline.closed = true;

				var extrudeSettings = {
					steps: 100,
					bevelEnabled: false,//允许斜面
					extrudePath: closedSpline //THREE.CurvePath。应该挤出形状的3D样条路径
				};


				var pts = [], count = 3;
                
				for ( var i = 0; i < count; i ++ ) {

					var l = 20;

					var a = 2 * i / count * Math.PI;

					pts.push( new THREE.Vector2( Math.cos( a ) * l, Math.sin( a ) * l ) );

				}
                
                //三边形
				var shape = new THREE.Shape( pts );
                
                //从路径形状创建拉伸的图形
				var geometry = new THREE.ExtrudeBufferGeometry( shape, extrudeSettings );
                
                //非光泽表面 
				var material = new THREE.MeshLambertMaterial( { color: 0xb00000, wireframe: false } );

				var mesh = new THREE.Mesh( geometry, material );

				scene.add( mesh );


				var randomPoints = [];

				for ( var i = 0; i < 10; i ++ ) {

					randomPoints.push( new THREE.Vector3( ( i - 4.5 ) * 50, THREE.Math.randFloat( - 50, 50 ), THREE.Math.randFloat( - 50, 50 ) ) );

				}

				var randomSpline = new THREE.CatmullRomCurve3( randomPoints );

				//

				var extrudeSettings = {
					steps: 200,
					bevelEnabled: false,
					extrudePath: randomSpline
				};


				var pts = [], numPts = 5;
                //画五角星,也就是两个不同半径的圆
				for ( var i = 0; i < numPts * 2; i ++ ) {

					var l = i % 2 == 1 ? 10 : 20;

					var a = i / numPts * Math.PI;

					pts.push( new THREE.Vector2( Math.cos( a ) * l, Math.sin( a ) * l ) );

				}

				var shape = new THREE.Shape( pts );

				var geometry = new THREE.ExtrudeBufferGeometry( shape, extrudeSettings );

				var material2 = new THREE.MeshLambertMaterial( { color: 0xff8000, wireframe: false } );

				var mesh = new THREE.Mesh( geometry, material2 );

				scene.add( mesh );


				var materials = [ material, material2 ];

				var extrudeSettings = {
					depth: 20,
					steps: 1,
					bevelEnabled: true,
					bevelThickness: 2,
					bevelSize: 4,
					bevelSegments: 1
				};

				var geometry = new THREE.ExtrudeBufferGeometry( shape, extrudeSettings );

				var mesh = new THREE.Mesh( geometry, materials );

				mesh.position.set( 50, 100, 50 );

				scene.ad			d( mesh );

}

 animate() 方法

function animate() {

	requestAnimationFrame( animate );

    //控制器变化更新视图
	controls.update();
	renderer.render( scene, camera );

}

 

你可能感兴趣的:(three.js 小例子分析)