- Home /
Heightmap on primitive plane
I've written a small function that attempts to assign a heightmap data from a procedurally generated image to the plane. The problem I'm having is that the heightmap results are not related to the procedurally generated image.
I've attached an image to demonstrate this:
![alt text][1]
I think I may be missing something vital to do with assigning height map data to displace the verts.
I've posted my function below.
any ideas? C
public void GenerateHeightmap() {
Texture2D heightMap = renderer.material.mainTexture as Texture2D;
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] baseVertices = mesh.vertices;
print((float)baseVertices.Length * 512f);
//return;
Vector3[] vertices = new Vector3[baseVertices.Length];
print(baseVertices.Length);
print(baseVertices[baseVertices.Length - 1]);
int i = baseVertices.Length - 1;
int y = 0;
int x = 0;
int numvertici = (int)Mathf.Sqrt(baseVertices.Length);
//float risoluzionehmp = heightMap.height;
int counter = 0;
for (y=numvertici;y>0;y--)
{
for (x=0;x<numvertici;x++)
{
float pixelHeight = heightMap.GetPixel(x, y).grayscale;
counter++;
print(pixelHeight);
Vector3 vertex = baseVertices[i];
vertex = vertex + new Vector3(vertex.x,vertex.y,vertex.z - pixelHeight);
vertices[i] = vertex;
i--;
}
}
mesh.vertices = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
So the plane shares all of its vertices between triangles? In your line:
vertex = vertex + new Vector3...
you are effectively doubling the vertex (albeit the z is only set to 2z - pixelHeight)
Please could you expanded on that, I think I'm not sure I grasp the concept of heightmapping very well. You have any links for further reading?
Well so
1: If you plane is made of triangles then it is possible that individual triangles do not share vertices even though they share vertex locations (so there are 2 vertices in the same physical place). It depends on how the plane was created. Secondly - it is very likely that the vertices in the plane are not laid out in neat y/x order!
So to circumvent this the best idea would be to iterate through all of the vertices - convert their model space coordinates to height map coordinates and then read in the height map data - this allows the vertices to be in any order.
2: At the moment you are adding the vertex to itself - that's can't be right. Presumably you just want to add the offset data so your line should read:
vertex = vertex - Vector3.up * pixelHeight;
or if you prefer
vertex = vertex - new Vector3(0,0,pixelHeight);
Also are you really modelling height in the Z dimension? Normally I have height as Y.
//Work out the model space size of the plane
var $$anonymous$$X = float.$$anonymous$$axValue;
var $$anonymous$$Y = float.$$anonymous$$axValue;
var maxX = float.$$anonymous$$inValue;
var maxY = float.$$anonymous$$inValue;
var vertices = mesh.vertices;
for(var v = 0; v < vertices.Length; v++)
{
var vertex = vertices[v];
$$anonymous$$X = $$anonymous$$athf.$$anonymous$$in(vertex.x, $$anonymous$$X);
$$anonymous$$Y = $$anonymous$$athf.$$anonymous$$in(vertex.y, $$anonymous$$Y);
maxX = $$anonymous$$athf.$$anonymous$$ax(vertex.x, maxX);
maxY = $$anonymous$$athf.$$anonymous$$ax(vertex.y, maxY);
}
for(var v = 0; v < vertices.Length; v++)
{
var vertex = vertices[v];
vertex -= Vector3.up * height$$anonymous$$ap.GetPixel($$anonymous$$athf.FloorToInt(height$$anonymous$$ap.width * (vertex.x - $$anonymous$$X)/(maxX - $$anonymous$$X))), $$anonymous$$athf.FloorToInt(height$$anonymous$$ap.height * (vertex.y - $$anonymous$$Y)/(maxY - $$anonymous$$Y)))).grayscale * someScalingFactor;
vertices[v] = vertex;
}
Answer by Bunny83 · Dec 04, 2012 at 03:04 PM
When the positive y axis is your planes normal vector (the buildin plane in Unity uses y), just do this:
baseVertices[i] += Vector3.up * pixelHeight * desiredMaxExtrude;
Vector3.up is just this vector: new Vector(0,1,0) so here's the long form:
baseVertices[i] = baseVertices[i] + new Vector3(0, pixelHeight * desiredMaxExtrude, 0);
Another alternative would be:
baseVertices[i].y = baseVertices[i].y + pixelHeight * desiredMaxExtrude;
Answer by alessandro_88 · Apr 19, 2014 at 05:49 PM
dear caiuse, could you please post the final working code? thanks a lot! :)
Your answer

Follow this Question
Related Questions
3D Tilemap 1 Answer
using grayscale texture as heightmap, vertices and pixel don't match 0 Answers
Import a 513x513 Texture2D (heightmap) 0 Answers
Terrain Clone 0 Answers