Jump to content

Whether can use TerrainModifier independently to modify the terrain at runtime?


Wuyan

Recommended Posts

Hello,

 

I noticed that the TerrainModifier is used to modify terrain in both GeNaTerrainDecoratorclas and Spline. Such as clear details, raise or lower terrain. In my game, player will be able to modify the terrain. For example, player can use a shovel to clear grass or dig. So can I call some methods in Gena to do this?  What steps should be followed to achieve this?

 

Frankly, I spent some time reading the source code and testing, but I haven't found clue yet.

 

Thanks!

Link to comment
Share on other sites

13 hours ago, Manny said:

Hey there @Wuyan!

You are indeed correct in saying the TerrainModifier is the class that controls the terrain modifications. We are constantly fleshing out the API so in future patches, this will become easier to use. However, in the current version, it is possible to modify the terrain at Runtime.

We've actually had a few questions about this in the past whereby, users wanted to modify the terrain at runtime. Just as a bit of advice, I would recommend copying the terrain data when the game starts (maybe as part of the loading screen) and making edits to the terrain afterward. The reason is that if you modify the terrain at Runtime, it gets persisted since the Terrain Data acts as a ScriptableObject.

There is an internal class that needs to be utilized in order for the Terrain modifier to work and that's called 'TerrainTools' it's actually part of the Game Manager and can get accessed via 'GameManager.GetInstance().TerrainTools'. The way the Terrain Modification system works is you create a 'TerrainModifier', you then pass it through the TerrainTool system and it generates a Terrain Entity that can be used over and over for modifying the terrain. 
image.png

That being said, the TerrainModifer has a method inside of it called 'GenerateTerrainEntity' that does this process for you and gives you back a Terrain Entity straight away. 

I have attached a full example of the TerrainModification system in action as a Unity Package. You can open the script and see this in action:
image.png

Import it into your project with GeNa and you can play around with the script. Let me know how it goes! 🙂

TerrainModifierExample.unitypackage 25.68 kB · 1 download

 

Hi Manny,  Thank you so much!  It works well!

Link to comment
Share on other sites

  • 2 weeks later...
14 hours ago, Manny said:

Could you show me the code you wrote to get it running at runtime? Thanks.

Hi @Manny,  I build and test the TerrainModifierDemo scene you sent to me, only the following lines been added for testing.

if (Input.GetKeyDown(KeyCode.H))
            {
                m_perform = true;
            }

I use development build and got the follow errors. I don't know which shader should be included.

ArgumentNullException: Value cannot be null.
Parameter name: shader
  at (wrapper managed-to-native) UnityEngine.Material.CreateWithShader(UnityEngine.Material,UnityEngine.Shader)
  at UnityEngine.Material..ctor (UnityEngine.Shader shader) [0x00008] in C:\buildslave\unity\build\Runtime\Export\Shaders\Material.bindings.cs:18 
  at UnityEngine.TerrainTools.TerrainPaintUtility.GetBlitMaterial () [0x00012] in C:\buildslave\unity\build\Modules\Terrain\Public\TerrainPaintUtility.cs:247 
  at GeNa.Core.GeNaMultiTerrainOperation.GetHeightmap () [0x0010f] in <4b897aaebe704301be127fb1ea57a331>:0 
  at GeNa.Core.TerrainTools.GetTerrainEntity (GeNa.Core.TerrainModifier terrainModifier) [0x0005f] in <4b897aaebe704301be127fb1ea57a331>:0 
  at GeNa.Core.TerrainTools.GenerateTerrainEntity (GeNa.Core.TerrainModifier terrainModifier) [0x00056] in <4b897aaebe704301be127fb1ea57a331>:0 
  at GeNa.Core.TerrainModifier.GenerateTerrainEntity () [0x00023] in <4b897aaebe704301be127fb1ea57a331>:0 
  at GeNa.Core.WYTerrainModifier.PerformModification () [0x00034] in C:\UnityProjects\The Mortal Body (HDRP)\Assets\Wuyan\scripts\Gaia\WYTerrainModifier.cs:77 ()
(m_terrainModifier.GenerateTerrainEntity();)
  at GeNa.Core.WYTerrainModifier.Update () [0x0005f] in C:\UnityProjects\The Mortal Body (HDRP)\Assets\Wuyan\scripts\Gaia\WYTerrainModifier.cs:61 

Link to comment
Share on other sites

2 hours ago, Manny said:

Thanks for sharing the error messages and investigating the issue further with me. 

After a long investigation into the issue, it appears that for some reason, Unity's internal shader we're using for blitting is missing at Runtime. We use this shader to generate textures of the Terrain heights at runtime for modification and typically this works. But depending on the Unity version, they could have changed this behaviour.
image.png

 

However, in the meantime, you could try to get it to work by importing the attached "BlitCopy" Shader into your project and placing it into a folder explicitly named "Resources" so that it gets shipped with your build. 
BlitCopy.shader


Let me know if that works for you! And if it does, we'll go ahead and ship the new GeNa version with a runtime compatible version of the shader.

Hi Manny, It still doesn't work.   unity 2021.2.14  HDRP

Link to comment
Share on other sites

35 minutes ago, Manny said:

Hmm, okay. I'll try with HDRP 2021.2.14.

Hi Manny, I did it. I made the following changes.

1,The shader you sent me:  Change  "Hidden/BlitCopy"   to   "Hidden/TerrainEngine/TerrainBlitCopyZWrite"

image.png.cebe801e8209194faf4607b966566cdb.png

2, Add  PW_GreyScale to Always include.

image.png.96bb4e363a7dac578a11e0c9757fa775.png

 

But Can't Undo in build. Please test!

 

Link to comment
Share on other sites

58 minutes ago, Manny said:

Ahh okay, I think I know what caused this now. 
Along with the BlitCopy Shader I gave you, there is also another shader that needs to be put in a Resources folder.

Try moving the "PW_GreyScale" Shader into the Resources folder along with BlitCopy.

Get the PW_GreyScale file from Procedural Worlds/GeNa/Shaders and place it in the folder ProceduralWorlds/GeNa/Resources/Shaders.image.png

I just tested it in HDRP and it seemed to have worked.

Let me know if this works and I'll include all of this into the newest patch. 😁

Hi @Manny, I test again. I'm sure that my first step above is necessary, the shader you sent me must be changed.   

In addition, please test the undo in build.

 

 

Link to comment
Share on other sites

7 hours ago, Manny said:

As for the Undo part, I am actually releasing a new Undo API setup that allows you to Undo + Redo any operations you've recorded through the GeNaUndoRedo class. 

So far it's working really well, it will be released in v3.3.17 of GeNa (coming shortly). I'll let you know when that is and how to use it.

For now, if you could verify that the Runtime parts are working, I can get a release coming sooner.  😁

Hi Manny, I haven't found any more problems at present. 

Two more questions.

1. You suggested to copy terrainData when start game. I use Gaia pro multi terrains. How can I make Gaia Pro terrain loader load new terrainData.

2. The terrain modified will not be saved in build . Is there a method to save new terrain data? Or how to get the modified terrains. For example, if the terrain modifier is at the seam of several terrains, it may modify several terrains at one time. How to get the modified terrains?

Thank you!

Link to comment
Share on other sites

6 hours ago, Wuyan said:

You suggested to copy terrainData when start game. I use Gaia pro multi terrains. How can I make Gaia Pro terrain loader load new terrainData.

You can ask Gaia questions in the Gaia forum.

6 hours ago, Wuyan said:

The terrain modified will not be saved in build . Is there a method to save new terrain data? Or how to get the modified terrains. For example, if the terrain modifier is at the seam of several terrains, it may modify several terrains at one time. How to get the modified terrains?

As you are the one modifying the terrains then you will need to make an assessment of what terrains have been modified. You would also need to provide your own solution to saving and loading these changes, as this is beyond the feature set that is currently offered by GeNa Pro.

Link to comment
Share on other sites

14 hours ago, Adam said:

You can ask Gaia questions in the Gaia forum.

As you are the one modifying the terrains then you will need to make an assessment of what terrains have been modified. You would also need to provide your own solution to saving and loading these changes, as this is beyond the feature set that is currently offered by GeNa Pro.

Hi Adam,Thank you.  

Maybe you don't understand what I mean. Because I use the terrain modifier of Gena to modify the terrain. About the multi terrain problem, Gena has calculated which terrains should be modified (I think calculated in GenaMultiTerrainOperation).  Because I can't see the source code, so I want to know if I can directly get Gena's calculation result.
 
But the good news is that I seem to have found the answer.The following threes can be used to get the modified terrains.

TerrainEntity. Operation. affectedHeightmapData;
TerrainEntity. Operation. GetAllAffectedTerrains();
TerrainEntity. Operation. affectedSplatmapData;

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Tell a friend

    Love Canopy - Procedural Worlds? Tell a friend!
  • Need help?

    We work with some of the biggest brands in global gaming, automotive, technology, and government to create environments, games, simulations, and product launches for desktop, mobile, and VR.

    Our unique expertise and technology enable us to deliver solutions that look and run better at a fraction of the time and cost of a typical project.

    Check out some of our non-NDA work in the Gallery, and then Contact Us to accelerate your next project!

×
×
  • Create New...