- Home /
Use built in terrain shader for other objects
I am creating a game where I need to replace a small part of a terrain with a plane object in order to manipulate it as I want, I use stencil shaders to not render the terrain object where one of these "tiles" are.
There are two obvious problems, first the shading needs to be exactly the same, the terrain uses the "Nature/Terrain/Standard" shader and I could just use the same shader but the problem is that this shader uses properties which are set by the terrain engine (see the properties box of the "Nature/Terrain/Standard" shader below).
Properties {
// set by terrain engine
_Control ("Control (RGBA)", 2D) = "red" {}
_Splat3 ("Layer 3 (A)", 2D) = "white" {}
_Splat2 ("Layer 2 (B)", 2D) = "white" {}
_Splat1 ("Layer 1 (G)", 2D) = "white" {}
_Splat0 ("Layer 0 (R)", 2D) = "white" {}
_Normal3 ("Normal 3 (A)", 2D) = "bump" {}
_Normal2 ("Normal 2 (B)", 2D) = "bump" {}
_Normal1 ("Normal 1 (G)", 2D) = "bump" {}
_Normal0 ("Normal 0 (R)", 2D) = "bump" {}
[Gamma] _Metallic0 ("Metallic 0", Range(0.0, 1.0)) = 0.0
[Gamma] _Metallic1 ("Metallic 1", Range(0.0, 1.0)) = 0.0
[Gamma] _Metallic2 ("Metallic 2", Range(0.0, 1.0)) = 0.0
[Gamma] _Metallic3 ("Metallic 3", Range(0.0, 1.0)) = 0.0
_Smoothness0 ("Smoothness 0", Range(0.0, 1.0)) = 1.0
_Smoothness1 ("Smoothness 1", Range(0.0, 1.0)) = 1.0
_Smoothness2 ("Smoothness 2", Range(0.0, 1.0)) = 1.0
_Smoothness3 ("Smoothness 3", Range(0.0, 1.0)) = 1.0
// used in fallback on old cards & base map
_MainTex ("BaseMap (RGB)", 2D) = "white" {}
_Color ("Main Color", Color) = (1,1,1,1)
}
When I try to just use this shader on my tiles it doesn't work because these properties are just the default values and not what is set by the terrain engine. One solution would be to use material.getXXX but it seems impossible to get a reference to the built in material the terrain is using. Any ideas how it can be done?
Secondly I need to get the UV coordinates for each of the splat and the control texture when I replace part of the terrain. How can this be done?
I know this is possible since it is done in the terrain slicing and dynamic loading kit but I have no idea how he's doing it.
Ok, getting the metallic, splats, normal and smoothness variables was easy. terrain.terrainData.splatPrototypes[X] contains all that information. I still don't know how to get the control texture and the uv coordinates.
Your answer
Follow this Question
Related Questions
Custom Sun terrain shader 0 Answers
How to change Terrain Shaders at URP 10.4.0 1 Answer
Toon Shading on Terrain Unity 3.4 0 Answers