Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Mesh Baker by Digital Opus [RELEASED]

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

  1. EiknarF

    EiknarF

    Joined:
    Apr 28, 2011
    Posts:
    138
    Thank you for the quick reply. As I get more into the workflow of baking to use texture arrays I have another question;

    If I have a single mesh with with a lot of materials, say 20, but I only want to convert 10 if them to use texture arrays, is it possible to use your tool to bake the mesh but only change the elements with the materials I have baked into an array. My tests are giving me the error;
    "Source object "object name" that was not in the baked materials."

    I'm not sure if makes any difference but my use case is trying to optimise a town with lots of buildings, I was going to write a script to make a simple hierarchy based on shaders and inside that a mesh for each material so I can visualise what exactly is being used, I would bake the array from these objects. When I'm happy with the arrays I want to bake all the building meshes to convert the applicable materials to use these new arrays.

    Thanks again!
     
  2. Phong

    Phong

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

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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.
  4. EiknarF

    EiknarF

    Joined:
    Apr 28, 2011
    Posts:
    138
    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 ☺️
     
  5. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    107
    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?
     
  6. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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.
     
  7. EiknarF

    EiknarF

    Joined:
    Apr 28, 2011
    Posts:
    138
    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?
     
  8. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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.
  9. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    553
    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
     
  10. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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
     
  11. Allen0012

    Allen0012

    Joined:
    Sep 5, 2009
    Posts:
    92
    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.
     
  12. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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.
  13. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    553
    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
  14. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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.
     
  15. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    553
    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
  16. 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
     
  17. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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.
     
  18. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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.
     
  19. KarlKarl2000

    KarlKarl2000

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

    thanks for the help
     
  20. 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
     
  21. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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?
     
  22. Kozaki2

    Kozaki2

    Joined:
    Apr 8, 2019
    Posts:
    45
    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?
     
  23. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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.
     
  24. Dev_UHG

    Dev_UHG

    Joined:
    Jun 5, 2017
    Posts:
    20
    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!
     
  25. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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?
     
  26. Dev_UHG

    Dev_UHG

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

    Dev_UHG

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

    jnbbender

    Joined:
    May 25, 2017
    Posts:
    461
    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
     
  29. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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.
  30. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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?
     
  31. jnbbender

    jnbbender

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

    Attached Files:

  32. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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.
     
  33. jnbbender

    jnbbender

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

    Dev_UHG

    Joined:
    Jun 5, 2017
    Posts:
    20
    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.
  35. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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.
     
  36. 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
  37. Hobodi

    Hobodi

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

    Immersive-Matthew

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

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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.
     
  40. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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.
  41. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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.
     
  42. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    553
    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 at 2:15 AM
  43. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,013
    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.