I’m trying to create a mesh from scratch. I’ve got everything coded and I’m not getting any errors or warnings but nothing is showing up in my game. Help appreciated.
import UnityEngine
//Unity will add these files to any object we attach this script file to
[RequireComponent(MeshCollider)]
[RequireComponent(MeshFilter)]
[RequireComponent(MeshRenderer)]
class TerrainMap (MonoBehaviour):
//size 4 gives a 4x4 square grid
public gridSize as int
def Start ():
generateTerrain()
def generateTerrain():
gridVerts as int = (gridSize+1)**2 //Number of vertices in the grid
//Create a new Mesh structure
newMesh as Mesh = Mesh()
//Create data containers we'll eventually send to the Mesh
vertices = array(Vector3, gridVerts)
normals = array(Vector3, gridVerts)
triangles = array(int, 6*(gridSize**2))
uv = array(Vector2, gridVerts)
//Label a file cabinet from 0 to #, where each drawer contains data specific to
//a certain vertex on a 3D map. Store 2 vectors in each drawer of the cabinet:
//A vector pointing to the vertex's position in 3d space, and a vector normal
//to the surface the vertex lies on.
//Put drawers in the cabinet
currentVerts_Cabinet as int //Drawer of the current vertex
diagnalVerts_Cabinet as int //Drawer -somewhere in the cabinet- for the vertex in the (x, -z) direction
lowerVerts_Cabinet as int //Drawer -somewhere in the cabinet- for the vertext in the (0,-z) direction
rightVerts_Cabinet as int //Drawer -somewhere in the cabinet- for the vertext in the (x,0) direction
for z as int in range(0,gridSize+1): //for columns on the map's z axis
for x as int in range(0, gridSize+1): //and rows on the map's x axis
currentVerts_Cabinet = (gridSize+1) * x + z //The vertex's drawer in our single, tall file cabinet
vertices[currentVerts_Cabinet] = Vector3(x, 0, -z) //Assign each vertices its own directional vector
normals[currentVerts_Cabinet] = Vector3.up //Assign each vertices its normal direction
uv[currentVerts_Cabinet] = Vector2(x, z) / gridSize
//We want the to be able to open the "Vertex Drawers" of our cabinet
//and use the info in them to draw triangles. But the drawers
//are ordered by where the Vertex's appear on the map, not by what
//triangle they are a part of. Let's create a list that groups them
//by triangle:
if x < (gridSize) and z < (gridSize): //If there are no vertex's around to group into a
//triangle, don't draw one.
currentTriangle = 6*(currentVerts_Cabinet-x) //Each group of 2 triangles (that make a tile) take
//6 vertex's to draw, so we'll need to reference
//6 drawers for each triangle.
//Get the drawer labels for the surrounding vertices
diagnalVerts_Cabinet = (gridSize+1) * (x+1) + (z+1)
lowerVerts_Cabinet = (gridSize+1) * x + (z+1)
rightVerts_Cabinet = (gridSize+1) * (x+1) + z
//Group the drawers, in order, that we need to draw the tile's left triangle
triangles[currentTriangle] = currentVerts_Cabinet
triangles[currentTriangle] = diagnalVerts_Cabinet
triangles[currentTriangle] = lowerVerts_Cabinet
//Group the drawers, in order, that we need to draw the tile's right triangle
triangles[currentTriangle] = currentVerts_Cabinet
triangles[currentTriangle] = rightVerts_Cabinet
triangles[currentTriangle] = diagnalVerts_Cabinet
//send data to new mesh
newMesh.vertices = vertices
newMesh.normals = normals
newMesh.triangles = triangles
newMesh.uv = uv
//Point Mesh filter to new mesh
mf as MeshFilter = GetComponent(MeshFilter)
mf.mesh = newMesh




