【发布时间】:2017-05-26 18:53:33
【问题描述】:
我正在通过tutorial here 工作。为了方便,我有the package shared through my google drive。
主题GameObject 是HexGridController 和三个禁用的对象:center_fill、Backdrop、DiagnosticDisplay。
...以及 HexGridController 脚本:
public class HexGridController : MonoBehaviour, IGridInterface {
public float cellWidth = 50F;
public HexOrientation cellFacing = HexOrientation.Acute;
public int cellCols = 20;
public int cellRows = 14;
public float degOffset = 0.0F;
// ... snipped IGridInterface implementations
// Use this for initialization
private void Start () {
print("Hexagonal Grid Controller started");
print("hex vertices:");
int idx = 0;
foreach(var vert in vertices){
print(string.Format("\t[{0}]: {1}", idx, vert.ToString()));
++idx;
}
}
#region HiddenInInspector: vertices, uv, triangles
[HideInInspector]
public static Vector3[] vertices = new Vector3[]
{
new Vector3(0f, Hexagon.floor, 1f), // north
new Vector3(1f, Hexagon.floor, .5f), // northeast
new Vector3(1f, Hexagon.floor, -.5f), // southeast
new Vector3(0f, Hexagon.floor, -1f), // south
new Vector3(-1f , Hexagon.floor, -.5f), // southwest
new Vector3(-1f, Hexagon.floor, .5f), // northwest
};
[HideInInspector]
public static Vector2[] uv = new Vector2[]
{
new Vector2(0.5F,1), // north
new Vector2(1,0.75F), // northeast
new Vector2(1,0.25F), // southeast
new Vector2(0.5F,0), // south
new Vector2(0,0.25F), // southwest
new Vector2(0,0.75F), // northwest
};
[HideInInspector]
public static int[] triangles = new int[]
{
1,5,0,
1,4,5,
1,2,4,
2,3,4
};
#endregion
}
最后,HexGrid 是一个 GameObject,附有以下脚本:
public class Hexagon : MonoBehaviour {
private MeshRenderer meshRenderer;
public const float floor = 0;
public HexGridController grid;
public Texture texture;
// Use this for initialization
private void Start () {
print("Hexagon started");
HexGridController.vertices[0] = new Vector3((grid.cellWidth * Mathf.Cos((float)(2*Mathf.PI*(1+0.5)/6))), Hexagon.floor, (grid.cellWidth * Mathf.Sin((float)(2*Mathf.PI*(1+0.5)/6))));
HexGridController.vertices[1] = new Vector3((grid.cellWidth * Mathf.Cos((float)(2*Mathf.PI*(0+0.5)/6))), Hexagon.floor, (grid.cellWidth * Mathf.Sin((float)(2*Mathf.PI*(0+0.5)/6))));
HexGridController.vertices[2] = new Vector3((grid.cellWidth * Mathf.Cos((float)(2*Mathf.PI*(5+0.5)/6))), Hexagon.floor, (grid.cellWidth * Mathf.Sin((float)(2*Mathf.PI*(5+0.5)/6))));
HexGridController.vertices[3] = new Vector3((grid.cellWidth * Mathf.Cos((float)(2*Mathf.PI*(4+0.5)/6))), Hexagon.floor, (grid.cellWidth * Mathf.Sin((float)(2*Mathf.PI*(4+0.5)/6))));
HexGridController.vertices[4] = new Vector3((grid.cellWidth * Mathf.Cos((float)(2*Mathf.PI*(3+0.5)/6))), Hexagon.floor, (grid.cellWidth * Mathf.Sin((float)(2*Mathf.PI*(3+0.5)/6))));
HexGridController.vertices[5] = new Vector3((grid.cellWidth * Mathf.Cos((float)(2*Mathf.PI*(2+0.5)/6))), Hexagon.floor, (grid.cellWidth * Mathf.Sin((float)(2*Mathf.PI*(2+0.5)/6))));
SetUpMesh();
}
private void SetUpMesh() {
MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
meshRenderer = gameObject.GetComponent<MeshRenderer>();
Mesh mesh = new Mesh();
//vertices
mesh.vertices = HexGridController.vertices;
//triangles
mesh.triangles = HexGridController.triangles;
//UV vectors
mesh.uv = HexGridController.uv;
//recalc for lighting
mesh.RecalculateNormals();
//game object's mesh filter
meshFilter.mesh = mesh;
//set to null when not testing
meshRenderer.material.mainTexture = texture;
}
private void Update ()
{
}
private void OnDestory()
{
Object.Destroy(meshRenderer.material);
}
}
注意:
对于它的价值,Hex Facing 是 North 的六边形方向 - 平坦或锐角(或尖锐)。我打算将最北点设为0并顺时针旋转:
0 - 北
1 - 东北
2 - 东南部
3 - 南
4 - 西南
5 - 西北
【问题讨论】:
-
这可能是一个愚蠢的问题,但你确定你的三角形设置正确(意味着它们相对于你的相机没有向后定向)和第二个 - 你的三角形在相机视锥?
-
@PavelPájaHalbich:这些不是愚蠢的问题。但由于我是 Unity 新手,所以我不知道如何回答这些问题。我只是在关注问题中链接的 tut。包也可以下载,你可以看到它是如何设置的,然后你可以告诉我你的问题的答案......我不知道。 ;)
-
嗯,我目前正在使用虚幻引擎,我没有安装 Unity,但原理是一样的。既然你被卡住了,我建议你遵循:用相机和一个游戏对象建立一个简单的关卡。在该游戏对象的脚本中做一件简单的事情——尝试画一个三角形。从一些统一论坛,我发现三角形必须以顺时针方式定义(forum.unity3d.com/threads/mesh-triangles-explanation.78564)。这是一个快速迭代的简单任务。然后,当你成功后,继续学习该教程。
-
但是,作为(专业)提示,我想提出另一种方法 - 在一些 3D 工具(3DS Max、Cinema 4D、Blender、Maya、...)中生成您的六边形,这样您就可以只有 4 个面(三角形)而不是 6 个(从而为显卡节省资源)。然后您可以从中创建 Prefab 并附加其他脚本。最后(如果可能的话)使用 GPU 实例化。这将节省大量 GPU 时间,尤其是在使用大量六边形图块时。 docs.unity3d.com/Manual/GPUInstancing.html
-
我只有 4 个三角形。