之前的面試的時候有被問到胆描,像4月4號清明節(jié)的時候诅妹,很多軟件的界面都變成灰色饼酿,想要實現(xiàn)這個有什么思路抖甘,當時由于緊張枉侧,沒有什么思路帽氓,后面想想盟猖,用屏幕后處理是可以實現(xiàn)的担巩,像很多游戲方援,主角死亡后,游戲畫面變灰涛癌,會不會也是這么實現(xiàn)的呢犯戏?話不多說,直接上代碼:
CS代碼:
首先我要準備渲染RenderTexture的材質(zhì)拳话,這里我們使用一個可以在屏幕后處理中使用的通用基類先匪。
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent (typeof(Camera))]
public class PostEffectsBase : MonoBehaviour {
protected void Start() {
CheckResources();
}
protected void CheckResources() {
bool isSupported = CheckSupport();
if (isSupported == false) {
NotSupported();
}
}
// 檢測設(shè)備是否支持屏幕特效和渲染紋理
protected bool CheckSupport() {
if (SystemInfo.supportsImageEffects == false || SystemInfo.supportsRenderTextures == false) {
Debug.LogWarning("This platform does not support image effects or render textures.");
return false;
}
return true;
}
protected void NotSupported() {
enabled = false;
}
// 創(chuàng)建新的材質(zhì)去渲染動態(tài)紋理
protected Material CheckShaderAndCreateMaterial(Shader shader, Material material) {
if (shader == null) {
return null;
}
if (shader.isSupported && material && material.shader == shader)
return material;
if (!shader.isSupported) {
return null;
}
else {
material = new Material(shader);
material.hideFlags = HideFlags.HideAndDontSave;
if (material)
return material;
else
return null;
}
}
}
創(chuàng)建一個我們操作界面變灰的腳本,繼承自PostEffectsBase弃衍,同時將該腳本掛到攝像機上呀非。
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class SceneGray : PostEffectsBase
{
//設(shè)置渲染的shader
public Shader curShader;
//灰度程度0~1,0為原來顏色镜盯,1為灰色
public float grayScaleAmount = 1.0f;
//新創(chuàng)建的材質(zhì)
private Material curMaterial;
Material material
{
get
{
if (curMaterial == null)
{
curMaterial = CheckShaderAndCreateMaterial(curShader,curMaterial);
}
return curMaterial;
}
}
//屏幕后處理函數(shù)岸裙,設(shè)置shader參數(shù)
void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
{
if (curShader != null)
{
material.SetFloat("_LuminosityAmount", grayScaleAmount);
Graphics.Blit(sourceTexture, destTexture, material);
}
else
{
Graphics.Blit(sourceTexture, destTexture);
}
}
//設(shè)置灰度程度
void Update()
{
grayScaleAmount = Mathf.Clamp(grayScaleAmount, 0, 1.0f);
}
//刪除新建的材質(zhì)
void OnDisable()
{
if (curMaterial)
{
DestroyImmediate(curMaterial);
}
}
}
shader代碼:
Shader "Custom/GrayColor" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_grayScale("_grayScale",Float) = 1.0
}
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
half _grayScale;
struct v2f {
float4 pos : SV_POSITION;
half2 uv: TEXCOORD0;
};
v2f vert(appdata_img v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed4 renderTex = tex2D(_MainTex, i.uv);
//置灰后的灰色值
float gray = dot(renderTex.rgb, float3(0.299, 0.587, 0.114));
fixed4 grayColor = fixed4(gray, gray, gray,renderTex.a);
//將原來的顏色與灰色做插值運算
fixed4 finalColor = lerp(renderTex,grayColor,_grayScale);
return finalColor;
}
ENDCG
}
}
Fallback Off
}
最終效果:
GrayScaleAmount=0.png
GrayScaleAmount=1.png