I’m trying to use GPU instancing to spawn some billboard elements in my screen, but the billboard shader isn’t working.
I’m using the shader code provided by Unity here as a starting point. I modified it and now it is only spawning the objects at the given world position data.xyz
.
This is the current state of the shader code:
Shader "Instanced/InstancedShader" {
Properties{
_MainTex("Albedo (RGB)", 2D) = "white" {}
}
SubShader{
Pass {
Tags {"LightMode" = "SRPDefaultUnlit"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
#pragma target 4.5
#include "UnityCG.cginc"
#include "UnityLightingCommon.cginc"
#include "AutoLight.cginc"
sampler2D _MainTex;
#if SHADER_TARGET >= 45
StructuredBuffer<float4> positionBuffer;
#endif
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
SHADOW_COORDS(4)
};
v2f vert(appdata_full v, uint instanceID : SV_InstanceID)
{
#if SHADER_TARGET >= 45
float4 data = positionBuffer[instanceID];
#else
float4 data = 0;
#endif
float3 localPosition = v.vertex.xyz;
float3 worldPosition = data.xyz + localPosition;
float3 worldNormal = v.normal;
v2f o;
o.pos = mul(UNITY_MATRIX_VP, float4(outPos, 1.0f));
o.uv = v.texcoord;
TRANSFER_SHADOW(o)
return o;
}
fixed4 frag(v2f i) : SV_Target
{
return tex2D(_MainTex, i.uv);;
}
ENDCG
}
}
}
How can I make a billboard out from this shader? I’ve been trying a bunch of stuff but in the end the instantiated meshes always look wierd or simply disappear or spawn in the wrong positions (they should be centered in data.xyz).