25.7 C
New York
Sunday, July 20, 2025

How can I add a static halo texture round a sky sphere in a Unity shader?


I am making an attempt to create a easy sky shader for my recreation in Unity.

I made a primary sphere within the sky whose place is managed by way of Shader.SetGlobalVector("_SphereDir", _sphere.rework.ahead);.

I need to add a halo round it, and I would like one thing easy and static that does not have an effect on efficiency since I am concentrating on cellular gadgets.

So as a substitute of making a procedural halo, I considered utilizing a easy texture, and I designed this halo texture.

I attempted putting the feel on the identical place because the sphere, however I couldn’t get it to work in any respect, Is there a approach to try this?

Shader Code:

Shader "Skybox Sphere"
{
    Properties
    {
        _SkyColor ("Sky Coloration", Coloration) = (0.5, 0.7, 1, 1)

        _SphereRadius ("Sphere Radius", Vary(0,1)) = 0.05
        _SphereTint ("Sphere Tint", Coloration) = (1, 1, 1, 1)
    }
    SubShader
    {
        Tags { "Queue" = "Background" "RenderType" = "Opaque" }
        Cull Off
        ZWrite Off
        ZTest At all times

        Go
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma goal 2.0
            #embody "UnityCG.cginc"

            fixed4 _SkyColor;

            half3 _SphereDir;
            half _SphereRadius;
            fixed4 _SphereTint;

            struct appdata_t
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4  pos             : SV_POSITION;
                float3  vertex          : TEXCOORD0;
            };

            v2f vert (appdata_t v)
            {
                v2f OUT;
                OUT.pos = UnityObjectToClipPos(v.vertex);

                float3 eyeRay = normalize(mul((float3x3)unity_ObjectToWorld, v.vertex.xyz));
                OUT.vertex = -eyeRay;

                return OUT;
            }

            // From Inigo Quilez, https://iquilezles.org/articles/intersectors/
            float sphIntersect(float3 rayDir, float3 spherePos, float radius)
            {
                float3 oc = -spherePos;
                float b = dot(oc, rayDir);
                float c = dot(oc, oc) - radius * radius;
                float h = b * b - c;

                if(h < 0.0) return -1.0;

                h = sqrt(h);
                float t = -b - h;

                if(t < 0.0) return -1.0;

                return t;
            }

            fixed4 frag (v2f IN) : SV_Target
            {
                float3 viewDir = normalize(IN.vertex.xyz);

                float SphereIntersect = sphIntersect(viewDir, _SphereDir, _SphereRadius);
                float SphereMask = SphereIntersect > -1 ? 1 : 0;
            
                float3 col = _SkyColor + SphereMask * _SphereTint.rgb;

                return float4(col, 0);
            }
            ENDCG
        }
    }
    FallBack Off
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles