Beispiel #1
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);
        }
    }
    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);
    }
Beispiel #3
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);
    }