LightingUtility.hlsl 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #if USE_NORMAL_MAP
  2. #if LIGHT_QUALITY_FAST
  3. #define NORMALS_LIGHTING_COORDS(TEXCOORDA, TEXCOORDB) \
  4. float4 lightDirection : TEXCOORDA;\
  5. float2 screenUV : TEXCOORDB;
  6. #define TRANSFER_NORMALS_LIGHTING(output, worldSpacePos)\
  7. float4 clipVertex = output.positionCS / output.positionCS.w;\
  8. output.screenUV = ComputeScreenPos(clipVertex).xy;\
  9. output.lightDirection.xy = _LightPosition.xy - worldSpacePos.xy;\
  10. output.lightDirection.z = _LightZDistance;\
  11. output.lightDirection.w = 0;\
  12. output.lightDirection.xyz = normalize(output.lightDirection.xyz);
  13. #define APPLY_NORMALS_LIGHTING(input, lightColor)\
  14. half4 normal = SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, input.screenUV);\
  15. float3 normalUnpacked = UnpackNormal(normal);\
  16. lightColor = lightColor * saturate(dot(input.lightDirection.xyz, normalUnpacked));
  17. #else
  18. #define NORMALS_LIGHTING_COORDS(TEXCOORDA, TEXCOORDB) \
  19. float4 positionWS : TEXCOORDA;\
  20. float2 screenUV : TEXCOORDB;
  21. #define TRANSFER_NORMALS_LIGHTING(output, worldSpacePos) \
  22. float4 clipVertex = output.positionCS / output.positionCS.w;\
  23. output.screenUV = ComputeScreenPos(clipVertex).xy; \
  24. output.positionWS = worldSpacePos;
  25. #define APPLY_NORMALS_LIGHTING(input, lightColor)\
  26. half4 normal = SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, input.screenUV);\
  27. float3 normalUnpacked = UnpackNormal(normal);\
  28. float3 dirToLight;\
  29. dirToLight.xy = _LightPosition.xy - input.positionWS.xy;\
  30. dirToLight.z = _LightZDistance;\
  31. dirToLight = normalize(dirToLight);\
  32. lightColor = lightColor * saturate(dot(dirToLight, normalUnpacked));
  33. #endif
  34. #define NORMALS_LIGHTING_VARIABLES \
  35. TEXTURE2D(_NormalMap); \
  36. SAMPLER(sampler_NormalMap); \
  37. float4 _LightPosition;\
  38. half _LightZDistance;
  39. #else
  40. #define NORMALS_LIGHTING_COORDS(TEXCOORDA, TEXCOORDB)
  41. #define NORMALS_LIGHTING_VARIABLES
  42. #define TRANSFER_NORMALS_LIGHTING(output, worldSpacePos)
  43. #define APPLY_NORMALS_LIGHTING(input, lightColor)
  44. #endif
  45. #define SHADOW_COORDS(TEXCOORDA)\
  46. float2 shadowUV : TEXCOORDA;
  47. #define SHADOW_VARIABLES\
  48. float _ShadowIntensity;\
  49. float _ShadowVolumeIntensity;\
  50. TEXTURE2D(_ShadowTex);\
  51. SAMPLER(sampler_ShadowTex);
  52. #define APPLY_SHADOWS(input, color, intensity)\
  53. if(intensity < 1)\
  54. {\
  55. half4 shadow = saturate(SAMPLE_TEXTURE2D(_ShadowTex, sampler_ShadowTex, input.shadowUV)); \
  56. half shadowIntensity = 1 - (shadow.r * saturate(2 * (shadow.g - 0.5f * shadow.b))); \
  57. color.rgb = (color.rgb * shadowIntensity) + (color.rgb * intensity*(1 - shadowIntensity));\
  58. }
  59. #define TRANSFER_SHADOWS(output)\
  60. output.shadowUV = ComputeScreenPos(output.positionCS / output.positionCS.w).xy;
  61. #define SHAPE_LIGHT(index)\
  62. TEXTURE2D(_ShapeLightTexture##index);\
  63. SAMPLER(sampler_ShapeLightTexture##index);\
  64. float2 _ShapeLightBlendFactors##index;\
  65. float4 _ShapeLightMaskFilter##index;\
  66. float4 _ShapeLightInvertedFilter##index;