Advertisement
Guest User

WireframeDissolveURP

a guest
Apr 29th, 2024
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 KB | None | 0 0
  1. Shader "Universal Render Pipeline/Wireframe/DisplacementFade"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Main Texture", 2D) = "white" { }
  6. _WireThickness ("Wire Thickness", RANGE(0, 800)) = 100
  7. [HDR] _WireColor ("Wire Color", Color) = (0,1,1,1)
  8. [Toggle(INVERT)] _INVERT("Invert", Float) = 1
  9. _MovingSlider ("Moving Slider", RANGE(-10, 10)) = 10
  10. _Extrude("Extrude Amount", RANGE(-10, 10)) = 10
  11. _WireFrameStay ("Wire Stay", RANGE(-1, 1)) = 0
  12.  
  13. }
  14. SubShader
  15. {
  16. Tags {"RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
  17. LOD 100
  18.  
  19. Pass
  20. {
  21. // Wireframe shader based on the the following
  22. // http://developer.download.nvidia.com/SDK/10/direct3d/Source/SolidWireframe/Doc/SolidWireframe.pdf
  23.  
  24. HLSLPROGRAM
  25. #pragma require geometry
  26.  
  27. #pragma vertex vert
  28. #pragma geometry geom
  29. #pragma fragment frag
  30. #pragma shader_feature INVERT
  31. // -------------------------------------
  32. // Unity defined keywords
  33. #pragma multi_compile_instancing
  34.  
  35. #include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitInput.hlsl"
  36. // #include "UnlitInput.hlsl"
  37.  
  38. float _WireThickness;
  39. float4 _WireColor;
  40. sampler2D _MainTex;
  41. float _MovingSlider;
  42. float _Extrude;
  43. float _WireFrameStay;
  44.  
  45. struct Attributes
  46. {
  47. float4 positionOS : POSITION;
  48. UNITY_VERTEX_INPUT_INSTANCE_ID
  49. float4 uv : TEXCOORD0;
  50. float3 normal :NORMAL;
  51. };
  52.  
  53. struct v2g
  54. {
  55. float4 projectionSpaceVertex : SV_POSITION;
  56. float4 uv :TEXCOORD0;
  57. float movingPos : TEXCOORD1;
  58. UNITY_VERTEX_OUTPUT_STEREO
  59. };
  60.  
  61. v2g vert(Attributes input)
  62. {
  63. v2g output = (v2g)0;
  64.  
  65. UNITY_SETUP_INSTANCE_ID(input);
  66. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  67.  
  68. // move over the mesh y axis using a slider
  69. // with option to invert
  70. #if INVERT
  71. float movingPos = input.positionOS.y + _MovingSlider;
  72. #else
  73. float movingPos = 1- input.positionOS.y + _MovingSlider;
  74. #endif
  75. input.positionOS.xyz -= saturate(1-movingPos) * input.normal * _Extrude;
  76. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  77. output.projectionSpaceVertex = vertexInput.positionCS;
  78. output.uv = input.uv;
  79. output.movingPos = movingPos;
  80. return output;
  81. }
  82.  
  83. struct g2f
  84. {
  85. float4 projectionSpaceVertex : SV_POSITION;
  86. float4 dist : TEXCOORD1;
  87. float4 uv : TEXCOORD0;
  88. float movingPos : TEXCOORD2;
  89. UNITY_VERTEX_OUTPUT_STEREO
  90. };
  91.  
  92. [maxvertexcount(3)]
  93. void geom(triangle v2g i[3], inout TriangleStream<g2f> triangleStream)
  94. {
  95. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i[0]);
  96.  
  97. float2 p0 = i[0].projectionSpaceVertex.xy / i[0].projectionSpaceVertex.w;
  98. float2 p1 = i[1].projectionSpaceVertex.xy / i[1].projectionSpaceVertex.w;
  99. float2 p2 = i[2].projectionSpaceVertex.xy / i[2].projectionSpaceVertex.w;
  100.  
  101. float2 edge0 = p2 - p1;
  102. float2 edge1 = p2 - p0;
  103. float2 edge2 = p1 - p0;
  104.  
  105. // To find the distance to the opposite edge, we take the
  106. // formula for finding the area of a triangle Area = Base/2 * Height,
  107. // and solve for the Height = (Area * 2)/Base.
  108. // We can get the area of a triangle by taking its cross product
  109. // divided by 2. However we can avoid dividing our area/base by 2
  110. // since our cross product will already be double our area.
  111. float area = abs(edge1.x * edge2.y - edge1.y * edge2.x);
  112. float wireThickness = 800 - _WireThickness;
  113.  
  114. g2f o;
  115. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  116. o.projectionSpaceVertex = i[0].projectionSpaceVertex;
  117. o.dist.xyz = float3( (area / length(edge0)), 0.0, 0.0) * o.projectionSpaceVertex.w * wireThickness;
  118. o.dist.w = 1.0 / o.projectionSpaceVertex.w;
  119. o.uv = i[0].uv;
  120. o.movingPos = i[0].movingPos;
  121. triangleStream.Append(o);
  122.  
  123. o.projectionSpaceVertex = i[1].projectionSpaceVertex;
  124. o.dist.xyz = float3(0.0, (area / length(edge1)), 0.0) * o.projectionSpaceVertex.w * wireThickness;
  125. o.dist.w = 1.0 / o.projectionSpaceVertex.w;
  126. o.uv = i[1].uv;
  127. o.movingPos = i[1].movingPos;
  128. triangleStream.Append(o);
  129.  
  130. o.projectionSpaceVertex = i[2].projectionSpaceVertex;
  131. o.dist.xyz = float3(0.0, 0.0, (area / length(edge2))) * o.projectionSpaceVertex.w * wireThickness;
  132. o.dist.w = 1.0 / o.projectionSpaceVertex.w;
  133. o.uv = i[2].uv;
  134. o.movingPos = i[2].movingPos;
  135. triangleStream.Append(o);
  136. }
  137.  
  138. half4 frag(g2f i) : SV_Target
  139. {
  140. float minDistanceToEdge = min(i.dist[0], min(i.dist[1], i.dist[2])) * i.dist[3];
  141. float4 mainTex = tex2D(_MainTex, i.uv.xy);
  142.  
  143. // show texture where there is no wireframe
  144. // also fades the wireframe over the moving position
  145. if(minDistanceToEdge > 0.9* 1- saturate(i.movingPos - _WireFrameStay))
  146. {
  147. // discard pixels where there is no wireframe earlier
  148. clip(i.movingPos - 0.2);
  149. return mainTex;
  150. }
  151. // discard pixels over the moving position
  152. clip(i.movingPos);
  153. return _WireColor;
  154. }
  155. ENDHLSL
  156. }
  157. }
  158.  
  159. FallBack "Hidden/Universal Render Pipeline/FallbackError"
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement