gpt4 book ai didi

c# - 从顶点位置以编程方式创建 3D 棱镜

转载 作者:太空宇宙 更新时间:2023-11-03 18:28:10 25 4
gpt4 key购买 nike

我正在使用 Unity 4.6 开发一个项目。我想知道是否有一种简单的方法来以编程方式设置顶点位置。目前我的代码看起来像这样 (C#):

public void createPoints(float x, float y, float z) {
point [0] = new Vector3 (x, y, z);
point [1] = new Vector3 (x, y, -z);
point [2] = new Vector3 (x, -y, z);
point [3] = new Vector3 (x, -y, -z);
point [4] = new Vector3 (-x, y, z);
point [5] = new Vector3 (-x, y, -z);
point [6] = new Vector3 (-x, -y, z);
point [7] = new Vector3 (-x, -y, -z);
}

其中 x、y 和 z 是预定值,点是 Vector3[](数组)。棱镜的中心点为 0,0,0(Unity 中的 Vector3(0,0,0))。

例如,使用以下值:

  • x = 1
  • y = 2
  • z = 3

生成的点应该如下(顺序无关紧要):

  1. (1,2,3)
  2. (1,2,-3)
  3. (1,-2,3)
  4. (1,-2,-3)
  5. (-1,2,3)
  6. (-1,2,-3)
  7. (-1,-2,3)
  8. (-1,-2,-3)

我的问题:有没有一种方法可以生成这些点而无需对其中的值进行实际硬编码?

我当前所需解决方案的代码是:

public void createPoints(float originalX, float originalY, float originalZ) {
for (int i = 0; i < 8; i++) { //8 vertices exist of a rectangular prism
point [i] = new Vector3 (x, y, z);
}
}

如何更改此代码以生成这些正负点?我的猜测是 x、y 和 z 的正负值之间的某种交替,使用 i 的模数或 i 除以其他整数的舍入,但是我无法全神贯注。 C# 或 UnityScript 都可以。

换句话说,在 for 循环的每次迭代中,x = -x,对于每个 something y = -y,对于每个 something z = -z.

非常感谢对此的任何帮助。

最佳答案

我会为此使用 linq

var alt = new[] { 1, -1 };
var result = ( from i in alt
from j in alt
from k in alt
select new Vector3(i * 1, j * 2, k * 3)
).ToList();

关于c# - 从顶点位置以编程方式创建 3D 棱镜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29054420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com