ShaderUtils.cginc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // The following comment prevents Unity from auto upgrading the shader. Please keep it to keep backward compatibility.
  2. // UNITY_SHADER_NO_UPGRADE
  3. #ifndef _VLB_SHADER_UTILS_INCLUDED_
  4. #define _VLB_SHADER_UTILS_INCLUDED_
  5. #include "ShaderMaths.cginc"
  6. // https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html
  7. #define VLB_CAMERA_NEAR_PLANE _ProjectionParams.y
  8. #define VLB_CAMERA_FAR_PLANE _ProjectionParams.z
  9. #define VLB_CAMERA_ORTHO unity_OrthoParams.w // w is 1.0 when camera is orthographic, 0.0 when perspective
  10. // Z buffer to linear depth
  11. float VLB_ZBufferToLinear(float depth, float near, float far)
  12. {
  13. float x = 1 - far / near;
  14. float y = far / near;
  15. float z = x / far;
  16. float w = y / far;
  17. return 1.0 / (z * depth + w);
  18. }
  19. #if VLB_DEPTH_BLEND || VLB_DITHERING
  20. inline float SampleSceneZ_Eye(float4 uv)
  21. {
  22. float rawDepth = VLBSampleDepthTexture(uv);
  23. float linearDepthPersp = VLBLinearEyeDepth(rawDepth);
  24. #if defined(UNITY_REVERSED_Z) // not reversed in OpenGL on WebGL
  25. rawDepth = 1.0 - rawDepth;
  26. #endif
  27. float linearDepthOrtho = (VLB_CAMERA_FAR_PLANE - VLB_CAMERA_NEAR_PLANE) * rawDepth + VLB_CAMERA_NEAR_PLANE;
  28. return lerp(linearDepthPersp, linearDepthOrtho, VLB_CAMERA_ORTHO);
  29. }
  30. inline float4 DepthFade_VS_ComputeProjPos(float3 vertexViewSpace, float4 vertexClipSpace)
  31. {
  32. float4 projPos = ComputeScreenPos(vertexClipSpace);
  33. projPos.z = -vertexViewSpace.z; // = COMPUTE_EYEDEPTH
  34. return projPos;
  35. }
  36. inline float DepthFade_PS_BlendDistance(float4 projPos, float distance)
  37. {
  38. float sceneZ = max(0, SampleSceneZ_Eye(projPos) - VLB_CAMERA_NEAR_PLANE);
  39. float partZ = max(0, projPos.z - VLB_CAMERA_NEAR_PLANE);
  40. return saturate((sceneZ - partZ) / distance);
  41. }
  42. #endif // VLB_DEPTH_BLEND || VLB_DITHERING
  43. #if VLB_NOISE_3D
  44. uniform sampler3D _VLB_NoiseTex3D;
  45. uniform float _VLB_NoiseCustomTime;
  46. float3 Noise3D_GetUVW(float3 posWorldSpace, float3 posLocalSpace)
  47. {
  48. float4 noiseVelocityAndScale = VLB_GET_PROP(_NoiseVelocityAndScale);
  49. float2 noiseParam = VLB_GET_PROP(_NoiseParam);
  50. float3 velocity = noiseVelocityAndScale.xyz;
  51. float scale = noiseVelocityAndScale.w;
  52. float3 posRef = lerp(posWorldSpace, posLocalSpace, noiseParam.y); // 0 -> World Space ; 1 -> Local Space
  53. // use _VLB_NoiseCustomTime if it's equal or higher than 0.0
  54. float currentTime = lerp(_Time.y, _VLB_NoiseCustomTime, isEqualOrGreater(_VLB_NoiseCustomTime, 0.0f));
  55. //return frac(posRef.xyz * scale + (currentTime * velocity)); // frac doesn't give good results on VS
  56. return (posRef.xyz * scale + (currentTime * velocity));
  57. }
  58. float Noise3D_GetFactorFromUVW(float3 uvw)
  59. {
  60. float2 noiseParam = VLB_GET_PROP(_NoiseParam);
  61. float intensity = noiseParam.x;
  62. float noise = tex3D(_VLB_NoiseTex3D, uvw).a;
  63. return lerp(1, noise, intensity);
  64. }
  65. #endif // VLB_NOISE_3D
  66. inline float ComputeAttenuation(float pixDistZ, float fallOffStart, float fallOffEnd, float lerpLinearQuad)
  67. {
  68. float distFromSourceNormalized = invLerpClamped(fallOffStart, fallOffEnd, pixDistZ);
  69. // Almost simple linear attenuation between Fade Start and Fade End: Use smoothstep for a better fall to zero rendering
  70. float attLinear = smoothstep(0, 1, 1 - distFromSourceNormalized);
  71. // Unity's custom quadratic attenuation https://forum.unity.com/threads/light-attentuation-equation.16006/
  72. float attQuad = 1.0 / (1.0 + 25.0 * distFromSourceNormalized * distFromSourceNormalized);
  73. const float kAttQuadStartToFallToZero = 0.8;
  74. attQuad *= saturate(smoothstep(1.0, kAttQuadStartToFallToZero, distFromSourceNormalized)); // Near the light's range (fade end) we fade to 0 (because quadratic formula never falls to 0)
  75. return lerp(attLinear, attQuad, lerpLinearQuad);
  76. }
  77. #if VLB_COLOR_GRADIENT_MATRIX_HIGH || VLB_COLOR_GRADIENT_MATRIX_LOW
  78. #if VLB_COLOR_GRADIENT_MATRIX_HIGH
  79. #define FLOAT_PACKING_PRECISION 64
  80. #else
  81. #define FLOAT_PACKING_PRECISION 8
  82. #endif
  83. inline half4 UnpackToColor(float packedFloat)
  84. {
  85. half4 color;
  86. color.a = packedFloat % FLOAT_PACKING_PRECISION;
  87. packedFloat = floor(packedFloat / FLOAT_PACKING_PRECISION);
  88. color.b = packedFloat % FLOAT_PACKING_PRECISION;
  89. packedFloat = floor(packedFloat / FLOAT_PACKING_PRECISION);
  90. color.g = packedFloat % FLOAT_PACKING_PRECISION;
  91. packedFloat = floor(packedFloat / FLOAT_PACKING_PRECISION);
  92. color.r = packedFloat;
  93. return color / (FLOAT_PACKING_PRECISION - 1);
  94. }
  95. inline float GetAtMatrixIndex(float4x4 mat, uint idx) { return mat[idx % 4][floor(idx / 4)]; }
  96. inline half4 DecodeGradient(float t, float4x4 colorMatrix)
  97. {
  98. #define kColorGradientMatrixSize 16
  99. float sampleIndexFloat = t * (kColorGradientMatrixSize - 1);
  100. float ratioPerSample = sampleIndexFloat - (int)sampleIndexFloat;
  101. uint sampleIndexInt = min((uint)sampleIndexFloat, kColorGradientMatrixSize - 2);
  102. half4 colorA = UnpackToColor(GetAtMatrixIndex(colorMatrix, sampleIndexInt + 0));
  103. half4 colorB = UnpackToColor(GetAtMatrixIndex(colorMatrix, sampleIndexInt + 1));
  104. return lerp(colorA, colorB, ratioPerSample);
  105. }
  106. #elif VLB_COLOR_GRADIENT_ARRAY
  107. inline half4 DecodeGradient(float t, float4 colorArray[kColorGradientArraySize])
  108. {
  109. uint arraySize = kColorGradientArraySize;
  110. float sampleIndexFloat = t * (arraySize - 1);
  111. float ratioPerSample = sampleIndexFloat - (int)sampleIndexFloat;
  112. uint sampleIndexInt = min((uint)sampleIndexFloat, arraySize - 2);
  113. half4 colorA = colorArray[sampleIndexInt + 0];
  114. half4 colorB = colorArray[sampleIndexInt + 1];
  115. return lerp(colorA, colorB, ratioPerSample);
  116. }
  117. #endif // VLB_COLOR_GRADIENT_*
  118. #endif // _VLB_SHADER_UTILS_INCLUDED_