18bf80f4bSopenharmony_ci/*
28bf80f4bSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License.
58bf80f4bSopenharmony_ci * You may obtain a copy of the License at
68bf80f4bSopenharmony_ci *
78bf80f4bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88bf80f4bSopenharmony_ci *
98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and
138bf80f4bSopenharmony_ci * limitations under the License.
148bf80f4bSopenharmony_ci */
158bf80f4bSopenharmony_ci
168bf80f4bSopenharmony_ci#include "picking.h"
178bf80f4bSopenharmony_ci
188bf80f4bSopenharmony_ci#include <algorithm>
198bf80f4bSopenharmony_ci#include <cinttypes>
208bf80f4bSopenharmony_ci#include <limits>
218bf80f4bSopenharmony_ci
228bf80f4bSopenharmony_ci#include <3d/ecs/components/camera_component.h>
238bf80f4bSopenharmony_ci#include <3d/ecs/components/joint_matrices_component.h>
248bf80f4bSopenharmony_ci#include <3d/ecs/components/layer_component.h>
258bf80f4bSopenharmony_ci#include <3d/ecs/components/mesh_component.h>
268bf80f4bSopenharmony_ci#include <3d/ecs/components/render_mesh_component.h>
278bf80f4bSopenharmony_ci#include <3d/ecs/components/transform_component.h>
288bf80f4bSopenharmony_ci#include <3d/ecs/components/world_matrix_component.h>
298bf80f4bSopenharmony_ci#include <3d/ecs/systems/intf_node_system.h>
308bf80f4bSopenharmony_ci#include <base/containers/fixed_string.h>
318bf80f4bSopenharmony_ci#include <base/math/mathf.h>
328bf80f4bSopenharmony_ci#include <base/math/matrix_util.h>
338bf80f4bSopenharmony_ci#include <base/math/vector_util.h>
348bf80f4bSopenharmony_ci#include <core/ecs/intf_ecs.h>
358bf80f4bSopenharmony_ci#include <core/implementation_uids.h>
368bf80f4bSopenharmony_ci#include <core/namespace.h>
378bf80f4bSopenharmony_ci#include <core/plugin/intf_class_factory.h>
388bf80f4bSopenharmony_ci#include <core/plugin/intf_class_register.h>
398bf80f4bSopenharmony_ci#include <core/plugin/intf_plugin_register.h>
408bf80f4bSopenharmony_ci#include <core/property/intf_property_handle.h>
418bf80f4bSopenharmony_ci
428bf80f4bSopenharmony_ci#include "util/scene_util.h"
438bf80f4bSopenharmony_ci
448bf80f4bSopenharmony_ciCORE3D_BEGIN_NAMESPACE()
458bf80f4bSopenharmony_ciusing namespace BASE_NS;
468bf80f4bSopenharmony_ciusing namespace CORE_NS;
478bf80f4bSopenharmony_ciusing namespace RENDER_NS;
488bf80f4bSopenharmony_ci
498bf80f4bSopenharmony_cinamespace {
508bf80f4bSopenharmony_ciMinAndMax GetWorldAABB(const Math::Mat4X4& world, const Math::Vec3& aabbMin, const Math::Vec3& aabbMax)
518bf80f4bSopenharmony_ci{
528bf80f4bSopenharmony_ci    // Based on https://gist.github.com/cmf028/81e8d3907035640ee0e3fdd69ada543f
538bf80f4bSopenharmony_ci    const auto center = (aabbMin + aabbMax) * 0.5f;
548bf80f4bSopenharmony_ci    const auto extents = aabbMax - center;
558bf80f4bSopenharmony_ci
568bf80f4bSopenharmony_ci    const auto centerW = Math::MultiplyPoint3X4(world, center);
578bf80f4bSopenharmony_ci
588bf80f4bSopenharmony_ci    Math::Mat3X3 absWorld;
598bf80f4bSopenharmony_ci    for (auto i = 0U; i < countof(absWorld.base); ++i) {
608bf80f4bSopenharmony_ci        absWorld.base[i].x = Math::abs(world.base[i].x);
618bf80f4bSopenharmony_ci        absWorld.base[i].y = Math::abs(world.base[i].y);
628bf80f4bSopenharmony_ci        absWorld.base[i].z = Math::abs(world.base[i].z);
638bf80f4bSopenharmony_ci    }
648bf80f4bSopenharmony_ci
658bf80f4bSopenharmony_ci    Math::Vec3 extentsW;
668bf80f4bSopenharmony_ci    extentsW.x = absWorld.x.x * extents.x + absWorld.y.x * extents.y + absWorld.z.x * extents.z;
678bf80f4bSopenharmony_ci    extentsW.y = absWorld.x.y * extents.x + absWorld.y.y * extents.y + absWorld.z.y * extents.z;
688bf80f4bSopenharmony_ci    extentsW.z = absWorld.x.z * extents.x + absWorld.y.z * extents.y + absWorld.z.z * extents.z;
698bf80f4bSopenharmony_ci
708bf80f4bSopenharmony_ci    return MinAndMax { centerW - extentsW, centerW + extentsW };
718bf80f4bSopenharmony_ci}
728bf80f4bSopenharmony_ci
738bf80f4bSopenharmony_ciconstexpr bool IntersectAabb(
748bf80f4bSopenharmony_ci    Math::Vec3 aabbMin, Math::Vec3 aabbMax, Math::Vec3 start, Math::Vec3 invDirection, float& hitDistance)
758bf80f4bSopenharmony_ci{
768bf80f4bSopenharmony_ci    const float tx1 = (aabbMin.x - start.x) * invDirection.x;
778bf80f4bSopenharmony_ci    const float tx2 = (aabbMax.x - start.x) * invDirection.x;
788bf80f4bSopenharmony_ci
798bf80f4bSopenharmony_ci    float tmin = Math::min(tx1, tx2);
808bf80f4bSopenharmony_ci    float tmax = Math::max(tx1, tx2);
818bf80f4bSopenharmony_ci
828bf80f4bSopenharmony_ci    const float ty1 = (aabbMin.y - start.y) * invDirection.y;
838bf80f4bSopenharmony_ci    const float ty2 = (aabbMax.y - start.y) * invDirection.y;
848bf80f4bSopenharmony_ci
858bf80f4bSopenharmony_ci    tmin = Math::max(tmin, Math::min(ty1, ty2));
868bf80f4bSopenharmony_ci    tmax = Math::min(tmax, Math::max(ty1, ty2));
878bf80f4bSopenharmony_ci
888bf80f4bSopenharmony_ci    const float tz1 = (aabbMin.z - start.z) * invDirection.z;
898bf80f4bSopenharmony_ci    const float tz2 = (aabbMax.z - start.z) * invDirection.z;
908bf80f4bSopenharmony_ci
918bf80f4bSopenharmony_ci    tmin = Math::max(tmin, Math::min(tz1, tz2));
928bf80f4bSopenharmony_ci    tmax = Math::min(tmax, Math::max(tz1, tz2));
938bf80f4bSopenharmony_ci
948bf80f4bSopenharmony_ci    hitDistance = tmin;
958bf80f4bSopenharmony_ci    // If hitDistance < 0, ray origin was inside the AABB.
968bf80f4bSopenharmony_ci    if (hitDistance < 0.0f) {
978bf80f4bSopenharmony_ci        hitDistance = tmax;
988bf80f4bSopenharmony_ci        // No hit, set distance to 0.
998bf80f4bSopenharmony_ci        if (hitDistance < 0.0f) {
1008bf80f4bSopenharmony_ci            hitDistance = 0.0f;
1018bf80f4bSopenharmony_ci        }
1028bf80f4bSopenharmony_ci    }
1038bf80f4bSopenharmony_ci
1048bf80f4bSopenharmony_ci    return tmax >= tmin && tmax > 0.0f;
1058bf80f4bSopenharmony_ci}
1068bf80f4bSopenharmony_ci
1078bf80f4bSopenharmony_cibool IntersectTriangle(const Math::Vec3 triangle[3], const Math::Vec3 start, const Math::Vec3 direction,
1088bf80f4bSopenharmony_ci    float& hitDistance, Math::Vec2& uv)
1098bf80f4bSopenharmony_ci{
1108bf80f4bSopenharmony_ci    const Math::Vec3 v0v1 = triangle[1] - triangle[0];
1118bf80f4bSopenharmony_ci    const Math::Vec3 v0v2 = triangle[2] - triangle[0];
1128bf80f4bSopenharmony_ci
1138bf80f4bSopenharmony_ci    const Math::Vec3 pvec = Math::Cross(direction, v0v2);
1148bf80f4bSopenharmony_ci    const float det = Math::Dot(v0v1, pvec);
1158bf80f4bSopenharmony_ci
1168bf80f4bSopenharmony_ci    // ray and triangle are parallel and backface culling
1178bf80f4bSopenharmony_ci    if (det < Math::EPSILON) {
1188bf80f4bSopenharmony_ci        hitDistance = 0.f;
1198bf80f4bSopenharmony_ci        return false;
1208bf80f4bSopenharmony_ci    }
1218bf80f4bSopenharmony_ci
1228bf80f4bSopenharmony_ci    const float invDet = 1.f / det;
1238bf80f4bSopenharmony_ci    const Math::Vec3 tvec = start - triangle[0];
1248bf80f4bSopenharmony_ci
1258bf80f4bSopenharmony_ci    const float u = Math::Dot(tvec, pvec) * invDet;
1268bf80f4bSopenharmony_ci    if (u < 0 || u > 1) {
1278bf80f4bSopenharmony_ci        hitDistance = 0.f;
1288bf80f4bSopenharmony_ci        uv = Math::Vec2(0, 0);
1298bf80f4bSopenharmony_ci        return false;
1308bf80f4bSopenharmony_ci    }
1318bf80f4bSopenharmony_ci
1328bf80f4bSopenharmony_ci    const Math::Vec3 qvec = Math::Cross(tvec, v0v1);
1338bf80f4bSopenharmony_ci    const float v = Math::Dot(direction, qvec) * invDet;
1348bf80f4bSopenharmony_ci    if (v < 0 || u + v > 1) {
1358bf80f4bSopenharmony_ci        hitDistance = 0.f;
1368bf80f4bSopenharmony_ci        uv = Math::Vec2(0, 0);
1378bf80f4bSopenharmony_ci        return false;
1388bf80f4bSopenharmony_ci    }
1398bf80f4bSopenharmony_ci
1408bf80f4bSopenharmony_ci    hitDistance = Math::Dot(v0v2, qvec) * invDet;
1418bf80f4bSopenharmony_ci    uv = Math::Vec2(u, v);
1428bf80f4bSopenharmony_ci    return true;
1438bf80f4bSopenharmony_ci}
1448bf80f4bSopenharmony_ci
1458bf80f4bSopenharmony_ci// Calculates AABB using WorldMatrixComponent.
1468bf80f4bSopenharmony_civoid UpdateRecursiveAABB(const IRenderMeshComponentManager& renderMeshComponentManager,
1478bf80f4bSopenharmony_ci    const IWorldMatrixComponentManager& worldMatrixComponentManager,
1488bf80f4bSopenharmony_ci    const IJointMatricesComponentManager& jointMatricesComponentManager, const IMeshComponentManager& meshManager,
1498bf80f4bSopenharmony_ci    const ISceneNode& sceneNode, bool isRecursive, MinAndMax& mamInOut)
1508bf80f4bSopenharmony_ci{
1518bf80f4bSopenharmony_ci    const Entity entity = sceneNode.GetEntity();
1528bf80f4bSopenharmony_ci    if (const auto jointMatrices = jointMatricesComponentManager.Read(entity); jointMatrices) {
1538bf80f4bSopenharmony_ci        // Take skinning into account.
1548bf80f4bSopenharmony_ci        mamInOut.minAABB = Math::min(mamInOut.minAABB, jointMatrices->jointsAabbMin);
1558bf80f4bSopenharmony_ci        mamInOut.maxAABB = Math::max(mamInOut.maxAABB, jointMatrices->jointsAabbMax);
1568bf80f4bSopenharmony_ci    } else {
1578bf80f4bSopenharmony_ci        const auto worldMatrixId = worldMatrixComponentManager.GetComponentId(entity);
1588bf80f4bSopenharmony_ci        const auto renderMeshId = renderMeshComponentManager.GetComponentId(entity);
1598bf80f4bSopenharmony_ci        if (worldMatrixId != IComponentManager::INVALID_COMPONENT_ID &&
1608bf80f4bSopenharmony_ci            renderMeshId != IComponentManager::INVALID_COMPONENT_ID) {
1618bf80f4bSopenharmony_ci            const Math::Mat4X4& worldMatrix = worldMatrixComponentManager.Get(worldMatrixId).matrix;
1628bf80f4bSopenharmony_ci
1638bf80f4bSopenharmony_ci            const RenderMeshComponent rmc = renderMeshComponentManager.Get(renderMeshId);
1648bf80f4bSopenharmony_ci            if (const auto meshHandle = meshManager.Read(rmc.mesh); meshHandle) {
1658bf80f4bSopenharmony_ci                const MinAndMax meshMam = GetWorldAABB(worldMatrix, meshHandle->aabbMin, meshHandle->aabbMax);
1668bf80f4bSopenharmony_ci                mamInOut.minAABB = Math::min(mamInOut.minAABB, meshMam.minAABB);
1678bf80f4bSopenharmony_ci                mamInOut.maxAABB = Math::max(mamInOut.maxAABB, meshMam.maxAABB);
1688bf80f4bSopenharmony_ci            }
1698bf80f4bSopenharmony_ci        }
1708bf80f4bSopenharmony_ci    }
1718bf80f4bSopenharmony_ci
1728bf80f4bSopenharmony_ci    if (isRecursive) {
1738bf80f4bSopenharmony_ci        for (ISceneNode* child : sceneNode.GetChildren()) {
1748bf80f4bSopenharmony_ci            if (child) {
1758bf80f4bSopenharmony_ci                UpdateRecursiveAABB(renderMeshComponentManager, worldMatrixComponentManager,
1768bf80f4bSopenharmony_ci                    jointMatricesComponentManager, meshManager, *child, isRecursive, mamInOut);
1778bf80f4bSopenharmony_ci            }
1788bf80f4bSopenharmony_ci        }
1798bf80f4bSopenharmony_ci    }
1808bf80f4bSopenharmony_ci}
1818bf80f4bSopenharmony_ci
1828bf80f4bSopenharmony_ci// Calculates AABB using TransformComponent.
1838bf80f4bSopenharmony_civoid UpdateRecursiveAABB(const IRenderMeshComponentManager& renderMeshComponentManager,
1848bf80f4bSopenharmony_ci    const ITransformComponentManager& transformComponentManager, const IMeshComponentManager& meshManager,
1858bf80f4bSopenharmony_ci    const ISceneNode& sceneNode, const Math::Mat4X4& parentWorld, bool isRecursive, MinAndMax& mamInOut)
1868bf80f4bSopenharmony_ci{
1878bf80f4bSopenharmony_ci    const Entity entity = sceneNode.GetEntity();
1888bf80f4bSopenharmony_ci    Math::Mat4X4 worldMatrix = parentWorld;
1898bf80f4bSopenharmony_ci
1908bf80f4bSopenharmony_ci    if (const auto transformId = transformComponentManager.GetComponentId(entity);
1918bf80f4bSopenharmony_ci        transformId != IComponentManager::INVALID_COMPONENT_ID) {
1928bf80f4bSopenharmony_ci        const TransformComponent tc = transformComponentManager.Get(transformId);
1938bf80f4bSopenharmony_ci        const Math::Mat4X4 localMatrix = Math::Trs(tc.position, tc.rotation, tc.scale);
1948bf80f4bSopenharmony_ci        worldMatrix = worldMatrix * localMatrix;
1958bf80f4bSopenharmony_ci    }
1968bf80f4bSopenharmony_ci
1978bf80f4bSopenharmony_ci    if (const auto renderMeshId = renderMeshComponentManager.GetComponentId(entity);
1988bf80f4bSopenharmony_ci        renderMeshId != IComponentManager::INVALID_COMPONENT_ID) {
1998bf80f4bSopenharmony_ci        const RenderMeshComponent rmc = renderMeshComponentManager.Get(renderMeshId);
2008bf80f4bSopenharmony_ci        if (const auto meshHandle = meshManager.Read(rmc.mesh); meshHandle) {
2018bf80f4bSopenharmony_ci            const MinAndMax meshMam = GetWorldAABB(worldMatrix, meshHandle->aabbMin, meshHandle->aabbMax);
2028bf80f4bSopenharmony_ci            mamInOut.minAABB = Math::min(mamInOut.minAABB, meshMam.minAABB);
2038bf80f4bSopenharmony_ci            mamInOut.maxAABB = Math::max(mamInOut.maxAABB, meshMam.maxAABB);
2048bf80f4bSopenharmony_ci        }
2058bf80f4bSopenharmony_ci    }
2068bf80f4bSopenharmony_ci
2078bf80f4bSopenharmony_ci    // Recurse to children.
2088bf80f4bSopenharmony_ci    if (isRecursive) {
2098bf80f4bSopenharmony_ci        for (ISceneNode* child : sceneNode.GetChildren()) {
2108bf80f4bSopenharmony_ci            if (child) {
2118bf80f4bSopenharmony_ci                UpdateRecursiveAABB(renderMeshComponentManager, transformComponentManager, meshManager, *child,
2128bf80f4bSopenharmony_ci                    worldMatrix, isRecursive, mamInOut);
2138bf80f4bSopenharmony_ci            }
2148bf80f4bSopenharmony_ci        }
2158bf80f4bSopenharmony_ci    }
2168bf80f4bSopenharmony_ci}
2178bf80f4bSopenharmony_ci
2188bf80f4bSopenharmony_ciRayCastResult HitTestNode(ISceneNode& node, const MeshComponent& mesh, const Math::Mat4X4& matrix,
2198bf80f4bSopenharmony_ci    const Math::Vec3& start, const Math::Vec3& invDir)
2208bf80f4bSopenharmony_ci{
2218bf80f4bSopenharmony_ci    RayCastResult raycastResult;
2228bf80f4bSopenharmony_ci    const auto direction = Math::Vec3(1.f / invDir.x, 1.f / invDir.y, 1.f / invDir.z);
2238bf80f4bSopenharmony_ci
2248bf80f4bSopenharmony_ci    const MinAndMax meshMinMax = GetWorldAABB(matrix, mesh.aabbMin, mesh.aabbMax);
2258bf80f4bSopenharmony_ci    float distance = 0;
2268bf80f4bSopenharmony_ci    if (IntersectAabb(meshMinMax.minAABB, meshMinMax.maxAABB, start, invDir, distance)) {
2278bf80f4bSopenharmony_ci        if (mesh.submeshes.size() > 1) {
2288bf80f4bSopenharmony_ci            raycastResult.centerDistance = std::numeric_limits<float>::max();
2298bf80f4bSopenharmony_ci
2308bf80f4bSopenharmony_ci            for (auto const& submesh : mesh.submeshes) {
2318bf80f4bSopenharmony_ci                const MinAndMax submeshMinMax = GetWorldAABB(matrix, submesh.aabbMin, submesh.aabbMax);
2328bf80f4bSopenharmony_ci                if (IntersectAabb(submeshMinMax.minAABB, submeshMinMax.maxAABB, start, invDir, distance)) {
2338bf80f4bSopenharmony_ci                    const float centerDistance =
2348bf80f4bSopenharmony_ci                        Math::Magnitude((submeshMinMax.maxAABB + submeshMinMax.minAABB) / 2.f - start);
2358bf80f4bSopenharmony_ci                    if (centerDistance < raycastResult.centerDistance) {
2368bf80f4bSopenharmony_ci                        raycastResult.node = &node;
2378bf80f4bSopenharmony_ci                        raycastResult.centerDistance = centerDistance;
2388bf80f4bSopenharmony_ci                        raycastResult.distance = distance;
2398bf80f4bSopenharmony_ci                        raycastResult.worldPosition = start + direction * distance;
2408bf80f4bSopenharmony_ci                    }
2418bf80f4bSopenharmony_ci                }
2428bf80f4bSopenharmony_ci            }
2438bf80f4bSopenharmony_ci        } else {
2448bf80f4bSopenharmony_ci            raycastResult.centerDistance = Math::Magnitude((meshMinMax.minAABB + meshMinMax.maxAABB) / 2.f - start);
2458bf80f4bSopenharmony_ci            raycastResult.distance = distance;
2468bf80f4bSopenharmony_ci            raycastResult.node = &node;
2478bf80f4bSopenharmony_ci            raycastResult.worldPosition = start + direction * raycastResult.distance;
2488bf80f4bSopenharmony_ci        }
2498bf80f4bSopenharmony_ci    }
2508bf80f4bSopenharmony_ci
2518bf80f4bSopenharmony_ci    return raycastResult;
2528bf80f4bSopenharmony_ci}
2538bf80f4bSopenharmony_ci
2548bf80f4bSopenharmony_ciMath::Vec3 ScreenToWorld(const CameraComponent& cameraComponent, const WorldMatrixComponent& cameraWorldMatrixComponent,
2558bf80f4bSopenharmony_ci    Math::Vec3 screenCoordinate)
2568bf80f4bSopenharmony_ci{
2578bf80f4bSopenharmony_ci    screenCoordinate.x = (screenCoordinate.x - 0.5f) * 2.f;
2588bf80f4bSopenharmony_ci    screenCoordinate.y = (screenCoordinate.y - 0.5f) * 2.f;
2598bf80f4bSopenharmony_ci
2608bf80f4bSopenharmony_ci    bool isCameraNegative = false;
2618bf80f4bSopenharmony_ci    Math::Mat4X4 projToView =
2628bf80f4bSopenharmony_ci        Math::Inverse(CameraMatrixUtil::CalculateProjectionMatrix(cameraComponent, isCameraNegative));
2638bf80f4bSopenharmony_ci
2648bf80f4bSopenharmony_ci    auto const& worldFromView = cameraWorldMatrixComponent.matrix;
2658bf80f4bSopenharmony_ci    const auto viewCoordinate =
2668bf80f4bSopenharmony_ci        (projToView * Math::Vec4(screenCoordinate.x, screenCoordinate.y, screenCoordinate.z, 1.f));
2678bf80f4bSopenharmony_ci    auto worldCoordinate = worldFromView * viewCoordinate;
2688bf80f4bSopenharmony_ci    worldCoordinate /= worldCoordinate.w;
2698bf80f4bSopenharmony_ci    return Math::Vec3 { worldCoordinate.x, worldCoordinate.y, worldCoordinate.z };
2708bf80f4bSopenharmony_ci}
2718bf80f4bSopenharmony_ci
2728bf80f4bSopenharmony_cistruct Ray {
2738bf80f4bSopenharmony_ci    Math::Vec3 origin;
2748bf80f4bSopenharmony_ci    Math::Vec3 direction;
2758bf80f4bSopenharmony_ci};
2768bf80f4bSopenharmony_ci
2778bf80f4bSopenharmony_ciRay RayFromCamera(const CameraComponent& cameraComponent, const WorldMatrixComponent& cameraWorldMatrixComponent,
2788bf80f4bSopenharmony_ci    Math::Vec2 screenCoordinate)
2798bf80f4bSopenharmony_ci{
2808bf80f4bSopenharmony_ci    if (cameraComponent.projection == CORE3D_NS::CameraComponent::Projection::ORTHOGRAPHIC) {
2818bf80f4bSopenharmony_ci        const Math::Vec3 worldPos = CORE3D_NS::ScreenToWorld(
2828bf80f4bSopenharmony_ci            cameraComponent, cameraWorldMatrixComponent, Math::Vec3(screenCoordinate.x, screenCoordinate.y, 0.0f));
2838bf80f4bSopenharmony_ci        const auto direction = cameraWorldMatrixComponent.matrix * Math::Vec4(0.0f, 0.0f, -1.0f, 0.0f);
2848bf80f4bSopenharmony_ci        return Ray { worldPos, direction };
2858bf80f4bSopenharmony_ci    }
2868bf80f4bSopenharmony_ci    // Ray origin is the camera world position.
2878bf80f4bSopenharmony_ci    const Math::Vec3& rayOrigin = Math::Vec3(cameraWorldMatrixComponent.matrix.w);
2888bf80f4bSopenharmony_ci    const Math::Vec3 targetPos = CORE3D_NS::ScreenToWorld(
2898bf80f4bSopenharmony_ci        cameraComponent, cameraWorldMatrixComponent, Math::Vec3(screenCoordinate.x, screenCoordinate.y, 1.0f));
2908bf80f4bSopenharmony_ci    const Math::Vec3 direction = Math::Normalize(targetPos - rayOrigin);
2918bf80f4bSopenharmony_ci    return Ray { rayOrigin, direction };
2928bf80f4bSopenharmony_ci}
2938bf80f4bSopenharmony_ci} // namespace
2948bf80f4bSopenharmony_ci
2958bf80f4bSopenharmony_ciMath::Vec3 Picking::ScreenToWorld(IEcs const& ecs, Entity cameraEntity, Math::Vec3 screenCoordinate) const
2968bf80f4bSopenharmony_ci{
2978bf80f4bSopenharmony_ci    if (!EntityUtil::IsValid(cameraEntity)) {
2988bf80f4bSopenharmony_ci        return {};
2998bf80f4bSopenharmony_ci    }
3008bf80f4bSopenharmony_ci
3018bf80f4bSopenharmony_ci    auto cameraComponentManager = GetManager<ICameraComponentManager>(ecs);
3028bf80f4bSopenharmony_ci    const auto cameraId = cameraComponentManager->GetComponentId(cameraEntity);
3038bf80f4bSopenharmony_ci    if (cameraId == IComponentManager::INVALID_COMPONENT_ID) {
3048bf80f4bSopenharmony_ci        return {};
3058bf80f4bSopenharmony_ci    }
3068bf80f4bSopenharmony_ci
3078bf80f4bSopenharmony_ci    auto worldMatrixComponentManager = GetManager<IWorldMatrixComponentManager>(ecs);
3088bf80f4bSopenharmony_ci    const auto worldMatrixId = worldMatrixComponentManager->GetComponentId(cameraEntity);
3098bf80f4bSopenharmony_ci    if (worldMatrixId == IComponentManager::INVALID_COMPONENT_ID) {
3108bf80f4bSopenharmony_ci        return {};
3118bf80f4bSopenharmony_ci    }
3128bf80f4bSopenharmony_ci    return CORE3D_NS::ScreenToWorld(
3138bf80f4bSopenharmony_ci        *cameraComponentManager->Read(cameraId), worldMatrixComponentManager->Get(worldMatrixId), screenCoordinate);
3148bf80f4bSopenharmony_ci}
3158bf80f4bSopenharmony_ci
3168bf80f4bSopenharmony_ciMath::Vec3 Picking::WorldToScreen(IEcs const& ecs, Entity cameraEntity, Math::Vec3 worldCoordinate) const
3178bf80f4bSopenharmony_ci{
3188bf80f4bSopenharmony_ci    if (!EntityUtil::IsValid(cameraEntity)) {
3198bf80f4bSopenharmony_ci        return {};
3208bf80f4bSopenharmony_ci    }
3218bf80f4bSopenharmony_ci
3228bf80f4bSopenharmony_ci    auto cameraComponentManager = GetManager<ICameraComponentManager>(ecs);
3238bf80f4bSopenharmony_ci    const auto cameraId = cameraComponentManager->GetComponentId(cameraEntity);
3248bf80f4bSopenharmony_ci    if (cameraId == IComponentManager::INVALID_COMPONENT_ID) {
3258bf80f4bSopenharmony_ci        return {};
3268bf80f4bSopenharmony_ci    }
3278bf80f4bSopenharmony_ci
3288bf80f4bSopenharmony_ci    auto worldMatrixComponentManager = GetManager<IWorldMatrixComponentManager>(ecs);
3298bf80f4bSopenharmony_ci    const auto worldMatrixId = worldMatrixComponentManager->GetComponentId(cameraEntity);
3308bf80f4bSopenharmony_ci    if (worldMatrixId == IComponentManager::INVALID_COMPONENT_ID) {
3318bf80f4bSopenharmony_ci        return {};
3328bf80f4bSopenharmony_ci    }
3338bf80f4bSopenharmony_ci
3348bf80f4bSopenharmony_ci    const CameraComponent cameraComponent = cameraComponentManager->Get(cameraId);
3358bf80f4bSopenharmony_ci    bool isCameraNegative = false;
3368bf80f4bSopenharmony_ci    Math::Mat4X4 viewToProj = CameraMatrixUtil::CalculateProjectionMatrix(cameraComponent, isCameraNegative);
3378bf80f4bSopenharmony_ci
3388bf80f4bSopenharmony_ci    const WorldMatrixComponent worldMatrixComponent = worldMatrixComponentManager->Get(worldMatrixId);
3398bf80f4bSopenharmony_ci    auto const worldToView = Math::Inverse(worldMatrixComponent.matrix);
3408bf80f4bSopenharmony_ci    const auto viewCoordinate = worldToView * Math::Vec4(worldCoordinate.x, worldCoordinate.y, worldCoordinate.z, 1.f);
3418bf80f4bSopenharmony_ci    auto screenCoordinate = viewToProj * viewCoordinate;
3428bf80f4bSopenharmony_ci
3438bf80f4bSopenharmony_ci    // Give sane results also when the point is behind the camera.
3448bf80f4bSopenharmony_ci    if (screenCoordinate.w < 0.0f) {
3458bf80f4bSopenharmony_ci        screenCoordinate.x *= -1.0f;
3468bf80f4bSopenharmony_ci        screenCoordinate.y *= -1.0f;
3478bf80f4bSopenharmony_ci        screenCoordinate.z *= -1.0f;
3488bf80f4bSopenharmony_ci    }
3498bf80f4bSopenharmony_ci
3508bf80f4bSopenharmony_ci    screenCoordinate /= screenCoordinate.w;
3518bf80f4bSopenharmony_ci    screenCoordinate.x = screenCoordinate.x * 0.5f + 0.5f;
3528bf80f4bSopenharmony_ci    screenCoordinate.y = screenCoordinate.y * 0.5f + 0.5f;
3538bf80f4bSopenharmony_ci
3548bf80f4bSopenharmony_ci    return Math::Vec3 { screenCoordinate.x, screenCoordinate.y, screenCoordinate.z };
3558bf80f4bSopenharmony_ci}
3568bf80f4bSopenharmony_ci
3578bf80f4bSopenharmony_civector<RayCastResult> Picking::RayCast(const IEcs& ecs, const Math::Vec3& start, const Math::Vec3& direction) const
3588bf80f4bSopenharmony_ci{
3598bf80f4bSopenharmony_ci    vector<RayCastResult> result;
3608bf80f4bSopenharmony_ci
3618bf80f4bSopenharmony_ci    auto nodeSystem = GetSystem<INodeSystem>(ecs);
3628bf80f4bSopenharmony_ci    auto const& renderMeshComponentManager = GetManager<IRenderMeshComponentManager>(ecs);
3638bf80f4bSopenharmony_ci    auto const& worldMatrixComponentManager = GetManager<IWorldMatrixComponentManager>(ecs);
3648bf80f4bSopenharmony_ci    auto const& jointMatricesComponentManager = GetManager<IJointMatricesComponentManager>(ecs);
3658bf80f4bSopenharmony_ci    auto const& meshComponentManager = *GetManager<IMeshComponentManager>(ecs);
3668bf80f4bSopenharmony_ci    float distance = 0;
3678bf80f4bSopenharmony_ci
3688bf80f4bSopenharmony_ci    auto const invDir = DirectionVectorInverse(direction);
3698bf80f4bSopenharmony_ci    for (IComponentManager::ComponentId i = 0; i < renderMeshComponentManager->GetComponentCount(); i++) {
3708bf80f4bSopenharmony_ci        const Entity id = renderMeshComponentManager->GetEntity(i);
3718bf80f4bSopenharmony_ci        if (auto node = nodeSystem->GetNode(id); node) {
3728bf80f4bSopenharmony_ci            if (const auto jointMatrices = jointMatricesComponentManager->Read(id); jointMatrices) {
3738bf80f4bSopenharmony_ci                // Use the skinned aabb's.
3748bf80f4bSopenharmony_ci                const auto& jointMatricesComponent = *jointMatrices;
3758bf80f4bSopenharmony_ci                if (IntersectAabb(jointMatricesComponent.jointsAabbMin, jointMatricesComponent.jointsAabbMax, start,
3768bf80f4bSopenharmony_ci                        invDir, distance)) {
3778bf80f4bSopenharmony_ci                    const float centerDistance = Math::Magnitude(
3788bf80f4bSopenharmony_ci                        (jointMatricesComponent.jointsAabbMax + jointMatricesComponent.jointsAabbMin) * 0.5f - start);
3798bf80f4bSopenharmony_ci                    const Math::Vec3 hitPosition = start + direction * distance;
3808bf80f4bSopenharmony_ci                    result.push_back(RayCastResult { node, centerDistance, distance, hitPosition });
3818bf80f4bSopenharmony_ci                }
3828bf80f4bSopenharmony_ci                continue;
3838bf80f4bSopenharmony_ci            } else {
3848bf80f4bSopenharmony_ci                if (const auto worldMatrixId = worldMatrixComponentManager->GetComponentId(id);
3858bf80f4bSopenharmony_ci                    worldMatrixId != IComponentManager::INVALID_COMPONENT_ID) {
3868bf80f4bSopenharmony_ci                    auto const renderMeshComponent = renderMeshComponentManager->Get(i);
3878bf80f4bSopenharmony_ci                    if (const auto meshHandle = meshComponentManager.Read(renderMeshComponent.mesh); meshHandle) {
3888bf80f4bSopenharmony_ci                        auto const worldMatrixComponent = worldMatrixComponentManager->Get(worldMatrixId);
3898bf80f4bSopenharmony_ci                        const auto raycastResult =
3908bf80f4bSopenharmony_ci                            HitTestNode(*node, *meshHandle, worldMatrixComponent.matrix, start, invDir);
3918bf80f4bSopenharmony_ci                        if (raycastResult.node) {
3928bf80f4bSopenharmony_ci                            result.push_back(raycastResult);
3938bf80f4bSopenharmony_ci                        }
3948bf80f4bSopenharmony_ci                    } else {
3958bf80f4bSopenharmony_ci                        CORE_LOG_W("no mesh resource for entity %" PRIx64 ", resource %" PRIx64, id.id,
3968bf80f4bSopenharmony_ci                            renderMeshComponent.mesh.id);
3978bf80f4bSopenharmony_ci                        continue;
3988bf80f4bSopenharmony_ci                    }
3998bf80f4bSopenharmony_ci                }
4008bf80f4bSopenharmony_ci            }
4018bf80f4bSopenharmony_ci        }
4028bf80f4bSopenharmony_ci    }
4038bf80f4bSopenharmony_ci
4048bf80f4bSopenharmony_ci    std::sort(
4058bf80f4bSopenharmony_ci        result.begin(), result.end(), [](const auto& lhs, const auto& rhs) { return (lhs.distance < rhs.distance); });
4068bf80f4bSopenharmony_ci
4078bf80f4bSopenharmony_ci    return result;
4088bf80f4bSopenharmony_ci}
4098bf80f4bSopenharmony_ci
4108bf80f4bSopenharmony_civector<RayCastResult> Picking::RayCast(
4118bf80f4bSopenharmony_ci    const IEcs& ecs, const Math::Vec3& start, const Math::Vec3& direction, uint64_t layerMask) const
4128bf80f4bSopenharmony_ci{
4138bf80f4bSopenharmony_ci    vector<RayCastResult> result;
4148bf80f4bSopenharmony_ci
4158bf80f4bSopenharmony_ci    auto nodeSystem = GetSystem<INodeSystem>(ecs);
4168bf80f4bSopenharmony_ci    auto const& renderMeshComponentManager = GetManager<IRenderMeshComponentManager>(ecs);
4178bf80f4bSopenharmony_ci    auto const& layerComponentManager = GetManager<ILayerComponentManager>(ecs);
4188bf80f4bSopenharmony_ci    auto const& worldMatrixComponentManager = GetManager<IWorldMatrixComponentManager>(ecs);
4198bf80f4bSopenharmony_ci    auto const& jointMatricesComponentManager = GetManager<IJointMatricesComponentManager>(ecs);
4208bf80f4bSopenharmony_ci    auto const& meshComponentManager = *GetManager<IMeshComponentManager>(ecs);
4218bf80f4bSopenharmony_ci
4228bf80f4bSopenharmony_ci    auto const invDir = DirectionVectorInverse(direction);
4238bf80f4bSopenharmony_ci    float distance = 0;
4248bf80f4bSopenharmony_ci    for (IComponentManager::ComponentId i = 0; i < renderMeshComponentManager->GetComponentCount(); i++) {
4258bf80f4bSopenharmony_ci        const Entity id = renderMeshComponentManager->GetEntity(i);
4268bf80f4bSopenharmony_ci        if (auto node = nodeSystem->GetNode(id); node) {
4278bf80f4bSopenharmony_ci            if (layerComponentManager->Get(id).layerMask & layerMask) {
4288bf80f4bSopenharmony_ci                if (const auto jointMatrices = jointMatricesComponentManager->Read(id); jointMatrices) {
4298bf80f4bSopenharmony_ci                    // Use the skinned aabb's.
4308bf80f4bSopenharmony_ci                    const auto& jointMatricesComponent = *jointMatrices;
4318bf80f4bSopenharmony_ci                    if (IntersectAabb(jointMatricesComponent.jointsAabbMin, jointMatricesComponent.jointsAabbMax, start,
4328bf80f4bSopenharmony_ci                            invDir, distance)) {
4338bf80f4bSopenharmony_ci                        const float centerDistance = Math::Magnitude(
4348bf80f4bSopenharmony_ci                            (jointMatricesComponent.jointsAabbMax + jointMatricesComponent.jointsAabbMin) * 0.5f -
4358bf80f4bSopenharmony_ci                            start);
4368bf80f4bSopenharmony_ci                        const Math::Vec3 hitPosition = start + direction * distance;
4378bf80f4bSopenharmony_ci                        result.push_back(RayCastResult { node, centerDistance, distance, hitPosition });
4388bf80f4bSopenharmony_ci                    }
4398bf80f4bSopenharmony_ci                } else {
4408bf80f4bSopenharmony_ci                    if (const auto worldMatrixId = worldMatrixComponentManager->GetComponentId(id);
4418bf80f4bSopenharmony_ci                        worldMatrixId != IComponentManager::INVALID_COMPONENT_ID) {
4428bf80f4bSopenharmony_ci                        auto const renderMeshComponent = renderMeshComponentManager->Get(i);
4438bf80f4bSopenharmony_ci                        if (const auto meshHandle = meshComponentManager.Read(renderMeshComponent.mesh); meshHandle) {
4448bf80f4bSopenharmony_ci                            auto const worldMatrixComponent = worldMatrixComponentManager->Get(worldMatrixId);
4458bf80f4bSopenharmony_ci                            const auto raycastResult =
4468bf80f4bSopenharmony_ci                                HitTestNode(*node, *meshHandle, worldMatrixComponent.matrix, start, invDir);
4478bf80f4bSopenharmony_ci                            if (raycastResult.node) {
4488bf80f4bSopenharmony_ci                                result.push_back(raycastResult);
4498bf80f4bSopenharmony_ci                            }
4508bf80f4bSopenharmony_ci                        } else {
4518bf80f4bSopenharmony_ci                            CORE_LOG_W("no mesh resource for entity %" PRIx64 ", resource %" PRIx64, id.id,
4528bf80f4bSopenharmony_ci                                renderMeshComponent.mesh.id);
4538bf80f4bSopenharmony_ci                        }
4548bf80f4bSopenharmony_ci                    }
4558bf80f4bSopenharmony_ci                }
4568bf80f4bSopenharmony_ci            }
4578bf80f4bSopenharmony_ci        }
4588bf80f4bSopenharmony_ci    }
4598bf80f4bSopenharmony_ci
4608bf80f4bSopenharmony_ci    std::sort(
4618bf80f4bSopenharmony_ci        result.begin(), result.end(), [](const auto& lhs, const auto& rhs) { return (lhs.distance < rhs.distance); });
4628bf80f4bSopenharmony_ci
4638bf80f4bSopenharmony_ci    return result;
4648bf80f4bSopenharmony_ci}
4658bf80f4bSopenharmony_ci
4668bf80f4bSopenharmony_ciBASE_NS::vector<RayTriangleCastResult> Core3D::Picking::RayCast(const BASE_NS::Math::Vec3& start,
4678bf80f4bSopenharmony_ci    const BASE_NS::Math::Vec3& direction, BASE_NS::array_view<const BASE_NS::Math::Vec3> triangles) const
4688bf80f4bSopenharmony_ci{
4698bf80f4bSopenharmony_ci    vector<RayTriangleCastResult> result;
4708bf80f4bSopenharmony_ci
4718bf80f4bSopenharmony_ci    if (triangles.size() % 3 != 0) { // 3 :param
4728bf80f4bSopenharmony_ci        CORE_LOG_W("Number of triangles vertices not divisible by 3!");
4738bf80f4bSopenharmony_ci        return result;
4748bf80f4bSopenharmony_ci    }
4758bf80f4bSopenharmony_ci
4768bf80f4bSopenharmony_ci    float distance = 0.f;
4778bf80f4bSopenharmony_ci    Math::Vec2 uv;
4788bf80f4bSopenharmony_ci    for (size_t ii = 0; ii < triangles.size(); ii += 3) { // 3 :param
4798bf80f4bSopenharmony_ci        if (IntersectTriangle(&triangles[ii], start, direction, distance, uv)) {
4808bf80f4bSopenharmony_ci            const Math::Vec3 hitPosition = start + direction * distance;
4818bf80f4bSopenharmony_ci
4828bf80f4bSopenharmony_ci            result.push_back(RayTriangleCastResult { distance, hitPosition, uv, static_cast<uint64_t>(ii / 3) });
4838bf80f4bSopenharmony_ci        }
4848bf80f4bSopenharmony_ci    }
4858bf80f4bSopenharmony_ci
4868bf80f4bSopenharmony_ci    return result;
4878bf80f4bSopenharmony_ci}
4888bf80f4bSopenharmony_ci
4898bf80f4bSopenharmony_civector<RayCastResult> Picking::RayCastFromCamera(IEcs const& ecs, Entity camera, const Math::Vec2& screenPos) const
4908bf80f4bSopenharmony_ci{
4918bf80f4bSopenharmony_ci    const auto* worldMatrixManager = GetManager<IWorldMatrixComponentManager>(ecs);
4928bf80f4bSopenharmony_ci    const auto* cameraManager = GetManager<ICameraComponentManager>(ecs);
4938bf80f4bSopenharmony_ci    if (!worldMatrixManager || !cameraManager) {
4948bf80f4bSopenharmony_ci        return vector<RayCastResult>();
4958bf80f4bSopenharmony_ci    }
4968bf80f4bSopenharmony_ci
4978bf80f4bSopenharmony_ci    const auto wmcId = worldMatrixManager->GetComponentId(camera);
4988bf80f4bSopenharmony_ci    const auto ccId = cameraManager->GetComponentId(camera);
4998bf80f4bSopenharmony_ci    if (wmcId != IComponentManager::INVALID_COMPONENT_ID && ccId != IComponentManager::INVALID_COMPONENT_ID) {
5008bf80f4bSopenharmony_ci        const auto cameraComponent = cameraManager->Read(ccId);
5018bf80f4bSopenharmony_ci        const auto worldMatrixComponent = worldMatrixManager->Get(wmcId);
5028bf80f4bSopenharmony_ci        const Ray ray = RayFromCamera(*cameraComponent, worldMatrixComponent, screenPos);
5038bf80f4bSopenharmony_ci        return RayCast(ecs, ray.origin, ray.direction);
5048bf80f4bSopenharmony_ci    }
5058bf80f4bSopenharmony_ci
5068bf80f4bSopenharmony_ci    return vector<RayCastResult>();
5078bf80f4bSopenharmony_ci}
5088bf80f4bSopenharmony_ci
5098bf80f4bSopenharmony_civector<RayCastResult> Picking::RayCastFromCamera(
5108bf80f4bSopenharmony_ci    IEcs const& ecs, Entity camera, const Math::Vec2& screenPos, uint64_t layerMask) const
5118bf80f4bSopenharmony_ci{
5128bf80f4bSopenharmony_ci    const auto* worldMatrixManager = GetManager<IWorldMatrixComponentManager>(ecs);
5138bf80f4bSopenharmony_ci    const auto* cameraManager = GetManager<ICameraComponentManager>(ecs);
5148bf80f4bSopenharmony_ci    if (!worldMatrixManager || !cameraManager) {
5158bf80f4bSopenharmony_ci        return vector<RayCastResult>();
5168bf80f4bSopenharmony_ci    }
5178bf80f4bSopenharmony_ci
5188bf80f4bSopenharmony_ci    const auto wmcId = worldMatrixManager->GetComponentId(camera);
5198bf80f4bSopenharmony_ci    const auto ccId = cameraManager->GetComponentId(camera);
5208bf80f4bSopenharmony_ci    if (wmcId != IComponentManager::INVALID_COMPONENT_ID && ccId != IComponentManager::INVALID_COMPONENT_ID) {
5218bf80f4bSopenharmony_ci        const auto cameraComponent = cameraManager->Read(ccId);
5228bf80f4bSopenharmony_ci        const auto worldMatrixComponent = worldMatrixManager->Get(wmcId);
5238bf80f4bSopenharmony_ci        const Ray ray = RayFromCamera(*cameraComponent, worldMatrixComponent, screenPos);
5248bf80f4bSopenharmony_ci        return RayCast(ecs, ray.origin, ray.direction, layerMask);
5258bf80f4bSopenharmony_ci    }
5268bf80f4bSopenharmony_ci
5278bf80f4bSopenharmony_ci    return vector<RayCastResult>();
5288bf80f4bSopenharmony_ci}
5298bf80f4bSopenharmony_ci
5308bf80f4bSopenharmony_ciBASE_NS::vector<RayTriangleCastResult> Core3D::Picking::RayCastFromCamera(CORE_NS::IEcs const& ecs,
5318bf80f4bSopenharmony_ci    CORE_NS::Entity camera, const BASE_NS::Math::Vec2& screenPos,
5328bf80f4bSopenharmony_ci    BASE_NS::array_view<const BASE_NS::Math::Vec3> triangles) const
5338bf80f4bSopenharmony_ci{
5348bf80f4bSopenharmony_ci    const auto* worldMatrixManager = GetManager<IWorldMatrixComponentManager>(ecs);
5358bf80f4bSopenharmony_ci    const auto* cameraManager = GetManager<ICameraComponentManager>(ecs);
5368bf80f4bSopenharmony_ci    if (!worldMatrixManager || !cameraManager) {
5378bf80f4bSopenharmony_ci        return vector<RayTriangleCastResult>();
5388bf80f4bSopenharmony_ci    }
5398bf80f4bSopenharmony_ci
5408bf80f4bSopenharmony_ci    const auto wmcId = worldMatrixManager->GetComponentId(camera);
5418bf80f4bSopenharmony_ci    const auto ccId = cameraManager->GetComponentId(camera);
5428bf80f4bSopenharmony_ci    if (wmcId != IComponentManager::INVALID_COMPONENT_ID && ccId != IComponentManager::INVALID_COMPONENT_ID) {
5438bf80f4bSopenharmony_ci        const auto cameraComponent = cameraManager->Read(ccId);
5448bf80f4bSopenharmony_ci        const auto worldMatrixComponent = worldMatrixManager->Get(wmcId);
5458bf80f4bSopenharmony_ci        const Ray ray = RayFromCamera(*cameraComponent, worldMatrixComponent, screenPos);
5468bf80f4bSopenharmony_ci
5478bf80f4bSopenharmony_ci        return RayCast(ray.origin, ray.direction, triangles);
5488bf80f4bSopenharmony_ci    }
5498bf80f4bSopenharmony_ci
5508bf80f4bSopenharmony_ci    return BASE_NS::vector<RayTriangleCastResult>();
5518bf80f4bSopenharmony_ci}
5528bf80f4bSopenharmony_ci
5538bf80f4bSopenharmony_ciMinAndMax Picking::GetWorldAABB(const Math::Mat4X4& world, const Math::Vec3& aabbMin, const Math::Vec3& aabbMax) const
5548bf80f4bSopenharmony_ci{
5558bf80f4bSopenharmony_ci    return CORE3D_NS::GetWorldAABB(world, aabbMin, aabbMax);
5568bf80f4bSopenharmony_ci}
5578bf80f4bSopenharmony_ci
5588bf80f4bSopenharmony_ciMinAndMax Picking::GetWorldMatrixComponentAABB(Entity entity, bool isRecursive, IEcs& ecs) const
5598bf80f4bSopenharmony_ci{
5608bf80f4bSopenharmony_ci    MinAndMax mam;
5618bf80f4bSopenharmony_ci
5628bf80f4bSopenharmony_ci    if (ISceneNode* node = GetSystem<INodeSystem>(ecs)->GetNode(entity); node) {
5638bf80f4bSopenharmony_ci        auto& renderMeshComponentManager = *GetManager<IRenderMeshComponentManager>(ecs);
5648bf80f4bSopenharmony_ci        auto& worldMatrixComponentManager = *GetManager<IWorldMatrixComponentManager>(ecs);
5658bf80f4bSopenharmony_ci        auto& jointworldMatrixComponentManager = *GetManager<IJointMatricesComponentManager>(ecs);
5668bf80f4bSopenharmony_ci        auto& meshComponentManager = *GetManager<IMeshComponentManager>(ecs);
5678bf80f4bSopenharmony_ci
5688bf80f4bSopenharmony_ci        UpdateRecursiveAABB(renderMeshComponentManager, worldMatrixComponentManager, jointworldMatrixComponentManager,
5698bf80f4bSopenharmony_ci            meshComponentManager, *node, isRecursive, mam);
5708bf80f4bSopenharmony_ci    }
5718bf80f4bSopenharmony_ci
5728bf80f4bSopenharmony_ci    return mam;
5738bf80f4bSopenharmony_ci}
5748bf80f4bSopenharmony_ci
5758bf80f4bSopenharmony_ciMinAndMax Picking::GetTransformComponentAABB(Entity entity, bool isRecursive, IEcs& ecs) const
5768bf80f4bSopenharmony_ci{
5778bf80f4bSopenharmony_ci    MinAndMax mam;
5788bf80f4bSopenharmony_ci
5798bf80f4bSopenharmony_ci    if (ISceneNode* node = GetSystem<INodeSystem>(ecs)->GetNode(entity); node) {
5808bf80f4bSopenharmony_ci        auto& renderMeshComponentManager = *GetManager<IRenderMeshComponentManager>(ecs);
5818bf80f4bSopenharmony_ci        auto& transformComponentManager = *GetManager<ITransformComponentManager>(ecs);
5828bf80f4bSopenharmony_ci        auto& meshComponentManager = *GetManager<IMeshComponentManager>(ecs);
5838bf80f4bSopenharmony_ci
5848bf80f4bSopenharmony_ci        UpdateRecursiveAABB(renderMeshComponentManager, transformComponentManager, meshComponentManager, *node,
5858bf80f4bSopenharmony_ci            Math::Mat4X4(1.0f), isRecursive, mam);
5868bf80f4bSopenharmony_ci    }
5878bf80f4bSopenharmony_ci
5888bf80f4bSopenharmony_ci    return mam;
5898bf80f4bSopenharmony_ci}
5908bf80f4bSopenharmony_ci
5918bf80f4bSopenharmony_ciconst IInterface* Picking::GetInterface(const Uid& uid) const
5928bf80f4bSopenharmony_ci{
5938bf80f4bSopenharmony_ci    if ((uid == IPicking::UID) || (uid == IInterface::UID)) {
5948bf80f4bSopenharmony_ci        return this;
5958bf80f4bSopenharmony_ci    }
5968bf80f4bSopenharmony_ci    return nullptr;
5978bf80f4bSopenharmony_ci}
5988bf80f4bSopenharmony_ci
5998bf80f4bSopenharmony_ciIInterface* Picking::GetInterface(const Uid& uid)
6008bf80f4bSopenharmony_ci{
6018bf80f4bSopenharmony_ci    if ((uid == IPicking::UID) || (uid == IInterface::UID)) {
6028bf80f4bSopenharmony_ci        return this;
6038bf80f4bSopenharmony_ci    }
6048bf80f4bSopenharmony_ci    return nullptr;
6058bf80f4bSopenharmony_ci}
6068bf80f4bSopenharmony_ci
6078bf80f4bSopenharmony_civoid Picking::Ref() {}
6088bf80f4bSopenharmony_ci
6098bf80f4bSopenharmony_civoid Picking::Unref() {}
6108bf80f4bSopenharmony_ciCORE3D_END_NAMESPACE()
611