在three.js中创建锯齿形结构?

时间:2018-09-20 09:51:35

标签: javascript three.js

我正在为自己正在开发的3D工具开发少量结构,而我却想要这样的https://jsfiddle.net/cdu96z3c/21/之字形结构。到现在为止,我已经通过重复多次产生“ V”的相同函数来实现该目标的方法实现了这一目标,我希望使用一个单一的几何体和网格来实现相同的目标。

function modelShape(points){
   this.shape = new THREE.Shape();
   this.shape.moveTo(points[0][0], points[0][1]);
   for(var i=1; i<points.length; i++){
       this.shape.lineTo(points[i][0], points[i][1]);
   }
   this.shape.lineTo(points[0][0], points[0][1]);
}
function structure(i){
   points = [
      [-1.5,0],
      [-1.5,.5],
      [0,3.5],
      [1.5,.5],
      [1.5,0],
      [0,3]
   ];
   modelShape.call(this, points);
   var extrudeSettings = { amount: .25, bevelEnabled: true, bevelSegments: 0, steps: 1, bevelSize: 0, bevelThickness: 0 };
   var geometry = new THREE.ExtrudeGeometry( this.shape, extrudeSettings );
   geometry.dynamic = true;
   geometry.colorsNeedUpdate = true;
   var material = new THREE.MeshStandardMaterial({ 
           color: 0xafafaf,
           metalness: 0.9
   });
   this.mesh = new THREE.Mesh( geometry, material);
   this.mesh.position.set(i*3,0,0)
}
for(i = 0;i < 20; i++){
    var struc = new structure(i);
    scene.add(struc.mesh);
}

1 个答案:

答案 0 :(得分:1)

这是一个小函数,它根据一些参数生成点以形成锯齿形:

// amount - number of zigzag segments
// width - width of one segment
// height - height of one segment
// thickness - thickness/width of the line
function generateZigZagPoints(amount, width, height, thickness) {

  let points = [];

  for (let i = 0; i < amount; i++) {
    points.push([ i * width, 0 ]);
    points.push([ i * width + width / 2, height ]);
  }

  points.push([ amount * width, 0 ]);
  points.push([ amount * width, -thickness ]);

  for (let i = amount; i > -1; i--) {
    points.push([ i * width + width / 2, height - thickness ]);
    points.push([ i * width, -thickness ]);
  }

  return points;

}