Search Unity

DrawMeshInstanceIndirect with transparency

Discussion in 'Shaders' started by datscharf, Jul 30, 2020.

  1. datscharf

    datscharf

    Joined:
    Jul 26, 2020
    Posts:
    2
    Hi all,

    Just a beginners question before I bang my head at the wall. I have spent the better part of the last 4 days trying to get transparency working with Graphics.DrawMeshInstanceIndirect.

    My problem is that I want overlay a lot of quads with semi transparent textures on top of another layer of quads. Like 2d images of chess pieces on top a chess board. I do each 'tilemap' in separate calls to DrawMeshInstanceIndirect so I am guessing that Z ordering doesn't matter as the GPU doesn't need to sort the meshes in each call as they don't overlap.

    I have a very basic understanding of shaders, how they work and how to interact with them from my scripts. So I have resorted mostly to google and trying to find examples of how its done or if its even possible.

    I now have a working example but it doesn't look very good. As I understand it I need to have ZWrite on and then use the clip() function. But this either lets the background color bleed through in the areas that are semitransparent or makes very jagged edges on my overlay textures.

    Any advice?

    Best regards
    Martin B. Andersen

    Heres my resulting shader with "clip(col.a - 240.0 / 255.0);" which doesn't bleed too much but has some roughness around the edges.:

    Code (CSharp):
    1.   Shader "Custom/InstancedShader" {
    2.           Properties {
    3.         _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
    4.     }
    5.         SubShader {
    6.  
    7.         Tags{
    8.             "Queue"="Transparent"
    9.             "IgnoreProjector"="True"
    10.             "RenderType"="Transparent"
    11.         }
    12.         Lighting Off
    13.         ZWrite on
    14.         Blend One OneMinusSrcAlpha
    15.  
    16.         pass {
    17.             ZWrite On
    18.            
    19.         }
    20.  
    21.         Pass {
    22.             CGPROGRAM
    23.             #pragma vertex vert
    24.             #pragma fragment frag
    25.            
    26.             #include "UnityCG.cginc"
    27.            
    28.             struct appdata_t {
    29.                 float4 vertex   : POSITION;
    30.                 float4 color    : COLOR;
    31.                 float2 uv    : TEXCOORD0;
    32.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    33.             };
    34.  
    35.             struct v2f {
    36.                 float4 vertex   : SV_POSITION;
    37.                 fixed4 color    : COLOR;
    38.                 float2 uv    : TEXCOORD0;
    39.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    40.             };
    41.  
    42.             struct MeshProperties {
    43.                 float4x4 mat;
    44.                 float4 color;
    45.             };
    46.  
    47.             StructuredBuffer<MeshProperties> _Properties;
    48.  
    49.             v2f vert(appdata_t i, uint instanceID: SV_InstanceID) {
    50.                 v2f o;
    51.  
    52.                 float4 pos = mul(_Properties[instanceID].mat, i.vertex);
    53.                 o.vertex = UnityObjectToClipPos(pos);
    54.                 o.color = _Properties[instanceID].color;
    55.                 o.uv = i.uv;
    56.  
    57.                 return o;
    58.             }
    59.  
    60.             sampler2D _MainTex;
    61.            
    62.             float4 frag(v2f i) : SV_Target {
    63.                 //return tex2D(_MainTex, i.uv) * i.color;
    64.                 fixed4 col = tex2D(_MainTex, i.uv) * i.color;
    65.                 clip(col.a - 240.0 / 255.0);
    66.                 col.rgb *= col.a;
    67.                 return col;
    68.             }
    69.  
    70.            
    71.             ENDCG
    72.         }
    73.     }
    74. }