Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

TriLib - Model Loading Package

Discussion in 'Assets and Asset Store' started by rickomax, Jun 21, 2017.

  1. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    TriLib 2.0 combine meshes by default and that cannot be disabled at the moment.
    Regarding the model, could you send any for testing to contato@ricardoreis.net?
     
    Last edited: Oct 20, 2020
  2. icefallgames

    icefallgames

    Joined:
    Dec 6, 2014
    Posts:
    75
    Sure, I just emailed you.
     
  3. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    The model you attached in the e-mail has GLTF validation errors.
    You can test it at:
    https://gltf-viewer.donmccurdy.com/
    I will check if I can suppress these errors but TriLib is very strict regarding GLTF validation.
     
  4. icefallgames

    icefallgames

    Joined:
    Dec 6, 2014
    Posts:
    75
    Ok, thanks for looking into it, anyway. Every glTF model I have downloaded from poly.google.com has had validation errors, it seems (The viewer at https://gltf-viewer.donmccurdy.com/ still loads them, but shows errors).

    It would be nice to have an option to suppress them somehow - those same models loaded "ok" with TriLib1.
     
  5. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    Disabling validation doesn't help at all at this moment.
    I've been testing some Google Poly models and looks like models made in Blocks have validation errors when exported, while models uploaded straight to Poly work fine in many cases.
    I'm not sure I can load data that cannot be validated, that would require a lot of effort, but I will study and test it during the week.
     
    icefallgames likes this.
  6. chrpetry

    chrpetry

    Joined:
    Mar 7, 2018
    Posts:
    65
    Well, now that I want to use Trilib 2.0 I have a problem with all my save files from Trilib 1.x. A wrapper GameObject doesn't work in my situation (n some cases I have to use direct child transforms).

    I need a rotation override on the first node. Could you please add this to your assetLoaderOptions? Without this I won't be able to upgrade to 2.0 :(

    Best regards,
    Christian
     
  7. yosun

    yosun

    Joined:
    May 18, 2017
    Posts:
    5
    trying to load a fbx from url got this issue

    An error ocurred while loading your Model: System.NullReferenceException: Object reference not set to an instance of an object
    at TriLibCore.AssetLoader.LoadModel (TriLibCore.AssetLoaderContext assetLoaderContext) [0x00063] in C:\ \Addressables-Sample-master\Addressables-Sample-master\Basic\Basic AssetReference\Assets\TriLib\TriLibCore\Scripts\AssetLoader.cs:949
    at TriLibCore.General.ContextualizedAction`1[T].Invoke () [0x00000] in <85a20f13f319482bb211f6b1b16d4f15>:0
    at TriLibCore.Utils.ThreadUtils+<>c__DisplayClass0_0`1[T].<RunThread>b__0 () [0x00011] in <85a20f13f319482bb211f6b1b16d4f15>:0
     
  8. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    I've added some code to suppress the validation issues and use accessor information to load model data. I will include the fix in the next update:
     
  9. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    It has been fixed and will be available on the next update.
     
  10. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    If you set the angles manually to the AssetLoaderContext.RootGameObject it wont work?
     
  11. Camilojr

    Camilojr

    Joined:
    Mar 26, 2014
    Posts:
    6
    Thank you Ricardo!
    I'm really a little confused about this!
    I await the tutorial!
     
    ina likes this.
  12. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,080
    Hi! Is there documentation for v2?

    How does one get the original file URI after a OnMaterialsLoad( AssetLoaderContext ) return?
     
  13. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    API Documentation will be released in the next few days.
    On your OnMaterialsLoad method, use the following code:
    Code (CSharp):
    1. var uriLoadCustomContextData = assetLoaderContext.CustomData as UriLoadCustomContextData;
    2. if (uriLoadCustomContextData != null) {
    3.     var url = uriLoadCustomContextData.UnityWebRequest.url;
    4. }
     
    ina likes this.
  14. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,080
    Ah okay... what is ` assetLoaderContext.BasePath` then? had thought that was the URl base path?
     
  15. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,080
    if the materials are adjusted in Unity, does that also work?
     
  16. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    BasePath is used when the model is loaded from local storage. It is the directory where the original model is.
     
    Last edited: Oct 22, 2020
  17. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,251
    Do you have support for loading vertex colors in an fbx model?

    Also you can not import TriLib2 without the demos I think - you get errors on some editor scripts which should be in a subfolder of demos
     
    Last edited: Oct 22, 2020
  18. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,251
    some issues with v2, though it loads everything in with good scaling now and everything in more correct facing, no big override on the root object.

    On Fbxs:
    * You put TriLibTransparent texture in empty material-map slots (not necessary), could you not do that or have it as an option as it breaks many materials for me? - I change some materials/shaders after loading (returning a list of unique materials generated would help)
    Could be more expensive material shader generated too

    Loading:
    * The root Gameobject created has a couple of scripts on for animation and asset unloading - optional?
    * The async call LoadModelFromFile - the callback names are odd - onMaterialsLoad is the 'onLoaded' callback, not 'onLoad'?! How about a version with just an onLoaded action?
     
    Last edited: Oct 22, 2020
  19. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    Sorry, I'm not sure what you mean
    Yes, vertex colors are loaded by default, but you need materials that can display them.

    You can apply your own materials to the models by creating a class that inherits the "StandardMaterialMapper" (or "UniversalRPMapper," depending on your render pipeline) and set it in your "AssetLoaderOptions.MaterialMappers" field.
     
    Last edited: Oct 22, 2020
  20. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    Thank you for the feedback!

    Regarding the material slots, they are pre-filled so that Unity will include the full shader variant (containing albedo, gloss, occlusion, normal, and emission textures) to the project build.
    It is not practical, creating thousands of variations depending on these settings.

    I'm open to suggestions regarding this subject.

    The 2.0.8 version doesn't add the animation scripts by default on the objects anymore.

    Regarding the callback names, I accept suggestions. "OnLoad" is used when the model hierarchy has been loaded, and "OnMaterialsLoad" is used when the model materials have been loaded by the end of the loading process.

    There are two callbacks because a developer can let the user manipulate the model, while the textures and materials are loading. The callback gives the developer this option.
     
  21. amanverma007

    amanverma007

    Joined:
    Dec 1, 2017
    Posts:
    1
    I am trying to download fbx via trilib from the server but I am not getting the textures fetched on it, please help!!
     
  22. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    Please send me an e-mail containing the model URL and the code you're using to load it.
    contato@ricardoreis.net
     
  23. icefallgames

    icefallgames

    Joined:
    Dec 6, 2014
    Posts:
    75
    In our case, we could have lots of user-loaded models in the scene at one time, so I'm worried about the performance of using a shader that has the full feature set. Is there a way to detect which shaders are included (assuming you're using the standard shader), and use those if possible? Or maybe we can specify which variations we include?

    I wonder how did TriLib1 solve this... If I load the same simple model, in TriLib1 it uses a Standard shader with just an albedo texture, but in TriLib2 it is using the "uber" standard shader with all features turned on.
     
  24. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    TriLib1 doesn't include all necessary shader variants, so you may have black or invalid materials when building your project.
    The first TriLib 2.0 minor versions were using Material.EnableKeyword to force Unity to find the best suitable shader for the material. The problem with this approach is that the user would have to include the Shader Variant manually in the project and that is a bit tricky and can be easily missed, so I did it the easier way where the user wouldn't have to worry. I'm thinking about, however, in including an option to allow TriLib enable/disable Material Keywords, so the user can come up with the preferable Shader Variants.
     
    icefallgames likes this.
  25. icefallgames

    icefallgames

    Joined:
    Dec 6, 2014
    Posts:
    75
    Oh, I thought of another question. Is it possible to get a list of the dependent files for a model? i.e. .mtl files or image files that are referenced by the model. TriLib 1 didn't offer this (as far as I know), so we have the limitation in our game that when a user loads a model, we assume that all files in the folder are part of that model.
     
    Last edited: Oct 22, 2020
  26. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    286
    I have the same problem as @chrpetry.
    There is a problem that it rotates 180 degrees around the Y axis.
    This can be temporarily avoided by rotating the RootGameObject, but it will be affected if you set the rotation in Quaternion.identity.
     
  27. chrpetry

    chrpetry

    Joined:
    Mar 7, 2018
    Posts:
    65
    Sadly this doesn't fix my problem. I have multiple "saves" with certain rotations set, which then would be loaded incorrectly. Also I have a default orientation in my models (pointing in x-Direction).
    I need a solution to "bake/apply" a rotation into the root transform.
     
  28. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,251
    I have a problem with empty transforms coming in with 0,0,0 angles, not the original facing in the fbx (empty transforms may exist in a model to mark attachment positions).
     
  29. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,080
    Is it possible to download a .fbx and a .jpg or .png texture separately instead of needing to use a .zip?
     
  30. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    Yes, on your "OnMaterialsLoaded" callback, you can get a reference to all loaded Materials and External Resources:

    Code (CSharp):
    1.  
    2. private void OnMaterialsLoad(AssetLoaderContext assetLoaderContext) {
    3.     if (assetLoaderContext.RootGameObject != null)
    4.         foreach (var kvp in assetLoaderContext.LoadedExternalData) {
    5.             var originalExternalResourceFilename = kvp.Key;
    6.             var mappedExternalResourceFilename = kvp.Value;
    7.         }
    8.         foreach (var kvp in assetLoaderContext.LoadedTextures) {
    9.             var originalTextureFilename = kvp.Key.Filename;
    10.             var mappedTextureFilename = kvp.Key.ResolvedFilename;
    11.         }
    12.     }
    13. }
     
    Last edited: Oct 23, 2020
  31. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    So you want to rotate the RootGameObject while preserving the children's rotation?
     
  32. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    On TriLib 1.9 or TriLib 2.0?
     
  33. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    Yes, that would require a custom "ExternalDataMapper" and a custom "TextureMapper".
    I will provide a sample on that today.
     
    ina likes this.
  34. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    286
    Thanx.
    In my case, that seems to solve the problem.
     
  35. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,080
    Also, suppose a zip file contains a number of textures and the material, but fbx does not have the materials or textures embedded - can it parse the zip for needed files?
     
  36. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    @chrpetry @gtk2k
    I could not test the code yet, but this should fix the issues.
    On your "OnMaterialsLoad" callback, use the following code:

    Code (CSharp):
    1. private void OnMaterialsLoad(AssetLoaderContext assetLoaderContext) {
    2.     if (assetLoaderContext.RootGameObject != null)
    3.     {
    4.         var transformBackups = new Dictionary<Transform, Quaternion>();
    5.         var transforms = assetLoaderContext.RootGameObject.GetComponentsInChildren<Transform>();
    6.         foreach (var childTransform in transforms) {
    7.             if (childTransform == assetLoaderContext.RootGameObject.transform) {
    8.                 continue;
    9.             }
    10.             transformBackups.Add(childTransform, childTransform.rotation);
    11.         }
    12.         assetLoaderContext.RootGameObject.transform.localRotation = Quaternion.Euler(0f,90f,0f); //sample rotation, provide the right values here
    13.         foreach (var kvp in transformBackups) {
    14.             kvp.Key.rotation = kvp.Value;
    15.         }
    16.     }
    17. }
    If you need to retrieve the right rotation from a custom source, you can use the "assetLoaderContext.CustomData" field or write it to a Dictionary before loading the model, so you can use the "assetLoaderContext.RootModel" as the Dictionary key and the Rotation as the Dictionary value.
     
    Last edited: Oct 23, 2020
  37. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    TriLib should look for textures automatically, in this case.
     
  38. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,251
    I have a problem with empty transforms coming in with 0,0,0 angles, not the original facing in the fbx (empty transforms may exist in a model to mark attachment positions).
    2.0
     
  39. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    286
    Thanx!
    This code solved the problem.
     
  40. icefallgames

    icefallgames

    Joined:
    Dec 6, 2014
    Posts:
    75
    Ah, that's great, thanks. Also, with your latest update, loading bad glTF files works much better.

    I am getting an error trying to load this obj file with TriLib2 (it works with TriLib1, but no materials load properly):
    https://3d-api.si.edu/content/docum...ce_Shuttle_OV-103_Discovery-150k-4096-obj.zip

    Code (csharp):
    1.  
    2. TriLibCore.General.ContextualizedError`1[TriLibCore.AssetLoaderContext]: A contextualized error has occurred. ---> System.ArgumentNullException: Value cannot be null.
    3. Parameter name: key
    4.   at System.Collections.Generic.Dictionary`2[TKey,TValue].FindEntry (TKey key) [0x00008] in <9577ac7a62ef43179789031239ba8798>:0
    5.   at System.Collections.Generic.Dictionary`2[TKey,TValue].TryGetValue (TKey key, TValue& value) [0x00000] in <9577ac7a62ef43179789031239ba8798>:0
    6.   at TriLibCore.Obj.ObjProcessor.ProcessMap (TriLibCore.Obj.ObjStreamReader objStreamReader, System.Single& multiplier) [0x00377] in <628fc50f25fc46558aba0c48edd9010b>:0
    7.   at TriLibCore.Obj.ObjProcessor.ProcessMaterialLibrary (System.String mtLibName) [0x0043a] in <628fc50f25fc46558aba0c48edd9010b>:0
    8.   at TriLibCore.Obj.ObjProcessor.Process (TriLibCore.Obj.Reader.ObjReader reader, System.IO.Stream stream) [0x00176] in <628fc50f25fc46558aba0c48edd9010b>:0
    9.   at TriLibCore.Obj.Reader.ObjReader.ReadStream (System.IO.Stream stream, TriLibCore.AssetLoaderContext assetLoaderContext, System.String filename, System.Action`2[T1,T2] onProgress) [0x00011] in <628fc50f25fc46558aba0c48edd9010b>:0
    10.   at TriLibCore.AssetLoader.LoadModel (TriLibCore.AssetLoaderContext assetLoaderContext) [0x000e7] in D:\ModelLoading\TestModelLoading\Assets\TriLib\TriLibCore\Scripts\AssetLoader.cs:946
    11.  
    12.  
    13.  
     
  41. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    Gonna test it now
     
  42. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    You have an empty texture name on the material file.
    I'm providing a fix and will include it in the next update.
     
    icefallgames likes this.
  43. yuki2006

    yuki2006

    Joined:
    Oct 11, 2018
    Posts:
    11
    An obj file of about 7MB can be read in about 1 second on Mac OS,
    It takes more than 10 seconds on iPad 6th (iPad OS 14). What can I do about this?
     
  44. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    I haven't profiled mobile devices yet, only tested them for bugs.
    I don't have this specific device, but I have an iPhone and will profile OBJ loading till the end of this week.
     
    yuki2006 and karonte like this.
  45. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    Here is a drawing showing TriLib 2.0 async model loading flow. The sync flow is basically the same, but with everything done in the main thread
     
    Last edited: Oct 23, 2020
    ina, yuki2006 and gtk2k like this.
  46. karonte

    karonte

    Joined:
    Jun 2, 2013
    Posts:
    48
    Hi Rickomax. thanks for your work. we are developing a simple " loader" from a database. I have one question:
    what about the Dimension of the model? i mean...i create 2/3 objects from 3dsmax/painter and export gltf/glb. When i load the objects inside unity with trilib2 they are 100 times bigger. when I try my objects with ios viewer or android viewer they are ok ( i usually work in cm). is there a way to check the dimensions? or have I to scale by 0.1 runtime my models?

    AH, just another issue... if I load a simple GLB, file it doesn't load. but if I "Zip the file (glb) it will work. so trilib works only on zipped glb?

    regards
     
  47. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    Hi!
    TriLib uses the model default scale. You can use the "assetLoaderOptions.ScaleFactor" to multiply it by any factor and the "assetLoaderOptions.UseFileScale" to disable/enable the original scale from FBX files.
    It is possible to read the scale parameters from different file formats metadata. I will include a tutorial on my website on accessing models metadata in the next days.

    TriLib should load the unzipped GLB model.
    Could you send the model to contato@ricardoreis.net?
     
    Last edited: Oct 25, 2020
    karonte likes this.
  48. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,251
    What are these zero vector logs I get on loading an fbx model in v2? (top line in console if editor)

    Code (CSharp):
    1. Look rotation viewing vector is zero
    2. UnityEngine.Quaternion:LookRotation(Vector3, Vector3)
    3. TriLibCore.Extensions.MatrixExtensions:GetMatrixRotation(Matrix4x4)
    4. TriLibCore.Fbx.FBXMatrices:TransformMatrices(Vector3&, Quaternion&, Vector3&)
    5. TriLibCore.Fbx.FBXModel:TransformMatrices()
    6. TriLibCore.Fbx.FBXProcessor:ProcessModel(FBXNode, Int64, String, String)
    7. TriLibCore.Fbx.FBXProcessor:ProcessObjects(FBXNode)
    8. TriLibCore.Fbx.FBXProcessor:Process(FBXNode, Boolean)
    9.  
     
  49. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    Probably identity rotation values. If you can, please send the model to contato@ricardoreis.net so I can check it out
     
    Last edited: Oct 28, 2020
  50. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    Maya LT PBR material workflow tutorial: