- Home /
How can I enable GPU instancing on terrain details?
I have tried to follow this manual entry: https://docs.unity3d.com/Manual/GPUInstancing.html
But there isn't a user accessible terrain detail material available to check the "enable GPU instancing checkbox". I also tried to edit the "Hidden/TerrainEngine/Details/Vertexlit" to add GPU instancing directives based on this example from the manual:
Shader "SimplestInstancedShader"
{
Properties
{
_Color ("Color", Color) = (1, 1, 1, 1)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID // necessary only if you want to access instanced properties in fragment Shader.
};
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(float4, _Color)
UNITY_INSTANCING_BUFFER_END(Props)
v2f vert(appdata v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_TRANSFER_INSTANCE_ID(v, o); // necessary only if you want to access instanced properties in the fragment Shader.
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(i); // necessary only if any instanced properties are going to be accessed in the fragment Shader.
return UNITY_ACCESS_INSTANCED_PROP(Props, _Color);
}
ENDCG
}
}
}
But that doesn't seem to work. I have googled extensively on instancing terrain detail, but only a commercial asset surfaces. I'm surprised that the default terrain system doesn't promote instancing details, as they are ideal candidates for this optimization.
I have a lot of grass on my terrain, any help is greatly appreciated.
GPU instancing is tricky, and some shaders show it, while others don't.
I'd suggest editing a Legacy shader that has it, like Legacy/Transparent/Specular.
Thanks for your thought. The real problem is that I can't activate the feature because Unity hides the material used on terrain details. I just found that the relevant native material property is named "EnableInstancingVariants". $$anonymous$$aybe there's a way to access the terrain detail material instance via scripting and turn that flag on.
Are you sure it's not enabled by default? This article claims it is.
Your answer
![](https://koobas.hobune.stream/wayback/20220612212945im_/https://answers.unity.com/themes/thub/images/avi.jpg)