I wanted to find a way to combine these two shaders, one transforms the texture into a vertical billboard and the other makes the texture transparent when the camera turns to the back of the object. I want to add alpha transparent render too
Shader "Custom/Billboard Y Rotation Only"
{
Properties
{
_MainTex("Texture Image", 2D) = "white" {}
_ScaleX("Scale X", Float) = 1.0
_ScaleY("Scale Y", Float) = 1.0
}
SubShader
{
Tags { "RenderType" = "Opaque" "Queue" = "Transparent"}
Cull Off
Pass
{
CGPROGRAM
#include "UnityCG.cginc"
#pragma shader_feature IGNORE_ROTATION_AND_SCALE
#pragma vertex vert
#pragma fragment frag
// User-specified uniforms
uniform sampler2D _MainTex;
uniform float _ScaleX;
uniform float _ScaleY;
float4 _MainTex_ST;
struct vertexInput
{
float4 vertex : POSITION;
float4 tex : TEXCOORD0;
};
struct vertexOutput
{
float4 pos : SV_POSITION;
float2 tex : TEXCOORD0;
};
vertexOutput vert(vertexInput input)
{
// The world position of the center of the object
float3 worldPos = mul(unity_ObjectToWorld, float4(0, 0, 0, 1)).xyz;
// Distance between the camera and the center
float3 dist = _WorldSpaceCameraPos - worldPos;
// atan2(dist.x, dist.z) = atan (dist.x / dist.z)
// With atan the tree inverts when the camera has the same z position
float angle = atan2(dist.x, dist.z);
float3x3 rotMatrix;
float cosinus = cos(angle);
float sinus = sin(angle);
// Rotation matrix in Y
rotMatrix[0].xyz = float3(cosinus, 0, sinus);
rotMatrix[1].xyz = float3(0, 1, 0);
rotMatrix[2].xyz = float3(-sinus, 0, cosinus);
// The position of the vertex after the rotation
float4 newPos = float4(mul(rotMatrix, input.vertex * float4(_ScaleX, _ScaleY, 0, 0)), 1);
// The model matrix without the rotation and scale
float4x4 matrix_M_noRot = unity_ObjectToWorld;
matrix_M_noRot[0][0] = 1;
matrix_M_noRot[0][1] = 0;
matrix_M_noRot[0][2] = 0;
matrix_M_noRot[1][0] = 0;
matrix_M_noRot[1][1] = 1;
matrix_M_noRot[1][2] = 0;
matrix_M_noRot[2][0] = 0;
matrix_M_noRot[2][1] = 0;
matrix_M_noRot[2][2] = 1;
vertexOutput output;
// The position of the vertex in clip space ignoring the rotation and scale of the object
#if IGNORE_ROTATION_AND_SCALE
output.pos = mul(UNITY_MATRIX_VP, mul(matrix_M_noRot, newPos));
#else
output.pos = mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, newPos));
#endif
output.tex = TRANSFORM_TEX(input.tex, _MainTex);
return output;
}
float4 frag(vertexOutput input) : COLOR
{
return tex2D(_MainTex, input.tex.xy);
}
ENDCG
}
}
}
Shader "Custom/Front" {
Properties{
_MainTex("Texture", 2D) = "white" {}
_AlphaThreshold("Alpha Threshold", Range(0, 1)) = 0.5
}
SubShader{
Tags { "Queue" = "Transparent" }
LOD 100
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float2 texcoord : TEXCOORD0;
float4 vertex : SV_POSITION;
};
float4 _MainTex_ST;
float _AlphaThreshold;
v2f vert(appdata_t v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = v.texcoord;
return o;
}
sampler2D _MainTex;
half4 frag(v2f i) : SV_Target {
// Calculate the vector from the camera to the particle.
float3 toCamera = _WorldSpaceCameraPos - i.vertex.xyz;
toCamera = normalize(toCamera);
// Calculate the dot product between the camera's forward direction
// and the particle's forward direction (in world space).
float dotProduct = dot(float3(0, 0, 1), toCamera);
// If the dot product is negative, the camera is looking away,
// so we make the particle invisible.
if (dotProduct < 0) {
discard;
}
// Sample the texture and apply alpha testing.
half4 col = tex2D(_MainTex, i.texcoord * _MainTex_ST.xy + _MainTex_ST.zw);
if (col.a < _AlphaThreshold) {
discard;
}
return col;
}
ENDCG
}
}
}