- Home /
Mesh creation during runtime
I was messing around with some of unity's example projects, more specifically, the heightmap one and the fractal texture one. I managed to combine them to make some really nice stuff. It was kinda hard because I had to convert them to c# (the language I know) from javascript. Although I figured out the majority of the script, I can't seem to figure out the mesh creation part. Can someone help me out?
// Build triangle indices: 3 indices into vertex array for each triangle
int[] triangles = new int[(height - 1) * (width - 1) * 6];
int index = 0;
for (y=0;y<height-1;y++)
{
for (x=0;x<width-1;x++)
{
// For each grid cell output two triangles
triangles[index++] = (y * width) + x;
triangles[index++] = ((y+1) * width) + x;
triangles[index++] = (y * width) + x + 1;
triangles[index++] = ((y+1) * width) + x;
triangles[index++] = ((y+1) * width) + x + 1;
triangles[index++] = (y * width) + x + 1;
}
}
// And assign them to the mesh
mesh.triangles = triangles;
// Auto-calculate vertex normals from the mesh
mesh.RecalculateNormals();
mesh.RecalculateBounds();
// Assign tangents after recalculating normals
mesh.tangents = tangents;
I think this is the part that creates the mesh. Basically, what the script does, is it creates a mesh from a texture generated with perlin noise. I want to do 2 things:
1- be able to create the meshes separated from each other (people say that's how you make the mesh hard-edged, and that's what I want) and 2 - I want to be able to change the position of the vertices.
But I don't see what part of the code that's setting up the positions. And I can't debug this for loop, if I put a print() or a debug.log() there, the editor crashes!
I'm very motivated to learn about procedural meshes, because they're so fascinating. So I appreciate if someone would be able to help me out with this :)
In this code, the vertices are not positionated at all. There are a lot of mesh generation tutorials for Unity, check out this one:
Procedural generated mesh in Unity
as well as the Scripting API for Unity's $$anonymous$$esh class.
Well, it isn't? Now i'm really confused. This is the original script, in javascript, found in one of unity's procedural examples:
var height$$anonymous$$ap : Texture2D;
var material : $$anonymous$$aterial;
var size = Vector3(200, 30, 200);
function Start ()
{
GenerateHeightmap();
}
function GenerateHeightmap ()
{
// Create the game object containing the renderer
gameObject.AddComponent($$anonymous$$eshFilter);
gameObject.AddComponent.<$$anonymous$$eshRenderer>();
if (material)
GetComponent.<Renderer>().material = material;
else
GetComponent.<Renderer>().material.color = Color.white;
// Retrieve a mesh instance
var mesh : $$anonymous$$esh = GetComponent($$anonymous$$eshFilter).mesh;
var width : int = $$anonymous$$athf.$$anonymous$$in(height$$anonymous$$ap.width, 255);
var height : int = $$anonymous$$athf.$$anonymous$$in(height$$anonymous$$ap.height, 255);
var y = 0;
var x = 0;
// Build vertices and UVs
var vertices = new Vector3[height * width];
var uv = new Vector2[height * width];
var tangents = new Vector4[height * width];
var uvScale = Vector2 (1.0 / (width - 1), 1.0 / (height - 1));
var sizeScale = Vector3 (size.x / (width - 1), size.y, size.z / (height - 1));
for (y=0;y<height;y++)
{
for (x=0;x<width;x++)
{
var pixelHeight = height$$anonymous$$ap.GetPixel(x, y).grayscale;
var vertex = Vector3 (x, pixelHeight, y);
vertices[y*width + x] = Vector3.Scale(sizeScale, vertex);
uv[y*width + x] = Vector2.Scale(Vector2 (x, y), uvScale);
// Calculate tangent vector: a vector that goes from previous vertex
// to next along X direction. We need tangents if we intend to
// use bumpmap shaders on the mesh.
var vertexL = Vector3( x-1, height$$anonymous$$ap.GetPixel(x-1, y).grayscale, y );
var vertexR = Vector3( x+1, height$$anonymous$$ap.GetPixel(x+1, y).grayscale, y );
var tan = Vector3.Scale( sizeScale, vertexR - vertexL ).normalized;
tangents[y*width + x] = Vector4( tan.x, tan.y, tan.z, -1.0 );
}
}
// Assign them to the mesh
mesh.vertices = vertices;
mesh.uv = uv;
// Build triangle indices: 3 indices into vertex array for each triangle
var triangles = new int[(height - 1) * (width - 1) * 6];
var index = 0;
for (y=0;y<height-1;y++)
{
for (x=0;x<width-1;x++)
{
// For each grid cell output two triangles
triangles[index++] = (y * width) + x;
triangles[index++] = ((y+1) * width) + x;
triangles[index++] = (y * width) + x + 1;
triangles[index++] = ((y+1) * width) + x;
triangles[index++] = ((y+1) * width) + x + 1;
triangles[index++] = (y * width) + x + 1;
}
}
// And assign them to the mesh
mesh.triangles = triangles;
// Auto-calculate vertex normals from the mesh
mesh.RecalculateNormals();
// Assign tangents after recalculating normals
mesh.tangents = tangents;
}
Lines 28-54 set the position of the vertices (the points). It also sets up the uvs (used for textures). Then lines 55-75 build the triangles from the vertices.
Every int in int[]triangles
is the index of a vertex in Vector3[]vertices
. Every 3 ints in int[]triangles
defines 1 triangles.
Could you give me an example of how could I change the position of a single vertex? Also, how can I make both triangles separated from each other? This code is really complex for me. I'm really trying to understand it. I can't visualize how does it get each vertex and sets up a position for it.
Your answer
Follow this Question
Related Questions
Check if a mesh has UV map at runtime 1 Answer
Conforming a mesh path to arbitrary surface - runtime 1 Answer
Runtime import of Collada files 3 Answers
Deleting one triangle out of mesh? Cut hole in mesh in runtime? 4 Answers
Mesh Mountains 1 Answer