Why is the mesh not rendering?
I am working through a tutorial here. I think I can share the package through my google drive for convenience.
I have set up a scene that looks like this:
The subject object is HexGridController
and three objects that are disabled: center_fill
, Backdrop
, DiagnosticDisplay
.
The grid controller is an otherwise empty GameObject except for attached script:
...and the HexGridController script:
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
}
And finally, HexGrid
is a GameObject
with the following script attached:
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);
}
}
If you want developers to ask questions here (first as opposed to others, e.g., StackOverflow), maybe you should focus on answering the questions. I will add a link to my question on StackOverflow where my question at least received comments which led me in a direction to solve the problem myself. Thanks!!!
Answer by badkraft · May 31, 2017 at 09:27 PM
Since no one else provided any hint of an explanation or attempt to help, here is what I did to resolve the problem.
Your answer
Follow this Question
Related Questions
How to make mesh render objects overlay UI? 1 Answer
i dont see the sprite mesh in the list 2 Answers
How to make adjacent meshes look like they were joined? 0 Answers
Empty mesh with vertices and triangles 0 Answers
How can I layer images and mesh? 0 Answers