ZED_Lighting.cginc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. #if !defined(ZED_LIGHTING)
  3. #define ZED_LIGHTING
  4. #include "UnityCG.cginc"
  5. #include "Lighting.cginc"
  6. #include "UnityPBSLighting.cginc"
  7. #include "AutoLight.cginc"
  8. /******************************************************************/
  9. /************** Point lights information **************************/
  10. /******************************************************************/
  11. struct ZED_PointLight{
  12. float4 color;
  13. float range;
  14. float3 position;
  15. };
  16. /******************************************************************/
  17. /************** Spot lights information **************************/
  18. /******************************************************************/
  19. struct ZED_SpotLight{
  20. float4 color;
  21. float3 position;
  22. float4 direction;
  23. float4 params;// angle, intensity, 1/range, cone interior
  24. };
  25. sampler2D _NormalsTex;
  26. uniform float4 _CameraRotationQuat;
  27. #if defined(ZED_SPOT_LIGHT_DECLARATION)
  28. StructuredBuffer<ZED_SpotLight> spotLights;
  29. int numberSpotLights;
  30. #endif
  31. #if defined(ZED_POINT_LIGHT_DECLARATION)
  32. StructuredBuffer<ZED_PointLight> pointLights;
  33. int numberPointLights;
  34. #endif
  35. //FallOff the light
  36. float FallOff(float dist, float inverseRange, float coeff) {
  37. return lerp( 1.0, ( 1.0 - pow( dist * inverseRange * inverseRange, coeff ) ), 1 );
  38. }
  39. #define ZED_WORLD_DIR(index) float3 worldDirection : TEXCOORD##index;
  40. #define ZED_TRANSFER_WORLD_DIR(o) o.worldDirection = mul(unity_ObjectToWorld, v.vertex).xyz - _WorldSpaceCameraPos;
  41. #define GET_XYZ(o, z, world) world = (o.worldDirection/ o.pos.w) * z + _WorldSpaceCameraPos;
  42. float _Metallic;
  43. // Create a point light or spot light to be used per Unity
  44. UnityLight CreateLight (float3 pos, float4 color, float3 worldPos, float3 normal) {
  45. UnityLight light;
  46. light.dir = pos;
  47. light.color = color;
  48. light.ndotl = DotClamped(normal, light.dir);
  49. return light;
  50. }
  51. //Compute the light for all light
  52. #if defined(ZED_SPOT_LIGHT_DECLARATION) || defined(ZED_POINT_LIGHT_DECLARATION)
  53. half4 computeLighting(float3 albedo, float3 normals, float3 worldPos, float alpha) {
  54. fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));
  55. #ifdef UNITY_COMPILER_HLSL
  56. SurfaceOutputStandard o = (SurfaceOutputStandard)0;
  57. #else
  58. SurfaceOutputStandard o;
  59. #endif
  60. o.Albedo.rgb = albedo;
  61. o.Emission = 0.0;
  62. o.Alpha = 1.0;
  63. o.Occlusion = 1.0;
  64. o.Metallic = _Metallic;
  65. o.Normal.rgb = mul((float3x3)unity_CameraToWorld, normals);
  66. float3 specularTint;
  67. float oneMinusReflectivity;
  68. o.Albedo.rgb = DiffuseAndSpecularFromMetallic(
  69. albedo, o.Metallic, specularTint, oneMinusReflectivity
  70. );
  71. float4 c = 0;
  72. // Setup lighting environment
  73. UnityGI gi;
  74. UNITY_INITIALIZE_OUTPUT(UnityGI, gi);
  75. gi.indirect.diffuse = 0;
  76. gi.indirect.specular = 0;
  77. int indexPointLights = 0;
  78. //For the point light
  79. #if defined(ZED_POINT_LIGHT_DECLARATION)
  80. UNITY_LOOP
  81. for (indexPointLights = 0; indexPointLights < numberPointLights; indexPointLights++) {
  82. float3 lightVec = pointLights[indexPointLights].position - worldPos;
  83. if (pointLights[indexPointLights].range - length(lightVec) < 0) {
  84. continue;
  85. }
  86. float att = FallOff(dot(lightVec, lightVec), 1 / pointLights[indexPointLights].range, 0.2);
  87. float v = dot(lightVec, float3(o.Normal.x, o.Normal.y, o.Normal.z));
  88. //Remove light from backward
  89. //UNITY_BRANCH
  90. if (dot(lightVec, float3(o.Normal.x, o.Normal.y, o.Normal.z)) <= 0.0) {
  91. continue;
  92. }
  93. UnityLight p = CreateLight(lightVec, pointLights[indexPointLights].color*att*alpha, worldPos, o.Normal.rgb);
  94. gi.light = p;
  95. c += UNITY_BRDF_PBS(o.Albedo.rgb, specularTint, oneMinusReflectivity, 0, o.Normal.rgb, normalize(_WorldSpaceCameraPos - worldPos), p, gi.indirect);
  96. c.a = 1.0;
  97. }
  98. #endif
  99. c.a = 1.0;
  100. //For the spot light
  101. #if defined(ZED_SPOT_LIGHT_DECLARATION)
  102. int indexSpotLights = 0;
  103. UNITY_LOOP
  104. for (indexSpotLights = 0; indexSpotLights < numberSpotLights; indexSpotLights++) {
  105. float3 lightVec = spotLights[indexSpotLights].position - worldPos;
  106. float att = FallOff(dot(lightVec, lightVec), spotLights[indexSpotLights].params.z, 0.8);
  107. float3 dirSpotToWorld = -lightVec;
  108. float dotDirectionWorld = dot(normalize(dirSpotToWorld), spotLights[indexSpotLights].direction.xyz);
  109. float angleWorld = degrees(acos(dotDirectionWorld));
  110. float angleMax = spotLights[indexSpotLights].params.x / 2.0;
  111. UNITY_BRANCH
  112. if (dotDirectionWorld < 0 || dotDirectionWorld < spotLights[indexSpotLights].direction.w) {
  113. continue;
  114. }
  115. else {
  116. float angleP = angleMax*(1 - spotLights[indexSpotLights].params.w);
  117. UNITY_BRANCH
  118. if (angleP < angleWorld && angleWorld < angleMax) {
  119. att *= (angleMax - angleWorld) / (angleMax - angleP);
  120. }
  121. }
  122. att = saturate(att);
  123. UnityLight p = CreateLight(lightVec, (spotLights[indexSpotLights].color)*att*alpha, worldPos, o.Normal.rgb);
  124. gi.light = p;
  125. c += UNITY_BRDF_PBS(o.Albedo.rgb, specularTint, oneMinusReflectivity, 1, o.Normal.rgb, normalize(_WorldSpaceCameraPos - worldPos), p, gi.indirect);
  126. c.a = 1.0;
  127. }
  128. #endif
  129. return c;
  130. }
  131. #endif
  132. #endif