Efecto simple del sombreador de geometría de Unity

1. Similar al cabello
inserte la descripción de la imagen aquí

Shader "Unlit/HandGemotroy"
{
    
    
	Properties
	{
    
    
		 _MainTex("Texture",2D) = "white"{
    
    }
		 _Length("Length",float) = 1.0
		 _Color("Color",Color) = (1,1,1,1)
	}

	 SubShader
	 {
    
    
		 Tags{
    
    "RenderType" = "Opaque"}
		 LOD 100

		 Pass
		 {
    
    
			 CGPROGRAM

			 #pragma vertex vert
			 #pragma fragment frag
			 #pragma geometry geom
			 #include "UnityCG.cginc"

			 float _Length;
			 sampler2D _MainTex;
			 float4 _MainTex_ST;
			 float4 _Color;

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

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

			 struct g2f
			 {
    
    
				 float4 vertex:SV_POSITION;
				 float2 uv:TEXCOORD0;
				 float4 col:COLOR;
			 };

			 v2g vert(a2v v)
			 {
    
    
				 v2g o;
				 o.vertex = v.vertex;
				 o.uv = TRANSFORM_TEX(v.uv,_MainTex);
				 return o;
			 }

			 [maxvertexcount(9)]
			 void geom(triangle v2g IN[3],inout TriangleStream<g2f> tristream)
			 {
    
    
				 g2f o;

				 //法线
				 float3 edgeA = IN[1].vertex - IN[0].vertex;
				 float3 edgeB = IN[2].vertex - IN[0].vertex;
				 float3 normalFace = normalize(cross(edgeA, edgeB));

				 //中心位置
				 float3 centerPos = (IN[0].vertex + IN[1].vertex + IN[2].vertex) / 3;

				 //中心点UV
				 float2 centerUV = (IN[0].uv + IN[1].uv + IN[2].uv) / 3;

				 //外拓的顶点距离
				 centerPos += float4(normalFace, 0) * _Length;

				 for (uint i = 0; i < 3; i++)
				 {
    
    
					 o.vertex = UnityObjectToClipPos(IN[i].vertex);
					 o.uv = IN[i].uv;
					 o.col = fixed4(0.0,0.0,0.0,1.0);

					 //添加顶点
					 tristream.Append(o);

					 uint index = (i + 1) % 3;
					 o.vertex = UnityObjectToClipPos(IN[index].vertex);
					 o.uv = IN[index].uv;
					 o.col = fixed4(0,0,0,1);
					 tristream.Append(o);

					 //外部颜色
					 o.vertex = UnityObjectToClipPos(float4(centerPos,1));
					 o.uv = centerUV;
					 o.col = fixed4(1,1,1,1);

					 tristream.Append(o);

					 //添加三角面
					 tristream.RestartStrip();
				 }
			 }

			 fixed4 frag(g2f f) :SV_Target
			 {
    
    
				 return  tex2D(_MainTex,f.uv) * f.col * _Color;
			 }


			 ENDCG
		 }
	 }
}

2. efecto de punto

inserte la descripción de la imagen aquí

Shader "Unlit/HandGemotroyPoint"
{
    
    
	Properties
	{
    
    
		_MainTex("Texture",2D) = "white"{
    
    }
		_Size("Size",Range(0.0,0.1)) = 0.0
		[HDR]_Color("Color",Color) = (1,1,1,1)
	}
	SubShader
	{
    
    
		Tags{
    
    "RenderType" = "Opaque"}
		LOD 100

		Pass
		{
    
    
			CGPROGRAM

			#pragma vertex vert
			#pragma fragment frag
			#pragma geometry geom
			#include "UnityCG.cginc"

			sampler2D _MainTex;
			float4 _MainTex_ST;
			fixed _Size;
			fixed4 _Color;

			struct a2v
			{
    
    
				float4 vertex:POSITION;
				float2 texcoord:TEXCOORD0;
				float3 normal:NORMAL;
			};

			struct v2g
			{
    
    
				float4 vertex:POSITION;
				float2 texcoord:TEXCOORD0;
			};

			struct g2f
			{
    
    
				float4 pos:SV_POSITION;
				float2 uv:TEXCOORD0;
			};

			v2g vert(a2v v)
			{
    
    
				v2g o;
				o.vertex = v.vertex;
				o.texcoord = v.texcoord;
				return o;
			}

			[maxvertexcount(10)]
			void geom(triangle v2g IN[3],inout PointStream<g2f> pointStream)
			{
    
    
				g2f o;

				//法线
				float3 edge1 = IN[1].vertex - IN[0].vertex;
				float3 edge2 = IN[2].vertex - IN[0].vertex;
				float3 normalDir = normalize(cross(edge1, edge2));

				float3 centerPos = (IN[0].vertex + IN[1].vertex + IN[2].vertex) / 3;
				centerPos += normalDir * _Size;

				for (uint i = 0; i < 3; i++)
				{
    
    
					o.pos = UnityObjectToClipPos(IN[i].vertex);
					o.uv = IN[i].texcoord;
					pointStream.Append(o);
				}

				o.pos = UnityObjectToClipPos(centerPos);
				o.uv = (IN[0].texcoord + IN[1].texcoord + IN[2].texcoord) / 3;
				pointStream.Append(o);
			}

			fixed4 frag(g2f i):SV_Target
			{
    
    
				return tex2D(_MainTex,i.uv) * _Color;
			}

			ENDCG
		}
	}
}

Supongo que te gusta

Origin blog.csdn.net/weixin_41155760/article/details/128011376
Recomendado
Clasificación