GeometryWirePlane.shader 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. ///
  3. /// Basic wireframe shader with alpha color that can be used for rendering planes.
  4. /// This will draw a mesh along with 0.5 transparency inside the vertices
  5. ///
  6. Shader "Custom/Plane Detection/ GeometryWirePlane"
  7. {
  8. Properties
  9. {
  10. _WireColor("Wire color", Color) = (1.0, 1.0, 1.0, 1.0)
  11. _WireThickness("Wire thickness", Range(0, 800)) = 100
  12. }
  13. SubShader
  14. {
  15. Tags{ "RenderType" = "Opaque" }
  16. Pass
  17. {
  18. Blend SrcAlpha OneMinusSrcAlpha
  19. ZTest Always
  20. CGPROGRAM
  21. #pragma vertex vert
  22. #pragma geometry geom
  23. #pragma fragment frag
  24. #pragma target 5.0
  25. #pragma only_renderers d3d11
  26. #include "UnityCG.cginc"
  27. float4 _WireColor;
  28. float _WireThickness;
  29. // Based on approach described in Shader-Based Wireframe Drawing (2008)
  30. // http://orbit.dtu.dk/en/publications/id(13e2122d-bec7-48de-beca-03ce6ea1c3f1).html
  31. struct v2g
  32. {
  33. float4 viewPos : SV_POSITION;
  34. UNITY_VERTEX_OUTPUT_STEREO
  35. };
  36. v2g vert(appdata_base v)
  37. {
  38. UNITY_SETUP_INSTANCE_ID(v);
  39. v2g o;
  40. o.viewPos = UnityObjectToClipPos(v.vertex);
  41. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  42. return o;
  43. }
  44. // inverseW is to counteract the effect of perspective-correct interpolation so that the lines
  45. // look the same thickness regardless of their depth in the scene.
  46. struct g2f
  47. {
  48. float4 viewPos : SV_POSITION;
  49. float inverseW : TEXCOORD0;
  50. float3 dist : TEXCOORD1;
  51. UNITY_VERTEX_OUTPUT_STEREO
  52. };
  53. [maxvertexcount(3)]
  54. void geom(triangle v2g i[3], inout TriangleStream<g2f> triStream)
  55. {
  56. // Calculate the vectors that define the triangle from the input points.
  57. float2 point0 = i[0].viewPos.xy / i[0].viewPos.w;
  58. float2 point1 = i[1].viewPos.xy / i[1].viewPos.w;
  59. float2 point2 = i[2].viewPos.xy / i[2].viewPos.w;
  60. // Calculate the area of the triangle.
  61. float2 vector0 = point2 - point1;
  62. float2 vector1 = point2 - point0;
  63. float2 vector2 = point1 - point0;
  64. float area = abs(vector1.x * vector2.y - vector1.y * vector2.x);
  65. float3 distScale[3];
  66. distScale[0] = float3(area / length(vector0), 0, 0);
  67. distScale[1] = float3(0, area / length(vector1), 0);
  68. distScale[2] = float3(0, 0, area / length(vector2));
  69. float wireScale = 800 - _WireThickness;
  70. // Output each original vertex with its distance to the opposing line defined
  71. // by the other two vertices.
  72. g2f o;
  73. [unroll]
  74. for (uint idx = 0; idx < 3; ++idx)
  75. {
  76. o.viewPos = i[idx].viewPos;
  77. o.inverseW = 1.0 / o.viewPos.w;
  78. o.dist = distScale[idx] * o.viewPos.w * wireScale;
  79. UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(i[idx], o);
  80. triStream.Append(o);
  81. }
  82. }
  83. void frag(g2f i, out half4 outColor : SV_Target)
  84. {
  85. // Calculate minimum distance to one of the triangle lines, making sure to correct
  86. // for perspective-correct interpolation.
  87. float dist = min(i.dist[0], min(i.dist[1], i.dist[2])) * i.inverseW;
  88. // Make the intensity of the line very bright along the triangle edges but fall-off very
  89. // quickly to go back to half transparency
  90. float I = exp2(-2 * dist * dist / 10)+ 0.2;
  91. // Fade out the alpha but not the color so we don't get any weird halo effects from
  92. // a fade to a different color.
  93. float4 color = _WireColor;
  94. color.a = I;
  95. outColor = color;
  96. }
  97. ENDCG
  98. }
  99. }
  100. FallBack "Diffuse"
  101. }