# Mesh **命名空间**: `XCEngine::Resources` **类型**: `class` **描述**: 网格资源类,管理 3D 模型的顶点数据、索引数据和网格分段信息。 ## 概述 `Mesh` 是 XCEngine 中的网格资源类,继承自 `IResource`。它管理网格的顶点数据(位置、法线、UV、切线、颜色、骨骼权重等)、索引数据和网格分段(submesh)。 ## 头文件 ```cpp #include ``` ## 枚举类型 ### VertexAttribute 顶点属性标志枚举(可组合)。 | 值 | 描述 | |----|------| | `Position` | 位置坐标 | | `Normal` | 法线 | | `Tangent` | 切线 | | `Color` | 顶点颜色 | | `UV0` | 第一组纹理坐标 | | `UV1` | 第二组纹理坐标 | | `UV2` | 第三组纹理坐标 | | `UV3` | 第四组纹理坐标 | | `BoneWeights` | 骨骼权重 | | `BoneIndices` | 骨骼索引 | ## MeshSection 结构体 网格分段(Submesh)描述。 | 成员 | 类型 | 描述 | |------|------|------| | `baseVertex` | `Core::uint32` | 基础顶点索引 | | `vertexCount` | `Core::uint32` | 顶点数量 | | `startIndex` | `Core::uint32` | 起始索引 | | `indexCount` | `Core::uint32` | 索引数量 | | `materialID` | `Core::uint32` | 对应材质 ID | ## 公共方法 ### 基础属性 | 方法 | 描述 | |------|------| | `ResourceType GetType() const` | 返回 `ResourceType::Mesh` | | `const Containers::String& GetName() const` | 获取网格名称 | | `const Containers::String& GetPath() const` | 获取网格路径 | | `ResourceGUID GetGUID() const` | 获取全局唯一标识符 | | `bool IsValid() const` | 检查网格是否有效 | | `size_t GetMemorySize() const` | 获取内存大小 | | `void Release()` | 释放网格引用 | ### 顶点数据 | 方法 | 描述 | |------|------| | `void SetVertexData(const void* data, size_t size, Core::uint32 vertexCount, Core::uint32 vertexStride, VertexAttribute attributes)` | 设置顶点数据 | | `const void* GetVertexData() const` | 获取顶点数据指针 | | `size_t GetVertexDataSize() const` | 获取顶点数据大小 | | `Core::uint32 GetVertexCount() const` | 获取顶点数量 | | `Core::uint32 GetVertexStride() const` | 获取顶点结构体大小(字节) | | `VertexAttribute GetVertexAttributes() const` | 获取顶点属性标志 | ### 索引数据 | 方法 | 描述 | |------|------| | `void SetIndexData(const void* data, size_t size, Core::uint32 indexCount, bool use32Bit)` | 设置索引数据 | | `const void* GetIndexData() const` | 获取索引数据指针 | | `size_t GetIndexDataSize() const` | 获取索引数据大小 | | `Core::uint32 GetIndexCount() const` | 获取索引数量 | | `bool IsUse32BitIndex() const` | 是否使用 32 位索引 | ### 网格分段 | 方法 | 描述 | |------|------| | `void AddSection(const MeshSection& section)` | 添加网格分段 | | `const Containers::Array& GetSections() const` | 获取所有分段 | ## 使用示例 ```cpp // 加载网格 ResourceHandle mesh = ResourceManager::Get().Load("models/player.fbx"); // 手动设置顶点数据 struct Vertex { float position[3]; float normal[3]; float uv[2]; }; Containers::Array vertices; vertices.Resize(vertexCount); // ... 填充顶点数据 ... mesh->SetVertexData( vertices.Data(), vertices.Size() * sizeof(Vertex), vertexCount, sizeof(Vertex), VertexAttribute::Position | VertexAttribute::Normal | VertexAttribute::UV0 ); // 设置索引数据 Containers::Array indices; indices.Resize(indexCount); // ... 填充索引数据 ... mesh->SetIndexData( indices.Data(), indices.Size() * sizeof(uint32_t), indexCount, true // 32 位索引 ); // 添加分段(一个网格可能有多个材质) MeshSection section; section.baseVertex = 0; section.vertexCount = vertexCount; section.startIndex = 0; section.indexCount = indexCount; section.materialID = 0; mesh->AddSection(section); // 访问数据 uint32_t vCount = mesh->GetVertexCount(); uint32_t iCount = mesh->GetIndexCount(); auto sections = mesh->GetSections(); ``` ## 相关文档 - [IResource](../iresource/iresource.md) - 资源基类 - [Material](../material/material.md) - 材质资源 - [ResourceManager](../resourcemanager/resourcemanager.md) - 资源管理器 - [Resources 总览](../resources.md) - 返回模块总览 - **实现说明**: `MeshLoader::Load()` 仅为示例实现,不解析 FBX/OBJ 等格式的实际网格数据