【发布时间】:2016-11-05 14:19:16
【问题描述】:
我已经构建了这个snake game,其中使用一堆立方体网格和THREE.BoxGeometry:
我希望每条蛇只包含一个网格和一个几何体,以便我可以轻松添加纹理、圆角边缘等。
我正在尝试获取一组 3d 点并将它们转换为单个几何体,类似于演示中的盒状蛇(就像几个 THREE.BoxGeometry 连接在一起)。
我尝试使用THREE.CurvePath 和THREE.ExtrudeGeometry 来实现这一点:
_makeSnakeGeometry(positions) {
const vectors = positions.map(p => new THREE.Vector3(...p));
const curvePath = new THREE.CurvePath();
const first = vectors[1];
const last = vectors[vectors.length - 1];
let curveStart = vectors[0];
let previous = curveStart;
let previousDirection;
// Walk through the positions. If there is a change in direction, add
// a new curve to the curve path.
for (let vector of vectors.slice(1)) {
let direction = previous.clone().sub(vector);
if (vector.equals(first)) {
curveStart.sub(direction.clone().divideScalar(2));
}
if (vector.equals(last)) {
previous.sub(previousDirection.clone().divideScalar(2));
}
if ((previousDirection && !previousDirection.equals(direction)) || vector.equals(last)) {
curvePath.add(new THREE.LineCurve3(curveStart, previous));
curveStart = previous;
}
previous = vector;
previousDirection = direction;
}
const p = Const.TILE_SIZE / 2;
const points = [[-p, -p], [-p, p], [p, p], [p, -p]];
const shape = new THREE.Shape(points.map(p => new THREE.Vector2(...p)));
const geometry = new THREE.ExtrudeGeometry(shape, {
steps: 20,
extrudePath: curvePath,
amount: 20
});
return geometry;
}
不幸的是,它看起来很丑陋,我们最终得到了路径方向改变的圆角:
如果我增加steps 选项,生成的网格看起来不那么难看,但由于平滑的弯曲边缘,几何图形具有大量顶点。我担心顶点数量过多,因为我可能必须在每个动画帧上重新创建这个几何体。
为了为几何图形设置动画,我尝试使用geometry.morphTargets,但收效甚微。变形发生了,但它看起来也很丑陋。也许我需要手动为几何的顶点设置动画?
总而言之,我的问题是:
- 如何创建一个具有最少顶点的几何图形,类似于拼凑在一起的多个盒子几何图形?
- 当底层顶点数可能发生变化时,为几何图形设置动画的正确方法是什么?
【问题讨论】:
-
我认为您需要在每次更新期间重新计算链的三角剖分 - 除非您需要流畅的动画?
-
理想的平滑动画。我希望获得与链接演示中相同的结果,但使用单个几何体。
-
这个问题可以简化很多,只需使用未优化的几何体(即每个盒子都有自己的几何体)
标签: javascript animation 3d three.js