Skip to main content
1 of 2
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
        }
    }
}
Mangata
  • 2.8k
  • 1
  • 4
  • 10