码迷,mamicode.com
首页 > Web开发 > 详细

three.js 源码注释(七十七)extras/geometries/LatheGeometry.js

时间:2015-01-31 12:56:13      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:webgl   三维   数据可视化   three.js   web3d   

商域无疆 (http://blog.csdn.net/omni360/)

本文遵循“署名-非商业用途-保持一致”创作公用协议

转载请保留此句:商域无疆 -  本博客专注于 敏捷开发及移动和物联设备研究:数据可视化、GOLANG、Html5、WEBGL、THREE.JS否则,出自本博客的文章拒绝转载或再转载,谢谢合作。


俺也是刚开始学,好多地儿肯定不对还请见谅.

以下代码是THREE.JS 源码文件中extras/geometries/LatheGeometry.js文件的注释.

更多更新在 : https://github.com/omni360/three.js.sourcecode


/**
 * @author astrodud / http://astrodud.isgreat.org/
 * @author zz85 / https://github.com/zz85
 * @author bhouston / http://exocortex.com
 */
/*
///LatheGeometry类通过截面顶点数组(points)创建旋转几何体.
///
///	用法: 
///		var points = [];
///		for ( var i = 0; i < 10; i ++ ) {
///			points.push( new THREE.Vector3( Math.sin( i * 0.2 ) * 15 + 50, 0, ( i - 5 ) * 2 ) );
///		}
///		var geometry = new THREE.LatheGeometry( points );
///		var material = new THREE.MeshBasicMaterial( { color: 0xffff00 } );
///		var lathe = new THREE.Mesh( geometry, material );
///		scene.add( lathe );
*/
///<summary>LatheGeometry</summary>
///<param name ="points" type="float">旋转体截面顶点数组</param>
///<param name ="segments" type="int">旋转体旋转圆周的细分线段数</param>
///<param name ="phiStart" type="float">旋转体的起始点</param>
///<param name ="phiLength" type="float">旋转体的弧长</param>
// points - to create a closed torus, one must use a set of points //创建一个封闭的环面,
//    like so: [ a, b, c, d, a ], see first is the same as last.	//例如[a,b,c,d,a],第一个顶点必须和最后一个顶点相同.
// segments - the number of circumference segments to create //旋转圆周的细分线段数
// phiStart - the starting radian 	//旋转截面的起始点,默认初始化为0.
// phiLength - the radian (0 to 2*PI) range of the lathed section
//    2*pi is a closed lathe, less than 2PI is a portion. 	//旋转的弧长默认初始化为2*PI.
THREE.LatheGeometry = function ( points, segments, phiStart, phiLength ) {

	THREE.Geometry.call( this );

	segments = segments || 12;
	phiStart = phiStart || 0;
	phiLength = phiLength || 2 * Math.PI;

	var inversePointLength = 1.0 / ( points.length - 1 );
	var inverseSegments = 1.0 / segments;
	//旋转体截面顶点数组通过旋转,获得所有的顶点数组.
	for ( var i = 0, il = segments; i <= il; i ++ ) {

		var phi = phiStart + i * inverseSegments * phiLength;

		var c = Math.cos( phi ),
			s = Math.sin( phi );

		for ( var j = 0, jl = points.length; j < jl; j ++ ) {

			var pt = points[ j ];

			var vertex = new THREE.Vector3();

			vertex.x = c * pt.x - s * pt.y;
			vertex.y = s * pt.x + c * pt.y;
			vertex.z = pt.z;

			this.vertices.push( vertex );

		}

	}

	var np = points.length;
	//将所有的顶点计算生成三角面,并计算出三角面贴图的uv
	for ( var i = 0, il = segments; i < il; i ++ ) {

		for ( var j = 0, jl = points.length - 1; j < jl; j ++ ) {

			var base = j + np * i;
			var a = base;
			var b = base + np;
			var c = base + 1 + np;
			var d = base + 1;

			var u0 = i * inverseSegments;
			var v0 = j * inversePointLength;
			var u1 = u0 + inverseSegments;
			var v1 = v0 + inversePointLength;

			this.faces.push( new THREE.Face3( a, b, d ) );

			this.faceVertexUvs[ 0 ].push( [

				new THREE.Vector2( u0, v0 ),
				new THREE.Vector2( u1, v0 ),
				new THREE.Vector2( u0, v1 )

			] );

			this.faces.push( new THREE.Face3( b, c, d ) );

			this.faceVertexUvs[ 0 ].push( [

				new THREE.Vector2( u1, v0 ),
				new THREE.Vector2( u1, v1 ),
				new THREE.Vector2( u0, v1 )

			] );


		}

	}

	this.mergeVertices();	//合并顶点,删除多余的顶点 
	this.computeFaceNormals();	//计算三角面的法线
	this.computeVertexNormals();	//计算顶点的法线

};
/*************************************************
****下面是LatheGeometry对象的方法属性定义,继承自Geometry对象.
**************************************************/
THREE.LatheGeometry.prototype = Object.create( THREE.Geometry.prototype );


商域无疆 (http://blog.csdn.net/omni360/)

本文遵循“署名-非商业用途-保持一致”创作公用协议

转载请保留此句:商域无疆 -  本博客专注于 敏捷开发及移动和物联设备研究:数据可视化、GOLANG、Html5、WEBGL、THREE.JS否则,出自本博客的文章拒绝转载或再转载,谢谢合作。


以下代码是THREE.JS 源码文件中extras/geometries/LatheGeometry.js文件的注释.

更多更新在 : https://github.com/omni360/three.js.sourcecode

three.js 源码注释(七十七)extras/geometries/LatheGeometry.js

标签:webgl   三维   数据可视化   three.js   web3d   

原文地址:http://blog.csdn.net/omni360/article/details/43320785

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!