Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Brandon1375 · Sep 09, 2016 at 10:37 AM · c#terrainheightmaprawheightmaps

Raw File / Heightmap Switch at Runtime

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image villevli · Sep 09, 2016 at 01:39 PM 1
Share

http://answers.unity3d.com/questions/1084016/how-to-use-a-script-to-import-terrain-raw.html

avatar image Brandon1375 · Sep 09, 2016 at 08:36 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by villevli · Sep 09, 2016 at 09:10 PM

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[i];
                 }
                 else {
                     // big-endian (mac)
                     u = rawData[i] << 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) 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);
 }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Brandon1375 · Sep 09, 2016 at 10:50 PM 0
Share

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

avatar image Brandon1375 · Sep 10, 2016 at 12:28 PM 0
Share

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

avatar image villevli Brandon1375 · Sep 10, 2016 at 12:54 PM 0
Share

Is the raw file in the correct format? The script reads it as 16bit "windows" (little endian).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Set Hightmap resolution while importing custom heightmap texture? 1 Answer

Terrainmap is lined 1 Answer

why heightmap flips in terrain import raw(certain case)? 0 Answers

C#: Generate Raw File from byte[]? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges