我已經建立了這個snake game其中蛇用的是一堆立方體網格和THREE.BoxGeometry
動畫周圍:three.js所:動畫箱狀的幾何
我寧願每個蛇包括只有一個網格和一個幾何圖形,以便我可以輕鬆添加紋理,圓角邊緣等。
我試圖帶一組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
,但收效甚微。變形發生了,但它也看起來很難看。也許我需要手動動畫幾何的頂點?
總之,我的問題是:
- 我如何可以創建類似於拼湊幾箱的幾何形狀最小的頂點幾何?
- 當底層頂點數可以改變時,動畫幾何的正確方法是什麼?
我想你需要每次更新時重計算鏈中的三角 - 除非你需要一個平滑的動畫? –
理想情況下,一個流暢的動畫。我希望獲得與鏈接演示中相同的結果,但使用單個幾何體。 –
只需使用未優化的幾何圖形就可以簡化該問題(即每個框具有其自己的幾何圖形) –