- Home /
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 screenshot
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613011850im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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