我试图在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) Closer,但现在还不正确。现在它是一个有圆锥体的金字塔。

发布于 2019-12-17 12:42:41
假设你的电流长度是外径,你必须引入一个厚度可变,并与当前的厚度相比,所以有一个内径是自由的。
(currentLength - thickness) < distanceToMiddle && distanceToMiddle < currentLength 发布于 2019-12-17 18:08:01
如果你想创造一个“空的”圆锥体。试着先用英语描述那个形状。
在0.h的高度上创建一个圆,每一步从0.r到0..r都随半径的增加而增大
所以现在你需要找到一个圆的方程(应该带你回到HS trig):sin(theta)^2 + cos(theta)^2 = radius

现在,您希望在增加高度的同时创建这个圆圈。
这只会产生你想要的空圆锥体。
下面是一个快速实现(根据需要进行调整):
public List<GameObject> instantiatedObjects = new List<GameObject>();
public GameObject rootParent;
public int numOfRows = 10;
public int incrementPerRow = 4;
public float radiusStep = 0.5f;
public float height = 5;
[Button]
public void CreateVoxelCone()
{
// first one. root object.
rootParent = GameObject.CreatePrimitive(PrimitiveType.Cube);
rootParent.name = "Pivot";
rootParent.transform.position = Vector3.zero;
var itemsForThisRow = incrementPerRow;
var heightStep = height / numOfRows;
// for each row...
for (int i = 0; i < numOfRows; i++)
{
// create items in a circle
var radianStep = Mathf.PI * 2 / itemsForThisRow;
for (float j = 0; j < Mathf.PI*2; j=j+radianStep)
{
var newPosition = new Vector3(Mathf.Cos(j) * radiusStep * i, heightStep * i, Mathf.Sin(j) * radiusStep*i);
var point = GameObject.CreatePrimitive(PrimitiveType.Cube);
point.name = ($"Row: {i}, {j}");
point.transform.SetParent(rootParent.transform);
point.transform.localPosition = newPosition;
instantiatedObjects.Add(point);
}
itemsForThisRow += incrementPerRow;
}
}
[Button]
public void CleanUp()
{
DestroyImmediate(rootParent);
}


https://stackoverflow.com/questions/59374060
复制相似问题