- Home /
Why are my GPU instanced objects all black?
Long story short aka TLDR:
2D Grass shader > vertex animated > can't use batching afaik because objects loose their individual pivot > Trying gpu instancing > pivots are preserved yay! > Instance are all black boo :(
Can it be fixed? > Should I rethink the whole thing?
Long story, the details!:
I am making a shader to animate 2D grass, all features are implemented. Worked great until I implemented a render texture to make the grass fold when objects move through it.
Had to disable batching to get things to work (vertex animated using offsets afaik I need individual pivot points... is there a way around this?) ...batching completely screwed my performance so I got GPU instancing working on my sprites with the code bellow and HURRAY! It works and it doesn't mess with pivot points so my vertex animations work! BOoo! the instances are black.
What It looks like:
Honestly, I really dont care how I fix this. In the end, I need to cover my screen with vegetation. Right now I have about 3500 of those sprites covering the screen and with batching disabled a lot of preformance is lost just for grass. I hope gpu instancing can fix this but I am not even sure I am doing this whole grass thing the right way.
Features I want to keep working:
-Looks like actuall grass standing up, not tiles
-Wind simulation
-Can be displaced by objects moving through it
I need help fixing this and/or figuring if I should take another approach to create the grass.
Thank you for you assistance!
Answer by tcz8 · Apr 16, 2017 at 04:12 AM
Well I got this working. The mesh I was creating in code and feeding to Graphic.DrawMeshInstanced had issues.
Just apply the material to a Quad instead.
Answer by tanoshimi · Apr 03, 2017 at 06:01 AM
You need to use an Instanced shader on the material assigned to the object.
I already am and I am not setting it's parameters while creating the instance in the script but the shader has default values configured for everything.
Can you post the shader code you're using then? I don't see any problems per se with the code you've posted to create the mesh. (And does your instanced shader display other instanced objects correctly?)
The instanced shader is able to display nonrmal objects properly but any instance created with Graphics.Draw$$anonymous$$eshInstanced come out black.
I made the shader with Amplify Shader Editor which is node based.
I read about GPU instancing in Unity's manual and I still cant figure it out.
Here is the code:
Shader "Custom/Fred/TESTinstance"
{
Properties
{
_$$anonymous$$askClipValue( "$$anonymous$$ask Clip Value", Float ) = 0.5
[HideInInspector] __dirty( "", Int ) = 1
_Color0("Color 0", Color) = (1,1,1,1)
_$$anonymous$$ainTex("$$anonymous$$ainTex", 2D) = "white" {}
[HideInInspector] _texcoord( "", 2D ) = "white" {}
}
SubShader
{
Tags{ "RenderType" = "Opaque" "Queue" = "AlphaTest+0" }
Cull Back
CGPROGRA$$anonymous$$
#pragma target 3.0
#pragma multi_compile_instancing
#pragma surface surf Standard keepalpha addshadow fullforwardshadows
struct Input
{
float2 uv_texcoord;
};
uniform sampler2D _$$anonymous$$ainTex;
uniform float4 _$$anonymous$$ainTex_ST;
uniform float _$$anonymous$$askClipValue = 0.5;
UNITY_INSTANCING_CBUFFER_START(CustomFredTESTinstance)
UNITY_DEFINE_INSTANCED_PROP(float4, _Color0)
UNITY_INSTANCING_CBUFFER_END
void surf( Input i , inout SurfaceOutputStandard o )
{
float2 uv_$$anonymous$$ainTex = i.uv_texcoord * _$$anonymous$$ainTex_ST.xy + _$$anonymous$$ainTex_ST.zw;
float4 tex2DNode1 = tex2D( _$$anonymous$$ainTex,uv_$$anonymous$$ainTex);
o.Albedo = ( ( UNITY_ACCESS_INSTANCED_PROP(_Color0) * UNITY_ACCESS_INSTANCED_PROP(_Color0).a ) + tex2DNode1 ).xyz;
o.Alpha = 1;
clip( tex2DNode1.a - _$$anonymous$$askClipValue );
}
ENDCG
}
Fallback "Diffuse"
}
Your answer
Follow this Question
Related Questions
Unity 5.4 Gpu Instancing reduces amount of saved batches to zero 0 Answers
GPU Instancing (Dynamic Batching) Not Working on Oculus Quest 2 (Android). Works fine in-editor. 0 Answers
How to instance custom sprite shader? 0 Answers
When I enable 'GPU Instancing', transparent object starts flickering. 0 Answers