ZED_Lighting_URP.cginc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //Copied from LitInput.hlsl then modified.
  2. #ifndef UNIVERSAL_LIT_INPUT_INCLUDED
  3. #define UNIVERSAL_LIT_INPUT_INCLUDED
  4. #endif
  5. #ifndef unity_ColorSpaceDielectricSpec
  6. #define unity_ColorSpaceDielectricSpec half4(0.220916301, 0.220916301, 0.220916301, 1.0 - 0.220916301)
  7. #endif
  8. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  9. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
  10. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
  11. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  12. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
  13. //#include "UnityStandardUtils.cginc"
  14. CBUFFER_START(UnityPerMaterial)
  15. float4 _BaseMap_ST;
  16. half4 _BaseColor;
  17. half4 _SpecColor;
  18. half4 _EmissionColor;
  19. half _Cutoff;
  20. half _Smoothness;
  21. half _Metallic;
  22. half _BumpScale;
  23. half _OcclusionStrength;
  24. CBUFFER_END
  25. TEXTURE2D(_OcclusionMap); SAMPLER(sampler_OcclusionMap);
  26. TEXTURE2D(_MetallicGlossMap); SAMPLER(sampler_MetallicGlossMap);
  27. TEXTURE2D(_SpecGlossMap); SAMPLER(sampler_SpecGlossMap);
  28. #ifdef _SPECULAR_SETUP
  29. #define SAMPLE_METALLICSPECULAR(uv) SAMPLE_TEXTURE2D(_SpecGlossMap, sampler_SpecGlossMap, uv)
  30. #else
  31. #define SAMPLE_METALLICSPECULAR(uv) SAMPLE_TEXTURE2D(_MetallicGlossMap, sampler_MetallicGlossMap, uv)
  32. #endif
  33. /******************************************************************/
  34. /************** Point lights information **************************/
  35. /******************************************************************/
  36. struct ZED_PointLight {
  37. float4 color;
  38. float range;
  39. float3 position;
  40. };
  41. /******************************************************************/
  42. /************** Spot lights information **************************/
  43. /******************************************************************/
  44. struct ZED_SpotLight {
  45. float4 color;
  46. float3 position;
  47. float4 direction;
  48. float4 params;// angle, intensity, 1/range, cone interior
  49. };
  50. ///From ZED_Lighting.cginc
  51. sampler2D _NormalsTex;
  52. uniform float4 _CameraRotationQuat;
  53. #if defined(ZED_SPOT_LIGHT_DECLARATION)
  54. StructuredBuffer<ZED_SpotLight> spotLights;
  55. int numberSpotLights;
  56. #endif
  57. #if defined(ZED_POINT_LIGHT_DECLARATION)
  58. StructuredBuffer<ZED_PointLight> pointLights;
  59. int numberPointLights;
  60. #endif
  61. bool Unity_IsNan_float3(float3 In)
  62. {
  63. bool Out = (In < 0.0 || In > 0.0 || In == 0.0) ? 0 : 1;
  64. return Out;
  65. }
  66. //Compute the light for all light
  67. #if defined(ZED_SPOT_LIGHT_DECLARATION) || defined(ZED_POINT_LIGHT_DECLARATION)
  68. half4 computeLightingLWRP(float3 albedo, float3 normals, float3 worldPos, float alpha, float zedfactoraffectreal)
  69. {
  70. half3 worldViewDir = normalize(worldPos - _WorldSpaceCameraPos.xyz);
  71. #ifdef UNITY_COMPILER_HLSL
  72. SurfaceData o = (SurfaceData)0;
  73. #else
  74. SurfaceData o;
  75. #endif
  76. o.albedo.rgb = albedo;
  77. o.specular = 1.0;
  78. o.metallic = _Metallic;
  79. o.smoothness = 1.0;
  80. o.normalTS = normals;
  81. o.emission = 0.0;
  82. o.occlusion = 0.0;
  83. o.alpha = 1;
  84. float3 specularTint = lerp(unity_ColorSpaceDielectricSpec.rgb, o.albedo, o.metallic); //Don't think this is needed.
  85. float oneMinusReflectivity = unity_ColorSpaceDielectricSpec.a - o.metallic * unity_ColorSpaceDielectricSpec.a;
  86. o.albedo.rgb *= oneMinusReflectivity;
  87. BRDFData brdfDataRaw;
  88. InitializeBRDFData(o.albedo, o.metallic, o.specular, o.smoothness, o.alpha, brdfDataRaw);
  89. float4 c = float4(albedo.rgb * zedfactoraffectreal, 1);
  90. #ifndef _RECEIVE_SHADOWS_OFF
  91. #ifdef _MAIN_LIGHT_SHADOWS
  92. #if SHADOWS_SCREEN
  93. float3 modws = float3(worldPos.x, worldPos.y, worldPos.z);
  94. float4 newshadcoords = TransformWorldToShadowCoord(modws);
  95. Light mainLight = GetMainLight();
  96. ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
  97. half shadowStrength = GetMainLightShadowStrength();
  98. mainLight.shadowAttenuation = SampleShadowmap(newshadcoords, TEXTURE2D_ARGS(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture), shadowSamplingData, shadowStrength, false);
  99. #else
  100. float3 modws = float3(worldPos.x, worldPos.y, worldPos.z);
  101. float4 newshadcoords = TransformWorldToShadowCoord(modws);
  102. Light mainLight = GetMainLight(newshadcoords);
  103. #endif
  104. #else
  105. Light mainLight = GetMainLight();
  106. #endif
  107. //Only apply directional shadows if it's not opposite of the normal, so that we don't draw shadows from objects onto walls that they're behind.
  108. float dirlightnormdot = dot(normals, mainLight.direction);
  109. if (!Unity_IsNan_float3(normals)) c.rgb = lerp(c.rgb, c.rgb *=mainLight.shadowAttenuation, step(0, dirlightnormdot));
  110. //c.rgb *= clamp(dirlightnormdot, 0, 1);
  111. /*if (dirlightnormdot > 0)
  112. {
  113. c.rgb *= mainLight.shadowAttenuation;
  114. }*/
  115. #endif
  116. int indexPointLights = 0;
  117. //For the point light
  118. #if defined(ZED_POINT_LIGHT_DECLARATION)
  119. UNITY_LOOP
  120. for (indexPointLights = 0; indexPointLights < numberPointLights; indexPointLights++) {
  121. float3 lightVec = pointLights[indexPointLights].position - worldPos;
  122. if (pointLights[indexPointLights].range - length(lightVec) < 0) {
  123. continue;
  124. }
  125. float distanceSqr = max(dot(lightVec, lightVec), HALF_MIN);
  126. //distanceSqr *= length(lightVec) / pointLights[indexPointLights].range;
  127. distanceSqr /= pointLights[indexPointLights].range;
  128. distanceSqr = saturate(distanceSqr);
  129. float att = DistanceAttenuation(distanceSqr, pointLights[indexPointLights].color);
  130. if (dot(lightVec, float3(normals.x, normals.y, normals.z)) <= 0.0) {
  131. continue;
  132. }
  133. Light p;
  134. p.direction = lightVec;
  135. p.distanceAttenuation = att;
  136. p.color = pointLights[indexPointLights].color*att*alpha;
  137. p.shadowAttenuation = 1.0;
  138. c.rgb += LightingPhysicallyBased(brdfDataRaw, p, normals, worldViewDir);
  139. c.a = 1.0;
  140. }
  141. #endif
  142. c.a = 1.0;
  143. //For the spot light
  144. #if defined(ZED_SPOT_LIGHT_DECLARATION)
  145. int indexSpotLights = 0;
  146. UNITY_LOOP
  147. for (indexSpotLights = 0; indexSpotLights < numberSpotLights; indexSpotLights++) {
  148. float3 lightVec = spotLights[indexSpotLights].position - worldPos;
  149. float3 dirSpotToWorld = lightVec;
  150. float dotDirectionWorld = dot(normalize(dirSpotToWorld), normalize(spotLights[indexSpotLights].direction.xyz));
  151. float angleWorld = degrees(acos(-dotDirectionWorld));
  152. float angleMax = spotLights[indexSpotLights].params.x / 2.0;
  153. //float distanceSqr = max(dotDirectionWorld, HALF_MIN);
  154. float distanceSqr = length(lightVec) / (1 / spotLights[indexSpotLights].params.z);
  155. //float distanceSqr = max(dot(lightVec, lightVec), HALF_MIN);
  156. //float att = DistanceAttenuation(distanceSqr, spotLights[indexSpotLights].color);
  157. float att = DistanceAttenuation(distanceSqr, half2(1, 1));
  158. //float att = DistanceAttenuation(.0001, spotLights[indexSpotLights].color);
  159. att = saturate(att);
  160. //att = dotDirectionWorld;
  161. UNITY_BRANCH
  162. if (dotDirectionWorld > 0 || dotDirectionWorld > -spotLights[indexSpotLights].direction.w) {
  163. continue;
  164. }
  165. else {
  166. float angleP = angleMax * (1 - spotLights[indexSpotLights].params.w);
  167. if (angleP < angleWorld && angleWorld < angleMax)
  168. {
  169. att *= (angleMax - angleWorld) / (angleMax - angleP);
  170. }
  171. }
  172. Light p;
  173. p.direction = -spotLights[indexSpotLights].direction.xyz;
  174. p.distanceAttenuation = att;
  175. p.color = spotLights[indexSpotLights].color.xyz*att*alpha;
  176. p.shadowAttenuation = 1.0;
  177. c.rgb += LightingPhysicallyBased(brdfDataRaw, p, normals, worldViewDir);
  178. c.a = 1.0;
  179. }
  180. #endif
  181. return c;
  182. }
  183. #endif