New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor material. #122
Refactor material. #122
Conversation
enum class MaterialPropertyGroup | ||
{ | ||
BaseColor = 0, | ||
Normal, | ||
Metalness, | ||
Occlusion, | ||
Roughness, | ||
Metallic, | ||
Normal, | ||
Emissive, | ||
AO, | ||
Count | ||
General, | ||
|
||
Count, | ||
}; | ||
|
||
constexpr const char* MaterialTextureTypeName[] = | ||
// As each MaterialPropertyGroup just has one texture, | ||
// it's fine to consider MaterialPropertyGroup as MaterialTextureType | ||
// from the perspective of texture. | ||
using MaterialTextureType = MaterialPropertyGroup; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enums name changes a bit to match O3DE implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem. After this change, you need to update in the engine side.
public/Scene/MaterialTextureType.h
Outdated
inline std::string GetMaterialPropertyKey(MaterialPropertyGroup propertyGroup, MaterialProperty property) | ||
{ | ||
std::stringstream ss; | ||
ss << GetMaterialPropertyGroupName(propertyGroup) << "_" << GetMaterialPropertyName(property); | ||
|
||
return ss.str(); | ||
} | ||
|
||
inline const char* GetMaterialTextureTypeName(MaterialTextureType textureType) | ||
inline std::string GetMaterialPropertyTextureKey(MaterialPropertyGroup propertyGroup) | ||
{ | ||
return MaterialTextureTypeName[static_cast<int>(textureType)]; | ||
return GetMaterialPropertyKey(propertyGroup, MaterialProperty::Texture); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use these util functions to easily get the key of PropertyMap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use CD_FORCEINLINE
for one line function as inline
keyword just suggets compiler to do inline but not force.
MaterialImpl::MaterialImpl(MaterialID materialID, std::string materialName, MaterialType materialType) | ||
{ | ||
Init(materialID, MoveTemp(materialName)); | ||
Init(materialID, MoveTemp(materialName), materialType); | ||
|
||
switch (materialType) | ||
{ | ||
case MaterialType::BasePBR: | ||
InitBasePBR(); | ||
break; | ||
|
||
default: | ||
printf("Unknow material type!\n"); | ||
break; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Material's constructor requires a new parameter to init default value of different material type.
public/Scene/MaterialTextureType.h
Outdated
inline std::string GetMaterialPropertyKey(MaterialPropertyGroup propertyGroup, MaterialProperty property) | ||
{ | ||
std::stringstream ss; | ||
ss << GetMaterialPropertyGroupName(propertyGroup) << "_" << GetMaterialPropertyName(property); | ||
|
||
return ss.str(); | ||
} | ||
|
||
inline const char* GetMaterialTextureTypeName(MaterialTextureType textureType) | ||
inline std::string GetMaterialPropertyTextureKey(MaterialPropertyGroup propertyGroup) | ||
{ | ||
return MaterialTextureTypeName[static_cast<int>(textureType)]; | ||
return GetMaterialPropertyKey(propertyGroup, MaterialProperty::Texture); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use CD_FORCEINLINE
for one line function as inline
keyword just suggets compiler to do inline but not force.
enum class MaterialPropertyGroup | ||
{ | ||
BaseColor = 0, | ||
Normal, | ||
Metalness, | ||
Occlusion, | ||
Roughness, | ||
Metallic, | ||
Normal, | ||
Emissive, | ||
AO, | ||
Count | ||
General, | ||
|
||
Count, | ||
}; | ||
|
||
constexpr const char* MaterialTextureTypeName[] = | ||
// As each MaterialPropertyGroup just has one texture, | ||
// it's fine to consider MaterialPropertyGroup as MaterialTextureType | ||
// from the perspective of texture. | ||
using MaterialTextureType = MaterialPropertyGroup; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem. After this change, you need to update in the engine side.
private/Scene/MaterialImpl.h
Outdated
void AddProperty(MaterialPropertyGroup propertyGroup, MaterialProperty property, T value) | ||
{ | ||
m_propertyGroups.Add(GetMaterialPropertyKey(propertyGroup, property), value); | ||
} | ||
template<typename T> | ||
void SetProperty(MaterialPropertyGroup propertyGroup, MaterialProperty property, T value) | ||
{ | ||
m_propertyGroups.Set(GetMaterialPropertyKey(propertyGroup, property), value); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should avoid copy value if T is a string so you can MoveTemp(value) for all usages.
You can consider to serialize material type to SceneDatabase. And material type will have an ID. Then material instance will hold an ID to reference MaterialType. The advantage is that we don't limit material type by enum so everyone who wants to create a cutomized material type can add a new material type into SceneDatabase and set its ID for material instances. For example, I want to write a new shader about cartoon. In the high level, I need to create my cartoon material type and add to SceneDatabase. Then I will create multiple materials(instances) for multiple meshes which assigned to a cartoon material and fill their actual parameters. For naming, it can be Effect and Material. Or MaterialType and MaterialInstance. It is two generic naming rules about this thing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK to merge as it doesn't make actual feature changes.
Refer to #112
Abstract Material Hierarchy
For example:
Actual Data Structure
PropertyMap
A key-value map to store variant properties.
which format is
MaterialPropertyGroup_MaterialProperty
.TODO
Need to apply these changes and test on Engine side.