Search Unity

[RELEASED] Piglet: glTF Importer

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

  1. DerrickBarra

    DerrickBarra

    Joined:
    Nov 19, 2013
    Posts:
    210
    @awesomesaucelabs : Yep! Anyways to reduce build sizes are always welcome in my book. Congrats on another great update!

    Here's a couple of updates from our team, tangentially related to your work.

    - The BrandXR team is looking at becoming a DAO, which helps provide a business model around open-sourcing our code.

    - Due to its open-source nature, the BrandXR player will be compatible with Web3 marketplaces and wallets.

    - The BrandXR members are exploring making our code cross-compatible with Unity3D and Godot using C#'s scripting define symbols.
     
  2. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    @DerrickBarra,

    Those sound like some big changes!

    Yes, the current lack of business models for funding open source dev is a very interesting problem, and something that I would love to see addressed! Even though Piglet is only half-assed open source (a.k.a. "source-available"), I'd much rather be doing proper open source if I could fund it. (And I'd love to see Unity go open source, too.) Generally speaking, open source seems to be a better methodology along all axes except money: kinder to users, better designs, better communities, better longevity. Maybe one day us software devs can have our cake and eat it too.

    Making your software work with both Unity and Godot would be a smooth move indeed, btw!
     
  3. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    Announcement: macOS and iOS now supported!

    Hi everyone :)

    I'm happy to announce that the Piglet glTF importer now officially supports macOS and iOS!

    Many people have been using Piglet with macOS/iOS for quite some time anyway, but now that these platforms are part of my testing routine I finally feel confident enough to advertise official support.

    To my surprise, I encountered zero problems while testing Piglet 1.3.6 on macOS and iOS, across multiple Unity versions (2018, 2019, 2020, 2021). So my hat goes off to Unity for making cross-platform compatibility a (mostly) painless experience.

    While Piglet did not require any code fixes, I did make a new PigletViewer 1.1.5 release to provide some quality-of-life improvements for iOS developers:

    - Added build instructions for macOS and iOS to README.md
    - Configured .gltf/.glb/.zip file associations on iOS (implemented in Assets/PigletViewer/Editor/iOSBuilder.cs). This allows you to open a glTF file from your Mac using AirDrop, or by dragging the glTF file onto the iOS app in Finder. (You can find the iOS app in Finder by selecting "iPhone"/"iPad" in the left sidebar and then selecting the "Files" tab).
    - Changed GUI code to automatically scale font sizes and GUI controls on iOS screens (and other HiDPI displays), since the text was rather tiny and unreadable.

    That's all for now. Thanks everyone for your support and happy hacking!

    Shout-outs

    @myspace_unity @IMC-Lab @omechano Because you enquired about iOS support. Sorry, I know that for some of you it was a long time ago!
     
    shikhrr likes this.
  4. multimediamarkers

    multimediamarkers

    Joined:
    Feb 17, 2016
    Posts:
    49
    Hi there,

    A question; i like to use Dotween to fade in/out a loaded 3D gltf model. But when i use the .Fade method i am getting an error:

    Material 'lambert1 (Instance)' with Shader 'Piglet/MetallicRoughnessOpaque' doesn't have a color property '_Color'
    UnityEngine.Material:get_color ()

    How can i fix this?

    Greetings,
    Mark
     
  5. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    Hi Mark! (@multimediamarkers)

    I haven't tested it, but I think it might be fixed by adding the
    [MainColor]
    attribute to the
    _baseColorFactor
    property in
    Assets/Piglet/Resources/Shaders/Standard/MetallicRoughnessOpaque.shader
    .

    In
    Assets/Piglet/Resources/Shaders/Standard/MetallicRoughnessOpaque.shader
    , try changing the following line from:

    Code (csharp):
    1. _baseColorFactor ("Base Color Factor", Color) = (1, 1, 1, 1)
    to:

    Code (csharp):
    1. [MainColor] _baseColorFactor ("Base Color Factor", Color) = (1, 1, 1, 1)
    I'll include that change in the next Piglet version, if it doesn't break anything during testing. (I've seen a similar issue that was fixed by adding the
    [MainTexture]
    attribute, but unfortunately that change ended up causing weird bugs in certain versions of Unity, so it wasn't worth it.)
     
  6. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
  7. multimediamarkers

    multimediamarkers

    Joined:
    Feb 17, 2016
    Posts:
    49
    @awesomesaucelabs Thanks answering my question. I will dive into it this afternoon to see if your suggestion helps.

    Thanks again,

    Mark
     
  8. multimediamarkers

    multimediamarkers

    Joined:
    Feb 17, 2016
    Posts:
    49
    @awesomesaucelabs,

    The error message disapeared but it was still not possible to fade in with dotween. I also changed the other shaders before you could set the alpha of the color with the dotween "Fade" command.
    Fade in .gif
    Above example with very slowly fade in to show the effect.
     
  9. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    @multimediamarkers,

    Hmm. Sorry to hear that the
    [MainColor]
    attribute did not work :-/

    You could also try globally replacing all occurrences of
    _baseColorFactor
    with
    _Color
    . The affected files/lines are:

    ./Assets/Piglet/Scripts/Importer/GltfImporter.cs:1572: material.SetColor("_baseColorFactor",
    ./Assets/Piglet/Resources/Shaders/Standard/MetallicRoughness.cginc:14:float4 _baseColorFactor;
    ./Assets/Piglet/Resources/Shaders/Standard/MetallicRoughness.cginc:52: fixed4 c = tex2D (_baseColorTexture, IN.uv_baseColorTexture) * IN.vertexColor * _baseColorFactor;
    ./Assets/Piglet/Resources/Shaders/Standard/MetallicRoughnessBlend.shader:28: _baseColorFactor ("Base Color Factor", Color) = (1, 1, 1, 1)
    ./Assets/Piglet/Resources/Shaders/Standard/MetallicRoughnessMask.shader:30: _baseColorFactor ("Base Color Factor", Color) = (1, 1, 1, 1)
    ./Assets/Piglet/Resources/Shaders/Standard/MetallicRoughnessOpaque.shader:28: _baseColorFactor ("Base Color Factor", Color) = (1, 1, 1, 1)
     
  10. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    @multimediamarkers,

    Oops, I just realized why the Fade routine isn't working!

    The
    MetallicRoughnessOpaque
    shader ignores the alpha channel and always renders the materials as opaque (by design). The proper solution is to change the
    alphaMode
    value in your glTF file from
    OPAQUE
    to
    BLEND
    , so that Piglet uses the
    MetallicRoughnessBlend
    shader instead of
    MetallicRoughnessOpaque
    . (The relevant section of the glTF spec is here: https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html#alpha-coverage).

    If you're unable to edit/regenerate your glTF file to use BLEND materials instead of OPAQUE, a quick-and-dirty workaround is to change the Piglet code to always use the
    MetallicRoughnessBlend
    shader instead of
    MetallicRoughnessOpaque
    .

    You could do that by changing line 1448 of
    Assets/Piglet/Scripts/Importer/GltfImporter.cs
    from:

    shader = Shader.Find("Piglet/MetallicRoughnessOpaque");


    to:

    shader = Shader.Find("Piglet/MetallicRoughnessBlend");


    You will also need to add the
    [MainColor]
    attribute to
    _baseColorFactor
    in
    MetallicRoughnessBlend.shader
    , but it sounds like you have already done that. Please my ignore my suggestion above to globally replace
    _baseColorFactor
    with
    _Color
    -- that shouldn't be necessary if the
    [MainColor]
    works as intended.

    Hope that helps!
     
    Last edited: Apr 7, 2022
  11. multimediamarkers

    multimediamarkers

    Joined:
    Feb 17, 2016
    Posts:
    49
  12. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    @multimediamarkers

    Great! Glad to hear you got it working and thanks for letting me know :)
     
  13. blaher

    blaher

    Joined:
    Oct 21, 2013
    Posts:
    80
    Love the importer but having an issue not sure how to get around. I am using a rimlight shader to mouse-over highlight imported objects. The import uses it's own Piglet shaders which seem to have different UV coords so changing to standard shader or my rimlight the textures are all in wrong positions. Ideas how to resolve this?
     
  14. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    Hi @blaher!

    Are you importing the glTF models in the Editor or at runtime?

    Are you seeing vertically flipped textures when you switch from the Piglet shader to the Standard shader (or your own shader)? Can you share a screenshot?

    During runtime imports of PNG/JPG textures, I call
    material.SetTextureScale(1, -1)
    to correct for the fact that UnityWebRequestTexture loads PNG/JPG images into textures upside down. (
    Texture2D.LoadImage
    also loads textures upside-down.)

    I used correct the problem by vertically flipping the image with a RenderTexture, but I switched to calling
    material.SetTextureScale(1, -1)
    instead because: (1) I had trouble getting the RenderTexture code to work reliably across platforms, and (2) using
    SetTextureScale
    has zero performance overhead.
     
  15. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,268
  16. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    Hi @andyz!

    I'm the author of Piglet.

    Sorry, I haven't done any detailed comparisons with glTFast or GLTFUtility, so you will have to do your own research about what works best for your project. IMO, they are all good glTF importers, and so far I don't see any "killer features" that make any one of them stand out. I'd be happy to provide a free copy of Piglet so you can include it in your comparison, btw -- just send me an email at "awesomesaucelabs" (at gmail) and I'll reply with a voucher code.

    I think Piglet is probably the most accessible option for beginners right now, because it can be installed directly from the Asset Store and it has lots of learning materials: YouTube videos, detailed manual, web demo / example viewer app. But on the other hand, if you're planning to get your hands dirty and make your own improvements/extensions to the importer, the open source options might make more sense. (I often think about making Piglet open source too, but so far I haven't figured out how to make a living that way.)

    Sorry I couldn't be more help. Since I'm a one-person operation, I feel it's better to focus my energy on supporting my customers, rather than directly trying to beat the competitors.
     
    AlanPT likes this.
  17. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    Piglet 1.3.7 released!

    Happy Friday, everyone :)

    I'm happy to announce the release of Piglet 1.3.7!

    Highlights in this release:

    * There is now a
    CreateMipmaps
    option to create mipmaps during runtime glTF imports. For now, this option is disabled by default because creating mipmaps has an extra performance cost. For further details, please see Runtime Import Options in the manual.
    * The glTF extensions KHR_materials_unlit and KHR_texture_transform are now supported. See Supported glTF Extensions for the full list of currently supported glTF extensions.
    * UWP (e.g. Hololens) is now an officially supported platform.
    * I made some important bug fixes related to texture mapping and color correctness.
    * I got rid of an annoying (but harmless) warning that happens during Editor glTF imports:
    The Animator Controller (controller) you have used is not valid. Animations will not play
    .

    Please see the Changelog for the full details!
     
  18. Mentallic3D

    Mentallic3D

    Joined:
    Aug 21, 2013
    Posts:
    35
    @awesomesaucelabs Just purchased Piglet and very happy with it in Unity. One issue we are seeing and were hoping that would could get some assistance with:

    1.) We are attempting to eventually create a WebGL system in which the user can point at a location and from a UI and select an object to load from a URL. The current tutorials show loading of the object on Start and were were wondering if you could provide some guidance on how to tie this to a button press for example. Later we can convert that button press to our UI system.
     
  19. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    Hi @Mentallic3D!

    Below is a modified version of the code from the Runtime Import Tutorial code, which delays the glTF import until the user clicks a button. The code is nearly the same, except the code from the
    Start
    method has been moved into the
    OnGUI
    method and the
    Start
    method has been removed.

    Code (csharp):
    1.  
    2. using Piglet;
    3. using UnityEngine;
    4.  
    5. /// <summary>
    6. /// This MonoBehaviour provides a minimal example for using
    7. /// Piglet to import glTF models at runtime.
    8. /// </summary>
    9. public class RuntimeImportBehaviour : MonoBehaviour
    10. {
    11.     /// <summary>
    12.     /// The currently running glTF import task.
    13.     /// </summary>
    14.     private GltfImportTask _task;
    15.  
    16.     /// <summary>
    17.     /// Root GameObject of the imported glTF model.
    18.     /// </summary>
    19.     private GameObject _model;
    20.  
    21.     /// <summary>
    22.     /// Callback that is invoked by the glTF import task
    23.     /// after it has successfully completed.
    24.     /// </summary>
    25.     /// <param name="importedModel">
    26.     /// the root GameObject of the imported glTF model
    27.     /// </param>
    28.     private void OnComplete(GameObject importedModel)
    29.     {
    30.         _model = importedModel;
    31.         Debug.Log("Success!");
    32.     }
    33.  
    34.     /// <summary>
    35.     /// Callback that is invoked by the glTF import task
    36.     /// to report intermediate progress.
    37.     /// </summary>
    38.     /// <param name="step">
    39.     /// The current step of the glTF import process.  Each step imports
    40.     /// a different type of glTF entity (e.g. textures, materials).
    41.     /// </param>
    42.     /// <param name="completed">
    43.     /// The number of glTF entities (e.g. textures, materials) that have been
    44.     /// successfully imported for the current import step.
    45.     /// </param>
    46.     /// <param name="total">
    47.     /// The total number of glTF entities (e.g. textures, materials) that will
    48.     /// be imported for the current import step.
    49.     /// </param>
    50.     private void OnProgress(GltfImportStep step, int completed, int total)
    51.     {
    52.         Debug.LogFormat("{0}: {1}/{2}", step, completed, total);
    53.     }
    54.  
    55.     /// <summary>
    56.     /// Unity callback that is invoked after every frame.
    57.     /// Here we call MoveNext() to advance execution
    58.     /// of the glTF import task. Once the model has been successfully
    59.     /// imported, we auto-spin the model about the y-axis.
    60.     /// </summary>
    61.     void Update()
    62.     {
    63.         // advance execution of glTF import task
    64.         _task?.MoveNext();
    65.  
    66.         // spin model about y-axis
    67.         if (_model != null)
    68.             _model.transform.Rotate(0, 1, 0);
    69.     }
    70.  
    71.     void OnGUI()
    72.     {
    73.         if (GUI.Button(new Rect(0, 0, 100, 30), "Import"))
    74.         {
    75.             // destroy previously loaded model
    76.             if (_model != null)
    77.                 Destroy(_model);
    78.            
    79.             // start the glTF import
    80.             _task = RuntimeGltfImporter.GetImportTask(
    81.                 "https://awesomesaucelabs.github.io/piglet-webgl-demo/StreamingAssets/piggleston.glb");
    82.             _task.OnProgress = OnProgress;
    83.             _task.OnCompleted = OnComplete;
    84.         }
    85.     }
    86. }
    87.  
    This example uses Unity's "IMGUI" system for creating the runtime UI (i.e. the button). Unity has two other systems for creating runtime UIs (see [1] for details), but I recommend starting with IMGUI because it the easiest to learn and also the quickest to implement. See "IMGUI Basics" [2] for an introduction programming with IMGUI and "Going Deep with IMGUI" [3] if you want a deeper understanding about how IMGUI works.

    Also: The source code for the Piglet web demo (a.k.a. PigletViewer) is available on Github [4], which might serve as a helpful example (or starting template) for your app. The PigletViewer UI is implemented using IMGUI, and the code is published under an MIT license, which means that you are free to reuse it in your own project.

    Hope that helps!

    [1]: https://docs.unity3d.com/Manual/UIToolkits.html
    [2]: https://docs.unity3d.com/Manual/gui-Basics.html
    [3]: https://blog.unity.com/technology/going-deep-with-imgui-and-editor-customization
    [4]: https://github.com/AwesomesauceLabs/piglet-viewer
     
  20. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    @Mentallic3D,

    I forgot to mention:

    I also changed one line in
    Update
    from:

    Code (csharp):
    1. _task.MoveNext();
    to:

    Code (csharp):
    1. _task?.MoveNext();
    That is important because
    _task
    will initially be
    null
    , until the first time the user clicks the button.
     
  21. limr19961209

    limr19961209

    Joined:
    May 10, 2022
    Posts:
    2
    I want to ask when can support KHR_ lights_ punctual
     
  22. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    Hi @limr19961209,

    I would like to add support for
    KHR_lights_punctual
    , but it probably will not happen in the near future. So far, you are only the second person to request
    KHR_lights_punctual
    , and there many other features that are in higher demand (glTF export, avatar support, etc.). Also, I am currently busy with another (top secret) Piglet-related project.

    If you badly need support for
    KHR_lights_punctual
    , there are three options:

    (1) Find a different glTF importer that already supports
    KHR_lights_punctual
    (glTFast, GLTFUtility, etc.)
    (2) Add
    KHR_lights_punctual
    support to Piglet yourself. Piglet provides the full source code, and I would be happy to provide guidance about how to modify the code.
    (3) You can pay me to add support for
    KHR_lights_punctual
    as a contract job. Please DM me or send an email to
    awesomesaucelabs at gmail
    to discuss price and deadline.
     
  23. limr19961209

    limr19961209

    Joined:
    May 10, 2022
    Posts:
    2



    I see Thank you
     
  24. travlake

    travlake

    Joined:
    Oct 4, 2019
    Posts:
    50
    @awesomesaucelabs What is the best practice when importing lots (in the 50-200 range) of models simultaneously? Should we only .MoveNext() one task at a time and queue up the rest? Or is it safe to .MoveNext() on multiple tasks in the same frame? If the latter, do you have any sense for how many is reasonable/optimal?

    (And thanks for the awesome package)
     
  25. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    Hi @travlake :)

    Thanks for the kind words about Piglet. (I've put a ton of work into it!)

    It is best to call
    MoveNext()
    on only one
    GltfImportTask
    per frame, and queue up the rest. Each call to
    GltfImportTask.MoveNext()
    tries to do a minimum of 16 ms of work per frame, by repeatedly calling
    MoveNext()
    on its underlying import tasks (
    LoadTextures
    ,
    LoadMeshes
    , etc.) until its timer runs out. So if you call
    MoveNext()
    on multiple
    GltfImportTask
    's per frame, your frame times will increase by multiples of 16 ms (32 ms, 48 ms, ...), which is not what you want!

    The variable that controls the per-frame time limit is
    MillisecondsPerYield
    , in
    Assets/Piglet/Scripts/Importer/GltfImportTask.cs
    . And you can see how the per-frame time limit is implemented by looking at the code for
    GltfImportTask.MoveNext()
    method in the same file.

    You won't get any speed boosts by running multiple
    GltfImportTasks
    at the same time, because C# coroutines don't really execute in parallel. The situation is similar to multi-tasking on a single-core CPU -- coroutines give the illusion of parallelism by time-slicing between coroutines, but in reality everything runs on the main Unity thread and only one coroutine can be executing at any given time. (The same is true for Unity's async/await tasks, btw.)

    The best way to speed up your imports is to optimize your glTF files. It is always wise to profile before optimizing, but usually the bottleneck is texture loading (PNG/JPG decompression), and you can speed that up by converting your glTF textures to a "GPU-ready" format like KTX/BasisU (see Supercompressed Textures in the Piglet manual for instructions). Of course, if your application is loading user-provided glTF files, then you won't have any opportunity to optimize/convert the textures before you load the models.

    It sounds like you're pretty comfortable with C# coroutines, but for the benefit of others I've appended a short code example for queuing up multiple glTF imports. And for those that prefer a real-world example, have a look at the GameManager class from the example PigletViewer app, which uses the same type of queueing approach.

    Hope that helps. I've never done any stress tests with loading a large number of models, so I would be interested to hear how it goes!

    Code Example: Importing Multiple glTF Models using a Queue

    Code (csharp):
    1.  
    2. using Piglet;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class MyImporter : MonoBehaviour
    7. {
    8.    private List<GltfImportTask> _importTasks;
    9.  
    10.    public void Start()
    11.    {
    12.        _importTasks = new List<GltfImportTask>();
    13.    }
    14.  
    15.    public void QueueImport(string path, Transform parent)
    16.    {
    17.        var task = RuntimeGltfImporter.GetImportTask(path);
    18.  
    19.        task.OnCompleted += (model) => {
    20.  
    21.            // This is an example of an anonymous function
    22.            // (a.k.a. "lambda function").
    23.            //
    24.            // Even though this method is called later, it still has
    25.            // access to the value of the `parent` variable that was
    26.            // passed to this method. In programming
    27.            // lingo, this is called a "closure".
    28.  
    29.            model.transform.SetParent(parent, true);
    30.        };
    31.  
    32.        _importTasks.Add(task);
    33.    }
    34.  
    35.    public void Update()
    36.    {
    37.        // Advance execution of import tasks
    38.        while (_importTasks.Count > 0 && !_importTasks[0].MoveNext())
    39.            _importTasks.RemoveAt(0);
    40.    }
    41. }
    42.  
     
    Last edited: Oct 22, 2022
    travlake likes this.
  26. thephox1982

    thephox1982

    Joined:
    Jan 21, 2022
    Posts:
    20
    We're going to go ahead and buy this asset in a couple weeks in hopes HDRP support is coming soon, no other GLTF importer handles Machinima animation system like this one and that is a requirement for our project, so here is to hoping something is on the way!
     
  27. travlake

    travlake

    Joined:
    Oct 4, 2019
    Posts:
    50
    FWIW this change you suggested much earlier in the thread was useful for me when using
    Code (csharp):
    1. material.color =
    to adjust a loaded model's color.

    Given you haven't added it to the more-recent version of Piglet, I'm guessing it breaks something? If so, do you remember what (so I know what to be worried about)?

     
  28. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    @thephox1982:

    Thanks for your interest!

    Yes, a few people have been asking about HDRP support lately. It is on my TODO list but I'm in the middle of other (Piglet-related) work, so I probably won't get to it for a couple of months at least (sorry!). Also, I estimate that implementing and testing the HDRP shaders would require a few weeks of work.

    If you need to get it done faster, there are a few options:

    (1) I'm willing to implement HDRP support by a deadline if you pay for it as a contract job. (DM me or email awesomesaucelabs at gmail to negotiate price/deadline.)
    (2) I'd be happy to provide guidance if you want to try implementing the HDRP shaders yourself. They should be pretty similar to the existing URP shaders, so you could use those as an example.
    (3) I can provide a refund if you decide to use a different glTF importer. Just let me know the e-mail address for your PayPal account, either by DM or by sending an e-mail to awesomesaucelabs at gmail.
     
  29. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    @travlake,

    Yeah, unfortunately I did see some problems.

    After adding
    [MainTex]
    to the shaders, I saw the following (repeated) console errors during drag-and-drop imports in Unity 2020.3.4f1 and Unity 2021.1.1f1:



    I assume the same would happen with the
    [MainColor]
    attribute as well.

    I found an old screenshot in my notes:

     
    Last edited: Oct 23, 2022
  30. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    @travlake,

    I could also change the property names in the Piglet shaders to
    _MainTex
    /
    _MainColor
    . However, that would break any existing prefabs in users' projects, since the old prefabs would still be referencing the old shader property names (e.g.
    _diffuseTexture
    ).

    Sometimes breaking changes are necessary, but I really try to avoid them, and I didn't feel it was worth it in this case.
     
  31. travlake

    travlake

    Joined:
    Oct 4, 2019
    Posts:
    50
    That all makes sense, thanks for the reply.

    I'm only doing runtime imports and so far using [MainColor] hasn't caused any problems.

     
  32. thephox1982

    thephox1982

    Joined:
    Jan 21, 2022
    Posts:
    20
    I'm assuming the shaders are just for the application of the materials to the meshs after import and otherwise Piglet would still do the importing of the mesh or is it a case of not being able to simply just ignore materials on importing and it failing due to errors? If the importing of the mesh is possible and any resulting errors could be ignored then I could at least make use of it.

    I did however just noticed a missing feature according to the documentation that is essential which is setting up the Machinima components which is why I picked Piglet as I read it supported that already then I read it doesn't yet, it was the whole reason I bookmarked it because I saw that it did, I believe in a Youtube video none the less, perhaps the documentation hasn't caught up there?

    Unfortunately, I don't have the ability to hire anyone right now, if I did I would certainly take you up on your offer.
     
  33. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    @thephox1982,

    Sorry I didn't reply to your post! Unfortunately I didn't receive any e-mail notification and I only noticed your message today :-/

    Yes, you are right -- the shaders are only needed for importing the materials. I never considered just disabling material imports in order to use Piglet with HDRP, but I just did a quick test and it works!

    Piglet doesn't have a user option to disable material imports, so I needed to manually change a few lines of code to disable materials. (Otherwise, both Editor glTF imports and runtime glTF imports will fail with a "render pipeline not supported" error.) The code changes are shown in the diff screenshot below, and in the attached
    hdrp-diff.txt
    file.

    hdrp-diff.png

    Once you've made the changes, you should be able to import glTF files in HDRP projects. All models/meshes will have the null material and will show as pink:

    null-material.png

    Could you describe in more detail which feature you are talking about? As far as I know, everything in the YouTube videos is working as advertised and the manual is up to date. But it's possible I made a mistake somewhere, so it's good to check.

    I'd be happy to send you free copy of Piglet so you can try it yourself without risk. Just send an e-mail to
    awesomesaucelabs
    at gmail, and I'll send you the
    piglet-1.3.7.unitypackage
    file.

    Hope that helps!
     

    Attached Files:

  34. thephox1982

    thephox1982

    Joined:
    Jan 21, 2022
    Posts:
    20
    First off I want to thank you for your offering of the code diffs and the offer to send the package which I will be considering.

    I meant Mecanim, not sure why I wrote Machinima.. I saw I believe either in the Asset Store or in your documentation that is says Mecanim isn't supported yet as in the importing and setting up of Humanoid Avatar and animations to work along with those. While the animations don't need to be setup so much, the humanoid configuration is a must as we're using root motion exclusively and the Mecanim animation system.

    Thanks again, cheers!
     
  35. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    @thephox1982,

    Ah, I see. That makes more sense now (Machinima -> Mecanim).

    Sadly, automatic creation of humanoid avatars for glTF models is not supported by Piglet. (Sorry for the bad news!) As far as I know, the only glTF importer that has avatar support TriLib, although I've never tested it myself.

    The Piglet demo/tutorial videos do indeed show character animations, but (using Unity's terminology) those are "generic animations" and not "humanoid animations". "Generic animations" contain hard-coded references to the GameObject names for a specific character model, and can't be easily reused with other character models. (For example, when you download an animated character model from Sketchfab, you get a glTF file that contains generic-style animations.)

    As you might guess, generic animations can be "retargeted" by mapping the model's GameObject names onto a "skeleton" of standard GameObject names (e.g. "Hips", "UpperLeftLeg", etc.). In Unity, the mapping from model-specific GameObject names to the standard skeleton names is stored in an "Avatar" asset.

    There are a lot of moving parts with Unity's animation systems and it can take some time to understand how it all fits together (Mecanim system, Avatar assets, Legacy system, etc.). IMO, the most effective way to understand it is to work through an example of retargeting a Mixamo animation onto an FBX model. The best walkthrough video I've seen is here. There is also a very good series by iHeartGameDev called Unity's Animation System.

    It is a bit of long story about why it's hard to add avatar/retargeting support to a glTF importer. (I did a deep dive on this subject a couple of months ago.) The main problem is that a lot of Unity's built-in functionality for creating avatar mappings (e.g. their heuristics-based automapper and their interactive avatar editor) doesn't have any public API and so isn't reusable by third parties. Unity does provide the (virtually undocumented) AvatarBuilder API, but the automapper and avatar editor are pretty important for making it useable in practice.

    All of the C# source code for the built-in automapper and interactive avatar editor is publicly available from their UnityCsReference repo. However, the Unity license doesn't permit modifying and redistributing that code as part of an asset. It might be okay if you kept the (modified) Unity avatar code inside a DLL, but it seems a bit dicey from a legal perspective (especially since C# DLLs can easily be decompiled).

    My conclusion is that the only way to support avatars in Piglet is to reimplement all of the existing avatar functionality in the Editor. It's definitely possible, but it would be a big job.
     
    Last edited: Nov 3, 2022
  36. Scarletor

    Scarletor

    Joined:
    Sep 24, 2016
    Posts:
    6
    Hello, im using piglet to load the gltf file from local and show it to scene.

    but the mesh that i get from pidglet can not mark as static and cannot get saved by batching.

    Any idea about it? I only have 1 gltf file, I use piglet to load then spawn about 100 of them but I can not save by batching it
     
  37. travlake

    travlake

    Joined:
    Oct 4, 2019
    Posts:
    50
    @awesomesaucelabs Is it possible to change the imported material's Transparency at runtime? Like the whole object so I can fade it in & out gradually.

    Using the MetallicRoughnessBlend shader I can turn down the BaseColor alpha and it fades out the main texture but there's still a grey mesh left I can't figure out how to fade:
    upload_2022-11-13_22-40-12.png
     
  38. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    Hi @Scarletor,

    I was not aware of the possibly to mark meshes as static, so that they could be batched into a single draw call. Interesting!

    I did some searching, and it seems possible to mark the meshes as static and combine them at runtime, using StaticBatchingUtility. I haven't tested it myself, though!

    I found this forum post with a code example: https://forum.unity.com/threads/changing-objects-to-from-static-at-runtime.575080/#post-7140668

    Maybe you can give it a try?

    I think the best approach is to start with a very simple test, without using Piglet or glTF files. Roughly speaking, the test would be:

    (1) Write a MonoBehaviour script to create a simple mesh at runtime (e.g. a single triangle). Catlike Coding has a tutorial about how to create a mesh at runtime.
    (2) Create a second mesh using the same method as (1).
    (3) Try to mark the two meshes as static and combine them, using the example code from the forum post above.
    (4) Check that the two meshes were combined into a single draw call, using Unity's frame debugger.

    If the above test works correctly, then the same approach can be used with Piglet-imported meshes too. (Piglet uses the same method to create meshes at runtime as the Catlike Coding tutorial.)
     
  39. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    @travlake,

    Hmmm, I'm not sure why it would be gray instead of transparent. Is gray the background color of your camera or skybox?

    Can you share your glTF file with me, so I can have a look at it? (I will keep your file private, of course!) My email address is
    awesomesaucelabs
    at gmail.

    To understand what is happening, I would try adjusting values and/or disabling textures in the Material inspector.
     
  40. badjano

    badjano

    Joined:
    Aug 2, 2012
    Posts:
    24
    I am trying to build the avatar for the model, but it seems to be loaded backwards and the bones are inverted when animated... I also see that the forward arrow is pointing back, the opposite that I have tested from other gltf loading solutions
     
  41. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    Hi @badjano!

    Yes, I know exactly what you mean... it would be better if the models were facing towards the user when they are imported (i.e. facing towards the positive Z-axis). The main reason I never fixed it is that I didn't want to give my existing users any bad surprises, where they would have to update their code/workflows because the default orientation of the models suddenly changed.

    A few months ago I did an experiment where I tried to do exactly what you are trying to do now: Create a Unity Avatar asset for a Piglet-imported glTF model, then retarget a Mixamo animation onto it. (I used "RiggedFigure" [1] as my test glTF model.) Eventually I succeeded, although it was not easy! IIRC, I struggled for about 2-3 weeks before I finally got it working. It's really hard to troubleshoot Unity's animation retargeting when things aren't working as expected (e.g. limbs bending backwards), because the Unity code is closed source and there is no documentation explaining how it works "under the hood".

    Just like you, I used the
    unity-avatar-generation
    repo [2] as my starting point. Also like you, the default backwards orientation of the Piglet-imported model caused me a lot of trouble. In my case, the key was to manually fix the orientation of the model before running my script to generate the Avatar asset. It's important to get the pose/orientation correct before generating the Avatar asset because the model transforms get "baked" into the Avatar. And just to be clear, the correct orientation is with the character facing forwards along the positive Z-axis, with feet level to the ground (i.e. the XZ plane), and with the body in a T-pose (legs together, arms pointing out).

    I hope that helps provide some encouragement and ideas!

    [1]: https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/RiggedFigure
    [2]: https://github.com/bkevelham/unity-avatar-generation
     
  42. badjano

    badjano

    Joined:
    Aug 2, 2012
    Posts:
    24
    I think you can put like a "fix orientation" on the options, because this is really important
    Can you point me to the class where I can change it to load in correct orientation?
     
  43. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    That's a good suggestion -- I'll add that option in the next release. (It might be a while before the next Piglet release, though.)

    I've been trying to find a quick fix for you, but I'm still working on it. It's not quite as easy as I thought!

    I thought that the following change would be enough, but it's not:

    failed-fix.png

    That fixes the default orientation of the model, but for some reason when I play the imported animations in the Editor, they still play with the character facing backwards. (I'm still trying to understand why.)
     
  44. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    Oh, I get it now... Actually the above fix works fine, except for the minor problem of the character facing backwards in the preview window. When you actually show the model in your game (or generate the Avatar asset with your Editor script), it should be in the correct orientation.

    The reason that the character is facing backwards in the preview window is that it doesn't include the transform of the parent "Scene" GameObject (i.e. the 180 rotation) when it is generating the preview. It only renders the subhierarchy of GameObjects that are directly referenced by the animation clip.

    The proper fix would be to change the transforms on the individual meshes, or to change the vertex coordinates in the mesh data. I'll look into that later.

    preview-backwards.jpg
     
    Last edited: Nov 24, 2022
  45. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    I forgot to tag you in my messages above, so that you would get a notification: @badjano
     
  46. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Hi I have a question before I purchase it. Does it load objects separately per material or does it keep the original structure and pivots/origins? I am using a free importer right now and it is much slower than the Kronos importer they provide, but it doesn't split it up by material, which is something I need as I need them to have the original object list and original pivots set in Blender. If I have "Car", "person" and "ground" in my scene for example, and each one has 2 or 3 materials, I need it to import at runtime with "Car", "person" and "ground" with all of their materials on it instead of split (ground_mat1, etc).
     
  47. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    Hi @carking1996,

    I received your email too, btw. (I'm replying here in case the discussion benefits anyone else.)

    Sorry, unfortunately Piglet does indeed split multi-material meshes into multiple (sibling) GameObjects. This is the most straightforward/logical way to implement a glTF importer for two reasons:

    (1) In glTF format, multi-material meshes are represented as separate "mesh primitives", where each mesh primitive has its own geometry data and material. See comment at [1] for a brief explanation.
    (2) Unity only allows one mesh per GameObject (i.e. one MeshFilter/MeshRenderer pair).

    I think it's possible to implement the behaviour you want using "submeshes" (see [2] for an example), but it would require a lot of code changes and it would add unwanted complexity to the code base, for not much benefit. I'd be happy to answer questions if you want to try to implement the changes yourself, though. Or I can provide a refund if you let me know the address for your PayPal account.

    Sorry for the bad news!

    [1]: https://github.com/KhronosGroup/glTF/issues/821#issuecomment-277036938
    [2]: https://answers.unity.com/questions/1436857/small-submesh-example.html
     
    Last edited: Nov 28, 2022
  48. Stormer2020

    Stormer2020

    Joined:
    Sep 14, 2020
    Posts:
    92
    Hi~ Thank you for the amazing plugin.
    Why the texture is blur when imported?
    I already set texture's filter mode to point.
    How fix this?
     

    Attached Files:

  49. awesomesaucelabs

    awesomesaucelabs

    Joined:
    Aug 6, 2019
    Posts:
    189
    Just a note for others: @starock2020 solved this issue by manually changing the Texture Filter setting in the URP shader from "Linear" => "Point", as shown in the screenshot below.

    Last time I checked, it wasn't possible to control this setting from the importer code, so unfortunately the user has to change it manually in the shader. The default "Linear" interpolation is usually what people want, but for pixel-art style games like Minecraft, it needs to be changed to "Point".

    Piglet has a bunch of URP shaders, but "MetallicRoughnessOpaqueOrMask" is the one that is used by most models.

    EDIT: This manual adjustment of the texture filter setting is only necessary if your project uses URP (and it is uses pixel art style graphics). Projects that use the built-in render pipeline will automatically apply the correct texture filter setting from the glTF file (linear or point).

    texture-filter-point.png
     
    Last edited: Mar 22, 2023
    Stormer2020 likes this.
  50. Stormer2020

    Stormer2020

    Joined:
    Sep 14, 2020
    Posts:
    92
    Thanks!!! I was change filter mode to point by code, dosen't work. Until change the shader graph.