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
1
Question by scarpelius · Oct 21, 2011 at 08:10 AM · terrainsplatmapalphamap

Update terrain alphamap at runtime makes my game crawling

Basically what i want to do is to paint roads on terrain as my NPCs are moving. I manage to get this done, but when using

 terrain.terrainData.SetAlphamaps(0, 0, splatmapData);

in conjunction with FixedUpdate or Update, the frame rate is going down to a crawling slide-show. I've tried to move it to a coroutine but it doesnt make any difference, i also tried to edit only one tile on the alphamap but with no success. You can see the code here http://forum.unity3d.com/threads/108885-Modify-terrain-texture-at-runtime

P.S. I did an extensively search on this subject but couldn't find anything related to my problem.

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 acaton · May 10, 2015 at 10:22 PM 0
Share

I'm having the same problem, but only with Unity 5! In unity 4, I could change alphamaps (SetAlphamap) in small patches over several different terrain 'chunks' with little slowdown, but now in Unity 5, if I change two 4x4 terrain alphamaps per frame, then game drops from 60fps to 30!!! Is there by chance a different way to change terrain that I'm missing, or is Unity 'baking' the terrain with each change or something? BTW, my terrain has 8 textures and I'm using the 'cutout' mode in my shader so my 1st terrain can have 'holes', if any of that makes any difference.

Any help would be very much appreciated. I've tried updating to the latest version of Unity (today), and searched forums for at least 4-5 hours so not sure what to do next. Thanks again and have a great day!

3 Replies

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

Answer by aldonaletto · Oct 21, 2011 at 11:44 AM

The terrain docs are too vague - no examples, no detailed explanations - but what I understood from them is that the dimensions of the array passed to SetAlphamaps(x, y, arrayOfFloats) define the size of the region to paint. If your array is too large, it will take a big time to update the terrain. Try the following: create a single element array, copy the new data to it (update your splatmapData array also, if you want) and pass the single terrain element to SetAlphamaps - something like this:

    // I'm a JS guy, thus the code below may have horrible C# errors
    float[,,] element = new float[1,1,2]; // create a temp 1x1x2 array
    splatmapData[y, x, 0] = element[0, 0, 0] = 0; // set the element and
    splatmapData[y, x, 1] = element[0, 0, 1] = 1; // update splatmapData
    terrain.terrainData.SetAlphamaps(y, x, element); // update only the selected terrain element
    ...
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 PixelFireXY · Dec 03, 2015 at 04:30 PM 0
Share

I still drop frames, and I don't understand why, can you please help me where am I wrong? (Please ignore all dirty code, is just for test...). This script is attached on gameobject that walk on myTerrain.

     float[,,] element;
     int mapX, mapY;
     TerrainData terrainData;
     Vector3 terrainPosition;
     public Terrain myTerrain;
     float[,,] map;
     private Vector3 lastPos;

 void Awake()
     {
         map = new float[myTerrain.terrainData.alphamapWidth, myTerrain.terrainData.alphamapHeight, myTerrain.terrainData.alphamapLayers];
 
         element = new float[1, 1, myTerrain.terrainData.alphamapLayers];
         terrainData = myTerrain.terrainData;
         terrainPosition = myTerrain.transform.position;
 
         lastPos = transform.position;
     }

   void Update()
     {
         Update$$anonymous$$apOnTheTarget();
     }
 
     void Update$$anonymous$$apOnTheTarget()
     {
 //just update if you move
         if(Vector3.Distance(transform.position, lastPos) > 1)
         {
             print("paint");
 //convert world coords to terrain coords
             mapX = (int)(((transform.position.x - terrainPosition.x) / terrainData.size.x) * terrainData.alphamapWidth);
             mapY = (int)(((transform.position.z - terrainPosition.z) / terrainData.size.z) * terrainData.alphamapHeight);
 
             map[mapY, mapX, 0] = element[0, 0, 0] = 0;
             map[mapY, mapX, 1] = element[0, 0, 1] = 1;
 
             myTerrain.terrainData.SetAlphamaps(mapX, mapY, element);
 
             lastPos = transform.position;
         }
     }




avatar image julkiewicz PixelFireXY · Nov 13, 2016 at 01:25 PM 0
Share

I wasn't changing alpha maps. However I was playing with heightmap and that was similarly causing frame drops. In that case frame drops were cause by the fact that unity see$$anonymous$$gly needs to recompute some terrain collider data.

In case of alpha maps I'd guess there is some similar postprocessing involved. For instance I know there's the question of recomputing LODs. That operation i think is quite costly.

avatar image julkiewicz julkiewicz · Nov 13, 2016 at 01:27 PM 0
Share

In general it seems terrain component in Unity hasn't really been designed for runtime modification. For that I'm planning to look into some third-party assets in the asset store.

avatar image
0

Answer by scarpelius · Oct 21, 2011 at 12:04 PM

You my friend are a genius :D I've tried something similar last night, but it seems i was too tired to notice that i created a

 float[,,] element = new float[1,1,2]; 

with too many elements, I used float[,,] element = new float[x,y,2]; which in my case x and y was around 300 so there was no difference :)

Cheers mate.

Comment
Add comment · Show 4 · 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 aldonaletto · Oct 21, 2011 at 07:29 PM 0
Share

@scarpelius, please click the "check" button below the voting thumbs in my answer to mark it as accepted. It renders no karma to me (unfortunately), but may help some desperate programmers with crawling terrain scripts to find the solution. PS: please, use the $$anonymous$$uscule add new comment to reply or comment; the your answer box must be used only to answer the question (answers are counted in Unity Answers).

avatar image scarpelius · Oct 21, 2011 at 07:36 PM 2
Share

This system is unnecessary complicated.

avatar image aldonaletto · Oct 21, 2011 at 11:11 PM 0
Share

Yeah, I agree - it could be improved a lot! The title Your answer could be changed to Use this box only to answer the question, add new comment should be larger, but above all: this moderation thing should be replaced by Captcha or other anti-spam measure!

avatar image MrScrumbles001 · Nov 19, 2013 at 04:32 AM 1
Share

hey guys!

is there any way I can see the whole script so I can get a better understanding of what is going on?

I am trying to find a tutorial on this but with no luck. cheers

avatar image
0

Answer by farooq03245149691 · Feb 16, 2017 at 01:37 PM

@penna91 Im using this code in a project im developing. But I get frame drops if i want to paint a larger area. I do it by duplicating the objects to which this script is attached to.This results in a huge drop in frames. From 40Fps to 6Fps. Can you tell me how to increase the area that im trying to paint?? So that this script is attached to a single object.

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

8 People are following this question.

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

Related Questions

how does a splatmap alpha maps work (terrains) etc... 1 Answer

Splat Map with transition texture? 0 Answers

Generated terrain heightmap is flipped from the alphamap. 1 Answer

Can you change terrain material? 4 Answers

Splatmap dry/healthy 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