docs: update resources API docs

This commit is contained in:
2026-03-20 02:35:35 +08:00
parent fd792b7df1
commit ea756c0177
314 changed files with 9439 additions and 1360 deletions

View File

@@ -4,11 +4,13 @@
**类型**: `class`
**头文件**: `XCEngine/Resources/Material.h`
**描述**: 材质资源类,管理渲染所需的着色器和属性参数。
## 概述
`Material` 是 XCEngine 中的材质资源类,继承自 `IResource`。它管理材质关联的着色器和各种属性参数(浮点数、向量、纹理等),并支持将属性数据打包到常量缓冲区。
`Material` 是 XCEngine 中的材质资源类,继承自 `IResource`。它管理材质关联的着色器和各种属性参数(浮点数、向量、整数、布尔值、纹理等),并支持将属性数据打包到常量缓冲区供 GPU 着色器使用
## 头文件
@@ -20,7 +22,7 @@
### MaterialPropertyType
材质属性类型枚举。
材质属性类型枚举,定义材质支持的属性数据类型
| 值 | 描述 |
|----|------|
@@ -28,124 +30,135 @@
| `Float2` | 二维浮点向量 |
| `Float3` | 三维浮点向量 |
| `Float4` | 四维浮点向量 |
| `Int` | 整数 |
| `Int` | 32位整数 |
| `Int2` | 二维整数向量 |
| `Int3` | 三维整数向量 |
| `Int4` | 四维整数向量 |
| `Bool` | 布尔值 |
| `Texture` | 纹理资源 |
| `Texture` | 二维纹理资源 |
| `Cubemap` | 立方体贴图资源 |
## 结构体
### MaterialProperty
材质属性结构体。
材质属性结构体,存储单个属性的名称、类型和值
| 成员 | 类型 | 描述 |
|------|------|------|
| `name` | `Containers::String` | 属性名称 |
| `type` | `MaterialPropertyType` | 属性类型 |
| `value` | `union` | 属性值float/int/bool |
| `name` | `Containers::String` | 属性名称标识符 |
| `type` | `MaterialPropertyType` | 属性数据类型 |
| `value` | `union Value` | 属性值联合体,支持 float[4]、int[4]、bool |
| `refCount` | `Core::uint32` | 引用计数 |
## 公共方法
## 公共方法表格
### 基础属性
### 资源基类方法
| 方法 | 描述 |
|------|------|
| `ResourceType GetType() const` | 返回 `ResourceType::Material` |
| `const Containers::String& GetName() const` | 获取材质名称 |
| `const Containers::String& GetPath() const` | 获取材质路径 |
| `ResourceGUID GetGUID() const` | 获取全局唯一标识符 |
| `bool IsValid() const` | 检查材质是否有效 |
| `size_t GetMemorySize() const` | 获取内存大小 |
| `void Release()` | 释放材质引用 |
| `GetType()` | 返回 `ResourceType::Material` |
| `GetName()` | 获取材质名称 |
| `GetPath()` | 获取材质资源路径 |
| `GetGUID()` | 获取全局唯一标识符 |
| `IsValid()` | 检查材质是否有效且已加载 |
| `GetMemorySize()` | 获取材质占用的内存大小 |
| `Release()` | 释放材质所有引用和资源 |
### 着色器管理
| 方法 | 描述 |
|------|------|
| `void SetShader(const ResourceHandle<Shader>& shader)` | 设置材质着色器 |
| `Shader* GetShader() const` | 获取材质着色器 |
| [`SetShader`](set-shader.md) | 设置材质使用的着色器 |
| [`GetShader`](get-shader.md) | 获取材质关联的着色器指针 |
### 属性设置
### 属性设置方法
| 方法 | 描述 |
|------|------|
| `void SetFloat(const Containers::String& name, float value)` | 设置浮点属性 |
| `void SetFloat2(const Containers::String& name, const Math::Vector2& value)` | 设置 Vector2 属性 |
| `void SetFloat3(const Containers::String& name, const Math::Vector3& value)` | 设置 Vector3 属性 |
| `void SetFloat4(const Containers::String& name, const Math::Vector4& value)` | 设置 Vector4 属性 |
| `void SetInt(const Containers::String& name, Core::int32 value)` | 设置整数属性 |
| `void SetBool(const Containers::String& name, bool value)` | 设置布尔属性 |
| `void SetTexture(const Containers::String& name, const ResourceHandle<Texture>& texture)` | 设置纹理属性 |
| [`SetFloat`](set-float.md) | 设置浮点属性 |
| [`SetFloat2`](set-float2.md) | 设置二维向量属性 |
| [`SetFloat3`](set-float3.md) | 设置三维向量属性 |
| [`SetFloat4`](set-float4.md) | 设置四维向量属性 |
| [`SetInt`](set-int.md) | 设置整数属性 |
| [`SetBool`](set-bool.md) | 设置布尔属性 |
| [`SetTexture`](set-texture.md) | 设置纹理属性 |
### 属性获取
### 属性获取方法
| 方法 | 描述 |
|------|------|
| `float GetFloat(const Containers::String& name) const` | 获取浮点属性 |
| `Math::Vector2 GetFloat2(const Containers::String& name) const` | 获取 Vector2 属性 |
| `Math::Vector3 GetFloat3(const Containers::String& name) const` | 获取 Vector3 属性 |
| `Math::Vector4 GetFloat4(const Containers::String& name) const` | 获取 Vector4 属性 |
| `Core::int32 GetInt(const Containers::String& name) const` | 获取整数属性 |
| `bool GetBool(const Containers::String& name) const` | 获取布尔属性 |
| `ResourceHandle<Texture> GetTexture(const Containers::String& name) const` | 获取纹理属性 |
| [`GetFloat`](get-float.md) | 获取浮点属性值,不存在返回 0.0f |
| [`GetFloat2`](get-float2.md) | 获取二维向量属性 |
| [`GetFloat3`](get-float3.md) | 获取三维向量属性 |
| [`GetFloat4`](get-float4.md) | 获取四维向量属性 |
| [`GetInt`](get-int.md) | 获取整数属性值,不存在返回 0 |
| [`GetBool`](get-bool.md) | 获取布尔属性值,不存在返回 false |
| [`GetTexture`](get-texture.md) | 获取纹理属性句柄 |
### 常量缓冲区
| 方法 | 描述 |
|------|------|
| `const Containers::Array<Core::uint8>& GetConstantBufferData() const` | 获取常量缓冲区数据 |
| `void UpdateConstantBuffer()` | 更新常量缓冲区数据 |
| [`GetConstantBufferData`](get-constant-buffer-data.md) | 获取常量缓冲区原始数据 |
| [`UpdateConstantBuffer`](update-constant-buffer.md) | 更新常量缓冲区数据 |
### 属性管理
### 属性查询与管理
| 方法 | 描述 |
|------|------|
| `bool HasProperty(const Containers::String& name) const` | 检查属性是否存在 |
| `void RemoveProperty(const Containers::String& name)` | 移除属性 |
| `void ClearAllProperties()` | 清空所有属性 |
## 实现说明
**注意**: `MaterialLoader::ParseMaterialData()` 为 stub始终返回 true。`MaterialLoader::Load()` 仅为示例实现,未解析材质属性。
| [`HasProperty`](has-property.md) | 检查指定属性是否存在 |
| [`RemoveProperty`](remove-property.md) | 移除指定属性 |
| [`ClearAllProperties`](clear-all-properties.md) | 清空所有属性和纹理绑定 |
## 使用示例
```cpp
// 加载材质
// 通过资源管理器加载材质
ResourceHandle<Material> mat = ResourceManager::Get().Load<Material>("materials/player.mat");
// 设置着色器
mat->SetShader(shaderHandle);
ResourceHandle<Shader> shader = ResourceManager::Get().Load<Shader>("shaders/pbr.shader");
mat->SetShader(shader);
// 设置各种属性
// 设置材质属性
mat->SetFloat("roughness", 0.5f);
mat->SetFloat("metallic", 0.0f);
mat->SetFloat3("albedo", Math::Vector3(1.0f, 0.8f, 0.6f));
mat->SetFloat4("emission", Math::Vector4(1.0f, 0.5f, 0.2f, 2.0f));
mat->SetInt("normalScale", 1);
mat->SetBool("receiveShadow", true);
// 设置纹理
ResourceHandle<Texture> albedoTex = ResourceManager::Get().Load<Texture>("textures/albedo.png");
ResourceHandle<Texture> normalTex = ResourceManager::Get().Load<Texture>("textures/normal.png");
ResourceHandle<Texture> roughnessTex = ResourceManager::Get().Load<Texture>("textures/roughness.png");
mat->SetTexture("albedoMap", albedoTex);
mat->SetTexture("normalMap", normalTex);
mat->SetInt("normalScale", 1);
mat->SetTexture("roughnessMap", roughnessTex);
// 获取属性
float roughness = mat->GetFloat("roughness");
Math::Vector3 albedo = mat->GetFloat3("albedo");
Shader* shader = mat->GetShader();
// 检查属性
// 属性查询
if (mat->HasProperty("metallic")) {
float metallic = mat->GetFloat("metallic");
}
// 更新常量缓冲区
// 更新常量缓冲区供 GPU 使用
mat->UpdateConstantBuffer();
auto cbData = mat->GetConstantBufferData();
const auto& cbData = mat->GetConstantBufferData();
// 释放材质
mat->Release();
```
## 相关文档
- [IResource](../iresource/iresource.md) - 资源基类
- [IResource](../iresource/iresource.md) - 资源基类接口
- [Shader](../shader/shader.md) - 着色器资源
- [Texture](../texture/texture.md) - 纹理资源
- [Resources 总览](../resources.md) - 返回模块总览
- [ResourceHandle](../resourcehandle/resourcehandle.md) - 资源句柄
- [Resources 模块总览](../resources.md) - 返回模块总览