3.9 KiB
3.9 KiB
Mesh
命名空间: XCEngine::Resources
类型: class (inherits from IResource)
头文件: XCEngine/Resources/Mesh.h
描述: 网格资源类,用于存储和管理 3D 网格数据,包括顶点数据、索引数据和网格分段信息
概述
Mesh 类是 XCEngine 引擎中的核心资源类型之一,负责管理 3D 模型的网格数据。它存储顶点属性(位置、法线、切线、UV 坐标、骨骼权重等)、索引数据以及网格分段(MeshSection)信息。Mesh 资源通常由资源加载器从外部文件(如 .obj、.fbx 等格式)加载,或通过程序化方式生成。
公共方法
| 方法 | 描述 |
|---|---|
Mesh |
构造函数 |
~Mesh |
析构函数 |
GetType |
获取资源类型 |
GetName |
获取资源名称 |
GetPath |
获取资源路径 |
GetGUID |
获取资源全局唯一标识符 |
IsValid |
检查资源是否有效 |
GetMemorySize |
获取资源内存大小 |
Release |
释放资源 |
SetVertexData |
设置顶点数据 |
GetVertexData |
获取顶点数据指针 |
GetVertexDataSize |
获取顶点数据大小 |
GetVertexCount |
获取顶点数量 |
GetVertexStride |
获取顶点步长 |
GetVertexAttributes |
获取顶点属性 |
SetIndexData |
设置索引数据 |
GetIndexData |
获取索引数据指针 |
GetIndexDataSize |
获取索引数据大小 |
GetIndexCount |
获取索引数量 |
IsUse32BitIndex |
是否使用 32 位索引 |
AddSection |
添加网格分段 |
GetSections |
获取所有网格分段 |
相关类型
VertexAttribute
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
struct MeshSection {
Core::uint32 baseVertex;
Core::uint32 vertexCount;
Core::uint32 startIndex;
Core::uint32 indexCount;
Core::uint32 materialID;
};
网格分段结构,描述网格中某个子网格的区域信息,包括顶点范围、索引范围和材质 ID。
使用示例
#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 - 资源基类