Hi all,
these are my fist steps in Unity3D to create a mesh manually, but I dont succeed yet :-(. I hope you can help me, I am not that long using Unity3D also, so I am a horrible beginner and not sure where to investigate first.
Idea:
Try to render a 128x128 plane manually by defining vertices and triangles. I do not necessarly need to store an instance in the assets (I do so currently), for me its sufficient when the object shows up at runtime in the scene.
Problem:
After I add a new GameObject by the Unity3D’s menu, the object shows up in the scene (so createPlane() ) is correctly called), but it does not update once I play/run the scene. I have the impression start() isnt called at all?! Setting breakpoints within start() in Mono does not help, the scene is just created with the mesh from the CreatePlane() method, nothing happens…
Any idea how to find the mistake?
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class ProceduralPlane4 : MonoBehaviour {
static int MESHFIELD_WIDTH = 128;
static int VERTEXARRAY_SIZE = MESHFIELD_WIDTH * MESHFIELD_WIDTH;
public VertexDefinition[] vertexDefinitionArray = new VertexDefinition[VERTEXARRAY_SIZE];
public List<ushort> vertexDefinitionArrayFreeSlots = new List<ushort>();
public List<TriangleIndicesDefinition> triangleIndicesDefinitionList = new List<TriangleIndicesDefinition>();
public string UUID = System.Guid.NewGuid().ToString();
void Awake () {
Debug.Log("ProceduralPlane4::Awake()");
}
void Start () {
Debug.Log("ProceduralPlane4::Start()");
for (ushort i = 0; i <VERTEXARRAY_SIZE; i++)
this.vertexDefinitionArrayFreeSlots.Add(i);
// Create the plane
float verticeDistance = 1;
int verticeIndex1;
int verticeIndex2;
int verticeIndex3;
VertexDefinition vertexDefinition;
for (ushort y=0; y < MESHFIELD_WIDTH;y++) {
for (ushort x=0; x < MESHFIELD_WIDTH;x++) {
ushort z = 0;
// First triangle
vertexDefinition = new VertexDefinition();
vertexDefinition.setPositionVector3(new Vector3(x,y,z));
verticeIndex1 = this.addVertexDefinition(vertexDefinition);
vertexDefinition = new VertexDefinition();
vertexDefinition.setPositionVector3(new Vector3(x+verticeDistance,y,z));
verticeIndex2 = this.addVertexDefinition(vertexDefinition);
vertexDefinition = new VertexDefinition();
vertexDefinition.setPositionVector3(new Vector3(x,y+verticeDistance,z));
verticeIndex3 = this.addVertexDefinition(vertexDefinition);
triangleIndicesDefinitionList.Add(new TriangleIndicesDefinition(verticeIndex1,verticeIndex2,verticeIndex3));
// Second triangle
vertexDefinition = new VertexDefinition();
vertexDefinition.setPositionVector3(new Vector3(x+verticeDistance,y,z));
verticeIndex1 = this.addVertexDefinition(vertexDefinition);
vertexDefinition = new VertexDefinition();
vertexDefinition.setPositionVector3(new Vector3(x+verticeDistance,y+verticeDistance,z));
verticeIndex2 = this.addVertexDefinition(vertexDefinition);
vertexDefinition = new VertexDefinition();
vertexDefinition.setPositionVector3(new Vector3(x,y+verticeDistance,z));
verticeIndex3 = this.addVertexDefinition(vertexDefinition);
triangleIndicesDefinitionList.Add(new TriangleIndicesDefinition(verticeIndex1,verticeIndex2,verticeIndex3));
}
}
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear();
// Add vertices
int vertices_index = 0;
for (int i = 0; i < VERTEXARRAY_SIZE; i++)
{
mesh.vertices[i] = vertexDefinitionArray[i].getPositionVector3();
}
// Add triangles
int triangles_index = 0;
for (int i = 0; i < triangleIndicesDefinitionList.Count; i++)
{
mesh.triangles[triangles_index] = triangleIndicesDefinitionList[i].getTriangleIndex1();
triangles_index++;
mesh.triangles[triangles_index] = triangleIndicesDefinitionList[i].getTriangleIndex2();
triangles_index++;
mesh.triangles[triangles_index] = triangleIndicesDefinitionList[i].getTriangleIndex3();
triangles_index++;
}
}
// Update is called once per frame
void Update () {
Debug.Log("ProceduralPlane4::Update()");
}
// CreatePlane()
[MenuItem("GameObject/Create Other/ProceduralPlane4")]
static public void CreatePlane() {
Debug.Log("ProceduralPlane3::CreatePlane()");
Mesh mesh = new Mesh();
Vector3[] vertices_pos = new Vector3[4];
Vector2[] vertices_uv = new Vector2[4];
int[] triangles = new int[6] {0,1,2,2,1,3};
// Vertex Layout
// 0 ------ 1
// | \ |
// | \ |
// | \ |
// | \|
// 2 ------ 3
vertices_pos[0] = -Vector3.right + Vector3.up;
vertices_pos[1] = Vector3.right + Vector3.up;
vertices_pos[2] = -Vector3.right - Vector3.up;
vertices_pos[3] = Vector3.right - Vector3.up;
vertices_uv[0] = new Vector2(0.0f, 1.0f);
vertices_uv[1] = new Vector2(1.0f, 1.0f);
vertices_uv[2] = new Vector2(0.0f, 0.0f);
vertices_uv[3] = new Vector2(1.0f, 0.0f);
mesh.vertices = vertices_pos;
mesh.triangles = triangles;
mesh.uv = vertices_uv;
mesh.RecalculateNormals();
GameObject newMeshObject = new GameObject();
newMeshObject.AddComponent<MeshFilter>().mesh = mesh;
newMeshObject.AddComponent<MeshRenderer>();
AssetDatabase.CreateAsset(mesh, "Assets/ProceduralAssets/ProceduralPlane4.asset");
}
// Add VertexDefinition to VertexDefinitionArray;
private ushort addVertexDefinition(VertexDefinition vertexDefinition)
{
ushort index = (ushort)this.vertexDefinitionArrayFreeSlots.IndexOf(0);
this.vertexDefinitionArray[index] = vertexDefinition;
this.vertexDefinitionArrayFreeSlots.Remove(0);
return index;
}
// Remove VertexDefinition from VertexDefinitionArray;
private void removeVertexDefinition(VertexDefinition vertexDefinition)
{
ushort index;
for (ushort i = 0; i < VERTEXARRAY_SIZE; i++)
{
if (vertexDefinitionArray[i] = vertexDefinition) {
index = i;
vertexDefinitionArray[i] = null;
this.vertexDefinitionArrayFreeSlots.Add(index);
}
}
}
}