I have a procedural generated Terrain
, based on Unity's Terrain System
.
Now i want a Map
from the Terrain
, not a minimap but a full map, that is saved as a 2D Texture
.
First i thought of a RenderTexture
, but if i take a Camera
to catch the whole Terrain
, the result depends on the resolution aspect, and i also have problems cause the width
of the Terrain
is 3.2x
the length
.
Is there a better way to solve this problem?
Make a new camera and place it over the terrain.
Make sure it is in orthographic mode
Set the camera.aspect
field to terrainData.size.x / terrainData.size.z
Set the camera.orthographicSize
field to terrainData.size.z/2f
Make sure the camera frame is oriented with the terrain's axes. Something along the lines of cam.transform.LookAt(terrain.GetPosition(), terrain.transform.forward);
will do the trick.
Then, you should be able to create a RenderTexture
with the pixel resolution you'd like (based on this answer by Rafal Wiliński):
int resHeight = 1000;
int resWidth = Mathf.RoundToInt(resHeight * camera.aspect);
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
camera.targetTexture = rt; //Create new renderTexture and assign to camera
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); //Create new texture
camera.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); //Apply pixels from camera onto Texture2D
camera.targetTexture = null;
RenderTexture.active = null; //Clean
Destroy(rt); //Free memory
then, your terrain would be captured in screenshot