【发布时间】:2021-08-24 22:58:00
【问题描述】:
我有一个 Three.js 场景,由许多建筑物组成,这些建筑物是通过堆叠 ExtrudeGeometry 网格形成的(想想 Mapbox GL JS 中的建筑物):
我正在使用 THREE.Shape 和 THREE.ExtrudeGeometry 创建这些网格(我正在使用 react-three-fiber):
function coordsToShape(coords) {
const shape = new Shape();
let [x, z] = coords[0];
shape.moveTo(x, z);
for (const [x, z] of coords.slice(1)) {
shape.lineTo(x, z);
}
return shape;
}
function Floor(props) {
const {coords, bottom, top, color} = props;
const shape = coordsToShape(coords);
const geom = new ExtrudeGeometry(shape, {depth: top - bottom, bevelEnabled: false});
return (
<mesh castShadow geometry={geom} position={[0, bottom, 0]} rotation-x={-Math.PI / 2}>
<meshPhongMaterial color={color} />
</mesh>
)
}
然后我把地板叠起来制作场景:
export default function App() {
return (
<Canvas>
{ /* lights, controls, etc. */ }
<GroundPlane />
<Floor coords={coords1} bottom={0} top={1} color="skyblue" />
<Floor coords={coords2} bottom={1} top={3} color="pink" />
<Floor coords={coords3} bottom={0} top={1} color="aqua" />
<Floor coords={coords4} bottom={1} top={3} color="orange" />
</Canvas>
)
}
完整代码/演示here。这导致地平面一个网格,每个建筑部分一个网格,所以总共五个。
我 read 认为使用 instancing 减少网格数量是个好主意。实例化与这个场景有关吗?大多数实例化示例显示identical geometries,颜色、位置和旋转都不同。但是几何形状可以改变吗?我应该使用mergeBufferGeometries 吗?但如果我这样做,我还能获得性能上的胜利吗?由于我已经有了坐标数组,我也很乐意使用它们直接构造一个大的坐标缓冲区。
【问题讨论】:
标签: three.js react-three-fiber