Files
XCEngine/docs/api/resources/material-loader/methods/load.md

1.6 KiB
Raw Blame History

MaterialLoader::Load

方法签名

LoadResult Load(const Containers::String& path, const ImportSettings* settings = nullptr) override;

详细描述

加载指定路径的材质资源文件。加载过程包括:

  1. 读取文件数据
  2. 创建 Material 对象并设置基本信息路径、名称、GUID
  3. 解析 JSON 格式内容,提取 shader 路径
  4. 加载引用的 Shader 资源
  5. 设置材质的有效性标志和内存占用

当前实现仅支持解析 JSON 中的 "shader" 字段并加载对应的 Shader 资源。

参数

参数 类型 默认值 描述
path const Containers::String& - 材质文件路径
settings const ImportSettings* nullptr 导入设置(当前未使用)

返回值

LoadResult - 加载结果对象,包含成功加载的 Material 指针或错误信息

示例

#include "Resources/MaterialLoader.h"

using namespace XCEngine::Resources;

MaterialLoader loader;
LoadResult result = loader.Load("assets/materials/pbr.metallic");

if (result.IsSuccess()) {
    Material* material = static_cast<Material*>(result.GetResource());
    printf("Loaded material: %s, GUID: %s\n", 
           material->m_name.CStr(), 
           material->m_guid.ToString().CStr());
} else {
    printf("Failed to load material: %s\n", result.GetError().CStr());
}

JSON 材质文件格式

{
    "shader": "shaders/pbr.glsl",
    "properties": {
        "albedo": [1.0, 1.0, 1.0, 1.0],
        "metallic": 0.0,
        "roughness": 0.5
    }
}