Skip to content

Instantly share code, notes, and snippets.

@AngryAnt
Created August 1, 2011 14:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AngryAnt/1118288 to your computer and use it in GitHub Desktop.
Save AngryAnt/1118288 to your computer and use it in GitHub Desktop.
A simple example of a multiply blend in Unity. http://en.wikipedia.org/wiki/Blend_modes#Multiply
using UnityEngine;
using System.Collections;
public class Multiply : MonoBehaviour
{
public Camera source, destination;
private RenderTexture renderTexture;
private Texture2D sourceRender, destinationRender;
void Start ()
{
renderTexture = new RenderTexture (Screen.width, Screen.height, 24);
sourceRender = new Texture2D (Screen.width, Screen.height);
destinationRender = new Texture2D (Screen.width, Screen.height);
}
void Update ()
{
RenderTexture active = RenderTexture.active;
RenderTexture.active = renderTexture;
RenderTexture target = source.targetTexture;
source.targetTexture = renderTexture;
source.Render ();
sourceRender.ReadPixels (new Rect (0.0f, 0.0f, renderTexture.width, renderTexture.height), 0, 0);
source.targetTexture = target;
Color background = destination.backgroundColor;
destination.backgroundColor = Color.red;
target = destination.targetTexture;
destination.targetTexture = renderTexture;
destination.Render ();
destinationRender.ReadPixels (new Rect (0.0f, 0.0f, renderTexture.width, renderTexture.height), 0, 0);
destination.targetTexture = target;
destination.backgroundColor = background;
RenderTexture.active = active;
Color[] sourcePixels = sourceRender.GetPixels (), destinationPixels = destinationRender.GetPixels ();
for (int i = 0; i < sourcePixels.Length; i++)
{
destinationPixels[i] = new Color (
(sourcePixels[i].r * destinationPixels[i].r) / 1.0f,
(sourcePixels[i].g * destinationPixels[i].g) / 1.0f,
(sourcePixels[i].b * destinationPixels[i].b) / 1.0f,
(sourcePixels[i].a * destinationPixels[i].a) / 1.0f
);
}
destinationRender.SetPixels (destinationPixels);
destinationRender.Apply ();
}
void OnGUI ()
{
GUI.DrawTexture (new Rect (0.0f, 0.0f, 300.0f, 300.0f), destinationRender);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment