1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef CORE_UTIL_MESH_UTIL_H 16 #define CORE_UTIL_MESH_UTIL_H 17 18 #include <3d/util/intf_mesh_builder.h> 19 #include <3d/util/intf_mesh_util.h> 20 #include <base/containers/array_view.h> 21 #include <base/containers/string_view.h> 22 #include <core/namespace.h> 23 24 BASE_BEGIN_NAMESPACE() 25 namespace Math { 26 class Vec2; 27 class Vec3; 28 class Vec4; 29 } // namespace Math 30 BASE_END_NAMESPACE() 31 32 CORE_BEGIN_NAMESPACE() 33 class IClassFactory; 34 CORE_END_NAMESPACE() 35 36 CORE3D_BEGIN_NAMESPACE() 37 class MeshUtil : public IMeshUtil { 38 public: 39 CORE_NS::Entity GeneratePlaneMesh(const CORE_NS::IEcs& ecs, BASE_NS::string_view name, CORE_NS::Entity material, 40 float width, float depth) override; 41 CORE_NS::Entity GenerateSphereMesh(const CORE_NS::IEcs& ecs, BASE_NS::string_view name, CORE_NS::Entity material, 42 float radius, uint32_t rings, uint32_t sectors) override; 43 CORE_NS::Entity GenerateConeMesh(const CORE_NS::IEcs& ecs, BASE_NS::string_view name, CORE_NS::Entity material, 44 float radius, float length, uint32_t sectors) override; 45 CORE_NS::Entity GenerateTorusMesh(const CORE_NS::IEcs& ecs, BASE_NS::string_view name, CORE_NS::Entity material, 46 float majorRadius, float minorRadius, uint32_t majorSectors, uint32_t minorSectors) override; 47 CORE_NS::Entity GenerateCubeMesh(const CORE_NS::IEcs& ecs, BASE_NS::string_view name, CORE_NS::Entity material, 48 float width, float height, float depth) override; 49 50 CORE_NS::Entity GenerateEntity( 51 const CORE_NS::IEcs& ecs, BASE_NS::string_view name, CORE_NS::Entity meshEntity) override; 52 53 CORE_NS::Entity GenerateCube(const CORE_NS::IEcs& ecs, BASE_NS::string_view name, CORE_NS::Entity material, 54 float width, float height, float depth) override; 55 CORE_NS::Entity GeneratePlane(const CORE_NS::IEcs& ecs, BASE_NS::string_view name, CORE_NS::Entity material, 56 float width, float depth) override; 57 58 CORE_NS::Entity GenerateSphere(const CORE_NS::IEcs& ecs, BASE_NS::string_view name, CORE_NS::Entity material, 59 float radius, uint32_t rings, uint32_t sectors) override; 60 61 CORE_NS::Entity GenerateCone(const CORE_NS::IEcs& ecs, BASE_NS::string_view name, CORE_NS::Entity material, 62 float radius, float length, uint32_t sectors) override; 63 64 CORE_NS::Entity GenerateTorus(const CORE_NS::IEcs& ecs, BASE_NS::string_view name, CORE_NS::Entity material, 65 float majorRadius, float minorRadius, uint32_t majorSectors, uint32_t minorSectors) override; 66 67 explicit MeshUtil(CORE_NS::IClassFactory& factory); 68 ~MeshUtil() override = default; 69 70 static void CalculateTangents(const BASE_NS::array_view<const uint8_t>& indices, 71 const BASE_NS::array_view<const BASE_NS::Math::Vec3>& positions, 72 const BASE_NS::array_view<const BASE_NS::Math::Vec3>& normals, 73 const BASE_NS::array_view<const BASE_NS::Math::Vec2>& uvs, 74 BASE_NS::array_view<BASE_NS::Math::Vec4> outTangents); 75 static void CalculateTangents(const BASE_NS::array_view<const uint16_t>& indices, 76 const BASE_NS::array_view<const BASE_NS::Math::Vec3>& positions, 77 const BASE_NS::array_view<const BASE_NS::Math::Vec3>& normals, 78 const BASE_NS::array_view<const BASE_NS::Math::Vec2>& uvs, 79 BASE_NS::array_view<BASE_NS::Math::Vec4> outTangents); 80 static void CalculateTangents(const BASE_NS::array_view<const uint32_t>& indices, 81 const BASE_NS::array_view<const BASE_NS::Math::Vec3>& positions, 82 const BASE_NS::array_view<const BASE_NS::Math::Vec3>& normals, 83 const BASE_NS::array_view<const BASE_NS::Math::Vec2>& uvs, 84 BASE_NS::array_view<BASE_NS::Math::Vec4> outTangents); 85 86 protected: 87 IMeshBuilder::Ptr InitializeBuilder(const IMeshBuilder::Submesh& submesh) const; 88 CORE_NS::Entity CreateMesh(const CORE_NS::IEcs& ecs, const IMeshBuilder& builder, BASE_NS::string_view name) const; 89 90 CORE_NS::IClassFactory& factory_; 91 }; 92 93 CORE3D_END_NAMESPACE() 94 #endif // CORE_UTIL_MESH_UTIL_H 95