Ejemplo n.º 1
0
        /// <summary>
        /// Converts the given GLTF 2 material to a Unity Material.
        /// This is "best effort": we only interpret SOME, but not all GLTF material parameters.
        /// We try to be robust, and will always try to return *some* material rather than fail,
        /// even if crucial fields are missing or can't be parsed.
        /// </summary>
        /// <param name="gltfMat">The GLTF 2 material to convert.</param>
        /// <returns>The result of the conversion</returns>
        private UnityMaterial?ConvertGltf2Material(Gltf2Material gltfMat)
        {
            Material baseMaterial; {
                string alphaMode = gltfMat.alphaMode == null ? null : gltfMat.alphaMode.ToUpperInvariant();

                if (!gltfMat.doubleSided)
                {
                    Debug.LogWarning("Not yet supported: single-sided pbr materials");
                }

                switch (alphaMode)
                {
                case Gltf2Material.kAlphaModeMask:
                    Debug.LogWarning("Not yet supported: alphaMode=MASK");
                    baseMaterial = PtSettings.Instance.basePbrOpaqueDoubleSidedMaterial;
                    break;

                default:
                case Gltf2Material.kAlphaModeOpaque:
                    baseMaterial = PtSettings.Instance.basePbrOpaqueDoubleSidedMaterial;
                    break;

                case Gltf2Material.kAlphaModeBlend:
                    baseMaterial = PtSettings.Instance.basePbrBlendDoubleSidedMaterial;
                    break;
                }
            }

            if (gltfMat.pbrMetallicRoughness == null)
            {
                Debug.LogWarningFormat("Material #{0} has no PBR info.", gltfMat.gltfIndex);
                return(null);
            }

            return(CreateNewPbrMaterial(baseMaterial, gltfMat.pbrMetallicRoughness));
        }
 /// Map gltfIndex values (ie, int indices) names to the objects they refer to
 public override void Dereference(IUriLoader uriLoader = null, PolyFormat gltfFormat = null)
 {
     // "dereference" all the indices
     scenePtr = scenes[scene];
     for (int i = 0; i < buffers.Count; i++)
     {
         Gltf2Buffer buffer = buffers[i];
         buffer.gltfIndex = i;
         if (uriLoader != null)
         {
             buffer.data = uriLoader.Load(buffer.uri);
         }
         else if (gltfFormat != null)
         {
             // Runtime import case; the uris refer to resource files in the PolyFormat.
             foreach (PolyFile resource in gltfFormat.resources)
             {
                 if (resource.relativePath == buffer.uri)
                 {
                     buffer.data = new Reader(resource.contents);
                     break;
                 }
             }
         }
     }
     for (int i = 0; i < accessors.Count; i++)
     {
         accessors[i].gltfIndex     = i;
         accessors[i].bufferViewPtr = bufferViews[accessors[i].bufferView];
     }
     for (int i = 0; i < bufferViews.Count; i++)
     {
         bufferViews[i].gltfIndex = i;
         bufferViews[i].bufferPtr = buffers[bufferViews[i].buffer];
     }
     for (int i = 0; i < meshes.Count; i++)
     {
         meshes[i].gltfIndex = i;
         foreach (var prim in meshes[i].primitives)
         {
             prim.attributePtrs = prim.attributes.ToDictionary(
                 elt => elt.Key,
                 elt => accessors[elt.Value]);
             prim.indicesPtr  = accessors[prim.indices];
             prim.materialPtr = materials[prim.material];
         }
     }
     if (images != null)
     {
         for (int i = 0; i < images.Count; i++)
         {
             images[i].gltfIndex = i;
         }
     }
     if (textures != null)
     {
         for (int i = 0; i < textures.Count; i++)
         {
             textures[i].gltfIndex = i;
             textures[i].sourcePtr = images[textures[i].source];
         }
     }
     for (int i = 0; i < materials.Count; i++)
     {
         Gltf2Material mat = materials[i];
         mat.gltfIndex = i;
         DereferenceTextureInfo(mat.emissiveTexture);
         DereferenceTextureInfo(mat.normalTexture);
         if (mat.pbrMetallicRoughness != null)
         {
             DereferenceTextureInfo(mat.pbrMetallicRoughness.baseColorTexture);
             DereferenceTextureInfo(mat.pbrMetallicRoughness.metallicRoughnessTexture);
         }
     }
     for (int i = 0; i < nodes.Count; i++)
     {
         nodes[i].gltfIndex = i;
         Gltf2Node node = nodes[i];
         if (node.mesh >= 0)
         {
             node.meshPtr = meshes[node.mesh];
         }
         if (node.children != null)
         {
             node.childPtrs = node.children.Select(id => nodes[id]).ToList();
         }
     }
     for (int i = 0; i < scenes.Count; i++)
     {
         scenes[i].gltfIndex = i;
         var thisScene = scenes[i];
         if (thisScene.nodes != null)
         {
             thisScene.nodePtrs = thisScene.nodes.Select(index => nodes[index]).ToList();
         }
     }
 }