Example #1
0
        private void OnRenderImage(RenderTexture source, RenderTexture destination)
        {
            FogTypePass fogType = SetMaterialUniforms();

            if (fogType == FogTypePass.None)
            {
                Graphics.Blit(source, destination);
            }
            else
            {
                Graphics.Blit(source, destination, material, (int)fogType);
            }
        }
        private FogTypePass SetMaterialUniforms(Camera camera)
        {
            // Determine the fog type pass
            FogTypePass fogType = FogTypePass.DistanceOnly;

            if (!distanceFog.enabled && heightFog.enabled)
            {
                fogType = FogTypePass.HeightOnly;
            }

            // Share color settings if one of the sources are set to copy the other
            bool sharedColorSettings = (distanceFog.colorSelectionType == ColorSelectionType.CopyOther) ||
                                       (heightFog.colorSelectionType == ColorSelectionType.CopyOther);

            if (distanceFog.enabled && heightFog.enabled)
            {
                if (sharedColorSettings)
                {
                    fogType = FogTypePass.BothSharedColorSettings;
                }
                else
                {
                    fogType = FogTypePass.BothSeperateColorSettinsg;
                }
            }

            if (!distanceFog.enabled && !heightFog.enabled)
            {
                return(FogTypePass.None);
            }

            // Get the inverse view matrix for converting depth to world position.
            Matrix4x4 inverseViewMatrix = camera.cameraToWorldMatrix;

            Material.SetMatrix("_InverseViewMatrix", inverseViewMatrix);

            // Decide wheter the skybox should have fog applied
            Material.SetInt("_ApplyDistToSkybox", distanceFog.fogSkybox ? 1 : 0);
            Material.SetInt("_ApplyHeightToSkybox", heightFog.fogSkybox ? 1 : 0);

            // Is the shared color sampled from a texture? Otherwise it's from a single color( picker)
            if (sharedColorSettings)
            {
                bool               selectingFromDistance = true;
                FogColorSource     activeSelectionSource = distanceColorSource;
                ColorSelectionType activeSelectionType   = distanceFog.colorSelectionType;
                if (activeSelectionType == ColorSelectionType.CopyOther)
                {
                    activeSelectionType   = heightFog.colorSelectionType;
                    activeSelectionSource = heightColorSource;
                    selectingFromDistance = false;
                }

                SetDistanceFogUniforms();
                SetHeightFogUniforms();

                if (activeSelectionType == ColorSelectionType.Gradient)
                {
                    Material.SetTexture("_FogColorTexture0", selectingFromDistance ? DistanceColorTexture : HeightColorTexture);
                }
                else
                {
                    Material.SetTexture("_FogColorTexture0", selectingFromDistance ? distanceColorSource.colorRamp : heightColorSource.colorRamp);
                }
            }
            else
            {
                if (distanceFog.enabled)
                {
                    Material.SetTexture("_FogColorTexture0", distanceFog.colorSelectionType == ColorSelectionType.Gradient ? DistanceColorTexture : distanceColorSource.colorRamp);
                }

                if (heightFog.enabled)
                {
                    string colorTextureIdentifier = fogType == FogTypePass.HeightOnly ? "_FogColorTexture0" : "_FogColorTexture1";
                    Material.SetTexture(colorTextureIdentifier, heightFog.colorSelectionType == ColorSelectionType.Gradient ? HeightColorTexture : heightColorSource.colorRamp);
                }
            }

            // Set distance fog properties
            if (distanceFog.enabled)
            {
                SetDistanceFogUniforms();
            }

            // Set height fog properties
            if (heightFog.enabled)
            {
                SetHeightFogUniforms();
            }

            return(fogType);
        }