Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Sun disc position for physically based sky

Discussion in 'High Definition Render Pipeline' started by cloverme, Nov 2, 2022.

  1. cloverme

    cloverme

    Joined:
    Apr 6, 2018
    Posts:
    193
    I'm working on determining shadow positioning from a directional light. Is there a way to get the position of the sun disc in the physically based sky?
     
    impheris likes this.
  2. HIBIKI_entertainment

    HIBIKI_entertainment

    Joined:
    Dec 4, 2018
    Posts:
    594

    Altitude ° = X°
    Azimuth ° = Y°

    +Z is Cardinal North by default so Azimuth is flipped, but you flip either if you need it more accurate.
     
  3. cloverme

    cloverme

    Joined:
    Apr 6, 2018
    Posts:
    193
    Here's the code if anyone else is looking for it. You can use it to roughly determine the position of the Sun Disc that Unity generates when you select "Affect Physically Based Sky". In this case, place the script on the directional light for the scene.



    Code (CSharp):
    1.  
    2. public class PlaceObjectOnSun : MonoBehaviour
    3.  
    4. {
    5.     public Transform sun; // The object representing the simulated sun
    6.     public float horizonDistance = 5000f; // Distance from the center of the world to the horizon
    7.     public float zenithHeight = 5000f; // Height of the zenith above the center of the world
    8.     public float azimuthDegrees; // Azimuth angle in degrees
    9.     public float altitudeDegrees; // Altitude angle in degrees
    10.     public Vector3 centerOfWorld = Vector3.zero; // Center of the world
    11.  
    12.     private void Start()
    13.     {
    14.         azimuthDegrees = ((WrapAngle(transform.localEulerAngles.y) + 90f)) * -1.0f;
    15.         altitudeDegrees = ((WrapAngle(transform.localEulerAngles.x) - 90f)) * -1.0f;
    16.         calcSunPostion();
    17.     }
    18.  
    19.  
    20.     void calcSunPostion()
    21.     {
    22.         // Convert the Altitude and Azimuth angles to radians
    23.         float altitudeRad = altitudeDegrees * Mathf.Deg2Rad;
    24.         float azimuthRad = azimuthDegrees * Mathf.Deg2Rad;
    25.  
    26.         // Calculate the position of the sun in the sky
    27.         float x = horizonDistance * Mathf.Sin(altitudeRad) * Mathf.Cos(azimuthRad);
    28.         float y = zenithHeight * Mathf.Cos(altitudeRad);
    29.         float z = horizonDistance * Mathf.Sin(altitudeRad) * Mathf.Sin(azimuthRad);
    30.  
    31.         // Place the sun in the sky
    32.         sun.position = centerOfWorld + new Vector3(x, y, z);
    33.     }
    34.  
    35.     private float WrapAngle(float angle)
    36.     {
    37.         angle %= 360;
    38.         if (angle > 180)
    39.             return angle - 360;
    40.  
    41.         return angle;
    42.     }
    43.  
    44. }
    45.