/// <summary>
        /// Fills in gltfTexture.unityTexture with a Texture2D.
        /// </summary>
        /// <param name="gltfTexture">The glTF texture to convert.</param>
        /// <param name="loader">The IUriLoader to use for loading image files.</param>
        /// <returns>On completion of the coroutine, gltfTexture.unityTexture will be non-null
        /// on success.</returns>
        private static IEnumerable ConvertTextureCoroutine(
            GltfTextureBase gltfTexture, IUriLoader loader)
        {
            if (gltfTexture.unityTexture != null)
            {
                throw new InvalidOperationException("Already converted");
            }

            if (gltfTexture.SourcePtr == null)
            {
                Debug.LogErrorFormat("No image for texture {0}", gltfTexture.GltfId);
                yield break;
            }

            Texture2D tex;

            if (gltfTexture.SourcePtr.data != null)
            {
                // This case is hit if the client code hooks up its own threaded
                // texture-loading mechanism.
                var data = gltfTexture.SourcePtr.data;
                tex = new Texture2D(data.colorWidth, data.colorHeight, data.format, true);
                yield return(null);

                tex.SetPixels32(data.colorData);
                yield return(null);

                tex.Apply();
                yield return(null);
            }
            else
            {
#if UNITY_EDITOR
                // Prefer to load the Asset rather than create a new Texture2D;
                // that lets the resulting prefab reference the texture rather than
                // embedding it inside the prefab.
                tex = loader.LoadAsAsset(gltfTexture.SourcePtr.uri);
#else
                tex = null;
#endif
                if (tex == null)
                {
                    byte[] textureBytes;
                    using (IBufferReader r = loader.Load(gltfTexture.SourcePtr.uri)) {
                        textureBytes = new byte[r.GetContentLength()];
                        r.Read(textureBytes, destinationOffset: 0, readStart: 0, readSize: textureBytes.Length);
                    }
                    tex = new Texture2D(1, 1);
                    tex.LoadImage(textureBytes, markNonReadable: false);
                    yield return(null);
                }
            }

            tex.name = SanitizeName(gltfTexture.SourcePtr.uri);
            gltfTexture.unityTexture = tex;
        }
 /// 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");
             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();
         }
     }
 }
 public IBufferReader Load(string uri)
 {
     // null uri means the binary chunk of a .glb
     uri = (uri == null) ? null : PolyRawAsset.GetPolySanitizedFilePath(uri);
     return(m_delegate.Load(uri));
 }
 /// 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();
         }
     }
 }
Exemple #5
0
 /// Map glTFid values (ie, string names) names to the objects they refer to
 public override void Dereference(bool isGlb, IUriLoader uriLoader = 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);
         }
     }
     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();
         }
     }
 }
Exemple #6
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();
                }
            }
        }