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 User Banned · Aug 08, 2013 at 07:32 PM · terrainartifactsserializebinary

Terrain artifacts after binary serialization

Hi,

Im working towards serialising my Terrain, and from what I can gather I have to create a byte[] out of Getheights and then convert it back to Setheights to reimplement.

When testing a basic implementation of that process I run into many artefacts and much unpleasantness.

Im fairly sure its something to do with the way im reimplementing the byte[] into Setheights, as altering the division value changes the amplitude of the distortion, however ive hit a wall with how to resolve the situation.

Any help would be greatly appreciated.

An example of the distortion:

Happy

alt text

Not happy.

alt text

 using UnityEngine;
 using System.Collections;
 
 public class terrainSTATE : MonoBehaviour
 {
     public bool getdat;
     public bool setdat;
     byte[] Bytes;
 
     void Update ()
     {
         if (getdat) {
             GetData ();
         }
         if (setdat) {
             SetData ();
         }
     }
     
     void GetData ()
     {
         
         TerrainData TD = GetComponent<Terrain> ().terrainData;
         
         float[,] heights = TD.GetHeights (0, 0, 512, 512);
         Bytes = new byte[263169];
         
         int i = 0;
         for (int x = 0; x < 512; ++x) {
             for (int y = 0; y < 512; ++y) {
                 Bytes [i] = (byte)Mathf.Round (Mathf.Lerp (0, 65535, heights [x, y]));
                 i++;
             }
         }
         getdat = !getdat;
     }
     
     void SetData ()
     {
         TerrainData TD = GetComponent<Terrain> ().terrainData;
         int heightmapWidth = TD.heightmapWidth;
         int heightmapHeight = TD.heightmapHeight;
         
         float[,] heightmap = new float[heightmapWidth, heightmapHeight];
         int i = 0;
         for (int x = 0; x < 512; ++x) {
             for (int y = 0; y < 512; ++y) {
                 heightmap [x, y] = Bytes [i] / 512f;
                 i++;
 
             }
         }
         TD.SetHeights (0, 0, heightmap);
         setdat = !setdat;
     }
 }

1.jpg (35.0 kB)
2.jpg (48.1 kB)
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
2
Best Answer

Answer by whydoidoit · Aug 08, 2013 at 08:55 PM

A byte can only hold a value between 0 and 255 so I'm a bit confused about:

  • Your Lerp is between 0 and 65535

  • You are dividing the result by 512

I would have thought:

   Bytes[i] = (byte)Mathf.RoundToInt(255 * heights[x,y]);

   heightmap [x,y] = Bytes[i] / 255f;

You could of course just serialize the floats, or are you trying to keep it compressed? It will be "steppy" if you round it to bytes due to the reduction in precision.

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 User Banned · Aug 09, 2013 at 12:56 PM 0
Share

That works as described thanks. The confusion came from me stitching bits together from unrelated sources and not having the understanding to modify it correctly.

Actually im just experimenting with methods of saving the terrain using your Unity Serializer. I figured pulling Getheights into a byte[] on a holder script then serialising that was the most efficient way to do it.

Seeing how steppy the output is though floats would appear the best direction to go, even though i assume performance wise thats going to be a bit of a hit.

Thanks for your help.

avatar image whydoidoit · Aug 09, 2013 at 01:32 PM 0
Share

Well floats take 4 bytes each, compared to 1 byte for the way you have it. But I would go the "float" route myself.

avatar image User Banned · Aug 11, 2013 at 02:41 PM 0
Share

Hmm, storing as a float[] (or generic list) does work, however though Unity Serializer functions correctly over assembly reloads during runtime, stopping and replaying seems to wipe all save data. This behaviour only happens when this array is serialised, and does not happen with other arrays/lists of type.

Edit: On further testing with different length generic list of floats i find the following behaviour.

A length of >48975 no saved data persists across loads.

<=48975 && >24423 only 1 saved entry will persist.

<=24423 2 saves will persist.

<=12147 will allow 4 saves...

Any idea why this might be? Am i missing something?

$$anonymous$$y apologies for taking the topic well out of the scope of the original question.

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

16 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

Related Questions

Unity Terrain Serialize as Binary 3 Answers

Terrain texture artifacts when normal mapped 1 Answer

"Asset Serialization Mode" - Is that only during development or runtime as well? 1 Answer

Make a simple tree 1 Answer

Lighting Artifacts at Seams in Tiled Terrain 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