Unity3d old movie-style screen effects

Source: Internet
Author: User
Tags pow



Make this ancient effect, need: color yellow, vignette effect, dust and scratches the effect.

Build a C # script that will be placed in the camera

Declare variables first
Oldfilmshader Required shader (one will need to be written)
Oldfilmeffectamount Ancient degree (the degree value of the last interpolation)
Sepiacolor screen color (black and yellow)
Vigenttetexture old movie-style basic stickers
Vigentteamount of the total weight
Scratchestexture Scratch Map
Scratchesyspeed scratches y-direction movement speed
Scratchesxspeed scratches x Direction movement speed
Texture2d dusttexture; Dust map
Dustyspeed Dust y-direction movement speed
Dustxspeed dust x Direction movement speed
Randomvalue Random values

    Public Shader Oldfilmshader;    public float oldfilmeffectamount = 1.0f;    Public Color sepiacolor = Color.White;    Public texture2d vigenttetexture;    public float vigentteamount = 1.0f;    Public texture2d scratchestexture;    public float scratchesyspeed = 10.0f;    public float scratchesxspeed = 10.0f;    Public texture2d dusttexture;    public float dustyspeed = 10.0f;    public float dustxspeed = 10.0f;    Private Material curmaterial;    private float Randomvalue;


Still need onrenderimage () This function captures the image of the camera
Pass the required variable into the shader
Finally copy the source texture to the destination render texture
Graphics.blit ()


    void Onrenderimage (Rendertexture Sourcetex, rendertexture Desttex) {if (Oldfilmshader! = null) { Material.            SetColor ("_sepiacolor", Sepiacolor); Material.            SetFloat ("_vigentteamount", Vigentteamount); Material.            SetFloat ("_oldfilmeffectamount", Oldfilmeffectamount); if (vigenttetexture) {material.            SetTexture ("_vigenttetex", vigenttetexture); } if (scratchestexture) {material.                SetTexture ("_scratchestex", scratchestexture); Material.                SetFloat ("_scratchesyspeed", scratchesyspeed); Material.            SetFloat ("_scratchesxspeed", scratchesxspeed); } if (dusttexture) {material.                SetTexture ("_dusttex", dusttexture); Material.                SetFloat ("_dustxspeed", dustxspeed); Material.                SetFloat ("_dustyspeed", dustyspeed); Material. SetFloat ("_randomvalue", Randomvalue);        } graphics.blit (Sourcetex, Desttex, material);        } else {graphics.blit (Sourcetex, Desttex); }    }


Look at shader again.



declaring variables
Distortion Twist Degree
Cubicdistortion Three-dimensional twist degree



Properties {_maintex ("Base (RGB)", 2D) = "White" {}_vignettetex ("vignettetexture", 2D) = "White" {}_scratchestex ("SCARTC Hestexture ", 2D) =" White "{}_dusttex (" dusttexture ", 2D) =" White "{}_sepiacolor (" Sepiacolor ", Color) = (1,1,1,1) _effecta Mount ("Oldfilmeffectamount", Range (0,1)) = 1.0_vignetteamount ("Vignette Opacity", Range (0,1)) = 1.0_scratchesyspeed (" Scratchesyspeed ", float) = 10.0_scratchesxspeed (" Scratchesxspeed ", float) = 10.0_dustxspeed (" Dustxspeed ", float) = 10.0 _dustyspeed ("Dustyspeed", float) = 10.0_randomvalue ("Randomvalue", float) = 1.0_contrast ("contrast", float) = 3.0_disto Rtion ("Distortion", float) = 0.2_cubicdistortion ("cubicdistortion", float) = 0.6_scale ("scale (Zoom)", float) = 0.8}

Lens barrel distortion correction algorithm to produce barrel distortion effect
Taking a rectangular object into a four-sided outward convex formation of a barrel-shaped image, it is said that the lens has a negative distortion, or barrel-shaped distortion

You'll need to transform the Uvs with this one.
Incoming UV value FLOAT2 coord
Outgoing distorted UV values


Float2 barreldistortion (float2 coord) {//Inspired by Syntheyes lens distortion algorithm//See HTTP://WWW.SSONTECH.COM/C Ontent/lensalg.htmfloat2 h = coord.xy-float2 (0.5, 0.5); float r2 = h.x * h.x + h.y * h.y;float F = 1.0 + r2 * (_distortio n + _cubicdistortion * sqrt (R2)); return f * _scale * H + 0.5;}


Get the color and UV values of an image from V2f_img I
Get distorted Uvs with the barreldistortion () function above
Scratches on UV
Change the scratch UV to make it move with time
Lum to find the saturation of image
Final color First definition = saturation + defined color (floating luminance (linear interpolation))





Find the contrast of the color of the second square
The different layers together make up the screen final effect



Interpolate with old cinematic basic maps first
Then mix with the scratches, with the white interpolation to create the scratches of the flicker effect
Blending with dust, with white interpolation to create a flicker effect of dust
Finally interpolation with the original face of the image
Get the final color



Fixed4 Frag (v2f_img i): Color{//get the colors from the rendertexture and the UV ' S//from the v2f_img structhalf2 distorte DUV = Barreldistortion (i.uv);d Istorteduv = Half2 (i.uv.x, I.uv.y + (_randomvalue * _sintime.z * 0.005)); Fixed4 Rendertex = Tex2d (_maintex, I.UV);//get the pixels from the Vignette Texturefixed4 Vignettetex = tex2d (_vignettetex, I.UV);//process t He scratches UV and pixelshalf2 SCRATCHESUV = HALF2 (i.uv.x + (_randomvalue * _sintime.z * _scratchesxspeed), I.uv.y + (_ti me.x * _scratchesyspeed)); Fixed4 Scratchestex = tex2d (_scratchestex, SCRATCHESUV);//process the Dust UV and Pixelshalf2 du STUV = Half2 (i.uv.x + (_randomvalue * (_SINTIME.Z * _dustxspeed)), I.uv.y + (_randomvalue * (_SINTIME.Z * _dustyspeed))); f Ixed4 Dusttex = tex2d (_dusttex, DUSTUV);//Get the luminosity values from the render texture using the YIQ values.fixed lu m = Dot (fixed3 (0.299, 0.587, 0.114), Rendertex.rgb)//add the constant color to the Lum valuesfixed4 Finalcolor = lum + L ERP (_sepiacolor, _sepiacOlor + fixed4 (0.1f,0.1f,0.1f,1.0f), _randomvalue); finalcolor = Pow (finalcolor, _contrast);//create a constant white col Or we can use to adjust opacity of effectsfixed3 Constantwhite = Fixed3 (1,1,1);//composite together the different layers T o Create finsl screen effectfinalcolor = Lerp (Finalcolor, Finalcolor * Vignettetex, _vignetteamount); Finalcolor.rgb *= Ler P (Scratchestex, Constantwhite, (_randomvalue)); Finalcolor.rgb *= lerp (Dusttex.rgb, Constantwhite, (_RandomValue * _ SINTIME.Z)); Finalcolor = Lerp (Rendertex, Finalcolor, _effectamount); return finalcolor;}





The code is as follows



Using unityengine;using system.collections;public class shadertest:monobehaviour{#region Variables public Shader    Oldfilmshader;    public float oldfilmeffectamount = 1.0f;    Public Color sepiacolor = Color.White;    Public texture2d vigenttetexture;    public float vigentteamount = 1.0f;    Public texture2d scratchestexture;    public float scratchesyspeed = 10.0f;    public float scratchesxspeed = 10.0f;    Public texture2d dusttexture;    public float dustyspeed = 10.0f;    public float dustxspeed = 10.0f;    Private Material curmaterial;    private float Randomvalue;            #endregion #region Properties Public Material Material {get {if (curmaterial = = null)                {curmaterial = new Material (Oldfilmshader);            Curmaterial.hideflags = Hideflags.hideanddontsave;        } return curmaterial;     }} #endregion void Onrenderimage (Rendertexture Sourcetex, rendertexture Desttex) {   if (Oldfilmshader! = null) {material.            SetColor ("_sepiacolor", Sepiacolor); Material.            SetFloat ("_vigentteamount", Vigentteamount); Material.            SetFloat ("_oldfilmeffectamount", Oldfilmeffectamount); if (vigenttetexture) {material.            SetTexture ("_vigenttetex", vigenttetexture); } if (scratchestexture) {material.                SetTexture ("_scratchestex", scratchestexture); Material.                SetFloat ("_scratchesyspeed", scratchesyspeed); Material.            SetFloat ("_scratchesxspeed", scratchesxspeed); } if (dusttexture) {material.                SetTexture ("_dusttex", dusttexture); Material.                SetFloat ("_dustxspeed", dustxspeed); Material.                SetFloat ("_dustyspeed", dustyspeed); Material.            SetFloat ("_randomvalue", Randomvalue);        } graphics.blit (Sourcetex, Desttex, material); }       else {graphics.blit (Sourcetex, Desttex);        }}//Use this for initialization void Start () {if (systeminfo.supportsimageeffects = = False)            {enabled = false;        Return        } if (Oldfilmshader! = null && oldfilmshader.issupported = = False) {enabled = false; }}//update is called once per frame void update () {Vigentteamount = Mathf.clamp01 (Vigentteamou        NT);        Oldfilmeffectamount = Mathf.clamp (Oldfilmeffectamount, 0f, 1.5f);    Randomvalue = Random.range ( -1f, 1f); }}




Shader:





Shader "Custom/testshader" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}_vignettetex ("vignettetexture", 2D) = "whi TE "{}_scratchestex (" scartchestexture ", 2D) =" White "{}_dusttex (" dusttexture ", 2D) =" White "{}_sepiacolor (" Sepiacolor ", Color) = (1,1,1,1) _effectamount (" Oldfilmeffectamount ", Range (0,1)) = 1.0_vignetteamount (" Vignette Opacity ", range (0 , 1)) = 1.0_scratchesyspeed ("Scratchesyspeed", float) = 10.0_scratchesxspeed ("Scratchesxspeed", float) = 10.0_ Dustxspeed ("Dustxspeed", float) = 10.0_dustyspeed ("Dustyspeed", float) = 10.0_randomvalue ("Randomvalue", float) = 1.0_c Ontrast ("contrast", float) = 3.0_distortion ("distortion", float) = 0.2_cubicdistortion ("cubicdistortion", float) = 0.6_ Scale ("scale (Zoom)", Float) = 0.8}subshader {pass{cgprogram#pragma vertex vert_img#pragma fragment Frag#pragma Fragmentoption arb_precision_hint_fastest#include "unitycg.cginc" Uniform sampler2d _maintex;uniform Sampler2D _ Vignettetex;uniform sampler2d _scratchestex;uniform sampler2d _dusttex;fixed4 _sepiacolor;fixed _vignetteamount;fixed _scratchesyspeed;fixed _ScratchesXSpeed;fixed _dustXSpeed; Fixed _dustyspeed;fixed _effectamount;fixed _randomvalue;fixed _contrast;float _distortion;float _cubicDistortion; Float _scale;float2 barreldistortion (float2 coord) {//Inspired by Syntheyes lens distortion algorithm//See HTTP://WWW.SS  Ontech.com/content/lensalg.htmfloat2 h = coord.xy-float2 (0.5, 0.5); float r2 = h.x * h.x + h.y * h.y;float F = 1.0 + r2 * (_distortion + _cubicdistortion * sqrt (R2)); return f * _scale * H + 0.5;} Fixed4 Frag (v2f_img i): Color{//get the colors from the rendertexture and the UV ' S//from the v2f_img structhalf2 distorte DUV = Barreldistortion (i.uv);d Istorteduv = Half2 (i.uv.x, I.uv.y + (_randomvalue * _sintime.z * 0.005)); Fixed4 Rendertex = Tex2d (_maintex, I.UV);//get the pixels from the Vignette Texturefixed4 Vignettetex = tex2d (_vignettetex, I.UV);//process t He scratches UV and pixelshalf2 SCRATCHESUV = HALF2 (i.uv.x + (_randomvalue * _sintime.z * _scratchesxspeed), I.UV.Y + (_time.x * _scratchesyspeed)); Fixed4 Scratchestex = tex2d (_scratchestex, ScratchesUV);//Pr Ocess the Dust UV and Pixelshalf2 dustuv = HALF2 (i.uv.x + (_randomvalue * (_SINTIME.Z * _dustxspeed)), I.uv.y + (_RANDOMVA Lue * (_SINTIME.Z * _dustyspeed))) Fixed4 Dusttex = tex2d (_dusttex, DUSTUV);//Get the luminosity values from the render T Exture using the YIQ values.fixed lum = dot (fixed3 (0.299, 0.587, 0.114), RENDERTEX.RGB);//add the constant color to the L Um valuesfixed4 Finalcolor = lum + lerp (_sepiacolor, _sepiacolor + fixed4 (0.1f,0.1f,0.1f,1.0f), _randomvalue); Finalcolo  R = Pow (Finalcolor, _contrast);//create A constant white color we can use to adjust opacity of effectsfixed3 Constantwhite = Fixed3 (1,1,1);//composite together the different layers to create finsl screen Effectfinalcolor = Lerp (Finalcolor, FINA Lcolor * Vignettetex, _vignetteamount); Finalcolor.rgb *= lerp (Scratchestex, Constantwhite, (_RandomValue)); Finalcolor.rgb *= Lerp (Dusttex.rgb, consTantwhite, (_randomvalue * _sintime.z)); Finalcolor = Lerp (Rendertex, Finalcolor, _effectamount); return finalColor;} ENDCG}} FallBack off}



Photo resources:


vigenttetexture




Scratchestexture






<span style= "Font-size:14px;color: #FF6600;" >dustTexture</span>

                                                                                                  

                                                                                                                                       

-------- by wolf96







Unity3d old movie-style screen effects

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.