- Home /
Question by
ASFghost · Nov 16, 2012 at 06:10 AM ·
heightmapprocedural generationgetpixel
How do I access a heightmap to set the height of a cube?
I'm fairly new to the Unity engine and coding in general, and am interested in procedural terrain generation. To start out, I've been trying to figure out how to access a heightmap through scripts (instead of manually setting it through the Editor) to set the height of a Primitive cube on the y-axis. I already have a heightmap (in .raw format), but I'm not sure how to access it, nor how exactly to read a pixel (such as at the origin of a 256px x 256px image) to get a height value to apply to a the cube. I've tried using the Resources.Load function, but to no avail.
Here is my code so far; any help is much appreciated!
//JavaScript
function Start ()
{
var material : Material;
var heightMap : Texture2D;
var size = Vector3 (256, 10, 256);
var y = 0;
var Yheight = "?"; //This somehow should be set to the height of a pixel on the heightmap, such as at point (128,128)
var x : int = transform.position.x / size.x * heightmap.width;
var z : int = transform.position.z / size.z * heightmap.height;
transform.position.y = heightMap.GetPixel(x, z).grayscale * size.y;
var cube : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = Vector3(0, Yheight, 0);
cube.renderer.material.mainTexture = Resources.Load("TestTexture");
}
Comment