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 t5impact · Sep 16, 2020 at 07:59 PM · glsl

How to mitigate weird shader anomaly?

Thank you to anyone in advance for helping out.

I have been experiencing something that i can only describe as an anomaly that has to do with a shader I have been working on. I have this shader that displays a sphere inside an object of a radius I define. The transparency of the sphere is determined by how far the view ray travels through the sphere. Now, everything works perfectly as expected when the camera is inside the object or fairly distant from it, but whenever the camera gets close to the surface of the object, the sphere starts to contort and sorta oscillate.

The anomaly is seen in the game view of the screenshotalt text

Here is the shader code for anyone interested

 Shader "Atmosphere/Scattering"
 {
     Properties
     {
         _OpticalDepthTexture("Optical Depth Texture", 2D) = "white" {}
         _PlanetPosition("PlanetPosition", Vector) = (0,0,0)
         _SunDirection("SunDirection", Vector) = (0,1,0)
         _AtmosphereRadius("Atmosphere Radius", Float) = 10
     }
     
     SubShader
     {
         Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
         LOD 100
 
         ZWrite Off
         Cull Off
 
         Blend SrcAlpha OneMinusSrcAlpha
 
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
             #define EPSILON 0.01
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 float4 vertex : SV_POSITION;
                 float3 viewDir : TEXCOORD1;
                 float3 wVert : TEXCOORD2;
             };
 
             sampler2D _OpticalDepthTexture;
             float4 _OpticalDepthTexture_ST;
 
             float3 _PlanetPosition;
             float3 _SunDirection;
             float _AtmosphereRadius;
 
             float sqrLength(float3 v) {
                 return v.x * v.x + v.y * v.y + v.z * v.z;
             }
 
             bool sphereIntersect(float3 ro, float3 rd, float radius, inout float2 times) {
                 times = float2(0, 0);
 
                 float t = dot(_PlanetPosition - ro, rd);
                 float3 p = ro + rd * t;
                 float y = length(p - _PlanetPosition);
 
                 if (y > _AtmosphereRadius) return false;
 
                 float x = sqrt(_AtmosphereRadius * _AtmosphereRadius - y * y);
 
                 if (t + x > 0 && t - x < 0)
                     times.x = 0;
                 else
                     times.x = t - x;
 
                 times.y = t + x < 0 ? 0 : (t + x) - (times.x);
 
                 return true;
             }
 
             v2f vert (appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 float3 worldPos = mul(unity_ObjectToWorld, v.vertex);
                 o.viewDir = normalize(worldPos - _WorldSpaceCameraPos);
                 o.wVert = worldPos;
                 return o;
             }
 
             fixed4 frag(v2f i) : SV_Target
             {
                 float2 times;
 
                 float3 startPos = _WorldSpaceCameraPos;
 
                 if (!sphereIntersect(_WorldSpaceCameraPos, i.viewDir, _AtmosphereRadius, times)) return 0;
 
                 float dstThroughAtmosphere = (times.y) / (_AtmosphereRadius * 2);
 
                 return dstThroughAtmosphere;
             }
             ENDCG
         }
     }
 }

My only guess as to why this occurs is maybe something do to with precision but I have no idea how to go about mitigating the issue.

Thanks again for any help.

weird-shader-anomaly.png (402.1 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

133 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 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 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 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 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

Shader problem with alpha blend and zwrite iOS 1 Answer

Get absolute position of vertex in shader 1 Answer

GLSL link error: Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (261) 1 Answer

What does the unity_Scale shader uniform represent? 1 Answer

shaderlab: Matrix-vector multiplication in fragment shader on iOS not working? 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