Point.shader 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Pcx - Point cloud importer & renderer for Unity
  2. // https://github.com/keijiro/Pcx
  3. Shader "Point Cloud/Point"
  4. {
  5. Properties
  6. {
  7. _Tint("Tint", Color) = (0.5, 0.5, 0.5, 1)
  8. _PointSize("Point Size", Float) = 0.05
  9. [Toggle] _Distance("Apply Distance", Float) = 1
  10. }
  11. SubShader
  12. {
  13. Tags { "RenderType"="Opaque" }
  14. Pass
  15. {
  16. CGPROGRAM
  17. #pragma vertex Vertex
  18. #pragma fragment Fragment
  19. #pragma multi_compile_fog
  20. #pragma multi_compile _ UNITY_COLORSPACE_GAMMA
  21. #pragma multi_compile _ _DISTANCE_ON
  22. #pragma multi_compile _ _COMPUTE_BUFFER
  23. #include "UnityCG.cginc"
  24. #include "Common.cginc"
  25. struct Attributes
  26. {
  27. float4 position : POSITION;
  28. half3 color : COLOR;
  29. };
  30. struct Varyings
  31. {
  32. float4 position : SV_Position;
  33. half3 color : COLOR;
  34. half psize : PSIZE;
  35. UNITY_FOG_COORDS(0)
  36. };
  37. half4 _Tint;
  38. float4x4 _Transform;
  39. half _PointSize;
  40. #if _COMPUTE_BUFFER
  41. StructuredBuffer<float4> _PointBuffer;
  42. #endif
  43. #if _COMPUTE_BUFFER
  44. Varyings Vertex(uint vid : SV_VertexID)
  45. #else
  46. Varyings Vertex(Attributes input)
  47. #endif
  48. {
  49. #if _COMPUTE_BUFFER
  50. float4 pt = _PointBuffer[vid];
  51. float4 pos = mul(_Transform, float4(pt.xyz, 1));
  52. half3 col = PcxDecodeColor(asuint(pt.w));
  53. #else
  54. float4 pos = input.position;
  55. half3 col = input.color;
  56. #endif
  57. #ifdef UNITY_COLORSPACE_GAMMA
  58. col *= _Tint.rgb * 2;
  59. #else
  60. col *= LinearToGammaSpace(_Tint.rgb) * 2;
  61. col = GammaToLinearSpace(col);
  62. #endif
  63. Varyings o;
  64. o.position = UnityObjectToClipPos(pos);
  65. o.color = col;
  66. #ifdef _DISTANCE_ON
  67. o.psize = _PointSize / o.position.w * _ScreenParams.y;
  68. #else
  69. o.psize = _PointSize;
  70. #endif
  71. UNITY_TRANSFER_FOG(o, o.position);
  72. return o;
  73. }
  74. half4 Fragment(Varyings input) : SV_Target
  75. {
  76. half4 c = half4(input.color, _Tint.a);
  77. UNITY_APPLY_FOG(input.fogCoord, c);
  78. return c;
  79. }
  80. ENDCG
  81. }
  82. }
  83. CustomEditor "Pcx.PointMaterialInspector"
  84. }