Search Unity

Alpha channel from render texture by screen position

Discussion in 'Shaders' started by piratrodjer, May 26, 2021.

  1. piratrodjer

    piratrodjer

    Joined:
    Dec 3, 2017
    Posts:
    1
    Hello everyone!
    I'm trying to write a shader that imitates the coloring of a picture with a brush. It looks like this. There is a "Brush" game object on the stage, which leaves behind a trail of particles. These particles are rendered into a render texture using the second camera. I plan to use this texture to get the alpha channel. And there is a paintable picture that has a material with my shader. It is assumed that the shader in the fragment function should take the alpha channel at the screen position from the render texture.

    In the shader graph, the logic looks like this
    And everything works great

    But when I tried to write this shader myself, problems started. I can't to correctly calculate the screen position and translate it to the uv of render texture.

    Here is the shader code
    Code (CSharp):
    1.  
    2. Shader "Unlit/DrawShader2"
    3. {
    4.     Properties
    5.     {
    6.         [NoScaleOffset]_AlphaTexture("AlphaTexture", 2D) = "white" {}
    7.         [NoScaleOffset]_MainTex("MainTex", 2D) = "white" {}
    8.         _Alpha("Alpha", Float) = 1
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="Transparent"}
    13.         Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
    14.         Cull Off
    15.         ZWrite Off
    16.         LOD 200
    17.  
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.  
    24.             #include "UnityCG.cginc"
    25.  
    26.             struct appdata
    27.             {
    28.                 float4 vertex : POSITION;
    29.                 float2 uv : TEXCOORD0;
    30.             };
    31.  
    32.             struct v2f
    33.             {
    34.                 float2 uv : TEXCOORD0;
    35.                 float4 vertex : SV_POSITION;
    36.                 float4 screenPos: TEXCOORD1;
    37.             };
    38.  
    39.             sampler2D _MainTex;
    40.             sampler2D _AlphaTexture;
    41.             float4 _MainTex_ST;
    42.             float4 _AlphaTexture_ST;
    43.             float4 _AlphaTexture_TexelSize;
    44.  
    45.             v2f vert (appdata v)
    46.             {
    47.                 v2f o;
    48.                 o.vertex = UnityObjectToClipPos(v.vertex);
    49.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    50.                 o.screenPos = ComputeScreenPos(o.vertex);
    51.                 return o;
    52.             }
    53.  
    54.             fixed4 frag (v2f i) : SV_Target
    55.             {
    56.                 float2 screenUV = i.screenPos.xy/i.screenPos.zw;
    57.                
    58.                 fixed4 col = tex2D(_MainTex, i.uv);
    59.                 fixed4 alphaCol = tex2D(_AlphaTexture, screenUV);
    60.                 col.w = col.w * alphaCol.x;
    61.                 return col;
    62.             }
    63.             ENDCG
    64.         }
    65.     }
    66. }
    here is result:



    what i'm doing wrong?