Unity shader/C# - 如何改变效果的颜色?
Unity shader/C# - how to change colors of effect?
好的,所以我正在使用具有颜色 "drift"
的故障效果(模拟效果)
https://github.com/keijiro/KinoGlitch
它带有一个着色器和这个 Analog Glitch 脚本(全部在 link 中):
[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; }
}
// Color drift
[SerializeField, Range(0, 1)]
float _colorDrift = 0;
public float colorDrift {
get { return _colorDrift; }
set { _colorDrift = value; }
}
#endregion
#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;
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));
var vj = new Vector2(_verticalJump, _verticalJumpTime);
_material.SetVector("_VerticalJump", vj);
_material.SetFloat("_HorizontalShake", _horizontalShake * 0.2f);
var cd = new Vector2(_colorDrift * 0.04f, Time.time * 606.11f);
_material.SetVector("_ColorDrift", cd);
Graphics.Blit(source, destination, _material);
}
#endregion
我需要了解如何更改它 "glitches" 中的颜色 - 正如您现在看到的那样,它会出现绿色和洋红色故障。我需要红色和蓝色,但找不到具体的颜色值。
我可以根据需要 post 着色器 - 我怎样才能在 red/blue 统一中实现这个故障?
着色器:
Shader "Hidden/Kino/Glitch/Analog"
{
Properties
{
_MainTex ("-", 2D) = "" {}
}
CGINCLUDE
#include "UnityCG.cginc"
sampler2D _MainTex;
float2 _MainTex_TexelSize;
float2 _ScanLineJitter; // (displacement, threshold)
float2 _VerticalJump; // (amount, time)
float _HorizontalShake;
float2 _ColorDrift; // (amount, time)
float nrand(float x, float y)
{
return frac(sin(dot(float2(x, y), float2(12.9898, 78.233))) * 43758.5453);
}
half4 frag(v2f_img i) : SV_Target
{
float u = i.uv.x;
float v = i.uv.y;
// Scan line jitter
float jitter = nrand(v, _Time.x) * 2 - 1;
jitter *= step(_ScanLineJitter.y, abs(jitter)) * _ScanLineJitter.x;
// Vertical jump
float jump = lerp(v, frac(v + _VerticalJump.y), _VerticalJump.x);
// Horizontal shake
float shake = (nrand(_Time.x, 2) - 0.5) * _HorizontalShake;
// Color drift
float drift = sin(jump + _ColorDrift.y) * _ColorDrift.x;
half4 src1 = tex2D(_MainTex, frac(float2(u + jitter + shake, jump)));
half4 src2 = tex2D(_MainTex, frac(float2(u + jitter + shake + drift, jump)));
return half4(src1.r, src2.g, src1.b, 1);
}
ENDCG
SubShader
{
Pass
{
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#pragma target 3.0
ENDCG
}
}
_ColorDrift
用于在着色器中生成一个名为drift
的局部变量,此值然后仅在一个位置使用以生成src2
纹理。
这个纹理然后使用一次,return half4(src1.r, src2.g, src1.b, 1);
请注意,src2
纹理仅提供其绿色值。这意味着绿色通道被偏移(产生绿色和反绿色(洋红色)失真)。
您可以调整此 return
线以生成 Red/Anti-Red(青色)失真或 Blue/Anti-Blue(黄色)失真。只需更改 src2
变量适用的颜色值即可。
Red/Cyan:
return half4(src2.r, src1.g, src1.b, 1);
Blue/Yellow:
return half4(src1.r, src1.g, src2.b, 1);
请注意,将 src2
应用于 两个 颜色通道只会触发失真 (Red/Anti-Red -> Anti-Red/Red) 并且使它更强烈(纹理跳跃的明显距离更大)。
好的,所以我正在使用具有颜色 "drift"
的故障效果(模拟效果)https://github.com/keijiro/KinoGlitch
它带有一个着色器和这个 Analog Glitch 脚本(全部在 link 中):
[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; }
}
// Color drift
[SerializeField, Range(0, 1)]
float _colorDrift = 0;
public float colorDrift {
get { return _colorDrift; }
set { _colorDrift = value; }
}
#endregion
#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;
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));
var vj = new Vector2(_verticalJump, _verticalJumpTime);
_material.SetVector("_VerticalJump", vj);
_material.SetFloat("_HorizontalShake", _horizontalShake * 0.2f);
var cd = new Vector2(_colorDrift * 0.04f, Time.time * 606.11f);
_material.SetVector("_ColorDrift", cd);
Graphics.Blit(source, destination, _material);
}
#endregion
我需要了解如何更改它 "glitches" 中的颜色 - 正如您现在看到的那样,它会出现绿色和洋红色故障。我需要红色和蓝色,但找不到具体的颜色值。
我可以根据需要 post 着色器 - 我怎样才能在 red/blue 统一中实现这个故障?
着色器:
Shader "Hidden/Kino/Glitch/Analog"
{
Properties
{
_MainTex ("-", 2D) = "" {}
}
CGINCLUDE
#include "UnityCG.cginc"
sampler2D _MainTex;
float2 _MainTex_TexelSize;
float2 _ScanLineJitter; // (displacement, threshold)
float2 _VerticalJump; // (amount, time)
float _HorizontalShake;
float2 _ColorDrift; // (amount, time)
float nrand(float x, float y)
{
return frac(sin(dot(float2(x, y), float2(12.9898, 78.233))) * 43758.5453);
}
half4 frag(v2f_img i) : SV_Target
{
float u = i.uv.x;
float v = i.uv.y;
// Scan line jitter
float jitter = nrand(v, _Time.x) * 2 - 1;
jitter *= step(_ScanLineJitter.y, abs(jitter)) * _ScanLineJitter.x;
// Vertical jump
float jump = lerp(v, frac(v + _VerticalJump.y), _VerticalJump.x);
// Horizontal shake
float shake = (nrand(_Time.x, 2) - 0.5) * _HorizontalShake;
// Color drift
float drift = sin(jump + _ColorDrift.y) * _ColorDrift.x;
half4 src1 = tex2D(_MainTex, frac(float2(u + jitter + shake, jump)));
half4 src2 = tex2D(_MainTex, frac(float2(u + jitter + shake + drift, jump)));
return half4(src1.r, src2.g, src1.b, 1);
}
ENDCG
SubShader
{
Pass
{
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#pragma target 3.0
ENDCG
}
}
_ColorDrift
用于在着色器中生成一个名为drift
的局部变量,此值然后仅在一个位置使用以生成src2
纹理。
这个纹理然后使用一次,return half4(src1.r, src2.g, src1.b, 1);
请注意,src2
纹理仅提供其绿色值。这意味着绿色通道被偏移(产生绿色和反绿色(洋红色)失真)。
您可以调整此 return
线以生成 Red/Anti-Red(青色)失真或 Blue/Anti-Blue(黄色)失真。只需更改 src2
变量适用的颜色值即可。
Red/Cyan:
return half4(src2.r, src1.g, src1.b, 1);
Blue/Yellow:
return half4(src1.r, src1.g, src2.b, 1);
请注意,将 src2
应用于 两个 颜色通道只会触发失真 (Red/Anti-Red -> Anti-Red/Red) 并且使它更强烈(纹理跳跃的明显距离更大)。