Search Unity

Question [Help Wanted] Issue with custom skybox shader, sun placement.

Discussion in 'Universal Render Pipeline' started by TyroByte, Jul 11, 2021.

  1. TyroByte

    TyroByte

    Joined:
    Mar 16, 2017
    Posts:
    25
    Greetings.

    I was working on this gradient skybox shader with a simple sun for URP when I noticed that while it works, the placement of the sun is very wonky. Moving it around, you can see that it elongates sometimes and is a perfect circle in other positions.

    Additionally, I would also like to tie it to the rotation of the directional light like in the procedural shader. How would I go about doing that? Shader Code is posted below.

    Code (CSharp):
    1. Shader "Custom/GradientSkybox" {
    2.  
    3.     Properties {
    4.         _Color2 ("Top Color", Color) = (0.97, 0.67, 0.51, 0)
    5.         _Color1 ("Bottom Color", Color) = (0, 0.7, 0.74, 0)
    6.  
    7.         [Space]
    8.         _Intensity ("Intensity", Range (0, 2)) = 1.0
    9.         _Exponent ("Exponent", Range (0, 3)) = 1.0
    10.  
    11.         [Space]
    12.         _DirectionYaw ("Direction X angle", Range (0, 180)) = 0
    13.         _DirectionPitch ("Direction Y angle", Range (0, 180)) = 0
    14.  
    15.        
    16.         _SunPosition("Sun Position", Vector) = (0.0, 0.0, 1.0)
    17.  
    18.        [HDR] _SunColor("Sun Color", Color) = (1.0, 1.0, 1.0, 1.0)
    19.         _SunSize("Sun Size", Range(0, 1)) = 0.04
    20.         _SunHardness("Sun Hardness", Float) = 0.1
    21.         _SunGradient("Sun Gradient", Range(0, 1)) = 0.0
    22.        
    23.         [HideInInspector]
    24.         _Direction ("Direction", Vector) = (0, 1, 0, 0)
    25.     }
    26.  
    27.     CGINCLUDE
    28.  
    29.     #include "Lighting.cginc"
    30.     #include "UnityCG.cginc"
    31.  
    32.     uniform half3 _SunPosition, _SunColor;
    33.     uniform half _SunSize, _SunHardness;
    34.  
    35.     #define HARDNESS_EXPONENT_BASE 0.125
    36.  
    37.     struct appdata {
    38.         float4 position : POSITION;
    39.         float3 texcoord : TEXCOORD0;
    40.     };
    41.    
    42.     struct v2f {
    43.         float4 position : SV_POSITION;
    44.         float3 texcoord : TEXCOORD0;
    45.     };
    46.  
    47.     half3 calcSunSpot(half3 sunDirPos, half3 skyDirPos)
    48.     {
    49.         half3 delta = sunDirPos - skyDirPos;
    50.         half dist = length(delta);
    51.         half spot = 1.0 - smoothstep(0.0, _SunSize, dist);
    52.         return 1.0 - pow(HARDNESS_EXPONENT_BASE, spot * _SunHardness);
    53.     }
    54.  
    55.    
    56.     half4 _Color1;
    57.     half4 _Color2;
    58.     half3 _Direction;
    59.     half _Intensity;
    60.     half _Exponent;
    61.    
    62.     v2f vert (appdata v) {
    63.         v2f o;
    64.         o.position = UnityObjectToClipPos(v.position);
    65.         o.texcoord = v.texcoord;
    66.         return o;
    67.     }
    68.    
    69.     //changed this from fixed4 to half4
    70.     half4 frag (v2f i) : COLOR {
    71.         half d = dot(normalize(i.texcoord), _Direction) * 0.5f + 0.5f;
    72.  
    73.         half3 mie = calcSunSpot(_SunPosition.xyz, i.texcoord.xyz) * _SunColor;
    74.  
    75.        
    76.         //sun placement is wonky for some reason here.
    77.         //perfect circle in some positions and elongated in others.
    78.  
    79.         half3 col = lerp (_Color1, _Color2, pow(d, _Exponent)) * _Intensity + mie;
    80.  
    81.         return half4(col,1.0);
    82.     }
    83.  
    84.     ENDCG
    85.  
    86.     SubShader {
    87.         Tags { "RenderType"="Background" "Queue"="Background" }
    88.  
    89.         Pass {
    90.             ZWrite Off
    91.             Cull Off
    92.             Fog { Mode Off }
    93.             CGPROGRAM
    94.             #pragma fragmentoption ARB_precision_hint_fastest
    95.             #pragma vertex vert
    96.             #pragma fragment frag
    97.             ENDCG
    98.         }
    99.     }
    100.  
    101.     CustomEditor "GradientSkyboxEditor"
    102. }