3

如何在three.js中渲染点的二维形状?我没有找到任何工作几何......我只需要制作位于同一平面上的点的多边形。

形状不适合,因为它只支持 2D 坐标,我需要使用 3D 点...

我正在使用以下代码在 X、Y 平面内创建形状

 var squareShape = new THREE.Shape();
 squareShape.moveTo( 0,0 );
 squareShape.lineTo( 0, 50 );
 squareShape.lineTo( 20, 80 );
 squareShape.lineTo( 50, 50 );
 squareShape.lineTo( 0, 0 );

如何在工作中制作 3D 世界?有什么解决办法吗?像:

squareShape.moveTo( 0,0,0 );
squareShape.lineTo( 0, 50, 50 );

ETC

4

3 回答 3

10

您可以从 3D 点创建多边形,如下所示:

// geometry
var geometry = new THREE.Geometry();
geometry.vertices.push( new THREE.Vector3( 0, 5, 0 ) );
geometry.vertices.push( new THREE.Vector3( 5, -5, -2 ) );
geometry.vertices.push( new THREE.Vector3( -5, -5, 2 ) );
geometry.vertices.push( new THREE.Vector3( 0, 5, 0 ) ); // close the loop

// material
var material = new THREE.LineBasicMaterial( { color: 0xffffff, linewidth: 1 } );

// line
var line = new THREE.Line( geometry, material );
scene.add( line );

三.js r.69

于 2012-09-29T22:58:26.620 回答
9

从 r51 开始,出现了一种新型几何形状,THREE.ShapeGeometry它是在一个平面上构建的二维形状。可以在此处找到其使用示例:http: //mrdoob.github.com/three.js/examples/webgl_geometry_shapes.html

您可以使用THREE.Shape来构造 2d 形状,然后调用shape.makeGeometry()将其转换为THREE.ShapeGeometry. 这是一个制作圆圈的伪代码示例:

var circle = new THREE.Shape();
var radius = 6;

for (var i = 0; i < 16; i++) {
  var pct = (i + 1) / 16;
  var theta = pct * Math.PI * 2.0;
  var x = radius * Math.cos(theta);
  var y = radius * Math.sin(theta);
  if (i == 0) {
    circle.moveTo(x, y);
  } else {
    circle.lineTo(x, y);
  }
}

var geometry = circle.makeGeometry();
var material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
var mesh = new THREE.Mesh(geometry, material);

meshhas position, rotation, and which 可以帮助您在scale三维空间中定位对象。

于 2012-10-17T01:17:42.297 回答
1

您需要使用形状的顶点(提取后)创建几何体,并使用三角剖分来制作面。

代码应如下所示:

var geometry = new THREE.Geometry();
var shapePoints = shape.extractPoints();
var faces = THREE.Shape.Utils.triangulateShape(shapePoints.shape, shapePoints.holes);
for (var i = 0; i < shapePoints.shape.length; i++) {
    geometry.vertices.push(new THREE.Vector3(shapePoints.shape[i].x, 0, shapePoints.shape[i].y));
}
for (var i = 0; i < faces.length ; i++) {
    var a = faces[i][2] , b = faces[i][1] , c = faces[i][0] ;
    var v1 = shapePoints.shape[a], v2 = shapePoints.shape[b], v3 = shapePoints.shape[c];

    geometry.faces.push( new THREE.Face3(a, b, c) );    
    geometry.faceVertexUvs[0].push(
        [ new THREE.UV(v1.x ,v1.y ), new THREE.UV(v2.x, v2.y), new THREE.UV(v3.x, v3.y)]);
}
geometry.computeCentroids();
geometry.computeFaceNormals();
于 2012-09-29T22:59:14.650 回答