Hey it’s me again. Every time I work on this thing I learn so much more but I still don’t manage to figure it out fully lol.
I wanted to try working on a sort of x rotation for the objects but only calculated via the camera angle and I figured it would be easy given everything I already had and I was almost right.
My problem here is I’m not bothering to do a whole range of motion for this rotation. only the positive 180 degrees. here’s my spritesheet to illustrate this.
I have this functionality almost working in my shader but only problem is for the life of me I can’t get it to display the top row properly (directly above).
I have tried all sorts of poking and prodding all of which have their own failures and successes but this is the current most functional version of the shader which does most of what I need it to do.
please let me know if I am being silly I am still very new to this. Also ignore the bits of extra lighting code in there I was halfway implementing that.
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
Shader "Advanced Billboard"
{
Properties
{
_MainTex ("Albedo", 2D) = "white" {}
_Color ("Diffuse Color", Color) = (1,1,1,1)
_Frames ("Frames", Float) = 8
_Yframes ("Yframes", Float) = 5
_Smoothness ("Smoothness", Range(0, 1)) = 0.5
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "DisableBatching"="True"}
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
//Cull Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#define TAU (UNITY_PI * 2.0)
#define SINGLEFRAMEANGLE (TAU / _Frames)
#define SINGLEYFRAMEANGLE (TAU / _Yframes)
#define UVOFFSETX (1.0 / _Frames)
#define UVOFFSETY (1.0 / _Yframes)
uniform float4 _Color;
uniform sampler2D _MainTex;
uniform float4 _MainTex_ST;
uniform fixed4 _LightColor0;
struct vertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
struct vertex2Frag {
float4 pos : SV_POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
float3 normal : TEXCOORD1;
};
float _Frames;
int _Yframes;
vertex2Frag vert (vertexInput v)
{
vertex2Frag o;
// object's forward vector (local +Z direction)
float3 objWorldForward = unity_ObjectToWorld._m02_m12_m22;
//get objects current Y rotation from its rotation matrix in radians
float objWorldHeading = atan2(-objWorldForward.z, objWorldForward.x);
// object's world position
float3 objWorldPos = unity_ObjectToWorld._m03_m13_m23;
// get angle between object and camera in radians
float3 objToCam = _WorldSpaceCameraPos.xyz - objWorldPos;
float objToCamAngle = atan2(-objToCam.z, objToCam.x);
//calculates specific angles between camera and object in radians
float objToCamYAngle = atan2(-objToCam.y, min(min(objToCam.z,-objToCam.z),min(objToCam.x,-objToCam.x)));
// get angle difference between heading and camera relative position
float angleDiff = objToCamAngle - objWorldHeading;
// get current tilesheet frame on the x and y axis of the spritesheet
float index = round(angleDiff / SINGLEFRAMEANGLE);
float yindex = round(objToCamYAngle / SINGLEYFRAMEANGLE);
o.uv = float2(v.texcoord.x * UVOFFSETX + UVOFFSETX * index, -v.texcoord.y * UVOFFSETY + UVOFFSETY * yindex);
float3 normalDir = normalize(_WorldSpaceCameraPos - objWorldPos);
o.normal = v.normal;
o.pos = mul(UNITY_MATRIX_P,
mul(UNITY_MATRIX_V, float4(objWorldPos, 1.0))
+ float4(v.vertex.x, v.vertex.y, 0.0, 0.0));
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 diffuse = _Color.rgb; //* _LightColor0.rgb * max(0.0,dot(normalDir, lightDirection));
o.color = half4(diffuse, 1.0);
return o;
}
fixed4 frag(vertex2Frag i) : COLOR
{
return tex2D(_MainTex,i.uv) * i.color;
}
ENDCG
}
}
}