Skip to content

Commit

Permalink
feat: material support (unity3d-jp#803)
Browse files Browse the repository at this point in the history
* Adding improved material support.

* WIP material support.

* WIP material support

* More texture support

* WIP standard built-in pipeline support.

* Adding basic material support.
Adding material test scene.

* Extracting constants

* Removing dead code.

* Refactoring

* Reverting unwanted change

* Restore render target after changing it.

* Added comments

* Refactoring

* Enabling glass material

* Refactoring

* Refactoring.
Fix or normal map scale on HDRP.

* Catching more specific exception.

* Refactoring

* Refactoring

* Refactoring

* Tyypo fiiix.

* Deleting unused file

* Fix for runtime issues

* Adding texture wrapper to release resources.
Refactoring.

* Compile fixes for hdrp

* Fix for unity 2020

* Using 0.5 as default for AO and detail mask in mask map.
Fix for resetting transparency.
Updating test scene to test reroutes, add shader and mix shader.

* Removing test scene.

* Release rendertextures after replacing them with texture2d.

* Updating comments

* Fix for render textures not being released.

* Release textures that were created.

* Making class internal.
Disabling unused code.

* Refactoring

* Refactoring to use property name IDs.

* Format fix

* Extracting constants

* Force synchronous import to ensure texture exists when we need it.

* Refactoring to use constant.

* Refactoring

* Removing unnecessary code.

* Refactoring

* Removing unused parameter.

* Displacement fix

* Change to use height scale directly.

* Updating dll

* Ensure assets are updated after changing settings

* Ensure importer is updated.

* Adding ticket number

* Using constant

* Adding comment

* Adding comment

* Removing code we don't use yet.

* Extracting constant.

* Adding comments

* Releasing render texture

* Refactoring

* Log update

* Adding comment

* Renaming method

* Refactoring

* Docs update

* Reverting unwanted change

* Removing debug code.
  • Loading branch information
schinkowski authored Nov 22, 2022
1 parent b0d08aa commit e307c51
Show file tree
Hide file tree
Showing 21 changed files with 1,105 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MaterialProperty
Float,
Vector,
Matrix,
Texture,
Texture
};

struct TextureRecord
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ enum class TextureType
{
Default,
NormalMap,
NonColor
};

template<class T> struct GetTextureFormat;
Expand Down
15 changes: 13 additions & 2 deletions Plugin~/Src/MeshSync/Include/MeshSync/Utility/msMaterialExt.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
msDeclClassPtr(Material)

namespace ms {

// correspond to Unity's Standard shader
/**
* Generic PBR shader using field names from Unity's Standard shader.
*/
class StandardMaterial : public Material
{
public:
Expand All @@ -30,19 +31,29 @@ class StandardMaterial : public Material
void setEmissionMap(TexturePtr v);
TextureRecord* getEmissionMap() const;

void setEmissionStrength(float v);

void setMetallic(float v);
float getMetallic() const;
void setMetallicMap(const TextureRecord& v);
void setMetallicMap(TexturePtr v);
TextureRecord* getMetallicMap() const;
void setSmoothness(float v);
void setSmoothnessMap(const TextureRecord& v);
float getSmoothness() const;

void setBumpScale(float v);
float getBumpScale() const;
void setBumpMap(const TextureRecord& v);
void setBumpMap(TexturePtr v);
TextureRecord* getBumpMap() const;

void setHeightScale(float v);
void setHeightMap(const TextureRecord& v);

void setSpecular(mu::float3 v);

void setShader(const std::string shaderName);
};
msDeclPtr(StandardMaterial);
inline StandardMaterial& AsStandardMaterial(Material& p) { return reinterpret_cast<StandardMaterial&>(p); }
Expand Down
44 changes: 39 additions & 5 deletions Plugin~/Src/MeshSync/Utils/msMaterialExt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ namespace ms {
// shader keywords
static const char _Color[] = "_Color";
static const char _EmissionColor[] = "_EmissionColor";
static const char _EmissionStrength[] = "_EmissionStrength";
static const char _Metallic[] = "_Metallic";
static const char _Glossiness[] = "_Glossiness";
static const char _MainTex[] = "_MainTex";
static const char _EmissionMap[] = "_EmissionMap";
static const char _MetallicGlossMap[] = "_MetallicGlossMap";
static const char _GlossMap[] = "_GlossMap";
static const char _BumpScale[] = "_BumpScale";
static const char _BumpMap[] = "_BumpMap";
static const char DETAIL_ALBEDO_MAP_SHADER_VAR[] = "_DetailAlbedoMap";
static const char UV_SEC_SHADER_VAR[] = "_UVSec";
static const char _SpecColor[] = "_SpecColor";
static const char _ParallaxMap[] = "_ParallaxMap";
static const char _Parallax[] = "_Parallax";

using TextureRecord = MaterialProperty::TextureRecord;

Expand All @@ -33,8 +38,8 @@ void StandardMaterial::setColorMap(const TextureRecord& v)
}
void StandardMaterial::setColorMap(const TexturePtr v)
{
if (v)
addProperty(MaterialProperty(_MainTex, v ));
// Allow null:
addProperty(MaterialProperty(_MainTex, v));
}

Material::TextureRecord* StandardMaterial::getColorMap() const {
Expand Down Expand Up @@ -93,6 +98,11 @@ Material::TextureRecord* StandardMaterial::getEmissionMap() const
return p ? &p->get<TextureRecord>() : nullptr;
}

void StandardMaterial::setEmissionStrength(float v)
{
addProperty(MaterialProperty(_EmissionStrength, v));
}

void StandardMaterial::setMetallic(float v) {
addProperty( MaterialProperty( _Metallic, v ));
}
Expand All @@ -102,6 +112,7 @@ float StandardMaterial::getMetallic() const
return p ? p->get<float>() : 0.0f;
}
void StandardMaterial::setMetallicMap(const TextureRecord& v) {
// Allow null:
addProperty( MaterialProperty( _MetallicGlossMap, v ));
}
void StandardMaterial::setMetallicMap(TexturePtr v) {
Expand All @@ -122,9 +133,12 @@ float StandardMaterial::getSmoothness() const
const MaterialProperty* p = findProperty(_Glossiness);
return p ? p->get<float>() : 0.0f;
}
void StandardMaterial::setSmoothnessMap(const TextureRecord& v) {
// Allow null:
addProperty(MaterialProperty(_GlossMap, v));
}

void StandardMaterial::setBumpScale(float v)
{
void StandardMaterial::setBumpScale(float v) {
addProperty(MaterialProperty( _BumpScale, v ));
}
float StandardMaterial::getBumpScale() const
Expand All @@ -133,6 +147,7 @@ float StandardMaterial::getBumpScale() const
return p ? p->get<float>() : 0.0f;
}
void StandardMaterial::setBumpMap(const TextureRecord& v) {
// Allow null:
addProperty(MaterialProperty( _BumpMap, v ));
}
void StandardMaterial::setBumpMap(const TexturePtr v) {
Expand All @@ -145,11 +160,30 @@ Material::TextureRecord* StandardMaterial::getBumpMap() const
return p ? &p->get<TextureRecord>() : nullptr;
}

void StandardMaterial::setSpecular(mu::float3 v)
{
addProperty(MaterialProperty(_SpecColor, v));
}

void StandardMaterial::setHeightScale(float v) {
addProperty(MaterialProperty(_Parallax, v));
}

void StandardMaterial::setHeightMap(const TextureRecord& v)
{
// Allow null:
addProperty(MaterialProperty(_ParallaxMap, v));
}

void StandardMaterial::setShader(const std::string shaderName)
{
shader = shaderName;
}

//----------------------------------------------------------------------------------------------------------------------

// StandardSpecMaterial

static const char _SpecColor[] = "_SpecColor";
static const char _SpecGlossMap[] = "_SpecGlossMap";

void StandardSpecMaterial::setupShader() {
Expand Down
Loading

0 comments on commit e307c51

Please sign in to comment.