Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted image effects to use the new post processing stack #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 32 additions & 76 deletions Assets/Kino/Glitch/AnalogGlitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,95 +21,51 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

namespace Kino
{
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
[AddComponentMenu("Kino Image Effects/Analog Glitch")]
public class AnalogGlitch : MonoBehaviour
{
#region Public Properties

// Scan line jitter

[SerializeField, Range(0, 1)]
float _scanLineJitter = 0;

public float scanLineJitter {
get { return _scanLineJitter; }
set { _scanLineJitter = value; }
}

// Vertical jump

[SerializeField, Range(0, 1)]
float _verticalJump = 0;

public float verticalJump {
get { return _verticalJump; }
set { _verticalJump = value; }
}

// Horizontal shake

[SerializeField, Range(0, 1)]
float _horizontalShake = 0;

public float horizontalShake {
get { return _horizontalShake; }
set { _horizontalShake = value; }
}
[System.Serializable]
[UnityEngine.Rendering.PostProcessing.PostProcess(typeof(AnalogGlitchRenderer), PostProcessEvent.AfterStack, "Kino Image Effects/Analog Glitch")]
public class AnalogGlitch : PostProcessEffectSettings
{
[Range(0, 1)]
public FloatParameter scanLineJitter = new FloatParameter();

// Color drift
[Range(0, 1)]
public FloatParameter verticalJump = new FloatParameter();

[SerializeField, Range(0, 1)]
float _colorDrift = 0;
[Range(0, 1)]
public FloatParameter horizontalShake = new FloatParameter();

public float colorDrift {
get { return _colorDrift; }
set { _colorDrift = value; }
}
[Range(0, 1)]
public FloatParameter colorDrift = new FloatParameter();
}

#endregion
public class AnalogGlitchRenderer : PostProcessEffectRenderer<AnalogGlitch>
{
private float _verticalJumpTime;

#region Private Properties

[SerializeField] Shader _shader;

Material _material;

float _verticalJumpTime;

#endregion

#region MonoBehaviour Functions

void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (_material == null)
{
_material = new Material(_shader);
_material.hideFlags = HideFlags.DontSave;
}

_verticalJumpTime += Time.deltaTime * _verticalJump * 11.3f;
public override void Render(PostProcessRenderContext context)
{
var sheet = context.propertySheets.Get(Shader.Find("Hidden/Kino/Glitch/Analog"));

var sl_thresh = Mathf.Clamp01(1.0f - _scanLineJitter * 1.2f);
var sl_disp = 0.002f + Mathf.Pow(_scanLineJitter, 3) * 0.05f;
_material.SetVector("_ScanLineJitter", new Vector2(sl_disp, sl_thresh));
_verticalJumpTime += Time.deltaTime * settings.verticalJump * 11.3f;

var vj = new Vector2(_verticalJump, _verticalJumpTime);
_material.SetVector("_VerticalJump", vj);
var sl_thresh = Mathf.Clamp01(1.0f - settings.scanLineJitter * 1.2f);
var sl_disp = 0.002f + Mathf.Pow(settings.scanLineJitter, 3) * 0.05f;
sheet.properties.SetVector("_ScanLineJitter", new Vector2(sl_disp, sl_thresh));

_material.SetFloat("_HorizontalShake", _horizontalShake * 0.2f);
var vj = new Vector2(settings.verticalJump, _verticalJumpTime);
sheet.properties.SetVector("_VerticalJump", vj);

var cd = new Vector2(_colorDrift * 0.04f, Time.time * 606.11f);
_material.SetVector("_ColorDrift", cd);
sheet.properties.SetFloat("_HorizontalShake", settings.horizontalShake * 0.2f);

Graphics.Blit(source, destination, _material);
}
var cd = new Vector2(settings.colorDrift * 0.04f, Time.time * 606.11f);
sheet.properties.SetVector("_ColorDrift", cd);

#endregion
context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
}

}
}
146 changes: 66 additions & 80 deletions Assets/Kino/Glitch/DigitalGlitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,111 +20,97 @@
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

namespace Kino
{
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
[AddComponentMenu("Kino Image Effects/Digital Glitch")]
public class DigitalGlitch : MonoBehaviour
{
#region Public Properties

[SerializeField, Range(0, 1)]
float _intensity = 0;

public float intensity {
get { return _intensity; }
set { _intensity = value; }
}

#endregion

#region Private Properties
[System.Serializable]
[UnityEngine.Rendering.PostProcessing.PostProcess(typeof(DigitalGlitchRenderer), PostProcessEvent.AfterStack, "Kino Image Effects/DigitalGlitch")]
public class DigitalGlitch : PostProcessEffectSettings
{
[SerializeField, Range(0, 1f)]
public FloatParameter intensity = new FloatParameter() { value = 0 };
}

[SerializeField] Shader _shader;

Material _material;
Texture2D _noiseTexture;
RenderTexture _trashFrame1;
RenderTexture _trashFrame2;
public class DigitalGlitchRenderer : PostProcessEffectRenderer<DigitalGlitch>
{
private Texture2D _noiseTexture;
private RenderTexture _trashFrame1;
private RenderTexture _trashFrame2;

#endregion

#region Private Functions
#region Private Functions

static Color RandomColor()
{
return new Color(Random.value, Random.value, Random.value, Random.value);
}
public DigitalGlitchRenderer() : base()
{
if (_noiseTexture != null) return;

void SetUpResources()
{
if (_material != null) return;
_noiseTexture = new Texture2D(64, 32, TextureFormat.ARGB32, false);
_noiseTexture.hideFlags = HideFlags.DontSave;
_noiseTexture.wrapMode = TextureWrapMode.Clamp;
_noiseTexture.filterMode = FilterMode.Point;

_material = new Material(_shader);
_material.hideFlags = HideFlags.DontSave;
_trashFrame1 = new RenderTexture(Screen.width, Screen.height, 0);
_trashFrame2 = new RenderTexture(Screen.width, Screen.height, 0);
_trashFrame1.hideFlags = HideFlags.DontSave;
_trashFrame2.hideFlags = HideFlags.DontSave;

_noiseTexture = new Texture2D(64, 32, TextureFormat.ARGB32, false);
_noiseTexture.hideFlags = HideFlags.DontSave;
_noiseTexture.wrapMode = TextureWrapMode.Clamp;
_noiseTexture.filterMode = FilterMode.Point;
UpdateNoiseTexture();
}

_trashFrame1 = new RenderTexture(Screen.width, Screen.height, 0);
_trashFrame2 = new RenderTexture(Screen.width, Screen.height, 0);
_trashFrame1.hideFlags = HideFlags.DontSave;
_trashFrame2.hideFlags = HideFlags.DontSave;
static Color RandomColor()
{
return new Color(Random.value, Random.value, Random.value, Random.value);
}

UpdateNoiseTexture();
}
void UpdateNoiseTexture()
{
var color = RandomColor();

void UpdateNoiseTexture()
for (var y = 0; y < _noiseTexture.height; y++)
{
for (var x = 0; x < _noiseTexture.width; x++)
{
var color = RandomColor();

for (var y = 0; y < _noiseTexture.height; y++)
{
for (var x = 0; x < _noiseTexture.width; x++)
{
if (Random.value > 0.89f) color = RandomColor();
_noiseTexture.SetPixel(x, y, color);
}
}

_noiseTexture.Apply();
if (Random.value > 0.89f)
{
color = RandomColor();
}
_noiseTexture.SetPixel(x, y, color);
}
}

#endregion
_noiseTexture.Apply();
}

#region MonoBehaviour Functions
#endregion

void Update()
{
if (Random.value > Mathf.Lerp(0.9f, 0.5f, _intensity))
{
SetUpResources();
UpdateNoiseTexture();
}
}
public override void Render(PostProcessRenderContext context)
{
var sheet = context.propertySheets.Get(Shader.Find("Hidden/Kino/Glitch/Digital"));
if (Random.value > Mathf.Lerp(0.9f, 0.5f, settings.intensity.value))
{
UpdateNoiseTexture();
}

void OnRenderImage(RenderTexture source, RenderTexture destination)
{
SetUpResources();
// Update trash frames on a constant interval.
var fcount = Time.frameCount;
if (fcount % 13 == 0) context.command.Blit(context.source, _trashFrame1);

// Update trash frames on a constant interval.
var fcount = Time.frameCount;
if (fcount % 13 == 0) Graphics.Blit(source, _trashFrame1);
if (fcount % 73 == 0) Graphics.Blit(source, _trashFrame2);
if (fcount % 73 == 0) context.command.Blit(context.source, _trashFrame2);

_material.SetFloat("_Intensity", _intensity);
_material.SetTexture("_NoiseTex", _noiseTexture);
var trashFrame = Random.value > 0.5f ? _trashFrame1 : _trashFrame2;
_material.SetTexture("_TrashTex", trashFrame);

Graphics.Blit(source, destination, _material);
}
sheet.properties.SetFloat("_Intensity", settings.intensity);
sheet.properties.SetTexture("_NoiseTex", _noiseTexture);
var trashFrame = Random.value > 0.5f ? _trashFrame1 : _trashFrame2;
sheet.properties.SetTexture("_TrashTex", trashFrame);

#endregion
context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
}

}
}
57 changes: 0 additions & 57 deletions Assets/Kino/Glitch/Editor/AnalogGlitchEditor.cs

This file was deleted.

12 changes: 0 additions & 12 deletions Assets/Kino/Glitch/Editor/AnalogGlitchEditor.cs.meta

This file was deleted.

Loading