Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Render to texture isn't setting the texture

Discussion in 'Editor & General Support' started by Spankenstein, Nov 13, 2014.

  1. Spankenstein

    Spankenstein

    Joined:
    Jan 9, 2014
    Posts:
    25
    I have a shader that takes a texture and adds a transparent overlay. It is blue in the image below:



    Shader "Custom/Overlay"
    {
    Properties
    {
    _MainTex ("Texture", Rect) = "white" {}
    _Color ("Color", Color) = (0.0, 0.0, 0.0, 0.0)
    }
    SubShader
    {
    Tags { "Queue" = "Overlay" } // Render after everything else

    Pass
    {
    Blend SrcAlpha OneMinusSrcAlpha // Alpha blending
    ZWrite Off
    ZTest Always // Deactivate depth test

    CGPROGRAM

    #pragma vertex vert
    #pragma fragment frag
    #include "UnityCG.cginc"

    sampler2D _MainTex;
    uniform float4 _Color;

    struct VertexShaderInput
    {
    float4 Position : POSITION0;
    float2 TextureCoordinates : TEXCOORD0;
    };

    struct VertexShaderOutput
    {
    float4 Position : POSITION0;
    float2 TextureCoordinates : TEXCOORD0;
    };

    VertexShaderOutput vert(VertexShaderInput input)
    {
    VertexShaderOutput output;

    output.Position = input.Position;
    output.Position.w = 1.0;

    output.TextureCoordinates = input.TextureCoordinates;

    return output;
    }

    float4 frag(VertexShaderOutput input) : COLOR
    {
    float4 backgroundCol = tex2D(_MainTex, input.TextureCoordinates);

    return float4(lerp(backgroundCol, _Color, _Color.a).rgb, 1.0);
    }

    ENDCG
    }
    }
    }

    If I change the Target Texture then the target texture never gets written to with the blue image that is on the screen.

    Why isn't the blue tinted image being written to the Target Texture when it displays just fine on the screen when there is no Target Texture set?
     
  2. Spankenstein

    Spankenstein

    Joined:
    Jan 9, 2014
    Posts:
    25