private void GenerateBoneMatrix(int nameCode,
                                        int stateName,
                                        float stateTime,
                                        Matrix4x4 rootMatrix1stFrame,
                                        bool rootMotion)
        {
            UnityEngine.Profiling.Profiler.BeginSample("AddBoneMatrix()");
            AnimationInstancingMgr.VertexCache vertexCache = null;
            bool find = generateVertexCachePool.TryGetValue(nameCode, out vertexCache);

            if (!find)
            {
                return;
            }

            GenerateOjbectInfo matrixData = generateObjectData[currentDataIndex++];

            matrixData.nameCode      = nameCode;
            matrixData.stateName     = stateName;
            matrixData.animationTime = stateTime;
            matrixData.worldMatrix   = Matrix4x4.identity;
            matrixData.frameIndex    = -1;
            matrixData.boneListIndex = -1;

            UnityEngine.Profiling.Profiler.BeginSample("AddBoneMatrix:update the matrix");
            if (generateMatrixDataPool.ContainsKey(stateName))
            {
                ArrayList list = generateMatrixDataPool[stateName];
                matrixData.boneMatrix = UtilityHelper.CalculateSkinMatrix(
                    vertexCache.bonePose,
                    vertexCache.bindPose,
                    rootMatrix1stFrame,
                    rootMotion);

                GenerateOjbectInfo data = new GenerateOjbectInfo();
                UtilityHelper.CopyMatrixData(data, matrixData);
                list.Add(data);
            }
            else
            {
                UnityEngine.Profiling.Profiler.BeginSample("AddBoneMatrix:ContainsKey");
                matrixData.boneMatrix = UtilityHelper.CalculateSkinMatrix(
                    vertexCache.bonePose,
                    vertexCache.bindPose,
                    rootMatrix1stFrame,
                    rootMotion);

                ArrayList          list = new ArrayList();
                GenerateOjbectInfo data = new GenerateOjbectInfo();
                UtilityHelper.CopyMatrixData(data, matrixData);
                list.Add(data);
                generateMatrixDataPool[stateName] = list;

                UnityEngine.Profiling.Profiler.EndSample();
            }
            UnityEngine.Profiling.Profiler.EndSample();

            UnityEngine.Profiling.Profiler.EndSample();
        }
Пример #2
0
 public static void CopyMatrixData(GenerateOjbectInfo dst, GenerateOjbectInfo src)
 {
     dst.animationTime = src.animationTime;
     dst.boneListIndex = src.boneListIndex;
     dst.frameIndex    = src.frameIndex;
     dst.nameCode      = src.nameCode;
     dst.stateName     = src.stateName;
     dst.worldMatrix   = src.worldMatrix;
     dst.boneMatrix    = src.boneMatrix;
 }
 // Use this for initialization
 private void OnEnable()
 {
     generateInfo            = new List <AnimationBakeInfo>();
     cacheTransition         = new Dictionary <UnityEditor.Animations.AnimatorState, UnityEditor.Animations.AnimatorStateTransition[]>();
     cacheAnimationEvent     = new Dictionary <AnimationClip, UnityEngine.AnimationEvent[]>();
     generatedPrefab         = null;
     generateVertexCachePool = new Dictionary <int, AnimationInstancingMgr.VertexCache>();
     generateMatrixDataPool  = new Dictionary <int, ArrayList>();
     generateObjectData      = new GenerateOjbectInfo[BakeFrameCount];
     for (int i = 0; i != generateObjectData.Length; ++i)
     {
         generateObjectData[i] = new GenerateOjbectInfo();
     }
 }
        public void SetupAnimationTexture(ArrayList infoList)
        {
            int preNameCode = generateObjectData[0].stateName;

            //int preTextureIndex = m_bakedTextureIndex;
            for (int i = 0; i != currentDataIndex; ++i)
            {
                GenerateOjbectInfo matrixData = generateObjectData[i];
                if (matrixData.boneMatrix == null)
                {
                    continue;
                }
                if (preNameCode != matrixData.stateName)
                {
                    preNameCode = matrixData.stateName;
                    int totalFrames = currentDataIndex - i;
                    for (int j = i; j != currentDataIndex; ++j)
                    {
                        if (preNameCode != generateObjectData[j].stateName)
                        {
                            totalFrames = j - i;
                            break;
                        }
                    }

                    int width  = bakedBoneTexture[bakedTextureIndex].width;
                    int height = bakedBoneTexture[bakedTextureIndex].height;
                    int y      = pixely;
                    int currentLineBlockCount = (width - pixelx) / textureBlockWidth % (width / textureBlockWidth);
                    totalFrames -= currentLineBlockCount;
                    if (totalFrames > 0)
                    {
                        int framesEachLine = width / textureBlockWidth;
                        y += (totalFrames / framesEachLine) * textureBlockHeight;
                        y += currentLineBlockCount > 0 ? textureBlockHeight : 0;
                        if (height < y + textureBlockHeight)
                        {
                            ++bakedTextureIndex;
                            pixelx = 0;
                            pixely = 0;
                            Debug.Assert(bakedTextureIndex < bakedBoneTexture.Length);
                        }
                    }

                    foreach (var obj in infoList)
                    {
                        AnimationInfo info = obj as AnimationInfo;
                        if (info.animationNameHash == matrixData.stateName)
                        {
                            info.animationIndex = pixelx / textureBlockWidth + pixely / textureBlockHeight * bakedBoneTexture[bakedTextureIndex].width / textureBlockWidth;
                            info.textureIndex   = bakedTextureIndex;
                        }
                    }
                }
                if (matrixData.boneMatrix != null)
                {
                    Debug.Assert(pixely + textureBlockHeight <= bakedBoneTexture[bakedTextureIndex].height);
                    Color[] color = UtilityHelper.Convert2Color(matrixData.boneMatrix, boneCount);
                    bakedBoneTexture[bakedTextureIndex].SetPixels(pixelx, pixely, textureBlockWidth, textureBlockHeight, color);
                    matrixData.frameIndex = pixelx / textureBlockWidth + pixely / textureBlockHeight * bakedBoneTexture[bakedTextureIndex].width / textureBlockWidth;
                    pixelx += textureBlockWidth;
                    if (pixelx + textureBlockWidth > bakedBoneTexture[bakedTextureIndex].width)
                    {
                        pixelx  = 0;
                        pixely += textureBlockHeight;
                    }
                    if (pixely + textureBlockHeight > bakedBoneTexture[bakedTextureIndex].height)
                    {
                        Debug.Assert(generateObjectData[i + 1].stateName != matrixData.stateName);
                        ++bakedTextureIndex;
                        pixelx = 0;
                        pixely = 0;
                        Debug.Assert(bakedTextureIndex < bakedBoneTexture.Length);
                    }
                }
                else
                {
                    Debug.Assert(false);
                    ArrayList          list         = generateMatrixDataPool[matrixData.stateName];
                    GenerateOjbectInfo originalData = list[matrixData.boneListIndex] as GenerateOjbectInfo;
                    matrixData.frameIndex = originalData.frameIndex;
                }
            }
            currentDataIndex = 0;
        }