Shader Learning_Geometry Shader

Shader "Unlit/SimpleGeometryShader"
{
    
    
    Properties
    {
    
    
        _MainTex ("Texture", 2D) = "white" {
    
    }
    }
    SubShader
    {
    
    
        Tags {
    
     "RenderType"="Opaque" }
        LOD 100

        Pass
        {
    
    
            CGPROGRAM
            #pragma vertex vert
            #pragma geometry geom
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
    
    
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2g
            {
    
    
                float vertex: SV_POSITION;
                float uv : TEXCOORD0;
            };

            struct g2f
            {
    
    
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2g vert (appdata v)
            {
    
    
                v2g o;
                o.vertex = v.vertex;
                o.uv = v.uv;
                return o;
            }

            [maxvertexcount(3)]
            void geom(triangle v2g IN[3], inout TriangleStream<g2f> triStream)
            {
    
    
                g2f o;
                for(int i = 0;i < 3; i++)
                {
    
    
                    o.vertex = UnityObjectToClipPos(IN[i].vertex);
                    UNITY_TRANSFER_FOG(o, o.vertex);
                    o.uv = TRANSFORM_TEX(IN[i].uv, _MainTex);
                    triStream.Append(o);
                }
                triStream.RestartStrip();
            }

            fixed4 frag (g2f i) : SV_Target
            {
    
    
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}

Get familiar with geometry shaders:

  1. Similar to vertex and fragment shaders, you need to define the function #pragma geometry geom

  2. [maxvertexcount(3)] must be added above the function body: indicates the maximum number of vertices that can be increased

  3. triangle v2g IN[3] function input parameter types are point, line, triangle, lineadj, triangleadj five;
    insert image description here

  4. inout TriangleStream triStream function output parameter
    PointStream: output a series of point primitives
    LineStream: output a series of line primitives
    TriangleStream: output a series of triangular surface primitives

  5. Every time a vertex is output, it is necessary to output stream.Append(o); once.

  6. After outputting enough vertices, also triStream.RestartStrip(); once.

Guess you like

Origin blog.csdn.net/suixinger_lmh/article/details/125100185