Pārlūkot izejas kodu

Start removing materials common, and adding pbrSpecularGlossiness

Daniel Hritzkiv 8 gadi atpakaļ
vecāks
revīzija
863458cd4a
5 mainītis faili ar 63 papildinājumiem un 121 dzēšanām
  1. 8 61
      code/glTF2Asset.h
  2. 20 44
      code/glTF2Asset.inl
  3. 1 1
      code/glTF2AssetWriter.h
  4. 27 15
      code/glTF2AssetWriter.inl
  5. 7 0
      code/glTF2Importer.cpp

+ 8 - 61
code/glTF2Asset.h

@@ -44,7 +44,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  * glTF Extensions Support:
  *   KHR_binary_glTF: full
- *   KHR_materials_common: full
  */
 #ifndef GLTF2ASSET_H_INC
 #define GLTF2ASSET_H_INC
@@ -697,19 +696,6 @@ namespace glTF2
     //! The material appearance of a primitive.
     struct Material : public Object
     {
-        //Ref<Sampler> source; //!< The ID of the technique.
-        //std::gltf_unordered_map<std::string, std::string> values; //!< A dictionary object of parameter values.
-
-        //! Techniques defined by KHR_materials_common
-        enum Technique
-        {
-            Technique_undefined = 0,
-            Technique_BLINN,
-            Technique_PHONG,
-            Technique_LAMBERT,
-            Technique_CONSTANT
-        };
-
         //PBR metallic roughness properties
         vec4 baseColorFactor;
         TextureInfo baseColorTexture;
@@ -726,13 +712,12 @@ namespace glTF2
         float alphaCutoff;
         bool doubleSided;
 
-        //fallback material properties (compatible with non-pbr defintions)
-        /*FallbackTexProperty diffuse;
-        FallbackTexProperty emission;
-        FallbackTexProperty specular;
-        Ref<Texture> normal;*/
-
-        Technique technique;
+        //extension: KHR_materials_pbrSpecularGlossiness
+        vec4 diffuseFactor;
+        vec3 specularFactor;
+        float glossinessFactor;
+        TextureInfo diffuseTexture;
+        TextureInfo specularGlossinessTexture;
 
         Material() { SetDefaults(); }
         void Read(Value& obj, Asset& r);
@@ -943,35 +928,6 @@ namespace glTF2
         void Read(Value& obj, Asset& r);
     };
 
-
-    //! A light (from KHR_materials_common extension)
-    struct Light : public Object
-    {
-        enum Type
-        {
-            Type_undefined,
-            Type_ambient,
-            Type_directional,
-            Type_point,
-            Type_spot
-        };
-
-        Type type;
-
-        vec4 color;
-        float distance;
-        float constantAttenuation;
-        float linearAttenuation;
-        float quadraticAttenuation;
-        float falloffAngle;
-        float falloffExponent;
-
-        Light() {}
-        void Read(Value& obj, Asset& r);
-
-        void SetDefaults();
-    };
-
     struct Animation : public Object
     {
         struct AnimSampler {
@@ -1152,7 +1108,7 @@ namespace glTF2
         struct Extensions
         {
             bool KHR_binary_glTF;
-            bool KHR_materials_common;
+            bool KHR_materials_pbrSpecularGlossiness;
 
         } extensionsUsed;
 
@@ -1170,16 +1126,11 @@ namespace glTF2
         LazyDict<Material>    materials;
         LazyDict<Mesh>        meshes;
         LazyDict<Node>        nodes;
-        //LazyDict<Program>   programs;
         LazyDict<Sampler>     samplers;
         LazyDict<Scene>       scenes;
-        //LazyDict<Shader>    shaders;
         LazyDict<Skin>        skins;
-        //LazyDict<Technique> techniques;
         LazyDict<Texture>     textures;
 
-        LazyDict<Light>       lights; // KHR_materials_common ext
-
         Ref<Scene> scene;
 
     public:
@@ -1195,14 +1146,10 @@ namespace glTF2
             , materials     (*this, "materials")
             , meshes        (*this, "meshes")
             , nodes         (*this, "nodes")
-            //, programs    (*this, "programs")
             , samplers      (*this, "samplers")
             , scenes        (*this, "scenes")
-            //, shaders     (*this, "shaders")
-            , skins       (*this, "skins")
-            //, techniques  (*this, "techniques")
+            , skins         (*this, "skins")
             , textures      (*this, "textures")
-            , lights        (*this, "lights", "KHR_materials_common")
         {
             memset(&extensionsUsed, 0, sizeof(extensionsUsed));
         }

+ 20 - 44
code/glTF2Asset.inl

@@ -757,12 +757,12 @@ inline void Material::Read(Value& material, Asset& r)
 {
     SetDefaults();
 
-    if (Value* values = FindObject(material, "pbrMetallicRoughness")) {
-        ReadMember(*values, "baseColorFactor", this->baseColorFactor);
-        ReadTextureProperty(r, *values, "baseColorTexture", this->baseColorTexture);
-        ReadTextureProperty(r, *values, "metallicRoughnessTexture", this->metallicRoughnessTexture);
-        ReadMember(*values, "metallicFactor", this->metallicFactor);
-        ReadMember(*values, "roughnessFactor", this->roughnessFactor);
+    if (Value* pbrMetallicRoughness = FindObject(material, "pbrMetallicRoughness")) {
+        ReadMember(*pbrMetallicRoughness, "baseColorFactor", this->baseColorFactor);
+        ReadTextureProperty(r, *pbrMetallicRoughness, "baseColorTexture", this->baseColorTexture);
+        ReadTextureProperty(r, *pbrMetallicRoughness, "metallicRoughnessTexture", this->metallicRoughnessTexture);
+        ReadMember(*pbrMetallicRoughness, "metallicFactor", this->metallicFactor);
+        ReadMember(*pbrMetallicRoughness, "roughnessFactor", this->roughnessFactor);
     }
 
     ReadTextureProperty(r, material, "normalTexture", this->normalTexture);
@@ -774,30 +774,17 @@ inline void Material::Read(Value& material, Asset& r)
     ReadMember(material, "alphaMode", this->alphaMode);
     ReadMember(material, "alphaCutoff", this->alphaCutoff);
 
-    /* if (Value* extensions = FindObject(material, "extensions")) {
-        if (r.extensionsUsed.KHR_materials_common) {
-            if (Value* ext = FindObject(*extensions, "KHR_materials_common")) {
-                if (Value* tnq = FindString(*ext, "technique")) {
-                    const char* t = tnq->GetString();
-                    if      (strcmp(t, "BLINN") == 0)    technique = Technique_BLINN;
-                    else if (strcmp(t, "PHONG") == 0)    technique = Technique_PHONG;
-                    else if (strcmp(t, "LAMBERT") == 0)  technique = Technique_LAMBERT;
-                    else if (strcmp(t, "CONSTANT") == 0) technique = Technique_CONSTANT;
-                }
-
-                if (Value* values = FindObject(*ext, "values")) {
-                    ReadTextureProperty(r, *values, "ambient", this->ambient);
-                    ReadTextureProperty(r, *values, "diffuse", this->diffuse);
-                    ReadTextureProperty(r, *values, "specular", this->specular);
-
-                    ReadMember(*values, "doubleSided", doubleSided);
-                    ReadMember(*values, "transparent", transparent);
-                    ReadMember(*values, "transparency", transparency);
-                    ReadMember(*values, "shininess", shininess);
-                }
+    if (Value* extensions = FindObject(material, "extensions")) {
+        if (r.extensionsUsed.KHR_materials_pbrSpecularGlossiness) {
+            if (Value* pbrSpecularGlossiness = FindObject(*extensions, "KHR_materials_pbrSpecularGlossiness")) {
+                ReadMember(*pbrSpecularGlossiness, "diffuseFactor", this->diffuseFactor);
+                ReadTextureProperty(r, *pbrSpecularGlossiness, "diffuseTexture", this->diffuseTexture);
+                ReadTextureProperty(r, *pbrSpecularGlossiness, "specularGlossinessTexture", this->specularGlossinessTexture);
+                ReadMember(*pbrSpecularGlossiness, "specularFactor", this->specularFactor);
+                ReadMember(*pbrSpecularGlossiness, "glossinessFactor", this->glossinessFactor);
             }
         }
-    } */
+    }
 }
 
 namespace {
@@ -820,7 +807,10 @@ inline void Material::SetDefaults()
     alphaCutoff = 0.5;
     doubleSided = false;
 
-    technique = Technique_undefined;
+    //pbrSpecularGlossiness properties
+    SetVector(diffuseFactor, 1, 1, 1, 1);
+    SetVector(specularFactor, 1, 1, 1);
+    glossinessFactor = 1.0;
 }
 
 namespace {
@@ -1083,20 +1073,6 @@ inline void Node::Read(Value& obj, Asset& r)
         if (this->camera)
             this->camera->id = this->id;
     }
-
-    // TODO load "skeletons", "skin", "jointName"
-
-    /*if (Value* extensions = FindObject(obj, "extensions")) {
-        if (r.extensionsUsed.KHR_materials_common) {
-
-            if (Value* ext = FindObject(*extensions, "KHR_materials_common")) {
-                if (Value* light = FindUInt(*ext, "light")) {
-                    this->light = r.lights.Retrieve(light->GetUint());
-                }
-            }
-
-        }
-    }*/
 }
 
 inline void Scene::Read(Value& obj, Asset& r)
@@ -1293,7 +1269,7 @@ inline void Asset::ReadExtensionsUsed(Document& doc)
         if (exts.find(#EXT) != exts.end()) extensionsUsed.EXT = true;
 
     CHECK_EXT(KHR_binary_glTF);
-    CHECK_EXT(KHR_materials_common);
+    CHECK_EXT(KHR_materials_pbrSpecularGlossiness);
 
     #undef CHECK_EXT
 }

+ 1 - 1
code/glTF2AssetWriter.h

@@ -44,7 +44,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  * glTF Extensions Support:
  *   KHR_binary_glTF: full
- *   KHR_materials_common: full
+ *   KHR_materials_pbrSpecularGlossiness: full
  */
 #ifndef GLTF2ASSETWRITER_H_INC
 #define GLTF2ASSETWRITER_H_INC

+ 27 - 15
code/glTF2AssetWriter.inl

@@ -327,23 +327,35 @@ namespace glTF2 {
             obj.AddMember("doubleSided", m.doubleSided, w.mAl);
         }
 
-        /*Value v;
-        v.SetObject();
+        Value pbrSpecularGlossiness;
+        pbrSpecularGlossiness.SetObject();
         {
-            if (m.transparent && !m.diffuse.texture) {
-                m.diffuse.color[3] = m.transparency;
+            //pbrSpecularGlossiness
+
+            vec4 defaultDiffuseFactor = {1, 1, 1, 1};
+            WriteVec(pbrSpecularGlossiness, m.diffuseFactor, "diffuseFactor", defaultDiffuseFactor, w.mAl);
+
+            vec3 defaultSpecularFactor = {1, 1, 1};
+            WriteVec(pbrSpecularGlossiness, m.specularFactor, "specularFactor", defaultSpecularFactor, w.mAl);
+
+            if (m.glossinessFactor != 1) {
+                WriteFloat(obj, m.glossinessFactor, "glossinessFactor", w.mAl);
             }
-            WriteVecOrTex(v, m.ambient, m.ambient.texture ? "ambientTexture" : "ambientFactor", w.mAl);
-            WriteVecOrTex(v, m.diffuse, m.diffuse.texture ? "diffuseTexture" : "diffuseFactor", w.mAl);
-            WriteVecOrTex(v, m.specular, m.specular.texture ? "specularTexture" : "specularFactor", w.mAl);
-            WriteVecOrTex(v, m.emission, m.emission.texture ? "emissionTexture" : "emissionFactor", w.mAl);
-            v.AddMember("shininessFactor", m.shininess, w.mAl);
+
+            WriteTex(obj, m.diffuseTexture, "diffuseTexture", w.mAl);
+            WriteTex(obj, m.specularGlossinessTexture, "specularGlossinessTexture", w.mAl);
         }
-        v.AddMember("type", "commonPhong", w.mAl);
+
         Value ext;
         ext.SetObject();
-        ext.AddMember("KHR_materials_common", v, w.mAl);
-        obj.AddMember("extensions", ext, w.mAl);*/
+
+        if (!pbrSpecularGlossiness.ObjectEmpty()) {
+            ext.AddMember("KHR_materials_pbrSpecularGlossiness", pbrSpecularGlossiness, w.mAl);
+        }
+
+        if (!ext.ObjectEmpty()) {
+            obj.AddMember("extensions", ext, w.mAl);
+        }
     }
 
     namespace {
@@ -714,11 +726,11 @@ namespace glTF2 {
         Value exts;
         exts.SetArray();
         {
-            if (false)
-                exts.PushBack(StringRef("KHR_binary_glTF"), mAl);
+            //if (false)
+            //    exts.PushBack(StringRef("KHR_binary_glTF"), mAl);
 
             // This is used to export common materials with GLTF 2.
-            //exts.PushBack(StringRef("KHR_materials_common"), mAl);
+            //exts.PushBack(StringRef("KHR_materials_pbrSpecularGlossiness"), mAl);
         }
 
         if (!exts.Empty())

+ 7 - 0
code/glTF2Importer.cpp

@@ -237,6 +237,13 @@ void glTF2Importer::ImportMaterials(glTF2::Asset& r)
         aimat->AddProperty(&mat.doubleSided, 1, AI_MATKEY_TWOSIDED);
         aimat->AddProperty(&mat.alphaMode, 1, "$mat.gltf.alphaMode");
         aimat->AddProperty(&mat.alphaCutoff, 1, "$mat.gltf.alphaCutoff");
+
+        //pbrSpecularGlossiness
+        SetMaterialColorProperty(r, mat.diffuseFactor, aimat, "$clr.diffuse", 0, 1);
+        SetMaterialColorProperty(r, mat.specularFactor, aimat, "$clr.specular", 0, 1);
+        aimat->AddProperty(&mat.glossinessFactor, 1, "$mat.gltf.glossinessFactor");
+        SetMaterialTextureProperty(embeddedTexIdxs, r, mat.diffuseTexture, aimat, aiTextureType_DIFFUSE, 1);
+        SetMaterialTextureProperty(embeddedTexIdxs, r, mat.specularGlossinessTexture, aimat, aiTextureType_UNKNOWN, 1);
     }
 }