Search Unity

Question How can I make render my camera a Shader?

Discussion in 'Shaders' started by claudiok, Mar 19, 2023.

  1. claudiok

    claudiok

    Joined:
    Aug 16, 2022
    Posts:
    18
    I began a couple of days ago to learn about shading , it's very interesting.
    But i would to know how to make the camera render a shader instead the original colours?

    Here is my code of the shader that i want to make the camera render:
    Code (CSharp):
    1.  
    2. Shader "Effects/1900's"
    3. {
    4.     Properties{
    5.         _MainTex("Texture", 2D) = "white" {}
    6.         _ScanlineIntensity("Scanline Intensity", Range(0, 1)) = 0.2
    7.         _ScanlineSpeed("Scanline Speed", Range(0, 10)) = 0.1
    8.         _Vignette("Vignette", Range(0, 1)) = 0.5
    9.         _NoiseIntensity("Noise Intensity", Range(0, 1)) = 0.1
    10.         _Resolution("Resolution", Range(0.1, 1)) = 1
    11.     }
    12.         SubShader{
    13.             Tags { "RenderType" = "Opaque" }
    14.             LOD 100
    15.             Pass {
    16.                 CGPROGRAM
    17.                 #pragma vertex vert
    18.                 #pragma fragment frag
    19.                 #include "UnityCG.cginc"
    20.  
    21.                 struct appdata {
    22.                     float4 vertex : POSITION;
    23.                     float2 uv : TEXCOORD0;
    24.                 };
    25.  
    26.                 struct v2f {
    27.                     float2 uv : TEXCOORD0;
    28.                     float4 vertex : SV_POSITION;
    29.                 };
    30.  
    31.                 sampler2D _MainTex;
    32.                 float _ScanlineIntensity;
    33.                 float _ScanlineSpeed;
    34.                 float _Vignette;
    35.                 float _NoiseIntensity;
    36.                 float _Resolution;
    37.  
    38.                 v2f vert(appdata v) {
    39.                     v2f o;
    40.                     o.vertex = UnityObjectToClipPos(v.vertex);
    41.                     o.uv = v.uv * _Resolution;
    42.                     return o;
    43.                 }
    44.  
    45.                 fixed4 frag(v2f i) : SV_Target {
    46.                     // Sample the texture
    47.                     fixed4 tex = tex2D(_MainTex, i.uv);
    48.  
    49.                 // Apply scanlines
    50.                 float scanline = sin(i.uv.y * 400 * _Resolution + _Time.y * _ScanlineSpeed);
    51.                 scanline = pow(scanline, 2.5);
    52.                 tex.rgb -= tex.rgb * scanline * _ScanlineIntensity;
    53.  
    54.                 // Apply vignette
    55.                 float vignette = 1 - length(i.uv - 0.5) / sqrt(0.5);
    56.                 vignette = saturate(vignette * vignette * _Vignette);
    57.                 tex.rgb *= vignette;
    58.  
    59.                 // Add film noise
    60.                 float noise = tex2D(_MainTex, i.uv * 100).r;
    61.                 noise = (noise * 2 - 1) * _NoiseIntensity;
    62.                 tex.rgb += noise;
    63.  
    64.                 // Convert to grayscale
    65.                 float gray = dot(tex.rgb, float3(0.299, 0.587, 0.114));
    66.                 return fixed4(gray, gray, gray, 1);
    67.             }
    68.             ENDCG
    69.         }
    70.         }
    71.             FallBack "Diffuse"
    72. }