Extra2D/include/resource/material.h

111 lines
2.3 KiB
C++

#pragma once
#include <resource/resource.h>
#include <resource/shader.h>
#include <resource/texture.h>
#include <types/math/color.h>
#include <types/math/vec2.h>
#include <types/math/vec3.h>
#include <types/ptr/intrusive_ptr.h>
#include <string>
#include <unordered_map>
#include <variant>
namespace extra2d {
// 材质属性类型定义
using MaterialProperty = std::variant<
bool,
int,
float,
Vec2,
Vec3,
Color,
Ptr<Texture>
>;
/**
* @brief 材质资源类
*
* 封装着色器和材质属性
*/
class Material : public Resource {
public:
Material();
~Material() override;
/**
* @brief 获取资源类型
*/
ResourceType getType() const override { return ResourceType::Material; }
/**
* @brief 设置着色器
* @param shader 着色器指针
*/
void setShader(Ptr<Shader> shader);
/**
* @brief 获取着色器
*/
Shader* getShader() const { return shader_.get(); }
/**
* @brief 设置布尔属性
*/
void setBool(const std::string& name, bool value);
/**
* @brief 设置整数属性
*/
void setInt(const std::string& name, int value);
/**
* @brief 设置浮点数属性
*/
void setFloat(const std::string& name, float value);
/**
* @brief 设置 Vec2 属性
*/
void setVec2(const std::string& name, const Vec2& value);
/**
* @brief 设置 Vec3 属性
*/
void setVec3(const std::string& name, const Vec3& value);
/**
* @brief 设置颜色属性
*/
void setColor(const std::string& name, const Color& value);
/**
* @brief 设置纹理属性
*/
void setTexture(const std::string& name, Ptr<Texture> texture);
/**
* @brief 应用材质(绑定着色器并设置属性)
*/
void apply();
/**
* @brief 克隆材质
* @return 新材质指针
*/
Ptr<Material> clone() const;
private:
Ptr<Shader> shader_; // 着色器
std::unordered_map<std::string, MaterialProperty> properties_; // 属性表
mutable uint32 textureUnit_ = 0; // 当前纹理单元
/**
* @brief 应用所有属性到着色器
*/
void applyProperties();
};
} // namespace extra2d