Skip to content

Instantly share code, notes, and snippets.

@seiroise
Last active May 4, 2022 06:56
Show Gist options
  • Select an option

  • Save seiroise/b115ace3de7c10082d112b29e803a34d to your computer and use it in GitHub Desktop.

Select an option

Save seiroise/b115ace3de7c10082d112b29e803a34d to your computer and use it in GitHub Desktop.
RGB Separation Shader (Unity)
/*
RGB split shader
Built-inシェーダのvert_imgなどはここを参照のこと。
http://wiki.unity3d.com/index.php/Shader_Code
uvなどは頂点シェーダで計算したほうが軽くなりそう。
*/
Shader "Custom/RGBSplit" {
Properties {
_MainTex("MainTex", 2D) = ""{}
_Strength("Strength", Range(0, 1)) = 0
_Area("Area", Vector) = (0, 0, 1, 1)
_Red("Red", Vector) = (0, 0, 0, 0)
_Green("Green", Vector) = (0, 0, 0, 0)
_Blue("Blue", Vector) = (0, 0, 0, 0)
}
SubShader {
Pass {
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert_img
#pragma fragment frag
sampler2D _MainTex;
float _Strength;
half4 _Area;
half4 _Red, _Green, _Blue;
fixed4 frag(v2f_img i) : COLOR {
half2 uv = i.uv;
fixed4 c;
int inX = ((uv.x - _Area.x) * (_Area.z - uv.x)) >= 0;
int inY = ((uv.y - _Area.y) * (_Area.w - uv.y)) >= 0;
int isIn = inX * inY;
half2 r = uv - _Red.xy * isIn;
c.r = tex2D(_MainTex, r).r;
half2 g = uv - _Green.xy * isIn;
c.g = tex2D(_MainTex, g).g;
half2 b = uv - _Blue.xy * isIn;
c.b = tex2D(_MainTex, b).b;
return c;
}
ENDCG
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 特定の領域にだけRGBOffsetを掛けるための制御スクリプト
/// </summary>
public class RGBSplitEffect : MonoBehaviour {
/// <summary>
/// スプリットの定義
/// </summary>
[System.Serializable]
public class SplitDefine
{
public Vector4 area;
public Vector4 red;
public Vector4 green;
public Vector4 blue;
}
public Material rgbOffset;
public SplitDefine splitDefine;
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
BlitRGBOffset(splitDefine, source, destination);
}
private void BlitRGBOffset(SplitDefine define, RenderTexture src, RenderTexture dst)
{
rgbOffset.SetVector("_Area", define.area);
rgbOffset.SetVector("_Red", define.red);
rgbOffset.SetVector("_Green", define.green);
rgbOffset.SetVector("_Blue", define.blue);
Graphics.Blit(src, dst, rgbOffset);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment