public Vector3 GetSunCoords() { Vector3 result; #region |Orbital Elements| // Mean anomaly to radians. float M_Rad = SunOrbitalElements.M * Mathf.Deg2Rad; #endregion #region |Eccentric Anomaly| // Compute eccentric anomaly. float E = SunOrbitalElements.M + Mathf.Rad2Deg * SunOrbitalElements.e * Mathf.Sin(M_Rad) * (1 + SunOrbitalElements.e * Mathf.Cos(M_Rad)); // Debug.Log(E); // Eccentric anomaly to radians. float E_Rad = Mathf.Deg2Rad * E; #endregion #region |Rectangular Coordinates| // Rectangular coordinates of the sun in the plane of the ecliptic. float xv = (Mathf.Cos(E_Rad) - SunOrbitalElements.e); //Debug.Log(xv); float yv = (Mathf.Sin(E_Rad) * Mathf.Sqrt(1 - SunOrbitalElements.e * SunOrbitalElements.e)); // Debug.Log(yv); // Convert to distance and true anomaly(r = radians, v = degrees). float r = Mathf.Sqrt(xv * xv + yv * yv); //Debug.Log(r); float v = Mathf.Rad2Deg * Mathf.Atan2(yv, xv); //Debug.Log(v); // Get sun distance. m_SunDistance = r; #endregion #region |True Longitude| // True sun longitude. float lonsun = v + SunOrbitalElements.w; // Rev sun longitude lonsun = CSky_Mathf.Rev(lonsun); // Debug.Log(lonsun); // True sun longitude to radians. float lonsun_Rad = Mathf.Deg2Rad * lonsun; // Set true sun longitude(radians) for use in others celestials calculations. m_TrueSunLongitude = lonsun_Rad; #endregion #region |Ecliptic And Equatorial Coordinates| // Ecliptic rectangular coordinates(radians): float xs = r * Mathf.Cos(lonsun_Rad); float ys = r * Mathf.Sin(lonsun_Rad); // Ecliptic rectangular coordinates rotate these to equatorial coordinates(radians). float oblecl_Cos = Mathf.Cos(Oblecl); float oblecl_Sin = Mathf.Sin(Oblecl); float xe = xs; float ye = ys * oblecl_Cos - 0.0f * oblecl_Sin; float ze = ys * oblecl_Sin + 0.0f * oblecl_Cos; #endregion #region |Ascension And Declination| // Right ascension(degrees): float RA = Mathf.Rad2Deg * Mathf.Atan2(ye, xe) / 15; // Declination(radians). float Decl = Mathf.Atan2(ze, Mathf.Sqrt(xe * xe + ye * ye)); #endregion #region |Mean Longitude| // Mean sun longitude(degrees). float L = SunOrbitalElements.w + SunOrbitalElements.M; // Rev mean sun longitude. L = CSky_Mathf.Rev(L); // Set mean sun longitude for use in other celestials calculations. m_MeanSunLongitude = L; #endregion #region Sideral Time. // Sideral time(degrees). float GMST0 = /*(L + 180) / 15;*/ ((L / 15) + 12); m_SideralTime = GMST0 + Hour_UTC_Apply + m_Longitude / 15; m_LST = GMST0 + Hour_UTC_Apply + Mathf.Deg2Rad * m_Longitude / 15; m_LST *= 15; // Hour angle(degrees). float HA = (m_SideralTime - RA) * 15; //Debug.Log(HA); // Hour angle in radians. float HA_Rad = Mathf.Deg2Rad * HA; #endregion #region |Hour Angle And Declination In Rectangular Coordinates| // HA anf Decl in rectangular coordinates(radians). float Decl_Cos = Mathf.Cos(Decl); // X axis points to the celestial equator in the south. float x = Mathf.Cos(HA_Rad) * Decl_Cos; // Y axis points to the horizon in the west. float y = Mathf.Sin(HA_Rad) * Decl_Cos; // Z axis points to the north celestial pole. float z = Mathf.Sin(Decl); // Rotate the rectangualar coordinates system along of the Y axis(radians). float sinLatitude = Mathf.Sin(Latitude_Rad); float cosLatitude = Mathf.Cos(Latitude_Rad); float xhor = x * sinLatitude - z * cosLatitude; float yhor = y; float zhor = x * cosLatitude + z * sinLatitude; #endregion #region Azimuth, Altitude And Zenith[Radians]. result.x = Mathf.Atan2(yhor, xhor) + Mathf.PI; result.y = Mathf.Asin(zhor); result.z = CSky_Mathf.k_HalfPI - result.y; #endregion //Debug.Log(result.x * Mathf.Rad2Deg); //Debug.Log(result.y * Mathf.Rad2Deg); return(result); // Return coordinates in radians. }
public Vector3 GetMoonCoords() { Vector3 result; #region |Orbital Elements| // Orbital elements in radians. float N_Rad = Mathf.Deg2Rad * MoonOrbitalElements.N; float i_Rad = Mathf.Deg2Rad * MoonOrbitalElements.i; float M_Rad = Mathf.Deg2Rad * MoonOrbitalElements.M; #endregion #region |Eccentric Anomaly| // Compute eccentric anomaly. float E = MoonOrbitalElements.M + Mathf.Rad2Deg * MoonOrbitalElements.e * Mathf.Sin(M_Rad) * (1 + MoonOrbitalElements.e * Mathf.Cos(M_Rad)); // Eccentric anomaly to radians. float E_Rad = Mathf.Deg2Rad * E; #endregion #region |Rectangular Coordinates| // Rectangular coordinates of the sun in the plane of the ecliptic. float xv = MoonOrbitalElements.a * (Mathf.Cos(E_Rad) - MoonOrbitalElements.e); // Debug.Log(xv); float yv = MoonOrbitalElements.a * (Mathf.Sin(E_Rad) * Mathf.Sqrt(1 - MoonOrbitalElements.e * MoonOrbitalElements.e)) * Mathf.Sin(E_Rad); // Debug.Log(yv); // Convert to distance and true anomaly(r = radians, v = degrees). float r = Mathf.Sqrt(xv * xv + yv * yv); // Debug.Log(r); float v = Mathf.Rad2Deg * Mathf.Atan2(yv, xv); // Debug.Log(v); v = CSky_Mathf.Rev(v); // Longitude in radians. float l = Mathf.Deg2Rad * (v + MoonOrbitalElements.w); float Cos_l = Mathf.Cos(l); float Sin_l = Mathf.Sin(l); float Cos_N_Rad = Mathf.Cos(N_Rad); float Sin_N_Rad = Mathf.Sin(N_Rad); float Cos_i_Rad = Mathf.Cos(i_Rad); float xeclip = r * (Cos_N_Rad * Cos_l - Sin_N_Rad * Sin_l * Cos_i_Rad); float yeclip = r * (Sin_N_Rad * Cos_l + Cos_N_Rad * Sin_l * Cos_i_Rad); float zeclip = r * (Sin_l * Mathf.Sin(i_Rad)); #endregion #region Geocentric Coordinates. // Geocentric position for the moon and Heliocentric position for the planets. float lonecl = Mathf.Rad2Deg * Mathf.Atan2(yeclip, xeclip); // Rev lonecl lonecl = CSky_Mathf.Rev(lonecl); // Debug.Log(lonecl); float latecl = Mathf.Rad2Deg * Mathf.Atan2(zeclip, Mathf.Sqrt(xeclip * xeclip + yeclip * yeclip)); // Debug.Log(latecl); // Get true sun longitude. // float lonSun = m_TrueSunLongitude; // Ecliptic longitude and latitude in radians. float lonecl_Rad = Mathf.Deg2Rad * lonecl; float latecl_Rad = Mathf.Deg2Rad * latecl; float nr = 1.0f; float xh = nr * Mathf.Cos(lonecl_Rad) * Mathf.Cos(latecl_Rad); float yh = nr * Mathf.Sin(lonecl_Rad) * Mathf.Cos(latecl_Rad); float zh = nr * Mathf.Sin(latecl_Rad); // Geocentric posisition. float xs = 0.0f; float ys = 0.0f; // Convert the geocentric position to heliocentric position. float xg = xh + xs; float yg = yh + ys; float zg = zh; #endregion #region |Equatorial Coordinates| // Convert xg, yg in equatorial coordinates. float oblecl_Cos = Mathf.Cos(Oblecl); float oblecl_Sin = Mathf.Sin(Oblecl); float xe = xg; float ye = yg * oblecl_Cos - zg * oblecl_Sin; float ze = yg * oblecl_Sin + zg * oblecl_Cos; #endregion #region |Ascension, Declination And Hour Angle| // Right ascension. float RA = Mathf.Rad2Deg * Mathf.Atan2(ye, xe); //Debug.Log(RA); // Normalize right ascension. RA = CSky_Mathf.Rev(RA); //Debug.Log(RA); // Declination. float Decl = Mathf.Rad2Deg * Mathf.Atan2(ze, Mathf.Sqrt(xe * xe + ye * ye)); // Declination in radians. float Decl_Rad = Mathf.Deg2Rad * Decl; // Hour angle. float HA = ((m_SideralTime * 15) - RA); //Debug.Log(HA); // Rev hour angle. HA = CSky_Mathf.Rev(HA); //Debug.Log(HA); // Hour angle in radians. float HA_Rad = Mathf.Deg2Rad * HA; #endregion #region |Declination in rectangular coordinates| // HA y Decl in rectangular coordinates. float Decl_Cos = Mathf.Cos(Decl_Rad); float xr = Mathf.Cos(HA_Rad) * Decl_Cos; float yr = Mathf.Sin(HA_Rad) * Decl_Cos; float zr = Mathf.Sin(Decl_Rad); // Rotate the rectangualar coordinates system along of the Y axis(radians). float sinLatitude = Mathf.Sin(Latitude_Rad); float cosLatitude = Mathf.Cos(Latitude_Rad); float xhor = xr * sinLatitude - zr * cosLatitude; float yhor = yr; float zhor = xr * cosLatitude + zr * sinLatitude; #endregion #region |Azimuth, Altitude And Zenith[Radians]| result.x = Mathf.Atan2(yhor, xhor) + Mathf.PI; result.y = Mathf.Asin(zhor); result.z = CSky_Mathf.k_HalfPI - result.y; #endregion // Return coordinates in radians. return(result); }
void ComputePreethamAtmosphere() { #region |Calculations| // Wavelengths. Vector3 lambda = new Vector3() { x = m_WavelengthR * 1e-9f, y = m_WavelengthG * 1e-9f, z = m_WavelengthB * 1e-9f }; Vector3 wavelength = new Vector3() { x = Mathf.Pow(lambda.x, 4.0f), y = Mathf.Pow(lambda.y, 4.0f), z = Mathf.Pow(lambda.z, 4.0f) }; // constant factors. float n = 1.0003f; // Index of air refraction(n); float N = 2.545e25f; // Molecular density(N) float pn = 0.035f; // Depolatization factor for standart air. float n2 = n * n; // Molecular density exponentially squared. // Beta Rayleigh float ray = (8.0f * Mathf.Pow(Mathf.PI, 3.0f) * Mathf.Pow(n2 - 1.0f, 2.0f) * (6.0f + 3.0f * pn)); Vector3 theta = 3.0f * N * wavelength * (6.0f - 7.0f * pn); Vector3 BetaRay = new Vector3() { x = (ray / theta.x) * m_AtmosphereThickness * 0.5f, y = (ray / theta.y) * m_AtmosphereThickness * 0.5f, z = (ray / theta.z) * m_AtmosphereThickness * 0.5f }; // Beta Mie. Vector3 k = new Vector3(0.685f, 0.679f, 0.670f); float c = (0.2f * m_Turbidity) * 10e-18f; float mieFactor = 0.434f * c * Mathf.PI; const float v = 4.0f; float mie = (m_Mie * 1e+1f); // Adjust. Vector3 BetaMie = new Vector3() { x = (mieFactor * Mathf.Pow((2.0f * Mathf.PI) / lambda.x, v - 2.0f) * k.x) * mie, y = (mieFactor * Mathf.Pow((2.0f * Mathf.PI) / lambda.y, v - 2.0f) * k.y) * mie, z = (mieFactor * Mathf.Pow((2.0f * Mathf.PI) / lambda.z, v - 2.0f) * k.z) * mie }; #endregion #region |Set Global Params| Vector3 fadeParams; fadeParams.x = DayIntensity; // Day intensity. fadeParams.y = NightIntensity; // Night intensity. // Combined extinction factor fade(sunset/dawn color). fadeParams.z = CSky_Mathf.Saturate(Mathf.Clamp01(1.0f - SunDirection.y)); Shader.SetGlobalVector("CSky_BetaRay", BetaRay); Shader.SetGlobalVector("CSky_BetaMie", BetaMie); Shader.SetGlobalFloat("CSky_SunE", (m_SunBrightness * 3.0f) * EclipseMultiplier); Shader.SetGlobalVector("CSky_FadeParams", fadeParams); Shader.SetGlobalFloat("CSky_RayleighZenithLength", m_RayleighZenithLength); Shader.SetGlobalFloat("CSky_MieZenithLength", m_MieZenithLength); #endregion }