Skip to content

Commit 767c39b

Browse files
committed
Added effects and utilities.
1 parent 85450a0 commit 767c39b

File tree

81 files changed

+4509
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+4509
-0
lines changed

Effects/BlurEffect.cs

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
namespace SecondStar.Effects
5+
{
6+
/// <summary>
7+
/// Port of standard blur effect
8+
/// </summary>
9+
public class BlurEffect : ImageEffectBase {
10+
11+
public enum BlurType {
12+
StandardGauss = 0,
13+
SgxGauss = 1,
14+
}
15+
16+
[Range(0,2)]
17+
public int downsample;
18+
[Range(0f, 10f)]
19+
public float blurSize = 3;
20+
[Range(1,4)]
21+
public int blurIterations = 2;
22+
public BlurType blurType = BlurType.StandardGauss;
23+
24+
public LayerMask maskLayer;
25+
Camera effectsCamera;
26+
27+
Material mixMaterial;
28+
29+
void Reset()
30+
{
31+
shader = Shader.Find("KBD/Fast Blur");
32+
}
33+
34+
private Material MixMaterial
35+
{
36+
get
37+
{
38+
if (mixMaterial == null)
39+
{
40+
mixMaterial = new Material(Shader.Find("KBD/Mix Shader"));
41+
mixMaterial.hideFlags = HideFlags.HideAndDontSave;
42+
}
43+
return mixMaterial;
44+
}
45+
}
46+
47+
protected override void OnDisable()
48+
{
49+
base.OnDisable();
50+
if (mixMaterial != null)
51+
DestroyImmediate(mixMaterial);
52+
if (effectsCamera != null)
53+
DestroyImmediate(effectsCamera);
54+
}
55+
56+
protected override void OnPreRender()
57+
{
58+
if (maskLayer.value != 0 && effectsCamera == null)
59+
{
60+
GameObject effectCamObject = new GameObject();
61+
effectCamObject.hideFlags = HideFlags.HideAndDontSave;
62+
effectsCamera = effectCamObject.AddComponent<Camera>();
63+
effectsCamera.enabled = false;
64+
}
65+
}
66+
67+
protected override void OnRenderImage (RenderTexture source, RenderTexture destination)
68+
{
69+
float widthMod = 1.0f / (1.0f * (1<<downsample));
70+
Vector4 blurParams = new Vector4 (blurSize * widthMod, -blurSize * widthMod, 0.0f, 0.0f);
71+
material.SetVector("_Parameter", blurParams);
72+
source.filterMode = FilterMode.Bilinear;
73+
74+
int renderWidth = source.width >> downsample;
75+
int renderHeight = source.height >> downsample;
76+
77+
// downsample
78+
RenderTexture tempRender = RenderTexture.GetTemporary(renderWidth, renderHeight, 0, source.format);
79+
tempRender.filterMode = FilterMode.Bilinear;
80+
Graphics.Blit(source, tempRender, material, 0);
81+
82+
int passOffs = blurType == BlurType.StandardGauss ? 0 : 2;
83+
84+
for (int i = 0; i < blurIterations; i++)
85+
{
86+
float iterationOffs = i;
87+
blurParams = new Vector4 (blurSize * widthMod + iterationOffs, -blurSize * widthMod - iterationOffs, 0.0f, 0.0f);
88+
material.SetVector("_Parameter", blurParams);
89+
90+
// vertical blur
91+
RenderTexture tempRender2 = RenderTexture.GetTemporary(renderWidth, renderHeight, 0, source.format);
92+
tempRender2.filterMode = FilterMode.Bilinear;
93+
Graphics.Blit(tempRender, tempRender2, material, 1 + passOffs);
94+
RenderTexture.ReleaseTemporary(tempRender);
95+
tempRender = tempRender2;
96+
97+
// horizontal blur
98+
tempRender2 = RenderTexture.GetTemporary(renderWidth, renderHeight, 0, source.format);
99+
tempRender2.filterMode = FilterMode.Bilinear;
100+
Graphics.Blit(tempRender, tempRender2, material, 2 + passOffs);
101+
RenderTexture.ReleaseTemporary(tempRender);
102+
tempRender = tempRender2;
103+
}
104+
105+
if (maskLayer.value == 0)
106+
{
107+
Graphics.Blit(tempRender, destination);
108+
}
109+
else
110+
{
111+
RenderTexture maskBuffer = RenderTexture.GetTemporary(renderWidth, renderHeight, 0, source.format);
112+
113+
effectsCamera.CopyFrom(camera);
114+
effectsCamera.cullingMask = maskLayer;
115+
effectsCamera.backgroundColor = Color.black;
116+
effectsCamera.transparencySortMode = camera.transparencySortMode;
117+
effectsCamera.targetTexture = maskBuffer;
118+
effectsCamera.Render();
119+
120+
MixMaterial.SetTexture("_OverLayer", tempRender);
121+
MixMaterial.SetTexture("_Blend", maskBuffer);
122+
Graphics.Blit(source, destination, MixMaterial);
123+
124+
RenderTexture.ReleaseTemporary(maskBuffer);
125+
}
126+
127+
RenderTexture.ReleaseTemporary(tempRender);
128+
}
129+
130+
}
131+
}

Effects/BlurEffect.cs.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Effects/ImageEffectBase.cs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using UnityEngine;
2+
3+
namespace SecondStar.Effects
4+
{
5+
[RequireComponent (typeof(Camera))]
6+
[ExecuteInEditMode]
7+
public class ImageEffectBase : MonoBehaviour {
8+
/// Provides a shader property that is set in the inspector
9+
/// and a material instantiated from the shader
10+
public Shader shader;
11+
12+
private Material m_Material;
13+
protected Material material {
14+
get {
15+
if (m_Material == null) {
16+
m_Material = new Material (shader);
17+
m_Material.hideFlags = HideFlags.HideAndDontSave;
18+
}
19+
return m_Material;
20+
}
21+
}
22+
23+
public RenderTexture Output
24+
{
25+
get; protected set;
26+
}
27+
28+
protected virtual void Start ()
29+
{
30+
// Disable if we don't support image effects
31+
if (!SystemInfo.supportsImageEffects) {
32+
enabled = false;
33+
return;
34+
}
35+
36+
// Disable the image effect if the shader can't
37+
// run on the users graphics card
38+
if (!shader.isSupported)
39+
enabled = false;
40+
}
41+
42+
protected virtual void OnDisable() {
43+
if( m_Material )
44+
{
45+
DestroyImmediate( m_Material );
46+
}
47+
if (Output != null)
48+
{
49+
ClearBuffer(Output);
50+
}
51+
}
52+
53+
protected virtual void OnPreRender()
54+
{
55+
}
56+
57+
protected virtual void OnRenderImage(RenderTexture source, RenderTexture destination)
58+
{
59+
}
60+
61+
protected void ClearBuffer(RenderTexture buffer)
62+
{
63+
if (buffer != null)
64+
{
65+
RenderTexture.ReleaseTemporary(buffer);
66+
buffer = null;
67+
}
68+
}
69+
}
70+
}

Effects/ImageEffectBase.cs.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)