/// <summary> /// Get a color for a gradient given directional light position /// </summary> /// <param name="gradient">Gradient</param> /// <returns>Color</returns> public Color GetGradientColor(Gradient gradient) { if (gradient == null) { return(WeatherMakerLightManagerScript.EvaluateGradient(gradient, 1.0f)); } float sunGradientLookup = GetGradientLookup(); return(WeatherMakerLightManagerScript.EvaluateGradient(gradient, sunGradientLookup)); }
private void UpdateUnityFog(Camera camera) { if (!UseUnityFog) { return; } WeatherMakerCelestialObjectScript sun = WeatherMakerLightManagerScript.SunForCamera(camera); if (sun == null) { return; } RenderSettings.fog = (FogProfile.fogDensity > 0.0001f && FogProfile.FogMode != WeatherMakerFogMode.None); Color fogColor = (FogProfile.FogColor * (sun == null || !sun.LightIsOn ? Color.black : sun.GetGradientColor(FogProfile.FogGradientColor))); if (WeatherMakerLightManagerScript.Instance != null) { bool cameraIsPerspective = (camera == null || !camera.orthographic); foreach (WeatherMakerLightManagerScript.LightState light in WeatherMakerLightManagerScript.Instance.Lights) { if (light.Light.type == LightType.Directional && light.Light != sun.Light) { WeatherMakerCelestialObjectScript obj = light.Light.GetComponent <WeatherMakerCelestialObjectScript>(); if (obj != null && obj.OrbitTypeIsPerspective == cameraIsPerspective) { fogColor += (FogProfile.FogColor * (light.Light.color * light.Light.intensity)); } break; } } } RenderSettings.fogColor = fogColor; RenderSettings.fogDensity = FogProfile.fogDensity; switch (FogProfile.FogMode) { case WeatherMakerFogMode.Constant: case WeatherMakerFogMode.Linear: RenderSettings.fogMode = FogMode.Linear; break; case WeatherMakerFogMode.None: RenderSettings.fogDensity = 0.0f; break; case WeatherMakerFogMode.Exponential: RenderSettings.fogMode = FogMode.Exponential; break; case WeatherMakerFogMode.ExponentialSquared: RenderSettings.fogMode = FogMode.ExponentialSquared; break; } }
private void RaycastForEclipse(Camera camera, Material material) { if (WeatherMakerLightManagerScript.Instance == null) { return; } // disable allow eclipses everywhere by default float eclipsePower = 0.0f; foreach (WeatherMakerCelestialObjectScript moon in WeatherMakerLightManagerScript.Instance.Moons) { Shader.SetGlobalInt(WMS._WeatherMakerSkyEnableSunEclipse, 0); if (moon.Collider != null) { moon.Collider.enabled = CheckForEclipse; } } if (CheckForEclipse) { WeatherMakerCelestialObjectScript sun = WeatherMakerLightManagerScript.SunForCamera(camera); if (sun != null) { float sunRadius = Mathf.Lerp(0.0f, 1000.0f, Mathf.Pow(sun.Scale, 0.15f)); Vector3 origin = camera.transform.position - (sun.transform.forward * camera.farClipPlane * 1.7f); int hitCount = Physics.SphereCastNonAlloc(origin, sunRadius, sun.transform.forward, eclipseHits, camera.farClipPlane); for (int i = 0; i < hitCount; i++) { foreach (WeatherMakerCelestialObjectScript moon in WeatherMakerLightManagerScript.Instance.Moons) { if (moon != null && moon.transform.gameObject == eclipseHits[i].collider.gameObject) { float dot = Mathf.Abs(Vector3.Dot(eclipseHits[i].normal, sun.transform.forward)); eclipsePower += Mathf.Pow(dot, 256.0f); Shader.SetGlobalInt(WMS._WeatherMakerSkyEnableSunEclipse, 1); //Debug.LogFormat("Eclipse raycast normal: {0}, dot: {1}, power: {2}", eclipseHits[i].normal, dot, eclipsePower); break; } } } } } if (eclipsePower == 0.0f) { WeatherMakerLightManagerScript.Instance.DirectionalLightIntensityMultipliers["WeatherMakerSkySphereScriptEclipse"] = 1.0f; } else { float eclipseLightReducer = 1.0f - Mathf.Clamp(eclipsePower, 0.0f, 1.0f); WeatherMakerLightManagerScript.Instance.DirectionalLightIntensityMultipliers["WeatherMakerSkySphereScriptEclipse"] = eclipseLightReducer; Shader.SetGlobalFloat(WMS._NightSkyMultiplier, Mathf.Max(1.0f - Mathf.Min(1.0f, AtmosphereProfile.AtmosphereThickness), Mathf.Max(eclipsePower, WeatherMakerDayNightCycleManagerScript.Instance.NightMultiplier))); } }
/// <summary> /// Update a fog material with fog properties from this object /// </summary> /// <param name="material">Fog material</param> /// <param name="camera">Camera</param> /// <param name="global">Whether to set global shader properties</param> public virtual void UpdateMaterialProperties(Material material, Camera camera, bool global) { WeatherMakerCelestialObjectScript sun = WeatherMakerLightManagerScript.SunForCamera(camera); if (sun == null) { return; } fogLightAbsorption = (FogLightAbsorption == null ? 0.4f : sun.GetGradientColor(FogLightAbsorption).a); bool gamma = (QualitySettings.activeColorSpace == ColorSpace.Gamma); float scatterCover = (WeatherMakerFullScreenCloudsScript.Instance != null && WeatherMakerFullScreenCloudsScript.Instance.enabled && WeatherMakerFullScreenCloudsScript.Instance.CloudProfile != null ? WeatherMakerFullScreenCloudsScript.Instance.CloudProfile.CloudCoverTotal : 0.0f); scatterCover = Mathf.Pow(scatterCover, 4.0f); Color fogEmissionColor = FogEmissionColor * FogEmissionColor.a; fogEmissionColor.a = FogEmissionColor.a; float fogNoiseSampleCount = (float)(WeatherMakerScript.Instance == null ? FogNoiseSampleCount : WeatherMakerScript.Instance.PerformanceProfile.FogNoiseSampleCount); float invFogNoiseSampleCount = 1.0f / fogNoiseSampleCount; int hasNoiseInt = (HasNoise ? 1 : 0); float ditherLevel = (gamma ? DitherLevel : DitherLevel * 0.5f); Color fogDensityColor = sun.GetGradientColor(FogDensityGradient); fogDensity = FogDensity * fogDensityColor.a; if (!(this is WeatherMakerFullScreenFogProfileScript) || FogMode == WeatherMakerFogMode.None || fogDensity <= 0.0f || MaxFogFactor <= 0.001f) { fogScatterReduction = 1.0f; } else if (FogMode == WeatherMakerFogMode.Exponential) { fogScatterReduction = Mathf.Clamp(1.0f - ((fogDensity + scatterCover) * 0.5f), 0.15f, 1.0f); } else if (FogMode == WeatherMakerFogMode.Linear) { fogScatterReduction = Mathf.Clamp((1.0f - ((fogDensity + scatterCover) * 0.25f)), 0.15f, 1.0f); } else if (FogMode == WeatherMakerFogMode.ExponentialSquared) { fogScatterReduction = Mathf.Clamp((1.0f - ((fogDensity + scatterCover) * 0.75f)), 0.15f, 1.0f); } else if (FogMode == WeatherMakerFogMode.Constant) { fogScatterReduction = Mathf.Clamp(1.0f - (fogDensity + scatterCover), 0.5f, 1.0f); } if (global) { Shader.SetGlobalFloat(WMS._WeatherMakerFogDensityScatter, fogScatterReduction); Shader.SetGlobalInt(WMS._WeatherMakerFogNoiseEnabled, hasNoiseInt); } else { material.SetFloat(WMS._WeatherMakerFogDensityScatter, fogScatterReduction); material.SetInt(WMS._WeatherMakerFogNoiseEnabled, hasNoiseInt); } if (WeatherMakerScript.Instance == null || WeatherMakerScript.Instance.PerformanceProfile.EnableFogLights) { float fogShadowSampleCount = (float)(WeatherMakerScript.Instance == null ? FogShadowSampleCount : WeatherMakerScript.Instance.PerformanceProfile.FogShadowSampleCount); float brightness = Mathf.Min(sun.Light.intensity, Mathf.Pow(sun.Light.intensity, 2.0f)); if (QualitySettings.shadows != ShadowQuality.Disable && fogShadowSampleCount > 0 && sun.Light.intensity > 0.0f && sun.Light.shadows != LightShadows.None && FogShadowMaxRayLength > 0.0f && brightness > 0.0f) { float invFogShadowSampleCount = 1.0f / fogShadowSampleCount; float fogShadowBrightness = brightness * FogShadowBrightness; if (global) { Shader.SetGlobalInt(WMS._WeatherMakerFogVolumetricLightMode, 2); Shader.SetGlobalFloat(WMS._WeatherMakerFogLightShadowSampleCount, fogShadowSampleCount); Shader.SetGlobalFloat(WMS._WeatherMakerFogLightShadowInvSampleCount, invFogShadowSampleCount); Shader.SetGlobalFloat(WMS._WeatherMakerFogLightShadowMaxRayLength, FogShadowMaxRayLength); Shader.SetGlobalFloat(WMS._WeatherMakerFogLightShadowMultiplier, FogShadowMultiplier); Shader.SetGlobalFloat(WMS._WeatherMakerFogLightShadowPower, FogShadowPower); Shader.SetGlobalFloat(WMS._WeatherMakerFogLightShadowBrightness, fogShadowBrightness); Shader.SetGlobalFloat(WMS._WeatherMakerFogLightShadowDecay, FogShadowDecay); Shader.SetGlobalFloat(WMS._WeatherMakerFogLightShadowDither, FogShadowDither); Shader.SetGlobalVector(WMS._WeatherMakerFogLightShadowDitherMagic, FogShadowDitherMagic); Shader.SetGlobalFloat(WMS._WeatherMakerFogCloudShadowStrength, FogCloudShadowStrength); } else { material.SetInt(WMS._WeatherMakerFogVolumetricLightMode, 2); material.SetFloat(WMS._WeatherMakerFogLightShadowSampleCount, fogShadowSampleCount); material.SetFloat(WMS._WeatherMakerFogLightShadowInvSampleCount, invFogShadowSampleCount); material.SetFloat(WMS._WeatherMakerFogLightShadowMaxRayLength, FogShadowMaxRayLength); material.SetFloat(WMS._WeatherMakerFogLightShadowMultiplier, FogShadowMultiplier); material.SetFloat(WMS._WeatherMakerFogLightShadowPower, FogShadowPower); material.SetFloat(WMS._WeatherMakerFogLightShadowBrightness, fogShadowBrightness); material.SetFloat(WMS._WeatherMakerFogLightShadowDecay, FogShadowDecay); material.SetFloat(WMS._WeatherMakerFogLightShadowDither, FogShadowDither); material.SetVector(WMS._WeatherMakerFogLightShadowDitherMagic, FogShadowDitherMagic); material.SetFloat(WMS._WeatherMakerFogCloudShadowStrength, FogCloudShadowStrength); } } else { if (global) { Shader.SetGlobalInt(WMS._WeatherMakerFogVolumetricLightMode, 1); } else { material.SetInt(WMS._WeatherMakerFogVolumetricLightMode, 1); } } } else { if (global) { Shader.SetGlobalInt(WMS._WeatherMakerFogVolumetricLightMode, 0); } else { material.SetInt(WMS._WeatherMakerFogVolumetricLightMode, 0); } } Color fogColor = sun.GetGradientColor(FogGradientColor) * FogColor; FogEndDepth = Mathf.Max(FogStartDepth, FogEndDepth); float endDepth = (FogEndDepth <= FogStartDepth ? FogStartDepth + 5000.0f : FogEndDepth); float fogLinearFogFactor = 0.1f * (1.0f / Mathf.Max(0.0001f, (endDepth - FogStartDepth))) * fogDensity * (endDepth - FogStartDepth); if (global) { Shader.SetGlobalFloat(WMS._WeatherMakerFogStartDepth, FogStartDepth); Shader.SetGlobalFloat(WMS._WeatherMakerFogEndDepth, endDepth); Shader.SetGlobalColor(WMS._WeatherMakerFogColor, fogColor); Shader.SetGlobalColor(WMS._WeatherMakerFogEmissionColor, fogEmissionColor); Shader.SetGlobalFloat(WMS._WeatherMakerFogHeightFalloffPower, FogHeightFalloffPower); Shader.SetGlobalFloat(WMS._WeatherMakerFogLightAbsorption, fogLightAbsorption); Shader.SetGlobalFloat(WMS._WeatherMakerFogLinearFogFactor, fogLinearFogFactor); Shader.SetGlobalFloat(WMS._WeatherMakerFogNoisePercent, fogNoisePercent); Shader.SetGlobalFloat(WMS._WeatherMakerFogNoiseScale, FogNoiseScale); Shader.SetGlobalFloat(WMS._WeatherMakerFogNoiseAdder, FogNoiseAdder); Shader.SetGlobalFloat(WMS._WeatherMakerFogNoiseMultiplier, FogNoiseMultiplier); Shader.SetGlobalVector(WMS._WeatherMakerFogNoiseVelocity, fogNoiseVelocityAccum); Shader.SetGlobalFloat(WMS._WeatherMakerFogNoiseSampleCount, fogNoiseSampleCount); Shader.SetGlobalFloat(WMS._WeatherMakerFogNoiseSampleCountInverse, invFogNoiseSampleCount); Shader.SetGlobalVector(WMS._WeatherMakerFogNoisePositionOffset, fogNoisePositionOffset); Shader.SetGlobalFloat(WMS._WeatherMakerFogFactorMax, MaxFogFactor); Shader.SetGlobalInt(WMS._WeatherMakerFogMode, (int)FogMode); Shader.SetGlobalFloat(WMS._WeatherMakerFogDitherLevel, ditherLevel); Shader.SetGlobalFloat(WMS._WeatherMakerFogDensity, fogDensity); Shader.SetGlobalFloat(WMS._WeatherMakerFogFactorMultiplier, FogFactorMultiplier); } else { material.SetFloat(WMS._WeatherMakerFogStartDepth, FogStartDepth); material.SetFloat(WMS._WeatherMakerFogEndDepth, endDepth); material.SetColor(WMS._WeatherMakerFogColor, fogColor); material.SetColor(WMS._WeatherMakerFogEmissionColor, fogEmissionColor); material.SetFloat(WMS._WeatherMakerFogHeightFalloffPower, FogHeightFalloffPower); material.SetFloat(WMS._WeatherMakerFogLightAbsorption, fogLightAbsorption); material.SetFloat(WMS._WeatherMakerFogLinearFogFactor, fogLinearFogFactor); material.SetFloat(WMS._WeatherMakerFogNoisePercent, fogNoisePercent); material.SetFloat(WMS._WeatherMakerFogNoiseScale, FogNoiseScale); material.SetFloat(WMS._WeatherMakerFogNoiseAdder, FogNoiseAdder); material.SetFloat(WMS._WeatherMakerFogNoiseMultiplier, FogNoiseMultiplier); material.SetVector(WMS._WeatherMakerFogNoiseVelocity, fogNoiseVelocityAccum); material.SetFloat(WMS._WeatherMakerFogNoiseSampleCount, fogNoiseSampleCount); material.SetFloat(WMS._WeatherMakerFogNoiseSampleCountInverse, invFogNoiseSampleCount); material.SetVector(WMS._WeatherMakerFogNoisePositionOffset, fogNoisePositionOffset); material.SetFloat(WMS._WeatherMakerFogFactorMax, MaxFogFactor); material.SetInt(WMS._WeatherMakerFogMode, (int)FogMode); material.SetFloat(WMS._WeatherMakerFogDitherLevel, ditherLevel); material.SetFloat(WMS._WeatherMakerFogDensity, fogDensity); material.SetFloat(WMS._WeatherMakerFogFactorMultiplier, FogFactorMultiplier); } }
private void Strike(Vector3?_start, Vector3?_end, bool intense, float intensity, Camera camera, bool forceVisible) { WeatherMakerCelestialObjectScript sun = WeatherMakerLightManagerScript.SunForCamera(camera); if (sun == null) { return; } // save the generations and trunk width in case of cloud only lightning which will modify these properties int generations = LightningBoltScript.Generations; RangeOfFloats trunkWidth = LightningBoltScript.TrunkWidthRange; if (UnityEngine.Random.value < CloudLightningChance) { // cloud only lightning LightningBoltScript.TrunkWidthRange = new RangeOfFloats(); LightningBoltScript.Generations = 1; } Vector3 anchorPosition = camera.transform.position; Vector3 start = _start ?? CalculateStartPosition(ref anchorPosition, (forceVisible ? camera : null), intense, LightningBoltScript.Generations <= 1); Vector3 end = _end ?? CalculateEndPosition(ref anchorPosition, ref start, (forceVisible ? camera : null), intense); float intensityModifier = 1.0f; CameraMode mode = WeatherMakerScript.ResolveCameraMode(null, camera); if (sun != null) { if (mode != CameraMode.Perspective) { float sunX = (sun.transform.eulerAngles.x + 180.0f); sunX = (sunX >= 360.0f ? sunX - 360.0f : sunX); sunX = Mathf.Abs((sunX * 0.5f) - 90.0f); intensityModifier = Mathf.Lerp(0.1f, 0.75f, sunX * 0.016f); } else { float sunX = (sun.transform.eulerAngles.x + 90.0f); sunX = (sunX >= 360.0f ? sunX - 360.0f : sunX); sunX = Mathf.Abs((sunX * 0.5f) - 90.0f); intensityModifier = Mathf.Lerp(0.1f, 0.75f, sunX * 0.006f); } } LightningBoltScript.LightParameters.LightIntensity = Mathf.Max(1.0f, intensity * intensityModifier); if (mode == CameraMode.Perspective) { if (LightningBoltScript.Generations > 1) { Vector3 lightStart = start; if (camera != null) { // HACK: Pull light toward camera to light clouds better, for some reason the light appears behind the lightning bolt // need to figure out why... Vector3 toLight = camera.transform.position - lightStart; lightStart += (toLight * 0.5f); lightStart.y = 1000.0f; } LightningBoltScript.LightParameters.LightPosition = lightStart; } else { LightningBoltScript.LightParameters.LightPosition = start; } LightningBoltScript.LightParameters.LightRange = Random.Range(3000.0f, 6000.0f); } LightningBoltScript.Trigger(start, end); // restore properties in case they were modified LightningBoltScript.TrunkWidthRange = trunkWidth; LightningBoltScript.Generations = generations; }