Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 soliddanii · Jan 30, 2014 at 08:18 PM · heightmapraw16 bits8bit

Import 8bit .raw file, and 16bit explanation.

Hello, this is my first question here, so please forgive me if I make any mistake. Im trying to create a script that imports .raw heightmaps to a desired terrain (like in the included "Import Heightmap" option. I Googled a lot, and I came with this:

 System.IO.FileInfo fi = new System.IO.FileInfo(path);
 
 int size =  m_heightMapRes*m_heightMapRes*2;
 
 System.IO.FileStream fs = fi.OpenRead();
 
 byte[] data = new byte[size];
 fs.Read(data, 0, size);
 fs.Close();
 
 bool bigendian = (m_rawByteOrder == BYTE_ORDER.Mac) ? true : false;
 
 int i=0;
 
 for(int x = 0 ; x < m_heightMapRes; x++) 
         {
             for(int y = 0; y < m_heightMapRes; y++) 
             {
                 //Extract 16 bit data and normalize.
                 float ht = (bigendian) ? (data[i++]*256.0f + data[i++]) : (data[i++] + data[i++]*256.0f);
                 htmap[m_heightMapRes-x-1,y] = ht / 65535.0f;
             }
         };

This works for 16bit raw files. What I don't understand: Why "size = m_heightMapRes*m_heightMapRes*2" is multplied by 2? An explanation for the "Extract 16 bit data part"? (I understand the littleendian and bigendian part)

I want to understand it, because I want to convert it to support 8bit files. Yes, im a noob in this. Thanks in advance.

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 soliddanii · Jan 31, 2014 at 03:12 PM 0
Share

Ok so searhing more y discovered that the size is given by: yRes*xRes*BitDepth (where 2 is 2bytes = 16 bits hehe). So I supose if the file is 8bit it will be size=xRes*yRes ? Still haven't figured out the extract part. Please help :D

avatar image soliddanii · Jan 31, 2014 at 08:01 PM 0
Share

Ok, now I understand the normalize part I think. for unsigned integers 16bit range is [0,65535] and 8bit range is [0,255]. It scales the data. So for a 8 bit image will it be like this?

  float ht = (bigendian) ? (data[i++]*256.0f + data[i++]) : (data[i++] + data[i++]*256.0f);
           htmap[m_height$$anonymous$$apRes-x-1,y] = ht / 256.0f;

Please help.

2 Replies

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

Answer by soliddanii · Feb 01, 2014 at 01:49 PM

In case anyone is wondering, this is the solution I achieved:

For 8 bit heightmaps this changes

 size =  heightMapRes*heightMapRes;

And,

 int i = 0;
 for(int x = 0 ; x < heightMapRes; x++) {
     for(int y = 0; y < heightMapRes; y++) {
         //Extract 8 bit data and normalize.
         htmap[heightMapRes-x-1,y] = data[i++]/255.0f; 
     }
 };

Obviously you get much worst results with 8 bit heightmaps, but thats not the point here. Thanks for the help guys.

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

Answer by UDN_237f197f-1c0d-4823-8d3d-3c7de090485b · May 16, 2017 at 10:00 AM

Hi, You are working with 16 bit raw file, 16 bit for each height value. File consist of bytes, which is 8 bit. So, you have to multiply map resolution by 2 to get the byte array size.

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

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

19 People are following this question.

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

Related Questions

How can I import terrain from DXF? 1 Answer

How are the bytes in a 16bit heightmap organized? 3 Answers

How to read raw files for custom terrain generation system? 0 Answers

Is there a definitive explanation for generating and importing raw heightmaps? 1 Answer

Understanding heightmap RAW imports 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