LightComponent.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. // Copyright (C) 2009-2023, 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/SceneNode.h>
  7. #include <AnKi/Scene/Frustum.h>
  8. #include <AnKi/Scene/SceneNode.h>
  9. #include <AnKi/Scene/SceneGraph.h>
  10. #include <AnKi/Scene/Octree.h>
  11. #include <AnKi/Collision.h>
  12. #include <AnKi/Resource/ResourceManager.h>
  13. #include <AnKi/Resource/ImageResource.h>
  14. #include <AnKi/Shaders/Include/ClusteredShadingTypes.h>
  15. namespace anki {
  16. LightComponent::LightComponent(SceneNode* node)
  17. : SceneComponent(node, kClassType)
  18. , m_spatial(this)
  19. , m_type(LightComponentType::kPoint)
  20. {
  21. m_point.m_radius = 1.0f;
  22. setLightComponentType(LightComponentType::kPoint);
  23. m_worldTransform = node->getWorldTransform();
  24. }
  25. LightComponent::~LightComponent()
  26. {
  27. deleteArray(SceneMemoryPool::getSingleton(), m_frustums, m_frustumCount);
  28. m_spatial.removeFromOctree(SceneGraph::getSingleton().getOctree());
  29. if(m_type == LightComponentType::kDirectional)
  30. {
  31. SceneGraph::getSingleton().removeDirectionalLight(this);
  32. }
  33. }
  34. void LightComponent::setLightComponentType(LightComponentType type)
  35. {
  36. ANKI_ASSERT(type >= LightComponentType::kFirst && type < LightComponentType::kCount);
  37. m_shapeUpdated = true;
  38. m_typeChanged = type != m_type;
  39. if(type == LightComponentType::kDirectional)
  40. {
  41. m_spatial.setAlwaysVisible(true);
  42. m_spatial.setUpdatesOctreeBounds(false);
  43. }
  44. else
  45. {
  46. m_spatial.setAlwaysVisible(false);
  47. m_spatial.setUpdatesOctreeBounds(true);
  48. }
  49. if(m_typeChanged)
  50. {
  51. if(type == LightComponentType::kDirectional)
  52. {
  53. // Now it's directional, inform the scene
  54. SceneGraph::getSingleton().addDirectionalLight(this);
  55. }
  56. else if(m_type == LightComponentType::kDirectional)
  57. {
  58. // It was directional, inform the scene
  59. SceneGraph::getSingleton().removeDirectionalLight(this);
  60. }
  61. }
  62. m_type = type;
  63. }
  64. Error LightComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  65. {
  66. const Bool typeChanged = m_typeChanged;
  67. const Bool moveUpdated = info.m_node->movedThisFrame() || typeChanged;
  68. const Bool shapeUpdated = m_shapeUpdated || typeChanged;
  69. updated = moveUpdated || shapeUpdated || typeChanged;
  70. m_shapeUpdated = false;
  71. m_typeChanged = false;
  72. if(moveUpdated)
  73. {
  74. m_worldTransform = info.m_node->getWorldTransform();
  75. }
  76. if(updated && m_type == LightComponentType::kPoint)
  77. {
  78. const Sphere sphere(m_worldTransform.getOrigin(), m_point.m_radius);
  79. m_spatial.setBoundingShape(sphere);
  80. if(m_shadow)
  81. {
  82. if(m_frustums == nullptr || m_frustumCount != 6) [[unlikely]]
  83. {
  84. // Allocate, initialize and update the frustums, just do everything to avoid bugs
  85. deleteArray(SceneMemoryPool::getSingleton(), m_frustums, m_frustumCount);
  86. m_frustums = newArray<Frustum>(SceneMemoryPool::getSingleton(), 6);
  87. m_frustumCount = 6;
  88. for(U32 i = 0; i < 6; i++)
  89. {
  90. m_frustums[i].init(FrustumType::kPerspective);
  91. m_frustums[i].setPerspective(kClusterObjectFrustumNearPlane, m_point.m_radius, kPi / 2.0f, kPi / 2.0f);
  92. m_frustums[i].setWorldTransform(Transform(m_worldTransform.getOrigin(), Frustum::getOmnidirectionalFrustumRotations()[i], 1.0f));
  93. }
  94. }
  95. // Update the frustums
  96. for(U32 i = 0; i < 6; i++)
  97. {
  98. if(shapeUpdated)
  99. {
  100. m_frustums[i].setFar(m_point.m_radius);
  101. }
  102. if(moveUpdated || shapeUpdated)
  103. {
  104. m_frustums[i].setWorldTransform(Transform(m_worldTransform.getOrigin(), Frustum::getOmnidirectionalFrustumRotations()[i], 1.0f));
  105. }
  106. }
  107. }
  108. if(m_shadow && shapeUpdated)
  109. {
  110. m_uuid = SceneGraph::getSingleton().getNewUuid();
  111. }
  112. else if(!m_shadow)
  113. {
  114. m_uuid = 0;
  115. }
  116. // Upload to the GPU scene
  117. GpuSceneLight gpuLight = {};
  118. gpuLight.m_position = m_worldTransform.getOrigin().xyz();
  119. gpuLight.m_radius = m_point.m_radius;
  120. gpuLight.m_diffuseColor = m_diffColor.xyz();
  121. gpuLight.m_squareRadiusOverOne = 1.0f / (m_point.m_radius * m_point.m_radius);
  122. gpuLight.m_flags = GpuSceneLightFlag::kPointLight;
  123. gpuLight.m_flags |= (m_shadow) ? GpuSceneLightFlag::kShadow : GpuSceneLightFlag::kNone;
  124. gpuLight.m_arrayIndex = getArrayIndex();
  125. gpuLight.m_uuid = m_uuid;
  126. if(!m_gpuSceneLight.isValid())
  127. {
  128. m_gpuSceneLight.allocate();
  129. }
  130. m_gpuSceneLight.uploadToGpuScene(gpuLight);
  131. }
  132. else if(updated && m_type == LightComponentType::kSpot)
  133. {
  134. // Update texture matrix
  135. const Mat4 biasMat4(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);
  136. const Mat4 proj =
  137. Mat4::calculatePerspectiveProjectionMatrix(m_spot.m_outerAngle, m_spot.m_outerAngle, kClusterObjectFrustumNearPlane, m_spot.m_distance);
  138. m_spot.m_textureMat = biasMat4 * proj * Mat4(m_worldTransform.getInverse());
  139. // Update the spatial
  140. Array<Vec4, 4> points;
  141. computeEdgesOfFrustum(m_spot.m_distance, m_spot.m_outerAngle, m_spot.m_outerAngle, &points[0]);
  142. Array<Vec3, 5> worldPoints;
  143. for(U32 i = 0; i < 4; ++i)
  144. {
  145. m_spot.m_edgePointsWspace[i] = m_worldTransform.transform(points[i].xyz());
  146. worldPoints[i] = m_spot.m_edgePointsWspace[i].xyz();
  147. }
  148. worldPoints[4] = m_worldTransform.getOrigin().xyz();
  149. m_spatial.setBoundingShape(ConstWeakArray<Vec3>(worldPoints));
  150. if(m_shadow)
  151. {
  152. if(m_frustums == nullptr || m_frustumCount != 1) [[unlikely]]
  153. {
  154. // Allocate, initialize and update the frustums, just do everything to avoid bugs
  155. deleteArray(SceneMemoryPool::getSingleton(), m_frustums, m_frustumCount);
  156. m_frustums = newArray<Frustum>(SceneMemoryPool::getSingleton(), 1);
  157. m_frustumCount = 1;
  158. m_frustums[0].init(FrustumType::kPerspective);
  159. m_frustums[0].setPerspective(kClusterObjectFrustumNearPlane, m_spot.m_distance, m_spot.m_outerAngle, m_spot.m_outerAngle);
  160. m_frustums[0].setWorldTransform(m_worldTransform);
  161. }
  162. // Update the frustum
  163. if(shapeUpdated)
  164. {
  165. m_frustums[0].setFar(m_spot.m_distance);
  166. m_frustums[0].setFovX(m_spot.m_outerAngle);
  167. m_frustums[0].setFovY(m_spot.m_outerAngle);
  168. }
  169. if(moveUpdated)
  170. {
  171. m_frustums[0].setWorldTransform(m_worldTransform);
  172. }
  173. }
  174. if(m_shadow && shapeUpdated)
  175. {
  176. m_uuid = SceneGraph::getSingleton().getNewUuid();
  177. }
  178. else if(!m_shadow)
  179. {
  180. m_uuid = 0;
  181. }
  182. // Upload to the GPU scene
  183. GpuSceneLight gpuLight = {};
  184. gpuLight.m_position = m_worldTransform.getOrigin().xyz();
  185. for(U32 i = 0; i < 4; ++i)
  186. {
  187. gpuLight.m_edgePoints[i] = m_spot.m_edgePointsWspace[i].xyz0();
  188. }
  189. gpuLight.m_diffuseColor = m_diffColor.xyz();
  190. gpuLight.m_radius = m_spot.m_distance;
  191. gpuLight.m_direction = -m_worldTransform.getRotation().getZAxis();
  192. gpuLight.m_squareRadiusOverOne = 1.0f / (m_spot.m_distance * m_spot.m_distance);
  193. gpuLight.m_flags = GpuSceneLightFlag::kSpotLight;
  194. gpuLight.m_flags |= (m_shadow) ? GpuSceneLightFlag::kShadow : GpuSceneLightFlag::kNone;
  195. gpuLight.m_outerCos = cos(m_spot.m_outerAngle / 2.0f);
  196. gpuLight.m_innerCos = cos(m_spot.m_innerAngle / 2.0f);
  197. gpuLight.m_uuid = m_uuid;
  198. if(!m_gpuSceneLight.isValid())
  199. {
  200. m_gpuSceneLight.allocate();
  201. }
  202. m_gpuSceneLight.uploadToGpuScene(gpuLight);
  203. }
  204. else if(m_type == LightComponentType::kDirectional)
  205. {
  206. // Update the scene bounds always
  207. SceneGraph::getSingleton().getOctree().getActualSceneBounds(m_dir.m_sceneMin, m_dir.m_sceneMax);
  208. m_gpuSceneLight.free();
  209. }
  210. const Bool spatialUpdated = m_spatial.update(SceneGraph::getSingleton().getOctree());
  211. updated = updated || spatialUpdated;
  212. if(m_shadow)
  213. {
  214. for(U32 i = 0; i < m_frustumCount; ++i)
  215. {
  216. const Bool frustumUpdated = m_frustums[i].update();
  217. updated = updated || frustumUpdated;
  218. }
  219. }
  220. return Error::kNone;
  221. }
  222. void LightComponent::setupDirectionalLightQueueElement(const Frustum& primaryFrustum, DirectionalLightQueueElement& el,
  223. WeakArray<Frustum> cascadeFrustums) const
  224. {
  225. ANKI_ASSERT(m_type == LightComponentType::kDirectional);
  226. ANKI_ASSERT(cascadeFrustums.getSize() <= kMaxShadowCascades);
  227. const U32 shadowCascadeCount = cascadeFrustums.getSize();
  228. el.m_uuid = m_uuid;
  229. el.m_diffuseColor = m_diffColor.xyz();
  230. el.m_direction = -m_worldTransform.getRotation().getZAxis().xyz();
  231. for(U32 i = 0; i < shadowCascadeCount; ++i)
  232. {
  233. el.m_shadowCascadesDistances[i] = primaryFrustum.getShadowCascadeDistance(i);
  234. }
  235. el.m_shadowCascadeCount = U8(shadowCascadeCount);
  236. el.m_shadowLayer = kMaxU8;
  237. if(shadowCascadeCount == 0)
  238. {
  239. return;
  240. }
  241. // Compute the texture matrices
  242. const Mat4 lightTrf(m_worldTransform);
  243. if(primaryFrustum.getFrustumType() == FrustumType::kPerspective)
  244. {
  245. // Get some stuff
  246. const F32 fovX = primaryFrustum.getFovX();
  247. const F32 fovY = primaryFrustum.getFovY();
  248. // Compute a sphere per cascade
  249. Array<Sphere, kMaxShadowCascades> boundingSpheres;
  250. for(U32 i = 0; i < shadowCascadeCount; ++i)
  251. {
  252. // Compute the center of the sphere
  253. // ^ z
  254. // |
  255. // ----------|---------- A(a, -f)
  256. // \ | /
  257. // \ | /
  258. // \ C(0,z) /
  259. // \ | /
  260. // \ | /
  261. // \---|---/ B(b, -n)
  262. // \ | /
  263. // \ | /
  264. // v
  265. // --------------------------> x
  266. // |
  267. // The square distance of A-C is equal to B-C. Solve the equation to find the z.
  268. const F32 f = primaryFrustum.getShadowCascadeDistance(i); // Cascade far
  269. const F32 n = (i == 0) ? primaryFrustum.getNear() : primaryFrustum.getShadowCascadeDistance(i - 1); // Cascade near
  270. const F32 a = f * tan(fovY / 2.0f) * fovX / fovY;
  271. const F32 b = n * tan(fovY / 2.0f) * fovX / fovY;
  272. const F32 z = (b * b + n * n - a * a - f * f) / (2.0f * (f - n));
  273. ANKI_ASSERT(absolute((Vec2(a, -f) - Vec2(0, z)).getLength() - (Vec2(b, -n) - Vec2(0, z)).getLength()) <= kEpsilonf * 100.0f);
  274. Vec3 C(0.0f, 0.0f, z); // Sphere center
  275. // Compute the radius of the sphere
  276. const Vec3 A(a, tan(fovY / 2.0f) * f, -f);
  277. const F32 r = (A - C).getLength();
  278. // Set the sphere
  279. boundingSpheres[i].setRadius(r);
  280. boundingSpheres[i].setCenter(primaryFrustum.getWorldTransform().transform(C));
  281. }
  282. // Compute the matrices
  283. for(U32 i = 0; i < shadowCascadeCount; ++i)
  284. {
  285. const Sphere& sphere = boundingSpheres[i];
  286. const Vec3 sphereCenter = sphere.getCenter().xyz();
  287. const F32 sphereRadius = sphere.getRadius();
  288. const Vec3& lightDir = el.m_direction;
  289. const Vec3 sceneMin = m_dir.m_sceneMin - Vec3(sphereRadius); // Push the bounds a bit
  290. const Vec3 sceneMax = m_dir.m_sceneMax + Vec3(sphereRadius);
  291. // Compute the intersections with the scene bounds
  292. Vec3 eye;
  293. if(sphereCenter > sceneMin && sphereCenter < sceneMax)
  294. {
  295. // Inside the scene bounds
  296. const Aabb sceneBox(sceneMin, sceneMax);
  297. const F32 t = testCollisionInside(sceneBox, Ray(sphereCenter, -lightDir));
  298. eye = sphereCenter + t * (-lightDir);
  299. }
  300. else
  301. {
  302. eye = sphereCenter + sphereRadius * (-lightDir);
  303. }
  304. // Projection
  305. const F32 far = (eye - sphereCenter).getLength() + sphereRadius;
  306. Mat4 cascadeProjMat = Mat4::calculateOrthographicProjectionMatrix(sphereRadius, -sphereRadius, sphereRadius, -sphereRadius,
  307. kClusterObjectFrustumNearPlane, far);
  308. // View
  309. Transform cascadeTransform = m_worldTransform;
  310. cascadeTransform.setOrigin(eye.xyz0());
  311. const Mat4 cascadeViewMat = Mat4(cascadeTransform.getInverse());
  312. // Now it's time to stabilize the shadows by aligning the projection matrix
  313. {
  314. // Project a random fixed point to the light matrix
  315. const Vec4 randomPointAlmostLightSpace = (cascadeProjMat * cascadeViewMat) * Vec3(0.0f).xyz1();
  316. // Chose a random low shadowmap size and align the random point
  317. const F32 shadowmapSize = 128.0f;
  318. const F32 shadowmapSize2 = shadowmapSize / 2.0f; // Div with 2 because the projected point is in NDC
  319. const F32 alignedX = std::round(randomPointAlmostLightSpace.x() * shadowmapSize2) / shadowmapSize2;
  320. const F32 alignedY = std::round(randomPointAlmostLightSpace.y() * shadowmapSize2) / shadowmapSize2;
  321. const F32 dx = alignedX - randomPointAlmostLightSpace.x();
  322. const F32 dy = alignedY - randomPointAlmostLightSpace.y();
  323. // Fix the projection matrix by applying an offset
  324. Mat4 correctionTranslationMat = Mat4::getIdentity();
  325. correctionTranslationMat.setTranslationPart(Vec4(dx, dy, 0, 1.0f));
  326. cascadeProjMat = correctionTranslationMat * cascadeProjMat;
  327. }
  328. // Light matrix
  329. const Mat4 biasMat4(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);
  330. el.m_viewProjectionMatrices[i] = cascadeProjMat * cascadeViewMat;
  331. el.m_textureMatrices[i] = biasMat4 * el.m_viewProjectionMatrices[i];
  332. // Fill the frustum with the fixed projection parameters from the fixed projection matrix
  333. Plane plane;
  334. extractClipPlane(cascadeProjMat, FrustumPlaneType::kLeft, plane);
  335. const F32 left = plane.getOffset();
  336. extractClipPlane(cascadeProjMat, FrustumPlaneType::kRight, plane);
  337. const F32 right = -plane.getOffset();
  338. extractClipPlane(cascadeProjMat, FrustumPlaneType::kTop, plane);
  339. const F32 top = -plane.getOffset();
  340. extractClipPlane(cascadeProjMat, FrustumPlaneType::kBottom, plane);
  341. const F32 bottom = plane.getOffset();
  342. Frustum& cascadeFrustum = cascadeFrustums[i];
  343. cascadeFrustum.init(FrustumType::kOrthographic);
  344. cascadeFrustum.setOrthographic(kClusterObjectFrustumNearPlane, far, right, left, top, bottom);
  345. cascadeFrustum.setWorldTransform(cascadeTransform);
  346. [[maybe_unused]] const Bool updated = cascadeFrustum.update();
  347. ANKI_ASSERT(updated);
  348. }
  349. }
  350. else
  351. {
  352. ANKI_ASSERT(!"TODO");
  353. }
  354. }
  355. void LightComponent::computeCascadeFrustums(const Frustum& primaryFrustum, ConstWeakArray<F32> cascadeDistances, WeakArray<Mat4> cascadeViewProjMats,
  356. WeakArray<Mat3x4> cascadeViewMats) const
  357. {
  358. ANKI_ASSERT(m_type == LightComponentType::kDirectional);
  359. ANKI_ASSERT(m_shadow);
  360. ANKI_ASSERT(cascadeViewProjMats.getSize() <= kMaxShadowCascades && cascadeViewProjMats.getSize() > 0);
  361. ANKI_ASSERT(cascadeDistances.getSize() == cascadeViewProjMats.getSize());
  362. const U32 shadowCascadeCount = cascadeViewProjMats.getSize();
  363. // Compute the texture matrices
  364. const Mat4 lightTrf(m_worldTransform);
  365. if(primaryFrustum.getFrustumType() == FrustumType::kPerspective)
  366. {
  367. // Get some stuff
  368. const F32 fovX = primaryFrustum.getFovX();
  369. const F32 fovY = primaryFrustum.getFovY();
  370. // Compute a sphere per cascade
  371. Array<Sphere, kMaxShadowCascades> boundingSpheres;
  372. for(U32 cascade = 0; cascade < shadowCascadeCount; ++cascade)
  373. {
  374. // Compute the center of the sphere
  375. // ^ z
  376. // |
  377. // ----------|---------- A(a, -f)
  378. // \ | /
  379. // \ | /
  380. // \ C(0,z) /
  381. // \ | /
  382. // \ | /
  383. // \---|---/ B(b, -n)
  384. // \ | /
  385. // \ | /
  386. // v
  387. // --------------------------> x
  388. // |
  389. // The square distance of A-C is equal to B-C. Solve the equation to find the z.
  390. const F32 f = cascadeDistances[cascade]; // Cascade far
  391. const F32 n = (cascade == 0) ? primaryFrustum.getNear() : cascadeDistances[cascade - 1]; // Cascade near
  392. const F32 a = f * tan(fovY / 2.0f) * fovX / fovY;
  393. const F32 b = n * tan(fovY / 2.0f) * fovX / fovY;
  394. const F32 z = (b * b + n * n - a * a - f * f) / (2.0f * (f - n));
  395. ANKI_ASSERT(absolute((Vec2(a, -f) - Vec2(0, z)).getLength() - (Vec2(b, -n) - Vec2(0, z)).getLength()) <= kEpsilonf * 100.0f);
  396. Vec3 C(0.0f, 0.0f, z); // Sphere center
  397. // Compute the radius of the sphere
  398. const Vec3 A(a, tan(fovY / 2.0f) * f, -f);
  399. const F32 r = (A - C).getLength();
  400. // Set the sphere
  401. boundingSpheres[cascade].setRadius(r);
  402. boundingSpheres[cascade].setCenter(primaryFrustum.getWorldTransform().transform(C));
  403. }
  404. // Compute the matrices
  405. for(U32 cascade = 0; cascade < shadowCascadeCount; ++cascade)
  406. {
  407. const Sphere& sphere = boundingSpheres[cascade];
  408. const Vec3 sphereCenter = sphere.getCenter().xyz();
  409. const F32 sphereRadius = sphere.getRadius();
  410. const Vec3& lightDir = getDirection();
  411. const Vec3 sceneMin = m_dir.m_sceneMin - Vec3(sphereRadius); // Push the bounds a bit
  412. const Vec3 sceneMax = m_dir.m_sceneMax + Vec3(sphereRadius);
  413. // Compute the intersections with the scene bounds
  414. Vec3 eye;
  415. if(sphereCenter > sceneMin && sphereCenter < sceneMax)
  416. {
  417. // Inside the scene bounds
  418. const Aabb sceneBox(sceneMin, sceneMax);
  419. const F32 t = testCollisionInside(sceneBox, Ray(sphereCenter, -lightDir));
  420. eye = sphereCenter + t * (-lightDir);
  421. }
  422. else
  423. {
  424. eye = sphereCenter + sphereRadius * (-lightDir);
  425. }
  426. // View
  427. Transform cascadeTransform = m_worldTransform;
  428. cascadeTransform.setOrigin(eye.xyz0());
  429. const Mat4 cascadeViewMat = Mat4(cascadeTransform.getInverse());
  430. // Projection
  431. const F32 far = (eye - sphereCenter).getLength() + sphereRadius;
  432. Mat4 cascadeProjMat = Mat4::calculateOrthographicProjectionMatrix(sphereRadius, -sphereRadius, sphereRadius, -sphereRadius,
  433. kClusterObjectFrustumNearPlane, far);
  434. // Now it's time to stabilize the shadows by aligning the projection matrix
  435. {
  436. // Project a random fixed point to the light matrix
  437. const Vec4 randomPointAlmostLightSpace = (cascadeProjMat * cascadeViewMat) * Vec3(0.0f).xyz1();
  438. // Chose a random low shadowmap size and align the random point
  439. const F32 shadowmapSize = 128.0f;
  440. const F32 shadowmapSize2 = shadowmapSize / 2.0f; // Div with 2 because the projected point is in NDC
  441. const F32 alignedX = std::round(randomPointAlmostLightSpace.x() * shadowmapSize2) / shadowmapSize2;
  442. const F32 alignedY = std::round(randomPointAlmostLightSpace.y() * shadowmapSize2) / shadowmapSize2;
  443. const F32 dx = alignedX - randomPointAlmostLightSpace.x();
  444. const F32 dy = alignedY - randomPointAlmostLightSpace.y();
  445. // Fix the projection matrix by applying an offset
  446. Mat4 correctionTranslationMat = Mat4::getIdentity();
  447. correctionTranslationMat.setTranslationPart(Vec4(dx, dy, 0, 1.0f));
  448. cascadeProjMat = correctionTranslationMat * cascadeProjMat;
  449. }
  450. // Write the results
  451. cascadeViewProjMats[cascade] = cascadeProjMat * cascadeViewMat;
  452. if(cascade < cascadeViewMats.getSize())
  453. {
  454. cascadeViewMats[cascade] = Mat3x4(cascadeViewMat);
  455. }
  456. }
  457. }
  458. else
  459. {
  460. ANKI_ASSERT(!"TODO");
  461. }
  462. }
  463. } // end namespace anki