您的位置:首页 > 其它

【Shader】游戏屏幕黑白特效

2017-12-17 19:06 267 查看
.cs脚本挂载到MainCamera

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class TestImage : MonoBehaviour {

#region
public Shader curShader;
private float grayScaleAmount = 1.0f;
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;
}

if (!curShader && !curShader.isSupported)
{
enabled = false;
}
}

//   // Update is called once per frame
//   void Update () {
//       grayScaleAmount = Mathf.Clamp(grayScaleAmount, 0.0f, 1.0f);
//}

private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (curShader != null)
{
material.SetFloat("_LuminosityAmount",grayScaleAmount);
Graphics.Blit(source,destination,material);
} else
{
Graphics.Blit(source,destination);
}
}

private void OnDisable()
{
if (curMaterial)
{
DestroyImmediate(curMaterial);
}
}
}


.shader脚本附加到上面的属性中

Shader "Hidden/NewImageEffectShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_LuminosityAmount("GrayScale Amount",Range(0.0,1)) = 1.0
}
SubShader
{

Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"

uniform sampler2D _MainTex;
fixed _LuminosityAmount;

fixed4 frag (v2f_img i) : COLOR
{
fixed4 renderTex = tex2D(_MainTex,i.uv);
float luminosity = 0.299 * renderTex.r + 0.587 * renderTex.g + 0.114 * renderTex.b;
fixed4 finalColor = lerp(renderTex,luminosity,_LuminosityAmount);
return finalColor;
}
ENDCG
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐