Thursday, June 16, 2022
HomeGame Developmentunity - Displaying a RenderTexture in 2D

unity – Displaying a RenderTexture in 2D


enter image description here

Within the higher proper nook is a 3D Quad (MeshRenderer) and within the decrease proper nook is a 2D Sq. (SpriteRenderer). Now let me clarify the distinction and the right way to create them.

Utilizing the shortcut to create a mesh(dragging the Render Texture onto the mesh) will routinely create a fabric with a regular shader, and set the albedo because the dragged texture. So naturally this mesh might be affected by lighting.(I added a yellow direct mild there). And making a sprite renderer(creating a brand new materials with a customized shader) immediately shows the colour of the feel, which will depend on the shader and may be additional prolonged.

Why cannot you drag the feel to the spriteRenderer for shortcut operations? spriteRenderer requires a fabric to have a _MainTex parameter, the default shader(Sprites/Default) has just one texture channel (and is already utilized by spriteRenderer), so it’s unreasonable to connect an additional materials to SpriteRenderer’s materials. That is why the brand new model of unity units _MainTex is inoperable. The sprite is designed to show a picture solely, and such a setup is cheap.

Anyway, we will create a customized shader, present a _MainTex parameter however not use it, and create a brand new texture channel to position the RenderTexture, and use its shade.

Create a brand new materials, choose the shader because the customized shader beneath, and set it as the fabric of the SpriteRenderer. set the RenderTexture to _SubTex.

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

        Move
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #embrace "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
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments