Exemplo n.º 1
0
        void Start()
        {
            defaultFog        = RenderSettings.fogColor;
            nightFog          = new Color(defaultFog.r * 0.05f, defaultFog.g * 0.05f, defaultFog.b * 0.05f, 1f);
            defaultFogDensity = RenderSettings.fogDensity;

            CloudMaterial.SetFloat("_CloudSpeed", 0.03f);
            StarMaterial.SetFloat("_Exposure", 2.0f);

            WeatherSource.CurrentWeatherState  = WeatherState.LoadFromXML(WeatherSource.XMLWeatherStatePath + "PSWS_FALLBACK");
            WeatherSource.NextWeatherState     = null;
            WeatherSource.WeatherStateBlending = 0;

            StartCoroutine(WeatherSource.WeatherStateChanger());
            StartCoroutine(WeatherSource.UpdateCloudRenderTex());
            StartCoroutine(ReflectionProbeUpdater.UpdateProbe());
        }
Exemplo n.º 2
0
        void Update()
        {
            if (Main.settings.cloudShadowsEnabled)
            {
                SunLight.cookie = WeatherSource.SunShadowRenderImage;
            }
            else
            {
                SunLight.cookie = null;
            }

#if !CYBEX_TIME
            // fauxnik time algo
            ProceduralSkyTimeSource.Instance.CalculateTimeProgress(Time.deltaTime);
            var currentTime = TimeSourceAdapter.GetCurrentTime();
#if DEBUG
            DevGUI devGui = GetComponent <DevGUI>();
            if (devGui != null && devGui.dateTimeOverride)
            {
                currentTime = devGui.CurrentTime;
            }
#endif

            DateToSkyMapper.ApplyDate(currentTime);

            // daily & yearly rotation of skybox night
            SkyboxNight.localRotation = DateToSkyMapper.SkyboxNightRotation;

            // daily & seasonal rotation of the sun
            SunPathCenter.parent.localRotation = DateToSkyMapper.SunPivotRotation;
            SunPathCenter.localPosition        = DateToSkyMapper.SunOffsetFromPath;

            // daily & seasonal rotation of the moon
            MoonPathCenter.parent.localRotation = DateToSkyMapper.MoonPivotRotation;
            MoonPathCenter.localPosition        = DateToSkyMapper.MoonOffsetFromPath;
#else
            // cybex time algo
            CybexTime.CalculateTimeProgress(Main.settings.latitude, 0);

            // rotations
            SkyboxNight.localRotation           = Quaternion.Euler(CybexTime.SkyboxNightRotation);
            SunPathCenter.parent.localRotation  = Quaternion.Euler(CybexTime.SunPivotRotation);
            MoonPathCenter.parent.localRotation = Quaternion.Euler(CybexTime.MoonRotation);

#if DEBUG
            // TODO: update this
            DevGUI devGui = GetComponent <DevGUI>();
            if (devGui != null && devGui.posOverride)
            {
                devGui.CalculateRotationOverride();
                SkyboxNight.localRotation    = devGui.skyRot;
                SunPathCenter.localRotation  = devGui.sunRot;
                MoonPathCenter.localRotation = devGui.moonRot;
            }
#endif
#endif

            // movement
            worldPos           = PlayerManager.PlayerTransform.position - WorldMover.currentMove;
            transform.position = new Vector3(worldPos.x * .001f, 0, worldPos.z * .001f);

            Vector3 highLatitudeCorrection = SunPathCenter.parent.TransformVector(SunPathCenter.localPosition) - SunPathCenter.parent.position / DateToSkyMapper.maxProjectedSunOffset;
            Vector3 sunPos            = SunLight.transform.position - SunPathCenter.position + highLatitudeCorrection;
            float   sunOverHorizonFac = Mathf.Clamp01(sunPos.y);
            SunLight.intensity = sunOverHorizonFac * 1.5f;
            SunLight.color     = Color.Lerp(new Color(1f, 0.5f, 0), Color.white, sunOverHorizonFac);

            StarMaterial.SetFloat("_Visibility", (-sunOverHorizonFac + 1) * .01f);

            MoonMaterial.SetFloat("_MoonDayNight", Mathf.Lerp(2.19f, 1.5f, sunOverHorizonFac));
            // gives aproximate moon phase
            MoonMaterial.SetFloat("_MoonPhase", Vector3.SignedAngle(SunPathCenter.right, MoonPathCenter.right, SunPathCenter.forward) / 180);
            MoonMaterial.SetFloat("_Exposure", Mathf.Lerp(2f, 4f, sunOverHorizonFac));

            SkyMaterial.SetFloat("_Exposure", Mathf.Lerp(.01f, 1f, sunOverHorizonFac));
            SkyMaterial.SetFloat("_AtmosphereThickness", Mathf.Lerp(0.1f, 1f, Mathf.Clamp01(sunOverHorizonFac * 10)));

            CloudMaterial.SetFloat("_NScale", WeatherSource.CloudNoiseScaleBlend);
            CloudMaterial.SetFloat("_ClearSky", WeatherSource.CloudClearSkyBlend);
            float facC = Mathf.Lerp(.002f, 1f, sunOverHorizonFac);
            CloudMaterial.SetFloat("_CloudBright", WeatherSource.CloudBrightnessBlend * facC);
            float facG = Mathf.Lerp(.25f, 0.0f, sunOverHorizonFac);
            CloudMaterial.SetFloat("_CloudGradient", WeatherSource.CloudGradientBlend + facG);
            CloudMaterial.SetFloat("_CloudSpeed", WeatherSource.CloudSpeedBlend);
            CloudMaterial.SetFloat("_CloudChange", WeatherSource.CloudChangeBlend);
#if DEBUG
            if (devGui != null && devGui.cloudOverride)
            {
                CloudMaterial.SetFloat("_NScale", devGui.loadedWeatherState.cloudNoiseScale);
                CloudMaterial.SetFloat("_ClearSky", devGui.loadedWeatherState.cloudClearSky);
                CloudMaterial.SetFloat("_CloudBright", devGui.loadedWeatherState.cloudBrightness * facC);
                CloudMaterial.SetFloat("_CloudGradient", devGui.loadedWeatherState.cloudGradient + facG);
                CloudMaterial.SetFloat("_CloudSpeed", devGui.loadedWeatherState.cloudSpeed);
                CloudMaterial.SetFloat("_CloudChange", devGui.loadedWeatherState.cloudChange);
            }
#endif

            RenderSettings.fogColor        = Color.Lerp(nightFog, defaultFog, sunOverHorizonFac);
            RenderSettings.ambientSkyColor = Color.Lerp(ambientNight, ambientDay, sunOverHorizonFac);

#if DEBUG
            if (devGui != null && devGui.rainOverride)
            {
                RenderSettings.fogDensity = Mathf.Lerp(defaultFogDensity, defaultFogDensity * 3, devGui.loadedWeatherState.rainParticleStrength);
                RainController.SetRainStrength(devGui.loadedWeatherState.rainParticleStrength);
            }
            else
            {
                RenderSettings.fogDensity = Mathf.Lerp(defaultFogDensity, defaultFogDensity * 3, WeatherSource.RainStrengthBlend);
                RainController.SetRainStrength(WeatherSource.RainStrengthBlend);
            }
#else
            RenderSettings.fogDensity = Mathf.Lerp(defaultFogDensity, defaultFogDensity * 3, WeatherSource.RainStrengthBlend);
            RainController.SetRainStrength(WeatherSource.RainStrengthBlend);
#endif

            RainController.SetRainColor(new Color(RenderSettings.fogColor.r + 0.5f, RenderSettings.fogColor.g + 0.5f, RenderSettings.fogColor.b + 0.5f, 1));
        }
Exemplo n.º 3
0
    public void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (CloudMaterial == null || curlNoise == null || blueNoiseTexture == null || lowFreqNoise == null || highFreqNoise == null) // if some script public parameters are missing, do nothing
        {
            Graphics.Blit(source, destination);                                                                                      // do nothing
            return;
        }

        if (_cloudShapeTexture == null) // if shape texture is missing load it in
        {
            _cloudShapeTexture = TGALoader.load3DFromTGASlices(lowFreqNoise);
        }

        if (_cloudDetailTexture == null) // if detail texture is missing load it in
        {
            _cloudDetailTexture = TGALoader.load3DFromTGASlices(highFreqNoise);
        }
        Vector3 cameraPos = CurrentCamera.transform.position;
        // sunLight.rotation.x 364 -> 339, 175 -> 201

        float sunLightFactorUpdated     = sunLightFactor;
        float ambientLightFactorUpdated = ambientLightFactor;
        float sunAngle = sunLight.transform.eulerAngles.x;
        Color sunColor = highSunColor;
        float henyeyGreensteinGBackwardLerp = henyeyGreensteinGBackward;

        float noiseScale = 0.00001f + scale * 0.0004f;

        if (sunAngle > 170.0f) // change sunlight color based on sun's height.
        {
            float gradient  = Mathf.Max(0.0f, (sunAngle - 330.0f) / 30.0f);
            float gradient2 = gradient * gradient;
            sunLightFactorUpdated         *= gradient;
            ambientLightFactorUpdated     *= gradient;
            henyeyGreensteinGBackwardLerp *= gradient2 * gradient;
            ambientLightFactorUpdated      = Mathf.Max(0.02f, ambientLightFactorUpdated);
            sunColor = Color.Lerp(lowSunColor, highSunColor, gradient2);
        }

        updateMaterialKeyword(debugNoLowFreqNoise, "DEBUG_NO_LOW_FREQ_NOISE");
        updateMaterialKeyword(debugNoHighFreqNoise, "DEBUG_NO_HIGH_FREQ_NOISE");
        updateMaterialKeyword(debugNoCurlNoise, "DEBUG_NO_CURL");
        updateMaterialKeyword(allowFlyingInClouds, "ALLOW_IN_CLOUDS");
        updateMaterialKeyword(randomUnitSphere, "RANDOM_UNIT_SPHERE");
        updateMaterialKeyword(aLotMoreLightSamples, "SLOW_LIGHTING");

        switch (randomJitterNoise)
        {
        case RandomJitter.Off:
            updateMaterialKeyword(false, "RANDOM_JITTER_WHITE");
            updateMaterialKeyword(false, "RANDOM_JITTER_BLUE");
            break;

        case RandomJitter.Random:
            updateMaterialKeyword(true, "RANDOM_JITTER_WHITE");
            updateMaterialKeyword(false, "RANDOM_JITTER_BLUE");
            break;

        case RandomJitter.BlueNoise:
            updateMaterialKeyword(false, "RANDOM_JITTER_WHITE");
            updateMaterialKeyword(true, "RANDOM_JITTER_BLUE");
            break;
        }

        // send uniforms to shader
        CloudMaterial.SetVector("_SunDir", sunLight.transform ? (-sunLight.transform.forward).normalized : Vector3.up);
        CloudMaterial.SetVector("_PlanetCenter", planetZeroCoordinate - new Vector3(0, planetSize, 0));
        CloudMaterial.SetVector("_ZeroPoint", planetZeroCoordinate);
        CloudMaterial.SetColor("_SunColor", sunColor);
        //CloudMaterial.SetColor("_SunColor", sunLight.color);

        CloudMaterial.SetColor("_CloudBaseColor", cloudBaseColor);
        CloudMaterial.SetColor("_CloudTopColor", cloudTopColor);
        CloudMaterial.SetFloat("_AmbientLightFactor", ambientLightFactorUpdated);
        CloudMaterial.SetFloat("_SunLightFactor", sunLightFactorUpdated);
        //CloudMaterial.SetFloat("_AmbientLightFactor", sunLight.intensity * ambientLightFactor * 0.3f);
        //CloudMaterial.SetFloat("_SunLightFactor", sunLight.intensity * sunLightFactor);

        CloudMaterial.SetTexture("_ShapeTexture", _cloudShapeTexture);
        CloudMaterial.SetTexture("_DetailTexture", _cloudDetailTexture);
        CloudMaterial.SetTexture("_CurlNoise", curlNoise);
        CloudMaterial.SetTexture("_BlueNoise", blueNoiseTexture);
        CloudMaterial.SetVector("_Randomness", new Vector4(Random.value, Random.value, Random.value, Random.value));
        CloudMaterial.SetTexture("_AltoClouds", cloudsHighTexture);

        CloudMaterial.SetFloat("_CoverageHigh", 1.0f - coverageHigh);
        CloudMaterial.SetFloat("_CoverageHighScale", highCoverageScale * weatherScale * 0.001f);
        CloudMaterial.SetFloat("_HighCloudsScale", highCloudsScale * 0.002f);

        CloudMaterial.SetFloat("_CurlDistortAmount", 150.0f + curlDistortAmount);
        CloudMaterial.SetFloat("_CurlDistortScale", curlDistortScale * noiseScale);

        CloudMaterial.SetFloat("_LightConeRadius", lightConeRadius);
        CloudMaterial.SetFloat("_LightStepLength", lightStepLength);
        CloudMaterial.SetFloat("_SphereSize", planetSize);
        CloudMaterial.SetVector("_CloudHeightMinMax", new Vector2(startHeight, startHeight + thickness));
        CloudMaterial.SetFloat("_Thickness", thickness);
        CloudMaterial.SetFloat("_Scale", noiseScale);
        CloudMaterial.SetFloat("_DetailScale", detailScale * noiseScale);
        CloudMaterial.SetVector("_LowFreqMinMax", new Vector4(lowFreqMin, lowFreqMax));
        CloudMaterial.SetFloat("_HighFreqModifier", highFreqModifier);
        CloudMaterial.SetFloat("_WeatherScale", weatherScale * 0.00025f);
        CloudMaterial.SetFloat("_Coverage", 1.0f - coverage);
        CloudMaterial.SetFloat("_HenyeyGreensteinGForward", henyeyGreensteinGForward);
        CloudMaterial.SetFloat("_HenyeyGreensteinGBackward", -henyeyGreensteinGBackwardLerp);
        if (adjustDensity)
        {
            CloudMaterial.SetFloat("_SampleMultiplier", cloudSampleMultiplier * stepDensityAdjustmentCurve.Evaluate(steps / 256.0f));
        }
        else
        {
            CloudMaterial.SetFloat("_SampleMultiplier", cloudSampleMultiplier);
        }


        CloudMaterial.SetFloat("_Density", density);

        CloudMaterial.SetFloat("_WindSpeed", _multipliedWindSpeed);
        CloudMaterial.SetVector("_WindDirection", _windDirectionVector);
        CloudMaterial.SetVector("_WindOffset", _windOffset);
        CloudMaterial.SetVector("_CoverageWindOffset", _coverageWindOffset);
        CloudMaterial.SetVector("_HighCloudsWindOffset", _highCloudsWindOffset);

        CloudMaterial.SetVector("_Gradient1", gradientToVector4(gradientLow));
        CloudMaterial.SetVector("_Gradient2", gradientToVector4(gradientMed));
        CloudMaterial.SetVector("_Gradient3", gradientToVector4(gradientHigh));

        CloudMaterial.SetInt("_Steps", steps);
        CloudMaterial.SetInt("_renderInFront", renderInFront);//v0.1 choose to render in front of objects for reflections

        CloudMaterial.SetMatrix("_FrustumCornersES", GetFrustumCorners(CurrentCamera));
        CloudMaterial.SetMatrix("_CameraInvViewMatrix", CurrentCamera.cameraToWorldMatrix);
        CloudMaterial.SetVector("_CameraWS", cameraPos);
        CloudMaterial.SetVector("_CameraWSOffset", _CameraWSOffset);
        CloudMaterial.SetFloat("_FarPlane", CurrentCamera.farClipPlane);



        //v0.2
        //v3.5.3
        CloudMaterial.SetTexture("_InteractTexture", _InteractTexture);
        CloudMaterial.SetVector("_InteractTexturePos", _InteractTexturePos);
        CloudMaterial.SetVector("_InteractTextureAtr", _InteractTextureAtr);
        CloudMaterial.SetVector("_InteractTextureOffset", _InteractTextureOffset); //v4.0


        //////// SCATTER
        //Camera cam = GetComponent<Camera>(); //v2.1.15
        //Transform camtr = cam.transform;
        //float camNear = cam.nearClipPlane;
        //float camFar = cam.farClipPlane;
        //float camFov = cam.fieldOfView;
        //float camAspect = cam.aspect;

        //Matrix4x4 frustumCorners = Matrix4x4.identity;

        //float fovWHalf = camFov * 0.5f;

        //Vector3 toRight = camtr.right * camNear * Mathf.Tan(fovWHalf * Mathf.Deg2Rad) * camAspect;
        //Vector3 toTop = camtr.up * camNear * Mathf.Tan(fovWHalf * Mathf.Deg2Rad);

        //Vector3 topLeft = (camtr.forward * camNear - toRight + toTop);
        //float camScale = topLeft.magnitude * camFar / camNear;

        //topLeft.Normalize();
        //topLeft *= camScale;

        //Vector3 topRight = (camtr.forward * camNear + toRight + toTop);
        //topRight.Normalize();
        //topRight *= camScale;

        //Vector3 bottomRight = (camtr.forward * camNear + toRight - toTop);
        //bottomRight.Normalize();
        //bottomRight *= camScale;

        //Vector3 bottomLeft = (camtr.forward * camNear - toRight - toTop);
        //bottomLeft.Normalize();
        //bottomLeft *= camScale;

        //frustumCorners.SetRow(0, topLeft);
        //frustumCorners.SetRow(1, topRight);
        //frustumCorners.SetRow(2, bottomRight);
        //frustumCorners.SetRow(3, bottomLeft);

        CloudMaterial.SetInt("scatterOn", scatterOn);             //v0.3
        CloudMaterial.SetInt("sunRaysOn", sunRaysOn);             //v0.3
        CloudMaterial.SetFloat("zeroCountSteps", zeroCountSteps); //v0.3
        CloudMaterial.SetInt("sunShaftSteps", sunShaftSteps);     //v0.3

        //var camPos = camtr.position;
        float FdotC  = CurrentCamera.transform.position.y - height;
        float paramK = (FdotC <= 0.0f ? 1.0f : 0.0f);

        //fogMaterial.SetMatrix("_FrustumCornersWS", frustumCorners);
        //fogMaterial.SetVector("_CameraWS", camPos);
        CloudMaterial.SetVector("_HeightParams", new Vector4(height, FdotC, paramK, heightDensity * 0.5f));
        CloudMaterial.SetVector("_DistanceParams", new Vector4(-Mathf.Max(startDistance, 0.0f), 0, 0, 0));


        //v3.5.1
        //fogMaterial.SetFloat("_NearZCutoff", _NearZCutoff);
        //fogMaterial.SetFloat("_HorizonYAdjust", _HorizonYAdjust);
        //fogMaterial.SetFloat("_HorizonZAdjust", _HorizonZAdjust);//v2.1.24
        //fogMaterial.SetFloat("_FadeThreshold", _FadeThreshold);

        //v3.5
        //fogMaterial.SetFloat("_SampleCount0", _SampleCount0);
        //fogMaterial.SetFloat("_SampleCount1", _SampleCount1);
        //fogMaterial.SetInt("_SampleCountL", _SampleCountL);

        //fogMaterial.SetFloat("_NoiseFreq1", _NoiseFreq1);
        //fogMaterial.SetFloat("_NoiseFreq2", _NoiseFreq2);
        //fogMaterial.SetFloat("_NoiseAmp1", _NoiseAmp1);
        //fogMaterial.SetFloat("_NoiseAmp2", _NoiseAmp2);
        //fogMaterial.SetFloat("_NoiseBias", _NoiseBias);

        //fogMaterial.SetVector("_Scroll1", _Scroll1);
        //fogMaterial.SetVector("_Scroll2", _Scroll2);

        //fogMaterial.SetFloat("_Altitude0", _Altitude0);
        //fogMaterial.SetFloat("_Altitude1", _Altitude1);
        //fogMaterial.SetFloat("_FarDist", _FarDist);

        CloudMaterial.SetFloat("_Scatter", _Scatter);
        CloudMaterial.SetFloat("_HGCoeff", _HGCoeff);
        CloudMaterial.SetFloat("_Extinct", _Extinct);


        // CloudMaterial.SetFloat("_Exposure", _ExposureUnder); //v4.0
        //  CloudMaterial.SetVector("_GroundColor", _GroundColor);//
        //  CloudMaterial.SetFloat("_SunSize", _SunSize);
        CloudMaterial.SetVector("_SkyTint", _SkyTint);
        //fogMaterial.SetFloat("_AtmosphereThickness", _AtmosphereThickness);
        //v3.5
        CloudMaterial.SetFloat("_BackShade", _BackShade);
        //fogMaterial.SetFloat("_UndersideCurveFactor", _UndersideCurveFactor);

        //v2.1.19
        if (localLight != null)
        {
            Vector3 localLightPos = localLight.transform.position;
            //float intensity = Mathf.Pow(10, 3 + (localLightFalloff - 3) * 3);
            currentLocalLightIntensity = Mathf.Pow(10, 3 + (localLightFalloff - 3) * 3);
            //fogMaterial.SetVector ("_LocalLightPos", new Vector4 (localLightPos.x, localLightPos.y, localLightPos.z, localLight.intensity * localLightIntensity * intensity));
            CloudMaterial.SetVector("_LocalLightPos", new Vector4(localLightPos.x, localLightPos.y, localLightPos.z, localLight.intensity * localLightIntensity * currentLocalLightIntensity));
            CloudMaterial.SetVector("_LocalLightColor", new Vector4(localLight.color.r, localLight.color.g, localLight.color.b, localLightFalloff));
        }
        else
        {
            if (currentLocalLightIntensity > 0)
            {
                currentLocalLightIntensity = 0;
                //fogMaterial.SetVector ("_LocalLightPos", new Vector4 (localLightPos.x, localLightPos.y, localLightPos.z, localLight.intensity * localLightIntensity * intensity));
                CloudMaterial.SetVector("_LocalLightColor", Vector4.zero);
            }
        }

        //SM v1.7
        CloudMaterial.SetFloat("luminance", luminance);
        CloudMaterial.SetFloat("lumFac", lumFac);
        CloudMaterial.SetFloat("Multiplier1", ScatterFac);
        CloudMaterial.SetFloat("Multiplier2", TurbFac);
        CloudMaterial.SetFloat("Multiplier3", HorizFac);
        CloudMaterial.SetFloat("turbidity", turbidity);
        CloudMaterial.SetFloat("reileigh", reileigh);
        CloudMaterial.SetFloat("mieCoefficient", mieCoefficient);
        CloudMaterial.SetFloat("mieDirectionalG", mieDirectionalG);
        CloudMaterial.SetFloat("bias", bias);
        CloudMaterial.SetFloat("contrast", contrast);
        CloudMaterial.SetVector("v3LightDir", -sunLight.transform.forward);
        CloudMaterial.SetVector("_TintColor", new Vector4(TintColor.x, TintColor.y, TintColor.z, 1));//68, 155, 345

        float Foggy = 0;

        if (FogSky)
        {
            Foggy = 1;
        }
        CloudMaterial.SetFloat("FogSky", Foggy);
        CloudMaterial.SetFloat("ClearSkyFac", ClearSkyFac);

        var     sceneMode    = RenderSettings.fogMode;
        var     sceneDensity = 0.01f; //RenderSettings.fogDensity;//v3.0
        var     sceneStart   = RenderSettings.fogStartDistance;
        var     sceneEnd     = RenderSettings.fogEndDistance;
        Vector4 sceneParams;
        bool    linear  = (sceneMode == FogMode.Linear);
        float   diff    = linear ? sceneEnd - sceneStart : 0.0f;
        float   invDiff = Mathf.Abs(diff) > 0.0001f ? 1.0f / diff : 0.0f;

        sceneParams.x = sceneDensity * 1.2011224087f; // density / sqrt(ln(2)), used by Exp2 fog mode
        sceneParams.y = sceneDensity * 1.4426950408f; // density / ln(2), used by Exp fog mode
        sceneParams.z = linear ? -invDiff : 0.0f;
        sceneParams.w = linear ? sceneEnd * invDiff : 0.0f;
        CloudMaterial.SetVector("_SceneFogParams", sceneParams);
        CloudMaterial.SetVector("_SceneFogMode", new Vector4((int)sceneMode, useRadialDistance ? 1 : 0, 0, 0));


        ////////// END SCATTER



        // get cloud render texture and render clouds to it
        RenderTexture rtClouds = RenderTexture.GetTemporary((int)(source.width / ((float)downSample)), (int)(source.height / ((float)downSample)), 0, source.format, RenderTextureReadWrite.Default, source.antiAliasing);

        CustomGraphicsBlit(source, rtClouds, CloudMaterial, 0);

        //v5.1.1
        if (enableEdgeControl)
        {
            //UpscaleMaterial.SetTexture("_Clouds", rtClouds);
            UpscaleMaterial.SetVector("edgeControl", edgeControl);
            //UpscaleMaterial.SetInt("enableEdges", 1);
            if (debugClouds > 0)
            {
                UpscaleMaterial.SetInt("enableEdges", debugClouds - 1);
            }
            else
            {
                UpscaleMaterial.SetInt("enableEdges", 1);
            }
            RenderTexture rtTemporalA = RenderTexture.GetTemporary(rtClouds.width, rtClouds.height, 0, rtClouds.format, RenderTextureReadWrite.Default);
            //v5.1.1
            // CustomGraphicsBlit(rtClouds, rtTemporalA, UpscaleMaterial, 1);
            Graphics.Blit(rtClouds, rtTemporalA, UpscaleMaterial, 1);

            if (temporalAntiAliasing && Application.isPlaying) // if TAA is enabled, then apply it to cloud render texture //v5.1.0
            {
                RenderTexture rtTemporal = RenderTexture.GetTemporary(rtClouds.width, rtClouds.height, 0, rtClouds.format, RenderTextureReadWrite.Default, source.antiAliasing);
                _temporalAntiAliasing.TemporalAntiAliasing(rtTemporalA, rtTemporal);
                UpscaleMaterial.SetTexture("_Clouds", rtTemporal);

                // Apply clouds to background
                if (debugClouds > 0)
                {
                    Graphics.Blit(rtTemporalA, destination, UpscaleMaterial, 0);
                }
                else
                {
                    Graphics.Blit(source, destination, UpscaleMaterial, 0);
                }

                RenderTexture.ReleaseTemporary(rtTemporal);
            }
            else
            {
                UpscaleMaterial.SetTexture("_Clouds", rtTemporalA); //

                // Apply clouds to background
                if (debugClouds > 0)
                {
                    Graphics.Blit(rtTemporalA, destination, UpscaleMaterial, 0);
                }
                else
                {
                    Graphics.Blit(source, destination, UpscaleMaterial, 0);
                }
            }

            RenderTexture.ReleaseTemporary(rtClouds); RenderTexture.ReleaseTemporary(rtTemporalA);
        }
        else
        {
            UpscaleMaterial.SetInt("enableEdges", 0);
            if (temporalAntiAliasing && Application.isPlaying) // if TAA is enabled, then apply it to cloud render texture //v5.1.0
            {
                RenderTexture rtTemporal = RenderTexture.GetTemporary(rtClouds.width, rtClouds.height, 0, rtClouds.format, RenderTextureReadWrite.Default, source.antiAliasing);

                _temporalAntiAliasing.TemporalAntiAliasing(rtClouds, rtTemporal);
                UpscaleMaterial.SetTexture("_Clouds", rtTemporal);
                RenderTexture.ReleaseTemporary(rtTemporal);
            }
            else
            {
                UpscaleMaterial.SetTexture("_Clouds", rtClouds); //
            }
            // Apply clouds to background
            Graphics.Blit(source, destination, UpscaleMaterial, 0);
            RenderTexture.ReleaseTemporary(rtClouds);
        }
    }
Exemplo n.º 4
0
    public void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (CloudMaterial == null || curlNoise == null || blueNoiseTexture == null || lowFreqNoise == null || highFreqNoise == null) // if some script public parameters are missing, do nothing
        {
            Graphics.Blit(source, destination);                                                                                      // do nothing
            return;
        }

        if (_cloudShapeTexture == null) // if shape texture is missing load it in
        {
            _cloudShapeTexture = TGALoader.load3DFromTGASlices(lowFreqNoise);
        }

        if (_cloudDetailTexture == null) // if detail texture is missing load it in
        {
            _cloudDetailTexture = TGALoader.load3DFromTGASlices(highFreqNoise);
        }
        Vector3 cameraPos = CurrentCamera.transform.position;
        // sunLight.rotation.x 364 -> 339, 175 -> 201

        float sunLightFactorUpdated     = sunLightFactor;
        float ambientLightFactorUpdated = ambientLightFactor;
        float sunAngle = sunLight.transform.eulerAngles.x;
        Color sunColor = highSunColor;
        float henyeyGreensteinGBackwardLerp = henyeyGreensteinGBackward;

        float noiseScale = 0.00001f + scale * 0.0004f;

        if (sunAngle > 170.0f) // change sunlight color based on sun's height.
        {
            float gradient  = Mathf.Max(0.0f, (sunAngle - 330.0f) / 30.0f);
            float gradient2 = gradient * gradient;
            sunLightFactorUpdated         *= gradient;
            ambientLightFactorUpdated     *= gradient;
            henyeyGreensteinGBackwardLerp *= gradient2 * gradient;
            ambientLightFactorUpdated      = Mathf.Max(0.02f, ambientLightFactorUpdated);
            sunColor = Color.Lerp(lowSunColor, highSunColor, gradient2);
        }

        updateMaterialKeyword(debugNoLowFreqNoise, "DEBUG_NO_LOW_FREQ_NOISE");
        updateMaterialKeyword(debugNoHighFreqNoise, "DEBUG_NO_HIGH_FREQ_NOISE");
        updateMaterialKeyword(debugNoCurlNoise, "DEBUG_NO_CURL");
        updateMaterialKeyword(allowFlyingInClouds, "ALLOW_IN_CLOUDS");
        updateMaterialKeyword(randomUnitSphere, "RANDOM_UNIT_SPHERE");
        updateMaterialKeyword(aLotMoreLightSamples, "SLOW_LIGHTING");

        switch (randomJitterNoise)
        {
        case RandomJitter.Off:
            updateMaterialKeyword(false, "RANDOM_JITTER_WHITE");
            updateMaterialKeyword(false, "RANDOM_JITTER_BLUE");
            break;

        case RandomJitter.Random:
            updateMaterialKeyword(true, "RANDOM_JITTER_WHITE");
            updateMaterialKeyword(false, "RANDOM_JITTER_BLUE");
            break;

        case RandomJitter.BlueNoise:
            updateMaterialKeyword(false, "RANDOM_JITTER_WHITE");
            updateMaterialKeyword(true, "RANDOM_JITTER_BLUE");
            break;
        }

        // send uniforms to shader
        CloudMaterial.SetVector("_SunDir", sunLight.transform ? (-sunLight.transform.forward).normalized : Vector3.up);
        CloudMaterial.SetVector("_PlanetCenter", planetZeroCoordinate - new Vector3(0, planetSize, 0));
        CloudMaterial.SetVector("_ZeroPoint", planetZeroCoordinate);
        CloudMaterial.SetColor("_SunColor", sunColor);
        //CloudMaterial.SetColor("_SunColor", sunLight.color);

        CloudMaterial.SetColor("_CloudBaseColor", cloudBaseColor);
        CloudMaterial.SetColor("_CloudTopColor", cloudTopColor);
        CloudMaterial.SetFloat("_AmbientLightFactor", ambientLightFactorUpdated);
        CloudMaterial.SetFloat("_SunLightFactor", sunLightFactorUpdated);
        //CloudMaterial.SetFloat("_AmbientLightFactor", sunLight.intensity * ambientLightFactor * 0.3f);
        //CloudMaterial.SetFloat("_SunLightFactor", sunLight.intensity * sunLightFactor);

        CloudMaterial.SetTexture("_ShapeTexture", _cloudShapeTexture);
        CloudMaterial.SetTexture("_DetailTexture", _cloudDetailTexture);
        CloudMaterial.SetTexture("_CurlNoise", curlNoise);
        CloudMaterial.SetTexture("_BlueNoise", blueNoiseTexture);
        CloudMaterial.SetVector("_Randomness", new Vector4(Random.value, Random.value, Random.value, Random.value));
        CloudMaterial.SetTexture("_AltoClouds", cloudsHighTexture);

        CloudMaterial.SetFloat("_CoverageHigh", 1.0f - coverageHigh);
        CloudMaterial.SetFloat("_CoverageHighScale", highCoverageScale * weatherScale * 0.001f);
        CloudMaterial.SetFloat("_HighCloudsScale", highCloudsScale * 0.002f);

        CloudMaterial.SetFloat("_CurlDistortAmount", 150.0f + curlDistortAmount);
        CloudMaterial.SetFloat("_CurlDistortScale", curlDistortScale * noiseScale);

        CloudMaterial.SetFloat("_LightConeRadius", lightConeRadius);
        CloudMaterial.SetFloat("_LightStepLength", lightStepLength);
        CloudMaterial.SetFloat("_SphereSize", planetSize);
        CloudMaterial.SetVector("_CloudHeightMinMax", new Vector2(startHeight, startHeight + thickness));
        CloudMaterial.SetFloat("_Thickness", thickness);
        CloudMaterial.SetFloat("_Scale", noiseScale);
        CloudMaterial.SetFloat("_DetailScale", detailScale * noiseScale);
        CloudMaterial.SetVector("_LowFreqMinMax", new Vector4(lowFreqMin, lowFreqMax));
        CloudMaterial.SetFloat("_HighFreqModifier", highFreqModifier);
        CloudMaterial.SetFloat("_WeatherScale", weatherScale * 0.00025f);
        CloudMaterial.SetFloat("_Coverage", 1.0f - coverage);
        CloudMaterial.SetFloat("_HenyeyGreensteinGForward", henyeyGreensteinGForward);
        CloudMaterial.SetFloat("_HenyeyGreensteinGBackward", -henyeyGreensteinGBackwardLerp);
        if (adjustDensity)
        {
            CloudMaterial.SetFloat("_SampleMultiplier", cloudSampleMultiplier * stepDensityAdjustmentCurve.Evaluate(steps / 256.0f));
        }
        else
        {
            CloudMaterial.SetFloat("_SampleMultiplier", cloudSampleMultiplier);
        }


        CloudMaterial.SetFloat("_Density", density);

        CloudMaterial.SetFloat("_WindSpeed", _multipliedWindSpeed);
        CloudMaterial.SetVector("_WindDirection", _windDirectionVector);
        CloudMaterial.SetVector("_WindOffset", _windOffset);
        CloudMaterial.SetVector("_CoverageWindOffset", _coverageWindOffset);
        CloudMaterial.SetVector("_HighCloudsWindOffset", _highCloudsWindOffset);

        CloudMaterial.SetVector("_Gradient1", gradientToVector4(gradientLow));
        CloudMaterial.SetVector("_Gradient2", gradientToVector4(gradientMed));
        CloudMaterial.SetVector("_Gradient3", gradientToVector4(gradientHigh));

        CloudMaterial.SetInt("_Steps", steps);

        CloudMaterial.SetMatrix("_FrustumCornersES", GetFrustumCorners(CurrentCamera));
        CloudMaterial.SetMatrix("_CameraInvViewMatrix", CurrentCamera.cameraToWorldMatrix);
        CloudMaterial.SetVector("_CameraWS", cameraPos);
        CloudMaterial.SetFloat("_FarPlane", CurrentCamera.farClipPlane);

        // get cloud render texture and render clouds to it
        RenderTexture rtClouds = RenderTexture.GetTemporary((int)(source.width / ((float)downSample)), (int)(source.height / ((float)downSample)), 0, source.format, RenderTextureReadWrite.Default, source.antiAliasing);

        CustomGraphicsBlit(source, rtClouds, CloudMaterial, 0);

        if (temporalAntiAliasing) // if TAA is enabled, then apply it to cloud render texture
        {
            RenderTexture rtTemporal = RenderTexture.GetTemporary(rtClouds.width, rtClouds.height, 0, rtClouds.format, RenderTextureReadWrite.Default, source.antiAliasing);
            _temporalAntiAliasing.TemporalAntiAliasing(rtClouds, rtTemporal);
            UpscaleMaterial.SetTexture("_Clouds", rtTemporal);
            RenderTexture.ReleaseTemporary(rtTemporal);
        }
        else
        {
            UpscaleMaterial.SetTexture("_Clouds", rtClouds);
        }
        // Apply clouds to background
        Graphics.Blit(source, destination, UpscaleMaterial, 0);
        RenderTexture.ReleaseTemporary(rtClouds);
    }
Exemplo n.º 5
0
    public void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (CloudMaterial == null)              // if some script public parameters are missing, do nothing
        {
            Graphics.Blit(source, destination); // do nothing
            return;
        }

        Vector3 cameraPos = CurrentCamera.transform.position;

        float sunLightFactorUpdated     = sunLightFactor;
        float ambientLightFactorUpdated = ambientLightFactor;
        float sunAngle = sunLight.transform.eulerAngles.x;
        Color sunColor = highSunColor;
        float henyeyGreensteinGBackwardLerp = henyeyGreensteinGBackward;

        float noiseScale = 0.00001f + scale * 0.0004f;

        if (sunAngle > 170.0f) // change sunlight color based on sun's height.
        {
            float gradient  = Mathf.Max(0.0f, (sunAngle - 330.0f) / 30.0f);
            float gradient2 = gradient * gradient;
            sunLightFactorUpdated         *= gradient;
            ambientLightFactorUpdated     *= gradient;
            henyeyGreensteinGBackwardLerp *= gradient2 * gradient;
            ambientLightFactorUpdated      = Mathf.Max(0.02f, ambientLightFactorUpdated);
            sunColor = Color.Lerp(lowSunColor, highSunColor, gradient2);
        }

        updateMaterialKeyword(debugNoLowFreqNoise, "DEBUG_NO_LOW_FREQ_NOISE");
        updateMaterialKeyword(debugNoHighFreqNoise, "DEBUG_NO_HIGH_FREQ_NOISE");

        updateMaterialKeyword(debugNoBeer, "DEBUG_NO_BEER");
        updateMaterialKeyword(debugNoPowderEffect, "DEBUG_NO_POWDER_EFFECT");
        updateMaterialKeyword(debugNoHG, "DEBUG_NO_HG");
        updateMaterialKeyword(debugNoGradient, "DEBUG_NO_GRADIENT");
        updateMaterialKeyword(debugNoLightCone, "DEBUG_NO_LIGHT_CONE");

        CloudMaterial.SetVector("_SunDir", sunLight.transform ? (-sunLight.transform.forward).normalized : Vector3.up);
        CloudMaterial.SetVector("_PlanetCenter", planetZeroCoordinate - new Vector3(0, planetSize, 0));
        CloudMaterial.SetVector("_ZeroPoint", planetZeroCoordinate);
        CloudMaterial.SetColor("_SunColor", sunColor);

        CloudMaterial.SetColor("_CloudBaseColor", cloudBaseColor);
        CloudMaterial.SetColor("_CloudTopColor", cloudTopColor);
        CloudMaterial.SetFloat("_AmbientLightFactor", ambientLightFactorUpdated);
        CloudMaterial.SetFloat("_SunLightFactor", sunLightFactorUpdated);

        CloudMaterial.SetTexture("_ShapeTexture", _cloudShapeTexture);
        CloudMaterial.SetTexture("_DetailTexture", _cloudDetailTexture);
        CloudMaterial.SetVector("_Randomness", new Vector4(Random.value, Random.value, Random.value, Random.value));

        CloudMaterial.SetFloat("_LightConeRadius", lightConeRadius);
        CloudMaterial.SetFloat("_LightStepLength", lightStepLength);
        CloudMaterial.SetFloat("_SphereSize", planetSize);
        CloudMaterial.SetVector("_CloudHeightMinMax", new Vector2(startHeight, startHeight + thickness));
        CloudMaterial.SetFloat("_Thickness", thickness);
        CloudMaterial.SetFloat("_Scale", noiseScale);
        CloudMaterial.SetFloat("_DetailScale", detailScale * noiseScale);
        CloudMaterial.SetVector("_LowFreqMinMax", new Vector4(lowFreqMin, lowFreqMax));
        CloudMaterial.SetFloat("_HighFreqModifier", highFreqModifier);
        CloudMaterial.SetFloat("_WeatherScale", weatherScale * 0.00025f);
        CloudMaterial.SetFloat("_Coverage", 1.0f - coverage);
        CloudMaterial.SetFloat("_HenyeyGreensteinGForward", henyeyGreensteinGForward);
        CloudMaterial.SetFloat("_HenyeyGreensteinGBackward", -henyeyGreensteinGBackwardLerp);
        if (adjustDensity)
        {
            CloudMaterial.SetFloat("_SampleMultiplier", cloudSampleMultiplier * stepDensityAdjustmentCurve.Evaluate(steps / 256.0f));
        }
        else
        {
            CloudMaterial.SetFloat("_SampleMultiplier", cloudSampleMultiplier);
        }


        CloudMaterial.SetFloat("_Density", density);
        CloudMaterial.SetTexture("_WeatherTexture", weatherTexture);

        CloudMaterial.SetVector("_WindDirection", _windDirectionVector);
        CloudMaterial.SetVector("_WindOffset", _windOffset);
        CloudMaterial.SetVector("_CoverageWindOffset", _coverageWindOffset);

        CloudMaterial.SetVector("_Gradient1", gradientToVector4(gradientLow));
        CloudMaterial.SetVector("_Gradient2", gradientToVector4(gradientMed));
        CloudMaterial.SetVector("_Gradient3", gradientToVector4(gradientHigh));

        CloudMaterial.SetInt("_Steps", steps);

        CloudMaterial.SetMatrix("_FrustumCornersES", GetFrustumCorners(CurrentCamera));
        CloudMaterial.SetMatrix("_CameraInvViewMatrix", CurrentCamera.cameraToWorldMatrix);
        CloudMaterial.SetVector("_CameraWS", cameraPos);
        CloudMaterial.SetFloat("_FarPlane", CurrentCamera.farClipPlane);

        // get cloud render texture and render clouds to it
        RenderTexture rtClouds = RenderTexture.GetTemporary((int)(source.width / ((float)downSample)), (int)(source.height / ((float)downSample)), 0, source.format, RenderTextureReadWrite.Default, source.antiAliasing);

        CustomGraphicsBlit(source, rtClouds, CloudMaterial, 0);

        UpscaleMaterial.SetTexture("_Clouds", rtClouds);

        // Apply clouds to background
        Graphics.Blit(source, destination, UpscaleMaterial, 0);
        //Graphics.Blit(rtClouds, destination);
        RenderTexture.ReleaseTemporary(rtClouds);
    }