Search Unity

Global fog change colour?

Discussion in 'Editor & General Support' started by serbusfish, Mar 15, 2017.

  1. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    247
    I'm using the standard unity global fog script on my camera but there is no option to change its colour and I cant see any colour related lines in the script file. Is it possible to change it?

    Code (csharp):
    1.  
    2.  
    3. using System;
    4. using UnityEngine;
    5.  
    6. namespace UnityStandardAssets.ImageEffects
    7. {
    8.     [ExecuteInEditMode]
    9.     [RequireComponent (typeof(Camera))]
    10.     [AddComponentMenu ("Image Effects/Rendering/Global Fog")]
    11.     public class GlobalFog : PostEffectsBase
    12.    {
    13.        [Tooltip("Apply distance-based fog?")]
    14.         public bool  distanceFog = true;
    15.        [Tooltip("Distance fog is based on radial distance from camera when checked")]
    16.        public bool  useRadialDistance = false;
    17.        [Tooltip("Apply height-based fog?")]
    18.        public bool  heightFog = true;
    19.        [Tooltip("Fog top Y coordinate")]
    20.         public float height = 1.0f;
    21.         [Range(0.001f,10.0f)]
    22.         public float heightDensity = 2.0f;
    23.        [Tooltip("Push fog away from the camera by this amount")]
    24.         public float startDistance = 0.0f;
    25.  
    26.         public Shader fogShader = null;
    27.         private Material fogMaterial = null;
    28.  
    29.  
    30.         public override bool CheckResources ()
    31.        {
    32.             CheckSupport (true);
    33.  
    34.             fogMaterial = CheckShaderAndCreateMaterial (fogShader, fogMaterial);
    35.  
    36.             if (!isSupported)
    37.                 ReportAutoDisable ();
    38.             return isSupported;
    39.         }
    40.  
    41.         [ImageEffectOpaque]
    42.         void OnRenderImage (RenderTexture source, RenderTexture destination)
    43.        {
    44.             if (CheckResources()==false || (!distanceFog && !heightFog))
    45.             {
    46.                 Graphics.Blit (source, destination);
    47.                 return;
    48.             }
    49.  
    50.            Camera cam = GetComponent<Camera>();
    51.            Transform camtr = cam.transform;
    52.            float camNear = cam.nearClipPlane;
    53.            float camFar = cam.farClipPlane;
    54.            float camFov = cam.fieldOfView;
    55.            float camAspect = cam.aspect;
    56.  
    57.             Matrix4x4 frustumCorners = Matrix4x4.identity;
    58.  
    59.            float fovWHalf = camFov * 0.5f;
    60.  
    61.            Vector3 toRight = camtr.right * camNear * Mathf.Tan (fovWHalf * Mathf.Deg2Rad) * camAspect;
    62.            Vector3 toTop = camtr.up * camNear * Mathf.Tan (fovWHalf * Mathf.Deg2Rad);
    63.  
    64.            Vector3 topLeft = (camtr.forward * camNear - toRight + toTop);
    65.            float camScale = topLeft.magnitude * camFar/camNear;
    66.  
    67.             topLeft.Normalize();
    68.            topLeft *= camScale;
    69.  
    70.            Vector3 topRight = (camtr.forward * camNear + toRight + toTop);
    71.             topRight.Normalize();
    72.            topRight *= camScale;
    73.  
    74.            Vector3 bottomRight = (camtr.forward * camNear + toRight - toTop);
    75.             bottomRight.Normalize();
    76.            bottomRight *= camScale;
    77.  
    78.            Vector3 bottomLeft = (camtr.forward * camNear - toRight - toTop);
    79.             bottomLeft.Normalize();
    80.            bottomLeft *= camScale;
    81.  
    82.             frustumCorners.SetRow (0, topLeft);
    83.             frustumCorners.SetRow (1, topRight);
    84.             frustumCorners.SetRow (2, bottomRight);
    85.             frustumCorners.SetRow (3, bottomLeft);
    86.  
    87.            var camPos= camtr.position;
    88.             float FdotC = camPos.y-height;
    89.             float paramK = (FdotC <= 0.0f ? 1.0f : 0.0f);
    90.             fogMaterial.SetMatrix ("_FrustumCornersWS", frustumCorners);
    91.             fogMaterial.SetVector ("_CameraWS", camPos);
    92.             fogMaterial.SetVector ("_HeightParams", new Vector4 (height, FdotC, paramK, heightDensity*0.5f));
    93.             fogMaterial.SetVector ("_DistanceParams", new Vector4 (-Mathf.Max(startDistance,0.0f), 0, 0, 0));
    94.  
    95.             var sceneMode= RenderSettings.fogMode;
    96.             var sceneDensity= RenderSettings.fogDensity;
    97.             var sceneStart= RenderSettings.fogStartDistance;
    98.             var sceneEnd= RenderSettings.fogEndDistance;
    99.             Vector4 sceneParams;
    100.             bool  linear = (sceneMode == FogMode.Linear);
    101.             float diff = linear ? sceneEnd - sceneStart : 0.0f;
    102.             float invDiff = Mathf.Abs(diff) > 0.0001f ? 1.0f / diff : 0.0f;
    103.             sceneParams.x = sceneDensity * 1.2011224087f; // density / sqrt(ln(2)), used by Exp2 fog mode
    104.             sceneParams.y = sceneDensity * 1.4426950408f; // density / ln(2), used by Exp fog mode
    105.             sceneParams.z = linear ? -invDiff : 0.0f;
    106.             sceneParams.w = linear ? sceneEnd * invDiff : 0.0f;
    107.             fogMaterial.SetVector ("_SceneFogParams", sceneParams);
    108.            fogMaterial.SetVector ("_SceneFogMode", new Vector4((int)sceneMode, useRadialDistance ? 1 : 0, 0, 0));
    109.  
    110.             int pass = 0;
    111.             if (distanceFog && heightFog)
    112.                 pass = 0; // distance + height
    113.             else if (distanceFog)
    114.                 pass = 1; // distance only
    115.             else
    116.                 pass = 2; // height only
    117.             CustomGraphicsBlit (source, destination, fogMaterial, pass);
    118.         }
    119.  
    120.         static void CustomGraphicsBlit (RenderTexture source, RenderTexture dest, Material fxMaterial, int passNr)
    121.        {
    122.             RenderTexture.active = dest;
    123.  
    124.             fxMaterial.SetTexture ("_MainTex", source);
    125.  
    126.             GL.PushMatrix ();
    127.             GL.LoadOrtho ();
    128.  
    129.             fxMaterial.SetPass (passNr);
    130.  
    131.             GL.Begin (GL.QUADS);
    132.  
    133.             GL.MultiTexCoord2 (0, 0.0f, 0.0f);
    134.             GL.Vertex3 (0.0f, 0.0f, 3.0f); // BL
    135.  
    136.             GL.MultiTexCoord2 (0, 1.0f, 0.0f);
    137.             GL.Vertex3 (1.0f, 0.0f, 2.0f); // BR
    138.  
    139.             GL.MultiTexCoord2 (0, 1.0f, 1.0f);
    140.             GL.Vertex3 (1.0f, 1.0f, 1.0f); // TR
    141.  
    142.             GL.MultiTexCoord2 (0, 0.0f, 1.0f);
    143.             GL.Vertex3 (0.0f, 1.0f, 0.0f); // TL
    144.  
    145.             GL.End ();
    146.             GL.PopMatrix ();
    147.         }
    148.     }
    149. }
    150.  
    151.