重构材质系统

This commit is contained in:
2025-12-03 23:38:08 +08:00
parent bf62257884
commit 3c0e177905
47 changed files with 755 additions and 1151 deletions
+76 -70
View File
@@ -2,63 +2,79 @@
#include "dvc_config.h"
#include "dvc_pre_declare.h"
#include "math/dvc_vector2.h"
#include "math/dvc_vector3.h"
#include "math/dvc_vector4.h"
#include "math/dvc_matrix3.h"
#include "math/dvc_matrix4.h"
#include "dvc_texture.h"
DV_CORE_BEGIN_NAMESPACE
class DV_CORE_API Material
{
public:
struct TextureInfo
typedef std::variant<int32_t, float32_t, fVector2, fVector3, fVector4, fMatrix3, fMatrix4, TextureInfo> Value;
struct Param
{
TextureConstPtr texture;
int32_t texCoord = 0;
fMatrix3 uvTransform;
enum ParamType
{
PF_Int32,
PT_Float,
PT_Float2,
PT_Float3,
PT_Float4,
PT_Matrix3,
PT_Matrix4,
PT_Texture,
};
std::string name;
ParamType type;
Value value;
Param() = delete;
Param(int32_t v) : type(PF_Int32), value(v) { }
Param(float32_t v) : type(PT_Float), value(v) { }
Param(const fVector2& v) : type(PT_Float2), value(v) {}
Param(const fVector3& v) : type(PT_Float3), value(v) {}
Param(const fVector4& v) : type(PT_Float4), value(v) {}
Param(const fMatrix3& v) : type(PT_Matrix3), value(v) {}
Param(const fMatrix4& v) : type(PT_Matrix4), value(v) {}
Param(const TextureInfo& v) : type(PT_Texture), value(v) {}
};
typedef std::map<std::string, Param> ParamMap;
public:
template<typename T>
bool setParam(const char* name, const T& value)
{
ParamMap::iterator it = m_params.find(name);
if(it==m_params.end() || (!std::holds_alternative<T>(it->second.value)))
{
return false;
}
it->second.value = value;
return true;
}
template<typename T>
bool getParam(const char* name, T& value) const
{
ParamMap::const_iterator it = m_params.find(name);
if(it==m_params.end() || (!std::holds_alternative<T>(it->second.value)))
{
return false;
}
value = std::get<T>(it->second.value);
return true;
}
public:
int32_t getID(void) const { return m_id; }
void setBaseColorTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform,
const fVector4& baseColorfactor);
const TextureInfo& getBaseColorTexture() const {
return m_baseColorTexture;
}
const fVector4& getBaseColorFactor() const {
return m_baseColorFactor;
}
void setMetallicRoughnessTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform,
float32_t metallicFactor = 1, float32_t roughnessFactor = 1);
const TextureInfo& getMetallicRoughnessTexture() const {
return m_metallicRoughnessTexture;
}
float32_t getMetallicFactor() const {
return m_metallicFactor;
}
float32_t getRoughnessFactor() const {
return m_roughnessFactor;
}
void setNormalTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform,
float32_t scale = 1);
const TextureInfo& getNormalTexture() const {
return m_normalTexture;
}
float32_t getNormalScale() const {
return m_normalScale;
}
void setVertexShader(const char* szShaderFile, const char* vertexShaderName);
void setFragementShader(const char* szShaderFile, const char* fragementShaderName);
VertexShaderInterface* getVertexShader() const {
return m_vertexShader;
}
@@ -67,37 +83,26 @@ public:
return m_pixelShader;
}
virtual bool setupMaterial();
virtual bool pickupParam(const davinci::MaterialConstPtr otherMaterial) {
return true;
}
public:
bool loadShader();
protected:
void setVertexShader(const char* szShaderFile, const char* vertexShaderName);
void setFragementShader(const char* szShaderFile, const char* fragementShaderName);
template<typename T>
bool registerParam(const char* name, const T& defaultValue) {
if (m_params.find(name) != m_params.end()) return false;
m_params.insert(std::make_pair(name, Param(defaultValue)));
return true;
}
protected:
int32_t m_id;
std::string m_name;
/*The base color texture.
* The first three components (RGB) MUST be encoded with the sRGB transfer function.
*/
TextureInfo m_baseColorTexture;
fVector4 m_baseColorFactor = fVector4::ONE;
/*The metallic - roughness texture.
* The metalness values are sampled from the B channel.
* The roughness values are sampled from the G channel.
*/
TextureInfo m_metallicRoughnessTexture;
float32_t m_metallicFactor = 1.0f;
float32_t m_roughnessFactor = 1.0f;
/*The normal texture.
*/
TextureInfo m_normalTexture;
/*The scalar parameter applied to each normal vector of the texture.
* This value scales the normal vector in X and Y directions using the formula:
* scaledNormal = (normalize<sampled normal texture value> * 2.0 - 1.0) * vec3(<normal scale>, <normal scale>, 1.0).
*/
float32_t m_normalScale = 1.0f;
protected:
VertexShaderInterface* m_vertexShader = nullptr;
PixelShaderInterface* m_pixelShader = nullptr;
@@ -107,8 +112,9 @@ protected:
std::string m_fragementShaderFile;
std::string m_fragementShaderName;
ParamMap m_params;
public:
Material(int32_t id);
Material(int32_t id, const char* name);
~Material();
};
+8
View File
@@ -26,4 +26,12 @@ public:
Texture(int32_t id);
~Texture();
};
struct TextureInfo
{
TextureConstPtr texture;
int32_t texCoord = 0;
fMatrix3 uvTransform;
};
DV_CORE_END_NAMESPACE