Is there a way to use Instanced Indirect with a “universal render pipeline/lit” shader? So far I only manage to make a simple shader to work with instancing indirect.
or at least modify my simple shader so that it can roughly resemble the lighting characteristics of the “universal render pipeline/lit” shader
Shader "Custom/GPUInstancingShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color", Color) = (1, 1, 1, 1) // Optional color property
_AmbientIntensity ("Ambient Intensity", Range(0, 1)) = 0.2 // Ambient intensity property
_DiffMax ("diff max", Range(0, 1)) = 0.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL; // Add normal data
};
struct v2f {
float2 uv : TEXCOORD0;
float4 pos : SV_POSITION;
float4 worldPos : TEXCOORD1;
float3 normal : NORMAL; // Pass normal to fragment shader
};
struct InstanceData {
float3 position;
float3 scale;
};
sampler2D _MainTex; // Texture sampler
float4 _MainTex_ST; // Texture scale and offset
fixed4 _Color; // Color property
float _AmbientIntensity; // Ambient intensity
float _DiffMax;
StructuredBuffer<InstanceData> instanceData; // Buffer containing positions and scales
v2f vert(appdata_t v, uint id : SV_InstanceID)
{
v2f o;
// Transform vertex position and apply instance data
float4 worldPosition = mul(unity_ObjectToWorld, v.vertex);
worldPosition.xyz += instanceData[id].position; // Add position offset from buffer
worldPosition.xyz *= instanceData[id].scale; // Apply scale from buffer
o.pos = UnityObjectToClipPos(worldPosition);
o.uv = TRANSFORM_TEX(v.uv, _MainTex); // Transform UVs based on texture properties
o.worldPos = worldPosition; // Pass world position for lighting calculations
o.normal = normalize(mul((float3x3)unity_WorldToObject, v.normal)); // Transform normal to world space
return o;
}
fixed4 frag(v2f i) : SV_Target
{
// Calculate lighting
fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz); // Get directional light direction
fixed3 normal = normalize(i.normal); // Normalize the interpolated normal
// Calculate diffuse component using Lambert's cosine law
fixed4 texColor = tex2D(_MainTex, i.uv);
// Calculate diffuse lighting contribution
float diff = max(_DiffMax, dot(normal, lightDir));
// Combine diffuse color with texture color and light intensity
fixed3 diffuse = diff * texColor.rgb;
// Calculate ambient lighting contribution
fixed3 ambient = texColor.rgb * _AmbientIntensity;
return fixed4(diffuse + ambient * _Color.rgb, texColor.a); // Combine diffuse and ambient colors
}
ENDCG
}
}
}