Hi there, Please look at this shader. It’s a camera effect (Chromatic)
it works in editor but when I build the game It doesn’t applied
Why is that?
Shader
//--------------------------------------------------------------//
// Chromatic Abberation
//
// Copyright (c) Johannes Bausch. All rights reserved.
//
// Edited by that one guy who is realllyyy good at shaders, yeahh that guy. Optimized the.. wait why am I explaining I should be writing shader code now. Alright meet you at line 109.
//--------------------------------------------------------------//
Shader "Custom/AdvancedCA"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
#pragma glsl
#pragma target 3.0
#pragma multi_compile REDBLUE REDGREEN
//--------------------------------------------------------------//
// Params
//--------------------------------------------------------------//
uniform sampler2D _MainTex;
uniform float _Amount;
//--------------------------------------------------------------//
// Dangerous Shader that melts GPU's
//--------------------------------------------------------------//
float4 frag(v2f_img i) : COLOR
{
#ifdef REDGREEN
float2 coord = i.uv - float2(.5, .5);
float multiplier = length(coord), strength;
float2 color;
const float STRENGTH = _Amount / 50;
const int STEPS = 12;
float4 fragColor = float4(0.0, 0.0, 0.0, 1.0);
for (int i = 0; i < STEPS; i ++) {
strength = 1.0 - i*STRENGTH * multiplier;
color = tex2Dlod(_MainTex, float4(coord*strength + float2(.5, .5), 0.0, 0.0)).rg/STEPS;
color.r *= (1.0 - (float)i/STEPS);
color.g *= (float)i/STEPS;
fragColor.rg += color.rg;
}
for (int i = STEPS; i < 2*STEPS; i ++) {
strength = 1.0 - i*STRENGTH * multiplier;
color = tex2Dlod(_MainTex, float4(coord*strength + float2(.5, .5), 0.0, 0.0)).gb/STEPS;
color.r *= (1.0 - (float)(i-STEPS)/STEPS);
color.g *= (float)(i-STEPS)/STEPS;
fragColor.gb += color.rg;
}
for (int i = 2*STEPS; i < 3*STEPS; i ++) {
strength = 1.0 - i*STRENGTH * multiplier;
color = tex2Dlod(_MainTex, float4(coord*strength + float2(.5, .5), 0.0, 0.0)).br/STEPS;
color.r *= (1.0 - (float)(i-2*STEPS)/STEPS);
color.g *= (float)(i-2*STEPS)/STEPS;
fragColor.br += color.rg;
}
return fragColor;
#endif
#ifdef REDBLUE
float2 coord = i.uv - float2(.5, .5);
float multiplier = length(coord), strength;
float2 color;
const float STRENGTH = _Amount / 100;
const int STEPS = 12;
float4 fragColor = float4(0.0, 0.0, 0.0, 1.0);
for (int i = 0; i < STEPS; i ++) {
strength = 1.0 - i*STRENGTH * multiplier;
color = tex2Dlod(_MainTex, float4(coord*strength + float2(.5, .5), 0.0, 0.0)).rg/STEPS;
color.r *= (1.0 - (float)i/STEPS);
color.g *= (float)i/STEPS;
fragColor.rg += color.rg;
}
for (int i = STEPS; i <= 2*STEPS; i ++) {
strength = 1.0 - i*STRENGTH * multiplier;
color = tex2Dlod(_MainTex, float4(coord*strength + float2(.5, .5), 0.0, 0.0)).gb/STEPS;
color.r *= (1.0 - (float)(i-STEPS)/STEPS);
color.g *= (float)(i-STEPS)/STEPS;
fragColor.gb += color.rg;
}
// half cycling correction
fragColor.rb *= (float)STEPS/(1-.5*(STEPS+1)+STEPS);
return fragColor;
#endif
}
ENDCG
}
}
}
//Ummm yeahh, enjoy!
C#
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class AdvancedCA : MonoBehaviour {
#region Variables
private Shader curShader;
public float DispersionAmount = 1.0f;
public enum ColorSet { RedBlue = 0 , RedGreen = 1 };
public ColorSet Colors;
private Material curMaterial;
#endregion
#region Properties
Material material
{
get
{
if(curMaterial == null)
{
curMaterial = new Material(curShader);
curMaterial.hideFlags = HideFlags.HideAndDontSave;
}
return curMaterial;
}
}
#endregion
// Use this for initialization
void Start ()
{
if(!SystemInfo.supportsImageEffects)
{
enabled = false;
return;
}
//Find
curShader = Shader.Find("Custom/AdvancedCA");
}
void OnRenderImage (RenderTexture sourceTexture, RenderTexture destTexture)
{
if(curShader != null)
{
material.SetFloat("_Amount", DispersionAmount);
Graphics.Blit(sourceTexture, destTexture, material);
}
else
{
Graphics.Blit(sourceTexture, destTexture);
}
}
// Update is called once per frame
void Update ()
{
if(ColorSet.RedBlue == Colors)
{
Shader.EnableKeyword("REDBLUE");
Shader.DisableKeyword("REDGREEN");
}
else if(ColorSet.RedGreen == Colors)
{
Shader.EnableKeyword("REDGREEN");
Shader.DisableKeyword("REDBLUE");
}
}
void OnDisable ()
{
if(curMaterial)
{
DestroyImmediate(curMaterial);
}
}
}