예제 #1
0
        void ApplyBlendShapeFramesToMeshAndBuildMap()
        {
            if (MBVersion.GetMajorVersion() > 5 ||
                (MBVersion.GetMajorVersion() == 5 && MBVersion.GetMinorVersion() >= 3))
            {
                if (blendShapesInCombined.Length != blendShapes.Length)
                {
                    blendShapesInCombined = new MBBlendShape[blendShapes.Length];
                }
                Vector3[] targVerts = new UnityEngine.Vector3[verts.Length];
                Vector3[] targNorms = new UnityEngine.Vector3[verts.Length];
                Vector3[] targTans  = new UnityEngine.Vector3[verts.Length];

                ((SkinnedMeshRenderer)_targetRenderer).sharedMesh = null;

                MBVersion.ClearBlendShapes(_mesh);
                for (int bsIdx = 0; bsIdx < blendShapes.Length; bsIdx++)
                {
                    MBBlendShape         blendShape = blendShapes[bsIdx];
                    MB_DynamicGameObject dgo        = instance2Combined_MapGet(blendShape.gameObject);
                    if (dgo != null)
                    {
                        int destIdx = dgo.vertIdx;
                        for (int frmIdx = 0; frmIdx < blendShape.frames.Length; frmIdx++)
                        {
                            MBBlendShapeFrame frame = blendShape.frames[frmIdx];
                            Array.Copy(frame.vertices, 0, targVerts, destIdx, frame.vertices.Length);
                            Array.Copy(frame.normals, 0, targNorms, destIdx, frame.normals.Length);
                            Array.Copy(frame.tangents, 0, targTans, destIdx, frame.tangents.Length);
                            MBVersion.AddBlendShapeFrame(_mesh, ConvertBlendShapeNameToOutputName(blendShape.name) + blendShape.gameObjectID, frame.frameWeight, targVerts, targNorms, targTans);
                            // We re-use these arrays restore them to zero
                            _ZeroArray(targVerts, destIdx, frame.vertices.Length);
                            _ZeroArray(targNorms, destIdx, frame.normals.Length);
                            _ZeroArray(targTans, destIdx, frame.tangents.Length);
                        }
                    }
                    else
                    {
                        Debug.LogError("InstanceID in blend shape that was not in instance2combinedMap");
                    }
                    blendShapesInCombined[bsIdx] = blendShape;
                }

                //this is necessary to get the renderer to refresh its data about the blendshapes.
                ((SkinnedMeshRenderer)_targetRenderer).sharedMesh = null;
                ((SkinnedMeshRenderer)_targetRenderer).sharedMesh = _mesh;

                // Add the map to the target renderer.
                if (settings.doBlendShapes)
                {
                    MB_BlendShape2CombinedMap mapComponent = _targetRenderer.GetComponent <MB_BlendShape2CombinedMap>();
                    if (mapComponent == null)
                    {
                        mapComponent = _targetRenderer.gameObject.AddComponent <MB_BlendShape2CombinedMap>();
                    }
                    SerializableSourceBlendShape2Combined map = mapComponent.GetMap();
                    BuildSrcShape2CombinedMap(map, blendShapes);
                }
            }
        }
예제 #2
0
        void ApplyBlendShapeFramesToMeshAndBuildMap_MergeBlendShapesWithTheSameName()
        {
            if (MBVersion.GetMajorVersion() > 5 ||
                (MBVersion.GetMajorVersion() == 5 && MBVersion.GetMinorVersion() >= 3))
            {
                Vector3[] targVerts = new UnityEngine.Vector3[verts.Length];
                Vector3[] targNorms = new UnityEngine.Vector3[verts.Length];
                Vector3[] targTans  = new UnityEngine.Vector3[verts.Length];

                MBVersion.ClearBlendShapes(_mesh);

                // Group source that share the same blendShapeName
                bool numFramesError = false;
                Dictionary <string, List <MBBlendShape> > shapeName2objs = new Dictionary <string, List <MBBlendShape> >();
                {
                    for (int i = 0; i < blendShapes.Length; i++)
                    {
                        MBBlendShape        blendShape     = blendShapes[i];
                        string              blendShapeName = ConvertBlendShapeNameToOutputName(blendShape.name);
                        List <MBBlendShape> dgosUsingBlendShape;
                        if (!shapeName2objs.TryGetValue(blendShapeName, out dgosUsingBlendShape))
                        {
                            dgosUsingBlendShape = new List <MBBlendShape>();
                            shapeName2objs.Add(blendShapeName, dgosUsingBlendShape);
                        }

                        dgosUsingBlendShape.Add(blendShape);
                        if (dgosUsingBlendShape.Count > 1)
                        {
                            if (dgosUsingBlendShape[0].frames.Length != blendShape.frames.Length)
                            {
                                Debug.LogError("BlendShapes with the same name must have the same number of frames.");
                                numFramesError = true;
                            }
                        }
                    }
                }

                if (numFramesError)
                {
                    return;
                }

                if (blendShapesInCombined.Length != blendShapes.Length)
                {
                    blendShapesInCombined = new MBBlendShape[shapeName2objs.Keys.Count];
                }

                int bsInCombinedIdx = 0;
                foreach (string shapeName in shapeName2objs.Keys)
                {
                    List <MBBlendShape> groupOfSrcObjs  = shapeName2objs[shapeName];
                    MBBlendShape        firstBlendShape = groupOfSrcObjs[0];
                    int    numFrames        = firstBlendShape.frames.Length;
                    int    db_numVertsAdded = 0;
                    int    db_numObjsAdded  = 0;
                    string db_vIdx          = "";

                    for (int frmIdx = 0; frmIdx < numFrames; frmIdx++)
                    {
                        float firstFrameWeight = firstBlendShape.frames[frmIdx].frameWeight;

                        for (int dgoIdx = 0; dgoIdx < groupOfSrcObjs.Count; dgoIdx++)
                        {
                            MBBlendShape         blendShape = groupOfSrcObjs[dgoIdx];
                            MB_DynamicGameObject dgo        = instance2Combined_MapGet(blendShape.gameObject);
                            int destIdx = dgo.vertIdx;
                            Debug.Assert(blendShape.frames.Length == numFrames);
                            MBBlendShapeFrame frame = blendShape.frames[frmIdx];
                            Debug.Assert(frame.frameWeight == firstFrameWeight);
                            Array.Copy(frame.vertices, 0, targVerts, destIdx, frame.vertices.Length);
                            Array.Copy(frame.normals, 0, targNorms, destIdx, frame.normals.Length);
                            Array.Copy(frame.tangents, 0, targTans, destIdx, frame.tangents.Length);
                            if (frmIdx == 0)
                            {
                                db_numVertsAdded += frame.vertices.Length;
                                db_vIdx          += blendShape.gameObject.name + " " + destIdx + ":" + (destIdx + frame.vertices.Length) + ", ";
                            }
                        }

                        db_numObjsAdded += groupOfSrcObjs.Count;
                        MBVersion.AddBlendShapeFrame(_mesh, shapeName, firstFrameWeight, targVerts, targNorms, targTans);

                        // We re-use these arrays restore them to zero
                        _ZeroArray(targVerts, 0, targVerts.Length);
                        _ZeroArray(targNorms, 0, targNorms.Length);
                        _ZeroArray(targTans, 0, targTans.Length);
                    }

                    blendShapesInCombined[bsInCombinedIdx] = firstBlendShape;
                    bsInCombinedIdx++;
                }



                //this is necessary to get the renderer to refresh its data about the blendshapes.
                ((SkinnedMeshRenderer)_targetRenderer).sharedMesh = null;
                ((SkinnedMeshRenderer)_targetRenderer).sharedMesh = _mesh;

                // Add the map to the target renderer.
                if (settings.doBlendShapes)
                {
                    MB_BlendShape2CombinedMap mapComponent = _targetRenderer.GetComponent <MB_BlendShape2CombinedMap>();
                    if (mapComponent == null)
                    {
                        mapComponent = _targetRenderer.gameObject.AddComponent <MB_BlendShape2CombinedMap>();
                    }
                    SerializableSourceBlendShape2Combined map = mapComponent.GetMap();
                    BuildSrcShape2CombinedMap(map, blendShapesInCombined);
                }
            }
        }