Shader "Custom/ChromaticAberration" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform float _AberrationOffset;
float4 frag(v2f_img i) : COLOR
{
float2 coords = i.uv.xy;
_AberrationOffset /= 300.0f;
//Red Channel
float4 red = tex2D(_MainTex , float2(coords.x - _AberrationOffset, coords.y));
//Green Channel
float4 green = tex2D(_MainTex, coords.xy );
//Blue Channel
float4 blue = tex2D(_MainTex, float2(coords.x + _AberrationOffset, coords.y));
float4 finalColor = float4(red.r, green.g, blue.b, 1.0f);
return finalColor;
}
ENDCG
}
}
}
这是我的RGB拆分着色器 我正在android中对其进行测试。问题是当方向改变时,屏幕变黑。为什么?
我的脚本如下:
using UnityEngine;
namespace _MyScripts
{
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class ChromaticAberration : MonoBehaviour
{
public Shader shader;
private float ChromaticAberrationOffset = 1.0f;
public float ChromaticAberrationMaxOffset = 1.0f;
private Material _material;
private float _flickerTime = 0.5f;
private float flicker;
private void Start()
{
_material = new Material(shader);
}
private void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
{
flicker += Time.deltaTime;
if (flicker > _flickerTime)
{
flicker = 0;
_flickerTime = Random.value;
}
ChromaticAberrationOffset = flicker * ChromaticAberrationMaxOffset; //(Mathf.PingPong(Time.time, 1) * ChromaticAberrationMaxOffset) - ChromaticAberrationMaxOffset / 2;
_material.SetFloat("_AberrationOffset", ChromaticAberrationOffset);
Graphics.Blit(sourceTexture, destTexture, _material);
}
}
}