TopBottomBBox.shader 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. Shader "Unlit/TopBottomBBox"
  2. {
  3. Properties
  4. {
  5. _Color("Color", Color) = (1,0,0,1)
  6. _SectionHeight("Section Height (World)", float) = 0.3
  7. _XScale("X Scale", float) = 1
  8. _YScale("Y Scale", float) = 1
  9. _ZScale("Z Scale", float) = 1
  10. _FloorHeight("Floor Height", float) = 0
  11. _EdgeThickness("Edge Thickness", float) = 0.05
  12. }
  13. SubShader
  14. {
  15. Tags { "RenderType"="Transparent" "RenderQueue"="Transparent+5" }
  16. Blend SrcAlpha OneMinusSrcAlpha
  17. //Cull off
  18. //ZTest Always
  19. ZWrite off
  20. Pass
  21. {
  22. Cull off
  23. CGPROGRAM
  24. #pragma vertex vert
  25. #pragma fragment frag
  26. // make fog work
  27. #pragma multi_compile_fog
  28. #include "UnityCG.cginc"
  29. struct appdata
  30. {
  31. float4 vertex : POSITION;
  32. float2 uv : TEXCOORD0;
  33. float3 normal : NORMAL;
  34. };
  35. struct v2f
  36. {
  37. float4 reluv : TEXCOORD0;
  38. float4 vertex : SV_POSITION;
  39. float height : TEXCOORD1;
  40. };
  41. float4 _Color;
  42. float _SectionHeight;
  43. float _XScale;
  44. float _YScale;
  45. float _ZScale;
  46. float _FloorHeight;
  47. v2f vert (appdata v)
  48. {
  49. v2f o;
  50. o.vertex = UnityObjectToClipPos(v.vertex);
  51. //Get UVs relative to their face's scale.
  52. //o.uv = v.uv;
  53. float3 normal = UnityObjectToWorldNormal(v.normal);
  54. //float3 normal = float3(0, 0, 1);
  55. float horizdot = abs(dot(float3(1, 0, 0), normal));
  56. float horizscale = horizdot * _ZScale + (1 - horizdot) * _XScale;
  57. o.reluv.x = v.uv.x * horizscale;
  58. float vertdot = dot(float3(0, 1, 0), normal);
  59. float vertscale = vertdot * _ZScale + (1 - vertdot) * _YScale;
  60. o.reluv.y = v.uv.y * vertscale;
  61. //o.uv = v.uv;
  62. o.reluv.z = horizscale;
  63. o.reluv.w = vertscale;
  64. //Find how the section height fits into the box height.
  65. float realheightfrombot = mul(unity_ObjectToWorld, v.vertex).y - _FloorHeight;
  66. o.height = realheightfrombot;
  67. return o;
  68. }
  69. float _EdgeThickness;
  70. fixed4 frag (v2f i) : SV_Target
  71. {
  72. if (i.height == 0) discard; //Removes the bottom.
  73. //Calculate fade based on distance from top and bottoms, which we computed in the vertex shader.
  74. float alphaaddbottom = clamp(_SectionHeight - i.height, 0, _SectionHeight) / _SectionHeight;
  75. //alphaaddbottom *= step(0, alphaaddbottom);
  76. float alphaaddtop = clamp(i.height - (_YScale - _SectionHeight), 0, _SectionHeight) / _SectionHeight;
  77. //alphaaddtop *= step(0, alphaaddtop);
  78. fixed4 col = fixed4(_Color.rgb, _Color.a * (alphaaddbottom + alphaaddtop));
  79. //Determine if we're on the edge and should draw a line accordingly.
  80. int isedge = 0;
  81. isedge += step(i.reluv.x, _EdgeThickness);
  82. isedge += step(i.reluv.z - _EdgeThickness, i.reluv.x);
  83. isedge += step(i.reluv.y, _EdgeThickness);
  84. isedge += step(i.reluv.w - _EdgeThickness, i.reluv.y);
  85. col.a += ceil(col.a) * isedge; //We just draw the alpha as 1 if it's greater than zero.
  86. return col;
  87. }
  88. ENDCG
  89. }
  90. }
  91. }