Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Noxury · Aug 20, 2020 at 11:01 AM · camerashaderdirectionfadeforward

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
        }
    }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 = ...
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

260 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges