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 chva1582 · Nov 19, 2015 at 09:14 PM · shaderatmosphere

Smoothing Normals in Atmospheric Shader

I have a scene in which I am attempting to render a somewhat photorealistic view of Earth from orbit. The shaders I am using are coming from Scrawk Blog. The shader for the ground appears to be functioning as expected however the one for volumetric rendering of the atmosphere I am running into some difficulties. It would appear to me that my normals are incorrect but I truly have no experience with this. It looks, at its worst, like this...

alt text

Any ideas as to how to fix this or at least mask the error? I have tried a higher poly mesh and it does look better but still has limitations I have tried blurring but its still obvious. Or perhaps I should switch to a seperate shader all together?

 Shader "Atmosphere/SkyFromSpace" 
 {
     SubShader 
     {
         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
         //Tags { "RenderType"="Opaque" }
         Pass 
         {
             
             Blend SrcAlpha OneMinusSrcAlpha 
             ZWrite Off
             Cull Front
             //Blend One One
         
             CGPROGRAM
             #include "UnityCG.cginc"
             #pragma target 3.0
             #pragma vertex vert
             #pragma fragment frag
             
             uniform float3 v3Translate;        // The objects world pos
             uniform float3 v3LightPos;        // The direction vector to the light source
             uniform float3 v3InvWavelength; // 1 / pow(wavelength, 4) for the red, green, and blue channels
             uniform float fOuterRadius;        // The outer (atmosphere) radius
             uniform float fOuterRadius2;    // fOuterRadius^2
             uniform float fInnerRadius;        // The inner (planetary) radius
             uniform float fInnerRadius2;    // fInnerRadius^2
             uniform float fKrESun;            // Kr * ESun
             uniform float fKmESun;            // Km * ESun
             uniform float fKr4PI;            // Kr * 4 * PI
             uniform float fKm4PI;            // Km * 4 * PI
             uniform float fScale;            // 1 / (fOuterRadius - fInnerRadius)
             uniform float fScaleDepth;        // The scale depth (i.e. the altitude at which the atmosphere's average density is found)
             uniform float fScaleOverScaleDepth;    // fScale / fScaleDepth
             uniform float fHdrExposure;        // HDR exposure
             uniform float g;                // The Mie phase asymmetry factor
             uniform float g2;                // The Mie phase asymmetry factor squared
         
             struct v2f 
             {
                 float4  pos : SV_POSITION;
                 float2  uv : TEXCOORD0;
                 float3 t0 : TEXCOORD1;
                 float3 c0 : COLOR0;
                 float3 c1 : COLOR1;
             };
             
             float scale(float fCos)
             {
                 float x = 1.0 - fCos;
                 return 0.25 * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));
             }
             
 
             v2f vert(appdata_base v)
             {
             
                 float3 v3CameraPos = _WorldSpaceCameraPos - v3Translate;    // The camera's current position
                 float fCameraHeight = length(v3CameraPos);                    // The camera's current height
                 float fCameraHeight2 = fCameraHeight*fCameraHeight;            // fCameraHeight^2
             
                 // Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
                 float3 v3Pos = mul(_Object2World, v.vertex).xyz - v3Translate;
                 float3 v3Ray = v3Pos - v3CameraPos;
                 float fFar = length(v3Ray);
                 v3Ray /= fFar;
                 
                 // Calculate the closest intersection of the ray with the outer atmosphere (which is the near point of the ray passing through the atmosphere)
                 float B = 2.0 * dot(v3CameraPos, v3Ray);
                 float C = fCameraHeight2 - fOuterRadius2;
                 float fDet = max(0.0, B*B - 4.0 * C);
                 float fNear = 0.5 * (-B - sqrt(fDet));
                 
                 // Calculate the ray's start and end positions in the atmosphere, then calculate its scattering offset
                 float3 v3Start = v3CameraPos + v3Ray * fNear;
                 fFar -= fNear;
                 float fStartAngle = dot(v3Ray, v3Start) / fOuterRadius;
                 float fStartDepth = exp(-1.0/fScaleDepth);
                 float fStartOffset = fStartDepth*scale(fStartAngle);
                 
                 const float fSamples = 2.0;
             
                 // Initialize the scattering loop variables
                 float fSampleLength = fFar / fSamples;
                 float fScaledLength = fSampleLength * fScale;
                 float3 v3SampleRay = v3Ray * fSampleLength;
                 float3 v3SamplePoint = v3Start + v3SampleRay * 0.5;
             
                 // Now loop through the sample rays
                 float3 v3FrontColor = float3(0.0, 0.0, 0.0);
                 for(int i=0; i<int(fSamples); i++)
                 {
                     float fHeight = length(v3SamplePoint);
                     float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fHeight));
                     float fLightAngle = dot(v3LightPos, v3SamplePoint) / fHeight;
                     float fCameraAngle = dot(v3Ray, v3SamplePoint) / fHeight;
                     float fScatter = (fStartOffset + fDepth*(scale(fLightAngle) - scale(fCameraAngle)));
                     float3 v3Attenuate = exp(-fScatter * (v3InvWavelength * fKr4PI + fKm4PI));
                     v3FrontColor += v3Attenuate * (fDepth * fScaledLength);
                     v3SamplePoint += v3SampleRay;
                 }
             
                 v2f OUT;
                 OUT.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                 OUT.uv = v.texcoord.xy;
                 
                 // Finally, scale the Mie and Rayleigh colors and set up the varying variables for the pixel shader
                 OUT.c0 = v3FrontColor * (v3InvWavelength * fKrESun);
                 OUT.c1 = v3FrontColor * fKmESun;
                 OUT.t0 = v3CameraPos - v3Pos;
                             
                 return OUT;
             }
             
             // Calculates the Mie phase function
             float getMiePhase(float fCos, float fCos2, float g, float g2)
             {
                 return 1.5 * ((1.0 - g2) / (2.0 + g2)) * (1.0 + fCos2) / pow(1.0 + g2 - 2.0*g*fCos, 1.5);
             }
 
             // Calculates the Rayleigh phase function
             float getRayleighPhase(float fCos2)
             {
                 return 0.75 + 0.75*fCos2;
             }
             
             half4 frag(v2f IN) : COLOR
             {
                 float fCos = dot(v3LightPos, IN.t0) / length(IN.t0);
                 float fCos2 = fCos*fCos;
                 float3 col = getRayleighPhase(fCos2) * IN.c0 + getMiePhase(fCos, fCos2, g, g2) * IN.c1;
                 
                 col = 1.0 - exp(col * -fHdrExposure);
                 
                 return float4(col,col.b);
             }
             
             ENDCG
 
         }
     }
 }

faultyshader.png (464.4 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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Atmospheric Scattering Shader Glitch on Floating Origin Late Update 0 Answers

Planet/Atmosphere shader problem. 0 Answers

Atmospheric Shader Help 1 Answer

Slindev Planet Shader 3.x Not Working 0 Answers

Slindev Planet Shader - reversed lighting 2 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