Raw File / Heightmap Switch at Runtime
Asked Answered
S

1

0

Hello, I have a game where I want the heightmap of the terrrain to be changed at runtime by the player, but I cannot get that to work. I have multiple .raw files in the project already that are waiting to to be used, but I cant figure out how to extract the data from them, and apply it to the terrain at runtime. (btw I use c# mostly, but i’m desperate enough to accept javascript files)

Thank you for your help it is very much appreciated.

Syllabism answered 10/4, 2024 at 13:38 Comment(2)

https://mcmap.net/q/52763/unity-how-to-use-a-script-to-import-terrain-raw

Fungible

Thanks for the replies... I have seen that link but the problem is that I cant figure out how to use it... any help?

Syllabism
F
0

I came up with a better method. The raw files can be imported and serialized by Unity when their extension is changed to .bytes and you can use the UnityEngine.TextAsset type to reference them. I made extension methods for the TerrainData class which can load the raw data from UnityEngine.TextAsset.bytes array.

You can make a TextAsset array in your script and drag the .byte heightmaps to it in the inspector:

public TextAsset[] heightmaps;

When you want to load a heightmap from the array to a terrain, call:

terrain.terrainData.LoadHeightmap(heightmaps[index].bytes);

Here’s the extension class script for TerrainData. You can choose the endianness (Little (windows) or Big (macintosh)). If adjustResolution parameter is true, the heightmap resolution of the terrain is matched to the file.

using UnityEngine;
using System.Collections;

public enum Endianness
{
    Little,
    Big
}

public static class TerrainExtensions {

    public static void LoadHeightmap(this TerrainData tData, byte[] rawData, Endianness dataEndianness = Endianness.Little, bool adjustResolution = false)
    {
        int h = (int)Mathf.Sqrt((float)rawData.Length / 2);
        if (adjustResolution) {
            var size = tData.size;
            tData.heightmapResolution = h;
            tData.size = size;
        }
        else if (h > tData.heightmapHeight) {
            h = tData.heightmapHeight;
        }
        int w = h;

        float[,] data = new float[h, w];
        int i = 0;
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                int u;
                if (dataEndianness == Endianness.Little) {
                    // little-endian (windows)
                    u = rawData[i + 1] << 8 | rawData*;*

}
else {
// big-endian (mac)
u = rawData << 8 | rawData[i + 1];
}
float v = (float)u / 0xFFFF;
data[y, x] = v;
i += 2;
}
}

tData.SetHeights(0, 0, data);
}
}
Other way (load directly from .raw files):
When you build your project you have to copy the heightmap file to your data folder (indicated by [Application.dataPath][1]) after the build has finished . With the default path in this script you have to create a Heightmaps folder inside the data folder and put the heightmap.raw file there. In editor the data folder is ProjectFolder/Assets so you can have it in Assets/Heightmaps/
public string heightmapPath = “/Heightmaps/heightmap.raw”;
public Terrain terrain;

void SomeMethod() {
* LoadTerrain(heightmapPath, terrain.terrainData);*
}

void LoadTerrain(string aFileName, TerrainData aTerrain)
{
* aFileName = Application.dataPath + aFileName*
* int h = aTerrain.heightmapHeight;*
* int w = aTerrain.heightmapWidth;*
* float[,] data = new float[h, w];*
* using (var file = System.IO.File.OpenRead(aFileName))*
* using (var reader = new System.IO.BinaryReader(file))*
* {*
* for (int y = 0; y < h; y++)*
* {*
* for (int x = 0; x < w; x++)*
* {*
* float v = (float)reader.ReadUInt16() / 0xFFFF;*
* data[y, x] = v;*
* }*
* }*
* }*
* aTerrain.SetHeights(0, 0, data);*
}
_*[1]: http://docs.unity3d.com/ScriptReference/Application-dataPath.html*_

Fungible answered 6/6, 2023 at 2:39 Comment(3)

I got it to work! Thank you so much for your help it was very much appreciated!

Syllabism

Actually I have run into a little problem now.... My terrain seams to be created all zigzagy and I cant figure out how to fix it

Syllabism

Has anyone been able to get this solution working in 2021?

Talkfest

© 2022 - 2025 — McMap. All rights reserved.