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 /
  • Help Room /
avatar image
0
Question by Eldoir · Aug 09, 2017 at 11:29 AM · terrainscript.terraindataterraintexturesplatmap

How to modify a Terrain at runtime and get back to original Terrain on exit?

Hello,

I'm trying to rotate a terrain in runtime, then I want the terrain to get back to its original rotation when I quit the app.

I have basically this algorithm:

  • Copy ("clone") the Terrain Data

  • Rotate that copy

  • When quitting, the copy is automatically destroyed, so the terrain should get back to its original data.

The following code implements this algorithm:

 using UnityEngine;
 
 public class RotateTerrain : MonoBehaviour
 {
     [SerializeField]
     private Terrain terrain;
 
     void Start()
     {
         WorkOnNewCopy();
         // Now we can safely modify our terrain... Can we?
         TerrainRotate(terrain, degrees: 90);
     }
 
     void WorkOnNewCopy()
     {
         TerrainData backup_terrainData = terrain.terrainData;
 
         backup_terrainData.name = terrain.terrainData.name + "_tmp";
         backup_terrainData = Instantiate(backup_terrainData);
 
         terrain.terrainData = backup_terrainData;
         terrain.GetComponent<TerrainCollider>().terrainData = backup_terrainData;
 
         terrain.Flush();
     }
 
     void TerrainRotate(Terrain terrain, float degrees)
     {
         // Super-secret code
     }
 }

It's almost working: when I quit the app (or editor), I find myself with a weird mix between the original terrain, and the modified terrain.

I've attached some pictures (before rotation, and after rotation and quitting): everything seems to be back, except the ground texture (splat map). I feel close to the solution!

Have you got an idea? Thanks in advance! :)

alt textalt text

before.png (287.8 kB)
after-quitting.png (286.5 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

2 Replies

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

Answer by Eldoir · Aug 11, 2017 at 09:52 AM

EDIT:

I used this awesome script to safely clone my terrain: https://gist.github.com/Eldoir/d5a438dfedee55552915b55097dda1d4

Just had to write this:

 terrain.terrainData = TerrainDataCloner.Clone(terrain.terrainData);
 terrain.GetComponent<TerrainCollider>().terrainData = terrain.terrainData; // Don't forget to update the TerrainCollider as well

After this, the rotation works perfectly, and it reverts back automatically when I quit. Just what I wanted!

PREVIOUS ANSWER:

Hi Menyus777,

Yes I use 6 textures for my terrain. Maybe it could be this? I just need to rotate all of my splatmaps?

And yes, the "super-secret" comment was more of a joke, since I mostly use source code from Terrain Rotator plugin, which is free :D

I just wanted to show the algorithm and I wanted it clean and understandable so the rotation code was unrelated to me!

But, part of my code is about splatmaps, and I have the feeling it should already rotate ALL the splatmaps, so is it a bug?

 // alpha layer (texture splatmap) rotation
 dw = terrain.terrainData.alphamapWidth;
 dh = terrain.terrainData.alphamapHeight;
 int dz = terrain.terrainData.alphamapLayers;
 float alphaMiddle = (terrain.terrainData.alphamapResolution) / 2.0f; // pivot at middle
 float[,,] newAlphaMap = new float[dw, dh, dz];
 
 for (int z = 0; z < dz; z++)
 {
     for (int y = 0; y < dh; y++)
     {
         for (int x = 0; x < dw; x++)
         {
             cs = Mathf.Cos(angleRad);
             sn = Mathf.Sin(angleRad);
 
             nx = (int)((x - alphaMiddle) * cs - (y - alphaMiddle) * sn + alphaMiddle);
             ny = (int)((x - alphaMiddle) * sn + (y - alphaMiddle) * cs + alphaMiddle);
 
             if (nx < 0) nx = 0;
             if (nx > dw - 1) nx = dw - 1;
             if (ny < 0) ny = 0;
             if (ny > dh - 1) ny = dh - 1;
 
             newAlphaMap[x, y, z] = origAlphaMap[nx, ny, z];
         } // for x
     } // for y
 } // for z



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 Menyus777 · Aug 10, 2017 at 11:13 PM

Modifying TerrainData at runtime via script cannot be undone except with temp saves, your terrain fault is the splatmap, probably u are using more then one splat map because you use more then 4 textures for your terrain 1 splatmap can hold 4 textures, u sure u are roating all of them? Also when i tried roating my splatmap i member that the result was not accurate so i find another solution. And damn bro Terrain rotation is not that top secret ;P

Edit:

 GetAlphamaps(int x, int y, int width, int height);

Use this to access all the splat maps https://docs.unity3d.com/ScriptReference/TerrainData.GetAlphamaps.html

Comment
Add comment · Show 2 · 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 Eldoir · Aug 14, 2017 at 08:40 AM 0
Share

Thanks for that :) But if you look at the code I posted below, by getting the 'dz' variable, I'm already going through all splatmaps with my for loop on 'z' variable, so it should work, but apparently it does not.

avatar image Menyus777 · Aug 14, 2017 at 01:24 PM 0
Share

I have no idea buddy then, i read your code(sorry for not doing it first) , i suggest you to make a test project witha terrain with only 2 textures and check if this code rotates it perfectly, if yes your prblem is with the number of splatmaps, if not something with your algorythm

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

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

Related Questions

Terrain Layers cannot be populated at the same time as the splats 3 Answers

How to convert splat map to regular ol' map of terrain 0 Answers

[SetAlphaMaps] The Map is not aligned with the SplatAlpha? 0 Answers

Unity Terrain Missing Terrain Data! 2 Answers

how to set the SetAlphamaps to one certain texture? 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