public SphericalHarmonicsL2 RenderToSphericalHarmonics() { SphericalHarmonicsL2 result = default(SphericalHarmonicsL2); bool directLight = false; Color color = TOD_Util.ChangeSaturation(this.AmbientColor.linear, this.Ambient.Saturation); Vector3 vector = new Vector3(0.612372458f, 0.5f, 0.612372458f); Vector3 up = Vector3.up; Color linear = this.SampleAtmosphere(up, directLight).linear; result.AddDirectionalLight(up, linear, 0.428571433f); Vector3 direction = new Vector3(-vector.x, vector.y, -vector.z); Color color2 = TOD_Util.ChangeSaturation(this.SampleAtmosphere(direction, directLight).linear, this.Ambient.Saturation); result.AddDirectionalLight(direction, color2, 0.2857143f); Vector3 direction2 = new Vector3(vector.x, vector.y, -vector.z); Color color3 = TOD_Util.ChangeSaturation(this.SampleAtmosphere(direction2, directLight).linear, this.Ambient.Saturation); result.AddDirectionalLight(direction2, color3, 0.2857143f); Vector3 direction3 = new Vector3(-vector.x, vector.y, vector.z); Color color4 = TOD_Util.ChangeSaturation(this.SampleAtmosphere(direction3, directLight).linear, this.Ambient.Saturation); result.AddDirectionalLight(direction3, color4, 0.2857143f); Vector3 direction4 = new Vector3(vector.x, vector.y, vector.z); Color color5 = TOD_Util.ChangeSaturation(this.SampleAtmosphere(direction4, directLight).linear, this.Ambient.Saturation); result.AddDirectionalLight(direction4, color5, 0.2857143f); Vector3 left = Vector3.left; Color color6 = TOD_Util.ChangeSaturation(this.SampleAtmosphere(left, directLight).linear, this.Ambient.Saturation); result.AddDirectionalLight(left, color6, 0.142857149f); Vector3 right = Vector3.right; Color color7 = TOD_Util.ChangeSaturation(this.SampleAtmosphere(right, directLight).linear, this.Ambient.Saturation); result.AddDirectionalLight(right, color7, 0.142857149f); Vector3 back = Vector3.back; Color color8 = TOD_Util.ChangeSaturation(this.SampleAtmosphere(back, directLight).linear, this.Ambient.Saturation); result.AddDirectionalLight(back, color8, 0.142857149f); Vector3 forward = Vector3.forward; Color color9 = TOD_Util.ChangeSaturation(this.SampleAtmosphere(forward, directLight).linear, this.Ambient.Saturation); result.AddDirectionalLight(forward, color9, 0.142857149f); Vector3 direction5 = new Vector3(-vector.x, -vector.y, -vector.z); result.AddDirectionalLight(direction5, color, 0.2857143f); Vector3 direction6 = new Vector3(vector.x, -vector.y, -vector.z); result.AddDirectionalLight(direction6, color, 0.2857143f); Vector3 direction7 = new Vector3(-vector.x, -vector.y, vector.z); result.AddDirectionalLight(direction7, color, 0.2857143f); Vector3 direction8 = new Vector3(vector.x, -vector.y, vector.z); result.AddDirectionalLight(direction8, color, 0.2857143f); Vector3 down = Vector3.down; result.AddDirectionalLight(down, color, 0.428571433f); return(result); }
public void UpdateAmbient() { float saturation = this.Ambient.Saturation; float num = Mathf.Lerp(this.Night.AmbientMultiplier, this.Day.AmbientMultiplier, this.LerpValue); if (this.m_AmbientIntensity == -3.40282347E+38f) { this.m_AmbientIntensity = num; } Color color = TOD_Util.ChangeSaturation(this.AmbientColor, this.Ambient.Saturation); if (CaveSensor.s_NumSensorsInside != 0) { this.m_AmbientIntensity += this.m_AmbientIntensity * Time.deltaTime; } else if (EnvironmentLightingSensor.s_NumEnters > 0) { if (Application.isPlaying) { this.m_AmbientIntensity += (EnvironmentLightingSensor.s_IntenistyMul - this.m_AmbientIntensity) * Time.deltaTime; } } else { this.m_AmbientIntensity += (num - this.m_AmbientIntensity) * Time.deltaTime; } num = this.m_AmbientIntensity; switch (this.Ambient.Mode) { case TOD_AmbientType.Color: RenderSettings.ambientMode = AmbientMode.Flat; RenderSettings.ambientLight = color; RenderSettings.ambientIntensity = num; return; case TOD_AmbientType.Gradient: { Color ambientEquatorColor = TOD_Util.ChangeSaturation(this.SampleEquatorColor(), saturation); Color ambientSkyColor = TOD_Util.ChangeSaturation(this.SampleSkyColor(), saturation); RenderSettings.ambientMode = AmbientMode.Trilight; RenderSettings.ambientSkyColor = ambientSkyColor; RenderSettings.ambientEquatorColor = ambientEquatorColor; RenderSettings.ambientGroundColor = color; RenderSettings.ambientIntensity = num; return; } case TOD_AmbientType.Spherical: RenderSettings.ambientMode = AmbientMode.Skybox; RenderSettings.ambientLight = color; RenderSettings.ambientIntensity = num; RenderSettings.ambientProbe = this.RenderToSphericalHarmonics(); return; default: return; } }
/// Setup rayleigh and mie scattering by precalculating as much of it as possible. // See [2] page 2 // See [3] page 2 private void SetupScattering() { // Scale const float ray_scale_const = 1.0f; const float ray_scale_theta = 20.0f; const float mie_scale_const = 0.1f; const float mie_scale_theta = 2.0f; // Rayleigh { // Artistic color multiplier float mult_r = 0.001f + Atmosphere.RayleighMultiplier * Atmosphere.ScatteringColor.r; float mult_g = 0.001f + Atmosphere.RayleighMultiplier * Atmosphere.ScatteringColor.g; float mult_b = 0.001f + Atmosphere.RayleighMultiplier * Atmosphere.ScatteringColor.b; // Scattering coefficient const float beta_r = 5.8e-6f; const float beta_g = 13.5e-6f; const float beta_b = 33.1e-6f; // Phase function const float phase = (3) / (16 * pi); // Shader paramters betaRayleigh.x = ray_scale_const * beta_r * mult_r; betaRayleigh.y = ray_scale_const * beta_g * mult_g; betaRayleigh.z = ray_scale_const * beta_b * mult_b; betaRayleighTheta.x = ray_scale_theta * beta_r * mult_r * phase; betaRayleighTheta.y = ray_scale_theta * beta_g * mult_g * phase; betaRayleighTheta.z = ray_scale_theta * beta_b * mult_b * phase; opticalDepth.x = 8000; // * Mathf.Exp(-height * 50000 / 8000); } // Mie { // Artistic color multiplier float mult_r = 0.001f + Atmosphere.MieMultiplier * Atmosphere.ScatteringColor.r; float mult_g = 0.001f + Atmosphere.MieMultiplier * Atmosphere.ScatteringColor.g; float mult_b = 0.001f + Atmosphere.MieMultiplier * Atmosphere.ScatteringColor.b; // Scattering coefficient const float beta = 2e-5f; // Phase function float g = Atmosphere.Directionality; float phase = (3) / (4 * pi) * (1 - g * g) / (2 + g * g); // Shader paramters betaMie.x = mie_scale_const * beta * mult_r; betaMie.y = mie_scale_const * beta * mult_g; betaMie.z = mie_scale_const * beta * mult_b; betaMieTheta.x = mie_scale_theta * beta * mult_r * phase; betaMieTheta.y = mie_scale_theta * beta * mult_g * phase; betaMieTheta.z = mie_scale_theta * beta * mult_b * phase; betaMiePhase.x = 1 + g * g; betaMiePhase.y = 2 * g; opticalDepth.y = 1200; // * Mathf.Exp(-height * 50000 / 1200); } oneOverBeta = TOD_Util.Inverse(betaMie + betaRayleigh); betaNight = Vector3.Scale(betaRayleighTheta + betaMieTheta / Mathf.Pow(betaMiePhase.x, 1.5f), oneOverBeta); oneOverBeta = FakeHDR2LDR(oneOverBeta, Atmosphere.FakeHDR); }
private void UpdateCelestials() { float f = 0.0174532924f * this.World.Latitude; float num = Mathf.Sin(f); float num2 = Mathf.Cos(f); float longitude = this.World.Longitude; float num3 = 1.57079637f; int year = this.Cycle.Year; int month = this.Cycle.Month; int day = this.Cycle.Day; float num4 = this.Cycle.Hour - this.World.UTC; float num5 = (float)(367 * year - 7 * (year + (month + 9) / 12) / 4 + 275 * month / 9 + day - 730530) + num4 / 24f; float num6 = (float)(367 * year - 7 * (year + (month + 9) / 12) / 4 + 275 * month / 9 + day - 730530) + 0.5f; float num7 = 23.4393f - 3.563E-07f * num5; float f2 = 0.0174532924f * num7; float num8 = Mathf.Sin(f2); float num9 = Mathf.Cos(f2); float num10 = 282.9404f + 4.70935E-05f * num6; float num11 = 0.016709f - 1.151E-09f * num6; float num12 = 356.047f + 0.985600233f * num6; float num13 = 0.0174532924f * num12; float num14 = Mathf.Sin(num13); float num15 = Mathf.Cos(num13); float f3 = num13 + num11 * num14 * (1f + num11 * num15); float num16 = Mathf.Sin(f3); float num17 = Mathf.Cos(f3) - num11; float num18 = Mathf.Sqrt(1f - num11 * num11) * num16; float num19 = 57.29578f * Mathf.Atan2(num18, num17); float num20 = Mathf.Sqrt(num17 * num17 + num18 * num18); float num21 = num19 + num10; float f4 = 0.0174532924f * num21; float num22 = Mathf.Sin(f4); float num23 = Mathf.Cos(f4); float num24 = num20 * num23; float num25 = num20 * num22; float num26 = num24; float num27 = num25 * num9; float y = num25 * num8; float num28 = Mathf.Atan2(num27, num26); float num29 = 57.29578f * num28; float f5 = Mathf.Atan2(y, Mathf.Sqrt(num26 * num26 + num27 * num27)); float num30 = Mathf.Sin(f5); float num31 = Mathf.Cos(f5); float num32 = num19 + num10 + 180f; float num33 = num29 - num32 - longitude; float num34 = -6f; float num35 = Mathf.Acos((Mathf.Sin(0.0174532924f * num34) - num * num30) / (num2 * num31)); float num36 = 57.29578f * num35; this.SunsetTime = (24f + (num33 + num36) / 15f % 24f) % 24f; this.SunriseTime = (24f + (num33 - num36) / 15f % 24f) % 24f; float num37 = 282.9404f + 4.70935E-05f * num5; float num38 = 0.016709f - 1.151E-09f * num5; float num39 = 356.047f + 0.985600233f * num5; float num40 = 0.0174532924f * num39; float num41 = Mathf.Sin(num40); float num42 = Mathf.Cos(num40); float f6 = num40 + num38 * num41 * (1f + num38 * num42); float num43 = Mathf.Sin(f6); float num44 = Mathf.Cos(f6) - num38; float num45 = Mathf.Sqrt(1f - num38 * num38) * num43; float num46 = 57.29578f * Mathf.Atan2(num45, num44); float num47 = Mathf.Sqrt(num44 * num44 + num45 * num45); float num48 = num46 + num37; float f7 = 0.0174532924f * num48; float num49 = Mathf.Sin(f7); float num50 = Mathf.Cos(f7); float num51 = num47 * num50; float num52 = num47 * num49; float num53 = num51; float num54 = num52 * num9; float y2 = num52 * num8; float num55 = Mathf.Atan2(num54, num53); float f8 = Mathf.Atan2(y2, Mathf.Sqrt(num53 * num53 + num54 * num54)); float num56 = Mathf.Sin(f8); float num57 = Mathf.Cos(f8); float num58 = num46 + num37 + 180f + 15f * num4; float num59 = 0.0174532924f * (num58 + longitude); this.LocalSiderealTime = (num58 + longitude) / 15f; float f9 = num59 - num55; float num60 = Mathf.Sin(f9); float num61 = Mathf.Cos(f9) * num57; float num62 = num60 * num57; float num63 = num56; float num64 = num61 * num - num63 * num2; float num65 = num62; float y3 = num61 * num2 + num63 * num; float num66 = Mathf.Atan2(num65, num64) + 3.14159274f; float num67 = Mathf.Atan2(y3, Mathf.Sqrt(num64 * num64 + num65 * num65)); float num68 = num3 - num67; float num69 = num67; float num70 = num66; this.SunZenith = 57.29578f * num68; this.SunAltitude = 57.29578f * num69; this.SunAzimuth = 57.29578f * num70; float num112; float num113; float num114; if (this.Moon.Position == TOD_MoonPositionType.Realistic) { float num71 = 125.1228f - 0.05295381f * num5; float num72 = 5.1454f; float num73 = 318.0634f + 0.164357319f * num5; float num74 = 60.2666f; float num75 = 0.0549f; float num76 = 115.3654f + 13.0649929f * num5; float f10 = 0.0174532924f * num71; float num77 = Mathf.Sin(f10); float num78 = Mathf.Cos(f10); float f11 = 0.0174532924f * num72; float num79 = Mathf.Sin(f11); float num80 = Mathf.Cos(f11); float num81 = 0.0174532924f * num76; float num82 = Mathf.Sin(num81); float num83 = Mathf.Cos(num81); float f12 = num81 + num75 * num82 * (1f + num75 * num83); float num84 = Mathf.Sin(f12); float num85 = Mathf.Cos(f12); float num86 = num74 * (num85 - num75); float num87 = num74 * (Mathf.Sqrt(1f - num75 * num75) * num84); float num88 = 57.29578f * Mathf.Atan2(num87, num86); float num89 = Mathf.Sqrt(num86 * num86 + num87 * num87); float num90 = num88 + num73; float f13 = 0.0174532924f * num90; float num91 = Mathf.Sin(f13); float num92 = Mathf.Cos(f13); float num93 = num89 * (num78 * num92 - num77 * num91 * num80); float num94 = num89 * (num77 * num92 + num78 * num91 * num80); float num95 = num89 * (num91 * num79); float num96 = num93; float num97 = num94; float num98 = num95; float num99 = num96; float num100 = num97 * num9 - num98 * num8; float y4 = num97 * num8 + num98 * num9; float num101 = Mathf.Atan2(num100, num99); float f14 = Mathf.Atan2(y4, Mathf.Sqrt(num99 * num99 + num100 * num100)); float num102 = Mathf.Sin(f14); float num103 = Mathf.Cos(f14); float f15 = num59 - num101; float num104 = Mathf.Sin(f15); float num105 = Mathf.Cos(f15) * num103; float num106 = num104 * num103; float num107 = num102; float num108 = num105 * num - num107 * num2; float num109 = num106; float y5 = num105 * num2 + num107 * num; float num110 = Mathf.Atan2(num109, num108) + 3.14159274f; float num111 = Mathf.Atan2(y5, Mathf.Sqrt(num108 * num108 + num109 * num109)); num112 = num3 - num111; num113 = num111; num114 = num110; } else { num112 = num68 - 3.14159274f; num113 = num69 - 3.14159274f; num114 = num70; } this.MoonZenith = 57.29578f * num112; this.MoonAltitude = 57.29578f * num113; this.MoonAzimuth = 57.29578f * num114; Quaternion quaternion = Quaternion.Euler(90f - this.World.Latitude, 0f, 0f) * Quaternion.Euler(0f, this.World.Longitude, 0f) * Quaternion.Euler(0f, num59 * 57.29578f, 0f); if (this.Stars.Position == TOD_StarsPositionType.Rotating) { this.Components.SpaceTransform.localRotation = quaternion; this.Components.StarTransform.localRotation = quaternion; } else { this.Components.SpaceTransform.localRotation = Quaternion.identity; this.Components.StarTransform.localRotation = Quaternion.identity; } Vector3 localPosition = this.OrbitalToLocal(num68, num70); this.Components.SunTransform.localPosition = localPosition; this.Components.SunTransform.LookAt(this.Components.DomeTransform.position, this.Components.SunTransform.up); Vector3 localPosition2 = this.OrbitalToLocal(num112, num114); Vector3 worldUp = quaternion * -Vector3.right; this.Components.MoonTransform.localPosition = localPosition2; this.Components.MoonTransform.LookAt(this.Components.DomeTransform.position, worldUp); float num115 = 8f * Mathf.Tan(0.008726646f * this.Sun.MeshSize); Vector3 localScale = new Vector3(num115, num115, num115); this.Components.SunTransform.localScale = localScale; float num116 = 4f * Mathf.Tan(0.008726646f * this.Moon.MeshSize); Vector3 localScale2 = new Vector3(num116, num116, num116); this.Components.MoonTransform.localScale = localScale2; if (RainManager.Get() != null && MainLevel.Instance.m_TODAtmosphereAndCloudsUpdateEnabled) { float a = (this.Cycle.Hour > 19f || this.Cycle.Hour < 7f) ? 0f : 0.5f; this.Atmosphere.Fogginess = CJTools.Math.GetProportionalClamp(0f, a, RainManager.Get().m_WeatherInterpolated, 0f, 1f); this.Clouds.Coverage = CJTools.Math.GetProportionalClamp(this.m_DefaultCloudsCoverage, 0.8f, RainManager.Get().m_WeatherInterpolated, 0f, 1f); this.Clouds.Opacity = CJTools.Math.GetProportionalClamp(this.m_DefaultCloudsOpacity, 0.8f, RainManager.Get().m_WeatherInterpolated, 0f, 1f); } bool enabled = (1f - this.Atmosphere.Fogginess) * (1f - this.LerpValue) > 0f; this.Components.SpaceRenderer.enabled = enabled; this.Components.StarRenderer.enabled = enabled; bool enabled2 = this.Components.SunTransform.localPosition.y > -num115; this.Components.SunRenderer.enabled = enabled2; bool enabled3 = this.Components.MoonTransform.localPosition.y > -num116; this.Components.MoonRenderer.enabled = enabled3; bool enabled4 = true; this.Components.AtmosphereRenderer.enabled = enabled4; bool enabled5 = this.Components.Rays != null; this.Components.ClearRenderer.enabled = enabled5; bool enabled6 = this.Clouds.Coverage > 0f && this.Clouds.Opacity > 0f; this.Components.CloudRenderer.enabled = enabled6; this.LerpValue = Mathf.InverseLerp(105f, 90f, this.SunZenith); float time = Mathf.Clamp01(this.SunZenith / 90f); float time2 = Mathf.Clamp01((this.SunZenith - 90f) / 90f); float num117 = Mathf.Clamp01((this.LerpValue - 0.1f) / 0.9f); float num118 = Mathf.Clamp01((0.1f - this.LerpValue) / 0.1f); float num119 = Mathf.Clamp01((90f - num112 * 57.29578f) / 5f); this.SunVisibility = (1f - this.Atmosphere.Fogginess) * num117; this.MoonVisibility = (1f - this.Atmosphere.Fogginess) * num118 * num119; this.SunLightColor = TOD_Util.ApplyAlpha(this.Day.LightColor.Evaluate(time)); this.MoonLightColor = TOD_Util.ApplyAlpha(this.Night.LightColor.Evaluate(time2)); this.SunRayColor = TOD_Util.ApplyAlpha(this.Day.RayColor.Evaluate(time)); this.MoonRayColor = TOD_Util.ApplyAlpha(this.Night.RayColor.Evaluate(time2)); this.SunSkyColor = TOD_Util.ApplyAlpha(this.Day.SkyColor.Evaluate(time)); this.MoonSkyColor = TOD_Util.ApplyAlpha(this.Night.SkyColor.Evaluate(time2)); this.SunMeshColor = TOD_Util.ApplyAlpha(this.Day.SunColor.Evaluate(time)); this.MoonMeshColor = TOD_Util.ApplyAlpha(this.Night.MoonColor.Evaluate(time2)); this.SunCloudColor = TOD_Util.ApplyAlpha(this.Day.CloudColor.Evaluate(time)); this.MoonCloudColor = TOD_Util.ApplyAlpha(this.Night.CloudColor.Evaluate(time2)); Color b = TOD_Util.ApplyAlpha(this.Day.FogColor.Evaluate(time)); Color a2 = TOD_Util.ApplyAlpha(this.Night.FogColor.Evaluate(time2)); this.FogColor = Color.Lerp(a2, b, this.LerpValue); Color color = TOD_Util.ApplyAlpha(this.Day.AmbientColor.Evaluate(time)); Color color2 = TOD_Util.ApplyAlpha(this.Night.AmbientColor.Evaluate(time2)); if (CaveSensor.s_NumSensorsInside != 0) { this.AmbientColor += (Color.black - this.AmbientColor) * Time.deltaTime; if (this.AmbientColor != Color.black) { this.Ambient.UpdateInterval = 0f; } else { this.Ambient.UpdateInterval = 1f; } } else { Color color3 = Color.Lerp(color2, color, this.LerpValue); this.AmbientColor += (color3 - this.AmbientColor) * Time.deltaTime; if (this.AmbientColor != color3) { this.Ambient.UpdateInterval = 0f; } else { this.Ambient.UpdateInterval = 1f; } } Color b2 = color; Color a3 = color2; this.GroundColor = Color.Lerp(a3, b2, this.LerpValue); this.MoonHaloColor = TOD_Util.MulRGB(this.MoonSkyColor, this.Moon.HaloBrightness * num119); float shadowStrength; float intensity; Color color4; if (this.LerpValue > 0.1f) { this.IsDay = true; this.IsNight = false; shadowStrength = this.Day.ShadowStrength; intensity = Mathf.Lerp(0f, this.Day.LightIntensity * this.Day.m_SanityLightIntensityMul, this.SunVisibility); color4 = this.SunLightColor; } else { this.IsDay = false; this.IsNight = true; shadowStrength = this.Night.ShadowStrength; intensity = Mathf.Lerp(0f, this.Night.LightIntensity * this.Night.m_SanityLightIntensityMul, this.MoonVisibility); color4 = this.MoonLightColor; } this.Components.LightSource.color = color4; this.Components.LightSource.intensity = intensity; this.Components.LightSource.shadowStrength = shadowStrength; if (!Application.isPlaying || this.timeSinceLightUpdate >= this.Light.UpdateInterval) { this.timeSinceLightUpdate = 0f; Vector3 localPosition3 = this.IsNight ? this.OrbitalToLocal(Mathf.Min(num112, (1f - this.Light.MinimumHeight) * 3.14159274f / 2f), num114) : this.OrbitalToLocal(Mathf.Min(num68, (1f - this.Light.MinimumHeight) * 3.14159274f / 2f), num70); this.Components.LightTransform.localPosition = localPosition3; this.Components.LightTransform.LookAt(this.Components.DomeTransform.position); } else { this.timeSinceLightUpdate += Time.deltaTime; } this.SunDirection = -this.Components.SunTransform.forward; this.LocalSunDirection = this.Components.DomeTransform.InverseTransformDirection(this.SunDirection); this.MoonDirection = -this.Components.MoonTransform.forward; this.LocalMoonDirection = this.Components.DomeTransform.InverseTransformDirection(this.MoonDirection); this.LightDirection = -this.Components.LightTransform.forward; this.LocalLightDirection = this.Components.DomeTransform.InverseTransformDirection(this.LightDirection); }
private void UpdateCelestials() { // Celestial computations float lst_rad, sun_zenith_rad, sun_altitude_rad, sun_azimuth_rad, moon_zenith_rad, moon_altitude_rad, moon_azimuth_rad; { // Local latitude float lat_rad = Mathf.Deg2Rad * World.Latitude; float lat_sin = Mathf.Sin(lat_rad); float lat_cos = Mathf.Cos(lat_rad); // Local longitude float lon_deg = World.Longitude; // Horizon angle float horizon_rad = 90f * Mathf.Deg2Rad; // Date int year = Cycle.Year; int month = Cycle.Month; int day = Cycle.Day; float hour = Cycle.Hour - World.UTC; // Time scale float d = 367 * year - 7 * (year + (month + 9) / 12) / 4 + 275 * month / 9 + day - 730530 + hour / 24f; float d_noon = 367 * year - 7 * (year + (month + 9) / 12) / 4 + 275 * month / 9 + day - 730530 + 12f / 24f; // Tilt of earth's axis of rotation float ecl = 23.4393f - 3.563E-7f * d; float ecl_rad = Mathf.Deg2Rad * ecl; float ecl_sin = Mathf.Sin(ecl_rad); float ecl_cos = Mathf.Cos(ecl_rad); // Sunset and sunrise { // See http://www.stjarnhimlen.se/comp/ppcomp.html#4 float w = 282.9404f + 4.70935E-5f * d_noon; float e = 0.016709f - 1.151E-9f * d_noon; float M = 356.0470f + 0.9856002585f * d_noon; float M_rad = Mathf.Deg2Rad * M; float M_sin = Mathf.Sin(M_rad); float M_cos = Mathf.Cos(M_rad); // See http://www.stjarnhimlen.se/comp/ppcomp.html#5 float E_rad = M_rad + e * M_sin * (1f + e * M_cos); float E_sin = Mathf.Sin(E_rad); float E_cos = Mathf.Cos(E_rad); float xv = E_cos - e; float yv = Mathf.Sqrt(1f - e * e) * E_sin; float v = Mathf.Rad2Deg * Mathf.Atan2(yv, xv); float r = Mathf.Sqrt(xv * xv + yv * yv); float l_deg = v + w; float l_rad = Mathf.Deg2Rad * l_deg; float l_sin = Mathf.Sin(l_rad); float l_cos = Mathf.Cos(l_rad); float xs = r * l_cos; float ys = r * l_sin; float xe = xs; float ye = ys * ecl_cos; float ze = ys * ecl_sin; float rasc_rad = Mathf.Atan2(ye, xe); float rasc_deg = Mathf.Rad2Deg * rasc_rad; float decl_rad = Mathf.Atan2(ze, Mathf.Sqrt(xe * xe + ye * ye)); float decl_sin = Mathf.Sin(decl_rad); float decl_cos = Mathf.Cos(decl_rad); // See http://www.stjarnhimlen.se/comp/ppcomp.html#5b float Ls = v + w; float GMST0_deg = Ls + 180f; // See http://www.stjarnhimlen.se/comp/riset.html#2 float ut_deg = rasc_deg - GMST0_deg - lon_deg; float twilight_deg = -6f; float twilight_rad = Mathf.Deg2Rad * twilight_deg; float twilight_sin = Mathf.Sin(twilight_rad); float lha_cos = (twilight_sin - lat_sin * decl_sin) / (lat_cos * decl_cos); float lha_rad = Mathf.Acos(lha_cos); float lha_deg = Mathf.Rad2Deg * lha_rad; SunsetTime = (24f + ((ut_deg + lha_deg) / 15f + World.UTC) % 24f) % 24f; SunriseTime = (24f + ((ut_deg - lha_deg) / 15f + World.UTC) % 24f) % 24f; } // Sun position { // See http://www.stjarnhimlen.se/comp/ppcomp.html#4 float w = 282.9404f + 4.70935E-5f * d; float e = 0.016709f - 1.151E-9f * d; float M = 356.0470f + 0.9856002585f * d; float M_rad = Mathf.Deg2Rad * M; float M_sin = Mathf.Sin(M_rad); float M_cos = Mathf.Cos(M_rad); // See http://www.stjarnhimlen.se/comp/ppcomp.html#5 float E_rad = M_rad + e * M_sin * (1f + e * M_cos); float E_sin = Mathf.Sin(E_rad); float E_cos = Mathf.Cos(E_rad); float xv = E_cos - e; float yv = Mathf.Sqrt(1f - e * e) * E_sin; float v = Mathf.Rad2Deg * Mathf.Atan2(yv, xv); float r = Mathf.Sqrt(xv * xv + yv * yv); float l_deg = v + w; float l_rad = Mathf.Deg2Rad * l_deg; float l_sin = Mathf.Sin(l_rad); float l_cos = Mathf.Cos(l_rad); float xs = r * l_cos; float ys = r * l_sin; float xe = xs; float ye = ys * ecl_cos; float ze = ys * ecl_sin; float rasc_rad = Mathf.Atan2(ye, xe); float decl_rad = Mathf.Atan2(ze, Mathf.Sqrt(xe * xe + ye * ye)); float decl_sin = Mathf.Sin(decl_rad); float decl_cos = Mathf.Cos(decl_rad); // See http://www.stjarnhimlen.se/comp/ppcomp.html#5b float Ls = v + w; float GMST0_deg = Ls + 180f; float GMST_deg = GMST0_deg + 15f * hour; lst_rad = Mathf.Deg2Rad * (GMST_deg + lon_deg); LocalSiderealTime = (GMST_deg + lon_deg) / 15f; // See http://www.stjarnhimlen.se/comp/ppcomp.html#12b float HA_rad = lst_rad - rasc_rad; float HA_sin = Mathf.Sin(HA_rad); float HA_cos = Mathf.Cos(HA_rad); float x = HA_cos * decl_cos; float y = HA_sin * decl_cos; float z = decl_sin; float xhor = x * lat_sin - z * lat_cos; float yhor = y; float zhor = x * lat_cos + z * lat_sin; float azimuth = Mathf.Atan2(yhor, xhor) + Mathf.Deg2Rad * 180f; float altitude = Mathf.Atan2(zhor, Mathf.Sqrt(xhor * xhor + yhor * yhor)); sun_zenith_rad = horizon_rad - altitude; sun_altitude_rad = altitude; sun_azimuth_rad = azimuth; } SunZenith = Mathf.Rad2Deg * sun_zenith_rad; SunAltitude = Mathf.Rad2Deg * sun_altitude_rad; SunAzimuth = Mathf.Rad2Deg * sun_azimuth_rad; // Moon position if (Moon.Position == TOD_MoonPositionType.Realistic) { // See http://www.stjarnhimlen.se/comp/ppcomp.html#4 float N = 125.1228f - 0.0529538083f * d; float i = 5.1454f; float w = 318.0634f + 0.1643573223f * d; float a = 60.2666f; float e = 0.054900f; float M = 115.3654f + 13.0649929509f * d; float N_rad = Mathf.Deg2Rad * N; float N_sin = Mathf.Sin(N_rad); float N_cos = Mathf.Cos(N_rad); float i_rad = Mathf.Deg2Rad * i; float i_sin = Mathf.Sin(i_rad); float i_cos = Mathf.Cos(i_rad); float M_rad = Mathf.Deg2Rad * M; float M_sin = Mathf.Sin(M_rad); float M_cos = Mathf.Cos(M_rad); // See http://www.stjarnhimlen.se/comp/ppcomp.html#6 float E_rad = M_rad + e * M_sin * (1f + e * M_cos); float E_sin = Mathf.Sin(E_rad); float E_cos = Mathf.Cos(E_rad); float xv = a * (E_cos - e); float yv = a * (Mathf.Sqrt(1f - e * e) * E_sin); float v = Mathf.Rad2Deg * Mathf.Atan2(yv, xv); float r = Mathf.Sqrt(xv * xv + yv * yv); float l_deg = v + w; float l_rad = Mathf.Deg2Rad * l_deg; float l_sin = Mathf.Sin(l_rad); float l_cos = Mathf.Cos(l_rad); // See http://www.stjarnhimlen.se/comp/ppcomp.html#7 float xh = r * (N_cos * l_cos - N_sin * l_sin * i_cos); float yh = r * (N_sin * l_cos + N_cos * l_sin * i_cos); float zh = r * (l_sin * i_sin); // See http://www.stjarnhimlen.se/comp/ppcomp.html#11 float xg = xh; float yg = yh; float zg = zh; // See http://www.stjarnhimlen.se/comp/ppcomp.html#12 float xe = xg; float ye = yg * ecl_cos - zg * ecl_sin; float ze = yg * ecl_sin + zg * ecl_cos; float rasc_rad = Mathf.Atan2(ye, xe); float decl_rad = Mathf.Atan2(ze, Mathf.Sqrt(xe * xe + ye * ye)); float decl_sin = Mathf.Sin(decl_rad); float decl_cos = Mathf.Cos(decl_rad); // See http://www.stjarnhimlen.se/comp/ppcomp.html#12b float HA_rad = lst_rad - rasc_rad; float HA_sin = Mathf.Sin(HA_rad); float HA_cos = Mathf.Cos(HA_rad); float x = HA_cos * decl_cos; float y = HA_sin * decl_cos; float z = decl_sin; float xhor = x * lat_sin - z * lat_cos; float yhor = y; float zhor = x * lat_cos + z * lat_sin; float azimuth = Mathf.Atan2(yhor, xhor) + Mathf.Deg2Rad * 180f; float altitude = Mathf.Atan2(zhor, Mathf.Sqrt(xhor * xhor + yhor * yhor)); moon_zenith_rad = horizon_rad - altitude; moon_altitude_rad = altitude; moon_azimuth_rad = azimuth; } else { moon_zenith_rad = sun_zenith_rad - pi; moon_altitude_rad = sun_altitude_rad - pi; moon_azimuth_rad = sun_azimuth_rad; } MoonZenith = Mathf.Rad2Deg * moon_zenith_rad; MoonAltitude = Mathf.Rad2Deg * moon_altitude_rad; MoonAzimuth = Mathf.Rad2Deg * moon_azimuth_rad; } // Transform updates { Quaternion spaceRot = Quaternion.Euler(90 - World.Latitude, 0, 0) * Quaternion.Euler(0, 180 + lst_rad * Mathf.Rad2Deg, 0); if (Stars.Position == TOD_StarsPositionType.Rotating) { #if UNITY_EDITOR if (Components.SpaceTransform.localRotation * Vector3.one != spaceRot * Vector3.one) #endif { Components.SpaceTransform.localRotation = spaceRot; } #if UNITY_EDITOR if (Components.StarTransform.localRotation * Vector3.one != spaceRot * Vector3.one) #endif { Components.StarTransform.localRotation = spaceRot; } } else { #if UNITY_EDITOR if (Components.SpaceTransform.localRotation != Quaternion.identity) #endif { Components.SpaceTransform.localRotation = Quaternion.identity; } #if UNITY_EDITOR if (Components.StarTransform.localRotation != Quaternion.identity) #endif { Components.StarTransform.localRotation = Quaternion.identity; } } var sunPos = OrbitalToLocal(sun_zenith_rad, sun_azimuth_rad); #if UNITY_EDITOR if (Components.SunTransform.localPosition != sunPos) #endif { Components.SunTransform.localPosition = sunPos; Components.SunTransform.LookAt(Components.DomeTransform.position, Components.SunTransform.up); } var moonPos = OrbitalToLocal(moon_zenith_rad, moon_azimuth_rad); #if UNITY_EDITOR if (Components.MoonTransform.localPosition != moonPos) #endif { var moonFwd = spaceRot * -Vector3.right; Components.MoonTransform.localPosition = moonPos; Components.MoonTransform.LookAt(Components.DomeTransform.position, moonFwd); } float sunSize = 8.0f * Mathf.Tan(0.5f * Mathf.Deg2Rad * Sun.MeshSize); var sunScale = new Vector3(sunSize, sunSize, sunSize); #if UNITY_EDITOR if (Components.SunTransform.localScale != sunScale) #endif { Components.SunTransform.localScale = sunScale; } float moonSize = 4.0f * Mathf.Tan(0.5f * Mathf.Deg2Rad * Moon.MeshSize); var moonScale = new Vector3(moonSize, moonSize, moonSize); #if UNITY_EDITOR if (Components.MoonTransform.localScale != moonScale) #endif { Components.MoonTransform.localScale = moonScale; } bool starsEnabled = (1 - Atmosphere.Fogginess) * (1 - LerpValue) > 0; #if UNITY_EDITOR if (Components.SpaceRenderer.enabled != starsEnabled) #endif { Components.SpaceRenderer.enabled = starsEnabled; } #if UNITY_EDITOR if (Components.StarRenderer.enabled != starsEnabled) #endif { Components.StarRenderer.enabled = starsEnabled; } bool sunEnabled = (Components.SunTransform.localPosition.y > -sunSize); #if UNITY_EDITOR if (Components.SunRenderer.enabled != sunEnabled) #endif { Components.SunRenderer.enabled = sunEnabled; } bool moonEnabled = (Components.MoonTransform.localPosition.y > -moonSize); #if UNITY_EDITOR if (Components.MoonRenderer.enabled != moonEnabled) #endif { Components.MoonRenderer.enabled = moonEnabled; } bool atmoEnabled = true; #if UNITY_EDITOR if (Components.AtmosphereRenderer.enabled != atmoEnabled) #endif { Components.AtmosphereRenderer.enabled = atmoEnabled; } bool clearEnabled = false; #if UNITY_EDITOR if (Components.ClearRenderer.enabled != clearEnabled) #endif { Components.ClearRenderer.enabled = clearEnabled; } bool cloudEnabled = (Clouds.Coverage > 0 && Clouds.Opacity > 0); #if UNITY_EDITOR if (Components.CloudRenderer.enabled != cloudEnabled) #endif { Components.CloudRenderer.enabled = cloudEnabled; } } // Color calculations { // Lerp value LerpValue = Mathf.InverseLerp(105f, 90f, SunZenith); // Pseudo-time to sample color gradients with float dayTime = Mathf.Clamp01(SunZenith / 90f); float nightTime = Mathf.Clamp01((SunZenith - 90f) / 90f); // Constants const float lerpThreshold = 0.1f; const float falloffAngle = 5.0f; float sunLerpValue = Mathf.Clamp01((LerpValue - lerpThreshold) / (1 - lerpThreshold)); float moonLerpValue = Mathf.Clamp01((lerpThreshold - LerpValue) / lerpThreshold); float moonAboveHorizon = Mathf.Clamp01((90f - moon_zenith_rad * Mathf.Rad2Deg) / falloffAngle); // Sun and moon visibility SunVisibility = (1 - Atmosphere.Fogginess) * sunLerpValue; MoonVisibility = (1 - Atmosphere.Fogginess) * moonLerpValue * moonAboveHorizon; // Sun and moon light colors SunLightColor = TOD_Util.ApplyAlpha(Day.LightColor.Evaluate(dayTime)); MoonLightColor = TOD_Util.ApplyAlpha(Night.LightColor.Evaluate(nightTime)); // Sun and moon light ray colors SunRayColor = TOD_Util.ApplyAlpha(Day.RayColor.Evaluate(dayTime)); MoonRayColor = TOD_Util.ApplyAlpha(Night.RayColor.Evaluate(nightTime)); // Sky color SunSkyColor = TOD_Util.ApplyAlpha(Day.SkyColor.Evaluate(dayTime)); MoonSkyColor = TOD_Util.ApplyAlpha(Night.SkyColor.Evaluate(nightTime)); // Mesh color SunMeshColor = TOD_Util.ApplyAlpha(Day.SunColor.Evaluate(dayTime)); MoonMeshColor = TOD_Util.ApplyAlpha(Night.MoonColor.Evaluate(nightTime)); // Cloud color SunCloudColor = TOD_Util.ApplyAlpha(Day.CloudColor.Evaluate(dayTime)); MoonCloudColor = TOD_Util.ApplyAlpha(Night.CloudColor.Evaluate(nightTime)); // Fog color Color dayFogColor = TOD_Util.ApplyAlpha(Day.FogColor.Evaluate(dayTime)); Color nightFogColor = TOD_Util.ApplyAlpha(Night.FogColor.Evaluate(nightTime)); FogColor = Color.Lerp(nightFogColor, dayFogColor, LerpValue); // Ambient color Color dayAmbientColor = TOD_Util.ApplyAlpha(Day.AmbientColor.Evaluate(dayTime)); Color nightAmbientColor = TOD_Util.ApplyAlpha(Night.AmbientColor.Evaluate(nightTime)); AmbientColor = Color.Lerp(nightAmbientColor, dayAmbientColor, LerpValue); // Ground color Color dayGroundColor = dayAmbientColor; Color nightGroundColor = nightAmbientColor; GroundColor = Color.Lerp(nightGroundColor, dayGroundColor, LerpValue); // Moon halo color MoonHaloColor = TOD_Util.MulRGB(MoonSkyColor, Moon.HaloBrightness * moonAboveHorizon); // Light source parameters float lightIntensity; float lightShadowStrength; Color lightColor; if (LerpValue > lerpThreshold) { IsDay = true; IsNight = false; lightShadowStrength = Day.ShadowStrength; lightIntensity = Mathf.Lerp(0, Day.LightIntensity, SunVisibility); lightColor = SunLightColor; } else { IsDay = false; IsNight = true; lightShadowStrength = Night.ShadowStrength; lightIntensity = Mathf.Lerp(0, Night.LightIntensity, MoonVisibility); lightColor = MoonLightColor; } #if UNITY_EDITOR if (Components.LightSource.color != lightColor) #endif { Components.LightSource.color = lightColor; } #if UNITY_EDITOR if (Components.LightSource.intensity != lightIntensity) #endif { Components.LightSource.intensity = lightIntensity; } #if UNITY_EDITOR if (Components.LightSource.shadowStrength != lightShadowStrength) #endif { Components.LightSource.shadowStrength = lightShadowStrength; } } // Light source position if (!Application.isPlaying || timeSinceLightUpdate >= Light.UpdateInterval) { timeSinceLightUpdate = 0; var position = IsNight ? OrbitalToLocal(Mathf.Min(moon_zenith_rad, (1 - Light.MinimumHeight) * pi / 2), moon_azimuth_rad) : OrbitalToLocal(Mathf.Min(sun_zenith_rad, (1 - Light.MinimumHeight) * pi / 2), sun_azimuth_rad); #if UNITY_EDITOR if (Components.LightTransform.localPosition != position) #endif { Components.LightTransform.localPosition = position; Components.LightTransform.LookAt(Components.DomeTransform.position); } } else { timeSinceLightUpdate += Time.deltaTime; } // Direction vectors { SunDirection = -Components.SunTransform.forward; LocalSunDirection = Components.DomeTransform.InverseTransformDirection(SunDirection); MoonDirection = -Components.MoonTransform.forward; LocalMoonDirection = Components.DomeTransform.InverseTransformDirection(MoonDirection); LightDirection = -Components.LightTransform.forward; LocalLightDirection = Components.DomeTransform.InverseTransformDirection(LightDirection); } }