From b42d785afebd313f8a531dc8cf832d80ecd50910 Mon Sep 17 00:00:00 2001 From: Daniel Hritzkiv Date: Sun, 27 Aug 2017 23:47:54 -0400 Subject: [PATCH] Start managing and importing gltf2 pbr materials --- code/glTF2Asset.h | 40 +++++++++++++++++++++++++++++----------- code/glTF2Asset.inl | 19 +++++++++++++++---- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/code/glTF2Asset.h b/code/glTF2Asset.h index 2b37d6a0e..5d99735b2 100644 --- a/code/glTF2Asset.h +++ b/code/glTF2Asset.h @@ -671,11 +671,18 @@ namespace glTF2 inline void SetData(uint8_t* data, size_t length, Asset& r); }; - //! Holds a material property that can be a texture or a color - struct TexProperty + struct ColorProperty + { + union { + vec4, vec3 + } color; + } + + //! Holds a material property that can be a texture or a color (fallback for glTF 1) + struct FallbackTexProperty { Ref texture; - vec4 color; + ColorProperty color; }; //! The material appearance of a primitive. @@ -694,16 +701,27 @@ namespace glTF2 Technique_CONSTANT }; - TexProperty ambient; - TexProperty diffuse; - TexProperty specular; - TexProperty emission; - Ref normal; + //PBR metallic roughness properties + ColorProperty baseColor; + Ref baseColorTexture; + Ref metallicRoughnessTexture; + float metallicFactor; + float roughnessFactor; + //other basic material properties + Ref normalTexture; + Ref occlusionTexture; + Ref emissiveTexture; + ColorProperty emissiveFactor; + std::string alphaMode; + float alphaCutoff; bool doubleSided; - bool transparent; - float transparency; - float shininess; + + //fallback material properties (compatible with non-pbr defintions) + FallbackTexProperty diffuse; + FallbackTexProperty emissive; + FallbackTexProperty specular; + Ref normal; Technique technique; diff --git a/code/glTF2Asset.inl b/code/glTF2Asset.inl index 27fdd1c01..bf8cb5a36 100644 --- a/code/glTF2Asset.inl +++ b/code/glTF2Asset.inl @@ -725,18 +725,29 @@ inline void Material::Read(Value& material, Asset& r) SetDefaults(); if (Value* values = FindObject(material, "values")) { - ReadMaterialProperty(r, *values, "ambient", this->ambient); - ReadMaterialProperty(r, *values, "diffuse", this->diffuse); - ReadMaterialProperty(r, *values, "specular", this->specular); + ReadMember(*values, "transparency", transparency); - ReadMember(*values, "shininess", shininess); } if (Value* values = FindObject(material, "pbrMetallicRoughness")) { + //pbr + ReadMaterialProperty(r, *values, "baseColorFactor", this->baseColor); + ReadMaterialProperty(r, *values, "baseColorTexture", this->baseColorTexture); + + //non-pbr fallback ReadMaterialProperty(r, *values, "baseColorFactor", this->diffuse); + ReadMaterialProperty(r, *values, "baseColorTexture", this->diffuse); + + ReadMember(*values, "metallicFactor", metallicFactor); } + ReadMaterialProperty(r, *values, "normalTexture", this->normalTexture); + ReadMaterialProperty(r, *values, "normalTexture", this->normal); + ReadMaterialProperty(r, *values, "occlusionTexture", this->occlusionTexture); + ReadMaterialProperty(r, *values, "emissiveTexture", this->emissiveTexture); + ReadMember(*values, "metallicFactor", emissiveFactor); + ReadMember(material, "doubleSided", doubleSided); if (Value* extensions = FindObject(material, "extensions")) {