I achieved GPU Instancing to work with Mesh Renderer but i’m not sure why it is breaking with Sprite Renderers and the only message i’m getting in the Frame Debugger is:
Non-instanced properties set for instanced shader.
Shader:
Shader "GPU Instancing Sprite"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Blend ("Blend", float) = 0
[PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
}
ZWrite Off
Blend One OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_instancing
#include "UnityCG.cginc"
fixed4 _MainTex_ST;
sampler2D_half _AlphaTex;
sampler2D_half _MainTex;
UNITY_INSTANCING_BUFFER_START (Props)
UNITY_DEFINE_INSTANCED_PROP (fixed, _Blend)
UNITY_INSTANCING_BUFFER_END (Props)
struct Input
{
fixed3 vertex : POSITION;
fixed2 uv0 : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Output
{
fixed4 pos : SV_POSITION;
fixed2 uv0 : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
Output vert (Input i)
{
Output o;
UNITY_SETUP_INSTANCE_ID (i);
UNITY_TRANSFER_INSTANCE_ID (i, o);
o.pos = UnityObjectToClipPos (i.vertex);
o.uv0 = TRANSFORM_TEX (i.uv0, _MainTex);
return o;
}
fixed4 frag (Output i) : SV_target
{
UNITY_SETUP_INSTANCE_ID (i);
fixed4 tex = tex2D (_MainTex, i.uv0);
tex.rgb = lerp (fixed3 (0, 0, 0), tex.rgb, UNITY_ACCESS_INSTANCED_PROP (Props, _Blend));
tex.rgb *= tex.a;
return tex;
}
ENDCG
}
}
}
using System.Collections;
using UnityEngine;
[RequireComponent (typeof (SpriteRenderer))]
[DisallowMultipleComponent]
public sealed class ColorReveal : MonoBehaviour
{
IEnumerator Start ()
{
float _duration = Random.Range (5f, 10f);
SpriteRenderer _thisRenderer = GetComponent<SpriteRenderer> ();
MaterialPropertyBlock _propertyBlock = new MaterialPropertyBlock ();
_thisRenderer.GetPropertyBlock (_propertyBlock);
for (float _t = 0; _t < _duration; _t += Time.deltaTime)
{
float _step = _t / _duration;
_propertyBlock.SetFloat ("_Blend", _step);
_thisRenderer.SetPropertyBlock (_propertyBlock);
yield return null;
}
_propertyBlock.SetFloat ("_Blend", 1);
_thisRenderer.SetPropertyBlock (_propertyBlock);
}
}
With Mesh Renderers if _Blend value was not equal between different instances it still was working. Frame debugger showed only 1 instanced group.
With Sprite Renderers it’s only working if _Blend value is equal in all of the instances. For example with my script when the lerp finishes _Blend equals = 1 in all sprites so they get instanced correctly.
But while they are lerping they instancing breaks, frame debugger show all as separated batches.
Any ideas why this might be happening?