• 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 /
  • Help Room /
avatar image
Question by Jamoy1993 · Jun 04, 2017 at 01:51 AM · terrainimageproceduralheightmapheightmaps

How does one convert a System.Drawing type Bitmap into a compatable terrain heightmap for unity

Off the bat i find it extremely annoying how unity is really picky about its height maps. In reality it doesn't really need to be gray scale as any rgb image can be used for a height map. Find the average of all colours to get the height.

I have managed to slice a large heightmap up based on the segmentation of my terrain generator. In my case im generating 5x5 128 unit grid of terrain. I figgured that i take a 5125 x 5125 pixel image and slice it up in the same way. So that gives me for each 128x128 terrain a nice high resolution 1025x1025 pixel image to pull height data from.

However i have tried a multitude of solutions arround the web and all of them seem to just be mimicking gray scale images by shifting the colours toward gray rather than changing the bit rate of the image.

I have scoured the forums and salvaged pieces of code from simmilar problems and this is the image generation code as of current:

     public void GenerateChunkHeightmapTile(string outputPath, Vector2 position, TerrainData chunk)
     {
 
         //Find the bounds of the image based on the ammount of chunks and width of the image, ie cell 2,2 on a 5x5 with heightmap master being 5125x5125 would be 1025 -> 2025 for example
         int xMin = (int)((position.x / terrainSpec.numChunks.x) * heightmapImage.Size.Width);
         int xMax = xMin + (int)(heightmapImage.Size.Width / terrainSpec.numChunks.x);
         int yMin = (int)((position.y / terrainSpec.numChunks.y) * heightmapImage.Size.Height);
         int yMax = yMin + (heightmapImage.Size.Height / (int)terrainSpec.numChunks.y);
 
         //Check if the output directory exists
         if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); }
 
         //The name of the file to be writen
         string outputFileName = Path.Combine(outputPath, string.Format("Heightmap_{0}_{1}.raw", (int)position.x, (int)position.y));
 
         //Define a rectangle that we will sample for our heightmap
         Rectangle tileBounds = new Rectangle(xMin, yMin, heightmapImage.Size.Width / (int)terrainSpec.numChunks.x, heightmapImage.Size.Height / (int)terrainSpec.numChunks.y);
 
         //size and format of the image, tried all pixel formats such as 16bitGrayscale and 1bit indexed, none works. It throws errors!
         Bitmap target = new Bitmap(xMax - xMin, yMax - yMin, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
 
 
         using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(target))
         {
             graphics.DrawImage(
                 heightmapImage,
                 new Rectangle(0, 0, target.Width, target.Height),
                 tileBounds,
                 GraphicsUnit.Pixel);
         }
 
         target.Save(outputFileName);
 
     }

I have managed to get all my images saved in raw format and are segmenting correctly. However the challenge is getting them saved as a file that can be used with the unity terrain. I do not want to have to manually convert these images myself as i intend to have terrains with hundreds of chunks on it. That would take hours. Here is the image code as is. I have tried changing the PixelFormat to 16bit grayscale 1bitindexed and all that and nothing seems to work. This is so annoying as i cant fathom why the heightmap has to be in raw format. Can anyone help me out?

Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

123 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

unity terrain apply texture by altitude 0 Answers

Procedural Terrain Diamond Square Height Issue 0 Answers

World Machine to Unity 2018.4.10f1 Heightmap Import Issues 0 Answers

Heightmap from png 1 Answer

Jagged Heightmap 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