public
Created

A simple example of a multiply blend in Unity. http://en.wikipedia.org/wiki/Blend_modes#Multiply

  • Download Gist
Multiply.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
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);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.