2
0

LightComponent.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright (C) 2009-2019, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <anki/scene/components/LightComponent.h>
  6. #include <anki/scene/components/FrustumComponent.h>
  7. #include <anki/scene/SceneNode.h>
  8. #include <anki/scene/SceneGraph.h>
  9. #include <anki/scene/Octree.h>
  10. #include <anki/Collision.h>
  11. #include <shaders/glsl_cpp_common/ClusteredShading.h>
  12. namespace anki
  13. {
  14. LightComponent::LightComponent(LightComponentType type, U64 uuid)
  15. : SceneComponent(CLASS_TYPE)
  16. , m_uuid(uuid)
  17. , m_type(type)
  18. {
  19. ANKI_ASSERT(m_uuid > 0);
  20. switch(type)
  21. {
  22. case LightComponentType::POINT:
  23. m_point.m_radius = 1.0f;
  24. break;
  25. case LightComponentType::SPOT:
  26. setInnerAngle(toRad(45.0));
  27. setOuterAngle(toRad(30.0));
  28. m_spot.m_distance = 1.0f;
  29. m_spot.m_textureMat = Mat4::getIdentity();
  30. break;
  31. case LightComponentType::DIRECTIONAL:
  32. m_dir.m_sceneMax = Vec3(MIN_F32);
  33. m_dir.m_sceneMin = Vec3(MAX_F32);
  34. break;
  35. default:
  36. ANKI_ASSERT(0);
  37. }
  38. }
  39. Error LightComponent::update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated)
  40. {
  41. updated = false;
  42. if(m_flags.get(DIRTY))
  43. {
  44. updated = true;
  45. }
  46. if(m_flags.get(TRF_DIRTY))
  47. {
  48. updated = true;
  49. if(m_type == LightComponentType::SPOT)
  50. {
  51. static const Mat4 biasMat4(0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0);
  52. Mat4 proj = Mat4::calculatePerspectiveProjectionMatrix(
  53. m_spot.m_outerAngle, m_spot.m_outerAngle, LIGHT_FRUSTUM_NEAR_PLANE, m_spot.m_distance);
  54. m_spot.m_textureMat = biasMat4 * proj * Mat4(m_trf.getInverse());
  55. }
  56. }
  57. m_flags.unset(DIRTY | TRF_DIRTY);
  58. // Update the scene bounds always
  59. if(m_type == LightComponentType::DIRECTIONAL)
  60. {
  61. node.getSceneGraph().getOctree().getActualSceneBounds(m_dir.m_sceneMin, m_dir.m_sceneMax);
  62. }
  63. return Error::NONE;
  64. }
  65. void LightComponent::setupDirectionalLightQueueElement(const FrustumComponent& frustumComp,
  66. DirectionalLightQueueElement& el,
  67. WeakArray<FrustumComponent> cascadeFrustumComponents) const
  68. {
  69. ANKI_ASSERT(m_type == LightComponentType::DIRECTIONAL);
  70. ANKI_ASSERT(cascadeFrustumComponents.getSize() <= MAX_SHADOW_CASCADES);
  71. const U shadowCascadeCount = cascadeFrustumComponents.getSize();
  72. el.m_drawCallback = m_drawCallback;
  73. el.m_drawCallbackUserData = m_drawCallbackUserData;
  74. el.m_uuid = m_uuid;
  75. el.m_diffuseColor = m_diffColor.xyz();
  76. el.m_direction = -m_trf.getRotation().getZAxis().xyz();
  77. el.m_shadowCascadeCount = shadowCascadeCount;
  78. // Compute the texture matrices
  79. if(shadowCascadeCount == 0)
  80. {
  81. return;
  82. }
  83. const Mat4 lightTrf(m_trf);
  84. if(frustumComp.getFrustumType() == FrustumType::PERSPECTIVE)
  85. {
  86. // Get some stuff
  87. const F32 fovX = frustumComp.getFovX();
  88. const F32 fovY = frustumComp.getFovY();
  89. const F32 far = frustumComp.getEffectiveShadowDistance();
  90. // Compute a sphere per cascade
  91. Array<Sphere, MAX_SHADOW_CASCADES> boundingSpheres;
  92. for(U i = 0; i < shadowCascadeCount; ++i)
  93. {
  94. const F32 cascadeFarNearDist = far / F32(shadowCascadeCount);
  95. // Compute the center of the sphere
  96. // ^ z
  97. // |
  98. // ----------|---------- A(a, -f)
  99. // \ | /
  100. // \ | /
  101. // \ C(0,z) /
  102. // \ | /
  103. // \ | /
  104. // \---|---/ B(b, -n)
  105. // \ | /
  106. // \ | /
  107. // v
  108. // --------------------------> x
  109. // |
  110. // The square distance of A-C is equal to B-C. Solve the equation to find the z.
  111. const F32 f = F32(i + 1) * cascadeFarNearDist; // Cascade far
  112. const F32 n = max(frustumComp.getNear(), F32(i) * cascadeFarNearDist); // Cascade near
  113. const F32 a = f * tan(fovY / 2.0f) * fovX / fovY;
  114. const F32 b = n * tan(fovY / 2.0f) * fovX / fovY;
  115. const F32 z = (b * b + n * n - a * a - f * f) / (2.0f * (f - n));
  116. ANKI_ASSERT(absolute((Vec2(a, -f) - Vec2(0, z)).getLength() - (Vec2(b, -n) - Vec2(0, z)).getLength())
  117. <= EPSILON * 100.0f);
  118. Vec3 C(0.0f, 0.0f, z); // Sphere center
  119. // Compute the radius of the sphere
  120. const Vec3 A(a, tan(fovY / 2.0f) * f, -f);
  121. const F32 r = (A - C).getLength();
  122. // Set the sphere
  123. boundingSpheres[i].setRadius(r);
  124. boundingSpheres[i].setCenter(frustumComp.getTransform().transform(C));
  125. }
  126. // Compute the matrices
  127. for(U i = 0; i < shadowCascadeCount; ++i)
  128. {
  129. const Sphere& sphere = boundingSpheres[i];
  130. const Vec3 sphereCenter = sphere.getCenter().xyz();
  131. const F32 sphereRadius = sphere.getRadius();
  132. const Vec3& lightDir = el.m_direction;
  133. const Vec3 sceneMin = m_dir.m_sceneMin - Vec3(sphereRadius); // Push the bounds a bit
  134. const Vec3 sceneMax = m_dir.m_sceneMax + Vec3(sphereRadius);
  135. // Compute the intersections with the scene bounds
  136. Vec3 eye;
  137. if(sphereCenter > sceneMin && sphereCenter < sceneMax)
  138. {
  139. // Inside the scene bounds
  140. const Aabb sceneBox(sceneMin, sceneMax);
  141. const F32 t = testCollisionInside(sceneBox, Ray(sphereCenter, -lightDir));
  142. eye = sphereCenter + t * (-lightDir);
  143. }
  144. else
  145. {
  146. eye = sphereCenter + sphereRadius * (-lightDir);
  147. }
  148. // Projection
  149. const F32 far = (eye - sphereCenter).getLength() + sphereRadius;
  150. Mat4 cascadeProjMat = Mat4::calculateOrthographicProjectionMatrix(
  151. sphereRadius, -sphereRadius, sphereRadius, -sphereRadius, LIGHT_FRUSTUM_NEAR_PLANE, far);
  152. // View
  153. Transform cascadeTransform = m_trf;
  154. cascadeTransform.setOrigin(eye.xyz0());
  155. const Mat4 cascadeViewMat = Mat4(cascadeTransform.getInverse());
  156. // Now it's time to stabilize the shadows by aligning the projection matrix
  157. {
  158. // Project a random fixed point to the light matrix
  159. const Vec4 randomPointAlmostLightSpace = (cascadeProjMat * cascadeViewMat) * Vec3(0.0f).xyz1();
  160. // Chose a random low shadowmap size and align the random point
  161. const F32 shadowmapSize = 128.0f;
  162. const F32 shadowmapSize2 = shadowmapSize / 2.0f; // Div with 2 because the projected point is in NDC
  163. const F32 alignedX = round(randomPointAlmostLightSpace.x() * shadowmapSize2) / shadowmapSize2;
  164. const F32 alignedY = round(randomPointAlmostLightSpace.y() * shadowmapSize2) / shadowmapSize2;
  165. const F32 dx = alignedX - randomPointAlmostLightSpace.x();
  166. const F32 dy = alignedY - randomPointAlmostLightSpace.y();
  167. // Fix the projection matrix by applying an offset
  168. Mat4 correctionTranslationMat = Mat4::getIdentity();
  169. correctionTranslationMat.setTranslationPart(Vec4(dx, dy, 0, 1.0f));
  170. cascadeProjMat = correctionTranslationMat * cascadeProjMat;
  171. }
  172. // Light matrix
  173. static const Mat4 biasMat4(
  174. 0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 0.0f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
  175. el.m_textureMatrices[i] = biasMat4 * cascadeProjMat * cascadeViewMat;
  176. // Fill the frustum with the fixed projection parameters from the fixed projection matrix
  177. Plane plane;
  178. extractClipPlane(cascadeProjMat, FrustumPlaneType::LEFT, plane);
  179. const F32 left = plane.getOffset();
  180. extractClipPlane(cascadeProjMat, FrustumPlaneType::RIGHT, plane);
  181. const F32 right = -plane.getOffset();
  182. extractClipPlane(cascadeProjMat, FrustumPlaneType::TOP, plane);
  183. const F32 top = -plane.getOffset();
  184. extractClipPlane(cascadeProjMat, FrustumPlaneType::BOTTOM, plane);
  185. const F32 bottom = plane.getOffset();
  186. FrustumComponent& cascadeFrustumComp = cascadeFrustumComponents[i];
  187. cascadeFrustumComp.setOrthographic(LIGHT_FRUSTUM_NEAR_PLANE, far, right, left, top, bottom);
  188. cascadeFrustumComp.setTransform(cascadeTransform);
  189. }
  190. }
  191. else
  192. {
  193. ANKI_ASSERT(!"TODO");
  194. }
  195. }
  196. } // end namespace anki