threejs 形状几何体_07-THREE.JS 各种形状的几何图形

这篇博客探讨了如何使用THREE.js库通过参数化几何体来创建不同的3D形状,包括克莱因瓶、平面、莫比乌斯带(2D和3D)以及球体和飞机等几何图形。内容详细介绍了每个形状的数学实现,适用于WebGL和3D图形编程爱好者。
摘要由CSDN通过智能技术生成

/** @author zz85

*

* Experimenting of primitive geometry creation using Surface Parametric equations

**/THREE.ParametricGeometries={

klein:function(v, u) {

u*=Math.PI;

v*= 2 *Math.PI;

u= u * 2;varx, y, z;if (u

x= 3 * Math.cos(u) * (1 + Math.sin(u)) + (2 * (1 - Math.cos(u) / 2)) * Math.cos(u) *Math.cos(v);

z= -8 * Math.sin(u) - 2 * (1 - Math.cos(u) / 2) * Math.sin(u) *Math.cos(v);

}else{

x= 3 * Math.cos(u) * (1 + Math.sin(u)) + (2 * (1 - Math.cos(u) / 2)) * Math.cos(v +Math.PI);

z= -8 *Math.sin(u);

}

y= -2 * (1 - Math.cos(u) / 2) *Math.sin(v);return newTHREE.Vector3(x, y, z);

},

plane:function(width, height) {return function(u, v) {var x = u *width;var y = 0;var z = v *height;return newTHREE.Vector3(x, y, z);

};

},

mobius:function(u, t) {//flat mobius strip

//http://www.wolframalpha.com/input/?i=M%C3%B6bius+strip+parametric+equations&lk=1&a=ClashPrefs_*Surface.MoebiusStrip.SurfaceProperty.ParametricEquations-

u = u - 0.5;var v = 2 * Math.PI *t;varx, y, z;var a = 2;

x= Math.cos(v) * (a + u * Math.cos(v/2));

y = Math.sin(v) * (a + u * Math.cos(v/2));

z = u * Math.sin(v/2);

return newTHREE.Vector3(x, y, z);

},

mobius3d:function(u, t) {//volumetric mobius strip

u *=Math.PI;

t*= 2 *Math.PI;

u= u * 2;var phi = u / 2;var major = 2.25, a = 0.125, b = 0.65;varx, y, z;

x= a * Math.cos(t) * Math.cos(phi) - b * Math.sin(t) *Math.sin(phi);

z= a * Math.cos(t) * Math.sin(phi) + b * Math.sin(t) *Math.cos(phi);

y= (major + x) *Math.sin(u);

x= (major + x) *Math.cos(u);return newTHREE.Vector3(x, y, z);

}

};/*********************************************

*

* Parametric Replacement for TubeGeometry

*

*********************************************/THREE.ParametricGeometries.TubeGeometry= function(path, segments, radius, segmentsRadius, closed, debug) {this.path =path;this.segments = segments || 64;this.radius = radius || 1;this.segmentsRadius = segmentsRadius || 8;this.closed = closed || false;if (debug) this.debug = newTHREE.Object3D();var scope = this,

tangent, normal, binormal,

numpoints= this.segments + 1,

x, y, z, tx, ty, tz, u, v,

cx, cy, pos, pos2= newTHREE.Vector3(),

i, j, ip, jp, a, b, c, d, uva, uvb, uvc, uvd;var frames = newTHREE.TubeGeometry.FrenetFrames(path, segments, closed),

tangents=frames.tangents,

normals=frames.normals,

binormals=frames.binormals;//proxy internals

this.tangents =tangents;this.normals =normals;this.binormals =binormals;var ParametricTube = function(u, v) {

v*= 2 *Math.PI;

i= u * (numpoints - 1);

i=Math.floor(i);

pos=path.getPointAt(u);

tangent=tangents[i];

normal=normals[i];

binormal=binormals[i];if(scope.debug) {

scope.debug.add(new THREE.ArrowHelper(tangent, pos, radius, 0x0000ff));

scope.debug.add(new THREE.ArrowHelper(normal, pos, radius, 0xff0000));

scope.debug.add(new THREE.ArrowHelper(binormal, pos, radius, 0x00ff00));

}

cx= -scope.radius * Math.cos(v); //TODO: Hack: Negating it so it faces outside.

cy = scope.radius *Math.sin(v);

pos2.copy(pos);

pos2.x+= cx * normal.x + cy *binormal.x;

pos2.y+= cx * normal.y + cy *binormal.y;

pos2.z+= cx * normal.z + cy *binormal.z;returnpos2.clone();

};

THREE.ParametricGeometry.call(this, ParametricTube, segments, segmentsRadius);

};

THREE.ParametricGeometries.TubeGeometry.prototype=Object.create( THREE.Geometry.prototype );/*********************************************

*

* Parametric Replacement for TorusKnotGeometry

*

*********************************************/THREE.ParametricGeometries.TorusKnotGeometry= function( radius, tube, segmentsR, segmentsT, p, q, heightScale ) {var scope = this;this.radius = radius || 200;this.tube = tube || 40;this.segmentsR = segmentsR || 64;this.segmentsT = segmentsT || 8;this.p = p || 2;this.q = q || 3;this.heightScale = heightScale || 1;var TorusKnotCurve =THREE.Curve.create(function() {

},function(t) {

t*= Math.PI * 2;var r = 0.5;var tx = (1 + r * Math.cos(q * t)) * Math.cos(p *t),

ty= (1 + r * Math.cos(q * t)) * Math.sin(p *t),

tz= r * Math.sin(q *t);return new THREE.Vector3(tx, ty *heightScale, tz).multiplyScalar(radius);

}

);var segments =segmentsR;var radiusSegments =segmentsT;var extrudePath = newTorusKnotCurve();

THREE.ParametricGeometries.TubeGeometry.call(this, extrudePath, segments, tube, radiusSegments, true, false);

};

THREE.ParametricGeometries.TorusKnotGeometry.prototype=Object.create( THREE.Geometry.prototype );/*********************************************

*

* Parametric Replacement for SphereGeometry

*

*********************************************/THREE.ParametricGeometries.SphereGeometry= function(size, u, v) {functionsphere(u, v) {

u*=Math.PI;

v*= 2 *Math.PI;var x = size * Math.sin(u) *Math.cos(v);var y = size * Math.sin(u) *Math.sin(v);var z = size *Math.cos(u);return newTHREE.Vector3(x, y, z);

}

THREE.ParametricGeometry.call(this, sphere, u, v, !true);

};

THREE.ParametricGeometries.SphereGeometry.prototype=Object.create( THREE.Geometry.prototype );/*********************************************

*

* Parametric Replacement for PlaneGeometry

*

*********************************************/THREE.ParametricGeometries.PlaneGeometry= function(width, depth, segmentsWidth, segmentsDepth) {functionplane(u, v) {var x = u *width;var y = 0;var z = v *depth;return newTHREE.Vector3(x, y, z);

}

THREE.ParametricGeometry.call(this, plane, segmentsWidth, segmentsDepth);

};

THREE.ParametricGeometries.PlaneGeometry.prototype= Object.create( THREE.Geometry.prototype );

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值