- Home /
Need help with billboard shader
So I have this billboard shader that I found online and it works good and all but it's missing some things that I need and I have never worked with shaders before and don't know how to add it on my own.
So here is what I need to add:
I need to add a color property to the materials, because many of my textures use grayscales and I need to set the color for those grayscales. Material.color does not work for that
Some of my meshes share the same position, so then it happens that some are rendered in front of others. this becomes a problem because the wrong ones are renderer in front and I don't seem to have any control on the order of the billboards. I have no idea if it is even possible, but I would love to add some sort of layer sorting thingy that could help me choose the order of which the meshes are renderer so that the correct ones are shown in front of others. Maybe something similar to the 2D layering system for sprite renderer?
Any help would be greatly appreciated!
Here is the shader:
Shader "Custom/Billboard"
{
Properties
{
_MainTex("Texture Image", 2D) = "white" {}
_ScaleX("Scale X", Float) = 1.0
_ScaleY("Scale Y", Float) = 1.0
}
SubShader
{
Tags {"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"}
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// User-specified uniforms
uniform sampler2D _MainTex;
uniform float _ScaleX;
uniform float _ScaleY;
struct vertexInput
{
float4 vertex : POSITION;
float4 tex : TEXCOORD0;
};
struct vertexOutput
{
float4 pos : SV_POSITION;
float4 tex : TEXCOORD0;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
output.pos = mul(UNITY_MATRIX_P,
mul(UNITY_MATRIX_MV, float4(0.0, 0.0, 0.0, 1.0))
+ float4(input.vertex.x, input.vertex.y, 0.0, 0.0)
* float4(_ScaleX, _ScaleY, 1.0, 1.0));
output.tex = input.tex;
return output;
}
float4 frag(vertexOutput input) : COLOR
{
return tex2D(_MainTex, float2(input.tex.xy));
}
ENDCG
}
}
}
The first thing is easy, the second one I'm not sure about. So lemme answer the first one for you:
Add this line to the properties:
_Color("Color", Color) = (1, 1, 1, 1)
In the 'user-defined uniforms' region, add
uniform float4 _Color;
In the frag function, replace the line with
return tex2D(_MainTex, float2(input.tex.xy)) * _Color;
I think that'll do the trick. Unless I made some spelling errors or something, so let me know how it goes.