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
1
Question by DTE462 · Mar 16, 2017 at 04:44 AM · shadersspritesfog

Fog on a sprite

alt textTrying to get fog to affect a 2D sprite. Another question has modifying 'ZWrite On' to the shader as a solution.

http://answers.unity3d.com/questions/1271178/sprites-are-ignoring-global-fog.html

That doesn't work for me. I'm using 5.4.1.

In the Shader I am using though there is a line;

pragma surface surf Lambert vertex:vert nofog keepalpha

And getting rid of "nofog" does make the the sprite be affected by fog, but there is an outline that follows it that looks strange.

I also add the line ;

pragma multi_compile_fog

but this doesn't seem to change anything one way or the other.

For reference I am using the Sprites-Diffuse shader from the standard shaders download.

There are a few other questions addressing this but they are either unanswered or much older and also didn't work for me.

I copy and pasted this solution and got about as far. http://answers.unity3d.com/questions/1244402/trying-to-have-sprites-affected-by-fog-without-hav.html

Any help would be appreciated. Thank you.

fog-shader.jpg (16.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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by vikingsoulgames · Jul 13, 2019 at 06:47 PM

Although the post is from some time ago, I just had the same small problem and I want to provide the solution I found, thanks to the fact that someone charitable shared a shader just for this in the following link:

https://www.reddit.com/r/Unity3D/comments/6scjlo/a_friend_of_mine_made_a_sprite_shader_which/

The shader what Im talking about is the next: // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

 Shader "Sprites/Default with Fog"
 {
     Properties
     {
         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
         _Color ("Tint", Color) = (1,1,1,1)
         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
         [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
         [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
         [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
         [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
     }
 
     SubShader
     {
         Tags
         { 
             "Queue"="Transparent" 
             "IgnoreProjector"="True" 
             "RenderType"="Transparent" 
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
 
         Cull Off
         Lighting Off
         ZWrite Off
         Blend One OneMinusSrcAlpha
 
         Pass
         {
         CGPROGRAM
             #pragma vertex SpriteVertFog
             #pragma fragment SpriteFragFog
             #pragma target 2.0
             #pragma multi_compile_instancing
             #pragma multi_compile _ PIXELSNAP_ON
             #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
             #pragma multi_compile_fog
             #include "UnitySprites.cginc"
 
             struct v2f_fog
             {
                 float4 vertex   : SV_POSITION;
                 fixed4 color    : COLOR;
                 float2 texcoord : TEXCOORD0;
                 UNITY_FOG_COORDS(1)
                 UNITY_VERTEX_OUTPUT_STEREO
             };
 
             v2f_fog SpriteVertFog(appdata_t IN)
             {
                 v2f_fog OUT;
 
                 UNITY_SETUP_INSTANCE_ID (IN);
                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
 
             #ifdef UNITY_INSTANCING_ENABLED
                 IN.vertex.xy *= _Flip.xy;
             #endif
 
                 OUT.vertex = UnityObjectToClipPos(IN.vertex);
                 OUT.texcoord = IN.texcoord;
                 OUT.color = IN.color * _Color * _RendererColor;
 
             #ifdef PIXELSNAP_ON
                 OUT.vertex = UnityPixelSnap (OUT.vertex);
             #endif
 
 
                 UNITY_TRANSFER_FOG(OUT, OUT.vertex);
 
                 return OUT;
             }
 
 
             fixed4 SpriteFragFog(v2f_fog IN) : SV_Target
             {
                 fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;
 
                 // apply fog
                 UNITY_APPLY_FOG(IN.fogCoord, c);
                 c.rgb *= c.a;
 
                 return c;
             }
 
         ENDCG
         }
     }
 }


It was enough for me to create a material, assign it to this shader and then assign it to my sprites that material.

Comment
Add comment · Show 1 · 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
avatar image Tizizzails · Dec 20, 2021 at 02:29 AM 0
Share

You did it!
And I was able to set up this shader without even having to look it up. High five for both of us!

avatar image
0

Answer by eyeballTickler · May 02, 2018 at 12:19 AM

I ran into the same issue and resolved it by:

  1. Removing the nofog directive

  2. Adding this line to the end of the surf function

clip(o.Albedo - _CutoutThresh); where _CutoutThresh is a float that I set to 0.05

Note, I didn't have to turn ZWrite on or add the multi_compile_fog in my case but your results may vary.

EDIT: As @blargenswarg pointed out, the clip line should probably use o.Alpha instead of o.Albedo

Comment
Add comment · Show 4 · 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
avatar image blargenswarg · Jun 02, 2018 at 02:14 PM 0
Share

Thank you very much for this. I tried your solution but the line clip(o.Albedo - _CutoutThresh); caused the shader to cut holes in some sprites where they shouldn't be. After some experimentation I found that it worked much better with clip(o.Alpha - _CutoutThresh); with _CutoutThresh set to 1. I'm not using the most recent version of Unity though, so that might be why our results vary.

avatar image eyeballTickler blargenswarg · Jun 02, 2018 at 03:33 PM 0
Share

Thanks back at you! That actually fixed an issue where my sprite would disappear when the sprite color was set to a pure non-white color. I'll update my original answer.

avatar image mystman12 · Aug 15, 2018 at 02:52 AM 0
Share

I know this is a pretty old post, but would you $$anonymous$$d explaining how you added _CutoutThresh as a float to the shader? I'd really appreciate it.

avatar image eyeballTickler mystman12 · Aug 15, 2018 at 05:45 AM 0
Share

Here's the full shader:

 Shader "Custom/Shadow Casting Sprite" {
     Properties {
         _Cutoff("Cutoff", Range(0,1)) = 0.5
         _Color ("Tint", Color) = (1,1,1,1)
         [PerRendererData]_$$anonymous$$ainTex ("Sprite Texture", 2D) = "white" {}
     }
     SubShader {
         Tags
         {
             "RenderType"="Opaque"
             "IgnoreProjector"="True"
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
         LOD 200
 
         Cull Off
         Lighting Off
         //ZWrite Off
         Blend One One$$anonymous$$inusSrcAlpha
 
         CGPROGRA$$anonymous$$
         // Lambert lighting model, and enable shadows on all light types
         #pragma surface surf Lambert addshadow fullforwardshadows
 
         #pragma target 2.0
         #pragma multi_compile_instancing
 
         sampler2D _$$anonymous$$ainTex;
         fixed4 _Color;
         fixed _Cutoff;
 
         struct Input
         {
             float2 uv_$$anonymous$$ainTex;
         };
 
         void surf (Input IN, inout SurfaceOutput o) {
             fixed4 c = tex2D (_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex) * _Color;
             o.Albedo = c.rgb;
             o.Alpha = c.a;
             clip(o.Alpha - _Cutoff);
         }
         ENDCG
     }
     FallBack "Diffuse"
 }

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

107 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

Related Questions

Shaders 2D sprites diffuse - more than two source of lights - issue with full lightning objects 0 Answers

Anti-aliasing Sprite Shader Graph 0 Answers

Sprites do not work with global fog 0 Answers

Shader Graph - Inherit from Sprite Renderer or Image Component 0 Answers

Making an area behind a sprite slightly darker? 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