Last active
November 7, 2024 09:33
-
-
Save AlexMerzlikin/ce756564414985039feff71ee79fefe6 to your computer and use it in GitHub Desktop.
Template unlit URP shader that supports DOTS instancing to use as a guide to create shaders for BatchRendererGroup since BRG only works with DOTS instancing compatible shaders. Unfortunately, the official docs about it haven't helped me that much, and I thought others might face the same issues.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Universal Render Pipeline/Custom/UnlitWithDotsInstancing" | |
{ | |
Properties | |
{ | |
_BaseMap ("Base Texture", 2D) = "white" {} | |
_BaseColor ("Base Colour", Color) = (1, 1, 1, 1) | |
} | |
SubShader | |
{ | |
Tags | |
{ | |
"RenderPipeline"="UniversalPipeline" "Queue"="Geometry" | |
} | |
Pass | |
{ | |
Name "Forward" | |
Tags | |
{ | |
"LightMode"="UniversalForward" | |
} | |
Cull Back | |
HLSLPROGRAM | |
#pragma exclude_renderers gles gles3 glcore | |
#pragma target 4.5 | |
#pragma vertex UnlitPassVertex | |
#pragma fragment UnlitPassFragment | |
#pragma multi_compile_instancing | |
#pragma instancing_options renderinglayer | |
#pragma multi_compile _ DOTS_INSTANCING_ON | |
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" | |
struct Attributes | |
{ | |
float4 positionOS : POSITION; | |
float2 uv : TEXCOORD0; | |
float4 color : COLOR; | |
UNITY_VERTEX_INPUT_INSTANCE_ID | |
}; | |
struct Varyings | |
{ | |
float4 positionCS : SV_POSITION; | |
float2 uv : TEXCOORD0; | |
float4 color : COLOR; | |
UNITY_VERTEX_INPUT_INSTANCE_ID | |
}; | |
CBUFFER_START(UnityPerMaterial) | |
float4 _BaseMap_ST; | |
float4 _BaseColor; | |
CBUFFER_END | |
#ifdef UNITY_DOTS_INSTANCING_ENABLED | |
UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata) | |
UNITY_DOTS_INSTANCED_PROP(float4, _BaseColor) | |
UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata) | |
#define _BaseColor UNITY_ACCESS_DOTS_INSTANCED_PROP_WITH_DEFAULT(float4, _BaseColor) | |
#endif | |
TEXTURE2D(_BaseMap); | |
SAMPLER(sampler_BaseMap); | |
Varyings UnlitPassVertex(Attributes input) | |
{ | |
Varyings output; | |
UNITY_SETUP_INSTANCE_ID(input); | |
UNITY_TRANSFER_INSTANCE_ID(input, output); | |
const VertexPositionInputs positionInputs = GetVertexPositionInputs(input.positionOS.xyz); | |
output.positionCS = positionInputs.positionCS; | |
output.uv = TRANSFORM_TEX(input.uv, _BaseMap); | |
output.color = input.color; | |
return output; | |
} | |
half4 UnlitPassFragment(Varyings input) : SV_Target | |
{ | |
UNITY_SETUP_INSTANCE_ID(input); | |
half4 baseMap = half4(SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv)); | |
return baseMap * _BaseColor * input.color; | |
} | |
ENDHLSL | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A sample how it is used can be found here: https://github.com/AlexMerzlikin/Unity-BatchRendererGroup-Boids