GeometryWireframe.shader 3.2 KB

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