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 abhimanyu-singh · May 24, 2016 at 02:42 PM · shaderfog

How to enable Fog in vertex bending shader?

I recently found a Unity shader that bends the vertex to create a "Subway Surfers" game like effect. This is working beautifully, but it doesn't seem to support Fog.

 Shader "Custom/Curved" {
     Properties {
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _QOffset ("Offset", Vector) = (0,0,0,0)
         _Dist ("Distance", Float) = 100.0
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
 
                         sampler2D _MainTex;
             float4 _QOffset;
             float _Dist;
             
             struct v2f {
                 float4 pos : SV_POSITION;
                 float4 uv : TEXCOORD0;
             };
 
             v2f vert (appdata_base v)
             {
                 v2f o;
                 float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
                 float zOff = vPos.z/_Dist;
                 vPos += _QOffset*zOff*zOff;
                 o.pos = mul (UNITY_MATRIX_P, vPos);
                 o.uv = v.texcoord;
                 return o;
             }
 
             half4 frag (v2f i) : COLOR
             {
                 half4 col = tex2D(_MainTex, i.uv.xy);
                 return col;
             }
             ENDCG
         }
     }
     FallBack "Diffuse"
 }

I tried altering the "SubShader" section as shown below, but it has created a flickering Fog with no Alpha.

 Tags { "RenderType"="transparent" }
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile_fog
             
             #include "UnityCG.cginc"
 
             sampler2D _MainTex;
             float4 _QOffset;
             float _Dist;
             
             struct v2f {
                 float4 pos : SV_POSITION;
                 float4 uv : TEXCOORD0;
                 UNITY_FOG_COORDS(1)
             };
 
             v2f vert (appdata_base v)
             {
                 v2f o;
                 float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
                 float zOff = vPos.z/_Dist;
                 vPos += _QOffset*zOff*zOff;
                 o.pos = mul (UNITY_MATRIX_P, vPos);
                 o.uv = v.texcoord;
                 UNITY_TRANSFER_FOG(o,v.vertex);
                 return o;
             }
 
             half4 frag (v2f i) : COLOR
             {
                 half4 col = tex2D(_MainTex, i.uv.xy);
                 
                 UNITY_APPLY_FOG(i.fogCoord, col);
                 UNITY_OPAQUE_ALPHA(col.a);
                 
                 return col;
             }
             ENDCG
         }

Can anyone tell me exactly what I'm doing wrong.

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
0
Best Answer

Answer by abhimanyu-singh · May 25, 2016 at 05:47 AM

Thanks @Jessespike. I tried those earlier, but they were giving some compilation errors. I merged some code from another shader, and it now supports fog (and hopefully lightmapping as well). Since I'm not a shader programmer, I'm not sure if this is the most optimised way to do it, but at least it's in a working state right now. I'd again like to thank you for taking out the time to help me.

 Shader "Custom/Curved With Fog" {
     Properties {
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _QOffset ("Offset", Vector) = (0,0,0,0)
         _Dist ("Distance", Float) = 100.0
     }
     SubShader {
         Tags { "RenderType"="transparent" "IgnoreProjector"="True" }
         Pass
         {
             CGPROGRAM
             #include "UnityCG.cginc"
             #pragma vertex vert addshadow
             #pragma fragment frag
             #pragma multi_compile_fog
             #pragma multi_compile LIGHTMAP_ON LIGHTMAP_OFF
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
             float4 _QOffset;
             float _Dist;
 
             struct appdata_t {
                 float4 vertex : POSITION;
                 float2 texcoord : TEXCOORD0;
                 float2 texcoord1 : TEXCOORD1;
             };
 
             struct v2f {
                 float4 vertex : SV_POSITION;
                 float2 uv : TEXCOORD0;
                 half2 uv2 : TEXCOORD1;
                 UNITY_FOG_COORDS(2)
             };
 
             v2f vert (appdata_t v)
             {
                 v2f o;
                 float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
                 float zOff = vPos.z/_Dist;
                 vPos += _QOffset*zOff*zOff;
 
                 o.vertex = mul (UNITY_MATRIX_P, vPos);
                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                 o.uv2 = v.texcoord;
 
                 #ifdef LIGHTMAP_ON
                   o.uv2  = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
                  #endif
                 UNITY_TRANSFER_FOG(o,o.vertex);
 
                 return o;
             }
 
             half4 frag (v2f i) : COLOR
             {
                 half4 col = tex2D(_MainTex, i.uv.xy);
                 #ifdef LIGHTMAP_ON
                  half4 lm    = UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv2 );
                  col.rgb     = col.rgb * DecodeLightmap(lm);
                     col *= UNITY_LIGHTMODEL_AMBIENT * 10;
                    #endif
             
                 UNITY_APPLY_FOG(i.fogCoord, col);
                 UNITY_OPAQUE_ALPHA(col.a);
                 
                 return col;
             }
             ENDCG
         }
     }
     FallBack "Diffuse"
 }
Comment
Add comment · Show 2 · 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 Majid468 · May 18, 2020 at 06:08 PM 0
Share

Hi, I am facing the same problem, i am using curved shaders in endless game but it doesn't support Fog.....how can i manage to do this in single shader..?? help will be appreciated.

avatar image abhimanyu-singh Majid468 · May 20, 2020 at 06:31 AM 0
Share

@$$anonymous$$ajid468 since it's a pretty old thread, I do not remember the specifics. However, I've added the last working shader code in my previous comment. As mentioned, I am not sure about the performance though.

avatar image
3

Answer by Jessespike · May 24, 2016 at 09:20 PM

UNITY_TRANSFER_FOG(o,v.vertex); is wrong, it should be UNITY_TRANSFER_FOG(o,o.pos);

As for the alpha problem, I don't know. I would guess it has something to do with UNITY_OPAQUE_ALPHA(col.a); It wasn't in the original shader that you posted and it's not needed for fog, so it's probably not needed.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Fog/color tint shader for orthographic camera 0 Answers

Adding a static fog in the bottom of a terrain 1 Answer

Unity 5: Water Shader and Fog Not Rendering 1 Answer

Fog and Blur not affecting terrain and some trees! 1 Answer

Achieve Height Based Fog similar to default one? 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