Search Unity

How would I mask this geometry shader with a texture?

Discussion in 'Shaders' started by keenanwoodall, Aug 15, 2017.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    567
    This shader makes little triangle (billboards) on each vertex of a mesh.
    upload_2017-8-15_15-8-20.png

    I want to have an additional texture that masks where the points are. For example, I could put this shader on a sphere and supply a checkerboard texture. Then there would be a checkerboard pattern of showing/hiding the billboards. Or I could use a height map of the Earth and points would only be rendered where the texture is bright enough.

    I tried figuring it out, but I've never used a geometry shader before (and didn't write this one) I couldn't figure out how to same a texture from the uv coordinates of the original mesh. Instead it would sample the new texture from the uvs of the new mesh. It seems like I've got the right idea but I would love a hint to send me in the right direction.
    Code (csharp):
    1.  
    2. Shader "Unlit/PointCloud"
    3. {
    4.     Properties
    5.     {
    6.         _MainTex ("Texture (RGB)", 2D) = "white" {}
    7.         _Size ("Size", Float) = 0.1
    8.     }
    9.     SubShader
    10.     {
    11.         Tags { "Queue"="AlphaTest" "RenderType"="Transparent" "IgnoreProjector"="True" }
    12.         Blend One OneMinusSrcAlpha
    13.         AlphaToMask On
    14.         Cull Off
    15.  
    16.         Pass
    17.         {
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma geometry geom
    21.             #pragma fragment frag
    22.        
    23.             #include "UnityCG.cginc"
    24.  
    25.             sampler2D _MainTex;
    26.             float _Size;
    27.  
    28.             struct GS_INPUT
    29.             {
    30.                 float4 vertex : POSITION;
    31.                 float3 normal    : NORMAL;
    32.                 float4 color    : COLOR;
    33.                 float2 texcoord : TEXCOORD0;
    34.                 float2 texcoord1 : TEXCOORD1;
    35.             };
    36.  
    37.             struct FS_INPUT {
    38.                 float4 vertex : SV_POSITION;
    39.                 float3 normal : NORMAL;
    40.                 float4 color : COLOR;
    41.                 float2 texcoord : TEXCOORD0;
    42.             };
    43.  
    44.             GS_INPUT vert (appdata_full v)
    45.             {
    46.                 GS_INPUT o = (GS_INPUT)0;
    47.                 o.vertex = v.vertex;
    48.                 o.normal = v.normal;
    49.                 o.color = v.color;
    50.                 return o;
    51.             }
    52.  
    53.  
    54.             [maxvertexcount(3)]
    55.             void geom (point GS_INPUT tri[1], inout TriangleStream<FS_INPUT> triStream)
    56.             {
    57.                 FS_INPUT pIn = (FS_INPUT)0;
    58.                 pIn.normal = mul(unity_ObjectToWorld, tri[0].normal);
    59.                 pIn.color = tri[0].color;
    60.  
    61.                 float4 vertex = mul(unity_ObjectToWorld, tri[0].vertex);
    62.                 float3 tangent = normalize(cross(float3(0,1,0), pIn.normal));
    63.                 float3 up = normalize(cross(tangent, pIn.normal));
    64.  
    65.                 pIn.vertex = mul(UNITY_MATRIX_VP, vertex + float4(tangent * -_Size / 1.5, 0));
    66.                 pIn.texcoord = float2(-0.5,0);
    67.                 triStream.Append(pIn);
    68.  
    69.                 pIn.vertex = mul(UNITY_MATRIX_VP, vertex + float4(up * _Size, 0));
    70.                 pIn.texcoord = float2(0.5,1.5);
    71.                 triStream.Append(pIn);
    72.  
    73.                 pIn.vertex = mul(UNITY_MATRIX_VP, vertex + float4(tangent * _Size / 1.5, 0));
    74.                 pIn.texcoord = float2(1.5,0);
    75.                 triStream.Append(pIn);
    76.             }
    77.  
    78.             float4 frag (FS_INPUT i) : COLOR
    79.             {
    80.                 float4 color = i.color;
    81.                 color.a = step(0.5, tex2D(_MainTex, i.texcoord).a);
    82.                 return color;
    83.             }
    84.             ENDCG
    85.         }
    86.     }
    87. }
     
    Last edited: Aug 15, 2017
  2. Branxord

    Branxord

    Joined:
    May 13, 2018
    Posts:
    14

    Any solution for this??? i'm looking to do the same thing!