Code Monkey home page Code Monkey logo

Comments (4)

AzadNezamdoust avatar AzadNezamdoust commented on May 28, 2024

I have the same issue.

from gltfutility.

AzadNezamdoust avatar AzadNezamdoust commented on May 28, 2024

Code:
Importer.ImportGLTFAsync(path, new ImportSettings(),
model =>
{
onModelLoaded?.Invoke(model);
});

Model:
https://poly.google.com/view/eu-5IFEOh6C

Error:

JsonReaderException: Unexpected character encountered while parsing value: g. Path '', line 0, position 0.
Newtonsoft.Json.JsonTextReader.ParseValue () (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.JsonTextReader.Read () (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.JsonReader.ReadAndMoveToContent () (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonContract contract, System.Boolean hasConverter) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Siccity.GLTFUtility.Importer+<>c__DisplayClass9_0.b__0 () (at Assets/GLTFUtility/Scripts/Importer.cs:154)
System.Threading.Tasks.Task1[TResult].InnerInvoke () (at <df7127ba07dc446d9f5831a0ec7b1d63>:0) System.Threading.Tasks.Task.Execute () (at <df7127ba07dc446d9f5831a0ec7b1d63>:0) Rethrow as AggregateException: One or more errors occurred. System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) (at <df7127ba07dc446d9f5831a0ec7b1d63>:0) System.Threading.Tasks.Task1[TResult].GetResultCore (System.Boolean waitCompletionNotification) (at :0)
System.Threading.Tasks.Task`1[TResult].get_Result () (at :0)
Siccity.GLTFUtility.Importer+d__9.MoveNext () (at Assets/GLTFUtility/Scripts/Importer.cs:157)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

For all my models I get the same error.
Tried to get content of json string from LoadAsync method it is showing 'glTF' only, no json

from gltfutility.

AzadNezamdoust avatar AzadNezamdoust commented on May 28, 2024

Found it! 😃
Add this to Importer.cs:

    public static void ImportGLBAsync(string filepath, ImportSettings importSettings, Action<GameObject> onFinished)
    {
        byte[] bytes = File.ReadAllBytes(filepath);

        // 12 byte header
        // 0-4  - magic = "glTF"
        // 4-8  - version = 2
        // 8-12 - length = total length of glb, including Header and all Chunks, in bytes.
        string magic = Encoding.ASCII.GetString(bytes.SubArray(0, 4));
        if (magic != "glTF")
        {
            Debug.LogError("File at " + filepath + " does not look like a .glb file");
            onFinished(null);
        }
        uint version = System.BitConverter.ToUInt32(bytes, 4);
        if (version != 2)
        {
            Debug.LogError("Importer does not support gltf version " + version);
            onFinished(null);
        }
        // What do we even need the length for.
        //uint length = System.BitConverter.ToUInt32(bytes, 8);

        // Chunk 0 (json)
        uint chunkLength = System.BitConverter.ToUInt32(bytes, 12);
        // This prints out JSON. So predictable.
        //string chunkType = Encoding.ASCII.GetString(bytes.SubArray(16, 4));
        string json = Encoding.ASCII.GetString(bytes.SubArray(20, (int)chunkLength));
        // Parse json
        LoadAsync(json, filepath, importSettings, onFinished).RunCoroutine();
    }

Then use to load GLB on runtime:

using Siccity.GLTFUtility;

void ImportGLBAsync(string filepath)
{
    Importer.ImportGLBAsync(filepath, new ImportSettings(), OnFinishAsync);
}

void OnFinishAsync(GameObject result)
{
    Debug.Log("Finished importing " + result.name);
}

from gltfutility.

Siccity avatar Siccity commented on May 28, 2024

Async GLB import is now officially supported

from gltfutility.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.