Search Unity

Custom Shader Layering.

Discussion in 'Shaders' started by SyncInteractive, Jul 15, 2014.

  1. SyncInteractive

    SyncInteractive

    Joined:
    Mar 17, 2014
    Posts:
    12
    Hi guys

    I am very new to this shader business and need help fixing/understanding a shader.

    i am using TK2D Toolkit in my project and would like to draw on screen using my fingers (mobile development) I've got the drawing to work fine but it is always in front of all the assets on screen. I modified the PremulVertexColor shader and got the line to go behind the asset but then if the asset has any transparency it acts like a mask as shown below..

    Screen Shot 2014-07-14 at 15.56.41.png

    The NewPremulVertexColor shader:

    //unlit, vertexcolour, premultipliedalphablend

    Shader"tk2d/NewPremulVertexColor"
    {
    Properties
    {
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    }

    SubShader
    {
    Tags {"Queue"="Overlay""IgnoreProjector"="True""RenderType"="Transparent"}
    LightingOffCullOffFog { ModeOff } AlphaTestGreater0.5
    LOD110
    Pass
    {
    CGPROGRAM
    #pragmavertexvert_vct
    #pragmafragmentfrag_mult
    #pragmafragmentoptionARB_precision_hint_fastest
    #include "UnityCG.cginc"

    sampler2D_MainTex;
    float4_MainTex_ST;

    structvin_vct
    {
    float4vertex : POSITION;
    float4color : COLOR;
    float2texcoord : TEXCOORD0;
    };

    structv2f_vct
    {
    float4vertex : POSITION;
    fixed4color : COLOR;
    float2texcoord : TEXCOORD0;
    };

    v2f_vctvert_vct(vin_vctv)
    {
    v2f_vcto;
    o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    o.color = v.color;
    o.texcoord = v.texcoord;
    returno;
    }

    fixed4frag_mult(v2f_vcti) : COLOR
    {
    fixed4col = tex2D(_MainTex, i.texcoord) * i.color;
    returncol;
    }
    ENDCG
    }
    }

    SubShader
    {
    Tags {"Queue"="Transparent""IgnoreProjector"="True""RenderType"="Transparent"}
    ZWriteOffBlendOneOneMinusSrcAlphaCullOffFog { ModeOff }
    LOD150

    BindChannels
    {
    Bind"Vertex", vertex
    Bind"TexCoord", texcoord
    Bind"Color", color
    }

    Pass
    {
    LightingOff
    SetTexture [_MainTex] { combinetexture * primary }
    }
    }

    }

    The shader for the line:

    Shader "LineShader" {
    Properties {
    _Color ("Base (RGB)", Color) = (1, 1, 1, 1)
    }
    SubShader {
    Tags {"Queue"="Transparent""IgnoreProjector"="True"}
    LightingOff
    CullOff

    BindChannels {
    Bind"Color", color
    }
    Pass {
    Color [_Color]
    }
    }
    }

    Thanks
    Abdullah Awan
     
  2. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    nice, i didnt entirely understand the technical nature of the question from that explanation.

    I've got the drawing to work fine but it is always in front of all the assets on screen. I modified the PremulVertexColor shader and got the line to go behind the asset but then if the asset has any transparency it acts like a mask as shown below.

    you have to figure out transparency of the different objects on screen (in unity asset is more like a loadable file, textured gameobject is what we call thigns on screen, with a renderer, and it has alpha values, it has this page for controlling blending, and there is Z buffer, which is distance essentially to say where the distance of textures are on z axis from camera.

    thanks