# Mesh **命名空间**: `XCEngine::Resources` **类型**: `class` (inherits from `IResource`) **头文件**: `XCEngine/Resources/Mesh.h` **描述**: 网格资源类,用于存储和管理 3D 网格数据,包括顶点数据、索引数据和网格分段信息 ## 概述 Mesh 类是 XCEngine 引擎中的核心资源类型之一,负责管理 3D 模型的网格数据。它存储顶点属性(位置、法线、切线、UV 坐标、骨骼权重等)、索引数据以及网格分段(MeshSection)信息。Mesh 资源通常由资源加载器从外部文件(如 .obj、.fbx 等格式)加载,或通过程序化方式生成。 ## 公共方法 | 方法 | 描述 | |------|------| | [`Mesh`](constructor.md) | 构造函数 | | [`~Mesh`](destructor.md) | 析构函数 | | [`GetType`](get-type.md) | 获取资源类型 | | [`GetName`](get-name.md) | 获取资源名称 | | [`GetPath`](get-path.md) | 获取资源路径 | | [`GetGUID`](get-guid.md) | 获取资源全局唯一标识符 | | [`IsValid`](is-valid.md) | 检查资源是否有效 | | [`GetMemorySize`](get-memory-size.md) | 获取资源内存大小 | | [`Release`](release.md) | 释放资源 | | [`SetVertexData`](set-vertex-data.md) | 设置顶点数据 | | [`GetVertexData`](get-vertex-data.md) | 获取顶点数据指针 | | [`GetVertexDataSize`](get-vertex-data-size.md) | 获取顶点数据大小 | | [`GetVertexCount`](get-vertex-count.md) | 获取顶点数量 | | [`GetVertexStride`](get-vertex-stride.md) | 获取顶点步长 | | [`GetVertexAttributes`](get-vertex-attributes.md) | 获取顶点属性 | | [`SetIndexData`](set-index-data.md) | 设置索引数据 | | [`GetIndexData`](get-index-data.md) | 获取索引数据指针 | | [`GetIndexDataSize`](get-index-data-size.md) | 获取索引数据大小 | | [`GetIndexCount`](get-index-count.md) | 获取索引数量 | | [`IsUse32BitIndex`](is-use-32bit-index.md) | 是否使用 32 位索引 | | [`AddSection`](add-section.md) | 添加网格分段 | | [`GetSections`](get-sections.md) | 获取所有网格分段 | ## 相关类型 ### VertexAttribute ```cpp enum class VertexAttribute : Core::uint32 { Position = 1 << 0, Normal = 1 << 1, Tangent = 1 << 2, Color = 1 << 3, UV0 = 1 << 4, UV1 = 1 << 5, UV2 = 1 << 6, UV3 = 1 << 7, BoneWeights = 1 << 8, BoneIndices = 1 << 9 }; ``` 顶点属性枚举,用于指定网格包含哪些顶点数据通道。多个属性可以通过按位或组合使用。 ### MeshSection ```cpp struct MeshSection { Core::uint32 baseVertex; Core::uint32 vertexCount; Core::uint32 startIndex; Core::uint32 indexCount; Core::uint32 materialID; }; ``` 网格分段结构,描述网格中某个子网格的区域信息,包括顶点范围、索引范围和材质 ID。 ## 使用示例 ```cpp #include "XCEngine/Resources/Mesh.h" #include "XCEngine/Containers/Array.h" using namespace XCEngine; using namespace Resources; void CreateQuadMesh(Mesh* outMesh) { struct Vertex { float position[3]; float normal[3]; float uv[2]; }; Vertex vertices[4] = { {{-0.5f, -0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}}, {{ 0.5f, -0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}}, {{ 0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}}, {{-0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {0.0f, 1.0f}} }; uint32_t indices[6] = {0, 1, 2, 0, 2, 3}; VertexAttribute attributes = VertexAttribute::Position | VertexAttribute::Normal | VertexAttribute::UV0; outMesh->SetVertexData(vertices, sizeof(vertices), 4, sizeof(Vertex), attributes); outMesh->SetIndexData(indices, sizeof(indices), 6, false); MeshSection section; section.baseVertex = 0; section.vertexCount = 4; section.startIndex = 0; section.indexCount = 6; section.materialID = 0; outMesh->AddSection(section); } ``` ## 相关文档 - [IResource](../iresource/iresource.md) - 资源基类