【问题标题】:Why is the mesh not rendering?为什么网格不渲染?
【发布时间】:2017-05-26 18:53:33
【问题描述】:

我正在通过tutorial here 工作。为了方便,我有the package shared through my google drive

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

主题GameObjectHexGridController 和三个禁用的对象:center_fillBackdropDiagnosticDisplay

网格控制器是一个空的游戏对象,除了附加的脚本:

...以及 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 FacingNorth 的六边形方向 - 平坦或锐角(或尖锐)。我打算将最北点设为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 个三角形。

标签: c# unity3d mesh


【解决方案1】:

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

现在,我解决的问题和结果...

HexGridController 得到了简化,因为 Hexagon 提供了 verticesuv 的所有特定数据。三角形的数据保留在 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

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

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; 
}

现在,我选择了一种我没有看到广泛使用的三角形图案(当然这可能是有充分理由的)。

所有这些工作最终实现了原始教程的目标:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多