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 /
avatar image
0
Question by Darie2808 · Apr 08, 2017 at 11:20 PM · spriteshadersfog

Default-Sprite with fog

I am trying to have the sprites affected by fog. I checked this solution. But the Sprites-Default.shader has been completely changed in Unity 5.6. Can anyone help me?

Here is the code for the Sprites-Default.shader:

 // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
 
 Shader "Sprites/Default"
 {
     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 SpriteVert
             #pragma fragment SpriteFrag
             #pragma target 2.0
             #pragma multi_compile_instancing
             #pragma multi_compile _ PIXELSNAP_ON
             #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
             #include "UnitySprites.cginc"
         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

Answer by jjbish · Apr 25, 2017 at 12:09 AM

I Recently Solved This Problem. Here is My Sprites-Diffuse Shader

 Shader "Sprites/Sprite-Diffuse"
 {
     Properties
     {
         [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
         _Color ("Tint", Color) = (1,1,1,1)
         [MaterialToggle] PixelSnap ("Pixel Snap", Float) = 1
         [PerRendererData] _AlphaTex("External Alpha", 2D) = "white" {}
         [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
     }
 
     SubShader
     {
         Tags{"Queue"="Transparent" "PreviewType"="Plane" "CanUseSpriteAtlas"="True"}
 
         Cull Off
         Lighting Off
         ZWrite Off
         Blend One OneMinusSrcAlpha
 
         //Pass
         //{
         CGPROGRAM
         #pragma surface surf Lambert vertex:vert nolightmap nodynlightmap keepalpha noinstancing
         #pragma multi_compile _ PIXELSNAP_ON
         #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
         //#pragma multi_compile_fog
         //#include "UnityCG.cginc"
         #include "Includes/Sprites.cginc"
             
         struct Input 
         {
             float2 uv_MainTex;
             fixed4 color;
         };
 
         void vert (inout appdata_full v, out Input o)
         {
             
             #if defined(PIXELSNAP_ON)
             v.vertex = UnityPixelSnap(v.vertex);
             #endif
 
             // So Output doesnt have to be initialized?? - Does NOTHING?
             UNITY_INITIALIZE_OUTPUT(Input, o);
 
             o.color = v.color * _Color;
         }
 
         void surf(Input IN, inout SurfaceOutput o) 
         {
             fixed4 c = SampleSpriteTexture(IN.uv_MainTex) * IN.color;
             o.Albedo = c.rgb * c.a;
             o.Alpha = c.a;
         }
 
         ENDCG
         //}
     }
 
 //Fallback "Transparent/VertexLit"
 }

And I modified the "UnitySprites.cginc" include file as well which is called "Sprites.cginc" in the shader above. The modified include file is as follows:

 // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
 
 #ifndef UNITY_SPRITES_INCLUDED
 #define UNITY_SPRITES_INCLUDED
 
 #include "UnityCG.cginc"
 
 #ifdef UNITY_INSTANCING_ENABLED
 
     UNITY_INSTANCING_CBUFFER_START(PerDrawSprite)
         // SpriteRenderer.Color while Non-Batched/Instanced.
         fixed4 unity_SpriteRendererColorArray[UNITY_INSTANCED_ARRAY_SIZE];
         // this could be smaller but that's how bit each entry is regardless of type
         float4 unity_SpriteFlipArray[UNITY_INSTANCED_ARRAY_SIZE];
     UNITY_INSTANCING_CBUFFER_END
 
     #define _RendererColor unity_SpriteRendererColorArray[unity_InstanceID]
     #define _Flip unity_SpriteFlipArray[unity_InstanceID]
 
 #endif // instancing
 
 CBUFFER_START(UnityPerDrawSprite)
 #ifndef UNITY_INSTANCING_ENABLED
     fixed4 _RendererColor;
     float4 _Flip;
 #endif
     float _EnableExternalAlpha;
 CBUFFER_END
 
 // Material Color.
 fixed4 _Color;
 
 struct appdata_t
 {
     float4 vertex   : POSITION;
     float4 color    : COLOR;
     float2 texcoord : TEXCOORD0;
     UNITY_VERTEX_INPUT_INSTANCE_ID
 };
 
 struct v2f
 {
     float4 vertex   : SV_POSITION;
     fixed4 color    : COLOR;
     float2 texcoord : TEXCOORD0;
     UNITY_FOG_COORDS(1)
     UNITY_VERTEX_OUTPUT_STEREO
 };
 
 v2f SpriteVert(appdata_t IN)
 {
     v2f 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;
 }
 
 sampler2D _MainTex;
 sampler2D _AlphaTex;
 
 fixed4 SampleSpriteTexture (float2 uv)
 {
     fixed4 color = tex2D (_MainTex, uv);
 
 #if ETC1_EXTERNAL_ALPHA
     fixed4 alpha = tex2D (_AlphaTex, uv);
     color.a = lerp (color.a, alpha.r, _EnableExternalAlpha);
 #endif
 
     return color;
 }
 
 fixed4 SpriteFrag(v2f IN) : SV_Target
 {
     fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;
     c.rgb *= c.a;
     
     UNITY_APPLY_FOG(IN.fogCoord, c);

     return c;
 }
 
 #endif // UNITY_SPRITES_INCLUDED


Hope this Helps.

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 iLyxa3D · Sep 26, 2017 at 07:04 AM 0
Share

Not correct working woth FOG and sprite transparent: http://joxi.ru/YmEpGagUZ98k6r.jpg Good image when close to the camera, but squared white image at far distance.

How to fix it?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Skew a sprite at runtime while preserving the pixel shape? 1 Answer

Global fog doesn't work? 0 Answers

Distort shader not showing sprites 2 Answers

Simple shader fog functionality issue 0 Answers

[Shader] No Fog on Particles/Standard Unlit 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