Skip to main content
2 of 2
added 1133 characters in body
Mangata
  • 2.8k
  • 1
  • 4
  • 10

enter image description here

In the upper right corner is a 3D Quad (MeshRenderer) and in the lower right corner is a 2D Square (SpriteRenderer). Now let me explain the difference and how to create them.

Using the shortcut to create a mesh(dragging the Render Texture onto the mesh) will automatically create a material with a standard shader, and set the albedo as the dragged texture. So naturally this mesh will be affected by lighting.(I added a yellow direct light there). And creating a sprite renderer(creating a new material with a custom shader) directly displays the color of the texture, which depends on the shader and can be further extended.

Why can't you drag the texture to the spriteRenderer for shortcut operations? spriteRenderer requires a material to have a _MainTex parameter, the default shader(Sprites/Default) has only one texture channel (and is already used by spriteRenderer), so it is unreasonable to attach an extra material to SpriteRenderer's material. This is why the new version of unity sets _MainTex is inoperable. The sprite is designed to display an image only, and such a setup is reasonable.

Anyway, we can create a custom shader, provide a _MainTex parameter but not use it, and create a new texture channel to place the RenderTexture, and use its color.

Create a new material, select the shader as the custom shader below, and set it as the material of the SpriteRenderer. set the RenderTexture to _SubTex.

Shader "Custom/NewShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _SubTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            sampler2D _SubTex;
            float4 _SubTex_ST;
            
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_SubTex, i.uv);
                return col;
            }
            ENDCG
        }
    }
}

Some extra notes to clarify:

There are two ways to achieve Displaying a RenderTexture:

  1. Plan A: Use a 3D Quad (MeshRenderer)

    1. Drag the renderTexuture onto this mesh according to the tutorial instructions.
    2. And add a light to your scene. Because this texture is used as an albedo for the mesh. Its final color is affected by lighting, if there is no lighting it is black.
  2. Plan B: Use a 2D Square (SpriteRenderer)

    1. Create a .shader file in your Assets dir. And copy the code above into it.
    2. Create a new material and select the shader for it.(Custom/NewShader)
    3. Drag the renderTexuture file to the material(_SubTex).
    4. Drag the material to the 2D Square.

the preview window displays my sprite fine, but my square is still white in the end.

Because the spriteRenderer is used to display a sprite, it will overwrite the texture of the sprite to the material at runtime. so _MainTex is already used by spriteRenderer. You can not use that.

So we could create a new shader with extra texture paramater(_SubTex) and just ignore _MainTex. It will be fine.

Mangata
  • 2.8k
  • 1
  • 4
  • 10