Unity Products:Amplify Shader Editor/Terrain Shaders
A standard unity terrain is a regular surface shader with a few fixed requirements such as splat and control splat names, along with a few tags and render parameters. Creating terrains with Amplify Shader Editor is really quick and easy, follow the instructions below and you will be taking advantage of our editor in no time.
As with any other ASE shaders, right-click on the Project view, select Create > Amplify Shader > Surface Shader and name it as you like. We advise you to have 'First-Pass' included on the shader name since it performs what is called a first pass over the terrain.
Set the Queue Index to -100 on the Output Node Properties General tab.
By default a created shader already comes with the Opaque shader type selected, which is required by the terrain, but please go to the Blend Mode tab and check that Opaque is the selected option.
Specify Base Shader
A Base shader must be specified. This shader will be responsible to render the terrain when over a certain distance specified at the Base Map Distance on the terrain settings. Beyond this distance, a lower resolution composite image will be used with this shader for efficiency reasons.
We already have a base shader created and ready to be used, so you only need to:
1. Go to your Dependencies Tab on the Output Node Properties
2. Click on the Add Button to add a new entry
3. Write BaseMapShader into the Name textfield
4. Write ASESampleShaders/SimpleTerrainBase on the Value textfield
Feel free to open the ASESampleShaders/SimpleTerrainBase shader located at AmplifyShaderEditor > Examples > Official > SimpleTerrain > SimpleTerrainBase.shader and check what is being done. Please note that property naming in the base shader is very strict and property names must be maintained.
Specify Splat Count
This step can be skipped on latest Unity versions (Unity 2018.3 and above)
ASE supports multiple splats but its maximum amount needs to be specified into the shader through a custom tag. In order to do that:
1. Go to Custom SubShader Tags tab on the Output Node Properties
2. Click on the Add Button to add a new entry
3. Write SplatCount into the Name textfield
4. Write 4 into the Value textfield
This is the meat of how the terrain shader will behave. We already have a shader function, Four Splats First Pass Terrain, which replicates Unity standard terrain and should be used as starting point on creating your own shader.
Each splat can be accessed via their properties, having yet again strict naming to be maintained. The splat diffuse colors are accessed through the sampler variables _Splat0 through _Splat3, so in your shader you must create four Texture Sampler nodes with the names Splat 0 through Splat 3 as the _ is automatically placed and the empty spaces are also automatically removed from the created variable name.
The same goes to normal maps, they are accessed through the sampler variables _Normal0 through _Normal3 so, like with diffuse, in your shader you must create four Texture Sampler nodes with the names Normal 0 through Normal 3. One detail to take into account is that Unpack Normal Map should be turned off for each one of the nodes and an Unpack Scale Normal node should be applied after their values are combined.
Smoothness + Metallic
Smoothness and Metallic values must also have specific variable names, _Smoothness0 through _Smoothness3 and _Metallic0 through _Metallic3 respectively.
These variables however are Float properties, so to access them you need to create Float nodes, set them to Properties with the names Smoothness 0 through Smoothness 3 and Metallic 0 through Metallic 3 respectively.
To maintain consistency with the Unity terrain shader, you should add the Gamma attribute to the Metallic property nodes.
The weight of each layer is given by yet another sampler variable. The _Control sampler variable contains, in each of its channels, the contribution each layer/splat have on the final result. To get access to it, simply create a Texture Sampler node in your shader with its name being Control. This Control texture is dynamically created when you paint your terrain with the Terrain tools and can be inspected by selecting the terrain asset on the Project view.
Again, we invite you to check the Four Splats First Pass Terrain shader function located at AmplifyShaderEditor > Plugins > EditorResources > ShaderFunctions > Four Splats First Pass Terrain for a more in-depth view on how the Control texture is used.
Support for 4+ Splat Maps
We've introduced support for over 4 Splat Maps through the SimpleTerrainAddPass shader, which is registered in the main shader - SimpleTerrainFirstPass - as a dependency, and is used to add any additional pass that is set in the main shader's SplatCount Tag. As a further example, the shader function we provide may only use 4 splats, so if you have 9 splats, you'll be using the First Pass and the Add Pass will be called twice ( 4 + 4 + 1 ).
Please note that this Tag is only necessary for Unity 2018.2 and below, if you're using Unity 2018.3+ you'll have to remove this Tag in order for the shader to work as expected.
Starting from Unity 2018.3, terrains can be GPU instanced.
If the terrain is set to be GPU instanced ( option Draw Instanced turned on over Terrain Settings ) then each one of its shaders must be prepared for it.
ASE already supports this option via the Instanced Terrain over the Master Node properties panel.
Please note that, not only the First Pass shader, but also all its dependencies ( Base and Add Pass ) MUST be compiled with this option turned on.