- Home /
Fade shader based on heading angle to camera not working
I have written a shader that fades in depending on the angle difference between the attached objects forward direction and the cameras view direction.
However, the fading does not work smoothly. For example when the camera moves past the objects right side, the object gets instantly transparent. Any suggestions to fix this problem?
In terms of performance, this is also not very efficient. For example, since frag gets called for every pixel and the angle difference is always the same per frame, how would I call the angleDifference part only once which is then stored to the input struct?
Shader "VertexColor/AdditiveFadeAngle" //fades alpha based on viewDirection angle //vlt auch billboard
{
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Particle Texture", 2D) = "white" {}
_FadeAngle ("Fade Angle", Range(0.0, 180.0)) = 90
}
SubShader
{
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
LOD 100
Blend SrcAlpha One
Cull Off Lighting Off ZWrite Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
fixed4 _Color;
sampler2D _MainTex;
fixed4 _MainTex_ST;
fixed _FadeAngle;
struct appdata
{
fixed4 vertex : POSITION;
fixed4 color : COLOR;
fixed2 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.color = v.color;
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o, o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// 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; //asin(normalize(objToCam).y)
float objToCamAngle = atan2(objToCam.z, objToCam.x);
//get angle difference between heading and camera relative position
float angleDiff = abs(objToCamAngle - objWorldHeading);
half alpha = clamp(lerp (1, 0, angleDiff*57.29578 / _FadeAngle), 0, 1);
fixed4 col = i.color * tex2D(_MainTex, i.uv) * _Color * alpha;
UNITY_APPLY_FOG_COLOR(i.fogCoord, col, fixed4(0,0,0,0)); // fog towards black due to our blend mode
return col;
}
ENDCG
}
}
}
Answer by Namey5 · Aug 21, 2020 at 07:09 AM
You can mark instructions as static and const in shaders and they should only be executed once per frame (they cannot use input data from the vertex shader, but they can exist outside of functions). You can also use the cosine property of the dot product when both vectors are normalised to find the angle between them, thus only using one inverse-trig call;
...
static const float3 objWorldForward = unity_ObjectToWorld._m02_m12_m22;
static const float3 objWorldPos = unity_ObjectToWorld._m03_m13_m23;
static const float3 objToCam = _WorldSpaceCameraPos.xyz - objWorldPos;
static const float angleDiff = acos (dot (normalize (objToCam.xz), normalize (objWorldForward.xz)));
half alpha = ...
Your answer
Follow this Question
Related Questions
Shader Edges Transparent Relative to Camera 0 Answers
How can I have the camera forward in a vertex shader? 0 Answers
Change characters direction using camera view 0 Answers
Shader Transparent Background Camera Occlusion Fadeout 0 Answers
Forward Rendering for Terrain, overriding the shader? 0 Answers