Search Unity

Mesh Baker by Digital Opus [RELEASED]

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

  1. Zenchuck

    Zenchuck

    Joined:
    Jun 2, 2010
    Posts:
    297
    Hey there,

    Just another suggestion for the grid cluster type. Make an option to rotate the grid around the y axis.
     
  2. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    O.K. adding to todo.
     
  3. angelsm85

    angelsm85

    Joined:
    Oct 27, 2015
    Posts:
    63
    I have multiple meshes with multiple materials. The materials are Legacy/Diffuse and Legacy/Transparent/Diffuse. How can I do to combine all in one mesh with one atlas keeping the transparents areas? Thanks for your work.
     
  4. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I would recommend using the multiple materials feature with two result materials, one would be Legacy/Diffuse, one would be Legacy/Transparent/Diffuse. When you bake you will have one mesh with two submeshes.

    You could try to bake everything into the Legacy/Transparent/Diffuse (just set the result material to that), but I am not certain what transparency value will get chosen for the non-transparent source materials.
     
  5. jhughes2112

    jhughes2112

    Joined:
    Nov 20, 2014
    Posts:
    178
    I have been having trouble figuring out why sometimes the mesh would never update bounds correctly, even if I forced it to do so repeatedly. I tracked it down finally. Here's the bug:

    Repro steps: Create an empty transform, make a cube and sphere as children and an empty one with a MB3_MeshBaker component, and a child mesh underneath it as the Combined Mesh Object target. Drag the cube as the first element in the list of objects to bake. Move the root to (100, 0, 0). Hit Bake. Notice the generated mesh has a transform (-100, 0, 0) and all the verts are offset by (100, 0, 0) to compensate. This is indicative of the bug, but wait for the next step to really drive it home. Move the root to (200, 0, 0) and add the sphere as element 2 of the array and click Bake again. If you disable the original object renders, Notice the sphere and cube are basically at the origin. Totally wrong space. They should be where the object is. That's the whole point, right?

    To be fair, I'm doing stuff with skinned meshes not simple ones, but the effect is the same. Things have to be baked at the origin to work, and I just can't do that. How *should* I make this work? Or, rather, am I misusing this somehow?

    MB3_MeshCombinerSimple.cs line 1643 or thereabouts:

    meshGo.transform.parent = root.transform;

    should be

    meshGo.transform.SetParent(root.transform, false);

    This is part of the issue. It appears that the verts are not respecting the target node's space that they will be living under. If I find a fix for it, I'll post it.
     
    hopeful likes this.
  6. Phong

    Phong

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

    Thanks for bringing this to my attention. I will take a look at it.
     
  7. jhughes2112

    jhughes2112

    Joined:
    Nov 20, 2014
    Posts:
    178
    Basically, by forcing my character back to the origin with no rotation, rebaking, then moving back to wherever he was before made the problem go away. If you were to fix the inside the library, it would be nice, but there is a workaround for now. Just not incredibly convenient.
     
  8. strich

    strich

    Joined:
    Aug 14, 2012
    Posts:
    374
    @Phong this looks great. We have two problems that stand out in your limitations that I'm hoping there is a work-around for:
    1. Apparently one cannot optimize bones prior to baking the skinned meshes - In our tests one of the biggest performance killers in scenes with many NPCs is large amounts of useless bone transforms. How can we optimize out those bone transforms at run-time if we're not allowed to optimize them out in the editor?
    2. We need to be able to use Blend Shapes. In your limitations it is stated that Blend Shape baking is not currently supported - Does this mean all Blend Shape data is lost from all meshes being baked? Or does is simply mean it won't try to merge Blend Shape data? IE can we ensure that Blend Shapes do not overlap?
     
  9. Phong

    Phong

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

    (Nice to have the old forums back). The bone optimization is something I have struggled with. This optimization happens in the model importer and is not available in the regular mesh API. Also bones are not available in the mesh API for baking for models that have been optimized. The only way this will work is to write a full FBX exporter to export the combined mesh to FBX so it can be reimported (and optimized). This is on the TODO list but I am anticipating it will be a lot of work to get it working properly with animations will be tricky and it will only work for meshes baked in the editor since it will require the model importer. I don't know of any way to do this currently.

    Blend shapes are another deficient area of the Mesh API. It has not been possible to write a blend shape to a Mesh which means that all blend shapes are lost when a mesh is combined. Although I just checked and it appears that Unity has added the necessary methods (Not available in 5.1 but are available in 5.3). Hopefully I can remove this limitation over the coming weeks.
     
  10. strich

    strich

    Joined:
    Aug 14, 2012
    Posts:
    374
    Re the bones - I'm surprised you need to rely on these transforms at all - They're just transforms with the names of the bones and nothing more. Surely the bone information you need to perform your work is available elsewhere?

    What happens if you try to perform a Optimize Transform Hierarchy on the post-baked animator component?
     
  11. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Thanks for pointing that out. I was not aware that was there. Now that I look in the Animator class I see that the necessary functionality is available in the AnimatorUtility class. This is another limitation I should be able to remove over the next few weeks.
     
    strich likes this.
  12. strich

    strich

    Joined:
    Aug 14, 2012
    Posts:
    374
    Awesome. Well we'll hang tight until you push out said updates. Otherwise it all looks pretty good! :)
     
  13. stlouis4d

    stlouis4d

    Joined:
    Jul 19, 2015
    Posts:
    34
    Is it possible to use Mesh Baker to create a texture atlas or combine meshes of prefabs used in an object pool or instantiated into a scene at runtime (that aren't previously part of the scene) or do all prefabs have to already be game objects in a scene to create the texture atlas or combine their meshes?

    If this is possible, is it a simple process that works as easily and as well as a standard scene with all prefabs already in the scene as demonstrated in the promo videos?

    For reference, I'm interested in this for a mobile android build that has very little in the base scene itself, but instantiates many instances of several game objects that are used as enemies, obstacles, and pickups for each game level.
     
  14. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Yes, it is possible (and easy)

    The TextureBaker can use a completely different set of game objects than the MeshBaker. The important thing is the materials. The assets created by the TextureBaker are essentially just a map that maps materials to rectangles in the set of atlases. Any mesh can be combined that has one of the materials that was used in the Texture Baking process.

    The texture baking and mesh baking are loosely coupled. You can bake the textures in a different scene or even a different project using placeholder meshes (cubes) and re-use those atlases in different scenes.
     
  15. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    A new version of Mesh Baker is available in the Asset Store!

    This version offers improved support for non-texture material properties such as _Color and _Glossiness through the addition of TextureBlenders. TextureBlenders are custom scripts that blend non-texture properties with textures as they are being added to an atlas. It is easy to extend Mesh Baker by writing TextureBlenders for custom shaders.

    This version also offers support for multi-object editing for the MB3_TextureBaker and MB3_MeshBaker components.
     
    docsavage likes this.
  16. docsavage

    docsavage

    Joined:
    Jun 20, 2014
    Posts:
    1,021

    Hi @Phong,

    Just seen the latest update. Does this cover the issue re the multiple materials we discussed in our private conversation?
    That conversation has disappeared off the conversation list since the change over. Sorry if you have contacted me since there and I have not replied.

    Thanks
     
  17. stlouis4d

    stlouis4d

    Joined:
    Jul 19, 2015
    Posts:
    34
    That sounds very encouraging and just what I was hoping to hear. Is the process the same then (as shown in the promo video)?

    Would I (1) add these prefabs to my scene, (2) create the texture atlas the same way as shown in the promo video, (3) apply these texture atlases to my prefabs, (4) remove the prefabs from my scene, and then (5) instantiate the new prefabs with the atlas textures in my game as usual?

    And as an example, would Mesh Baker work to lower the draw calls for the Unity Learn tutorial "Space Shooter" so that each asteroid (or in my case, each enemy, obstacle, and pickup) would share a texture atlas?

    Does (or can) Mesh Baker lower poly count of game objects or just combine objects into a single prefab?
     
  18. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    For your situation, you would probably want to use the prefab baker. This helps prepare models for static and dynamic batching by copies of your prefabs that all share a material. See this video for the workflow.



    Mesh baker does not lower the poly count. It only combines meshes to reduce drawcalls. Combining meshes works best with static geometry. If your objects move independently from each other then you can see some benefit from dynamic batching or you can bake your meshes into skinned meshes which helps if the meshes are very low poly. If you have a lot of high poly meshes moving independently then you will have a hard time combining meshes.
     
  19. Phong

    Phong

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

    It should work. If I remember your model uses custom shaders that have no texture (only uses tint color). Is this correct? You should be able to write a texture blender that will handle this. Let me know if you have any questions as this is a fairly tricky setup.
     
  20. docsavage

    docsavage

    Joined:
    Jun 20, 2014
    Posts:
    1,021
    Hi @Phong,

    Thanks for the reply. You are right that pack is difficult to work with. Looks great but is making me work.I will try to sort it. Thanks for the offer of help.

    doc
     
  21. hopeful

    hopeful

    Joined:
    Nov 20, 2013
    Posts:
    5,686
    @Phong - The Texture Blender feature ... I'm trying to understand what it does and what the advantage is over how MB worked before. I haven't used MB yet, and my experience with mesh combining and atlasing is rudimentary.

    This is to handle those situations where a color is set in the shader? Like, if you have a gray albedo or emission texture, and you set the color in the standard shader?

    Does it mean that if you, say, have several characters using the same eye mesh, that you can use a gray albedo texture for the eye and it will be atlased just once and then each character displays using the same texture but with different colors as set in the materials?

    And how this would have worked previously is that for each different material I would have needed to atlas the same gray eye texture over and over for each different color?

    I'm not exactly sure how this feature works or when to use it, I guess. Examples? :)
     
    Last edited: Jul 20, 2016
  22. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    It is easiest to understand with tint color. The old way Mesh Baker handled tint colors was a bit of a hack. If there was no texture but there was a tint color then a small color texture would be created with the tint color that would be baked into the atlas. This only worked correctly if the source material used a tint color with no color texture. It also relied on the tint color having the property name _Color in the shader. If there was a texture and a tint color no attempt would be made to combine the tint and the shader. One reason I did not try to combine was that different shaders combine the tint differently (multiply, additive etc...). Also, this approach only worked for a small group of common texture properties hard coded into the Mesh Baker code.

    TextureBlenders make the process of blending textures and non-texture properties more flexible. A custom texture blender can be written for each different shader which blends the tint color and the texture color using the correct method (multiply, additive etc..) for that shader. The texture blender will correctly combine the texture and tint color as the colors are being added to the atlas. Texture Blenders can also handle other -nontexture properties such as emission, speculary metalness etc...

    A Texture Blender is a short script that defines how the color gets blended with the texture. At the heart of the texture blender is the OnBlendTexturePixel method which is called once for every pixel in the texture. Before OnBlendTexturePixel is called there is a call to
    OnBeforeTintTexture which allows the TextureBlender to prepare for the OnBlendTexturePixel calls.


    Code (CSharp):
    1.  
    2.  
    3. Color m_tintColor;
    4.  
    5.     public void OnBeforeTintTexture(Material sourceMat, string shaderTexturePropertyName)
    6.     {
    7.       if (shaderTexturePropertyName.EndsWith("_MainTex"))
    8.       {
    9.         m_tintColor = sourceMat.GetColor("_Color");
    10.       }
    11.     }
    12.  
    13.  
    14.         public Color OnBlendTexturePixel(string propertyToDoshaderPropertyName, Color pixelColor)
    15.         {
    16.             if (propertyToDoshaderPropertyName.Equals("_Color")
    17.             {
    18.                 return new Color(pixelColor.r * m_tintColor.r, pixelColor.g * m_tintColor.g, pixelColor.b * m_tintColor.b, pixelColor.a * m_tintColor.a);
    19.             }
    20.             return pixelColor;
    21.         }
    This blends the m_tintColor using multiply with the texture's pixel color.
     
    Last edited: Jul 21, 2016
    hopeful likes this.
  23. docsavage

    docsavage

    Joined:
    Jun 20, 2014
    Posts:
    1,021
    @Phong,

    Just saying thnks for the help re the Textureblender and help you gave. I have not had chance to sort it yet but you have been great and this new update is going to help I am sure.

    Thanks

    doc:)
     
  24. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Keep me informed how this goes. I keep trying to improve Mesh Baker and if there are cases that don't work I like to know about it.
     
  25. docsavage

    docsavage

    Joined:
    Jun 20, 2014
    Posts:
    1,021
    Thanks Phong will do. Although you may regret that offer. Actually have a plan B just in case it gets too messy. But realyl want to avoid it.:)
     
  26. MoribitoMT

    MoribitoMT

    Joined:
    Jun 1, 2013
    Posts:
    301
    I want to buy this asset but I have one question ?

    Lets say I have 3 objects, and I just want to make them use single material ( with textured atlas ) without combining meshes, still separated as object ? Is this possible ?
     
  27. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    I came here to ask exact same question, my buildings are destructable and its a city scene and only whant to combine textures into a couple of materials. Difues/ transparent.

    edit:
    I think it will be possible because the options are

    mesh & material

    multimesh & material baker.

    The issue I've is that my objects have unaplied materials in a multimaterial, this is because of the source data. This gives error if not all the buildings are selected, wish there was an option to cleanup objects.

    edit2:
    I was unable to aply the baked material to the existing mesehs. It bakes in an atlas in the existing shaders but the material is not aplied back nowhere. So you've to bake all into a single meshes and then yes it aplies the atlas material. :(

    I also disliked that it does not generate multiple atlases. It should allow to output an user defined amount of atlases instead of packing everything into a single atlas. In my case the output texture resolution si too low.
     
    Last edited: Jul 25, 2016
  28. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    It would be nice to allow multiple atlas output.
     

    Attached Files:

  29. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    You may be interested in the Prefab Baker:



    It allows you to create modified versions of your prefabs that share materials. You can also do this using the MeshBaker component using the "Bake Mesh Assets In Place" option but there are more steps.

    If you want multiple atlases. You should be able to use the "Multiple Materials feature to map the source materials to different combined materaials (atlases).
     
  30. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Nice, checkin your vid! omg if this is possibel I'll getting this right now! I'm in real need!
     
  31. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864

    ohh that was close!

    But why is it nos possible with non prefab meshes!

    I've 100 buildings and prefabing them and doing all the process described for the prefabs is going to kill me. The regular baker almost does the job, it simply should assing the newly atlased material to the source mesehs (or their duplicated ones) and there we go.

    fff. not sure what to do, may be I aquire a texture backing soft for my 3d package.

    edit: I'm going to rechedk your plugin anyway as there are a ton of options and whant to make sure it does not fit my needs befor dumping it.

    edit: ok I'm stupid, as my buildings hang from a parent I can process all the buildings in a batch run :)
     
    Last edited: Jul 25, 2016
  32. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Ok this asset has nice docuentation, I wen´t trought it.

    I now see the workflow. Definetly the prefab baker is the tool I need. Its super late here and need a rest, tomorrow I'll get the asset.

    I see how the multi atlas works. Its a bit too manual.
    upload_2016-7-26_1-7-50.png

    I've 106mats so if I want to split in 7 atlass I tried to duplicate the automatic generated list but if there are null entries it does not ignore them so I can only automatically put in the list the first 16 and then manually feed the rest. Definetly the option to output a number of atlases per shader with a max size would rock.

    I get an error:

    There are materials on the objects to combine that are not in the mapping: 005135_16321_1_1_7 (UnityEngine.Material),005135_16321_1_1_25 (UnityEngine.Material),005135_16321_1_1_26 (UnityEngine.Material),005135_16321_1_1_30 (UnityEngine.Material),005135_16321_1_1_34 (UnityEngine.Material),005135_16321_1_1_32 (UnityEngine.Material),005135_16321_1_1_6 (UnityEngine.Material),005135_16321_1_1_4 (UnityEngine.Material),005135_16321_1_1_17 (UnityEngine.Material),005135_16321_1_1_61 (UnityEngine.Material),005135_16321_1_1_60 (UnityEngine.Material),005135_16321_1_1_63 (UnityEngine.Material),005135_16321_1_1_22 (UnityEngine.Material),005135_16321_1_1_24 (UnityEngine.Material),005135_16321_L_-90231_0_01_01_210 (UnityEngine.Material),005135_16321_1_1_20 (UnityEngine.Material),005135_16321_1_1_3 (UnityEngine.Material),005135_16321_1_1_18 (UnityEngine.Material),005135_16321_1_1_23 (UnityEngine.Material),005135_16321_1_1_0 (UnityEngine.Material),005135_16321_1_1_11 (UnityEngine.Material),005135_16321_1_1_TransP_0 (UnityEngine.Material),005135_16321_1_1_1 (UnityEngine.Material),005135_16321_1_1_42 (UnityEngine.Material),005135_16321_L_-90255_0_01_01_71 (UnityEngine.Material),005135_16321_1_1_31 (UnityEngine.Material),005135_16321_1_1_19 (UnityEngine.Material),005135_16321_1_1_67 (UnityEngine.Material),005135_16321_1_1_56 (UnityEngine.Material),005135_16321_1_1_64 (UnityEngine.Material),005135_16321_1_1_43 (UnityEngine.Material),005135_16321_1_1_77 (UnityEngine.Material),005135_16321_1_1_80 (UnityEngine.Material),005135_16321_1_1_58 (UnityEngine.Material),005135_16321_1_1_74 (UnityEngine.Material),005135_16321_1_1_84 (UnityEngine.Material),005135_16321_1_1_53 (UnityEngine.Material),005135_16321_1_1_39 (UnityEngine.Material),005135_16321_1_1_46 (UnityEngine.Material),005135_16321_1_1_21 (UnityEngine.Material),005135_16321_1_1_75 (UnityEngine.Material),005135_16321_1_1_50 (UnityEngine.Material),005135_16321_1_1_68 (UnityEngine.Material),005135_16321_1_1_66 (UnityEngine.Material),005135_16321_1_1_44 (UnityEngine.Material),005135_16321_1_1_81 (UnityEngine.Material),005135_16321_1_1_47 (UnityEngine.Material),005135_16321_1_1_72 (UnityEngine.Material),005135_16321_1_1_73 (UnityEngine.Material),005135_16321_1_1_82 (UnityEngine.Material),005135_16321_1_1_83 (UnityEngine.Material),005135_16321_1_1_36 (UnityEngine.Material),005135_16321_1_1_57 (UnityEngine.Material),005135_16321_1_1_65 (UnityEngine.Material),005135_16321_1_1_52 (UnityEngine.Material),005135_16321_1_1_51 (UnityEngine.Material),005135_16321_1_1_48 (UnityEngine.Material),005135_16321_1_1_76 (UnityEngine.Material),005135_16321_1_1_62 (UnityEngine.Material),005135_16321_1_1_71 (UnityEngine.Material),005135_16321_L_-90224_0_01_01_45 (UnityEngine.Material),005135_16321_1_1_78 (UnityEngine.Material),005135_16321_1_1_59 (UnityEngine.Material),005135_16321_1_1_79 (UnityEngine.Material),005135_16321_1_1_70 (UnityEngine.Material),005135_16321_L_-90247_0_01_01_52 (UnityEngine.Material),005135_16321_1_1_33 (UnityEngine.Material),005135_16321_1_1_TransP_1 (UnityEngine.Material),005135_16321_1_1_69 (UnityEngine.Material),005135_16321_1_1_9 (UnityEngine.Material),005135_16321_L_-90249_0_01_01_39 (UnityEngine.Material),005135_16321_L_-90249_0_01_01_42 (UnityEngine.Material),005135_16321_1_1_37 (UnityEngine.Material),tex_panel_logo (UnityEngine.Material),005135_16321_1_1_85 (UnityEngine.Material),005135_16321_L_-90223_0_01_01_14 (UnityEngine.Material),005135_16321_L_-90223_0_01_01_16 (UnityEngine.Material),005135_16321_1_1_28 (UnityEngine.Material),005135_16321_1_1_41 (UnityEngine.Material),005135_16321_L_-90180_0_01_01_116 (UnityEngine.Material),tex_PQ_Build_D_01 (UnityEngine.Material),Default_Material (UnityEngine.Material),005135_16321_L_-90197_0_01_01_5 (UnityEngine.Material),005135_16321_L_-90248_0_01_01_7 (UnityEngine.Material),005135_16321_L_-90198_0_01_01_10 (UnityEngine.Material),005135_16321_L_-90189_0_01_01_16 (UnityEngine.Material),005135_16321_1_1_49 (UnityEngine.Material),namba (UnityEngine.Material),005135_16321_L_-90153_0_01_01_10 (UnityEngine.Material),
    UnityEngine.Debug:LogError(Object)
    MB3_TextureBaker:_ValidateResultMaterials() (at Assets/MeshBaker/scripts/MB3_TextureBaker.cs:355)
    MB3_TextureBaker:_CreateAtlases(ProgressUpdateDelegate, Boolean, MB2_EditorMethodsInterface) (at Assets/MeshBaker/scripts/MB3_TextureBaker.cs:172)
    MB3_TextureBaker:CreateAtlases(ProgressUpdateDelegate, Boolean, MB2_EditorMethodsInterface) (at Assets/MeshBaker/scripts/MB3_TextureBaker.cs:135)
    DigitalOpus.MB.Core.MB3_TextureBakerEditorInternal:DrawGUI(MB3_TextureBaker, Type) (at Assets/MeshBaker/scripts/Editor/MB3_TextureBakerEditorInternal.cs:245)
    MB3_TextureBakerEditor:OnInspectorGUI() (at Assets/MeshBaker/scripts/Editor/MB3_TextureBakerEditor.cs:32)
    UnityEditor.DockArea:OnGUI()



    THis is probably because the objects come with a lot of materials in the renderer but only one a couple ar assigned. This is because of the source data. I tried to cleanup in 3d editor but it was not possible for me.

    This asset has these multimaterials:
    https://www.assetstore.unity3d.com/en/#!/content/20359

    I found no script that will allow me to cleanup. in 3dmax there is clean multimaterial but works per scene and not per object.

    If you could provide me with a material cleanup script I would instabuy this asset.
     
  33. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Edit: If you tell me you would do he cleanup material script I could buy first and then you send me the script. Sure its a silly thing for someone with your skillset. I've never coded for editor and I'm in a total rush. We can discuss via private if you whant. This is important because it would help me to select a bunch of buildings and generate an atlas for the textures associated only to these, instead of having to select all the buildings as its needed now due to the error that having all the materials assigned to all the buildings. It would be a much faster process than manually assigning the source materials also.
     
  34. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Hi TooMany Sugar,

    What is that you need a script to do? It sounds like your source meshes have more materials in the list of materials than submeshes. You need these extra materials removed. Is that it?
     
  35. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    YEs! correct! This will allow me to select the buildings of witch I what to bake texture together istead of picking materials manually.
    I'll put buildings packed with a parent obj and then using the prefab baker bake materials for these buildings broups, so eah group will be a single atlas. If you tell me you prepare that script I'll buy. Pretty much right now :) . May be you read this tomorrow as I believe we're in very different timezones.
     
  36. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    This should remove the extra materials. Put it in a file callded RemoveUnusedMats.cs. Add it to a game object. Add a bunch of renderers to its list of renderers. Use the "Remove Unused Materials" command from the context menu in the component inspector (the little geer top right of the component inspector):

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RemoveUnusedMats : MonoBehaviour {
    5.  
    6.     public MeshRenderer[] renderers;
    7.  
    8.     // Use this for initialization
    9.     [ContextMenu("Remove Unused Materials")]
    10.     void RemoveUnusedMaterials () {
    11.         for (int i = 0; i < renderers.Length; i++)
    12.         {
    13.             MeshFilter mf = renderers[i].GetComponent<MeshFilter>();
    14.             if (mf != null && mf.sharedMesh != null)
    15.             {
    16.                 int numSubmeshes = mf.sharedMesh.subMeshCount;
    17.                 if(renderers[i].sharedMaterials.Length < numSubmeshes)
    18.                 {
    19.                     numSubmeshes = renderers[i].sharedMaterials.Length;
    20.                 }
    21.                 Material[] newMats = new Material[numSubmeshes];
    22.                 int numRemoved = renderers[i].sharedMaterials.Length - numSubmeshes;
    23.                 for (int j = 0; j < numSubmeshes; j++)
    24.                 {
    25.                     newMats[j] = renderers[i].sharedMaterials[j];
    26.                 }
    27.                 Debug.Log("Removed " + numRemoved);
    28.                 renderers[i].sharedMaterials = newMats;
    29.             }
    30.         }  
    31.     }
    32. }
     
  37. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    I tell you in a minute how it goes!
     
  38. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    mmm while I follow the steps I do not manage to clean, it sayw removed 0. O'll keep testing, I'll test with a dummy object
     
  39. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    I create this empy obj and trow a number of mats:
    upload_2016-7-27_1-12-29.png

    I create an object with the script and trow previous obj.
    upload_2016-7-27_1-13-21.png
    click remove mats:
    upload_2016-7-27_1-13-51.png

    but no mats removed
     
  40. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Ok, I put some debug.log

    Debug.Log ("sharedmat lenght" + renderers.sharedMaterials.Length);
    Debug.Log ("numSubmeshes" + numSubmeshes);

    And they're trowing equal values such as 43 and so on. Would this mean that the mesh has same material id number as materials? that would be pretty crazy....

    I keep testing.
     
  41. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Tested it on my end and it worked. Add a Debug.Log to the script to see how many submeshes the source mesh has. If there are enough submeshes for the extra materials then it won't work.
     
  42. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    aaaaamigo itw working!
    Your script works.

    Trying to figure out the hell is going on with my buildings!
     
  43. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Yes! just was Answering!

    OMG! that dataset could be such a brutal mess?!?!?!
    ________________________________________________________________
     
  44. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Baking into a single mesh without touching materials would reduce the drawcalls from the thousands to 108 that is the total mat number for the city buildings but my buildings fall apart so need to have them as separate meshes. So no batching possible. If this is confirmed the only way would be to be able to pack everything to several few atlases, say 2 o 3 4k atlases. The issues is thath mesh baker only bakes to 1 atlass. If I use the Prefab baker procedure, its not viable because as so many mats are shared diferent atlases will have same textures repeated... ufff

    If adding an option to output all the buildings to "n" atlases is too difficult or there is no time for so, I'm ready to donate 5-10$ bucks for the script you prepared.

    I feel like an idiot, I can not believe a single building has such a brutal number of materials, and that they're already in there...ffff
     
  45. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
  46. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    No worries. if you can use Mesh Baker that is great. Otherwise no worries. Don't worry about the script.
     
  47. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Oh thank you Phong!. I felt like an idiot. Now I've a serious mess to fix... I'll try may be with flatiron that seems to export to multiple atlases but is not a cheap solution. Thanks for your help! And may be consider the multiple atlas output solution for the future!

    Best,

    Pablo
     
  48. KnightPista

    KnightPista

    Joined:
    May 18, 2015
    Posts:
    40
    Hi,

    after downloading plugin, inserting MultiMeshBaker into scene, the OnEnable function in the MB3_MeshBakerEditorInternal.cs is not called resulting in _init function not being called and null reference exception causing not rendering any editor layout for this type of baker.

    I fixed that by inserting OnEnable() {...} function in the MB3_MultiMeshBakerEditor.cs

    I also found a null-reference exception bug (following text block is for developer, as he is the only one that can decrypt what will be written):
    When using MeshBakerGrouper, It would be great if child bakers could only contain objects from the parent texture baker set, because now these bakers contain all gameobjects in their cluster, causing null reference exception in MB3_MeshCombinerSimple.cs on line 1134, where mesh is null. This is caused because toAddDGOs and _goToAdd are not in sync by index when some _goToAdd is null. _goToAdd was null because it could not find a material for object, because that object material was not baked by parent material baker.

    I fixed it by inserting toAddGOs.Add(null) after if-block on MB3_MeshCombinerSimple.cs:933 fails and then checking for null+continue in "//add new" loop few lines later.
     
    hammmm likes this.
  49. aditya-tfg

    aditya-tfg

    Joined:
    Aug 8, 2016
    Posts:
    4
    Hi,

    We are planning to buy this wonderful asset, but have few doubts :
    1. Will it work with materials created with Substance Designer? I tired to check with your free version, but it did not work with substance. I've read that the free version supports only legacy diffuse & bumped diffuse, so thought it would be better to ask.
    2. Our character uses Double sided Standard-Fade shader for hair, n Standard-opaque for rest of the body parts. Will these be combined properly?
    3. We are making a multiplayer game, and so is it possible to bake/combine remote players characters when they spawn, i.e. run-time combining or something like that.
    4. We have multiple blend-shapes on our character, specially face, to change its features. Will the combined mesh keep the mesh same as we changed it using blend shapes?

    Note : We are using Unity3D 5.3

    Cheers
     
    Last edited: Aug 8, 2016
  50. buttmatrix

    buttmatrix

    Joined:
    Mar 23, 2015
    Posts:
    609
    Hey Ian, been a while since I last posted, but here we go: SpeedTrees? Specifically, 'Conifer - Desktop' and 'Broadleaf - Desktop'.
    1. Took Conifer and Broadleaf from Standard Assets (Environment)
    2. Extracted LOD_0 from each prefab in the hierarchy, deleted the rest
    3. Ran multi-mesh and material baker (4 submeshes; see Screenshot 240)
    You will notice a color discrepancy between the original (see Screenshot 239) and the new combined mesh (see Screenshot 238), why?

    Also, the new combined mesh does not animate in response to a windzone(?). [I see you addressed this question a couple years ago in response to TreeCreator trees, but I don't expect that will apply to SpeedTree].

    Suggestions?
    [Unity 5.4.0f3 / Windows 10 / Deferred / Linear]
     

    Attached Files:

    Last edited: Aug 17, 2016