Search Unity

Question Surface Shader with RenderTexture Effect

Discussion in 'Shaders' started by mybigorangehead, Dec 15, 2022.

  1. mybigorangehead

    mybigorangehead

    Joined:
    Jun 25, 2016
    Posts:
    4
    Hi!

    I am trying to write a shader that draws the colors of a render texture on top of anything in the space of the orthographic camera.

    My current setup:
    1. Orthographic camera in scene that renders to a render texture. Set to Don't Clear or Solid Color (0,0,0,0). Only renders the TransparentFX layer
    2. Ground and burger mesh using my shader
    3. Two cubes (blue and orange) with layer = Transparent FX using the standard shader. Red particle system with layer = TransparentFX using the Particles/UnlitShader.

    I want the shader to draw the colors in the render texture if there are any otherwise use the mainText * Color value. Problems arise when the alpha value != 1 in the render texture. Specifically the particles get drawn as invisible when I try to use the render texture alpha.

    Photo 1. See render texture in bottom right. Red pixels from a rendered particle system (alpha's don't equal 1?) are drawn completely transparent and effectively cut a hole in the mesh.
    ShaderUsingRenderTextureAlpha.png
    Photo 2. In shader, set alphas of render texture to 1. Closer to what I'd expect but this has unwanted black pixels around it.
    ShaderWithRenderTextureAlphas=1.png

    How can I get this to look as expected?

    Code (CSharp):
    1. Shader "Custom/Paint"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Texture", 2D) = "white" {}
    7.         _RT("Texture", 2D) = "white" {}
    8.         _OrthoSize("Othro size", Float) = 4
    9.         _CamWorldPos("Camera Pos", Vector) = (0, 0, 0)  
    10.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    11.         _Metallic ("Metallic", Range(0,1)) = 0.0
    12.      
    13.     }
    14.     SubShader
    15.     {
    16.         Tags { "RenderType"="Transparent" "Queue"="Transparent" }
    17.         Blend SrcAlpha OneMinusSrcAlpha
    18.  
    19.         CGPROGRAM
    20.        
    21.         #pragma surface surf Standard fullforwardshadows alpha:blend      
    22.         #pragma target 3.0
    23.  
    24.         sampler2D _MainTex;
    25.         sampler2D _RT;
    26.         struct Input
    27.         {
    28.             float2 uv_MainTex;
    29.             float4 screenPos;
    30.             float3 worldPos;
    31.         };
    32.  
    33.         half _Glossiness;
    34.         half _Metallic;
    35.         fixed4 _Color;
    36.         float3 _CamWorldPos;
    37.         float _OrthoSize;
    38.        
    39.         UNITY_INSTANCING_BUFFER_START(Props)
    40.          UNITY_INSTANCING_BUFFER_END(Props)
    41.  
    42.         void surf (Input IN, inout SurfaceOutputStandard o)
    43.         {
    44.             // Albedo comes from a texture tinted by color
    45.             fixed4 mainTexColor = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    46.  
    47.  
    48.             //convert from world space to rendertexture coords
    49.             float2 uv = IN.worldPos.xz - _CamWorldPos.xz;
    50.             uv = uv / (_OrthoSize * 2);
    51.             uv += .5;
    52.  
    53.             fixed4 renderTexColor = tex2D(_RT, uv);
    54.  
    55.             //render texture has any color?
    56.             if (renderTexColor.r != 0 || renderTexColor.g != 0 || renderTexColor.b != 0) {
    57.          
    58.                 o.Albedo = renderTexColor.rgb;
    59.                 o.Alpha = 1;
    60.                 //o.Alpha = renderTexColor.a; - DOES NOT WORK for particles. Draws completely transparent
    61.             }
    62.             else {
    63.                 o.Albedo = mainTexColor.rgb;
    64.                 o.Alpha = mainTexColor.a;
    65.             }
    66.  
    67.             // Metallic and smoothness come from slider variables
    68.             o.Metallic = _Metallic;
    69.             o.Smoothness = _Glossiness;
    70.  
    71.         }
    72.         ENDCG
    73.     }
    74.     FallBack "Diffuse"
    75. }
    76.