Search Unity

[RELEASED] Piglet: glTF Importer

Discussion in 'Assets and Asset Store' started by awesomesaucelabs, Jul 9, 2020.

  1. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    Hi @Pode,

    Sorry, Piglet does not support glTF 1.0, and I don't have any plans to add that.

    My understanding is that glTF 1.0 uses GLSL code for material definitions, which means that Piglet would have to somehow convert GLSL shader code to HLSL shader code (Unity's shader language) in order to support glTF 1.0 -- which is not very practical.

    There is a good discussion of converting glTF 1.0 to glTF 2.0 here: https://github.com/CesiumGS/gltf-pipeline/issues/436#issuecomment-437670702.
    That thread recommends converting from glTF 1.0 -> glTF 2.0 with https://github.com/CesiumGS/gltf-pipeline, loading into Blender, then re-exporting as glTF 2.0.

    I'm not sure that strategy would work, but it seems like your best option if you don't have access to the original 3D files for the assets (.fbx, .blend, etc.).
     
  2. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    @DerrickBarra

    I'm sorry that I didn't keep my promise to work on KTX support in November. I hope you haven't given up on me!

    I'm working hard to get the next Piglet release out, and I hope to get to your feature requests soon.
     
  3. DerrickBarra

    DerrickBarra

    Joined:
    Nov 19, 2013
    Posts:
    210
    @awesomesaucelabs : No problem! Right now we're using a modified version of GLTFUtility where we added support for KTXUnity and DracoUnity plugins.

    The big benefit of us re-enabling our #PIGLET define symbol and using your plugin instead of our #GLTFUTILITY scripting define symbol is that you're actively working on Piglet, and from what I've seen it's got a much smaller footprint and it's better optimized. I also think there's a chance your materials/shaders will be better than what we're using in GLTFUtility, but I haven't spent time comparing the shaders.

    Also, I believe @Pode was referring to WebGL 1.0, which is commonly used to support iOS Safari with Unity WebGL export (Apple is working on WebGL 2.0 support, but it isn't ready).
     
    Last edited: Dec 2, 2020
  4. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    Thanks for your patience and kind words, @DerrickBarra!

    I haven't done any detailed comparisons with GLTFUtility either. In general, I feel that my time is better spent making improvements to Piglet than doing comparisons with GLTFUtility and other competitors, although people do sometimes ask about those comparisons.

    Also, thanks for the heads up about misreading @Pode's post!
     
  5. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    Sorry about misreading your post, @Pode!

    I've tested Piglet WebGL 1.0 builds in Firefox and Chrome (both on Windows 10), and it works fine.

    However, I have not yet done any testing with MacOS/iOS/Safari. If you haven't done so already, I recommend testing a WebGL 1.0 build of the default (empty) Unity scene on your target platform and browser, just to make sure there aren't more general issues.

    I seem to recall that Unity WebGL builds on mobile browsers are not officially supported by Unity.
     
  6. Pode

    Pode

    Joined:
    Nov 13, 2013
    Posts:
    145
    @awesomesaucelabs I was indeed talking about WebGL1.0 (thanks @DerrickBarra for the clarification !)
    I'm very happy of your tool and it's working well. Indeed, when trying to use it on Safari/OSX, I need to build a WebGL1.0 app. And it seems to break when loading models (GTLF2.0 models), but I need to do some further investigations.

    Furthermore, I'm very interested in the Draco support !

    Thanks again for your work on the tool.
     
  7. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    @Pode,

    Ah, I see. Thanks for the kind words about my software, and sorry to hear it is not working on Safari/OSX/WebGL1.0 :-/

    Unfortunately, I don't own a Mac machine to test it, but perhaps I can set up a OSX VM in Windows (not sure about the feasibility of that yet).

    In the meantime, feel free to send me error messages and/or screenshots if you need help. My personal preference is to discuss issues publicly on this thread, but you can also send me private emails at "awesomesaucelabs" (at gmail).

    Also, your vote for Draco is noted!
     
  8. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    175
    Interesting project! Will keep my eye on it.

    If you need Draco support in WebGL right now (and don't need animation), feel free to try glTFast.

    https://github.com/atteneder/glTFast
     
  9. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    Nice to see you on here, @tteneder :)

    I regularly come across your projects and it seems like you do a lot of cool stuff.
     
  10. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    175
    Thanks for the flowers :)

    I love to see glTF being adopted by more users and projects. Feel free to use my stuff and reach out for support.
     
  11. Pode

    Pode

    Joined:
    Nov 13, 2013
    Posts:
    145
    @awesomesaucelabs I have another question. Is there a possibility for you to have some way to releases ressources used by the GLTF models when they are 'unloaded' ?
    The GLB files I'm seeing are heavy, with big textures. I know that it implies an heavy load on the machine. I'm downloading those GTLF files in the Unity app, at runtime. The app is used in a webpage. When the webpage ask the Unity app to close itself (Quit()), the ressources aren't freed. Do you know if there's a way to release the ressources used by the various binary assets before quitting the app ?
     
  12. ecutruin

    ecutruin

    Joined:
    Aug 19, 2014
    Posts:
    39
    Trying to find a good solution to loading glTF files for Unity and ran across your asset. I noticed that animation support was something you were working on awhile back. Has there been any update on that (perhaps I missed mention of it)?

    Specifically I'm aiming to use models created and animated in the tool Blockbench (free tool mostly used to create Minecraft models), to create models for my own game. I've attempted to use glTFUtility, but it doesn't seem to correctly load the glTF files generated from the tool, while your web demo tool DOES accurately load the models, but obviously doesn't play the animations.

    Edit:
    Read completely through this thread again, and figured I'd give Piglet a shot. Figure that I could at least animate within Unity if all else fails. However, I'm running into an issue where it does not seem pivot points come over. I created a very simple test model (with a single animation) via Blockbench and exported it to glTF to test it within Piglet.

    https://www.dropbox.com/s/y2cgrng87sc578e/Test%202.gltf

    The model loads perfectly in Microsoft 3D Viewer. Piglet seems to convert it to a prefab correctly, with the exception that pivot points seem to be lost (and animations missing, of course).

    Edit 2:
    I noticed that you mentioned potentially making it so that it would update prefabs when the corresponding glTF file was changed. This would be something I would love to see happen. Would be great to just export an updated model and have everything update within Unity.
     
    Last edited: Dec 9, 2020
  13. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    Hi @ecutruin,

    I'm very sorry that I didn't reply to your message! Normally I try to reply to Piglet inquiries within 24 hours, but somehow I missed the notification of your post :-/ (And also the notification for @Pode's message on the same day. Gah!)

    Re: animation support, I have good news for you! I'm currently in the process of submitting Piglet 1.1.0 to the Asset Store, which adds full support for animations (including skins and blendshapes). It should be out in the next couple of days and I'll give you a heads up when it's available! I've been working on animation support for ages and I'm really excited about this release.

    I'm not 100% sure what you mean by the pivot points. Do you mean the joints of the skeleton/skin (i.e. SkinnedMeshRenderer)? Piglet already loads the skeletons/skins from glTF files, even though it doesn't yet load/play the animations.

    I'll have a look at your model, either tomorrow or Monday, and see if I can understand the problem about the pivots. (I also use Microsoft 3D Viewer as ground truth.). BlockBench sounds neat... I'll probably check that out too.

    Thanks for your feedback about auto-updating the prefab when the glTF file changes! Several people have asked me about that now, so it's clearly something I need to fix/improve. I'm game to do it, but I don't think that change will come in the near future (e.g. December) because I've already promised to work on other features (e.g. supercompressed textures) before that.
     
    Last edited: Dec 13, 2020
  14. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    @Pode

    I'm sorry I didn't reply to your message! Somehow I missed the notification :-/

    Re: your question, unfortunately I'm not aware of any tricks to force a Unity webapp to release its memory. I'm surprised that Unity doesn't take care of that by default. I wonder if the memory leak happens in other browsers too? (I think you're testing in Safari?).

    To be honest, I haven't been paying attention to how much memory the Piglet WebGL demo is using, or if the memory for the old model is freed when a new model is loaded. I'll have a look at it next week and see what I can learn / figure out.
     
  15. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    Hi @ecutruin

    I had a look at your model, and it looks like Piglet is importing it correctly (including the box hinge/pivot). For example, when I import the .gltf file with the upcoming Piglet 1.1.0 version, the animation looks correct:



    If I understand correctly, you were trying to recreate the box-opening animation yourself in Unity, but having trouble getting the axis of rotation to match the original animation.

    I submitted Piglet 1.1.0 to the Asset Store today, but it may still may take a day or two before it is out, because the Unity staff manually reviews the submission and sometimes requests changes.
     
  16. ecutruin

    ecutruin

    Joined:
    Aug 19, 2014
    Posts:
    39
    That looks great! Yeah, I was trying to import the model and just animate it within Unity originally. However, the pivot points (where the object rotates around) were seemingly reset to the center of the individual objects, rather than where they were intended to be set by Blockbench.

    Seems it should no longer be an issue now that animations are supported. Models seem to be loading correctly as well, so kudos! Cannot wait to mess with it myself and start working on a game project.
     
  17. Pode

    Pode

    Joined:
    Nov 13, 2013
    Posts:
    145
    @awesomesaucelabs thanks for the answer. If there is no way for the moment to force Unity to release some memory, that means the models needs to be lighter. And Draco is the way to go, then ;)
     
  18. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    @Pode

    Indeed! Support for KTX (supercompressed textures) would help a lot too. (It is on my TODO list.)
     
  19. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    Piglet 1.1.0 Released!

    Hi Piglet users :)

    I'm happy to announce version 1.1.0 of the Piglet glTF Importer! This is a major release that adds support for importing and playing animations from glTF files, both in the Editor and at runtime. Animations of skins and blendshapes are fully supported.

    Piglet Web Demo: https://awesomesaucelabs.github.io/piglet-webgl-demo/
    Piglet Asset Store page: https://assetstore.unity.com/packages/slug/173425

    Showcase Video

    I made a video to show off the new animation capabilities:



    Tutorial Videos

    I've created some tutorial videos that demonstrate how to import and play glTF animations, both in the Editor and at runtime. There are also new sections in the manual that cover the same material.



     
    tteneder and DerrickBarra like this.
  20. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    @ecutruin

    Just a heads up that Piglet 1.1.0 is available now!
     
  21. ecutruin

    ecutruin

    Joined:
    Aug 19, 2014
    Posts:
    39
    Saw this right as I got up today. Great way to start a new day! :D Going to start messing with this right away here and will let you know if I run into any issues (loaded my box fine on the web demo already). I also shouted out your asset on the Blockbench community discord.
     
  22. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    Thanks, I appreciate that @ecutruin!
     
  23. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    FYI,

    Piglet is currently 50% off, as part of Unity's New Year Sale! (Dec 15 - Jan 7)

    This coincides nicely with the new Piglet 1.1.0 release for animation support, which just came out yesterday. (See announcement above.)
     
  24. DerrickBarra

    DerrickBarra

    Joined:
    Nov 19, 2013
    Posts:
    210
    @awesomesaucelabs Congrats on the Piglet 1.1.0 release! I'm pretty sure you're the first Unity GLTF import tool with full animation support (Skeletal & Blendshape... Mecanim & Legacy).

    I can't wait to be able to switch from our modified version of GLTFUtility once Draco and KTX are supported. Have you decided if you're going to roll your own solutions for those two or are you going to add support for @tteneder 's plugins?
     
  25. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    175
    Also congratulations for 1.1.0 from me :)

    @awesomesaucelabs If you decide to use my stuff, feel free to reach out. If you're going another route, I'd be happy to know why and use it as a chance to improve.

    thanks!
     
  26. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    @DerrickBarra @tteneder

    Thanks very much, guys!

    @DerrickBarra I don't know yet how I will implement KTX/Draco support, because I still haven't had a close look at the options. I'm going to take a rest for a day or two, but then I will get cracking on it! I know it has been a long wait for those features, so thanks for bearing with me. (I'll post an update next week, to let you know how it's going.)

    @tteneder Thanks again for your offer of support! I will indeed have a close look at your projects and see if it makes sense to use them (and provide feedback in either case). If I do end up using your work, it only seems fair that I compensate you in some way. I'll give you a heads up if/when I get to that point.
     
  27. JudahMantell

    JudahMantell

    Joined:
    Feb 28, 2017
    Posts:
    476
    Hey @awesomesaucelabs! I've been messing with Piglet for a while now and was wondering if you knew anything/plan on implementing/if even possible humanoid model support?
    I want my users to be able to import humanoid models at runtime then use FinalIK to move them around.
    In order for this to work there needs to be Avatar support for the animations and for them to be marked as humanoid.
    This might not be possible at all, but I have a feeling it would add so much to the asset.
    It would also allow retargeting of other existing animations to the imported models. It would have huge potential for making games "moddable," as I'm trying to do.
    Thanks!
     
  28. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    Hi @MidnightCoffeeInc,

    I agree that would be really cool! Being able to use animations from sites like Mixamo would be fantastic.

    At this point, I don't know what would be required to implement it, or if it is even possible. I would have to do some learning about the avatar functionality in Mecanim and how it's implemented under the hood.

    It looks like I'm going to be busy with other Piglet improvements for a while (e.g. KTX, Draco, iOS support, improving Editor importer). I will certainly investigate a bit, and let you know if I find anything interesting, though!

    It may also be possible that you could implement the needed functionality on your own, completely outside/independent of Piglet.
     
  29. JudahMantell

    JudahMantell

    Joined:
    Feb 28, 2017
    Posts:
    476
    After posting, I read in the documentation that Piglet relies on on the Legacy system and won't work with Mechanim. I'll try poking around and see if I can do it and update you as well! :)
     
  30. JudahMantell

    JudahMantell

    Joined:
    Feb 28, 2017
    Posts:
    476
    I actually had another question:
    I'm trying to load all models that are in a given folder, then do something when all the models are finished loading (not just a single one). Do you have any ideas? This might just be basic c# stuff, but I'm not sure how to implement it with Piglet.

    Here's the code I'm using:

    Code (CSharp):
    1.  private List<GltfImportTask> importTasks = new List<GltfImportTask>();
    2.  
    3. private void Update()
    4.     {
    5.         if (!isFinishedLoadingCustomModels) // This isn't used, not sure where to use it...
    6.         {
    7.             for (int i = 0; i < importTasks.Count; i++)
    8.             {
    9.                 importTasks[i].MoveNext();
    10.             }
    11.            
    12.         }
    13.     }
    14.  
    15. // THIS GETS CALLED IN ANOTHER SCRIPT
    16. public void initializeCustomModelLibrary()
    17.     {
    18.  
    19.         var filesInFolder = Directory.EnumerateFiles(modelPath, "*.*", SearchOption.AllDirectories)
    20.             .Where(s => s.EndsWith(".glb") || s.EndsWith(".gltf") || s.EndsWith(".zip")).ToArray();
    21.         for (int i = 0; i < filesInFolder.Length; i++)
    22.         {
    23.             GltfImportTask newTask = RuntimeGltfImporter.GetImportTask(filesInFolder[i]);
    24.             newTask.OnCompleted = OnModelLoaded;
    25.             importTasks.Add(newTask);
    26.         }
    27.     }
    28.  
    29.     private void OnModelLoaded(GameObject importedModel)
    30.     {
    31.         if (importedModel.GetComponent<SpawnableObject>() == null)
    32.         {
    33.             SpawnableObject SO = importedModel.AddComponent<SpawnableObject>();
    34.         }
    35.         customModelLibrary.Add(importedModel);
    36.         Debug.Log("ADDED CUSTOM MODEL TO DATABASE!");
    37.         importedModel.SetActive(false);
    38.     }
     
  31. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    @MidnightCoffeeInc

    Probably you understand this already, but just to make sure:

    Piglet can generate Mecanim or Legacy clips during Editor (drag-and-drop) imports, but can only generate Legacy clips during runtime imports.

    It is not possible to create Mecanim clips at runtime, because Unity does not support calling AnimationClip.SetCurve on Mecanim clips at runtime. This limitation has existed since Unity 4 (released in 2012), so I don't expect that Unity will fix it any time soon.
     
  32. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    @MidnightCoffeeInc

    Regarding your other question: sorry, no special tricks come to mind.

    Some possible approaches would be:

    * append each imported model to a List<GameObject> and then do your final cleanup/postprocessing code when the size of the List<GameObject> equals the size of the List<GltfImportTask>
    * increment a counter variable when you start an import and decrement the counter when you finish an import
    * periodically iterate through the List<GltfImportTask> and check if task.State == Completed for all tasks
     
    Last edited: Dec 23, 2020
  33. JudahMantell

    JudahMantell

    Joined:
    Feb 28, 2017
    Posts:
    476
    Okay, one more question, I promise! (Really sorry to bother you!)
    I found a way (sorta) to wait until all models are finished loading (code below), but I'm getting this error, stopping the whole process. Loading models from the specified path has worked fine in the past, so I don't know what's changed. BUT, the path I'm loading from is NOT the path in the error--I have no idea what that's from/about!

    pigletIssue.PNG

    This is the coroutine I'm calling yield return StartCoroutine(database.LoadAllCustomModels()); in another script.

    Code (CSharp):
    1.   public IEnumerator LoadAllCustomModels()
    2.     {
    3.         Debug.Log("MODELS LOADING");
    4.         Array.Clear(filesInFolder, 0, filesInFolder.Length);
    5.         filesInFolder = Directory.EnumerateFiles(modelPath, "*.*", SearchOption.AllDirectories)
    6.             .Where(s => s.EndsWith(".glb") || s.EndsWith(".gltf") || s.EndsWith(".zip")).ToArray();
    7.         for (int i = 0; i < filesInFolder.Length; i++)
    8.         {
    9.             GltfImportTask newTask = RuntimeGltfImporter.GetImportTask(filesInFolder[i]);
    10.             newTask.OnCompleted = OnModelLoaded;
    11.             newTask.OnProgress = OnProgress;
    12.             importTasks.Add(newTask);
    13.         }
    14.         yield return null;
    15.         while (!isFinishedLoadingCustomModels)
    16.         {
    17.             for (int i = 0; i < importTasks.Count; i++)
    18.             {
    19.                 importTasks[i].MoveNext();
    20.             }
    21.             if (filesInFolder.Length == customModelLibrary.Count)
    22.             {
    23.                 isFinishedLoadingCustomModels = true;
    24.                 Debug.Log("MODELS DONE LOADING");
    25.                 yield break;
    26.             }
    27.         }
    28.     }
     
    Last edited: Dec 23, 2020
  34. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    Hi @MidnightCoffeeInc!

    Sorry for the slow reply.

    I think I see what is happening.

    When Piglet loads a texture from a glTF file, it first writes out the PNG/JPG data to a temporary file and then reads it back in using
    UnityWebRequestTexture
    (for performance reasons).

    The name of the temp file used by each import task is supposed be randomly generated, but due to an error/misunderstanding on my part, multiple import tasks are generating the same file name and attempting to read/write from the same file.

    To fix it, change line 19 of
    Assets/Piglet/Scripts/Util/StringUtil.cs
    from:

    Code (CSharp):
    1. var random = new Random();
    to

    Code (CSharp):
    1. var random = new Random(Guid.NewGuid().GetHashCode());
    Sorry for the hassle. I'll make sure it's fixed in the next release.

    EDIT: This StackOverflow post explains the problem with my use of
    Random()
    and why it is was generating duplicate values: https://stackoverflow.com/questions...lass-to-avoid-getting-duplicate-random-values
     
    Last edited: Dec 23, 2020
  35. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    Sorry about the last garbled message. I accidentally sent it before I finished writing. (See the forum post for the complete response.)
     
  36. JudahMantell

    JudahMantell

    Joined:
    Feb 28, 2017
    Posts:
    476
    @awesomesaucelabs Thanks so much for the reply, no worries about the speed, I'm sure you're super busy!

    I changed the line, but now Unity hangs for a while, completely frozen, then when it finally picks up, I get this error:
    pigletIssue.PNG

    I'm running on a system with 32 GB of RAM, so it can't be that it legitimately ran out for a single model import, right?

    I apologize for asking so many questions, I'm nervous to mess around in the scripts myself, and no one knows the asset better than the creator! :)
     
    Last edited: Dec 23, 2020
  37. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    @MidnightCoffeeInc

    No problem!

    I'm not sure what's going on, though. Are you sure that you are only invoking
    yield return StartCoroutine(database.LoadAllCustomModels())
    once?

    If I were you, I would put some debug statements to check how many import tasks/coroutines you have running simultaneously.

    I agree. It's highly unlikely that one glTF import (or several simultaneous imports) would use 32GB RAM! More likely that you have many instances of
    LoadAllCustomModels
    running at the same time.
     
    Last edited: Dec 23, 2020
  38. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    Hi @DerrickBarra,

    Just a quick update about KTX2/Basis support.

    I've been trying out @tteneder's KtxUnity package as you suggested, and so far it's been working great for me. It's also been quite straightforward to integrate with Piglet, so I expect that the next Piglet release, coming in the next couple of weeks, will finally have KTX2/Basis support. I plan to support KtxUnity as an optional dependency, via the "Version Defines" section of the Piglet.asmdef, which I believe is what you were originally suggesting.

    I have not looked at DracoUnity yet, but I'll work on that after I've finished the release for KTX support.

    Happy Holidays, btw!
     
    Last edited: Dec 23, 2020
  39. JudahMantell

    JudahMantell

    Joined:
    Feb 28, 2017
    Posts:
    476
    Yeah, I'm definitely only calling it once. So strange.
    I guess I'll keep moving things around and see what works. Thanks for the help, I really appreciate it!
     
  40. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    @MidnightCoffeeInc

    A couple more suggestions:

    * Instead of submitting the coroutine to Unity with
    StartCoroutine
    , you could instead run it by manually calling
    MoveNext()
    on it. That might help you better understand/debug what is going on.
    * You could try running each glTF import task to completion before starting the next (rather than advancing them in parallel). When an import task is complete,
    MoveNext()
    will return false and
    importTask.State
    will be set to
    Completed
    .
     
  41. JudahMantell

    JudahMantell

    Joined:
    Feb 28, 2017
    Posts:
    476
    My apologies, I'm not the most advanced c# user, is there any way you can provide an example? Or at least in pseudocode. I understand what you're saying (I think!) but not exactly sure how to put it into practice.
     
  42. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    I think I see why your code is hanging. Try adding a
    yield return null
    line between lines 19 and 20 of the code you posted above (immediately after
    importTasks[i].MoveNext()
    ).

    Do you know how to set breakpoints in your C# code and how to step through it with a debugger? That will totally change your life, if you haven't learned how to do that yet! You'll actually be able to see what's happening in your code as it executes and you won't have to guess anymore :) What IDE/code editor are your using, btw?
     
  43. JudahMantell

    JudahMantell

    Joined:
    Feb 28, 2017
    Posts:
    476
    Woah, that worked! Crazy how waiting a single frame would fix something like that. I'm using Visual Studio that came installed with Unity I will definitely take a look at how to use Breakpoints, I've never used it before! Thanks for the reccommendation!
     
  44. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    @MidnightCoffeeInc

    Glad to hear!

    The problem was that you were stuck in that method. None of the code for your other game objects / MonoBehaviour scripts will be able to execute again until you return control to Unity with a
    yield return
    statement.
     
  45. JudahMantell

    JudahMantell

    Joined:
    Feb 28, 2017
    Posts:
    476
    Ah, I understand. Thanks!
     
  46. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    @MidnightCoffeeInc

    Thanks so much for the awesome review!! Good/bad reviews have a huge impact on my sales, especially when there are not many reviews on the asset yet, so I really appreciate it.

    Happy coding and best of luck with your project!
     
  47. JudahMantell

    JudahMantell

    Joined:
    Feb 28, 2017
    Posts:
    476
    Of course, no problem! I'm really enjoying the asset, and it's been working great for me!

    I'm having an issue with the ImportOptions though. I am generating box colliders for my imported models at runtime as well using the code below. All works fine, but often the models get imported way too big. I tried using the Import Options to limit the max size, and it works fine, but then the generated box collider comes out way too small to be usable. Any ideas?
    Thanks so much, once again.

    Let me know if anything below needs clarifying, but I think it's self-explanatory.

    Code (CSharp):
    1.     public static void SetBoxColliderBoundsToGameObjectSize(SpawnableObject SO)
    2.     {
    3.         SO.collider = SO.gameObject.AddComponent<BoxCollider>();
    4.         SO.renderer = SO.gameObject.GetComponent<MeshRenderer>();
    5.  
    6.         if (!SO.renderer)
    7.         {
    8.             SO.renderer = SO.gameObject.AddComponent<MeshRenderer>();
    9.         }
    10.  
    11.         Bounds bounds = SO.renderer.bounds;
    12.         foreach (var r in SO.gameObject.GetComponentsInChildren<Renderer>())
    13.         {
    14.             bounds.Encapsulate(r.bounds);
    15.         }
    16.         SO.collider.center = bounds.center - SO.gameObject.transform.position;
    17.         SO.collider.size = bounds.size;
    18.         SO.objectBounds = bounds;
    19.     }  
     
  48. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
    Hi @MidnightCoffeeInc,

    I think it is because
    MeshRenderer.bounds
    is an axis-aligned bounding box in world-space whereas
    BoxCollider.size
    is an axis-aligned bounding box in the local coordinate space of
    SO
    . So it's not necessarily "safe" to directly assign one to other. (The only time that works is when
    SO
    's local coordinate space is the same as world-space coordinates, i.e. not a child of any other game objects and with default position/rotation/scale values.)

    The
    GltfImportOptions.AutoScaleSize
    option works by uniformly changing the scale x/y/z values on the root game object of the model (which I assume is
    SO
    in this case). Since
    BoxCollider.size
    is in local units, changing the scale x/y/z values of
    SO
    also shrinks/grows the size of the BoxCollider in world space. (And that's how one would want
    BoxCollider
    to behave.)

    Off the top of my head, I'm not sure how best to translate the combined
    MeshRenderer.bounds
    from world space to local space. It's actually pretty tricky. See for example: https://answers.unity.com/questions/361275/cant-convert-bounds-from-world-coordinates-to-loca.html

    Maybe it would be easier to use "Compound Colliders" and let Unity do the calculations for you? (See relevant section at https://docs.unity3d.com/Manual/CollidersOverview.html). I don't have any personal experience using compound colliders, but it sounds like they don't work unless you attach a
    Rigidbody
    component to the root game object (which is fine, I guess).
     
    Last edited: Jan 4, 2021
  49. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    190
  50. JudahMantell

    JudahMantell

    Joined:
    Feb 28, 2017
    Posts:
    476
    Thanks so much for the detailed response, I will look into everything you linked!

    Edit: Follow up question. Would it be possible to move the Piglet rescaling of the object until after the box colliders are generated? Or will that produce the same result?
     
    Last edited: Jan 4, 2021