文章目录
  1. 1. FAQ
    1. 1.0.0.1. 1.合并网格后,BlendShape会丢失,组合网格,BlendShape不会丢失,但BlendShape动画会失效。
    2. 1.0.0.2. 2.合并网格时,如果SkinnedMeshRenderer里的Bone有些找不到,则会出现合并后看不到模型的情况。
    3. 1.0.0.3. 3.对MeshBaker 合并的网格进行烘焙,光照图出现黑斑,如图
  • 2. 合并网格代码
  • FAQ

    1.合并网格后,BlendShape会丢失,组合网格,BlendShape不会丢失,但BlendShape动画会失效。

    2.合并网格时,如果SkinnedMeshRenderer里的Bone有些找不到,则会出现合并后看不到模型的情况。

    3.对MeshBaker 合并的网格进行烘焙,光照图出现黑斑,如图


    这种情况是在Unity5下,没有设置LightMapping UVs 为Generate_new_uv2_layout导致的

    合并网格代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

    public class CombineBones : MonoBehaviour {

    public GameObject bone;
    public List<GameObject> models = new List<GameObject>();
    public GameObject human;

    // Use this for initialization
    void Start () {
    human = GameObject.Instantiate<GameObject>(bone);
    human.SetActive(true);
    bone.SetActive(false);

    // 这种是合并网格
    addPart2();
    // 这种是组合网格。只是把模型挂到骨骼里,并替换模型的骨骼
    // addPart3()
    }

    void addPart2()
    {
    var bones = getAllChildTransform(human.transform);
    List<Material> mats = new List<Material>();
    List<Transform> newBones = new List<Transform>();
    List<CombineInstance> combineInstances = new List<CombineInstance>();

    foreach (var go in models)
    {
    var smrs = go.GetComponentsInChildren<SkinnedMeshRenderer>();
    foreach (var smr in smrs)
    {
    foreach (var b1 in smr.bones)
    {
    Transform finded = null;
    foreach (var b in bones)
    if (b1.name == b.name)
    {
    finded = b;
    break;
    }
    if (finded != null)
    {
    newBones.Add(finded);
    }
    else
    {
    Debug.LogError("Can not find bone " + b1.name);
    }
    }
    mats.AddRange(smr.sharedMaterials);

    for (int sub = 0; sub < smr.sharedMesh.subMeshCount; ++sub)
    {
    var ci = new CombineInstance();
    ci.mesh = smr.sharedMesh;
    ci.subMeshIndex = sub;
    ci.transform = smr.transform.localToWorldMatrix;
    combineInstances.Add(ci);
    }
    }
    }

    var humanSMR = human.AddComponent<SkinnedMeshRenderer>();
    humanSMR.materials = mats.ToArray();
    humanSMR.sharedMesh = new Mesh();
    humanSMR.sharedMesh.CombineMeshes(combineInstances.ToArray(), false, false);
    humanSMR.sharedMesh.Optimize();
    humanSMR.bones = newBones.ToArray();
    }

    void addPart3()
    {
    foreach (var go in models)
    {
    var smrs = go.GetComponentsInChildren<SkinnedMeshRenderer>();
    foreach (var smr in smrs)
    {
    var originSmr = GameObject.Instantiate<SkinnedMeshRenderer>(smr);
    var bones = getAllChildTransform(human.transform);


    originSmr.transform.parent = human.transform;
    originSmr.transform.localPosition = originSmr.transform.localPosition;
    originSmr.transform.localScale = originSmr.transform.localScale;
    originSmr.transform.localRotation = originSmr.transform.localRotation;

    List<Transform> newBones = new List<Transform>();
    foreach (var b1 in originSmr.bones)
    {
    Transform finded = null;
    foreach (var b in bones)
    if (b1.name == b.name)
    {
    finded = b;
    break;
    }
    if (finded != null)
    {
    newBones.Add(finded);
    }
    else
    {
    Debug.LogError("Can not find bone " + b1.name);
    }
    }
    originSmr.bones = newBones.ToArray();
    }
    }
    }

    Transform[] getAllChildTransform(Transform t)
    {
    List<Transform> ts = new List<Transform>();
    _getAllChildTransform(t, ts);
    return ts.ToArray();
    }

    void _getAllChildTransform(Transform t, List<Transform> ts)
    {
    for (int i = 0; i < t.childCount; ++i)
    {
    ts.Add(t.GetChild(i));
    _getAllChildTransform(t.GetChild(i), ts);
    }
    }
    }
    文章目录
    1. 1. FAQ
      1. 1.0.0.1. 1.合并网格后,BlendShape会丢失,组合网格,BlendShape不会丢失,但BlendShape动画会失效。
      2. 1.0.0.2. 2.合并网格时,如果SkinnedMeshRenderer里的Bone有些找不到,则会出现合并后看不到模型的情况。
      3. 1.0.0.3. 3.对MeshBaker 合并的网格进行烘焙,光照图出现黑斑,如图
  • 2. 合并网格代码