示例#1
0
        private void GenerateBoneMatrix(int nameCode,
                                        int stateName,
                                        float stateTime,
                                        Matrix4x4 rootMatrix1stFrame,
                                        bool rootMotion)
        {
            UnityEngine.Profiling.Profiler.BeginSample("AddBoneMatrix()");
            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;
 }
示例#3
0
        private void OnEnable()
        {
            layerInfos      = new List <AnimationLayerInfo>();
            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, VertexCache>();
            generateMatrixDataPool  = new Dictionary <int, ArrayList>();
            generateObjectData      = new GenerateOjbectInfo[BakeFrameCount];
            for (int i = 0; i != generateObjectData.Length; ++i)
            {
                generateObjectData[i] = new GenerateOjbectInfo();
            }
            EditorApplication.update += GenerateAnimation;
        }
示例#4
0
        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);
                    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;
        }