CameraFog

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "== Description == This is a simple class that, when added to a GameObject with a camera, allows you to control the fog settings for that camera separately from the global ones...")
 
m (Updated code and included link to screenshot as well as unity 5 package demo)
 
Line 2: Line 2:
 
This is a simple class that, when added to a GameObject with a camera, allows you to control the fog settings for that camera separately from the global ones.
 
This is a simple class that, when added to a GameObject with a camera, allows you to control the fog settings for that camera separately from the global ones.
  
<br/>
+
Unity 5 package demo is available here [http://www.createdbyx.com/createdbyx/file.axd?file=2015%2f4%2fCameraFog.unitypackage CameraFog.unitypackage (46.66 kb)] and here [[File:CameraFog.zip]]
 
+
== Code ==
+
 
+
{{:User:Tenebrous}}
+
  
 +
[[File:CameraFogScreenShot.png]]
 
=== CSharp - CameraFog.cs ===
 
=== CSharp - CameraFog.cs ===
  
Line 15: Line 12:
 
// I'd love to hear from you if you do anything cool with this or have any suggestions :)
 
// I'd love to hear from you if you do anything cool with this or have any suggestions :)
 
// www.tenebrous.co.uk
 
// www.tenebrous.co.uk
 +
// =============
 +
// Code updated and tested in unity 5 by: Dean aka (Created by: X)
 +
// Unity Wiki user: http://wiki.unity3d.com/index.php/User:Createdbyx
 +
// Homepage: http://www.createdbyx.com/
  
using UnityEngine;
+
    using UnityEngine;
using System.Collections;
+
  
[RequireComponent(typeof(Camera))]
+
    /// <summary>
[ExecuteInEditMode]
+
    /// Modifies a camera to allows you to control the fog settings for that camera separately from the global scene fog or other cameras.
public class CameraFog : MonoBehaviour
+
    /// </summary>
{
+
    [RequireComponent(typeof(Camera))]
    public bool Enabled;
+
    [ExecuteInEditMode]
    public float StartDistance;
+
    public class CameraFog : MonoBehaviour
    public float EndDistance;
+
    public FogMode Mode;
+
    public float Density;
+
    public Color Color;
+
 
+
    float _startDistance;
+
    float _endDistance;
+
    FogMode _mode;
+
    float _density;
+
    Color _color;
+
    bool _enabled;
+
   
+
    void OnPreRender()
+
 
     {
 
     {
         _startDistance = RenderSettings.fogStartDistance;
+
         /// <summary>
         _endDistance = RenderSettings.fogEndDistance;
+
         /// The enabled state weather or not fog will be visible.
         _mode = RenderSettings.fogMode;
+
         /// </summary>
         _density = RenderSettings.fogDensity;
+
         public bool Enabled;
        _color = RenderSettings.fogColor;
+
        _enabled = RenderSettings.fog;
+
  
         RenderSettings.fog = Enabled;
+
         /// <summary>
         RenderSettings.fogStartDistance = StartDistance;
+
         /// The start distance from the camera where the fog will be drawn.
         RenderSettings.fogEndDistance = EndDistance;
+
         /// </summary>
         RenderSettings.fogMode = Mode;
+
         public float StartDistance;
        RenderSettings.fogDensity = Density;
+
        RenderSettings.fogColor = Color;
+
    }
+
  
    void OnPostRender()
+
        /// <summary>
    {
+
        /// The end distance from the camera where the fog will be drawn.
         RenderSettings.fog = _enabled;
+
        /// </summary>
        RenderSettings.fogStartDistance = _startDistance;
+
        public float EndDistance;
        RenderSettings.fogEndDistance = _endDistance;
+
 
        RenderSettings.fogMode = _mode;
+
        /// <summary>
        RenderSettings.fogDensity = _density;
+
        /// The fog mode that controls how the fog is rendered.
        RenderSettings.fogColor = _color;
+
        /// </summary>
 +
        public FogMode Mode;
 +
 
 +
        /// <summary>
 +
        /// The density of the fog that is rendered.
 +
        /// </summary>
 +
        public float Density;
 +
 
 +
        /// <summary>
 +
        /// The fog color.
 +
        /// </summary>
 +
        public Color Color;
 +
 
 +
        /// <summary>
 +
        /// Stores the pre-render state of the start distance.
 +
        /// </summary>
 +
        private float _startDistance;
 +
 
 +
        /// <summary>
 +
        /// Stores the pre-render state of the end  distance.
 +
        /// </summary>
 +
        private float _endDistance;
 +
 
 +
        /// <summary>
 +
        /// Stores the pre-render state of the fog mode.
 +
        /// </summary>
 +
        private FogMode _mode;
 +
 
 +
        /// <summary>
 +
        /// Stores the pre-render state of the density.
 +
        /// </summary>
 +
        private float _density;
 +
 
 +
        /// <summary>
 +
        /// Stores the pre-render state of the fog color.
 +
        /// </summary>
 +
        private Color _color;
 +
       
 +
        /// <summary>
 +
        /// Stores the pre-render state wheather or not the fog is enabled.
 +
        /// </summary>
 +
        private bool _enabled;
 +
 
 +
        /// <summary>
 +
        /// Event that is fired before any camera starts rendering.
 +
        /// </summary>
 +
        private void OnPreRender()
 +
        {
 +
            this._startDistance = RenderSettings.fogStartDistance;
 +
            this._endDistance = RenderSettings.fogEndDistance;
 +
            this._mode = RenderSettings.fogMode;
 +
            this._density = RenderSettings.fogDensity;
 +
            this._color = RenderSettings.fogColor;
 +
            this._enabled = RenderSettings.fog;
 +
 
 +
            RenderSettings.fog = this.Enabled;
 +
            RenderSettings.fogStartDistance = this.StartDistance;
 +
            RenderSettings.fogEndDistance = this.EndDistance;
 +
            RenderSettings.fogMode = this.Mode;
 +
            RenderSettings.fogDensity = this.Density;
 +
            RenderSettings.fogColor = this.Color;
 +
         }
 +
 
 +
        /// <summary>
 +
        /// Event that is fired after any camera finishes rendering.
 +
        /// </summary>
 +
        private void OnPostRender()
 +
        {
 +
            RenderSettings.fog = this._enabled;
 +
            RenderSettings.fogStartDistance = this._startDistance;
 +
            RenderSettings.fogEndDistance = this._endDistance;
 +
            RenderSettings.fogMode = this._mode;
 +
            RenderSettings.fogDensity = this._density;
 +
            RenderSettings.fogColor = this._color;
 +
        }
 
     }
 
     }
}
+
 
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 09:57, 3 April 2015

[edit] Description

This is a simple class that, when added to a GameObject with a camera, allows you to control the fog settings for that camera separately from the global ones.

Unity 5 package demo is available here CameraFog.unitypackage (46.66 kb) and here File:CameraFog.zip

CameraFogScreenShot.png

[edit] CSharp - CameraFog.cs

// Per-camera fog
//
// I'd love to hear from you if you do anything cool with this or have any suggestions :)
// www.tenebrous.co.uk
// =============
// Code updated and tested in unity 5 by: Dean aka (Created by: X) 
// Unity Wiki user: http://wiki.unity3d.com/index.php/User:Createdbyx
// Homepage: http://www.createdbyx.com/
 
    using UnityEngine;
 
    /// <summary>
    /// Modifies a camera to allows you to control the fog settings for that camera separately from the global scene fog or other cameras. 
    /// </summary>
    [RequireComponent(typeof(Camera))]
    [ExecuteInEditMode]
    public class CameraFog : MonoBehaviour
    {
        /// <summary>
        /// The enabled state weather or not fog will be visible.
        /// </summary>
        public bool Enabled;
 
        /// <summary>
        /// The start distance from the camera where the fog will be drawn.
        /// </summary>
        public float StartDistance;
 
        /// <summary>
        /// The end distance from the camera where the fog will be drawn.
        /// </summary>
        public float EndDistance;
 
        /// <summary>
        /// The fog mode that controls how the fog is rendered.
        /// </summary>
        public FogMode Mode;
 
        /// <summary>
        /// The density of the fog that is rendered.
        /// </summary>
        public float Density;
 
        /// <summary>
        /// The fog color.
        /// </summary>
        public Color Color;
 
        /// <summary>
        /// Stores the pre-render state of the start distance.
        /// </summary>
        private float _startDistance;
 
        /// <summary>
        /// Stores the pre-render state of the end  distance.
        /// </summary>
        private float _endDistance;
 
        /// <summary>
        /// Stores the pre-render state of the fog mode.
        /// </summary>
        private FogMode _mode;
 
        /// <summary>
        /// Stores the pre-render state of the density.
        /// </summary>
        private float _density;
 
        /// <summary>
        /// Stores the pre-render state of the fog color.
        /// </summary>
        private Color _color;
 
        /// <summary>
        /// Stores the pre-render state wheather or not the fog is enabled.
        /// </summary>
        private bool _enabled;
 
        /// <summary>
        /// Event that is fired before any camera starts rendering.
        /// </summary>
        private void OnPreRender()
        {
            this._startDistance = RenderSettings.fogStartDistance;
            this._endDistance = RenderSettings.fogEndDistance;
            this._mode = RenderSettings.fogMode;
            this._density = RenderSettings.fogDensity;
            this._color = RenderSettings.fogColor;
            this._enabled = RenderSettings.fog;
 
            RenderSettings.fog = this.Enabled;
            RenderSettings.fogStartDistance = this.StartDistance;
            RenderSettings.fogEndDistance = this.EndDistance;
            RenderSettings.fogMode = this.Mode;
            RenderSettings.fogDensity = this.Density;
            RenderSettings.fogColor = this.Color;
        }
 
        /// <summary>
        /// Event that is fired after any camera finishes rendering.
        /// </summary>
        private void OnPostRender()
        {
            RenderSettings.fog = this._enabled;
            RenderSettings.fogStartDistance = this._startDistance;
            RenderSettings.fogEndDistance = this._endDistance;
            RenderSettings.fogMode = this._mode;
            RenderSettings.fogDensity = this._density;
            RenderSettings.fogColor = this._color;
        }
    }
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox