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#ifndef CORE_UTIL_PICKING_H 168bf80f4bSopenharmony_ci#define CORE_UTIL_PICKING_H 178bf80f4bSopenharmony_ci 188bf80f4bSopenharmony_ci#include <3d/util/intf_picking.h> 198bf80f4bSopenharmony_ci#include <base/containers/string_view.h> 208bf80f4bSopenharmony_ci#include <core/namespace.h> 218bf80f4bSopenharmony_ci 228bf80f4bSopenharmony_ciCORE_BEGIN_NAMESPACE() 238bf80f4bSopenharmony_ciclass IEcs; 248bf80f4bSopenharmony_ciclass IEngine; 258bf80f4bSopenharmony_ciCORE_END_NAMESPACE() 268bf80f4bSopenharmony_ci 278bf80f4bSopenharmony_ciCORE3D_BEGIN_NAMESPACE() 288bf80f4bSopenharmony_ciclass IJointMatricesComponentManager; 298bf80f4bSopenharmony_ciclass IMeshComponentManager; 308bf80f4bSopenharmony_ciclass ITransformComponentManager; 318bf80f4bSopenharmony_ciclass IRenderMeshComponentManager; 328bf80f4bSopenharmony_ciclass IWorldMatrixComponentManager; 338bf80f4bSopenharmony_cistruct CameraComponent; 348bf80f4bSopenharmony_cistruct MeshComponent; 358bf80f4bSopenharmony_ci 368bf80f4bSopenharmony_ciclass Picking : public IPicking { 378bf80f4bSopenharmony_cipublic: 388bf80f4bSopenharmony_ci Picking() = default; 398bf80f4bSopenharmony_ci ~Picking() override = default; 408bf80f4bSopenharmony_ci BASE_NS::Math::Vec3 ScreenToWorld( 418bf80f4bSopenharmony_ci CORE_NS::IEcs const& ecs, CORE_NS::Entity cameraEntity, BASE_NS::Math::Vec3 screenCoordinate) const override; 428bf80f4bSopenharmony_ci 438bf80f4bSopenharmony_ci BASE_NS::Math::Vec3 WorldToScreen( 448bf80f4bSopenharmony_ci CORE_NS::IEcs const& ecs, CORE_NS::Entity cameraEntity, BASE_NS::Math::Vec3 worldCoordinate) const override; 458bf80f4bSopenharmony_ci 468bf80f4bSopenharmony_ci BASE_NS::vector<RayCastResult> RayCast(CORE_NS::IEcs const& ecs, const BASE_NS::Math::Vec3& start, 478bf80f4bSopenharmony_ci const BASE_NS::Math::Vec3& direction) const override; 488bf80f4bSopenharmony_ci BASE_NS::vector<RayCastResult> RayCast(CORE_NS::IEcs const& ecs, const BASE_NS::Math::Vec3& start, 498bf80f4bSopenharmony_ci const BASE_NS::Math::Vec3& direction, uint64_t layerMask) const override; 508bf80f4bSopenharmony_ci BASE_NS::vector<RayTriangleCastResult> RayCast(const BASE_NS::Math::Vec3& start, 518bf80f4bSopenharmony_ci const BASE_NS::Math::Vec3& direction, BASE_NS::array_view<const BASE_NS::Math::Vec3> triangles) const override; 528bf80f4bSopenharmony_ci 538bf80f4bSopenharmony_ci BASE_NS::vector<RayCastResult> RayCastFromCamera( 548bf80f4bSopenharmony_ci CORE_NS::IEcs const& ecs, CORE_NS::Entity camera, const BASE_NS::Math::Vec2& screenPos) const override; 558bf80f4bSopenharmony_ci BASE_NS::vector<RayCastResult> RayCastFromCamera(CORE_NS::IEcs const& ecs, CORE_NS::Entity camera, 568bf80f4bSopenharmony_ci const BASE_NS::Math::Vec2& screenPos, uint64_t layerMask) const override; 578bf80f4bSopenharmony_ci BASE_NS::vector<RayTriangleCastResult> RayCastFromCamera(CORE_NS::IEcs const& ecs, CORE_NS::Entity camera, 588bf80f4bSopenharmony_ci const BASE_NS::Math::Vec2& screenPos, BASE_NS::array_view<const BASE_NS::Math::Vec3> triangles) const override; 598bf80f4bSopenharmony_ci 608bf80f4bSopenharmony_ci MinAndMax GetWorldAABB(const BASE_NS::Math::Mat4X4& world, const BASE_NS::Math::Vec3& aabbMin, 618bf80f4bSopenharmony_ci const BASE_NS::Math::Vec3& aabbMax) const override; 628bf80f4bSopenharmony_ci 638bf80f4bSopenharmony_ci MinAndMax GetWorldMatrixComponentAABB(CORE_NS::Entity entity, bool isRecursive, CORE_NS::IEcs& ecs) const override; 648bf80f4bSopenharmony_ci 658bf80f4bSopenharmony_ci MinAndMax GetTransformComponentAABB(CORE_NS::Entity entity, bool isRecursive, CORE_NS::IEcs& ecs) const override; 668bf80f4bSopenharmony_ci 678bf80f4bSopenharmony_ci // IInterface 688bf80f4bSopenharmony_ci const IInterface* GetInterface(const BASE_NS::Uid& uid) const override; 698bf80f4bSopenharmony_ci IInterface* GetInterface(const BASE_NS::Uid& uid) override; 708bf80f4bSopenharmony_ci 718bf80f4bSopenharmony_ci void Ref() override; 728bf80f4bSopenharmony_ci void Unref() override; 738bf80f4bSopenharmony_ci 748bf80f4bSopenharmony_ciprotected: 758bf80f4bSopenharmony_ci /** Safely compute the inverse of a direction 1/direction with zero checking. */ 768bf80f4bSopenharmony_ci inline BASE_NS::Math::Vec3 DirectionVectorInverse(const BASE_NS::Math::Vec3& direction) const 778bf80f4bSopenharmony_ci { 788bf80f4bSopenharmony_ci BASE_NS::Math::Vec3 invDir { std::numeric_limits<float>::max(), std::numeric_limits<float>::max(), 798bf80f4bSopenharmony_ci std::numeric_limits<float>::max() }; 808bf80f4bSopenharmony_ci if (direction.x != 0.f) { 818bf80f4bSopenharmony_ci invDir.x = 1.f / direction.x; 828bf80f4bSopenharmony_ci } 838bf80f4bSopenharmony_ci if (direction.y != 0.f) { 848bf80f4bSopenharmony_ci invDir.y = 1.f / direction.y; 858bf80f4bSopenharmony_ci } 868bf80f4bSopenharmony_ci if (direction.z != 0.f) { 878bf80f4bSopenharmony_ci invDir.z = 1.f / direction.z; 888bf80f4bSopenharmony_ci } 898bf80f4bSopenharmony_ci return invDir; 908bf80f4bSopenharmony_ci } 918bf80f4bSopenharmony_ci}; 928bf80f4bSopenharmony_ci 938bf80f4bSopenharmony_ciinline constexpr BASE_NS::string_view GetName(const IPicking*) 948bf80f4bSopenharmony_ci{ 958bf80f4bSopenharmony_ci return "IPicking"; 968bf80f4bSopenharmony_ci} 978bf80f4bSopenharmony_ci 988bf80f4bSopenharmony_ciCORE3D_END_NAMESPACE() 998bf80f4bSopenharmony_ci 1008bf80f4bSopenharmony_ci#endif // CORE_UTIL_PICKING_H