Advertisement
Guest User

WireframeDissolveBIRP

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