Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by sarahnorthway · Jul 12, 2018 at 06:36 AM · shaderspritetransparencyfog

How to change RGB without changing alpha in sprite frag shader?

I'm adding fog to a custom sprite vert+surf shader, rendered with a SpriteRenderer. It's as easy as removing #pragma nofog, but the result applies the fog color to transparent portions of my sprites, resulting in semi-visible boxes around them.

alt text

I traced this to Unity's UNITY_FOG_LERP_COLOR, called at the end of the Unity generated frag shader (frag_surf):

 #define UNITY_FOG_LERP_COLOR(col,fogCol,fogFac) col.rgb = lerp((fogCol).rgb, (col).rgb, saturate(fogFac))

If I change it to:

 col.r = 1;

Now it only sets the red channel to 1. It shouldn't affect other colors, or the alpha, right? But the result still adds semi-visible red boxes around my sprites (pictured above). Does Unity somehow embed alpha information into the R channel? How do I change RGB in the frag shader without affecting alpha?

I don't want to use clip. Multiplying col.rgb by col.a after applying the fog looks close to correct, but adds a faint dark outline.

My shader:

 Shader "Northway/SpritesDiffuseWave" {
     Properties {
         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
     }
 
     SubShader {
         Tags {
             "Queue"="Geometry"
             "IgnoreProjector"="True"
             "RenderType"="Transparent"
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
 
         Cull Off
         Lighting Off
         ZWrite Off
         Blend One OneMinusSrcAlpha
 
         CGPROGRAM
         #pragma surface surf SimpleLambert vertex:vert nolightmap nodynlightmap keepalpha noinstancing //nofog 
         
         #include "UnitySprites.cginc"
 
         #undef UNITY_FOG_LERP_COLOR
         #define UNITY_FOG_LERP_COLOR(col,fogCol,fogFac) col.r = 1;
         
         struct Input {
             float2 uv_MainTex;
             fixed4 color;
         };
         
         half4 LightingSimpleLambert(SurfaceOutput s, half3 lightDir, half atten) {
             half4 c;
             c.rgb = s.Albedo * _LightColor0.rgb * atten * 0.60;
             c.a = s.Alpha;
             return c;
         }
  
         void vert (inout appdata_full v, out Input o) {
             UNITY_INITIALIZE_OUTPUT(Input, o);
 
             //... vertex shader code
         }
 
         void surf (Input IN, inout SurfaceOutput o) {
             fixed4 color = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
             o.Albedo = color.rgb * color.a;
             o.Alpha = color.a;
         }
 
         ENDCG
     }
 
     Fallback "Transparent/VertexLit"
 }
 


fog.jpg (187.3 kB)
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 sarahnorthway · Sep 19, 2018 at 09:34 PM

I was on the right track with multiplying rgb by alpha; I needed to multiply the FOG rgb by alpha before I lerp it in. Presumably col.rgb already has the alpha information baked in, but fogCol.rgb does not.

 #undef UNITY_FOG_LERP_COLOR
 #define UNITY_FOG_LERP_COLOR(col,fogCol,fogFac) col.rgb = lerp((fogCol).rgb * col.a, (col).rgb, saturate(fogFac))

This works with #pragma keepalpha or alpha:premul but not alpha:blend.

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

223 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

Related Questions

HDRP SpriteRenderer + Volumetric Fog shader 1 Answer

Render transparent mesh over sprite 0 Answers

Transparent cube and sprite order 0 Answers

Transparency using Scene Color node not layering multiple transparent objects over each other 0 Answers

Android: SVG Support 1 Answer


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