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

Rendering textured quad with alpha using GL methods

Discussion in 'Shaders' started by iko79, Sep 24, 2013.

  1. iko79

    iko79

    Joined:
    Jan 21, 2013
    Posts:
    45
    Hi,

    I'm trying to render a textured quad in OnRenderImage using GL methods. I've got a Material with this shader:
    Code (csharp):
    1. Shader "Custom/MarkerShader" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _SpecColor ("Spec Color", Color) = (1,1,1,0)
    5.         _Emission ("Emissive Color", Color) = (0,0,0,0)
    6.         _Shininess ("Shininess", Range (0.1, 1)) = 0.7
    7.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    8.     }
    9.  
    10.     SubShader {
    11.         Tags {"RenderType"="Transparent" "Queue"="Transparent"}
    12.         // Render normally
    13.         Pass {
    14.             ZWrite Off
    15.             Blend SrcAlpha OneMinusSrcAlpha
    16.             ColorMask RGB
    17.             Material {
    18.                 Diffuse [_Color]
    19.                 Ambient [_Color]
    20.                 Shininess [_Shininess]
    21.                 Specular [_SpecColor]
    22.                 Emission [_Emission]
    23.             }
    24.             Lighting On
    25.             SetTexture [_MainTex] {
    26.                 Combine texture * primary DOUBLE, texture * primary
    27.             }
    28.         }
    29.     }
    30. }
    In the inspector I set Main Color and Spec Color to (0,0,0,1), Emission to (0,1,0,1) and Shininess to 0. The texture is set to a png with alpha channel. I import the texture with "Alpha is Transparency" checked. In OnRenderImage, I first blit src to dst, then I try to draw the textured quad like this:
    Code (csharp):
    1.     Material mat = this.MarkerMaterial;
    2.  
    3.         GL.Color( Color.white );
    4.  
    5.         mat.SetPass( 0 );
    6.  
    7.         GL.PushMatrix();
    8.         {
    9.             GL.LoadOrtho();
    10.             GL.LoadIdentity();
    11.  
    12.             pos.x *= ar;
    13.             pos.y = 1.0f - pos.y;
    14.             GL.MultMatrix( Matrix4x4.Scale( new Vector3( 1.0f / ar, 1.0f, 1.0f ) ) * Matrix4x4.TRS( pos, q, Vector3.one ) );
    15.  
    16.             GL.Begin( GL.QUADS );
    17.             {
    18.                 GL.Vertex3( -markerWidth, -markerHeight, 0.0f );
    19.                 GL.Vertex3( -markerWidth, markerHeight, 0.0f );
    20.                 GL.Vertex3( markerWidth, markerHeight, 0.0f );
    21.                 GL.Vertex3( markerWidth, -markerHeight, 0.0f );
    22.             }
    23.             GL.End();
    24.         }
    25.         GL.PopMatrix();
    When I try to render using the texture including alpha channel, I see nothing. When I replace it by another texture without alpha, it works.

    Any ideas about what I'm doing wrong?

    Thanks a lot.


    <edit>
    Now, that's embarrassing. When I switched from lines to quad I forgot that I now have to also set the uv coordinates. I was so distracted by all the ShaderLab syntax I'm not quite familiar with that I thought I must have made a mistake there somewhere.
    </edit>
     
    Last edited: Sep 24, 2013
  2. MrEastwood

    MrEastwood

    Joined:
    Dec 9, 2013
    Posts:
    19
    You need to assign texture coordinates before you submit a vertex. Use GL.TexCoord2(x, y); before each vertex.
     
    SamFernGamer4k and ProFiLeR4100 like this.