Search Unity

Mesh Baker by Digital Opus [RELEASED]

Discussion in 'Assets and Asset Store' started by Phong, Nov 20, 2012.

  1. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Hmmmm, I think I need to give this a test. Will report back later today.
     
    EiknarF likes this.
  2. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I tried this and at this time I don't think that it is possible to do this with MeshBaker. Currently all the materials need to be included in the bake. I have made a note of this problem and will try to make it possible to not-texture-array-ise some of the materials in a future release.
     
    EiknarF likes this.
  3. EiknarF

    EiknarF

    Joined:
    Apr 28, 2011
    Posts:
    142
    Thanks for looking into for me, I'll have to try to find some workarounds but nice to know it might come in the future ☺️
     
  4. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    128
    I'm trying to use Meshbaker to combine all meshes in my scene. Since objects are generated at runtime, I can't bake them in the editor. I have written a sorting function which sorts all objects with the same mesh and material, then uses a runtime baking process to bake the mesh. This approach is working and is quite fast, but the problem is that Meshbaker is adding a material for each instance, so the batch count is not being reduced as much as desired. Is there any way to resolve this issue?
     
  5. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    When you say "adding a material for each instance", do you mean one material per Mesh Baker?


    Are you combining materials first using a TextureBaker? If you do that then when you combine the props they will share a combined material. This tutorial shows how to create combined materials. This can be done at runtime but it is slow and not full featured. I would recommend creating the combined material in the editor if possible.
     
  6. EiknarF

    EiknarF

    Joined:
    Apr 28, 2011
    Posts:
    142
    If models have had their uv0.Z index already set, is it possible to use mesh baker to combine them and keep the uv0.z data?
     
  7. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I don't think so. When Mesh Baker reads the UV channel is stores it as an array of Vector2s. It would take some hacking to modify it to read, modify and write the UV channel as a Vector3.
     
    EiknarF likes this.
  8. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    Hi @Phong

    How do we save a "shared settings" file?
    upload_2023-2-10_10-36-40.png

    Also I guess the "shared settings" file has to be loaded on each newly generated baker object? I wonder if there's a way to create a universal "shared settings" file based on the scene?:rolleyes:

    upload_2023-2-10_10-39-2.png


    Thanks for the help
     
  9. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    If you right-click in any project window folder there is a menu option for creating it: There is no feature for scene based or universal settings file.
    upload_2023-2-10_10-23-3.png
     
  10. Allen0012

    Allen0012

    Joined:
    Sep 5, 2009
    Posts:
    93
    Thank you for making this awesome tool!

    I am using MeshBaker to bake player customizable characters and it works beautifully.

    Since some of the customization items overlap, for example a jacket that is worn on a t-shirt, when animated since there are many thicknesses and shapes sometimes the mesh that is inside pokes and clips through the outer layer in certain poses. For example the t-shirt mesh pokes through the jacket mesh.

    My question is, does MeshBaker have a feature that can discard vertices that are inside other mesh at the time of baking?

    If not, do you think it's doable and where would you start?

    A bit more details regarding the models. We've tried redoing the character topology to share the same vertex loops to avoid this issue as much as possible and it's improved the clipping but in special cases this still occurs and we don't have the option of optimizing the models further at this point.
     
  11. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Mesh Baker does not have a feature that does this.

    This is a hard problem. Detecting vertices that are inside other (possibly concave) shapes is difficult and slow. Also shapes like shirts are usually not watertight meshes so determining insides and outsides is even more difficult.

    I think that it is possible to do this with a shader, but it is probably expensive. If your platform is mobile or VR this is probably not a good approach.

    Another approach is to have several versions of each mesh for body parts and clothing. For example if a vest is to be worn over a long sleeve shirt then you would combine these body and clothing parts:

    • hands to wrists (no arms, shoulders)
    • no chest and torso
    • shirt (sleeves only from wrist to shoulder, no chest or torso)
    • Vest

    This would mean that a mesh like a shirt would actually be several parts:
    • wrist to shoulder
    • several different chest pieces with different collar options
    • a torso part
     
    Allen0012 likes this.
  12. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    Got it! Thank you!! :)

    ----------------------------------------
    I got a follow up question. :rolleyes:

    In terms of performance and draw calls.

    When making a mesh bake, which is preferable:
    1. creating a brand new texture atlas?
    versus
    2. using the original textures as a material array?

    This mesh bake creates a new material\ texture atlas
    upload_2023-2-11_16-15-0.png

    upload_2023-2-11_16-15-10.png


    This mesh bake uses the original material\ texture which does not create an additional texture (texture atlas). Rather it just re-uses the materials as a mat array.
    upload_2023-2-11_16-7-4.png


    Hope this makes sense.

    Mesh bake is creating a lot of texture atlases and I was wondering if it was better to just use the original materials.

    Thanks for your help
     
    Last edited: Feb 11, 2023
  13. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Good question, the material array you are describing is called multiple-materials and is accomplished by the mesh having "submeshes". These submeshes are just extra arrays of triangles in the mesh. It is almost like have several meshes conveniently combined into one. Unfortunately a mesh with submeshes doesn't save any drawcalls. Unity needs one drawcall (and setpass) for each material on a mesh. It is best to combine as many submeshes as you reasonably can.
     
  14. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    got it! Thanks for the explanation

    ---------- update ---------
    Ok so I did a quick test.

    Using the "texture atlas" method, it provides the best optimization.

    But the flexibility of material arrays is very beneficial if textures need to be updated as well as reducing the amount of new materials \textures created.

    The submeshes you mentioned, are those the 16 batches\ 8 batches that are underlined in green?

    Do you see any big risks if I stuck with the "material arrays" method?

    Thanks for the help :)

    You can see the test here
    Material Array: 16 Batches, 10 Setpass (draw?) Calls
    MaterialArray.jpg

    Texture Atlas: 8 Batches, 8 Setpass (draw?) Calls
    TextureAtlas.jpg

    Original not meshbaked: 330 Batches, 14 Setpass (draw?) Calls
    NotMeshBaked.jpg
     
    Last edited: Feb 14, 2023
  15. unity_914E13B8DE8B22A29A3E

    unity_914E13B8DE8B22A29A3E

    Joined:
    Nov 11, 2022
    Posts:
    3
    I am using mesh baker to bake mesh but its giving this error

    Object Header (UnityEngine.GameObject) has a MeshRenderer but no MeshFilter.
    at DigitalOpus.MB.Core.MB_Utility.GetGOMaterials (UnityEngine.GameObject go) [0x00052] in D:\BigImmersiveprojects\Procedural_Room_Generation\Assets\MeshBaker\scripts\core\MB_Utility.cs:54
    at MB2_TextureBakeResults.CreateForMaterialsOnRenderer (UnityEngine.GameObject[] gos, System.Collections.Generic.List`1[T] matsOnTargetRenderer) [0x00041] in D:\BigImmersiveprojects\Procedural_Room_Generation\Assets\MeshBaker\scripts\MB2_TextureBakeResults.cs:504
    at MB3_MeshBakerEditorFunctions.BakeIntoCombined (MB3_MeshBakerCommon mom, System.Boolean& createdDummyTextureBakeResults, UnityEditor.SerializedObject& so) [0x001c1] in D:\BigImmersiveprojects\Procedural_Room_Generation\Assets\MeshBaker\Editor\core\MB3_MeshBakerEditorFunctions.cs:109
    at DigitalOpus.MB.MBEditor.MB3_MeshBakerEditorInternal.bake (MB3_MeshBakerCommon mom, UnityEditor.SerializedObject& so) [0x00029] in D:\BigImmersiveprojects\Procedural_Room_Generation\Assets\MeshBaker\Editor\MB3_MeshBakerEditorInternal.cs:585
    UnityEngine.Debug:LogError (object)
    DigitalOpus.MB.MBEditor.MB3_MeshBakerEditorInternal:bake (MB3_MeshBakerCommon,UnityEditor.SerializedObject&) (at Assets/MeshBaker/Editor/MB3_MeshBakerEditorInternal.cs:614)
    DigitalOpus.MB.MBEditor.MB3_MeshBakerEditorInternal:bake (MB3_MeshBakerCommon) (at Assets/MeshBaker/Editor/MB3_MeshBakerEditorInternal.cs:539)
    DigitalOpus.MB.MBEditor.MB3_MeshBakerEditorInternal:DrawGUI (UnityEditor.SerializedObject,MB3_MeshBakerCommon,UnityEngine.Object[],System.Type) (at Assets/MeshBaker/Editor/MB3_MeshBakerEditorInternal.cs:497)
    DigitalOpus.MB.MBEditor.MB3_MeshBakerEditorInternal:OnInspectorGUI (UnityEditor.SerializedObject,MB3_MeshBakerCommon,UnityEngine.Object[],System.Type) (at Assets/MeshBaker/Editor/MB3_MeshBakerEditorInternal.cs:156)
    DigitalOpus.MB.MBEditor.MB3_MeshBakerEditor:OnInspectorGUI () (at Assets/MeshBaker/Editor/MB3_MeshBakerEditor.cs:99)
    UnityEngine.GUIUtility:processEvent (int,intptr,bool&)

    Even thought i have object with mesh filter
    upload_2023-2-14_12-32-50.png
     
  16. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    There are no big risks with using the "material array" method. The "setpass" is more important than the batches. It is "setpass" that is very expensive. I notice that you are only getting two more setpass calls with the material array approach.

    FYI: You might want to try using the "Frame Debugger Window" for this kind of analysis rather than the stats window. It is much more revealing about what is happening in each drawcall and why batches each batch was necessary.
     
  17. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    It looks like the Game Object that has the MeshRenderer but not MeshFilter is called "Header". Can you find the "Header" Game Object in your scene and check it? Please let me know if it is somehow valid (has a component that provides a mesh). I would like to ensure that Mesh Baker can handle these situations.
     
  18. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    Ok I'll do more tests once I'm ready to go head deep into optimizations!

    thanks for the help
     
  19. unity_914E13B8DE8B22A29A3E

    unity_914E13B8DE8B22A29A3E

    Joined:
    Nov 11, 2022
    Posts:
    3
    Yes I have Checked it but the issue I found is that the first parent can be ignored but if there is any other parent it don't contain any mesh filter is gives error example is a follows
    Will acept if all child have mesh filter
    upload_2023-2-24_18-4-16.png

    In this case giving error
    upload_2023-2-24_18-5-25.png
    upload_2023-2-24_18-5-44.png
     
  20. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Yes, all objects added to the "list of objects to combine" need both a MeshFilter and MeshRenderer. Are you using the "drag and drop" box to add the objects?
     
  21. Kozaki2

    Kozaki2

    Joined:
    Apr 8, 2019
    Posts:
    47
    Hi, I am trying to bake my character skinned mesh with some clothes on but I get an error and I have no idea what i am doing wrong. Debugger tells me that my variable dgoBindPoses is Matrix4x4[142] and dgoBones is Transform[156] and the script is trying to get dgoBindPoses[142] and so on. Looks like those arrays should be the same size?
     
  22. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Hmmm, this does look like a bug. These arrays do need to be the same size. Would it be possible to share the baking setup so I can take a look at it? It is much easier to debug with a reproducible case. You can email me directly at contact.digitalopus@gmail.com.
     
  23. Dev_UHG

    Dev_UHG

    Joined:
    Jun 5, 2017
    Posts:
    28
    hi there just started with Mesh Baker to optimize a large level. I tried a first testrun with some trash prefabs to combine materials (2 materials) but somehow the combined one has swapped colors. Also it ask me to fix normals: if I ignore it seems to work, if I choose one I get a black mesh.
    Original:

    upload_2023-3-15_23-51-9.png


    optimized (following your you tube video) Apart from swapped the writing in the red is a little bit more washed out)
    upload_2023-3-15_23-53-25.png


    thank for any help!
     
  24. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Are you original materials using color tints? That could explain the color changing. I would suggest trying the "Blend Non Texture Properties feature". There is a tutorial video showing how to use it. Can you post the exact text of the fix-normals message?
     
  25. Dev_UHG

    Dev_UHG

    Joined:
    Jun 5, 2017
    Posts:
    28
    The material looks like this:
    upload_2023-3-16_0-43-8.png
     
  26. Dev_UHG

    Dev_UHG

    Joined:
    Jun 5, 2017
    Posts:
    28
    the other thing happens here:
    upload_2023-3-16_0-47-10.png
     
  27. jnbbender

    jnbbender

    Joined:
    May 25, 2017
    Posts:
    487
    I am following the tutorial to simply create texture atlases. I use the picker tool to select my common shaders & materials. But when I select "Bake Materials into Combined Material" I get no atlas. The MeshBaker combined material appears to just be a copy of another single material with the original Textures.

    upload_2023-3-16_11-20-7.png
     
  28. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Regarding the color swapping. Are you baking the textures and meshes using "consider UVs"? I think the problem may be UVs outside the 0..1 range. Your setup looks fairly straightforward so this should work. There are three causes I can think of:
    • consider UVs
    • color tint
    • when you bake textures, be sure to also bake meshes
    Regarding the normal map. Check your source materials that have a normal map assigned. Check the normal map texture that is assigned. It should be marked as "normal map". If it is not then your texture may not work properly as a normal map. Does this fix the problem?:

    upload_2023-3-16_9-48-4.png
     
    Dev_UHG likes this.
  29. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Are there any red error messages in the console? There should also be a line in the console after a TextureBake called "Report". That line should summarize which textures were found and baked into the atlas. What does that line say?

    I see that there are 3515 objects in the list of objects to combine. That is a lot of objects. Are there a huge number of distinct materials?
     
  30. jnbbender

    jnbbender

    Joined:
    May 25, 2017
    Posts:
    487
    A video is worth a thousand words ;)
     

    Attached Files:

  31. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Thanks for sending the video. I think I see what is going on. You are grouping by "material":

    upload_2023-3-16_10-40-57.png

    Each grouping (generated TextureBaker) bakes objects in the scene that use the same material. If all source objects already share a material, then the generated atlas will look exactly like the source material.

    If you group by "static", "shader" (remove "Material") then you will get groups that include different materials. Your atlas should combine different materials. However it looks like your scene has a lot of different materials and tiling. You will probably need to split the groups further to get good results.
     
  32. jnbbender

    jnbbender

    Joined:
    May 25, 2017
    Posts:
    487
    That makes sense. I'm not to swift on the uptake. Thanks!
     
    Phong likes this.
  33. Dev_UHG

    Dev_UHG

    Joined:
    Jun 5, 2017
    Posts:
    28
    The ConsiderUV Solved it.
    upload_2023-3-16_22-2-26.png
    The normals are marked as normals. I changed the normals to provide the some one named NormalDX instead of NormalGL (I do not know what internally the GL does in comparison to the directX one) but for now this question did not reappear.
    thanks for your support! I will play around more before starting to optimize my scene.
     
    Phong likes this.
  34. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Hi GrandMini,

    The problem is caused by a SkinnedMesh with a "null" bone.

    This is probably what happend:

    1) Skinned mesh was added to the unity project as a .fbx or .blend file. This shows up in the Unity project as a "ModelPrefab" with a special inspector for setting up Avators and importing animations.
    2) This ModelPrefab was instantiated in the scene and cleaned up:
    • Default materials embedded in the ModelPrefab replaced by materials in the Unity project folder
    • Unused transforms deleted
    3) The "cleaned up" skinned mesh instance is saved as a new prefab.
    4) The new prefab is used in Mesh Baker

    The problem is "Unused transforms deleted" marked in red above. Some of the deleted transforms were used by the skinned mesh as "bones". Mesh Baker needs valid references to bones/transforms to bake skinned meshes properly.
     
  35. GrandMini_Studios

    GrandMini_Studios

    Joined:
    Jul 11, 2022
    Posts:
    1
    Hello!

    Thank you very much for the quick reply. Is it possible to change the animations individually for each skinned mesh renderer after baking? I'm guessing it's not possible since they aren't separate anymore?

    Kind regards,

    GMS
     
    Last edited: Mar 19, 2023
  36. Hobodi

    Hobodi

    Joined:
    Dec 30, 2017
    Posts:
    101
    Hi. Is it possible to use agglomerative cluster type to bake meshes without textures?
     
  37. Immersive-Matthew

    Immersive-Matthew

    Joined:
    Mar 24, 2020
    Posts:
    137
    I am getting this error with just the out of box Unity cube mesh. Does it really not come with a UV?
     
  38. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Yes you can do this. The combined mesh is strange in that it usually has more than one Animator. The combined mesh shares the same Animators as the original original source skinned meshes. Whatever scripts were switching the animations on the original source animators will still work for the combined mesh.
     
  39. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Yes, this works. Use this workflow:

    1) Add a "GameObject->Mesh Baker->TextureBaker" or GameObject->Mesh Baker->TextureBaker and MeshBaker to your scene.
    2) Add all objects that you want to group to the TextureBaker. You don't need to create any combined material assets
    3) Do the agglomerative clustering
    4) Click "Generate Mesh Bakers" (delete existing child bakers if necessary)
    5) You can "Bake" each of the the generated child mesh bakers without having baked the parent texture baker. If the objects that you are combining use different materials, then the combined mesh will have one submesh per material.
     
    Hobodi likes this.
  40. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Hello,

    I will try to explain what is happening.

    Consider two meshes:
    • A single-brick-mesh that uses a small UV rectangle (single-brick) of a brick-wall.png texture.
    • A brick-wall-mesh that tiles the same brick-wall.png texture three times.
    If you then use the TextureBaker to create a texture atlas including the single-brick-mesh but NOT the brick-wall-mesh and you use the "considerUVs" feature. In this case the TextureBaker copies only the part of the the brick-wall.png texture to the texture atlas (a single brick).

    THE PROBLEM: If you now try to use the same atlas in a MeshBaker-bake with the brick-wall-mesh it will not work, because the brick-wall-mesh needs much more of the brick-wall.png texture than was copies to the atlas. It needs the entire brick-wall-mesh tiled three times.

    THE SOLUTION: If you include both the "single-brick-mesh" and the "brick-wall-mesh" in the original texture bake, then the TextureBaker will copy the entire brick-wall.png to the atlas tiled three times. This atlas rectangle will work for both the single-brick-mesh and the brick-wall-mesh.

    You need to include the default Unity cube with the material you want on the cube in the original texture bake. That will fix this error.
     
  41. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    Hi @Phong

    Mesh Baker Version 3.35.1

    I'm running into 2 bugs \ problems:

    1) I'm getting weird normal map results when I use the fast bake option. Are you getting the same results? Do you have any advice?:oops: If I don't use the "fast" option, I get a correct normal map. But it takes much longer to bake.
    1.jpg 2.jpg


    ----UPDATE SOLVED---
    2) I can't seem to get objects with material arrays to bake. I get this red error . But you can see that the material is clearly in the material array. Do you know what I'm doing wrong?
    Illustration.jpg

    For anyone else that runs into this error. The way to fix it is to delete the material that's created by Generate Baker.:)
    55.jpg

    Thanks for your help :)
     
    Last edited: Mar 26, 2023
  42. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Hello,

    Regarding the bad normal map, it looks like a color space issue. Can you tell me your project settings are:
    • Build platform?
    • Color Space (linear or gamma)?
    • Render Pipeline
    • Deferred or Forward rendering?
    Also what shader are you using? Does the normalMap parameter have an unusual name? Normal Maps get special treatment in Unity and Mesh Baker. Mesh Baker needs to know that that texture parameter is a normal map. It assumes that "_Bumpmap", "_Normalmap" are normal maps, but if your parameter has a different name it needs to be told that it is a normal map. You can do this in the "Custom Shader Property Names" list.
     
    seth01plays likes this.
  43. seth01plays

    seth01plays

    Joined:
    Oct 3, 2022
    Posts:
    3
    Hello @Phong ,

    I came to your thread to say 2 things:
    1. I just want to leave a thankyou for this amazing asset, and that is has saved me in many instances of complex scenes and models. Mesh Combiner is being used on in help with the gore system I've made for the game that I'm making at the moment.

    2. I also came here to ask a question. I've tried to go though the past 70 pages worth of conversations here to find an answer to solve disabled objects to not render its specific object on the skinned mesh. I have tried deleting object (that is supposed to be a disabled object) to prevent it from rendering, though this works, I need objects reenabled later in gameplay (example: Player changes a barrel type on a gun, or the gun parts change model as that part of it being damaged). Is this possible to achieve?

    best Regards,
    Seth01Plays (aka: TheLocalBogan)

    Examples below of what I'm talking about:
    As shown below on the "Hierarchy" there are Optics revealed on the gun when combined spite the child objects are disabled.
    upload_2023-3-28_20-44-57.png

    (Not mesh combined) what a gun should look like with certain child objects disabled:
    upload_2023-3-28_20-40-49.png

    (Is mesh combined) what the gun look like with certain child objects disabled:
    upload_2023-3-28_20-42-16.png
     
  44. SamuelBeijerNoid

    SamuelBeijerNoid

    Joined:
    Jan 16, 2023
    Posts:
    1
    Hello @Phong

    I'm trying to run the texture baking as a coroutine to not cause frame drops, but variable for maxTimePerFrame does seems to do anything. I followed it to MB3_TextureCombiner.CombineTexturesIntoAtlasesCoroutine().

    There seem to be a check that the value is greater than 0, but I can see that the coroutine use it to yield and limit the workload.

    Code (CSharp):
    1. //float _maxTimePerFrameForCoroutine;
    2.         public IEnumerator CombineTexturesIntoAtlasesCoroutine(ProgressUpdateDelegate progressInfo, MB_AtlasesAndRects resultAtlasesAndRects, Material resultMaterial, List<GameObject> objsToMesh, List<Material> allowedMaterialsFilter, List<string> texPropsToIgnore, MB2_EditorMethodsInterface textureEditorMethods = null, CombineTexturesIntoAtlasesCoroutineResult coroutineResult = null, float maxTimePerFrame = .01f, List<AtlasPackingResult> packingResults = null, bool onlyPackRects = false, bool splitAtlasWhenPackingIfTooBig = false)
    3.         {
    4.             if (!_RunCorutineWithoutPauseIsRunning && (MBVersion.GetMajorVersion() < 5 || (MBVersion.GetMajorVersion() == 5 && MBVersion.GetMinorVersion() < 3)))
    5.             {
    6.                 Debug.LogError("Running the texture combiner as a coroutine only works in Unity 5.3 and higher");
    7.                 yield return null;
    8.             }
    9.             coroutineResult.success = true;
    10.             coroutineResult.isFinished = false;
    11.             if (maxTimePerFrame <= 0f)
    12.             {
    13.                 Debug.LogError("maxTimePerFrame must be a value greater than zero");
    14.                 coroutineResult.isFinished = true;
    15.                 yield break;
    16.             }
    17.             //_maxTimePerFrameForCoroutine = maxTimePerFrame;
    18.             yield return _CombineTexturesIntoAtlases(progressInfo, coroutineResult, resultAtlasesAndRects, resultMaterial, objsToMesh, allowedMaterialsFilter, texPropsToIgnore, textureEditorMethods, packingResults, onlyPackRects, splitAtlasWhenPackingIfTooBig);
    19.             coroutineResult.isFinished = true;
    20.             yield break;
    21.         }

    Is there an other way create merge textures so it doesn't block the main thread?

    /Samuel
     
  45. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Hi Seth01Plays,

    There are a few options to hide parts of a skinned mesh (eg. a gun that a character is holding). There is no great way to do this. This is difficult because the verticies of the gun are in the mesh along with boneweights, a bindpose and bone reference.

    1) You can re-bake the mesh removing the gun from the list of objects to combine.
    2) You can hide the gun by moving it outside the camera fustrum. Note that the gun will still be skinned so it is still incurring some performance cost.
    3) You could bake the combined mesh with a separate material for the gun. Removing the material from the SkinnedMeshRenderer.materials list will hide it. However having an extra material does not save a drawcall so it is no different from using a separate MeshRenderer for the gun.

    You mentioned that deleting the gun transform works. I am surprised to hear this. I had assumed that you would get malformed skinned mesh. If this works then you should be able to hide the gun (without deleting the gameObject) by:
    • Get the bones array from the SkinnedMeshRenderer.bones
    • Find the "gun" transform in the bones array and set it to null
    • Assign the bones array back to the SkinnedMeshRenderer.bones
    You should be able to restore the gun later by putting the "gun" transform reference back in the bones array.
     
  46. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Hi Samuel, you are correct that I never finished implementing the maxTimePerFrame feature. That parameter does not do anything at this time.

    It is currently not possible to run the TextureCombiner off the main thread. I am currently exploring jobifying some of the MeshBaker features, however it is tough going. The TextureCombiner interacts with a lot of Texture2Ds. Most of the Texture2D API can only be used from the main thread. It will probably be six months before some of these features are released and they will only work with newer versions of Unity (2021+ or 2022+).
     
  47. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    hi @Phong

    Sure please find answers below
    • Build platform? -- Windows 10
    • Color Space (linear or gamma)? -- Linear
    • Render Pipeline -- URP
    • Deferred or Forward rendering? -- Forward
    I'm using the URP Lit shader. The normal map textures are set as normal maps. (Not default or Sprite\UI etc) If I don't use the "Fast" option the normal maps get baked properly. It's weird.. I'd love to be able to use the "Fast" option as it makes the textures much faster than the regular method. But if I'm the only one experiencing this issue.. then I just have to stick with the regular method. :oops:

    Thanks for the help.
     
  48. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Thanks for the information. I will see if I can reproduce the issue on my end and report back. It may take a day or two, so I may not get back to you until early next week.

    UPDATE:

    I was able to reproduce the problem. Try using: TexturePacker_Fast_V2_Beta. I was able to bake correctly using the new version of the TexturePacker_Fast_V2. I will take a look at the old TexturePacker to see if I can identify the problem, however I expect that it may not be fixable. The reason that I created the new TexturePackerFast was to get around issues I was having with URP & HDRP pipelines with linear color spaces and RenderTextures.
     
    Last edited: Mar 31, 2023
  49. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    Version I tested it on
    upload_2023-4-6_13-35-26.png
    upload_2023-4-6_13-49-57.png

    OK it works! Definitely faster. :D:DI had to change the Atlas Render Layer to the one I'm using. I did get some red errors though. (pls see below)
    upload_2023-4-6_13-29-5.png
    Code (CSharp):
    1. Layer index out of bounds
    2. 0x00007ff62ff2c77d (Unity) StackWalker::GetCurrentCallstack
    3. 0x00007ff62ff33339 (Unity) StackWalker::ShowCallstack
    4. 0x00007ff630ea0003 (Unity) GetStacktrace
    5. 0x00007ff63153fe1d (Unity) DebugStringToFile
    6. 0x00007ff62f7c47a8 (Unity) TagManager::LayerToString
    7. 0x00007ff62f593af5 (Unity) InternalEditorUtility_CUSTOM_GetLayerName
    8. 0x000001d747f1feda (Mono JIT Code) (wrapper managed-to-native) UnityEditorInternal.InternalEditorUtility:GetLayerName (int)
    9. 0x000001d747f312cb (Mono JIT Code) UnityEditor.EditorGUI:LayerFieldInternal (UnityEngine.Rect,UnityEngine.GUIContent,int,UnityEngine.GUIStyle)
    10. 0x000001d747f30cf3 (Mono JIT Code) UnityEditor.EditorGUI:LayerField (UnityEngine.Rect,UnityEngine.GUIContent,int,UnityEngine.GUIStyle)
    11. 0x000001d7480fdf93 (Mono JIT Code) UnityEditor.EditorGUILayout:LayerField (UnityEngine.GUIContent,int,UnityEngine.GUIStyle,UnityEngine.GUILayoutOption[])
    12. 0x000001d7480fde7b (Mono JIT Code) UnityEditor.EditorGUILayout:LayerField (UnityEngine.GUIContent,int,UnityEngine.GUILayoutOption[])
    13. 0x000001d747fd2c9b (Mono JIT Code) [MB3_TextureBakerEditorInternal.cs:397] DigitalOpus.MB.MBEditor.MB3_TextureBakerEditorInternal:DrawGUI (UnityEditor.SerializedObject,MB3_TextureBaker,UnityEngine.Object[],System.Type)
    14. 0x000001d747fd0c63 (Mono JIT Code) [MB3_TextureBakerEditor.cs:38] DigitalOpus.MB.MBEditor.MB3_TextureBakerEditor:OnInspectorGUI ()
    15. 0x000001d7479b5654 (Mono JIT Code) UnityEditor.UIElements.InspectorElement/<>c__DisplayClass59_0:<CreateIMGUIInspectorFromEditor>b__0 ()
    16. 0x000001d70b88aed7 (Mono JIT Code) UnityEngine.UIElements.IMGUIContainer:DoOnGUI (UnityEngine.Event,UnityEngine.Matrix4x4,UnityEngine.Rect,bool,UnityEngine.Rect,System.Action,bool)
    17. 0x000001d70b889fbb (Mono JIT Code) UnityEngine.UIElements.IMGUIContainer:HandleIMGUIEvent (UnityEngine.Event,UnityEngine.Matrix4x4,UnityEngine.Rect,System.Action,bool)
    18. 0x000001d70b887cc3 (Mono JIT Code) UnityEngine.UIElements.IMGUIContainer:DoIMGUIRepaint ()
    19. 0x000001d70b885be4 (Mono JIT Code) UnityEngine.UIElements.UIR.RenderChainCommand:ExecuteNonDrawMesh (UnityEngine.UIElements.UIR.DrawParams,single,System.Exception&)
    20. 0x000001d70b883f23 (Mono JIT Code) UnityEngine.UIElements.UIR.UIRenderDevice:EvaluateChain (UnityEngine.UIElements.UIR.RenderChainCommand,UnityEngine.Material,UnityEngine.Material,UnityEngine.Texture,UnityEngine.Texture,single,Unity.Collections.NativeSlice`1<UnityEngine.UIElements.UIR.Transform3x4>,Unity.Collections.NativeSlice`1<UnityEngine.Vector4>,UnityEngine.MaterialPropertyBlock,bool,System.Exception&)
    21. 0x000001d70b8806e3 (Mono JIT Code) UnityEngine.UIElements.UIR.RenderChain:Render ()
    22. 0x000001d70b83e8c3 (Mono JIT Code) UnityEngine.UIElements.UIRRepaintUpdater:Update ()
    23. 0x000001d70b80f626 (Mono JIT Code) UnityEngine.UIElements.VisualTreeUpdater:UpdateVisualTreePhase (UnityEngine.UIElements.VisualTreeUpdatePhase)
    24. 0x000001d70b80f4fb (Mono JIT Code) UnityEngine.UIElements.Panel:UpdateForRepaint ()
    25. 0x000001d70b81ebeb (Mono JIT Code) UnityEngine.UIElements.Panel:Repaint (UnityEngine.Event)
    26. 0x000001d70b81e31f (Mono JIT Code) UnityEngine.UIElements.UIElementsUtility:DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel)
    27. 0x000001d70b81e003 (Mono JIT Code) UnityEngine.UIElements.UIElementsUtility:UnityEngine.UIElements.IUIElementsUtility.ProcessEvent (int,intptr,bool&)
    28. 0x000001d70b81ddbf (Mono JIT Code) UnityEngine.UIElements.UIEventRegistration:ProcessEvent (int,intptr)
    29. 0x000001d70b81dcdb (Mono JIT Code) UnityEngine.UIElements.UIEventRegistration/<>c:<.cctor>b__1_2 (int,intptr)
    30. 0x000001d70b81db3d (Mono JIT Code) UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
    31. 0x000001d70b81dbf6 (Mono JIT Code) (wrapper runtime-invoke) <Module>:runtime_invoke_void_int_intptr_intptr& (object,intptr,intptr,intptr)
    32. 0x00007ffd1ceffeb4 (mono-2.0-bdwgc) [mini-runtime.c:3445] mono_jit_runtime_invoke
    33. 0x00007ffd1ce3e764 (mono-2.0-bdwgc) [object.c:3066] do_runtime_invoke
    34. 0x00007ffd1ce3e8fc (mono-2.0-bdwgc) [object.c:3113] mono_runtime_invoke
    35. 0x00007ff62fe51104 (Unity) scripting_method_invoke
    36. 0x00007ff62fe309a4 (Unity) ScriptingInvocation::Invoke
    37. 0x00007ff62fe2b5b5 (Unity) ScriptingInvocation::Invoke<void>
    38. 0x00007ff62ff757da (Unity) Scripting::UnityEngine::GUIUtilityProxy::ProcessEvent
    39. 0x00007ff63097fbc8 (Unity) GUIView::ProcessRetainedMode
    40. 0x00007ff630ed06eb (Unity) GUIView::OnInputEvent
    41. 0x00007ff63097f9da (Unity) GUIView::ProcessInputEvent
    42. 0x00007ff630ec94fe (Unity) GUIView::DoPaint
    43. 0x00007ff630ed278d (Unity) GUIView::RepaintAll
    44. 0x00007ff630a54157 (Unity) PlayerLoopController::UpdateScene
    45. 0x00007ff630a5215b (Unity) Application::TickTimer
    46. 0x00007ff630ea619a (Unity) MainMessageLoop
    47. 0x00007ff630eaaa5b (Unity) WinMain
    48. 0x00007ff63224cade (Unity) __scrt_common_main_seh
    49. 0x00007ffd99837614 (KERNEL32) BaseThreadInitThunk
    50. 0x00007ffd9a6426a1 (ntdll) RtlUserThreadStart
    51.  
    You can see the lighter cement brick(Right side) is the original and the slightly darker one(Left side) is the mesh baked version.:)
    upload_2023-4-6_13-30-2.png


    Also I know I asked earlier, but again if there is some way Mesh Bake could allow us to change the final image extension it'd be much appreciated.:oops:

    Currently I use photoshop to convert the emission, specular, normal map PNG files into JPG. (Because JPG are smaller in file size compared to PNG) I also fill in the transparent areas so that the JPG won't have white backgrounds in place of the transparent areas. (This is troublesome for emission\specular\metallic maps in particular)

    In a single instance editting the files and reapplying them into the respective material is not a big deal, but when I have to mesh bake hundreds of art assets, it can become manually intensive.:(:(

    I leave the albedo\diffuse textures as PNG to maintain the transparencies though.

    Am I the only person asking for this feature? Is everyone else using some tricks that I'm not aware of? :rolleyes: Or they don't care about file size? :eek:

    I found this particular Github discussion about converting PNG to JPG and converting the transparent zones to black color. (Albeit their discussion is that it's a bug ) Maybe you'd find the code useful in allowing us to save as JPG for certain textures?
    https://github.com/lovell/sharp/issues/687
    https://github.com/lovell/sharp

    Or perhaps we keep the PNG but use a better compression algorithm?
    https://github.com/kornelski/pngquant
    https://github.com/topics/png-compression
    https://github.com/jay2503/PNG-Compression

    Thanks again for the help!
     

    Attached Files:

    Last edited: Apr 6, 2023
  50. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Thanks for the update.

    I have had some requests to save the images in different formats, although most people want to save the images in .TGA, (even bigger) not .JPG. The main problem with .JPG is that the image format is lossy. Most people want as close to the source image as possible.

    This is a feature I will try to add. I am using Unity's API to create the .PNG files EncodeToPNG. there is also an EncodeToJPG() function.

    It would be fairly easy for you to modify Mesh Baker to save to JPG instead. Edit the file: MB3_TextureCombinerEditorFunctions.cs:

    Add an entry for ".jpg" to the "saveTextureFormat" enum.

    Code (CSharp):
    1.         enum saveTextureFormat
    2.         {
    3.             png,
    4.             tga,
    5.             jpg,
    6.         }


    Code (CSharp):
    1.     public void SaveAtlasToAssetDatabase(Texture2D atlas, ShaderTextureProperty texPropertyName, int atlasNum, bool doAnySrcMatsHaveProperty, Material resMat)
    2.         {
    3.         ...
    4.             if (SAVE_FORMAT == saveTextureFormat.png)
    5.                     {
    6.                         pth += ".png";
    7.                         relativePath += ".png";
    8.                         byte[] bytes = newTex.EncodeToPNG();
    9.                         System.IO.File.WriteAllBytes(pth, bytes);
    10.                     }
    11.  
    12.                    //    ============= ADD THIS
    13.                    else if (SAVE_FORMAT == saveTextureFormat.jpg)
    14.                     {
    15.                         pth += ".jpg";
    16.                         relativePath += ".jpg";
    17.                         byte[] bytes = newTex.EncodeToJPG();
    18.                         System.IO.File.WriteAllBytes(pth, bytes);
    19.                     }
    20.                     //================
    21.  
    22.                     else
    23.                     {
    24.                         pth += ".tga";
    25.                         relativePath += ".tga";
    26.         ...
    And change the SAVE_FORMAT to saveTextureFormat.jpg