三个JS Gridhelper Polar可能吗?

时间:2016-02-14 22:40:47

标签: javascript three.js grid polar-coordinates

我正在使用Three JS,我可以使用GridHelper为场景添加网格。是否有类似的功能产生极性网格?

这是生成2D笛卡尔平面几何的方式(source):

var geometry = new THREE.Geometry();
var material = new THREE.LineBasicMaterial({ vertexColors: THREE.VertexColors });

this.color1 = new THREE.Color(0x444444);
this.color2 = new THREE.Color(0x888888);

for (var i = -size; i <= size; i += step) {

    geometry.vertices.push(
        new THREE.Vector3(-size, 0, i), new THREE.Vector3(size, 0, i),
        new THREE.Vector3(i, 0, -size), new THREE.Vector3(i, 0, size)
    );

    var color = i === 0 ? this.color1 : this.color2;

    geometry.colors.push(color, color, color, color);

}

THREE.LineSegments.call(this, geometry, material);

我尝试重写如下:

var segments = 64;
var material = new THREE.LineBasicMaterial({ color: 0x1976D2 });
//Somehow below didn't work...
//var material = new THREE.LineBasicMaterial({ vertexColors: THREE.VertexColors });

var geometry = new THREE.Geometry();

this.color1 = new THREE.Color(0x444444);
this.color2 = new THREE.Color(0x888888);

for (var i = -size; i <= size; i += step) {
    var circle = new THREE.CircleGeometry(i, segments);

    // Remove center vertex
    circle.vertices.shift();

    geometry.merge(circle);

    var color = i === 0 ? this.color1 : this.color2;

    geometry.colors.push(color, color, color, color);

}

THREE.LineSegments.call(this, geometry, material);

但不知怎的,这些线条破灭......

enter image description here

2 个答案:

答案 0 :(得分:2)

尝试这样的事情:

THREE.PolarHelper = function(radius, rStep, aStep, color) {

  var helper = new THREE.Mesh();

  function line(r, a, color) {
    var material = new THREE.LineBasicMaterial({ color: color });
    var geometry = new THREE.Geometry();
    geometry.vertices.push(
      new THREE.Vector3( Math.cos(a)*r, Math.sin(a)*r, 0 ),
      new THREE.Vector3( -Math.cos(a)*r, -Math.sin(a)*r, 0 )
    );
    return new THREE.Line( geometry, material );
  }

  function circle(r, color) {
    var curve = new THREE.EllipseCurve( 0,  0,  r, r, 0,  2 * Math.PI, false,   0 );
    var path = new THREE.Path( curve.getPoints( 72 ) );
    var geometry = path.createPointsGeometry( 72 );
    var material = new THREE.LineBasicMaterial( { color : color } );
    return new THREE.Line( geometry, material );
  }

  var d = radius/rStep;
  for (var r=1; r<=rStep; r++) {
    helper.add(circle(r*d, color));
  }

  d = Math.PI/aStep;
  for (var a=0; a<aStep; a++) {
    helper.add( line(radius, a*d, color) );
  }

  return helper;
}

[https://jsfiddle.net/tzsd5cre/]

答案 1 :(得分:1)

它是虚线的,因为您使用THREE.LineSegments。此类将每两个后续点连接为一行。因此,如果你想要一条连续的线,你需要复制中间的点。

在这里解释一个例子:

v1 = new THREE.Vector3( 0, 0, 0 );
v2 = new THREE.Vector3( 10, 0, 0 );
v3 = new THREE.Vector3( 20, 0, 0 );
v4 = new THREE.Vector3( 30, 0, 0 );

现在,如果使用这些点制作线几何图形:

geometry = new THREE.Geometry();
geometry.vertices.push( v1, v2, v3, v4 );

line = new THREE.LineSegments( geometry );

看起来像这样:

.__.  .__.
1  2  3  4

如果你这样做:

geometry = new THREE.Geometry();
geometry.vertices.push( v1, v2, v2, v3, v3, v4 );

line = new THREE.LineSegments( geometry );

它看起来像这样:

.__.__.__.
1  2  3  4

THREE.Line的行为方式不同,而是用连续的线连接所有顶点。所以这可能更符合您的预期:

geometry = new THREE.Geometry();
geometry.vertices.push( v1, v2, v3, v4 );

line = new THREE.Line( geometry );

将绘制连接顶点数组中点的连续线:

.__.__.__.
1  2  3  4