Disk.cginc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Pcx - Point cloud importer & renderer for Unity
  2. // https://github.com/keijiro/Pcx
  3. #include "UnityCG.cginc"
  4. #include "Common.cginc"
  5. // Uniforms
  6. half4 _Tint;
  7. half _PointSize;
  8. float4x4 _Transform;
  9. #if _COMPUTE_BUFFER
  10. StructuredBuffer<float4> _PointBuffer;
  11. #endif
  12. // Vertex input attributes
  13. struct Attributes
  14. {
  15. #if _COMPUTE_BUFFER
  16. uint vertexID : SV_VertexID;
  17. #else
  18. float4 position : POSITION;
  19. half3 color : COLOR;
  20. #endif
  21. };
  22. // Fragment varyings
  23. struct Varyings
  24. {
  25. float4 position : SV_POSITION;
  26. #if !PCX_SHADOW_CASTER
  27. half3 color : COLOR;
  28. UNITY_FOG_COORDS(0)
  29. #endif
  30. };
  31. // Vertex phase
  32. Varyings Vertex(Attributes input)
  33. {
  34. // Retrieve vertex attributes.
  35. #if _COMPUTE_BUFFER
  36. float4 pt = _PointBuffer[input.vertexID];
  37. float4 pos = mul(_Transform, float4(pt.xyz, 1));
  38. half3 col = PcxDecodeColor(asuint(pt.w));
  39. #else
  40. float4 pos = input.position;
  41. half3 col = input.color;
  42. #endif
  43. #if !PCX_SHADOW_CASTER
  44. // Color space convertion & applying tint
  45. #if UNITY_COLORSPACE_GAMMA
  46. col *= _Tint.rgb * 2;
  47. #else
  48. col *= LinearToGammaSpace(_Tint.rgb) * 2;
  49. col = GammaToLinearSpace(col);
  50. #endif
  51. #endif
  52. // Set vertex output.
  53. Varyings o;
  54. o.position = UnityObjectToClipPos(pos);
  55. #if !PCX_SHADOW_CASTER
  56. o.color = col;
  57. UNITY_TRANSFER_FOG(o, o.position);
  58. #endif
  59. return o;
  60. }
  61. // Geometry phase
  62. [maxvertexcount(36)]
  63. void Geometry(point Varyings input[1], inout TriangleStream<Varyings> outStream)
  64. {
  65. float4 origin = input[0].position;
  66. float2 extent = abs(UNITY_MATRIX_P._11_22 * _PointSize);
  67. // Copy the basic information.
  68. Varyings o = input[0];
  69. // Determine the number of slices based on the radius of the
  70. // point on the screen.
  71. float radius = extent.y / origin.w * _ScreenParams.y;
  72. uint slices = min((radius + 1) / 5, 4) + 2;
  73. // Slightly enlarge quad points to compensate area reduction.
  74. // Hopefully this line would be complied without branch.
  75. if (slices == 2) extent *= 1.2;
  76. // Top vertex
  77. o.position.y = origin.y + extent.y;
  78. o.position.xzw = origin.xzw;
  79. outStream.Append(o);
  80. UNITY_LOOP for (uint i = 1; i < slices; i++)
  81. {
  82. float sn, cs;
  83. sincos(UNITY_PI / slices * i, sn, cs);
  84. // Right side vertex
  85. o.position.xy = origin.xy + extent * float2(sn, cs);
  86. outStream.Append(o);
  87. // Left side vertex
  88. o.position.x = origin.x - extent.x * sn;
  89. outStream.Append(o);
  90. }
  91. // Bottom vertex
  92. o.position.x = origin.x;
  93. o.position.y = origin.y - extent.y;
  94. outStream.Append(o);
  95. outStream.RestartStrip();
  96. }
  97. half4 Fragment(Varyings input) : SV_Target
  98. {
  99. #if PCX_SHADOW_CASTER
  100. return 0;
  101. #else
  102. half4 c = half4(input.color, _Tint.a);
  103. UNITY_APPLY_FOG(input.fogCoord, c);
  104. return c;
  105. #endif
  106. }