Search Unity

Mesh Baker by Digital Opus [RELEASED]

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

  1. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    Oooh! This sounds very promising! :) Ok I'll give this a try in a bit. Dealing with some tricky bugs at the moment. :oops:

    Personally .JPG works for me because it's only on the SPEC\NORM\EMI maps. I can live with a slightly blurrier version of those textures if I can get reduced file size. (Just a side tangent, but does Unity let us control the compression strength for .JPG? That might be an alternative for those that want to maintain high image fidelity, but potentially smaller file sizes)

    Yea, I think TGA and PNG don't have much differences? (besides TGA being larger in file size) .. though I'm not sure if their RGBA channel usages are the same? :confused:

    Thanks again! I'll update you once I've given the above script a try
     
    Phong likes this.
  2. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I have not tried the "EncodeToJPG()" method yet. However it has an overload EncodeToJPG(int quality); I think this allows to control the quality.
     
  3. eblumrich

    eblumrich

    Joined:
    Nov 12, 2015
    Posts:
    105
    Hi.

    I have a bunch of meshes that total 2.5 million tiangles,.

    I combine them using MeshBaker, and suddenly, the result of 79 million triangles.

    What is causing this?
     
  4. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Where are you seeing the 2.5 million triangles? Is it in the "stats window"?

    upload_2023-4-14_17-23-47.png

    This is the number of triangles being rendered, not the total number of triangles in the scene. Meshes that are not visible (their render bounds don't intersect the camera fustrum) can be culled and are not included in the "Tris: ".

    When meshes are combined, you end up with a single large mesh that can have physically very large bounds. If any part of this bounds intersects the camera fustrum then the entire combined mesh gets rendered, even if most of the triangles are not visible. This is probably what is happening.

    There are a few things you can do.

    1) Try to combined meshes that are close together. Don't combine meshes that are far apart or will never be visible at the same time.
    2) You can use the Batch Prefab Baker to make prefabs that share a material and use these in your scene instead. Then turn on static batching. Static batches are special combined meshes where parts of them can be culled. More on this in the Batch Prefab Baker tutorial video.
     
  5. eblumrich

    eblumrich

    Joined:
    Nov 12, 2015
    Posts:
    105
    No. This is in the profiler.

    When I bring the objects that are meant to be converted into one via MeshBaker into a blank scene, the tri count in the profiler is actually about 51K.

    After combining those same objects into one via the meshbaker via the method suggested in your documentation, the tri count in the profiler rises to 97 million.

    I understand that that might be caused by using a multitude of textures in the texture baker, so I tried to export the chunk as a sigle object, using a single atlas texture.

    The prefab that resulted contained in excess of 3.1 million tris - that is a HUGE increase from 1.9 million
     
    Last edited: Apr 15, 2023
  6. eblumrich

    eblumrich

    Joined:
    Nov 12, 2015
    Posts:
    105
    For example: Here is a screenhot showing all of the objects meant to be combined into a single sector, shown in-range of the camera. upload_2023-4-14_19-17-54.png |

    Yo can see that the unbaked objects have about 1.9 million tris.

    Now- let's bake those objects using Meshbaker. and bring that into the same blank scene.

    upload_2023-4-14_19-28-31.png

    (It is now all white, so that I can show you that it has nothing to do with multiple textures) The entirety of the meshbaker prefab is in view, and it now 3.31 million tris. If I had broken it into various textures, it would have topped out between 70-90 million tris.

    So- somehow Meshbaker turned an object that was formerly 1.9 million tris, into one that is 3.3 million tris to one that was almost 100 million. This should not be the case, right? It's like the exact OPPOSITE of what Meshbaker is supposed to do.

    EDIT: I have done this with smaller objects, and it invariably turns something that was a few hundred tris into something that is a few thousands/million.

    We're using Unity 2021.3.16f1
     

    Attached Files:

    Last edited: Apr 15, 2023
  7. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    The profiler is similar to the stats window, it is not measuring the number of triangles in the combined mesh, it is measuring the number of triangles processed when rendering. This is a huge jump in the number triangles being rendered. Mesh Baker can sometimes increase the number of vertices in meshes but I can't think of anything that would cause the tris to increase. If all the objects are in view before and after the bake, then this jump must be caused by the Renderer settings of the new combined mesh and the materials on the combined mesh. Check static flags, shadows, lights, render layers to see if there could be settings on the combined mesh that could be causing this jump. This things can have a dramatic effect on rendering efficiency.

    I would suggest checking a few things.

    1) Bake the combined mesh using the "bake into prefab" option. When you do this there will be a mesh asset generated in the project for the combined mesh. If you select the asset you should be able to see exactly how many triangles there are in the combined mesh. The number of triangles should be equal to the total number of triangles in the source meshes.

    2) Render the scene containing the combined mesh and capture the render using the "Frame Debugger Window". In the "Frame Debugger Window" you should be able to step through the render drawcall-by-drawcall. For each drawcall you can see the number of indicies and verts rendered. The "indicies" number will be number-of-triangles * 3. This should show you where the extra triangles are coming from. https://docs.unity3d.com/Manual/FrameDebugger.html
     
  8. BlackBunnyCrankn

    BlackBunnyCrankn

    Joined:
    Mar 21, 2020
    Posts:
    21
    Does anybody know of any resources around memory performance tuning? I noticed that mesh baker leaves all the of old meshes\materials in the scene so i am assuming when we do a full build these unused materials\meshes in the build are added but are not used?

    Wondering if anybody has done anything or is it just a bunch of manual work moving colliders over and deleting the old resource?
     
  9. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    There is a button on the Mesh Baker: "Disable/Enable Renderers on Source Objects" You could modify the code to delete the Renderer and MeshFilter components. Everything else (colliders, scripts, animators) you should be able to leave as is. This is probably the least amount of work. Be sure to back up your scenes before deleting all the source renderers.
     
    BlackBunnyCrankn likes this.
  10. YenCheng1978

    YenCheng1978

    Joined:
    Jul 25, 2021
    Posts:
    11
    Hi~
    UnlitTextureArrays Shader Does not work on URP.
    Can you help me get the Shader version that can be used?
    Thank you.
     
  11. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    What error are you getting?
    Also what version of Unity and URP are you using?
    What does the scene MeshBaker/Examples/SceneBaasicTextureArrays.unity look like? Is it pink textures?

    I am testing the UnlitTextureArray shader with Unity 2021.3.3 and URP 12.1.6 and it appears to work for me.
     
  12. YenCheng1978

    YenCheng1978

    Joined:
    Jul 25, 2021
    Posts:
    11

    Shader "Unlit/UnlitTextureArrays_URP"
    {
    Properties
    {
    _BaseMap ("Tex", 2DArray) = "white" {}
    //_ArrayID("ArrayID", Integer) = 0
    }

    SubShader
    {
    Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" "RenderPipeline" = "UniversalPipeline" }
    Cull Off

    Pass
    {
    Name "ForwardLit"
    Tags{"LightMode" = "UniversalForward"}

    HLSLPROGRAM
    #pragma target 4.5

    #pragma vertex LitPassVertexSimple
    #pragma fragment LitPassFragmentSimple

    #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

    CBUFFER_START(UnityPerMaterial)
    //int _ArrayID;
    CBUFFER_END

    TEXTURE2D_ARRAY(_BaseMap); SAMPLER(sampler_BaseMap);

    struct Attributes
    {
    float4 positionOS : POSITION;
    float2 texcoord : TEXCOORD0;

    UNITY_VERTEX_INPUT_INSTANCE_ID
    };

    struct Varyings
    {
    float4 positionCS : SV_POSITION;
    float2 uv : TEXCOORD0;

    UNITY_VERTEX_INPUT_INSTANCE_ID
    UNITY_VERTEX_OUTPUT_STEREO
    };

    Varyings LitPassVertexSimple(Attributes input)
    {
    Varyings output = (Varyings)0;

    UNITY_SETUP_INSTANCE_ID(input);
    UNITY_TRANSFER_INSTANCE_ID(input, output);
    UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);

    output.positionCS = TransformObjectToHClip(input.positionOS.xyz);

    output.uv = input.texcoord;

    return output;
    }

    half4 LitPassFragmentSimple(Varyings input) : SV_Target
    {
    UNITY_SETUP_INSTANCE_ID(input);
    UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);

    half4 baseColor = SAMPLE_TEXTURE2D_ARRAY(_BaseMap, sampler_BaseMap, input.uv, SLICE_ARRAY_INDEX);

    return baseColor;
    }
    ENDHLSL
    }
    }
    }






    SLICE_ARRAY_INDEX not work
     
  13. YenCheng1978

    YenCheng1978

    Joined:
    Jul 25, 2021
    Posts:
    11
    Unity 2021.3.19f1
     
  14. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    You are very close. This seems to work.


    Code (CSharp):
    1. Shader "Unlit/UnlitTextureArrays_URP"
    2. {
    3. Properties
    4. {
    5. _BaseMap ("Tex", 2DArray) = "white" {}
    6. //_ArrayID("ArrayID", Integer) = 0
    7. }
    8.  
    9. SubShader
    10. {
    11. Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" "RenderPipeline" = "UniversalPipeline" }
    12. Cull Off
    13.  
    14. Pass
    15. {
    16. Name "ForwardLit"
    17. Tags{"LightMode" = "UniversalForward"}
    18.  
    19. HLSLPROGRAM
    20. #pragma target 4.5
    21.  
    22. #pragma vertex LitPassVertexSimple
    23. #pragma fragment LitPassFragmentSimple
    24.  
    25. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    26.  
    27. CBUFFER_START(UnityPerMaterial)
    28. //int _ArrayID;
    29. CBUFFER_END
    30.  
    31. TEXTURE2D_ARRAY(_BaseMap); SAMPLER(sampler_BaseMap);
    32.  
    33. struct Attributes
    34. {
    35. float4 positionOS : POSITION;
    36. float3 texcoord : TEXCOORD0;
    37.  
    38. UNITY_VERTEX_INPUT_INSTANCE_ID
    39. };
    40.  
    41. struct Varyings
    42. {
    43. float4 positionCS : SV_POSITION;
    44. float3 uv : TEXCOORD0;
    45.  
    46. UNITY_VERTEX_INPUT_INSTANCE_ID
    47. UNITY_VERTEX_OUTPUT_STEREO
    48. };
    49.  
    50. Varyings LitPassVertexSimple(Attributes input)
    51. {
    52. Varyings output = (Varyings)0;
    53.  
    54. UNITY_SETUP_INSTANCE_ID(input);
    55. UNITY_TRANSFER_INSTANCE_ID(input, output);
    56. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    57.  
    58. output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
    59.  
    60. output.uv = input.texcoord;
    61.  
    62. return output;
    63. }
    64.  
    65. half4 LitPassFragmentSimple(Varyings input) : SV_Target
    66. {
    67. UNITY_SETUP_INSTANCE_ID(input);
    68. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    69.  
    70. half4 baseColor = SAMPLE_TEXTURE2D_ARRAY(_BaseMap, sampler_BaseMap, input.uv.xy, input.uv.z);
    71.  
    72. return baseColor;
    73. }
    74. ENDHLSL
    75. }
    76. }
    77. }
    The changes I made:
    • float3 texcoord : TEXCOORD0;
    • float3 texcoord : TEXCOORD0;
    • SAMPLE_TEXTURE2D_ARRAY(_BaseMap, sampler_BaseMap, input.uv.xy, input.uv.z);
     
  15. YenCheng1978

    YenCheng1978

    Joined:
    Jul 25, 2021
    Posts:
    11

    Really Works, Thanks a lot ^^
     
    Phong likes this.
  16. Nunggi

    Nunggi

    Joined:
    Apr 7, 2018
    Posts:
    7
    Hello
    Error using 'Batch Prefab Baker'.
    Add an object to "Texture Baker"
    "Populate Prefab Rows From Texture Baker"
    If you press , an error message is displayed and does not work.


    ArgumentNullException: Value cannot be null.
    Parameter name: obj
    UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource[TObject] (TObject componentOrGameObject) (at <3d9f46ac475f4fa9a3b71bacf80244a8>:0)
    DigitalOpus.MB.Core.MBVersionEditorConcrete.PrefabUtility_GetCorrespondingObjectFromSource (UnityEngine.GameObject go) (at Assets/_BarrelWalk/External_Assets/MeshBaker/Editor/MB3_MBVersionConcreteEditor.cs:165)
    DigitalOpus.MB.Core.MBVersionEditor.PrefabUtility_GetCorrespondingObjectFromSource (UnityEngine.GameObject go) (at Assets/_BarrelWalk/External_Assets/MeshBaker/Editor/core/MB3_MBVersionEditor.cs:95)
    DigitalOpus.MB.MBEditor.MB3_BatchPrefabBakerEditor.PopulatePrefabRowsFromTextureBaker (MB3_BatchPrefabBaker prefabBaker) (at Assets/_BarrelWalk/External_Assets/MeshBaker/Editor/MB3_BatchPrefabBakerEditor.cs:159)
    DigitalOpus.MB.MBEditor.MB3_BatchPrefabBakerEditor.OnInspectorGUI () (at Assets/_BarrelWalk/External_Assets/MeshBaker/Editor/MB3_BatchPrefabBakerEditor.cs:137)
    UnityEditor.UIElements.InspectorElement+<>c__DisplayClass58_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <3d9f46ac475f4fa9a3b71bacf80244a8>:0)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr)
     
  17. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Thanks for making me aware of this. I will definitely fix it. When you did this step:

    Add an object to "Texture Baker"

    Was the object an instance of a prefab? The Batch Prefab Baker only works with prefabs, so the object must be an instance of a prefab.
     
  18. AndrewLaBoyXR

    AndrewLaBoyXR

    Joined:
    Sep 23, 2016
    Posts:
    9
    Been trying to figure this out for last couple of days, I'm making an editor tool that bakes the meshes, and want to add the materials to the TextureBaker at runtime. I'm setting the colors on the material base color as opposed to writing textures as that would require hundreds to thousands of textures. It seems to work when I'm using the tool manually but if I'm doing it in runtime it doesn't appear to be properly saving the _BaseColor of the materials, as the final result turns out white. I have Include Colors checked. Edit: I tried setting a unique texture of different colors on each material's _BaseMap and that's not working either, still white.

    My code:

    textureBaker.textureBakeResults = ScriptableObject.CreateInstance<MB2_TextureBakeResults>();

    combinedMaterial = new Material(Shader.Find("Universal Render Pipeline/Unlit"));

    textureBaker.resultMaterial = combinedMaterial;

    textureBaker.objsToMesh.AddRange(voxelsToBake);

    textureBaker.CreateAtlases();

    meshBaker.textureBakeResults = textureBaker.textureBakeResults;

    meshBaker.AddDeleteGameObjects(voxelsToBake, null, false);
    //I've tried this variation as well with same result:
    //
    meshBaker.AddDeleteGameObjects(textureBaker.GetObjectsToCombine().ToArray(), null, false);


    meshBaker.Apply();

    AssetDatabase.CreateAsset(combinedMaterial, "Assets/Resources/PuzzlePrefabs/Materials/" + this.gameObject.name + "CombinedMaterial.mat");

    Warning: Mesh (UnityEngine.Mesh) didn't have colors. Generating an array of white colors
     
    Last edited: Jul 11, 2023
  19. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
     
  20. Phong

    Phong

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

    When you run:

    textureBaker.CreateAtlases();

    What does it say in the console? There should be a "Report ..." line describing which atlases are created. Is there an atlas for _baseMap? If none of the source materials have a texture assigned then you probably need to check "blend non-texture properties" on the TextureBaker settings. This will make the TextureBaker to pay attention to the different color tints in the source materials.

    Re: the warning: Warning: Mesh (UnityEngine.Mesh) didn't have colors. Generating an array of white colors

    You must have the "Colors" channel checked in the mesh baking settings. These are colors that get baked into the mesh itself. They are distinct from the _BaseMap texture atlas and material color tints. Your shader needs to be written to read the color tint from the mesh to use them. Mesh Baker is expecting your source meshes to have a color channel which it looks like they don't have. That is why you are getting the warning message. You probably want to uncheck the "Colors" channel in the mesh baker settings because I think you want to use the color tints in the _baseMap atlas.

    BTW: I am going to release a new example scene and script in the next version of Mesh Baker that shows how to create and populate a MB2_TextureBakeResults object without using a TextureBaker. It builds an atlas from color tints and sets up. This is useful at runtime. A MeshBaker can be used with this TextureBakeResults. The TextureBaker is a bit heavy to use at runtime. If you are working with solid color blocks then this script is much lighter and faster than using the full TextureBaker. Ping me at contact.digitalopus@gmail.com if you are interested and I can send you a UnityPackage with the script.
     
  21. AndrewLaBoyXR

    AndrewLaBoyXR

    Joined:
    Sep 23, 2016
    Posts:
    9
    Report:

    Report
    ----------
    This set of textures will be a rectangle in the atlas. It will be resized to:1x1
    [_BaseMap Bunny Voxels10_Texture0 1x1] obUV scale (0, 0) offset(0, 0)

    (Repeats hundreds of times until message truncates)

    A 2nd log confirms atlas as well.

    "Created Atlases"

    The final material simply doesn't have a texture on it. But if I manually texture bake with all settings being the same it works.

    I enabled Blend Non-Texture Properties and disabled Include Colors now and it still doesn't work. Yes all materials have a unique texture on it with its corresponding color and each one has their _BaseColor set the same as well to cover all bases but it still comes out white. Is it possible that since I'm running this in Editor mode (as in the play button in the scene is off) that it's not creating the atlas properly? I notice that the atlas only actually appears when I manually texture bake using the inspector buttons, even though it says "Created Atlases" when I call it in function. So the file itself isn't being truly created, which is why the final combined material's texture is null. Sure I will e-mail you now, but if that new function also doesn't work in an Editor script then the same problem will remain.
     
  22. Phong

    Phong

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

    I tried tinkering with the "Examples/BakeTexturesAtRuntime.Unity" scene and was able to reproduce your problem. The _BaseMap atlas came out white. The problem was the URP TextureBlender "TextureBlenderURPLit.cs". It wasn't being selected for the "Universal Rendering Pipeline/Unlit" shader.

    I have attached a UnityPackage here with a new version of the example scene (Modified to use URP Unlit shader), and updated TextureBlenderURPLit.cs script.

    Also attached is a prototype example script and scene for hacking a TextureBakeResult at runtime. Currently you can drive it at runtime using the script's ContextMenu commands.
     

    Attached Files:

  23. AliaksandrMarachkin

    AliaksandrMarachkin

    Joined:
    Oct 9, 2016
    Posts:
    3
    Hi, is it possible to use MeshBaker in runtime on the Ios or Android, bake meshes before playing on level, or this asset is not optimized for mobile platforms. Does somebody have experience using this asset for mobile development? thx
     
  24. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Baking the atlases (TextureBaker) at runtime is slow, and not all features are supported at runtime.
    Baking the meshes works well at runtime. It is possible to bake the atlases in the editor and bake the meshes at runtime. I recommend doing this if possible.
     
  25. pbiesek92

    pbiesek92

    Joined:
    Feb 9, 2016
    Posts:
    11
    Hi, after playing around with MeshBaker I finally managed to bake all armor pieces into one skinned mesh
    upload_2023-7-17_2-21-40.png
    however after I made this new mesh a child of my original prefab and saved it, the mesh in the prefab is not visible..
    upload_2023-7-17_2-23-8.png
    how can I save the baked mesh as part of an existing prefab?

    also how would I go about baking meshes for an object that has different levels of detail?
    do I have to bake each LOD separately?
     
    Last edited: Jul 17, 2023
  26. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Re: Saving as a prefab. There are two possibilities:
    1. Did you use the "Output -> Bake Into Prefab" when you baked the combined mesh. You need to use this. If you did not then the mesh is saved in the Scene and is not accessible from other assets and scenes in the project. If you use "Bake Into Prefab" then the mesh gets saved as an asset. Check the "mesh" field on the Skinned Mesh Renderer component that you added to the prefab. If it is blank then this is probably what happened. See this video at 4:18 for more info:

    2. If the mesh is present then the issue could be the Bounds settings on the Skinned Mesh Renderer. Select the game object with the bad SkinnedMeshRenderer and click the "Edit Bounds" button. The bounds should enclose the mesh in all possible poses. If it does not then the mesh can be culled (not drawn) if the bounds does not intersect the camera fustrum.
    Re: LODs Yes you need to bake a separate combined mesh for each LOD.
     
  27. Adam_Benko

    Adam_Benko

    Joined:
    Jun 16, 2018
    Posts:
    105
    How does mesh baker handle gameobjects with lods ? For example, I have asteroid field made out of various asteroids and each of them have 2 LODS and main GO with a collider. Will mesh baker merge each LOD mesh and create 3 separate meshes ? So that the LOD still exists. What about the mesh colliders ?
    Thanks.
     
  28. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Good question.

    Regarding colliders. You don't want to combine these. Optimizing physics is very different from optimizing rendering. With physics it is better to have many separate tight fitting colliders. When using colliders on LODGroups it is best to separate the colliders from the LODs using a hierarchy like this:

    • MyAsteroidPrefab (scripts can be placed here)
      • PhysicsColliders (good place for RigidBody, trigger and collision scripts)
        • ColliderA (SphereCollider)
        • ColliderB (BoxCollider)
        • etc...
      • RenderingLODGroup
        • LOD0
        • LOD1
        • etc...
    This hierarchy separates the physics scripts and components from the rendering. If you combine the rendering meshes you can disable or delete the renderers and your physics components + scripts are still there so they will function as normal.

    Regarding LODs. There are two options for LODs:

    1) Combine the LOD0 meshes together and combine the LOD1s together etc... Then build a new LODGroup using the combined meshes for LOD0, LOD1 etc... This only works well if your meshes are grouped close together (eg. a house + furniture props) because the combined LODGroup can only have one set of object-size-percentages for switching LOD level. This approach is probably not usable for you unless your asteroids are grouped into tight clusters.

    2) Use the Batch Prefab Baker and static batching. With this approach you create modified asteroid prefabs that all share a single combined material. These prefabs are identical to your original prefabs with their scripts and LODs except that they share a material and use meshes that have UVs adjusted to use the combined material. You can use these in your scene in place of your original prefabs (Mesh Baker has a tool for replacing instances). Then make them all static. Unity can combine these meshes using static batching. LODs + frustum culling still works per-renderer! See this video for a tutorial on this approach.

     
  29. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    128
    Hello @Phong , I'm trying to understand why my building meshes are coming out like this. So, I have this building model, which I can share if necessary. It is baked with Texture Baker and then with Mesh Baker. The walls are coming out like this: the texture is applied correctly, but it looks dark. I've watched the video and tried all the options suggested(Also tried multiple material option but same type of mesh was generated), but it still looks like this. It is not completely dark, but a dark shadow is planted on it. Also, if I move it, a baked mesh black spot generates over it. Is it due to lightmapping?

    I'm attaching images and settings for reference.
     

    Attached Files:

  30. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Is the original mesh lightmapped? If so then you probably need to re-bake the lightmapping on the combined mesh. I would suggest mesh-baking with "lightmapping-uvs = generate new UV Layout" or "lightmapping-uvs = copy UV2 to separate rects". Then set the baked mesh to static and re-bake the lighting.

    I would check the MeshRenderer render settings on the source MeshRenderer and the combinded MeshRenderer look for any differences.

    Also compare the source material(s) and the combined material. The HDRP shader can be very complex. are there any obvious differences? I would pay particular attention to the color tint and the smoothness, metallic settings.

    How does the atlas texture look compared to the original brick texture. Is the atlas darker. Are they using the same sRGB setting (could this be a linear vs gamma color space issue)?
     
  31. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    128
    Original meshes were lightmapped. I tried couple of ways to understand the issue. I tried unity's lightbaker issue was resolved but then I tried bakery where with different Lightmapping UV's setting I'm getting different results. Only somewhat good result(Last image) I'm getting when I'm generating new UV2 layout and setting Uv2 hard angle 180 and Uv2 packing margin 0. Do I need to check some settings in bakery before baking? I'm getting same result even if I disable Asset Uv packaging option to not set in bakery

    upload_2023-8-9_16-25-22.png upload_2023-8-9_16-25-41.png upload_2023-8-9_16-25-56.png
     
    Last edited: Aug 9, 2023
  32. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I would use a hard angle less than 90 degrees (80 would be good). And a small positive packing margin (4 texels). That should produce better results.

    Also are the source renderers disabled and combined renderers set to static?

    What is the "scale in lightmap" of the combined mesh? If it is too small you might be getting very low resolution lighting on your combined mesh.

    I am not very experienced with Bakery. does it have views where you can select an object in the scene and see its UV2 layout on the lightmap (Unity does this). This should show you if the scale in lightmap is reasonable and if the UV2 layout looks reasonable. Also Unity has a view where you can see the texel resolution on your mesh. This is very good for estimating a good scale in lightmap.
    upload_2023-8-9_9-47-38.png
     
  33. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    128
    UV map looks like this


    Bakery gives error that UV maps are overlapped for all combined meshes
     
  34. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    This is almost certainly the problem.

    Can you double check the bake settings "Lightmap Setting"? If you use Preserve existing UV layout then this will happen. If you choose Generate New UV2 Layout then it should generate a new UV2 layout. If you are using Generate New UV2 Layout then it should work. Are you getting any errors or warnings in the console when you bake. Note that Mesh Baker uses Unity's API for generating the UV2s. It is possible for this method to quietly fail (unfortunately it does not return an error message when this happens) if generating the UV2 map would generate more than 64k vertices. It is very possible that this is happening.

    You could try "Copy UV2 To Separate Rects". This might work better or split the source meshes into two meshes.
     
  35. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    Whenever I run the MB3_Mesh Baker script it says: "Plane (UnityEngine.GameObject) -38668has submeshes which share verticies. Adjusted uvs may not map correctly in combined atlas." The created mesh does not look correct (half of it is pure white without a texture. It has 3 materials - two are color-only, and one has a texture. The texture part works, but the other two materials didn't combine properly.

    What do I need to do in order to fix the model such that it doesn't share vertices?
     
  36. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    128
    Thanks, I've managed to fix the issue by removing a few mesh objects from the overall list. Unfortunately, I don't have the logs with me, but I had to use Unity Texture Packer multiple times because MeshBaker's Texture Baker crashed with an out of memory error.

    Now, I have a question. Currently, the atlas size is quite large, around 60-100MB. Is there anything else I can do to reduce it, as it consumes too much memory, apart from the usual Unity suggestions like compressing textures, etc.?
     
  37. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I think there are two things happening here:

    Re: Two materials with color only. I assume that you mean you are using a color tint and no texture. These are tricky. The safest option would be to create two small 16x16 solid color textures and assign these to the source materials. The textures will get copied to the atlas and everything should work. You could try "Blend Non-Texture Properties". This should work if your source materials are combined material are using one of the built in shaders such as URP or Standard or if your shader uses similarly named properties.

    Re: Submeshes sharing verticies. You will need to fix this in a modeling program. I believe this happens if all the channels for two vertices on different submeshes have the same value (position, normal, uv, tangent, ...). One way to fix it would be to shift the UV layout of each submesh by a tiny amount (.0001) so that the UV channel has a slightly different value for the verticies that line up.
     
    Last edited: Aug 14, 2023
    lclemens likes this.
  38. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Re: Atlas is large. Take a look at the atlas. Is there a lot of tiling baked into the atlas? If so, and the tiling is limited to a few materials, then you can use the Multiple Materials feature to map each of these materials to their own submesh. Don't use "consider UVs" for these singleton submeshes. Then the source texture is free to tile with only one copy of the texture in the atlas. This video has more information on this technique:


    Is there a lot of black space in the atlas? Try using the "resize power of two textures". This may help the atlas pack much more efficiently if your source materials are powers of two.

    If your atlas is packed efficiently without a lot of tiling, then your only option is to scale the atlas down to a smaller size and/or compress it.
     
  39. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    Thanks! That worked when I made a 16x16 solid black texture for the source material.

    I couldn't figure out how to shift UV layout in Blender (I'm not very proficient with it and I bought the fbx model), but it seems to be working okay without doing that despite the warning.
     
    Phong likes this.
  40. RetrKill0

    RetrKill0

    Joined:
    Sep 4, 2022
    Posts:
    7
    Hello, I would like to know if mesh bake handles models without uv mapping like the one below:

    Can anyone answer me?
    Because I only realized that it was important to do the UV for the objects when I went to bake the lighting in unity. I'm looking for alternatives to be able to optimize the game without having to map more than 150 houses/buildings with uv. They all use multiple textures and don't have a mapping.

    With mesh bake could I solve my problem?

    upload_2023-8-19_1-25-41.png
     
  41. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I am not sure I understand. Do the models not have UVs? In the image you posted there appear to be UVs in the blender window on the left?

    You say that the models use multiple textures. How are the textures mapped onto the meshes? Which shader are you using? Are they custom shaders?

    Mesh Baker relies heavily on UVs. It is a tool for creating atlases which require that textures be projected onto meshes using UVs.

    Note that if you only need UVs for lightmapping then Unity can generate these for you when the meshes are imported. This might solve your problem.

    upload_2023-8-19_11-36-0.png
     
    RetrKill0 likes this.
  42. RetrKill0

    RetrKill0

    Joined:
    Sep 4, 2022
    Posts:
    7
    look, I don't know who you are, but now you've become my hero, it's not that this already saved me, it worked, thank you very much, but I have a question, is it worth using this asset instead of using unity's bake?
     
  43. zevonbiebelbrott

    zevonbiebelbrott

    Joined:
    Feb 14, 2021
    Posts:
    122
    Hi, I was wondering for example if I have Object B that is a small cube, and I want to bake it into my main object A, a big sphere.

    Now I want to duplicate the same Object B 10 times, and place it in random positions before baking all 10 object Bs into Object A.

    Do I have to keep 10 duplicates of Object B in order to remove one? Lets say Object B is just a mesh renderer nothing else.
    Can I just keep 1 original Object B and use that to remove/unbake different clones of Object B at runtime? Or do I need to keep all 10 originals in the scene to remove the appropriate one later?
    Basically can I get away with using 1 object to add/remove multiple clones of that object, or does each Unbake needs its own separate object in order to unbake?

    Im asking because I have a Car Object (Object A) and I want to add/remove a lot of the same cloned turret (Object B). The turret is just a mesh renderer, no scripts no nothing.
     
  44. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    By 'this asset' I presume you mean Mesh Baker. Mesh Baker won't help much with Lightmap Baking (you might want to look at 'Bakery'), but it does help with static and dynamic batching and reducing drawcalls. Mesh Baker combines many materials into a single material and modifies your props to use the single material. This makes rendering much more efficient by allowing the GPU to process fewer, larger batches of meshes. This is very important on platforms with limited hardware like mobile and VR.
     
  45. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    You can delete objects using the GID of the object that was baked: myGameObject.GetInstanceID()
    You can cache this id before you delete the source cube. Then you can delete it using:

    Code (CSharp):
    1. int[] gidsOfGameObjectsToBeDeleted = new int[] { ... GIDs of GameObjects to be deleted}
    2. myMeshBaker.AddDeleteByID(null,  gidsOfGameObjectsToBeDeleted);
     
    zevonbiebelbrott likes this.
  46. talharing

    talharing

    Joined:
    Oct 27, 2013
    Posts:
    14
    hello, im having an issue - everytime i mesh bake unity reimports all the textures in my project, for hours :/
    im using 2021 lts urp, android platform.
    happens in different projects...
    help??
     
  47. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Note that MeshBaker needs the textures that it will be copying to the atlas to be readable and uncompressed. To do this it converts the texture formats before creating the atlases and unconverts them after. However this should only affect the textures that are on the list of objects to be combined, not all the textures in the project.

    Do you have any AssetPostProcessors in the project? Particularly ones that implement OnPostprocessTexture() . Unfortunately the behavior of this appears to be buggy. If you reimport a texture, often Unity will decide that many other unrelated textures need to be imported too. You can see this by manually changing a setting on one of your textures and clicking "Apply". Unity will reimport a bunch of unrelated textures in addition to the one you modified. It might take a few different textures before you find a bad one that triggers a cascade. Could this be the issue?
     
  48. talharing

    talharing

    Joined:
    Oct 27, 2013
    Posts:
    14
    hi!
    changing a setting on one of the textures doesn't trigger reimporting of any other textures... :(
    so it doesn't seem to be the case...
    in your first paragraph you said the textures i want to combine need to be read\write enabled and not using crunch compression? is that what u mean? could that have triggered somehow to reimporting of all the project's textures?
     
  49. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Re: "changing a setting on one of the textures doesn't trigger reimporting..." Try several different textures. Some of the textures trigger a cascade, others don't. It is a weird bug. Did you search the sourcecode to see if there are any AssetPostProcessor in the project?

    Mesh Baker will change the textures automatically to: readonly, uncompressed, RGB or RGBA format. After the bake it reverses this to whatever the original settings were. You can set these texture settings yourself before the bake and the importing won't be necessary. However that is tedious and hard to find all the textures.

    You could also try using the Mesh Baker Texture Packer Fast or Mesh Baker Texture Packer Fast V2 texture packers. These packers use the compressed version of the the textures. The downside is that you are copying compressed/degraded images to the atlas so the quality is not as good.
     
  50. talharing

    talharing

    Joined:
    Oct 27, 2013
    Posts:
    14
    Did you search the sourcecode to see if there are any AssetPostProcessor in the project?

    im sorry, im not sure how to do that...
    but this is happening to me on another unrelated project... a test one, much simpler...
    have you ran into this issue ever? what could be the problem?
    i have a gtx 2060...