- Home /
using grayscale texture as heightmap, vertices and pixel don't match
Hello, my problem is shown in the following picture:
In my scene, I render 6 textures, and use them on a spheriphied cube, as heightmaps. Now, aside the seams at the borders of the faces, ( I've been addressed that it's because of different normals direction, but have no idea on how to deal with them now, tho)
But mainly, there is something wrong, probably in my texture-to-mesh adaptation procedure. In fact, looking at the attached image, you can see that the southern mountains (circled in green) is erroneously repeated on the last vertices of the upper border (the one vertex-wide "cliff" circled in red)
I don't know if it's clear from the image, or from my words, but I have no idea of a better and more precise way of interpreting a pixel-vertex perfect heightmap.
here's the code for rendering the 6 heightmaps
function rendercubemap()
{
var go = new GameObject ("SkyboxCamera", Camera);
go.camera.backgroundColor = Color.black;
go.camera.clearFlags = CameraClearFlags.Skybox;
go.camera.fieldOfView = 90;
go.camera.aspect = 1.0;
go.transform.position = transform.position;
go.transform.rotation = Quaternion.identity;
for (var orientation = 0; orientation < 2 ; orientation++)
{
renderSkyImage(orientation, go);
singleplane[orientation].renderer.material.mainTexture = top_tex;
}
}
function renderSkyImage(orientation : int, go : GameObject)
{
go.transform.eulerAngles = direzione_angolo[orientation];
var screenSize = 128;
var rt = new RenderTexture (screenSize, screenSize, 24);
go.camera.targetTexture = rt;
var screenShot = new Texture2D (screenSize, screenSize, TextureFormat.RGB24, false);
var screenShotdiff = new Texture2D (screenSize, screenSize, TextureFormat.RGB24, false);
go.camera.Render();
RenderTexture.active = rt;
screenShot.ReadPixels (Rect (0, 0, screenSize, screenSize), 0, 0);
screenShotdiff.ReadPixels (Rect (0, 0, screenSize, screenSize), 0, 0);
screenShotdiff = colorizza(screenShotdiff);
screenShot.Apply();
screenShotdiff.Apply();
top_tex=screenShot;
top_dif=screenShotdiff;
RenderTexture.active = null;
DestroyImmediate (rt);
}
and here is the part where i use it as a heightmap on the spheriphied planes:
function elevate()
{
for (var orientation = 0; orientation < direzione_angolo.length ; orientation++)
{
heightMap = singleplane[orientation].renderer.material.mainTexture;
var baseVertices : Vector3[];
var mesh : Mesh = singleplane[orientation].GetComponent(MeshFilter).mesh;
if (baseVertices == null)
baseVertices = mesh.vertices;
var vertices = new Vector3[baseVertices.Length];
// print(baseVertices.Length);
var i=16640;
//texture
var y = 0;
var x = 0;
var maxvertices = Mathf.Sqrt(baseVertices.Length);
var counter = 0;
for (y=maxvertices;y>0;y--)
{
for (x=0;x<maxvertices;x++)
{
var pixelHeight = heightMap.GetPixel(x, y).grayscale;
counter++;
var vertex = baseVertices[i];
//that is just a temporary workaround to avoid extruding border vertices
if(x<1)
{
vertex = vertex*1;
}
if(y<2)
{
vertex = vertex*1;
}
if(x>(maxvertices-2))
{
vertex = vertex*1;
}
if(y>(maxvertices-1))
{
vertex = vertex*1;
}
//if it's not a border vertex, then :
else if (x>=1 && y>=2 && x<=(maxvertices-2) && y<=(maxvertices-1))
{
vertex = vertex*((pixelHeight/10)+1);
}
vertices[i] = vertex;
i--;
}
}
print(counter);
mesh.vertices = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
print(mesh.vertexCount);
DestroyImmediate(collider);
}
}
thank you all for your time.