Advertisement
Guest User

BasicWireframeURP.shader

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