BoostColors

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(C# - BloomEffect.cs)
m (Text replace - "</csharp>" to "</syntaxhighlight>")
 
(11 intermediate revisions by 5 users not shown)
Line 6: Line 6:
 
==Description==
 
==Description==
 
This simple image effect subtracts a certain amount of color from your rendering, then doubles the strength.
 
This simple image effect subtracts a certain amount of color from your rendering, then doubles the strength.
 +
[[Image:Boostcolors.jpg|thumb|Example of the BoostColors image effect in action.]]
  
 
==Usage==
 
==Usage==
Line 12: Line 13:
 
Simply attach this script to a Camera and watch your colors go off the radar.
 
Simply attach this script to a Camera and watch your colors go off the radar.
  
==C# - BloomEffect.cs==
+
==C# - BoostColors.cs==
<csharp>using UnityEngine;
+
<syntaxhighlight lang="csharp">using UnityEngine;
 
using System.Collections;
 
using System.Collections;
  
 
[AddComponentMenu("Image Effects/Boost Colors")]
 
[AddComponentMenu("Image Effects/Boost Colors")]
public class BoostColors : MonoBehaviour {
+
public class BoostColors : MonoBehaviour {
static Material s_BoostMaterial = null;
+
    static Material s_BoostMaterial = null;
public float blackLevel = .15f;
+
    public float blackLevel = .15f;
// Update is called once per frame
+
void OnRenderImage (RenderTexture source, RenderTexture dest) {
+
if (!s_BoostMaterial) {
+
s_BoostMaterial = new Material (
+
"Shader \"\" {" +
+
" Properties {" +
+
" _Color (\"Color\", Color) = (.3,.3,.3,.3)" +
+
" _RTex (\"RenderTex\", RECT) = \"\" {}" +
+
" }" +
+
" SubShader {" +
+
" Cull Off ZWrite Off ZTest Always" +
+
" Pass {" +
+
" SetTexture [_RTex] { constantColor [_Color] combine texture - constant DOUBLE }" +
+
" }" +
+
" }" +
+
"}"
+
);
+
}
+
RenderTexture.active = dest;
+
s_BoostMaterial.SetTexture ("_RTex", source);
+
s_BoostMaterial.SetColor ("_Color", new Color (blackLevel,blackLevel,blackLevel,blackLevel));
+
s_BoostMaterial.SetPass (0);
+
// Part one: Render a white alpha quad behind
+
GL.PushMatrix ();
+
GL.LoadOrtho ();
+
  
GL.Begin (GL.QUADS);
+
    void OnRenderImage (RenderTexture source, RenderTexture dest) {
GL.TexCoord2 (0,0); GL.Vertex3 (0,0,1);
+
        if (!s_BoostMaterial) {
GL.TexCoord2 (1,0); GL.Vertex3 (1,0,1);
+
            s_BoostMaterial = new Material (
GL.TexCoord2 (1,1); GL.Vertex3 (1,1,1);
+
                "Shader \"\" {" +
GL.TexCoord2 (0,1); GL.Vertex3 (0,1,1);
+
                "  Properties {" +
GL.End ();
+
                "      _Color (\"Color\", Color) = (.3,.3,.3,.3)" +
GL.PopMatrix ();
+
                "      _RTex (\"RenderTex\", RECT) = \"\" {}" +
}
+
                "  }" +
 +
                "  SubShader {" +
 +
                "      Cull Off ZWrite Off ZTest Always" +
 +
                "      Pass {" +
 +
                "          SetTexture [_RTex] { constantColor [_Color] combine texture - constant DOUBLE }" +
 +
                "      }" +
 +
                "  }" +
 +
                "}"
 +
            );
 +
        }
 +
        RenderTexture.active = dest;
 +
        s_BoostMaterial.SetTexture ("_RTex", source);
 +
        s_BoostMaterial.SetColor ("_Color", new Color (blackLevel,blackLevel,blackLevel,blackLevel));
 +
        s_BoostMaterial.SetPass (0);
 +
 
 +
        GL.PushMatrix ();
 +
        GL.LoadOrtho ();
 +
 
 +
        GL.Begin (GL.QUADS);
 +
        GL.TexCoord2 (0,0); GL.Vertex3 (0,0,0.1f);
 +
        GL.TexCoord2 (1,0); GL.Vertex3 (1,0,0.1f);
 +
        GL.TexCoord2 (1,1); GL.Vertex3 (1,1,0.1f);
 +
        GL.TexCoord2 (0,1); GL.Vertex3 (0,1,0.1f);
 +
        GL.End ();
 +
        GL.PopMatrix ();
 +
    }
 
}
 
}
</csharp>
+
</syntaxhighlight>

Latest revision as of 20:44, 10 January 2012

Author: Nicholas Francis

[edit] Description

This simple image effect subtracts a certain amount of color from your rendering, then doubles the strength.

Example of the BoostColors image effect in action.

[edit] Usage

This script (and all image effects) use render textures by need and therefore require Unity Pro. There isn't a way around this!

Simply attach this script to a Camera and watch your colors go off the radar.

[edit] C# - BoostColors.cs

using UnityEngine;
using System.Collections;
 
[AddComponentMenu("Image Effects/Boost Colors")]
public class BoostColors : MonoBehaviour {  
    static Material s_BoostMaterial = null;
    public float blackLevel = .15f;
 
    void OnRenderImage (RenderTexture source, RenderTexture dest) {
        if (!s_BoostMaterial) {
            s_BoostMaterial = new Material (
                "Shader \"\" {" +
                "   Properties {" +
                "       _Color (\"Color\", Color) = (.3,.3,.3,.3)" +
                "       _RTex (\"RenderTex\", RECT) = \"\" {}" +
                "   }" +
                "   SubShader {" +
                "       Cull Off ZWrite Off ZTest Always" +
                "       Pass {" +
                "           SetTexture [_RTex] { constantColor [_Color] combine texture - constant DOUBLE }" +
                "       }" +
                "   }" +
                "}"
            );
        }
        RenderTexture.active = dest;
        s_BoostMaterial.SetTexture ("_RTex", source);
        s_BoostMaterial.SetColor ("_Color", new Color (blackLevel,blackLevel,blackLevel,blackLevel));
        s_BoostMaterial.SetPass (0);
 
        GL.PushMatrix ();
        GL.LoadOrtho ();
 
        GL.Begin (GL.QUADS);
        GL.TexCoord2 (0,0); GL.Vertex3 (0,0,0.1f);
        GL.TexCoord2 (1,0); GL.Vertex3 (1,0,0.1f);
        GL.TexCoord2 (1,1); GL.Vertex3 (1,1,0.1f);
        GL.TexCoord2 (0,1); GL.Vertex3 (0,1,0.1f);
        GL.End ();
        GL.PopMatrix ();
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox