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 CloudChing · Sep 17, 2015 at 10:44 AM · shadershadersshader programmingshaderlabshader writing

Fog not working in my own Vertext Shader

Fog not working in my own Vertext Shader,i use default surface shader is working.

I see the documentation:

 Fog handling was changed
 
 Unity 5.0 makes built-in Fog work on WP8 and consoles, but in order to achieve that we’ve changed how Fog is done a bit. For surface shaders and fixed function shaders, nothing extra needs to be done - fog will be added automatically (you can add “nofog” to surface shader #pragma line to explicitly make it not support fog).
 
 For manually written vertex/fragment shaders, fog does not happen automagically now. You need to add
 #pragma multi_compile_fog and fog handling macros to your shader code. Check out built-in shader source, for example Unlit-Normal how to do it.

i add the #pragma multi_compile_fog , but still not working...

there is my code:

  Properties {
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _QOffset ("Offset", Vector) = (0,0,0,0)
         _Dist ("Distance", Float) = 100.0
     }
   
     SubShader {
         Tags { "RenderType"="Opaque"  }
         Fog  {Mode Linear }
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
             #pragma multi_compile_fog
              
   
             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 = mul( UNITY_MATRIX_TEXTURE0, v.texcoord );
                 return o;
             }
   
             half4 frag (v2f i) : COLOR
             {
                 half4 col = tex2D(_MainTex, i.uv.xy);
                 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

2 Replies

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

Answer by cod · Sep 18, 2015 at 08:43 AM

From the documentation :

 // - no lighting
 // - no lightmap support
 // - no per-material color
 
 Shader "Unlit/Texture" {
 Properties {
     _MainTex ("Base (RGB)", 2D) = "white" {}
 }

 SubShader {
     Tags { "RenderType"="Opaque" }
     LOD 100
     
     Pass {  
         CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile_fog
             
             #include "UnityCG.cginc"
 
             struct appdata_t {
                 float4 vertex : POSITION;
                 float2 texcoord : TEXCOORD0;
             };
 
             struct v2f {
                 float4 vertex : SV_POSITION;
                 half2 texcoord : TEXCOORD0;
                 UNITY_FOG_COORDS(1)
             };
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
             
             v2f vert (appdata_t v)
             {
                 v2f o;
                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                 UNITY_TRANSFER_FOG(o,o.vertex);
                 return o;
             }
             
             fixed4 frag (v2f i) : SV_Target
             {
                 fixed4 col = tex2D(_MainTex, i.texcoord);
                 UNITY_APPLY_FOG(i.fogCoord, col);
                 UNITY_OPAQUE_ALPHA(col.a);
                 return col;
             }
         ENDCG
     }
 }
 
 }

Hope it works

PS if you want to access fog's data look here under the section fog http://docs.unity3d.com/Manual/SL-UnityShaderVariables.html unity_FogColor and unity_FogParams

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 brlan10 · Jun 23, 2017 at 01:45 PM 0
Share

how would you do the same thing but WITH lighting and transparency?

avatar image
0

Answer by CloudChing · Sep 18, 2015 at 01:53 AM

some one help me?

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 cod · Sep 18, 2015 at 08:43 AM 1
Share

Avoid posting a new reply, edit your question ins$$anonymous$$d

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

30 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

Related Questions

Is there any way to do multiple passes on a texture / in a fragment shader? 1 Answer

Shader error : unexpected $end, expecting TOK_SETTEXTURE or '}' at line 18 1 Answer

_Time in Image Effects 0 Answers

Operate the shader, rotate the material 90 degrees 0 Answers

Is it possible to set the 'ForceNoShadowCasting' SubShader Tag via shader properties or a custom material editor? 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