I wanted to convert my Terrain Shader to work with Entities. I’ve read the Unity Manual on DOTS instancing shaders for 6.0 Preview. I am not trying to use BatchRendererGroup and am instead defining the properties using UNITY_DOTS_INSTANCED_PROP. I don’t really want a DOTS instancing shader, but a shader that works on Entities.
Here is the shader. Am I doing something wrong? Should I use BatchRendererGroup or is it ok to define the properties just within the shader?
Shader "Custom/Terrain2"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_GradientColor1 ("Gradient Color 1", Color) = (0, 0, 1, 1) // Blue
_GradientColor2 ("Gradient Color 2", Color) = (0, 1, 0, 1) // Green
_GradientColor3 ("Gradient Color 3", Color) = (1, 1, 0, 1) // Yellow
_GradientColor4 ("Gradient Color 4", Color) = (1, 0, 0, 1) // Red
_Height1 ("Height 1", Range(-256, 1024)) = 85
_Height2 ("Height 2", Range(0, 1024)) = 170
_Height3 ("Height 3", Range(0, 1024)) = 700
_Height4 ("Height 4", Range(0, 1024)) = 1024
_DetailTex1 ("Detail Texture 1", 2D) = "white" {}
_DetailTex2 ("Detail Texture 2", 2D) = "white" {}
_DetailTex3 ("Detail Texture 3", 2D) = "white" {}
_DetailTex4 ("Detail Texture 4", 2D) = "white" {}
_NoiseTex1 ("Noise Texture1", 2D) = "white" {} // New noise texture
_NoiseTex2 ("Noise Texture2", 2D) = "white" {} // New noise texture
_NoiseScale1 ("Noise Scale1", Range(0.1, 10.0)) = 1.0 // Noise scale control
_NoiseScale2 ("Noise Scale2", Range(0.1, 100.0)) = 1.0 // Noise scale control
_NoiseInfluence1 ("Noise Influence1", Range(0, 1)) = 0.5
_NoiseInfluence2 ("Noise Influence2", Range(0, 1)) = 0.5
_ColorFog ("Fog Color", Color) = (0.5, 0.5, 0.5, 1)
_FogStartDistance ("Fog Start Distance", Range(0, 20000)) = 100
_FogEndDistance ("Fog End Distance", Range(0, 20000)) = 300
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#pragma target 4.5
#pragma instancing_options renderinglayer
#pragma multi_compile _ DOTS_INSTANCING_ON
#include "UnityCG.cginc"
CBUFFER_START(UnityPerMaterial)
half4 _Color;
half _Glossiness;
half _Metallic;
float4 _GradientColor1;
float4 _GradientColor2;
float4 _GradientColor3;
float4 _GradientColor4;
float _Height1;
float _Height2;
float _Height3;
float _Height4;
float _FogStartDistance;
float _FogEndDistance;
float4 _ColorFog;
float _NoiseScale1;
float _NoiseScale2;
float _NoiseInfluence1;
float _NoiseInfluence2;
CBUFFER_END
// UNITY_INSTANCING_BUFFER_START(Props)
// UNITY_DEFINE_INSTANCED_PROP(float4, _Color_arr)
// UNITY_DEFINE_INSTANCED_PROP(half, _Glossiness_arr)
// UNITY_DEFINE_INSTANCED_PROP(half, _Metallic_arr)
// UNITY_DEFINE_INSTANCED_PROP(float4, _GradientColor1_arr)
// UNITY_DEFINE_INSTANCED_PROP(float4, _GradientColor2_arr)
// UNITY_DEFINE_INSTANCED_PROP(float4, _GradientColor3_arr)
// UNITY_DEFINE_INSTANCED_PROP(float4, _GradientColor4_arr)
// UNITY_DEFINE_INSTANCED_PROP(float, _Height1_arr)
// UNITY_DEFINE_INSTANCED_PROP(float, _Height2_arr)
// UNITY_DEFINE_INSTANCED_PROP(float, _Height3_arr)
// UNITY_DEFINE_INSTANCED_PROP(float, _Height4_arr)
// UNITY_DEFINE_INSTANCED_PROP(float4, _ColorFog_arr)
// UNITY_DEFINE_INSTANCED_PROP(float, _FogDensity_arr)
// UNITY_DEFINE_INSTANCED_PROP(float, _FogStartDistance_arr)
// UNITY_DEFINE_INSTANCED_PROP(float, _FogEndDistance_arr)
// UNITY_DEFINE_INSTANCED_PROP(float, _NoiseScale1_arr)
// UNITY_DEFINE_INSTANCED_PROP(float, _NoiseScale2_arr)
// UNITY_DEFINE_INSTANCED_PROP(float, _NoiseInfluence1_arr)
// UNITY_DEFINE_INSTANCED_PROP(float, _NoiseInfluence2_arr)
// UNITY_INSTANCING_BUFFER_END(Props)
#if defined(UNITY_DOTS_INSTANCING_ENABLED)
UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata)
UNITY_DOTS_INSTANCED_PROP(float4, _Color)
UNITY_DOTS_INSTANCED_PROP(half, _Glossiness)
UNITY_DOTS_INSTANCED_PROP(half, _Metallic)
UNITY_DOTS_INSTANCED_PROP(float4, _GradientColor1)
UNITY_DOTS_INSTANCED_PROP(float4, _GradientColor2)
UNITY_DOTS_INSTANCED_PROP(float4, _GradientColor3)
UNITY_DOTS_INSTANCED_PROP(float4, _GradientColor4)
UNITY_DOTS_INSTANCED_PROP(float, _Height1)
UNITY_DOTS_INSTANCED_PROP(float, _Height2)
UNITY_DOTS_INSTANCED_PROP(float, _Height3)
UNITY_DOTS_INSTANCED_PROP(float, _Height4)
UNITY_DOTS_INSTANCED_PROP(float4, _ColorFog)
UNITY_DOTS_INSTANCED_PROP(float, _FogStartDistance)
UNITY_DOTS_INSTANCED_PROP(float, _FogEndDistance)
UNITY_DOTS_INSTANCED_PROP(float, _NoiseScale1)
UNITY_DOTS_INSTANCED_PROP(float, _NoiseScale2)
UNITY_DOTS_INSTANCED_PROP(float, _NoiseInfluence1)
UNITY_DOTS_INSTANCED_PROP(float, _NoiseInfluence2)
UNITY_DOTS_INSTANCED_PROP(sampler2D, _MainTex)
UNITY_DOTS_INSTANCED_PROP(sampler2D, _DetailTex1)
UNITY_DOTS_INSTANCED_PROP(sampler2D, _DetailTex2)
UNITY_DOTS_INSTANCED_PROP(sampler2D, _DetailTex3)
UNITY_DOTS_INSTANCED_PROP(sampler2D, _DetailTex4)
UNITY_DOTS_INSTANCED_PROP(sampler2D, _NoiseTex1)
UNITY_DOTS_INSTANCED_PROP(sampler2D, _NoiseTex2)
UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata)
#endif
sampler2D _MainTex;
sampler2D _DetailTex1;
sampler2D _DetailTex2;
sampler2D _DetailTex3;
sampler2D _DetailTex4;
sampler2D _NoiseTex1;
sampler2D _NoiseTex2;
struct appdata_t
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float height : TEXCOORD1;
float3 normal : NORMAL;
float3 worldPos : TEXCOORD2;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
v2f vert(appdata_t v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_TRANSFER_INSTANCE_ID(v, o);
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.height = v.vertex.y;
float3 normal = float3(0.0, 1.0, 0.0);
o.normal = normal;
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(i);
#if defined(UNITY_DOTS_INSTANCING_ENABLED)
float noiseValue1 = tex2D(UNITY_ACCESS_DOTS_INSTANCED_PROP(sampler2D, _NoiseTex1), i.uv * UNITY_ACCESS_DOTS_INSTANCED_PROP(float, _NoiseScale1)).r;
float noiseValue2 = tex2D(UNITY_ACCESS_DOTS_INSTANCED_PROP(sampler2D, _NoiseTex2), i.uv * UNITY_ACCESS_DOTS_INSTANCED_PROP(float, _NoiseScale2)).r;
float modifiedHeight = i.height + (noiseValue1 * UNITY_ACCESS_DOTS_INSTANCED_PROP(float, NoiseInfluence1) * 100) + (noiseValue2 * UNITY_ACCESS_DOTS_INSTANCED_PROP(float, NoiseInfluence2) * 100);
float3 gradientColor;
fixed4 detailColor;
float height1 = UNITY_ACCESS_DOTS_INSTANCED_PROP(float _Height1);
float height2 = UNITY_ACCESS_DOTS_INSTANCED_PROP(float _Height2);
float height3 = UNITY_ACCESS_DOTS_INSTANCED_PROP(float _Height3);
float height4 = UNITY_ACCESS_DOTS_INSTANCED_PROP(float _Height4);
float4 gradientColor1 = UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, _GradientColor1);
float4 gradientColor2 = UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, _GradientColor2);
float4 gradientColor3 = UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, _GradientColor3);
float4 gradientColor4 = UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, _GradientColor4);
if (modifiedHeight < height1)
{
gradientColor = _GradientColor1.rgb;
detailColor = tex2D(UNITY_ACCESS_DOTS_INSTANCED_PROP(sampler2D, _DetailTex1), i.uv);
}
else if (modifiedHeight < height2)
{
gradientColor = lerp(gradientColor1.rgb, gradientColor2.rgb, (modifiedHeight - height1) / (height2 - height1));
detailColor = tex2D(UNITY_ACCESS_DOTS_INSTANCED_PROP(sampler2D, _DetailTex2), i.uv);
}
else if (modifiedHeight < height3)
{
gradientColor = lerp(gradientColor2.rgb, gradientColor3.rgb, (modifiedHeight - height2) / (height3 - height2));
detailColor = tex2D(UNITY_ACCESS_DOTS_INSTANCED_PROP(sampler2D, _DetailTex3), i.uv);
}
else if (modifiedHeight < height4)
{
gradientColor = lerp(gradientColor3.rgb, gradientColor4.rgb, (modifiedHeight - height3) / (height4 - height3));
detailColor = tex2D(UNITY_ACCESS_DOTS_INSTANCED_PROP(sampler2D, _DetailTex4), i.uv);
}
else
{
gradientColor = gradientColor4.rgb;
detailColor = tex2D(UNITY_ACCESS_DOTS_INSTANCED_PROP(sampler2D, _DetailTex4), i.uv);
}
float3 lightDir = normalize(float3(1, 1, -1));
float lightIntensity = max(dot(i.normal, lightDir), 0.0);
float fogDistance = length(i.worldPos - _WorldSpaceCameraPos);
float fogFactor = saturate((fogDistance - UNITY_ACCESS_DOTS_INSTANCED_PROP(float, _FogStartDistance)) / (UNITY_ACCESS_DOTS_INSTANCED_PROP(float, _FogEndDistance) - UNITY_ACCESS_DOTS_INSTANCED_PROP(float, _FogStartDistance)));
fixed4 baseColor = tex2D(UNITY_ACCESS_DOTS_INSTANCED_PROP(sampler2D, _MainTex), i.uv) * fixed4(gradientColor, 1.0) * UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, _Color) * detailColor * lightIntensity;
fixed4 fogColor = fixed4(UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, _ColorFog).rgb, 1.0);
return lerp(baseColor, fogColor, fogFactor);
#endif
return (1,1,1,1);
}
ENDCG
}
}
FallBack "Diffuse"
}
The Exeption:
Trying to render a BatchRendererGroup (or Entities Graphics) batch with wrong cbuffer setup. Missing DOTS_INSTANCING_ON variant?
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)