Search Unity

Generating Texture2D with custom color array at runtime seems to be not working correctly

Discussion in 'Scripting' started by satium2142, Sep 27, 2019.

  1. satium2142

    satium2142

    Joined:
    Sep 27, 2019
    Posts:
    2
    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:

    https://i.imgur.com/tJXuvjM.png
    (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 <.<)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class TerrainComponentExporter : MonoBehaviour
    8. {
    9.  
    10.     public GameObject template;
    11.     public GameObject meshdebug;
    12.     public SpriteRenderer spritedebug;
    13.     public Image imageDebug;
    14.  
    15.     public TextureFormat format;
    16.     public bool mipChain;
    17.     public bool isLinear;
    18.  
    19.  
    20.     void Start()
    21.     {
    22.         Terrain t = Terrain.activeTerrain;
    23.         int size = (int)t.terrainData.size.x;
    24.  
    25.         Texture2D tex = new Texture2D(size, size, format,mipChain ,isLinear);
    26.    
    27.         //Color32[] colors = tex.GetPixels32();
    28.         Color32[] colors = new Color32[(size*size)];
    29.  
    30. // generate colors for the "highmap"
    31.         for (int i = 0; i < size; i++)
    32.         {
    33.             for (int ii = 0; ii < size; ii++)
    34.             {
    35.                 Vector3 testPos = new Vector3(0.5f + i, 0, 0.5f + ii);
    36.                 byte height = (byte)Mathf.Clamp(Mathf.Round(t.SampleHeight(testPos)), 0, 255);
    37.                 colors[(i + ii)] = new Color32(height, height, height, 255);
    38.             }
    39.         }
    40.         GenerateDebugWorldPixels(colors,size,size,new Vector3(0,-10,0));
    41.         tex.SetPixels32(colors);
    42.         tex.Apply();
    43.         //GenerateDebugWorldPixels(tex.GetPixels32(),size,size, new Vector3(0, -10, 0));
    44.  
    45.         meshdebug.GetComponent<MeshRenderer>().material.mainTexture = tex;
    46.  
    47.         Sprite sp = Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(), 16f);
    48.         spritedebug.sprite = sp;
    49.         imageDebug.sprite = sp;
    50.        
    51.         // byte[] bytes = tex.EncodeToPNG();
    52.         // File.WriteAllBytes(Application.dataPath + "/heightmap.png", bytes);
    53.  
    54.  
    55.  
    56.     }
    57.  
    58.  
    59.     void GenerateDebugWorldPixels(Color32[] colors, int sizeX, int sizeY, Vector3 worldPos)
    60.     {
    61.         for (int i = 0; i < sizeX; i++)
    62.         {
    63.             for (int ii = 0; ii < sizeY; ii++)
    64.             {
    65.                 GameObject go = Instantiate(template, new Vector3(worldPos.x+i, worldPos.y, worldPos.z+ii), Quaternion.Euler(90, 0, 0));
    66.                 go.name = (i + "," + ii);
    67.                 go.GetComponent<SpriteRenderer>().color = colors[(i + ii)];
    68.  
    69.             }
    70.         }
    71.     }
    72.  
    73. }
    74.  
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Havent understood all your code but this looks odd:
    colors[(i + ii)]
    Usually the 2d index mapped to a 1d one is (in your notation):
    i*size + ii
    Just try it for a small array and Debug.Log the position you write to.
     
  3. satium2142

    satium2142

    Joined:
    Sep 27, 2019
    Posts:
    2
    Thanks bro... i didnt see that xD, fixes my problem and now he writes all colors correctly in the array, so the export is no problem anymore