I try to write some code for exporting terraindata as an highmap. that is all okay but wehn i put my generated color array in a code created texture2D the result is this:
(the big one is the correct generated color array. the smaler ones the finished texture)
its seems to be that the pixels will be only set in the first 2 rows. maybe some wrong format or something?
here is my code:
(sorry for the mess <.<)
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class TerrainComponentExporter : MonoBehaviour
{
public GameObject template;
public GameObject meshdebug;
public SpriteRenderer spritedebug;
public Image imageDebug;
public TextureFormat format;
public bool mipChain;
public bool isLinear;
void Start()
{
Terrain t = Terrain.activeTerrain;
int size = (int)t.terrainData.size.x;
Texture2D tex = new Texture2D(size, size, format,mipChain ,isLinear);
//Color32[] colors = tex.GetPixels32();
Color32[] colors = new Color32[(size*size)];
// generate colors for the "highmap"
for (int i = 0; i < size; i++)
{
for (int ii = 0; ii < size; ii++)
{
Vector3 testPos = new Vector3(0.5f + i, 0, 0.5f + ii);
byte height = (byte)Mathf.Clamp(Mathf.Round(t.SampleHeight(testPos)), 0, 255);
colors[(i + ii)] = new Color32(height, height, height, 255);
}
}
GenerateDebugWorldPixels(colors,size,size,new Vector3(0,-10,0));
tex.SetPixels32(colors);
tex.Apply();
//GenerateDebugWorldPixels(tex.GetPixels32(),size,size, new Vector3(0, -10, 0));
meshdebug.GetComponent<MeshRenderer>().material.mainTexture = tex;
Sprite sp = Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(), 16f);
spritedebug.sprite = sp;
imageDebug.sprite = sp;
// byte[] bytes = tex.EncodeToPNG();
// File.WriteAllBytes(Application.dataPath + "/heightmap.png", bytes);
}
void GenerateDebugWorldPixels(Color32[] colors, int sizeX, int sizeY, Vector3 worldPos)
{
for (int i = 0; i < sizeX; i++)
{
for (int ii = 0; ii < sizeY; ii++)
{
GameObject go = Instantiate(template, new Vector3(worldPos.x+i, worldPos.y, worldPos.z+ii), Quaternion.Euler(90, 0, 0));
go.name = (i + "," + ii);
go.GetComponent<SpriteRenderer>().color = colors[(i + ii)];
}
}
}
}