Search Unity

Mesh Baker by Digital Opus [RELEASED]

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

  1. bac9-flcl

    bac9-flcl

    Joined:
    Dec 5, 2012
    Posts:
    829
    I'm wondering if it is still impossible to make MeshBaker skip generation of new bones when merging objects into a skinned mesh. I am getting objects with 250 bones when I attach modular armor to my characters, and not a single one of those new bones is useful in any way. I'd love to have an option where parenting determines which bone any MeshRenderer goes into and no new per-renderer bones are ever generated. :)
     
  2. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I have not implemented a feature like that. The main problem I am running into is interface. I don't want in interface that would look complex or intimidating to users. Many people complain that they already find Mesh Baker too complicated. I could see people wanting to be able to configure this bone-by-bone (some bones would use the parent, some bones would keep the extra transform). Not sure where that should be set up.
     
  3. bac9-flcl

    bac9-flcl

    Joined:
    Dec 5, 2012
    Posts:
    829
    I don't use the interface so I'd appreciate an option even if it's not exposed in the UI, especially because configuring excluded Transforms is impossible in advance in my case (they are attached by code and the character is merged through code immediately after, there is never a step where UI configuration is possible). :)

    Overall, I feel that the existing distinction between SkinnedMeshRenderer and MeshRenderer already serves to explain to users what to expect. If you would introduce a very simple boolean toggle that reads "only include bones from skinned meshes", that would cover most real-world cases requiring no new bones and would make what's included very easy to predict.
     
  4. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I will see what I can come up with.
     
  5. neksyt

    neksyt

    Joined:
    Feb 17, 2015
    Posts:
    3
    the function "Batch Bake Prefab (Replace Prefab), doesnt work right now in Unity 2017.4, cause it doesn't actually replace the prefab.
    gave a 1 star review until this is fixed, thanks :)
     
  6. neksyt

    neksyt

    Joined:
    Feb 17, 2015
    Posts:
    3
    also , do you have any plans to support texture-arrays, instead of texture-atlases?
    since this is a way better way to optimize on modern platforms as far as i know.
     
  7. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Are you using the latest version? The Batch Prefab Baker works for me in Unity 2017.3.1 Can you tell me what error you are getting?
     
  8. Cargomancer

    Cargomancer

    Joined:
    Dec 3, 2015
    Posts:
    9
    Hey Phong, Wicked tool you have here

    Having a little issue with vertex colors however. It seems after baking my meshes, I lose all my vertex colors.. That or they all become one color. I ticked "colors" in the options but that didnt seem to fix it sadly. What could I be doing wrong

    Image below

    Bottom building original
    Top building is the baked building with all vertex colors removed for some reason. It seems half the building is glass. My shader has 4 vert colors in it. Base, Base2, metal and glass. It apears my entire building has gone to the glass vert color
     

    Attached Files:

    • 57.jpg
      57.jpg
      File size:
      69.9 KB
      Views:
      486
  9. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Yes, working on support for this.
    I just tested and it worked for me. Some things to check.

    • If the combined mesh has been saved as an asset (bake into prefab) then you should be able to see the channels that exist in the inspector preview when the mesh asset is selected. This should include colors.
    • That the shader on the combined material is the same as the shader on the source material.
    • Include Colors is checked:
    upload_2019-6-25_12-16-12.png

    I can send you a working example scene if that would help.
     
  10. Cargomancer

    Cargomancer

    Joined:
    Dec 3, 2015
    Posts:
    9
    Hmmm I had that option ticked on my baker. Uploaded the work flow below. A working file for a demo could be really useful!

    I drag and dropped my prefab in the scene
    I created a mesh baker > I open the tool for adding objects. > I generated a baker for one of the materials in the scene
    Made sure to select said baker and enable Include colors ( I selected the wrong one in the image. By the way, which baker do I select include colors on. Do I do it on the first baker file or all of them? Iv been doing all of them just in case)

    I then bake everything in scene

    2 image is the result. Sadly all the vertex color info has been removed it seems.
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      356.6 KB
      Views:
      503
    • 2.jpg
      2.jpg
      File size:
      139.3 KB
      Views:
      480
  11. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I think I know what is going on. I added a feature to Mesh Baker for sharing settings. There are "Mesh Baker Settings" on the MB3_MeshBakerGrouper. And also "Mesh Baker Settings" on the child MB3_MeshBaker's. The child Mesh Baker is supposed to use the shared settings from the Mesh Baker Grouper (The use shared settings field on the child should be assigned the settings from the grouper).

    The original MB3_MeshBaker that is created when you click "Generate Baker" is not set up to share settings with the grouper. Will fix that on the next release. To fix this try one of the following:
    1) Assign the MB3_MeshBakerGrouper to the "use shared settings" field of its child Mesh Baker.
    2) Delete the child mesh baker and use the "Generate Mesh Bakers" button on the MB3_MeshBakerGrouper to re-create it.
    3) Check the "colors" on the child mesh baker.

    After doing this, re-bake the combined mesh. If this does not work let me know and I can send you the example scene.
     
    Last edited: Jun 27, 2019
  12. mangax

    mangax

    Joined:
    Jul 17, 2013
    Posts:
    336
    been a long time since i checked mesh baker, am wondering is there any plans to support texture arrays?
    there is a huge potential and benefit in using texture arrays compared to regular texture atlases.. especially for tiling textures..
    i would recommend storing the index on uv0.z or w values or inside uv3 would be very handy!
     
  13. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Yes, I do plan to support this in the near future. You can expect something over the next 1-2 months.
     
  14. swGeneral

    swGeneral

    Joined:
    Jun 5, 2015
    Posts:
    2
  15. swGeneral

    swGeneral

    Joined:
    Jun 5, 2015
    Posts:
    2
    Also, there are materials have only Albedo color, main texture is empty.

    QQ截图20190717115838.png
     
  16. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Try enabling:

    Blend Non-Texture Properties

    On the TextureBaker settings.
     
  17. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Was using your free version to test on before purchasing full version. It seems like it's not copying the bone information to the baked SMR, or at least that's my guess.

    So I baked a character with several skinned meshes and then manually adjusted the bounds, copied the parent bone transform to the baked version and set the reference in the SMR. Added an animator using the avatar from the original model. No animation.

    If I leave the original enabled in the scene, it animates the baked version. Disable everything on the original except the animator, it's still driving the baked.

    I know you can disable the original SMR's and copy the baked one into place, and that does work. I guess if that's easier for some reason it's ok, although I'd prefer to just build a whole new gameobject for the baked version.

    Also why is mesh baking not available at runtime? Or is it via the api? Are there editor only tools you are using there?
     
  18. Mandelboxed

    Mandelboxed

    Joined:
    Apr 17, 2015
    Posts:
    50
    Hi. I've just tried using Mesh Baker and have great success in reducing draw calls in my scene, however when I try to bake a lightmap I get huge black areas that were not there before.

    These images are baked lighting using Bakery (GPU Lightmapper) but I have similar results with Enlighten.

    upload_2019-7-19_9-36-16.png

    upload_2019-7-19_9-36-43.png

    upload_2019-7-19_9-37-8.png

    Additional screenshot showing the table in an empty scene baked with Enlighten:
    upload_2019-7-19_9-51-32.png

    How the table should look:
    upload_2019-7-19_9-38-36.png

    If I disable shadows the lighting looks mostly normal.

    Am I doing something wrong?

    Is this a bug?

    I have tried both preserve UV2 and generate a new UV2 set.

    Any help you can offer would be much appreciated. Thanks!
     
  19. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    You should be able to bake into a skinned mesh by setting the Mesh Baker output to "skinned mesh" on the Mesh Baker inspector. There is an example scene that does this. You can combine skinned and regular "mesh-renderer" meshes into a single combined mesh.

    The animation is probably due to the culling settings on the animator. If you set the culling settings to "always animate" then it should animate. You can use the culling settings if the skinned mesh renderer is a child of the animator.

    If you copied the bones (transform) from the original as you describe then it is these transforms that are driving the new mesh. You could create new transforms and use those instead (assign them as bones). However those would not animate unless you put them in a hierarchy and also added an Animator etc... I set it up this way because there could be a lot of things driving the source bones (animations, IK, scripts, physics). By re-using the original transforms, then whatever was driving the source bones still works.

    IMO it is easier to re-use the bones from the original skinned mesh. If you want the bones to be part of a new combined mesh prefab, then you can unpack the source prefab, move the bone hierarchies and animators to be children of the new combined mesh game game object. Delete the old source skinned mesh and make a prefab of that.

    Mesh Baking is absolutely available at runtime (a lot of the example scenes do this). However Unity has some limitations on creating/importing new assets while in play mode so not all editor functionality is available. Some of the TextureBaking functions are limited at runtime because it is not possible to convert textures to readable formats at runtime.
     
  20. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    The "generate new UV2" should work. I am surprised to hear that it does not. Did you re-bake the mesh after swtiching the setting and then re-bake the lighting?

    The other setting that should work is "Copy UV2 To Separate Rects". I would recommend this if your models had UV2 channels generated in the Modeling tool.
     
  21. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    So this is potentially confusing. If you just copy the bone hierarchy from the original to the baked. Let's call the copied bone hierarchy B. Then you link the root of B to the baked SMR. That doesn't do what some people think. That is not telling the SMR use B. It's telling it which transform is the root.. Because the top of B is not necessarily the root bone. But it's not actually using the transform you assign, it's just using the name of it to index into the internal bones array.

    If you want to use a different set of bones, you have to actually assign that via code.
     
  22. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Yes, you need to assign the different set of bones (an array of transforms) using code to the SkinnedMeshRenderer. BTW the SkinnedMeshRenderer component does not have any concept of "root bone". Nor does it care where the bones are in the hierarchy. The Animator component does care about where the bones are and has a root bone. It likes the bones to be children of it. It also likes the SkinnedMeshRenderer to be a child of the animator (for culling to work correctly).

    Unfortunately if you are combining several skinned mesh characters that have different Animator components, it is not possible to put the combined skinned mesh renderer under multiple Animator's in the hierarchy. It can have only one parent.
     
  23. TonismoGames

    TonismoGames

    Joined:
    Jun 12, 2018
    Posts:
    111
    upload_2019-7-20_19-22-0.png Hi I am getting this error at runtime when trying to bake in game.
    upload_2019-7-20_19-23-8.png how do I bypass this error?
     
  24. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Trying to figure out the flow for baking multiple skinned meshes into a single skinned mesh via code. Just the simplest setup mesh combine only no materials that creates the target and sets up the target SMR. Like what the Bake button does in the editor.

    I created and set the resultSceneObject and targetRenderer but calling apply just creates a mesh with 0 verts/tries and does nothing with the target SMR, so obviously I'm missing something.
     
  25. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    To get the benefits of using an atlas you would normally use the TextureBaker to create an atlas and the "TextureBakeResult". This asset needs to be assigned to the texture baker.

    If you want to combine meshes without texture baking you can generate a TextureBakeResult for this purpose. It won't be an atlas, all the original textures will be on their own submesh. To generate one of these use:

    MB2_TextureBakeResult.CreateForMaterialsOnRenderer(GameObject[] gos, List<Material> matsOnTargetRenderer)

    Assigne the generated TextureBakeResult to the Mesh Baker.
     
  26. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    The example Scene "SkinnedMeshRenderer" does this. Did you call AddDelete before calling apply? Also did you ensure that the "output" field on the baker is set to "SkinnedMeshRenderer"?
     
  27. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    AddDeleteGameObjects is what I was missing.

    Hey do you have plans to convert some of the expensive parts of texture atlasing to be job friendly? Texture2D now has support for reading/writing using NativeArray, and Burst would have a huge impact on performance here.
     
  28. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    So the packing time is kind of a problem. The asset will still be useful but in a multiplayer game where every character can have custom clothing it's just untenable for that specific use case.

    I was looking through how UMA handles this as they can atlas a hundred or so in just a few seconds and it's an interesting approach. The basic flow is they use an algorithm to determine the best fit of an original material into the atlas, find the best rectangle basically. Draw all of those using Graphics.DrawMesh into a rendertexture and that's the atlas.
     
  29. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Thanks for the notes. I do plan to make the mesh baking and atlas baking Job friendly. ATM I am working on Texture Array support.

    Re: UMA approach. That is similar to how MeshBakerFast works, although it uses Graphics.DrawTexture instead of DrawMesh. Now that I think about it may be faster to do this with DrawMesh. I will take a look to see if the speed is similar.
     
  30. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    My bad there I mistyped they use DrawTexture.
     
  31. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Try the MeshBakerFast approach.

    Thinking about it though, DrawMesh may be faster. The reason is that I am calling DrawTexture 9 times in order to bleed the texture into the padding (sort of like a nine slices sprite). However this could be done with a special mesh with custom UVs which may be faster. When I get time, I will give it a try.
     
  32. TonismoGames

    TonismoGames

    Joined:
    Jun 12, 2018
    Posts:
    111
    Thank You !I ran into another issue upon modifying the new line of codes.
    upload_2019-7-23_14-51-38.png
    upload_2019-7-23_14-52-47.png
     
  33. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Hmmm, that code works for me and looks good. Can I ask which version of Mesh Baker you are using?

    Also can you confirm that the array of GameObjects and "sameMaterials" are not zero length?
     
  34. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Quick question. When combining are the meshes to combine processed and written out to the new mesh in the the original order?

    Our specific use case allows for a faster atlasing approach, but my uv mapping is off and being that it relies on order here thought I'd check.
     
  35. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    You know actually probably easier to just create a texture bake result and pass that to the mesh baker, that way I don't have to deal with the uv mapping.
     
  36. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Yes, the order is the original order if the combined mesh is "empty". However if the mesh is not empty, ( there is data in the combined mesh and you are updating using AddDelete and deleting some meshes). Then I am not certain that the order will be preserved.
     
  37. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Ok so looking at the MB2_TextureBakeResults that MB creates the data looks off. I'm just assigning it to a public field after atlassing so I can see how to set it up. Maybe it's fine at that stage and at some later point it gets messed up?

    Below isn't the entire thing but it's enough to see the issue. There are multiple entries where it say's the source object is the same when it's actually not, and all of those have the same rect coords.

    upload_2019-7-25_15-15-53.png
     
  38. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Nevermind, those are all cases of the same texture, so the output seems odd at first but actually makes sense.
     
    Phong likes this.
  39. TonismoGames

    TonismoGames

    Joined:
    Jun 12, 2018
    Posts:
    111
    upload_2019-7-25_19-36-35.png
    Thank you for the tip!It is a bug withing my codes I didnt add the EnginePartsObject Component in race mode. Everythinf works good now!
     
    Last edited: Jul 26, 2019
    Phong likes this.
  40. shi946

    shi946

    Joined:
    Jun 3, 2019
    Posts:
    16
    Is there a faster way to add source materials other than manually? I have a building where the wall panels each have 2-4 materials, in total ~50 materials (graffiti, lights, smooth metal/rough metal/glass/etc.). When I generate a baker, it only includes one material.
     
  41. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Mesh Baker works with source game objects and will take all the materials on each of those game objects. There are two tools for assisting with adding objects.

    1) Below the "Objects To Be Combined" field in the inspector there is a "drag and drop" box. You can drag a parent/root scene object to this field and all the child object descendant that have renderers will be added.

    2) There is a window that can be opened with "Open Tools For Adding Objects". The second tab of this window has filters for adding objects. You can select one or more root objects in the scene, then these filters will be applied to those objects and all the children.
     
  42. thehen2

    thehen2

    Joined:
    Apr 1, 2014
    Posts:
    54
    What is the recommended workflow with tight memory limitations? The docs say this:

    There are effectively three copies of each mesh in memory when a mesh is baked (the source mesh, the combined mesh and internal buffers of the MeshBaker object) If you don’t intend to modify a combined mesh then consider deleting the source meshes and the MeshBaker object from the scene.

    I quickly reach my memory limit with all three, and my scenes crash on my target device. I can't delete the source meshes though, as scenes are being actively iterated on.
     
  43. shi946

    shi946

    Joined:
    Jun 3, 2019
    Posts:
    16
    Thank you for the reply. I followed this video exactly (youtube.com/watch?v=2oIJrAJM0io) but am using a multi-mesh and material baker since my building has >> 65K verts. The 5 bakers generated have plenty of objects under their "objects to be combined" fields. But there's only one source material generated for each baker even though each object has more than one material (image attached).

    For example, "Wall_Set03_A13_LOD2" object has a "Concrete_Panels4_a" material and a "Windows1_emissive" material but only the first material is included in the "Source Materials" list.

    Edit: Nevermind. Just followed this video exactly (youtube.com/watch?v=fvNY9fjhsR8&t=2s) using multi-mesh baker. Works now!
     

    Attached Files:

    Last edited: Jul 29, 2019
  44. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Hit a really strange issue where destroying the meshbaker instance, the one with the texture baker as the parent, is destroying the resulting combined mesh somehow. It all works in a test scene I have, both in and out of playmode. But in my game scene the skinned mesh renderer mesh was ending up null. Couldn't figure it out until I tried as a last resort not destroying it in my cleanup process. And that fixed it. This might not be you but just in case it sounds familiar.
     
  45. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Also it seems to be a delayed thing, like immediately after I destroy the baker it's not null. If I clone (instantiate) the combined mesh before destroying the bake that fixes it also, which further implies something in the baker.

    Are there any Unity api's here that use statics that we might both be using that could have some reference to the mesh? Very strange the trigger is a different scene.
     
  46. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    If at all possible I would recommend baking the atlases in the editor and baking the meshes at runtime. If you do need to bake atlases and runtime then use the Mesh Baker Fast packer.
    If you are not re-baking meshes, then turn on "clear buffers after bake" for the Mesh Baker.
    Are a lot of your meshes static (eligible for static batching)? Could you use the batch prefab baker to prepare prefabs that use a combined material then Unity's static batching will do the combining for you. This is a good practice in general because these meshes are much easier and quicker to combine. The can be combined using Unity's StaticBatchingUtility or Unity's Mesh.CombineMeshes.

    If you describe what you are doing I might be able to provide better advice. If you have an open world then it can be easy to run into memory limitations. For this situation you need a system with good LODs and that can load and unload chunks of the world as the user moves around.
     
  47. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Hmmm, this is possible. Mesh Baker tires to clean up after itself by disposing of meshes that it created at runtime meshes in OnDestroy. You can see this in the OnDestroy method of the MB2_MeshBaker.cs file.

    You could modify the OnDestroy behaviour. Or you could modify MeshBaker so that the MB3_MeshCombiner._mesh field is public. Then you could assign your own Mesh before baking. MeshBaker shouldn't destroy this mesh because it didn't create it. Although you should destroy the mesh yourself before closing the scene or you will leak Meshes. I will try to add an "AssignMesh" method to the next version so you don't need to modify MeshBaker in the future.

    Regaring the mesh not being null right away after destroying the MeshBaker component. This is a Unity thing. "Destroy" doesn't clean up the memorey for destroyed objects right away. The object being destroyed will test as null, when doing a comparison, but you can still access the methods and properties of the object for an undefined amount of time before Unity gets around to cleaning it up.
     
  48. Sound_Priest

    Sound_Priest

    Joined:
    Nov 7, 2013
    Posts:
    8
    Hi! "Analyse Scene & Generate Bakers" made list shaders for whole scene. Can it analyse only selected root object in scene (only rocks or buildings in my scene for example) ??
     
  49. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,374
    Baking meshes at runtime is broken with the latest version. Reproducible with the example scene "SceneRuntimeExample". It will throw an error "Object Cube2PF has not been added".
     
  50. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    The "Analyse Scene" tools only work on the entire scene.

    However the other tab of the Mesh Baker window is for adding objects to bakers (Texture Bakers and Mesh Bakers). It works on selected objects and their children. It has filters for filtering by shader and material. You may be able to use this to collect only rocks and buildings.