internal Matrix4x4[] GetBindposes(Renderer r, out bool isSkinnedMeshWithBones)
            {
                MeshChannels mc;
                Mesh         m = MB_Utility.GetMesh(r.gameObject);

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }

                if (mc.bindPoses == null)
                {
                    mc.bindPoses = _getBindPoses(r, out isSkinnedMeshWithBones);
                }
                else
                {
                    if (r is SkinnedMeshRenderer &&
                        mc.bindPoses.Length > 0)
                    {
                        isSkinnedMeshWithBones = true;
                    }
                    else
                    {
                        isSkinnedMeshWithBones = false;
                        if (r is SkinnedMeshRenderer)
                        {
                            Debug.Assert(m.blendShapeCount > 0, "Skinned Mesh Renderer " + r + " had no bones and no blend shapes");
                        }
                    }
                }

                return(mc.bindPoses);
            }
 internal MBBlendShape[] GetBlendShapes(Mesh m, int gameObjectID, GameObject gameObject)
 {
     if (MBVersion.GetMajorVersion() > 5 ||
         (MBVersion.GetMajorVersion() == 5 && MBVersion.GetMinorVersion() >= 3))
     {
         MeshChannels mc;
         if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
         {
             mc = new MeshChannels();
             meshID2MeshChannels.Add(m.GetInstanceID(), mc);
         }
         if (mc.blendShapes == null)
         {
             MBBlendShape[] shapes   = new MBBlendShape[m.blendShapeCount];
             int            arrayLen = m.vertexCount;
             for (int i = 0; i < shapes.Length; i++)
             {
                 MBBlendShape shape = shapes[i] = new MBBlendShape();
                 shape.frames        = new MBBlendShapeFrame[MBVersion.GetBlendShapeFrameCount(m, i)];
                 shape.name          = m.GetBlendShapeName(i);
                 shape.indexInSource = i;
                 shape.gameObjectID  = gameObjectID;
                 shape.gameObject    = gameObject;
                 for (int j = 0; j < shape.frames.Length; j++)
                 {
                     MBBlendShapeFrame frame = shape.frames[j] = new MBBlendShapeFrame();
                     frame.frameWeight = MBVersion.GetBlendShapeFrameWeight(m, i, j);
                     frame.vertices    = new Vector3[arrayLen];
                     frame.normals     = new Vector3[arrayLen];
                     frame.tangents    = new Vector3[arrayLen];
                     MBVersion.GetBlendShapeFrameVertices(m, i, j, frame.vertices, frame.normals, frame.tangents);
                 }
             }
             mc.blendShapes = shapes;
             return(mc.blendShapes);
         }
         else
         { //copy cached blend shapes assigning a different gameObjectID
             MBBlendShape[] shapes = new MBBlendShape[mc.blendShapes.Length];
             for (int i = 0; i < shapes.Length; i++)
             {
                 shapes[i]               = new MBBlendShape();
                 shapes[i].name          = mc.blendShapes[i].name;
                 shapes[i].indexInSource = mc.blendShapes[i].indexInSource;
                 shapes[i].frames        = mc.blendShapes[i].frames;
                 shapes[i].gameObjectID  = gameObjectID;
                 shapes[i].gameObject    = gameObject;
             }
             return(shapes);
         }
     }
     else
     {
         return(new MBBlendShape[0]);
     }
 }
Beispiel #3
0
 /// <summary>
 /// Get the blend shapes from the source mesh
 /// </summary>
 public static MBBlendShape[] GetBlendShapes(Mesh m, int gameObjectID, GameObject gameObject, Dictionary <int, MeshChannels> meshID2MeshChannels)
 {
     if (MBVersion.GetMajorVersion() > 5 ||
         (MBVersion.GetMajorVersion() == 5 && MBVersion.GetMinorVersion() >= 3))
     {
         MeshChannels mc;
         if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
         {
             mc = new MeshChannels();
             meshID2MeshChannels.Add(m.GetInstanceID(), mc);
         }
         if (mc.blendShapes == null)
         {
             MBBlendShape[] shapes   = new MBBlendShape[m.blendShapeCount];
             int            arrayLen = m.vertexCount;
             for (int shapeIdx = 0; shapeIdx < shapes.Length; shapeIdx++)
             {
                 MBBlendShape shape = shapes[shapeIdx] = new MBBlendShape();
                 shape.frames        = new MBBlendShapeFrame[MBVersion.GetBlendShapeFrameCount(m, shapeIdx)];
                 shape.name          = m.GetBlendShapeName(shapeIdx);
                 shape.indexInSource = shapeIdx;
                 shape.gameObjectID  = gameObjectID;
                 shape.gameObject    = gameObject;
                 for (int frameIdx = 0; frameIdx < shape.frames.Length; frameIdx++)
                 {
                     MBBlendShapeFrame frame = shape.frames[frameIdx] = new MBBlendShapeFrame();
                     frame.frameWeight = MBVersion.GetBlendShapeFrameWeight(m, shapeIdx, frameIdx);
                     frame.vertices    = new Vector3[arrayLen];
                     frame.normals     = new Vector3[arrayLen];
                     frame.tangents    = new Vector3[arrayLen];
                     MBVersion.GetBlendShapeFrameVertices(m, shapeIdx, frameIdx, frame.vertices, frame.normals, frame.tangents);
                 }
             }
             mc.blendShapes = shapes;
             return(mc.blendShapes);
         }
         else
         { //copy cached blend shapes from same mesh assiged to a different gameObjectID
             MBBlendShape[] shapes = new MBBlendShape[mc.blendShapes.Length];
             for (int i = 0; i < shapes.Length; i++)
             {
                 shapes[i]               = new MBBlendShape();
                 shapes[i].name          = mc.blendShapes[i].name;
                 shapes[i].indexInSource = mc.blendShapes[i].indexInSource;
                 shapes[i].frames        = mc.blendShapes[i].frames;
                 shapes[i].gameObjectID  = gameObjectID;
                 shapes[i].gameObject    = gameObject;
             }
             return(shapes);
         }
     }
     else
     {
         return(new MBBlendShape[0]);
     }
 }
            internal int[] GetTriangles(Mesh m)
            {
                MeshChannels mc;

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.triangles == null)
                {
                    mc.triangles = m.triangles;
                }
                return(mc.triangles);
            }
            internal Color[] GetColors(Mesh m)
            {
                MeshChannels mc;

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.colors == null)
                {
                    mc.colors = _getMeshColors(m);
                }
                return(mc.colors);
            }
            internal Vector2[] GetUv0Raw(Mesh m)
            {
                MeshChannels mc;

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.uv0raw == null)
                {
                    mc.uv0raw = _getMeshUVs(m);
                }
                return(mc.uv0raw);
            }
            internal Vector4[] GetTangents(Mesh m)
            {
                MeshChannels mc;

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.tangents == null)
                {
                    mc.tangents = _getMeshTangents(m);
                }
                return(mc.tangents);
            }
            internal Vector3[] GetNormals(Mesh m)
            {
                MeshChannels mc;

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.normals == null)
                {
                    mc.normals = _getMeshNormals(m);
                }
                return(mc.normals);
            }
            internal Vector3[] GetVertices(Mesh m)
            {
                MeshChannels mc;

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.vertices == null)
                {
                    mc.vertices = m.vertices;
                }
                return(mc.vertices);
            }
            internal BoneWeight[] GetBoneWeights(Renderer r, int numVertsInMeshBeingAdded)
            {
                MeshChannels mc;
                Mesh         m = MB_Utility.GetMesh(r.gameObject);

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.boneWeights == null)
                {
                    mc.boneWeights = _getBoneWeights(r, numVertsInMeshBeingAdded);
                }
                return(mc.boneWeights);
            }
            internal Matrix4x4[] GetBindposes(Renderer r)
            {
                MeshChannels mc;
                Mesh         m = MB_Utility.GetMesh(r.gameObject);

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.bindPoses == null)
                {
                    mc.bindPoses = _getBindPoses(r);
                }
                return(mc.bindPoses);
            }
            internal Vector2[] GetUv0Modified(Mesh m)
            {
                MeshChannels mc;

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.uv0modified == null)
                {
                    //todo
                    mc.uv0modified = null;
                }
                return(mc.uv0modified);
            }
            internal Vector2[] GetUVChannel(int channel, Mesh m)
            {
                MeshChannels mc;

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }

                switch (channel)
                {
                case 0:
                    if (mc.uv0raw == null)
                    {
                        mc.uv0raw = GetUv0Raw(m);
                    }
                    return(mc.uv0raw);

                case 2:
                    if (mc.uv2 == null)
                    {
                        mc.uv2 = _getMeshUV2s(m);
                    }
                    return(mc.uv2);

                case 3:
                    if (mc.uv3 == null)
                    {
                        mc.uv3 = MBVersion.GetMeshChannel(channel, m, LOG_LEVEL);
                    }
                    return(mc.uv3);

                case 4:
                    if (mc.uv4 == null)
                    {
                        mc.uv4 = MBVersion.GetMeshChannel(channel, m, LOG_LEVEL);
                    }
                    return(mc.uv4);

                case 5:
                    if (mc.uv5 == null)
                    {
                        mc.uv5 = MBVersion.GetMeshChannel(channel, m, LOG_LEVEL);
                    }
                    return(mc.uv5);

                case 6:
                    if (mc.uv6 == null)
                    {
                        mc.uv6 = MBVersion.GetMeshChannel(channel, m, LOG_LEVEL);
                    }
                    return(mc.uv6);

                case 7:
                    if (mc.uv7 == null)
                    {
                        mc.uv7 = MBVersion.GetMeshChannel(channel, m, LOG_LEVEL);
                    }
                    return(mc.uv7);

                case 8:
                    if (mc.uv8 == null)
                    {
                        mc.uv8 = MBVersion.GetMeshChannel(channel, m, LOG_LEVEL);
                    }
                    return(mc.uv8);

                default:
                    Debug.LogError("Error mesh channel " + channel + " not supported");
                    break;
                }

                return(null);
            }