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
1
Question by COMMUNIST239 · Oct 19, 2015 at 11:42 PM · rotationterrainimportscriptingbasicsmap

how to use a script to import terrain .raw?

I want players to be able to add the game the maps . I want that added .raw files . (via script)

Comment
Add comment
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

1 Reply

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

Answer by Bunny83 · Oct 20, 2015 at 12:37 AM

So you want to load a terrain heightmap at runtime from a file? ".raw" is not a specific format. It simply contains raw data. A quite common format for a height map is 16 grayscale, however in theory it could contains anything.

There is no built-in method to load raw terrain data. You have to load the file yourself and convert it into a float array that can be used with TerrainData.SetHeights.

If you want to support the same options Unity has for the terrain heightmap importer, you have to provide a similar GUI with options for bit depth and resolution. Without those you can't load the data. raw files don't have any header. It literally only contains the raw data. So when reading the file you either only support one specific format (for example 513 x 513 16bit) or you have to provide options for the user to choose from.

Here's an example how to read a 16 bit raw image with "windows" format (little endian):

 void LoadTerrain(string aFileName, TerrainData aTerrain)
 {
     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);
 }

There are no error checks. So if the file doesn't contain enough data it will fail with an exception. If the resolution doesn't match the terrain size the image won't appear right.

Comment
Add comment · Show 8 · 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 noonbear · Mar 09, 2016 at 02:28 AM 1
Share

What does the " / 0xFFFF " part mean in line 13 in your code?

avatar image Bunny83 noonbear · Mar 09, 2016 at 10:20 AM 2
Share

In that line I divide by the max possible value that can be represented by 16 bit.

0xFFFF = 65535

So an unsigned 16 bit value is in range 0 - 65535. By dividing that value by 0xFFFF the resulting values are in range 0.0 - 1.0

The float cast is important. ReadUInt16 returns an unsigned integer while 0xFFFF also represents an integer. If you would divide an integer by an integer the result would be again an integer. One of the operands need to be a float to get a float result.

Just in case you don't know:

FFFF is hexadecimal so each digit doesn't have 10 possible values but 16. So in addition to the numbers 0 to 9 there is A=10, B=11, C=12, D=13, E=14 and F=15. Unlike the decimal system the value of each digit isn't a multiple of 10 (1, 10, 100, 1000), but a multiple of 16 (1, 16, 256, 4096)

avatar image noonbear Bunny83 · Mar 09, 2016 at 05:09 PM 1
Share

Ah, thanks. Great explanation. So if we were reading an 8-bit heightmap, it would be " / 0xFF " or " / 255 ", since 8-bit is in the range 0 - 255, right?

Show more comments
avatar image noonbear · Mar 10, 2016 at 12:51 AM 0
Share

One more question--would you still use "ReadUInt16()" if the heightmap were 8-bit?

avatar image Bunny83 noonbear · Mar 10, 2016 at 11:38 AM 2
Share

No, :) of course not. ReadUInt16() reads an unsigned 16 bit integer from the binary stream. If your height map samples have 8 bit you have to read a single unsigned byte using ReadByte. See the documentation of the BinaryReader to find an appropriate method.

Note: As soon as a single sample has more than 8 bit you can get in trouble with the endianness of the system you work on. x86 / x64 PCs use the "little endian" format. That means types that require two or more bytes memory are stored in reverse order. So for example the integer value "5000" which is represented as 16 bit hex as "0x1388" is stored like this in memory:

  0  1
 -------
 88 13

As you can see the low order byte comes first and the high order byte comes last. There are systems (like $$anonymous$$ac afaik) which uses the big endian format so the same data would be stored like this:

  0  1
 -------
 13 88

I only work on x86 / x64 PCs so i never really had to take care about that. I just want to re$$anonymous$$d the reader to keep this in $$anonymous$$d when dealing with raw data. The Unity importer also provides a dropdown to allow the user to specify the endianness of the file.

avatar image noonbear Bunny83 · Mar 10, 2016 at 08:46 PM 0
Share

I see now, thanks a bunch!

avatar image spinaljack · Jun 26, 2018 at 01:27 PM 0
Share

Hi, This works great but I need it in mac byte order and also flipped vertical. How can I do that?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Terrain Tree Problem 0 Answers

Climbing script - impossible get under the map and turning around 0 Answers

How to use Height Maps 2 Answers

Stopping the 270 degree FBX default rotation on import 6 Answers

Adding additional maps to the texture layers 0 Answers


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