为什么网格不渲染?

时间:2017-05-26 18:53:33

标签: c# unity3d mesh

我正在通过tutorial here。为方便起见,我the package shared through my google drive

我设置了一个如下所示的场景:
hexgrid main scene

主题GameObjectHexGridController,三个已停用的对象:center_fillBackdropDiagnosticDisplay

除了附加的脚本外,网格控制器是一个空的GameObject:
hex grid controller properties

...和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
     }  

最后,HexGridGameObject,附带以下脚本:

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 - North
1 - NorthEast
2 - SouthEast
3 - South
4 - SouthWest
5 - NorthWest

1 个答案:

答案 0 :(得分:0)

好的,因为我正在学习Unity,我无法解释为什么教程的API与我发现的正确的差异。我发现有几件事情是不正确的,按照@PavelPájaHalbich的建议,我完成了一个简短的练习,其中我成功地渲染了一个三角形。

现在,我修复的问题和结果......

由于Hexagon提供verticesuv的所有特定数据,因此简化了HexGridController。三角形的数据保留在HexGridController中。

#region HiddenInInspector: vertices, uv, triangles
[HideInInspector]
public static Vector3[] vertices = new Vector3[6];

[HideInInspector]
public static Vector2[] uv = new Vector2[6];

[HideInInspector]
public static int[] triangles = new int[]
{
    0,1,2,
    2,3,0,
    3,4,0,
    4,5,0,
};
#endregion

...接下来,Hexagon类在我正确订购vertices并映射uv向量时得到了一些改进:

private void Start () {
    print("Hexagon started");

    //  set up vertices
    vertices[(int)HexFacing.North] = new Vector3(0F, 1F, floor);
    vertices[(int)HexFacing.NorthEeast] = new Vector3(1F, .5F, floor);
    vertices[(int)HexFacing.SouthEast] = new Vector3(1F, -.5F, floor);
    vertices[(int)HexFacing.South] = new Vector3(0F, -1F, floor);
    vertices[(int)HexFacing.SouthWest] = new Vector3(-1F, -.5F, floor);
    vertices[(int)HexFacing.NorthWest] = new Vector3(-1F, .5F, floor);

    //  set up uv
    uv[(int)HexFacing.North] = new Vector2(0.5F, 1);
    uv[(int)HexFacing.NorthEeast] = new Vector2(1, 0.75F);
    uv[(int)HexFacing.SouthEast] = new Vector2(1, 0.25F);
    uv[(int)HexFacing.South] = new Vector2(0.5F, 0);
    uv[(int)HexFacing.SouthWest] = new Vector2(0, 0.25F);
    uv[(int)HexFacing.NorthWest] = new Vector2(0, 0.75F);

    SetUpMesh();
}

...最后在SetUpMesh()方法中进行了一些更正:

private void SetUpMesh() {
    MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
    gameObject.AddComponent<MeshRenderer>();
    meshRenderer = gameObject.GetComponent<MeshRenderer>();

    Mesh mesh = meshFilter.mesh; // this had been newing a mesh
    mesh.Clear();                // this hadn't been called

    //vertices
    mesh.vertices = vertices;
    //triangles
    mesh.triangles = HexGridController.triangles;
    //UV vectors
    mesh.uv = uv;

    //recalc for lighting
    mesh.RecalculateNormals();

    //game object's mesh filter
    meshFilter.mesh = mesh;

    //set to null when not testing
    meshRenderer.material.mainTexture = texture; 
}

现在,我选择了一种我从未见过广泛使用过的三角形图案(并且可能是有充分理由的。) triangulated hexagon

所有这些都有助于最终实现原始教程的目标:rendered hexagon