Search Unity

How does EncodeToPng work

Discussion in 'Scripting' started by harpingseal, Mar 4, 2022.

  1. harpingseal

    harpingseal

    Joined:
    Sep 3, 2020
    Posts:
    57
    So suppost o have an array as the pixel data exported from another program which is an array {{pixelx,pixely,colorR}{...}} and i want to export it to a heightmap by essentially uaing only the R as R ,G, and B how should i do that,the wiki only send me with the texture 2d approach but ive think ive say someone doing this with math.noise, but i cant seem to find it. I know that i ahoud probably use a foreach loop and then index the inner table but i don't know how to go from there
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    I'm a bit confused about your question...

    You have texture data from this program, a single channel R, and you want to just turn that into grayscale at that value?

    Then just do like you said: make the color out of R,R,R

    You lost me with the noise part though, I'm not sure what that is for.
     
    Bunny83 likes this.
  3. harpingseal

    harpingseal

    Joined:
    Sep 3, 2020
    Posts:
    57
    I know i could rrr, but what do i do next with the color data
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,993
    I'm equally confused here ^^. You said you get your data from another application as a pixel coordinate (x and y) as well as a sample value at this point. So you already have your data from an external source. I'm not sure why you mention using a noise function since those are used to generate heightmap data procedurally. This would be pretty much the opposite of importing / reading data you already have from another program.

    You also should be more clear what you mean by

    .
    An "array" is not a file format. If data is exported from an application it usually have some kind of encoding format (binary, json, xml, whatever) which you first have to read in and interpret correctly. In order to create a Texture2D out of your data you have to create a flattened Color or Color32 array inside Unity which you can use with SetPixels / SetPixels32.

    We know absolutely nothing about the format you receive your data in and what metadata you have about that data. Do you even know the width / height of the data? If not you may first need to iterate though all your samples to determine the min / max coordinates that your data samples represent. Though as already mentioned your question is way to vague.

    If you already read in the data successfully, you may want to provide some code example how your data structure currently looks like and what it actually represents. It's kinda unusual to have pixel coordinates associated with a sample value. This is usually used when you have point cloud data which represents volumetric data and not a surface heightmap.