-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrayscaleLayers.cs
78 lines (62 loc) · 2.42 KB
/
GrayscaleLayers.cs
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
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using UnityEngine;
// Grayscale effect, with added ExcludeLayer option
// Usage: Attach this script to camera, Select excludeLayers, Set your excluded objects to that layer
#if !UNITY_5_3_OR_NEWER
namespace UnityStandardAssets.ImageEffects
{
[ExecuteInEditMode]
[AddComponentMenu("Image Effects/Color Adjustments/Grayscale")]
public class GrayscaleLayers : ImageEffectBase
{
public Texture textureRamp;
public LayerMask excludeLayers = 0;
private GameObject tmpCam = null;
private Camera _camera;
[Range(-1.0f, 1.0f)]
public float rampOffset;
// Called by camera to apply image effect
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
material.SetTexture("_RampTex", textureRamp);
material.SetFloat("_RampOffset", rampOffset);
Graphics.Blit(source, destination, material);
// exclude layers
Camera cam = null;
if (excludeLayers.value != 0) cam = GetTmpCam();
if (cam && excludeLayers.value != 0)
{
cam.targetTexture = destination;
cam.cullingMask = excludeLayers;
cam.Render();
}
}
// taken from CameraMotionBlur.cs
Camera GetTmpCam()
{
if (tmpCam == null)
{
if (_camera == null) _camera = GetComponent<Camera>();
string name = "_" + _camera.name + "_GrayScaleTmpCam";
GameObject go = GameObject.Find(name);
if (null == go) // couldn't find, recreate
{
tmpCam = new GameObject(name, typeof(Camera));
} else
{
tmpCam = go;
}
}
tmpCam.hideFlags = HideFlags.DontSave;
tmpCam.transform.position = _camera.transform.position;
tmpCam.transform.rotation = _camera.transform.rotation;
tmpCam.transform.localScale = _camera.transform.localScale;
tmpCam.GetComponent<Camera>().CopyFrom(_camera);
tmpCam.GetComponent<Camera>().enabled = false;
tmpCam.GetComponent<Camera>().depthTextureMode = DepthTextureMode.None;
tmpCam.GetComponent<Camera>().clearFlags = CameraClearFlags.Nothing;
return tmpCam.GetComponent<Camera>();
}
}
}
#endif