- Home /
Question by
abbsimoga · Sep 25, 2021 at 10:49 AM ·
rotationbillboardbillboards
Billboard shader does not support rotation of object
Why does this billboard shader not face the camera if the object is to be rotated. With zero world rotation this shader works as expected.
#include "/Assets/Graphics/CGincFiles/BillboardSetup.cginc"
float _TilePixelSize;
float _ExtrudeDistance;
float3 _UniformDisplacementRandom;
struct g2f
{
float4 pos : SV_POSITION;
float light : TEXCOORD0;
float2 uv : TEXCOORD2;
};
g2f VertexOutput(float3 pos, float directionalLightValue)
{
g2f o;
o.pos = UnityObjectToClipPos(pos);
o.light = directionalLightValue;
return o;
}
fixed rand(float3 co)
{
return sin(dot(co.xyz, float3(12.9898, 78.233, 53.539)));
}
[maxvertexcount(4)]
void geo(triangle vertexOutput IN[3], inout TriangleStream<g2f> outStream)
{
float3 flatNormal = normalize(cross(IN[1].vertex - IN[0].vertex, IN[2].vertex - IN[0].vertex));
float3 center = (IN[0].vertex + IN[1].vertex + IN[1].vertex) / 3;
fixed lightValue = LightCalculation(center, flatNormal);
fixed3 uniform_displacement = float3(rand(center), rand(center.yzx), rand(center.xzy)) * _UniformDisplacementRandom;
center += uniform_displacement + flatNormal * _ExtrudeDistance;
fixed pixelSize = _TilePixelSize / (5.4 * 2);
float4 vectors[4];
Get4Vectors(center, pixelSize, vectors);
g2f idealVertexOutput = VertexOutput(center, lightValue);
// Only change SV_POSITION and append to outStream
idealVertexOutput.pos = UnityObjectToClipPos(vectors[0]);
idealVertexOutput.uv = float2(1, 0);
outStream.Append(idealVertexOutput);
idealVertexOutput.pos = UnityObjectToClipPos(vectors[1]);
idealVertexOutput.uv = float2(1, 1);
outStream.Append(idealVertexOutput);
idealVertexOutput.pos = UnityObjectToClipPos(vectors[2]);
idealVertexOutput.uv = float2(0, 0);
outStream.Append(idealVertexOutput);
idealVertexOutput.pos = UnityObjectToClipPos(vectors[3]);
idealVertexOutput.uv = float2(0, 1);
outStream.Append(idealVertexOutput);
}
Get4Vectors function are in BillboardSetup.cginc:
fixed3 GetLookVector()
{
float3 look = mul(mul((float3x3)unity_CameraToWorld, float3(0,0,-1)), unity_ObjectToWorld);
return normalize(look);
}
fixed3 GetRightVector(fixed3 look)
{
fixed3 world_up = float3(0, 1, 0);
fixed3 right = normalize(cross(world_up, look));
return right;
}
void Get4Vectors(float3 center, fixed pixelSize, out float4 vectors[4])
{
fixed3 look = GetLookVector();
fixed3 right = GetRightVector(look) * pixelSize;
fixed3 up = fixed3(0, pixelSize, 0);
vectors[0] = float4(center + right - up, 1.0f);
vectors[1] = float4(center + right + up, 1.0f);
vectors[2] = float4(center - right - up, 1.0f);
vectors[3] = float4(center - right + up, 1.0f);
}
Comment
Your answer
Follow this Question
Related Questions
Problem with unity tree billboarding 0 Answers
Stop Billboard Texture Rotation in Triplanar Shader 0 Answers
Problem with a Billboard script 0 Answers
Flip over an object (smooth transition) 3 Answers