public abstract void Dereference(IUriLoader uriLoader = null, PolyFormat gltfFormat = null);
Exemplo n.º 2
0
 /// Map glTFid values (ie, string names) names to the objects they refer to
 public override void Dereference(IUriLoader uriLoader = null, PolyFormat gltfFormat = null)
 {
     // "dereference" all the names
     scenePtr = scenes[scene];
     foreach (var pair in buffers)
     {
         pair.Value.gltfId = pair.Key;
         Gltf1Buffer buffer = pair.Value;
         if (uriLoader != null)
         {
             Debug.Assert(buffer.type == "arraybuffer");
             // The .glb binary chunk is indicated by the id "binary_glTF", which must lack a uri
             // It's an error for other buffers to lack URIs.
             if (pair.Key == "binary_glTF")
             {
                 Debug.Assert(buffer.uri == null);
             }
             else
             {
                 Debug.Assert(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.
             Debug.Assert(buffer.type == "arraybuffer");
             foreach (PolyFile resource in gltfFormat.resources)
             {
                 if (resource.relativePath == buffer.uri)
                 {
                     buffer.data = new Reader(resource.contents);
                     break;
                 }
             }
         }
     }
     foreach (var pair in accessors)
     {
         pair.Value.gltfId        = pair.Key;
         pair.Value.bufferViewPtr = bufferViews[pair.Value.bufferView];
     }
     foreach (var pair in bufferViews)
     {
         pair.Value.gltfId    = pair.Key;
         pair.Value.bufferPtr = buffers[pair.Value.buffer];
     }
     foreach (var pair in meshes)
     {
         pair.Value.gltfId = pair.Key;
         foreach (var prim in pair.Value.primitives)
         {
             prim.attributePtrs = prim.attributes.ToDictionary(
                 elt => elt.Key,
                 elt => accessors[elt.Value]);
             prim.indicesPtr  = accessors[prim.indices];
             prim.materialPtr = materials[prim.material];
         }
     }
     if (shaders != null)
     {
         foreach (var pair in shaders)
         {
             pair.Value.gltfId = pair.Key;
         }
     }
     if (programs != null)
     {
         foreach (var pair in programs)
         {
             pair.Value.gltfId = pair.Key;
             var program = pair.Value;
             if (program.vertexShader != null)
             {
                 program.vertexShaderPtr = shaders[program.vertexShader];
             }
             if (program.fragmentShader != null)
             {
                 program.fragmentShaderPtr = shaders[program.fragmentShader];
             }
         }
     }
     if (techniques != null)
     {
         foreach (var pair in techniques)
         {
             pair.Value.gltfId = pair.Key;
             var technique = pair.Value;
             if (technique.program != null)
             {
                 technique.programPtr = programs[technique.program];
             }
         }
     }
     if (images != null)
     {
         foreach (var pair in images)
         {
             pair.Value.gltfId = pair.Key;
         }
         foreach (var pair in textures)
         {
             pair.Value.gltfId = pair.Key;
             var texture = pair.Value;
             if (texture.source != null)
             {
                 texture.sourcePtr = images[texture.source];
             }
         }
     }
     if (materials != null)
     {
         foreach (var pair in materials)
         {
             pair.Value.gltfId = pair.Key;
             var material = pair.Value;
             if (material.technique != null)
             {
                 material.techniquePtr = techniques[material.technique];
             }
             if (material.values != null)
             {
                 if (material.values.BaseColorTex != null)
                 {
                     material.values.BaseColorTexPtr = textures[material.values.BaseColorTex];
                 }
             }
         }
     }
     foreach (var pair in nodes)
     {
         pair.Value.gltfId = pair.Key;
         var node = pair.Value;
         if (node.meshes != null)
         {
             node.meshPtrs = node.meshes.Select(id => meshes[id]).ToList();
         }
         if (node.children != null)
         {
             node.childPtrs = node.children.Select(id => nodes[id]).ToList();
         }
     }
     foreach (var pair in scenes)
     {
         pair.Value.gltfId = pair.Key;
         var scene2 = pair.Value;
         if (scene2.nodes != null)
         {
             scene2.nodePtrs = scene2.nodes.Select(name => nodes[name]).ToList();
         }
     }
 }
Exemplo n.º 3
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();
         }
     }
 }