如何在不使用SphereGeometry的情况下绘制球体?

时间:2015-08-17 07:13:55

标签: javascript three.js

我正在尝试不使用SphereGeometry绘制球体。我试图像纬度和经度画出球体。这是我的代码:

for (var phi = -Math.PI / 2; phi < Math.PI / 2; phi += Math.PI / 15) {
                    var longVertices = new THREE.Geometry()
                    for (var theta = 0; theta <= 2 * Math.PI; theta += Math.PI/2 ) {

                        longitudes = this.point[this.numberOfVertices] = new THREE.Vector3();

                        longitudes.x = origin.x + this.radius * Math.cos(theta) * Math.cos(phi);
                        longitudes.y = origin.z + Math.sin(theta) * this.radius;
                        longitudes.z = origin.y + this.radius * Math.cos(theta) * Math.sin(phi);

                        this.numberOfVertices++;
                        longVertices.vertices.push(longitudes);
                    }
                    longVertices.vertices.push(longVertices.vertices[0]);
                    longVerticesArr.push(longVertices);
                }

此代码帮助我绘制经度。 enter image description here

        for (var phi = -Math.PI / 2; phi < Math.PI / 2; phi += Math.PI / 15) {

            var delta = Math.cos(phi) * this.radius;
            var fixedY = Math.sin(phi) * this.radius * direction;
            var latVertices = new THREE.Geometry();
            for (var theta = 0; theta < 2 * Math.PI; theta += Math.PI / 10) {
                latitudes =/* this.point[this.numberOfVertices] =*/ new THREE.Vector3();

                latitudes.z = origin.z + delta * Math.sin(theta);
                latitudes.y = fixedY;
                latitudes.x = origin.x + delta * Math.cos(theta);

                this.numberOfVertices++;

                latVertices.vertices.push(latitudes);
               }
            latVertices.vertices.push(latVertices.vertices[0]);
            latVerticesArr.push(latVertices);

        }
这有助于我画出纬度。

enter image description here

现在我面临的问题是我在交叉路口没有获得纬度和经度的分数。如何准确地在交叉点获得这些点?

1 个答案:

答案 0 :(得分:3)

简单的嵌套循环: [http://jsfiddle.net/cdjtdkwa/]

var R = 18; // radius
var LON = 32; var LAT = 16; // approximation
var PILAT = Math.PI/LAT;
var PILON = 2*Math.PI/LON;    
var cos1,cos2,sin1,sin2,t1,t2;    
var y1,y2,r1,r2,t1,t2;    
var geometry = new THREE.Geometry();

for (var i=0; i<LAT; i++) { // walk latitudes segments
    t1 = Math.PI - i*PILAT;
    t2 = Math.PI - (i+1)*PILAT;

    y1 = Math.cos(t1); // 1 latitudes radius y-position;
    y2 = Math.cos(t2); // 2 latitudes radius y-position;

    r1 = Math.abs( Math.sin(t1) ); // 1 latitudes radius;
    r2 = Math.abs( Math.sin(t2) ); // 2 latitudes radius;          

    for (var j=0; j<LON; j++) { // walk longitudes segments
        t1 = j*PILON;
        t2 = (j+1)*PILON;

        cos1 = Math.cos(t1);
        cos2 = Math.cos(t2);
        sin1 = Math.sin(t1);
        sin2 = Math.sin(t2);

        geometry.vertices.push(
            new THREE.Vector3( r1*cos1, y1, r1*sin1 ),
            new THREE.Vector3( r2*cos1, y2, r2*sin1 ),
            new THREE.Vector3( r2*cos2, y2, r2*sin2 )          
        );

    }
}