Advertisement
Guest User

BasicWireframeBIRP.shader

a guest
Apr 29th, 2024
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
  2.  
  3. Shader "Wireframe/Basic"
  4. {
  5. Properties
  6. {
  7. _WireThickness ("Wire Thickness", RANGE(0, 800)) = 100
  8. _WireColor ("Wire Color", Color) = (0,1,1,1)
  9. }
  10.  
  11. SubShader
  12. {
  13. // Each color represents a meter.
  14.  
  15. Tags { "RenderType"="Opaque" }
  16.  
  17. Pass
  18. {
  19. // Wireframe shader based on the the following
  20. // http://developer.download.nvidia.com/SDK/10/direct3d/Source/SolidWireframe/Doc/SolidWireframe.pdf
  21.  
  22. CGPROGRAM
  23. #pragma vertex vert
  24. #pragma geometry geom
  25. #pragma fragment frag
  26.  
  27. #include "UnityCG.cginc"
  28.  
  29. float _WireThickness;
  30. float4 _WireColor;
  31.  
  32. struct appdata
  33. {
  34. float4 vertex : POSITION;
  35. UNITY_VERTEX_INPUT_INSTANCE_ID
  36. };
  37.  
  38. struct v2g
  39. {
  40. float4 projectionSpaceVertex : SV_POSITION;
  41. UNITY_VERTEX_OUTPUT_STEREO_EYE_INDEX
  42. };
  43.  
  44. struct g2f
  45. {
  46. float4 projectionSpaceVertex : SV_POSITION;
  47. float4 dist : TEXCOORD1;
  48. UNITY_VERTEX_OUTPUT_STEREO
  49. };
  50.  
  51. v2g vert (appdata v)
  52. {
  53. v2g o;
  54. UNITY_SETUP_INSTANCE_ID(v);
  55. UNITY_INITIALIZE_OUTPUT_STEREO_EYE_INDEX(o);
  56.  
  57. o.projectionSpaceVertex = UnityObjectToClipPos(v.vertex);
  58. return o;
  59. }
  60.  
  61. [maxvertexcount(3)]
  62. void geom(triangle v2g i[3], inout TriangleStream<g2f> triangleStream)
  63. {
  64. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i[0]);
  65.  
  66. float2 p0 = i[0].projectionSpaceVertex.xy / i[0].projectionSpaceVertex.w;
  67. float2 p1 = i[1].projectionSpaceVertex.xy / i[1].projectionSpaceVertex.w;
  68. float2 p2 = i[2].projectionSpaceVertex.xy / i[2].projectionSpaceVertex.w;
  69.  
  70. float2 edge0 = p2 - p1;
  71. float2 edge1 = p2 - p0;
  72. float2 edge2 = p1 - p0;
  73.  
  74. // To find the distance to the opposite edge, we take the
  75. // formula for finding the area of a triangle Area = Base/2 * Height,
  76. // and solve for the Height = (Area * 2)/Base.
  77. // We can get the area of a triangle by taking its cross product
  78. // divided by 2. However we can avoid dividing our area/base by 2
  79. // since our cross product will already be double our area.
  80. float area = abs(edge1.x * edge2.y - edge1.y * edge2.x);
  81. float wireThickness = 800 - _WireThickness;
  82.  
  83. g2f o;
  84. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  85. o.projectionSpaceVertex = i[0].projectionSpaceVertex;
  86. o.dist.xyz = float3( (area / length(edge0)), 0.0, 0.0) * o.projectionSpaceVertex.w * wireThickness;
  87. o.dist.w = 1.0 / o.projectionSpaceVertex.w;
  88. triangleStream.Append(o);
  89.  
  90.  
  91. o.projectionSpaceVertex = i[1].projectionSpaceVertex;
  92. o.dist.xyz = float3(0.0, (area / length(edge1)), 0.0) * o.projectionSpaceVertex.w * wireThickness;
  93. o.dist.w = 1.0 / o.projectionSpaceVertex.w;
  94. triangleStream.Append(o);
  95.  
  96.  
  97. o.projectionSpaceVertex = i[2].projectionSpaceVertex;
  98. o.dist.xyz = float3(0.0, 0.0, (area / length(edge2))) * o.projectionSpaceVertex.w * wireThickness;
  99. o.dist.w = 1.0 / o.projectionSpaceVertex.w;
  100. triangleStream.Append(o);
  101. }
  102.  
  103. fixed4 frag (g2f i) : SV_Target
  104. {
  105. float minDistanceToEdge = min(i.dist[0], min(i.dist[1], i.dist[2])) * i.dist[3];
  106.  
  107. // Early out if we know we are not on a line segment.
  108. if(minDistanceToEdge > 0.9)
  109. {
  110. return fixed4(0,0,0,0);
  111. }
  112. return _WireColor;
  113. }
  114. ENDCG
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement