我正在尝试在 C# 中创建一个体素风格的圆锥形状。我已经使用立方体构建了圆锥体,但无法弄清楚如何仅构建外层(使其成为空心)而不是构建如图所示的实心圆锥体。
到目前为止的代码(从别人的脚本编辑)
// Create a cone made of cubes. Can be called at runtime
public void MakeVoxelCone()
{
for (int currentLength = 0; currentLength < maxLength; currentLength++)
MakeNewLayer(currentLength);
}
// Make a new layer of cubes on the cone
private void MakeNewLayer(int currentLength)
{
center = new Vector3(0, currentLength, 0);
for (int x = -currentLength; x < currentLength; x++)
{
for (int z = -currentLength; z < currentLength; z++)
{
// Set position to spawn cube
Vector3 pos = new Vector3(x, currentLength, z);
// The distance to the center of the cone at currentLength point
float distanceToMiddle = Vector3.Distance(pos, center);
// Create another layer of the hollow cone
if (distanceToMiddle < currentLength)
{
// Add all cubes to a List array
Cubes.Add(MakeCube(pos));
}
}
}
}
// Create the cube and set its properties
private GameObject MakeCube(Vector3 position)
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.GetComponent<Renderer>().material.color = Color.blue;
if (!AddCollider)
Destroy(cube.GetComponent<BoxCollider>());
cube.transform.position = position;
cube.transform.parent = transform;
cube.name = "cube [" + position.y + "-" + position.x + "," + position.z + "]";
return cube;
}
我认为它可能很简单,但无法弄清楚。
也许与
if (distanceToMiddle < currentLength)
部分有关,但是将 <
换成 ==
会破坏整个形状。@Jax297
>= (currentLength-1)
更接近,但目前还不正确。现在它是一个锥体切出的金字塔。最佳答案
假设您的 currentlength 是外径,您必须引入一个可变的厚度并与 currentleght 进行比较 - 厚度,因此有一个内径可以保持自由
(currentLength - thickness) < distanceToMiddle && distanceToMiddle < currentLength
关于c# - 制作空心体素锥,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59374060/