unity 线框效果

// modified version of "VR/SpatialMapping/Wireframe.shader" from Unity 5.5f2
// added colors, discard option, removed stereo support and color by distance

Shader "UnityLibrary/Effects/Wireframe"
{
	Properties
	{
		_LineColor ("LineColor", Color) = (1,1,1,1)
		_FillColor ("FillColor", Color) = (0,0,0,0)
		_WireThickness ("Wire Thickness", RANGE(0, 800)) = 100
		[MaterialToggle] UseDiscard("Discard Fill", Float) = 1
 }

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


		Pass
		{
			// Wireframe shader based on the the following
			// http://developer.download.nvidia.com/SDK/10/direct3d/Source/SolidWireframe/Doc/SolidWireframe.pdf

			CGPROGRAM
			#pragma vertex vert
			#pragma geometry geom
			#pragma fragment frag
			#pragma multi_compile _ USEDISCARD_ON
			#include "UnityCG.cginc"

			float _WireThickness;

			struct appdata
			{
				float4 vertex : POSITION;
			};

			struct v2g
			{
				float4 projectionSpaceVertex : SV_POSITION;
				float4 worldSpacePosition : TEXCOORD1;
			};

			struct g2f
			{
				float4 projectionSpaceVertex : SV_POSITION;
				float4 worldSpacePosition : TEXCOORD0;
				float4 dist : TEXCOORD1;
			};

			
			v2g vert (appdata v)
			{
				v2g o;
//				UNITY_SETUP_INSTANCE_ID(v);
//				UNITY_INITIALIZE_OUTPUT(v2g, o);
				o.projectionSpaceVertex = UnityObjectToClipPos(v.vertex);
				o.worldSpacePosition = mul(unity_ObjectToWorld, v.vertex);
				return o;
			}
			
			[maxvertexcount(3)]
			void geom(triangle v2g i[3], inout TriangleStream<g2f> triangleStream)
			{
				float2 p0 = i[0].projectionSpaceVertex.xy / i[0].projectionSpaceVertex.w;
				float2 p1 = i[1].projectionSpaceVertex.xy / i[1].projectionSpaceVertex.w;
				float2 p2 = i[2].projectionSpaceVertex.xy / i[2].projectionSpaceVertex.w;

				float2 edge0 = p2 - p1;
				float2 edge1 = p2 - p0;
				float2 edge2 = p1 - p0;

				// To find the distance to the opposite edge, we take the
				// formula for finding the area of a triangle Area = Base/2 * Height, 
				// and solve for the Height = (Area * 2)/Base.
				// We can get the area of a triangle by taking its cross product
				// divided by 2.  However we can avoid dividing our area/base by 2
				// since our cross product will already be double our area.
				float area = abs(edge1.x * edge2.y - edge1.y * edge2.x);
				float wireThickness = 800 - _WireThickness;

				g2f o;
				o.worldSpacePosition = i[0].worldSpacePosition;
				o.projectionSpaceVertex = i[0].projectionSpaceVertex;
				o.dist.xyz = float3( (area / length(edge0)), 0.0, 0.0) * o.projectionSpaceVertex.w * wireThickness;
				o.dist.w = 1.0 / o.projectionSpaceVertex.w;
				triangleStream.Append(o);

				o.worldSpacePosition = i[1].worldSpacePosition;
				o.projectionSpaceVertex = i[1].projectionSpaceVertex;
				o.dist.xyz = float3(0.0, (area / length(edge1)), 0.0) * o.projectionSpaceVertex.w * wireThickness;
				o.dist.w = 1.0 / o.projectionSpaceVertex.w;
				triangleStream.Append(o);

				o.worldSpacePosition = i[2].worldSpacePosition;
				o.projectionSpaceVertex = i[2].projectionSpaceVertex;
				o.dist.xyz = float3(0.0, 0.0, (area / length(edge2))) * o.projectionSpaceVertex.w * wireThickness;
				o.dist.w = 1.0 / o.projectionSpaceVertex.w;
				triangleStream.Append(o);
			}

			uniform fixed4 _LineColor;
			uniform fixed4 _FillColor;

			fixed4 frag (g2f i) : SV_Target
			{
				float minDistanceToEdge = min(i.dist[0], min(i.dist[1], i.dist[2])) * i.dist[3];

				// Early out if we know we are not on a line segment.
				if(minDistanceToEdge > 0.9)
				{
				    #ifdef USEDISCARD_ON
					discard;
					#else
					return _FillColor;
					#endif
				}

				return _LineColor;
			}
			ENDCG
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity 线框材质是一种在Unity游戏引擎中使用的材质类型,它可以添加到3D模型上以实现边缘线框的效果。使用线框材质可以使得模型的边缘显得更加鲜明,帮助玩家更好地辨别物体的形状和轮廓。 在Unity中,要创建一个线框材质非常简单。首先,我们需要创建一个新的材质,并将其属性设置为线框。然后,可以对线框的颜色、宽度和样式进行调整。可以选择的样式包括实线、点线、虚线等,根据具体需要进行设置。最后,将这个材质应用于我们想要展示线效果的模型上即可。 线框材质在游戏开发中有诸多应用。它可以用来突出显示关键物体,帮助玩家更好地识别游戏中的目标或障碍物。例如,在一款射击游戏中,可以使用线框材质将敌人或任务物品的轮廓标记出来,使其更加显眼。线框材质还可以用于场景探索和建模调试,帮助开发者更好地了解模型的结构和细节。 除了游戏开发,线框材质在建筑设计、工业设计等领域也有广泛应用。例如,在建筑设计中,线框材质可以用来展示建筑物的结构、空间布局等。设计师可以通过添加线框材质,更清晰地显示出建筑物的各个部分,并进行进一步修改和调整。 总之,Unity线框材质提供了一种简便的方式来实现模型边缘的线效果。在游戏开发和其他设计领域中,使用线框材质可以提高物体的可见性,使其更加突出,增强用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值