6 sided Skybox and Sun

Hello,

I’ve got a big problem in my hands, Im using a 6 sided Skybox on my project, its a Space ship game, but unlike the procedural skyboxes, the 6 sided dont render a “Sun” based in your Directional Light. This became a major problem to me, I dont need a full day cycle, but I definitely need a static sun in the scene because the effects Im using cant be achived using a Game Object as a Sun in the scene…

Im not familiar with shaders coding and couldnt find any posts or help about this particular subject, adding a dynamic sun to 6 sided skybox shaders. Someone told me the only way to do this is write my own shader, so my question is, is it actually possible do this? I just cant find the answer anywhere I look.

Thanks

Did you know about lens flares?
Create a simple circle white texture with black background and use it as 1 texture lens flare type.

Oh I know, the problem with lens flare is that there is no ‘transition’ between it being visible or invisible behind an object, like a planet. If I start flying the ship to an angle where the sun should raising in the horizon of a planet, Im still unable to see it. The “sun” (flare) wont show, not until there is enough angle where you can see the flare entirely, then it will simply pop up in the screen. You either see ir or dont see it, there is no “half” the flare effect, for example. Thats the big problem using it to siulate a Sun

Well, there’s 2 ways I see of doing it. Either one; use a custom lens flare effect that samples multiple points for intensity, or two; do exactly what you said and combine the skybox shaders. If it is just the sun you need, you can always fake it by using dot products. The following example is a copy of the old six sided skybox shader with a sun overlay;

Shader "Skybox/6 Sided With Sun" {
Properties {
    _Tint ("Tint Color", Color) = (.5, .5, .5, .5)
    _Sun ("Sun Size", Float) = 2.5
    _SunPow ("Sun Intensity", Float) = 4.0
    [Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0
    _Rotation ("Rotation", Range(0, 360)) = 0
    [NoScaleOffset] _FrontTex ("Front [+Z]   (HDR)", 2D) = "grey" {}
    [NoScaleOffset] _BackTex ("Back [-Z]   (HDR)", 2D) = "grey" {}
    [NoScaleOffset] _LeftTex ("Left [+X]   (HDR)", 2D) = "grey" {}
    [NoScaleOffset] _RightTex ("Right [-X]   (HDR)", 2D) = "grey" {}
    [NoScaleOffset] _UpTex ("Up [+Y]   (HDR)", 2D) = "grey" {}
    [NoScaleOffset] _DownTex ("Down [-Y]   (HDR)", 2D) = "grey" {}
}

SubShader {
    Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
    Cull Off ZWrite Off
   
    CGINCLUDE
    #include "UnityCG.cginc"
    #include "Lighting.cginc"

    half4 _Tint;
    half _Sun;
    half _SunPow;
    half _Exposure;
    float _Rotation;

    float3 RotateAroundYInDegrees (float3 vertex, float degrees)
    {
        float alpha = degrees * UNITY_PI / 180.0;
        float sina, cosa;
        sincos(alpha, sina, cosa);
        float2x2 m = float2x2(cosa, -sina, sina, cosa);
        return float3(mul(m, vertex.xz), vertex.y).xzy;
    }
   
    struct appdata_t {
        float4 vertex : POSITION;
        float2 texcoord : TEXCOORD0;
        UNITY_VERTEX_INPUT_INSTANCE_ID
    };
    struct v2f {
        float4 vertex : SV_POSITION;
        float2 texcoord : TEXCOORD0;
        float3 viewDir : TEXCOORD1;
        UNITY_VERTEX_OUTPUT_STEREO
    };
    v2f vert (appdata_t v)
    {
        v2f o;
        UNITY_SETUP_INSTANCE_ID(v);
        UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
        float3 rotated = RotateAroundYInDegrees(v.vertex, _Rotation);
        o.vertex = UnityObjectToClipPos(rotated);
        o.texcoord = v.texcoord;
        o.viewDir = WorldSpaceViewDir (v.vertex);
        return o;
    }
    half4 skybox_frag (v2f i, sampler2D smp, half4 smpDecode)
    {
        half4 tex = tex2D (smp, i.texcoord);
        half3 c = DecodeHDR (tex, smpDecode);
        c = c * _Tint.rgb * unity_ColorSpaceDouble.rgb;
        c *= _Exposure;
        half spec = pow (max (0, dot (normalize (i.viewDir), -_WorldSpaceLightPos0)), pow (_Sun, 8)) * _SunPow;
        return half4(c + _LightColor0 * spec, 1);
    }
    ENDCG
   
    Pass {
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #pragma target 2.0
        sampler2D _FrontTex;
        half4 _FrontTex_HDR;
        half4 frag (v2f i) : SV_Target { return skybox_frag(i,_FrontTex, _FrontTex_HDR); }
        ENDCG
    }
    Pass{
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #pragma target 2.0
        sampler2D _BackTex;
        half4 _BackTex_HDR;
        half4 frag (v2f i) : SV_Target { return skybox_frag(i,_BackTex, _BackTex_HDR); }
        ENDCG
    }
    Pass{
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #pragma target 2.0
        sampler2D _LeftTex;
        half4 _LeftTex_HDR;
        half4 frag (v2f i) : SV_Target { return skybox_frag(i,_LeftTex, _LeftTex_HDR); }
        ENDCG
    }
    Pass{
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #pragma target 2.0
        sampler2D _RightTex;
        half4 _RightTex_HDR;
        half4 frag (v2f i) : SV_Target { return skybox_frag(i,_RightTex, _RightTex_HDR); }
        ENDCG
    }   
    Pass{
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #pragma target 2.0
        sampler2D _UpTex;
        half4 _UpTex_HDR;
        half4 frag (v2f i) : SV_Target { return skybox_frag(i,_UpTex, _UpTex_HDR); }
        ENDCG
    }   
    Pass{
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #pragma target 2.0
        sampler2D _DownTex;
        half4 _DownTex_HDR;
        half4 frag (v2f i) : SV_Target { return skybox_frag(i,_DownTex, _DownTex_HDR); }
        ENDCG
    }
}
}

Just keep in mind that the larger the “Sun Size” property, the smaller the sun is.

3 Likes