Ejemplo n.º 1
0
 /// 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)
         {
             // Only id 0 may lack a URI; this indicates that it is the binary chunk.
             Debug.Assert(!(i != 0 && buffer.uri == 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();
         }
     }
 }
Ejemplo n.º 2
0
        /// Map gltfIndex values (ie, int indices) names to the objects they refer to
        public override void Dereference(bool isGlb, IUriLoader uriLoader = null)
        {
            // "dereference" all the indices
            scenePtr = scenes[scene];
            for (int i = 0; i < buffers.Count; i++)
            {
                Gltf2Buffer buffer = buffers[i];
                buffer.gltfIndex = i;
                if (buffer.uri == null && !(i == 0 && isGlb))
                {
                    Debug.LogErrorFormat("Buffer {0} isGlb {1} has null uri", i, isGlb);
                    // leave the data buffer null
                    return;
                }

                if (uriLoader != null)
                {
                    buffer.data = uriLoader.Load(buffer.uri);
                }
            }
            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);
                }
                DereferenceTextureInfo(mat.extensions?.KHR_materials_pbrSpecularGlossiness?.diffuseTexture);
            }
            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();
                }
            }
        }