Unity3d screen effect for full screen black and white effects similar to death

Source: Internet
Author: User

full-screen effect black and white (learn about the shader and screen effects development cheats)


the effect of death or black and white effects can be achieved

The principle is to change the color (saturation) when the camera is rendered by the onrenderimage () function.


Create a new C # script that will be placed in the camera

[Executeineditmode]
is to let the script not start and run directly, easy to observe the effect


Using unityengine;using System.Collections; [Executeineditmode]public class shadertest:monobehaviour{



Start declaring the required variable

Curshader need to use the shader;
Grayscaleamount Gray size;


Public Shader curshader;public Float grayscaleamount = 1.0f;private Material curmaterial;
Detection at start
If! Systeminfo.supportsimageeffects (if the computer does not support image processing)
Or no Curshader (will be used later) or!curshader.issupported (cannot run this shader on the end user's graphics card)
Just cancel the script.
Enabled = FALSE;


void Start () {if (systeminfo.supportsimageeffects = = False) {enabled = False;return;} if (Curshader! = null && curshader.issupported = = False) {enabled = false;}}
onrenderimage () grab the rendered image of the camera (we change it inside)
Pass the grayscale value to the material.

Then change it graphics.blit () (copy source texture to destination render texture)


void Onrenderimage (Rendertexture sourcetexture, rendertexture desttexture) {if (Curshader! = null) {material. SetFloat ("_luminosityamount", Grayscaleamount); Graphics.blit (sourcetexture, desttexture, material);} else {graphics.blit (sourcetexture, desttexture);}}

Put this phrase in the update
Grayscaleamount = Mathf.clamp (Grayscaleamount, 0.0f, 1.0f);
The goal is to make it easy for you to change the grayscale value at any time. If the grayscale value determines this sentence can not be.

void Update () {grayscaleamount = Mathf.clamp (Grayscaleamount, 0.0f, 1.0f);}


Easy to remove recycling

void Ondisable () {if (curmaterial! = null) {destroyimmediate (curmaterial);}}


Look at shader again.



The book uses pure CG code, not Unity's built-in shader language, so screen effects are more optimized because only the pixels of the rendered texture need to be manipulated.

First declare the variable in the properties
Include Maintex and grayscale values

Properties {_maintex ("Base (RGB)", 2D) = "White" {}_luminosityamount ("grayscale Amount", Range (0.0, 1.0)) = 1.0}

Add Pass Module
Use #pragma to declare some new


Subshader {Pass {cgprogram#pragma vertex vert_img#pragma fragment Frag#include "Unitycg.cginc"

The core is in this frag function.
Tex2d () returns the data for a texture
Put the red *0.299, Green *0.587, Blue *0.114, to get a luminance value (that is, the color depth value) luminosity

The final color value finalcolor is a linear interpolation of Rendertex and grayscale values, depending on the luminance value (shade value)
OK, the death effect screen effect is done.


Fixed4 Frag (v2f_img i): Color{//get the colors from the rendertexture and the UV ' S//from the v2f_img structfixed4 Rendert ex = tex2d (_maintex, I.UV);//apply the luminosity values to our render texturefloat luminosity = 0.299 * RENDERTEX.R + 0.5 * RENDERTEX.G + 0.114 * rendertex.b;fixed4 Finalcolor = lerp (Rendertex, luminosity, _luminosityamount); return FinalColo t;;


All of the following code


Using unityengine;using System.Collections; [Executeineditmode]public class shadertest:monobehaviour{#region variablespublic Shader curshader;public float Grayscaleamount = 1.0f;private Material curmaterial; #endregion #region propertiespublic Material Material {get {if ( Curmaterial = = null) {curmaterial = new Material (curshader); curmaterial.hideflags = Hideflags.hideanddontsave;} return curmaterial;}} #endregion//Use of this for Initializationvoid Start () {if (systeminfo.supportsimageeffects = = False) {enabled = False;retu RN;} if (Curshader! = null && curshader.issupported = = False) {enabled = false;}} void Onrenderimage (Rendertexture sourcetexture, rendertexture desttexture) {if (Curshader! = null) {material. SetFloat ("_luminosityamount", Grayscaleamount); Graphics.blit (sourcetexture, desttexture, material);} else {graphics.blit (sourcetexture, desttexture);}} Update is called once per framevoid update () {Grayscaleamount = Mathf.clamp (Grayscaleamount, 0.0f, 1.0f);} void Ondisable () {if (curmaterial! = null) {destroyimmediate (curmaterial);}}} 

Shader

Shader "Custom/imagetest" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}_luminosityamount ("grayscale Amount", Rang E (0.0, 1.0)) = 1.0}subshader {Pass {cgprogram#pragma vertex vert_img#pragma fragment Frag#include "Unitycg.cginc" uniform Sampler2d _maintex;fixed _luminosityamount;fixed4 Frag (v2f_img i): Color{//get the colors from the rendertexture and the UV ' S//from the v2f_img structfixed4 Rendertex = tex2d (_maintex, I.UV);//apply the luminosity values to our render Texturef loat luminosity = 0.299 * RENDERTEX.R + 0.587 * rendertex.g + 0.114 * rendertex.b;fixed4 Finalcolor = Lerp (Rendertex, Lumi Nosity, _luminosityamount); return finalcolor;} Endcg}}fallback "Diffuse"}



---------by wolf96



Unity3d screen effect for full screen black and white effects similar to death

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.