Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef UNIVERSAL_LIGHT_COOKIE_INCLUDED
  2. #define UNIVERSAL_LIGHT_COOKIE_INCLUDED
  3. // Includes
  4. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  5. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl"
  6. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/LightCookie/LightCookieInput.hlsl"
  7. #if defined(_LIGHT_COOKIES)
  8. #ifndef REQUIRES_WORLD_SPACE_POS_INTERPOLATOR
  9. #define REQUIRES_WORLD_SPACE_POS_INTERPOLATOR 1
  10. #endif
  11. #endif
  12. float2 ComputeLightCookieUVDirectional(float4x4 worldToLight, float3 samplePositionWS, float4 atlasUVRect, uint2 uvWrap)
  13. {
  14. // Translate and rotate 'positionWS' into the light space.
  15. // Project point to light "view" plane, i.e. discard Z.
  16. float2 positionLS = mul(worldToLight, float4(samplePositionWS, 1)).xy;
  17. // Remap [-1, 1] to [0, 1]
  18. // (implies the transform has ortho projection mapping world space box to [-1, 1])
  19. float2 positionUV = positionLS * 0.5 + 0.5;
  20. // Tile texture for cookie in repeat mode
  21. positionUV.x = (uvWrap.x == URP_TEXTURE_WRAP_MODE_REPEAT) ? frac(positionUV.x) : positionUV.x;
  22. positionUV.y = (uvWrap.y == URP_TEXTURE_WRAP_MODE_REPEAT) ? frac(positionUV.y) : positionUV.y;
  23. positionUV.x = (uvWrap.x == URP_TEXTURE_WRAP_MODE_CLAMP) ? saturate(positionUV.x) : positionUV.x;
  24. positionUV.y = (uvWrap.y == URP_TEXTURE_WRAP_MODE_CLAMP) ? saturate(positionUV.y) : positionUV.y;
  25. // Remap to atlas texture
  26. float2 positionAtlasUV = atlasUVRect.xy * float2(positionUV) + atlasUVRect.zw;
  27. return positionAtlasUV;
  28. }
  29. float2 ComputeLightCookieUVSpot(float4x4 worldToLightPerspective, float3 samplePositionWS, float4 atlasUVRect)
  30. {
  31. // Translate, rotate and project 'positionWS' into the light clip space.
  32. float4 positionCS = mul(worldToLightPerspective, float4(samplePositionWS, 1));
  33. float2 positionNDC = positionCS.xy / positionCS.w;
  34. // Remap NDC to the texture coordinates, from NDC [-1, 1]^2 to [0, 1]^2.
  35. float2 positionUV = saturate(positionNDC * 0.5 + 0.5);
  36. // Remap into rect in the atlas texture
  37. float2 positionAtlasUV = atlasUVRect.xy * float2(positionUV) + atlasUVRect.zw;
  38. return positionAtlasUV;
  39. }
  40. float2 ComputeLightCookieUVPoint(float4x4 worldToLight, float3 samplePositionWS, float4 atlasUVRect)
  41. {
  42. // Translate and rotate 'positionWS' into the light space.
  43. float4 positionLS = mul(worldToLight, float4(samplePositionWS, 1));
  44. float3 sampleDirLS = normalize(positionLS.xyz / positionLS.w);
  45. // Project direction to Octahederal quad UV.
  46. float2 positionUV = saturate(PackNormalOctQuadEncode(sampleDirLS) * 0.5 + 0.5);
  47. // Remap to atlas texture
  48. float2 positionAtlasUV = atlasUVRect.xy * float2(positionUV) + atlasUVRect.zw;
  49. return positionAtlasUV;
  50. }
  51. real3 SampleMainLightCookie(float3 samplePositionWS)
  52. {
  53. if(!IsMainLightCookieEnabled())
  54. return real3(1,1,1);
  55. float2 uv = ComputeLightCookieUVDirectional(_MainLightWorldToLight, samplePositionWS, float4(1, 1, 0, 0), URP_TEXTURE_WRAP_MODE_NONE);
  56. real4 color = SampleMainLightCookieTexture(uv);
  57. return IsMainLightCookieTextureRGBFormat() ? color.rgb
  58. : IsMainLightCookieTextureAlphaFormat() ? color.aaa
  59. : color.rrr;
  60. }
  61. real3 SampleAdditionalLightCookie(int perObjectLightIndex, float3 samplePositionWS)
  62. {
  63. if(!IsLightCookieEnabled(perObjectLightIndex))
  64. return real3(1,1,1);
  65. int lightType = GetLightCookieLightType(perObjectLightIndex);
  66. int isSpot = lightType == URP_LIGHT_TYPE_SPOT;
  67. int isDirectional = lightType == URP_LIGHT_TYPE_DIRECTIONAL;
  68. float4x4 worldToLight = GetLightCookieWorldToLightMatrix(perObjectLightIndex);
  69. float4 uvRect = GetLightCookieAtlasUVRect(perObjectLightIndex);
  70. float2 uv;
  71. if(isSpot)
  72. {
  73. uv = ComputeLightCookieUVSpot(worldToLight, samplePositionWS, uvRect);
  74. }
  75. else if(isDirectional)
  76. {
  77. uv = ComputeLightCookieUVDirectional(worldToLight, samplePositionWS, uvRect, URP_TEXTURE_WRAP_MODE_REPEAT);
  78. }
  79. else
  80. {
  81. uv = ComputeLightCookieUVPoint(worldToLight, samplePositionWS, uvRect);
  82. }
  83. real4 color = SampleAdditionalLightsCookieAtlasTexture(uv);
  84. return IsAdditionalLightsCookieAtlasTextureRGBFormat() ? color.rgb
  85. : IsAdditionalLightsCookieAtlasTextureAlphaFormat() ? color.aaa
  86. : color.rrr;
  87. }
  88. #endif //UNIVERSAL_LIGHT_COOKIE_INCLUDED