CTI URP SG Lighting.hlsl 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. void CTI_SimpleTranslucentLighting_float(
  2. float3 PositionWS,
  3. half3 NormalWS,
  4. half3 TangentWS,
  5. half3 BitangentWS,
  6. half3 Albedo,
  7. half3 NormalTS,
  8. half3 Transmission,
  9. half TransmissionMask,
  10. half Occlusion,
  11. out half3 o_NormalWS,
  12. out half3 o_Transmission
  13. )
  14. {
  15. #ifdef SHADERGRAPH_PREVIEW
  16. o_NormalWS = half3(0,1,0);
  17. o_Transmission = 0;
  18. #else
  19. half3 translucency = 1;
  20. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(PositionWS);
  21. half3x3 tangentToWorld = half3x3(TangentWS, BitangentWS, NormalWS);
  22. o_NormalWS = NormalizeNormalPerPixel(TransformTangentToWorld(NormalTS, tangentToWorld));
  23. half4 shadowCoord = TransformWorldToShadowCoord(PositionWS);
  24. Light mainLight = GetMainLight(shadowCoord);
  25. half w = 0.3; // 0.4
  26. half NdotL = saturate((dot(o_NormalWS, mainLight.direction) + w) / ((1 + w) * (1 + w)));
  27. half3 transLightDir = mainLight.direction + o_NormalWS * Transmission.z;
  28. half transDot = dot( transLightDir, -viewDirWS );
  29. transDot = exp2(saturate(transDot) * Transmission.y - Transmission.y);
  30. o_Transmission = transDot * (1.0 - NdotL) * mainLight.color * mainLight.shadowAttenuation * Transmission.x;
  31. if(_AmbientTranslucency > 0)
  32. {
  33. o_Transmission += SampleSH(-o_NormalWS) * _AmbientTranslucency * Occlusion;
  34. }
  35. o_Transmission *= Albedo * TransmissionMask;
  36. #endif
  37. }
  38. void SampleSH(half3 normalWS, out half3 Ambient)
  39. {
  40. // LPPV is not supported in Ligthweight Pipeline
  41. real4 SHCoefficients[7];
  42. SHCoefficients[0] = unity_SHAr;
  43. SHCoefficients[1] = unity_SHAg;
  44. SHCoefficients[2] = unity_SHAb;
  45. SHCoefficients[3] = unity_SHBr;
  46. SHCoefficients[4] = unity_SHBg;
  47. SHCoefficients[5] = unity_SHBb;
  48. SHCoefficients[6] = unity_SHC;
  49. Ambient = max(half3(0, 0, 0), SampleSH9(SHCoefficients, normalWS));
  50. }