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. Dismiss Notice

graphics.blit ( src, dest, mat) How to grab the src in the shader?

Discussion in 'Shaders' started by Quatum1000, Feb 19, 2015.

  1. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    888
    Hi everyone,

    I use the graphics.blit ( src, dest, mat) in a cs script.
    Unfortunately I did not receive the src in the frag() shader.

    I tried to grab the src from the _MainTex after using graphics.blit graphics.blit ( src, dest, mat)
    but it stays white.

    Code (CSharp):
    1.  
    2.        dest = new RenderTexture(256, 256, 0);
    3.        ...
    4.        src = new RenderTexture(256, 256, 0);
    5.        src.wrapMode = TextureWrapMode.Repeat;
    6.  
    7.        // init color for RT                    
    8.       RenderTexture.active = src;
    9.       GL.Begin(GL.TRIANGLES);
    10.       GL.Clear(true, true, new Color(.2f, .3f, .6f, 1));
    11.       GL.End();
    12.  
    13.       Graphics.Blit(src, dest, mat);
    14.  
    15.  



    Test frag() shader:

    Code (CSharp):
    1. Properties
    2.     {
    3.         _MainTex    ("Base (RGB)", 2D) = "white" { }
    4.     }
    5.  
    6.     SubShader {
    7.  
    8.     Lighting Off
    9.     ZTest Always Cull Off ZWrite Off
    10.         Fog { Mode off }
    11.  
    12.         Pass {
    13.         CGPROGRAM
    14.             #pragma target 3.0
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             #include "UnityCG.cginc"
    18.          
    19.             uniform sampler2D _MainTex;
    20.                                  
    21.             // TRANSFORM_TEX in the vertex method
    22.             // _ST variables are automatically populated with the
    23.             // texture tiling (x,y) and Offset (z,w) values from the
    24.             // correspondingly-named texture (in this case, _MainTex).                                      
    25.  
    26.          half4 _MainTex_ST;                                              
    27.                                  
    28.             struct v2f {
    29.                 float4 pos    : SV_POSITION;
    30.                 float2 mainuv : TEXCOORD0;
    31.             };
    32.          
    33.         v2f vert (appdata_base v){
    34.         v2f o;
    35.         o.pos         = mul (UNITY_MATRIX_MVP, v.vertex);
    36.         o.mainuv     = TRANSFORM_TEX (v.texcoord.xy, _MainTex);
    37.         // Tried both..  
    38.         //o.mainuv     = v.texcoord;
    39.         return o;
    40.                }
    41.          
    42.             half4 frag (v2f i) : COLOR0{
    43.          
    44.                 half3  main = tex2D(_MainTex, i.mainuv).xyz;
    45.                  return half4(main.x , main.y, main.z, 1);  
    46.             }
    47.  
    48.             ENDCG
    49.         }
    50.     }
    How to receive the src coming from the graphics.blit ( src, dest, mat) then?

    Thanks and kind regards,
    Quatum
     
  2. varfare

    varfare

    Joined:
    Feb 12, 2013
    Posts:
    227
    Try this:

    Code (CSharp):
    1. Shader "YourShader" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "" {}
    4.     }
    5.     SubShader {
    6.         ZTest Always Cull Off ZWrite Off Fog { Mode Off }
    7.         Pass {
    8.             CGPROGRAM
    9.             #pragma vertex vert_img
    10.             #pragma fragment frag
    11.             #pragma fragmentoption ARB_precision_hint_fastest
    12.             #include "UnityCG.cginc"
    13.            
    14.             uniform sampler2D _MainTex;
    15.  
    16.             fixed4 frag(v2f_img i) : COLOR {
    17.            
    18.                 fixed4 renderTex = tex2D( _MainTex, i.uv);
    19.                 return renderTex;
    20.             }
    21.            
    22.             ENDCG
    23.         }
    24.        
    25.     }
    26.  
    27. }
    28.  
    The code is tested and it works. But if it does not, then you are probably doing something wrong while blitting.
     
  3. TechnoCraft

    TechnoCraft

    Joined:
    Apr 6, 2012
    Posts:
    28
    Graphics.Blit is mostly used for implementing image effects, and requires Unity Pro. Unity Free does not support anything that involves RenderTexture.
     
  4. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    888
    Thanks for your answers.. Yes I have the pro version U5RC2.
     
  5. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    888
    Unfortunately it does not currently. Here is an full example:
    Don't know whats wrong here.

    I want to change the SRC texture in the Shader and the result should to into DST.
    Does anyone know how to do correctly?
    Thank you..

    Code (CSharp):
    1. Shader "Custom/RT2Shader" {
    2. Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "" {}
    4.     }
    5.     SubShader {
    6.         ZTest Always Cull Off ZWrite Off Fog { Mode Off }
    7.         Pass {
    8.             CGPROGRAM
    9.             #pragma vertex vert_img
    10.             #pragma fragment frag
    11.             #pragma fragmentoption ARB_precision_hint_fastest
    12.             #include "UnityCG.cginc"
    13.      
    14.             uniform sampler2D _MainTex;
    15.             fixed4 frag(v2f_img i) : COLOR {
    16.      
    17.                 fixed4 renderTex = tex2D( _MainTex, i.uv);
    18.                 return renderTex;
    19.             }
    20.      
    21.             ENDCG
    22.         }
    23.     }
    24. }
    25.  
    C#
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RT2Shader : MonoBehaviour {
    5.  
    6.     public RenderTexture Src;
    7.     public RenderTexture Dst;
    8.     public Material Mat;
    9.  
    10.     void Start () {
    11.  
    12.         Src = new RenderTexture(256, 256, 0);
    13.         Dst = new RenderTexture(256, 256, 0);
    14.     }
    15.  
    16.     void Update () {
    17.  
    18.         // Green in
    19.         RenderTexture.active = Src;
    20.         GL.Begin(GL.TRIANGLES);
    21.         GL.Clear(true, true, new Color(0f, 1f, 0f, 1f));
    22.         GL.End();
    23.  
    24.         RenderTexture.active = Dst;
    25.         GL.Begin(GL.TRIANGLES);
    26.         GL.Clear(true, true, new Color(1f, 0f, 0f, 1f));
    27.         GL.End();
    28.  
    29.         Graphics.Blit(Src, Dst, Mat);
    30.         // Red was overwritten with Green
    31.     }
    32. }
    33.  
    Src (Green) will transfered directly into DST.
    Untitled-1.jpg
     
    Last edited: Apr 3, 2015