Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MaT227 · Jun 14, 2013 at 12:11 PM · shaderfoghlslfragmentglsl

Fog shader

Hi,

I have some difficulties with my fog shader. I have a good visual result but the problem is that the gradient is based on the camera's position, it moves as the camera moves. I don't know how to fix it.

Here is the shader code.

 struct v2f {
     float4 pos : SV_POSITION;
     float4 grabUV : TEXCOORD0;
     float2 uv_depth : TEXCOORD1;
     float4 interpolatedRay : TEXCOORD2;
     float4 screenPos : TEXCOORD3;
 };
 
 v2f vert(appdata_base v) {
     v2f o;
     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
     o.uv_depth = v.texcoord.xy;
     o.grabUV = ComputeGrabScreenPos(o.pos);
     half index = v.vertex.z;
     o.screenPos = ComputeScreenPos(o.pos); 
     o.interpolatedRay = mul(UNITY_MATRIX_MV, v.vertex);
     return o;
 }
 
 sampler2D _GrabTexture;
 
 float4 frag(v2f IN) : COLOR {
     float3 uv = UNITY_PROJ_COORD(IN.grabUV);
     float dpth = UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, uv));
     dpth = LinearEyeDepth(dpth);
     float4 wsPos = (IN.screenPos + dpth * IN.interpolatedRay); // Here is the problem but how to fix it
     float fogVert = max(0.0, (wsPos.y - _Depth) * (_DepthScale * 0.1f));
     fogVert *= fogVert; 
     fogVert = (exp (-fogVert));
     return fogVert;
 }

Thanks a lot !

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 MaT227 · Jun 14, 2013 at 07:31 PM

It seems that it's a Matrix problem

 o.interpolatedRay = mul(UNITY_MATRIX_MV, v.vertex);
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 Owen-Reynolds · Jun 15, 2013 at 12:50 AM 0
Share

That would compute "interpolatedRay" in local camera coords. In other words, it should give the coords if the model was a child of the camera. It's pretty much the same as ScreenPos, except everything is in meters.

I would imagine that fog only really wants that Z from the camera (the built-in fog just fakes it from the 0 to 1 depth bit after $$anonymous$$VP.)

avatar image MaT227 · Jun 15, 2013 at 06:44 AM 0
Share

Ok, then I should use _Object2World to have the object matrix and using the _WorldSpaceCameraPos.z ?

avatar image Owen-Reynolds · Jun 15, 2013 at 03:47 PM 1
Share

I think I just got the point of the shader. It runs post, on the screen texture. So it wants to take into account the distance from the camera, and the world Y. It back-computes world Y using a ray from the known world-coords on the camera-plane.

The math looks roughly correct. to me. But shaders are shaders. There's got to be a better non-Unity source for height-based fog.

avatar image MaT227 · Jun 16, 2013 at 10:39 AM 0
Share

Yes you're right, this shader was a camera shader which I tried to convert into a mesh shader. If you have some good resources concerning fog I will be glad to look at them. I created another post with a simple depth shader and there a strange behaviour if you have some time to look at it.

Anyways thanks for your help !

avatar image
0

Answer by Owen-Reynolds · Jun 14, 2013 at 02:02 PM

The fog (gradient?) is supposed to change based on the camera's position, at least as far as near/far. But do you mean that there's an up/down gradient, which moves with the camera and shouldn't? If so, I'd think adjusting o.screenPos to use world y instead of local camera-y might do something.

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 MaT227 · Jun 14, 2013 at 02:25 PM 0
Share

Sorry, yes by gradient I mean fog... Yes the fog follows the camera up/down then, I can't have a plunge view of the scene.

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

15 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

Related Questions

Vertex and Fragment Shader worldNormal 1 Answer

Can a CG shader fail to work on hardware? 1 Answer

Why doesn't this vertex+fragment shader work? 0 Answers

Access to svPosition in fragment shader 1 Answer

Grabpass refraction masking 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