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
4
Question by zharramadar · Jul 18, 2011 at 05:19 AM · terrainsplatmapedit mode

Terrain Splatmap problem in Edit Mode

Hello everybody,

I'm creating a mass terrain importer for my project here, and I managed to read a folder and get all the heightmaps on it, create the terrainData, apply the heightmaps to it, add all the splatPrototypes for it, read the splatmap (a grayscale image) for each splat layer and create the float array required for SetAlphaMap method of terrainData. All works pretty nicely, the only problem is, the splatting doesn't get applied. All I get is a terrainData saved in my asset database with all the texture set but the first texture spreading all over the terrain.

I read around that I needed to call the Flush method of the Terrain to apply the changes, but I'm creating no terrain in the scene, I'm creating terrainData objects and saving them in the AssetDatabase, so I can't call Flush.

How can I make my splatmaps set with SetAlphaMap stick to the terrain?

This is the code snippet that creates the splats for a terrain and save it in the AssetDatabase:

 float[, ,] splatarray = null;

         SplatPrototype[] prototypes = null;

         if (splats.Count > 0)

         {

             splatarray = new float[(int)heightmapsize.x, (int)heightmapsize.y, splats.Count];

             prototypes = new SplatPrototype[splats.Count];

         }

         for (int k = 0; k < splats.Count; k++)

         {

             SplatMapInfo splat = splats[k];

             // Load splatmap file

             string splatfile = string.Format(splat.Filemask, xvalue, yvalue);

             string[] splatfiles = System.IO.Directory.GetFiles(imagesPath, splatfile + ".*");

             System.Uri uri = new System.Uri(splatfiles[0]);

             WWW imageloader = new WWW(uri.AbsoluteUri);

             loadImageUrl(imageloader);

             Texture2D t = imageloader.texture;

             //imageloader.LoadImageIntoTexture(t);

             // Parse splat image and populate array

             for (int v = 0; v < heightmapsize.y; v++)

             {

                 for (int u = 0; u < heightmapsize.x; u++)

                 {

                     Color c = t.GetPixel(u, v);

                     splatarray[u, v, k] = c.grayscale;

                 }

             }

             DestroyImmediate(t);

             // Set splat prototype for this splat layer

             SplatPrototype prototype = new SplatPrototype();

             prototype.texture = splat.texture;

             prototype.tileOffset = new Vector2(splat.TileOffsetX, splat.TileOffsetY);

             prototype.tileSize = new Vector2(splat.TileSizeX, splat.TileSizeY);

             prototypes[k] = prototype;

         }

         // If we have splats, assign them

         if (splats.Count > 0)

         {

             tdata.splatPrototypes = prototypes;

             tdata.SetAlphamaps(0, 0, splatarray);

         }

         tdata.RefreshPrototypes();

         // Save this asset to the Asset Database

         string assetname = string.Format(terrainMask, xvalue, yvalue);

         AssetDatabase.CreateAsset(tdata, "Assets/" + assetname + ".asset");
Comment
Add comment · Show 1
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 zharramadar · Jul 18, 2011 at 08:53 PM 0
Share

No one?? Boohoo...

1 Reply

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

Answer by zharramadar · Jul 22, 2011 at 04:31 AM

Okay, I did not get any help, and I understand this is a troublesome area and I can understand if no one knew this at all, or just there are so few who knew it that these ppl never came to see this topic, but I found the solution for this after struggling for 3 days with it.

The problem with it is the AssetDatabase.CreateAsset() method. If you try to completely populate a terrainData object and THEN save it, it'll discard your splatmap on it (it does something very weird I can't explain, but the result is that you get a splatmap that covers your terrain with the 1st texture you set in the splat prototype).

All you have to do is to create your terrainData, populate the heights on it, THEN save it to the AssetDatabase, THEN retrieve another terrainData object from the saved asset with AssetDatabase.LoadAssetAtPath() and then use that loaded terrainData object to populate splatmaps, detail maps and tree instances. After this, you save the AssetDatabase by calling AssetDatabase.SaveAssets(); and then AssetDatabase.Refresh();

So, the code snippet is:

             // Create a new TerrainData object as we'll SetHeight it.
             TerrainData tdata = new TerrainData ();
             tdata.heightmapResolution = 513;
             tdata.size = terrainsize;
             tdata.alphamapResolution = 513;
             tdata.baseMapResolution = 513;
             
             // Generate the heightmap
             tdata.SetHeights (0, 0, heightmapData);
 
 
 
             AssetDatabase.CreateAsset(tdata, "Assets/terrain.asset");
             AssetDatabase.SaveAssets();
 
 
 
             // Now if we have splatmaps for this terrain, time to load them
             float[,,] splatarray = null;
             SplatPrototype[] prototypes = null;
             LoadSplats (0, 0, ref splatarray, ref prototypes);
             
             TerrainData td = (TerrainData)AssetDatabase.LoadAssetAtPath("Assets/terrain.asset", typeof(TerrainData));
 
         
             td.splatPrototypes = prototypes;
             td.RefreshPrototypes ();
             td.SetAlphamaps (0, 0, splatarray);
 
             AssetDatabase.SaveAssets();
             AssetDatabase.Refresh();

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 nerophon · Jan 06, 2014 at 11:47 PM 0
Share

This... is amazing. I'm really glad someone figured this out :)

avatar image T_Strijker · Jan 25, 2018 at 04:04 PM 0
Share

Thx this solution save me a lot of time figuring this out.

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

6 People are following this question.

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

Related Questions

Copying sections of terrain textures 0 Answers

Having issues with terrain splat 0 Answers

How are trees and detail meshes placed on the terrain? 0 Answers

Base map problem when loading terrains from asset bundles in build. 1 Answer

How are trees and detail meshes placed on the terrain? 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