Byte order?

Hey Hey!
Just your friendly local code no0b here :slight_smile:

Hoping someone can help me fix this last bit I’ve been stuck on for a little now.

Using this script, How are the bytes in a 16bit heightmap organized? - Questions & Answers - Unity Discussions

I’ve been able to import a 16bit heightmap, but as described by Eric I believe my byte order is mismatched…unfortunately this is my first time working with ANY of this information and I’m at a total loss on how to correct this. I have tried reversing the byte order, but everything goes out of whack and the terrain is completely unrecognizable…

Here is the current code (basically the same as what’s posted in the link above):

using UnityEngine;
using System.Collections;

public class testScript : MonoBehaviour {
    public TextAsset binaryData;
   
    private Terrain terrain; // terrain to modify
    protected int heightmapWidth; // heightmap width
    protected int heightmapHeight; // heightmap height
   
    void Start (){
        Terrain.activeTerrain.terrainData.heightmapResolution = 256;

        terrain = Terrain.activeTerrain;
        heightmapWidth = terrain.terrainData.heightmapWidth;
        heightmapHeight = terrain.terrainData.heightmapHeight;
       
        byte[] bytes = binaryData.bytes; // Loads the whole heightmap file into this array

        //System.Array.Reverse (bytes);

        float[,] heightmap = new float[heightmapWidth,heightmapHeight];
        int byteNumber = 0; // Will be used to iterate through the 'bytes' array
        for(int width = 0; width < bytes.Length/(heightmapWidth*2); width++){
            for(int height = 0; height < bytes.Length/(heightmapHeight*2); height++){
                heightmap[width, height] = ((bytes[byteNumber] << 0x08) | bytes[byteNumber+1])/65536f; // Sets value in 'bytes' array to matrix
                byteNumber += 2;
            }
        }
       
        terrain.terrainData.SetHeights(0, 0, heightmap);
    }
}

I’ve commented out the reverse line because I don’t think it’s what I’m looking for, I could be wrong though, I think what I’m probably going for is something more along these lines:
http://www.csharp-examples.net/reverse-bytes/

Either way…I can’t get any of it to work, so yeah. Here I am…again.

Current project build:

Array.Reverse is indeed not at all what you want; you’d reverse each 16-bit byte pair. But anyway that’s not relevant since it’s not a byte order problem, but a size problem. The dimensions must be power of two plus one.

–Eric

…embarrassing.
Thanks Eric!

Follow-up question.

I think I may be able to refine this a bit further, looking for second opinion. Rather than having however many .bytes files containing a segment of an image, is it possible to simply have 1 byte file containing the total image and grab the needed bytes from within the file? Possibly iterate through an array in order to achieve the same goal?

I believe I may, but not entirely sure how to tackle it just yet. If so it’ll save me a great deal of time as well as reduce overall project size. May even increase initial loading times.

Hopefully I’ve explained this well enough, but here’s an image that may clear what I’m asking up a bit