private void AddMeshVertex2Generate(SkinnedMeshRenderer[] meshRender,
                                            Transform[] boneTransform,
                                            Matrix4x4[] bindPose)
        {
            boneCount          = boneTransform.Length;
            textureBlockWidth  = 4;
            textureBlockHeight = boneCount;
            for (int i = 0; i != meshRender.Length; ++i)
            {
                Mesh m = meshRender[i].sharedMesh;
                if (m == null)
                {
                    continue;
                }

                int nameCode = meshRender[i].name.GetHashCode();
                if (generateVertexCachePool.ContainsKey(nameCode))
                {
                    continue;
                }

                AnimationInstancingMgr.VertexCache vertexCache = new AnimationInstancingMgr.VertexCache();
                generateVertexCachePool[nameCode] = vertexCache;
                vertexCache.nameCode = AnimationInstancingMgr.getVertexName(meshRender[i]);                 // nameCode;
                vertexCache.bonePose = boneTransform;
                vertexCache.bindPose = bindPose;
                break;
            }
        }
        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();
        }
Exemple #3
0
 private void RefreshAttachmentAnimation(int index)
 {
     for (int k = 0; k != listAttachment.Count; ++k)
     {
         AnimationInstancing attachment = listAttachment[k];
         for (int i = 0; i != attachment.lodInfo.Length; ++i)
         {
             LodInfo info = attachment.lodInfo[i];
             for (int j = 0; j != info.meshRenderer.Length; ++j)
             {
                 //MeshRenderer render = info.meshRenderer[j];
                 AnimationInstancingMgr.VertexCache cache = info.vertexCacheList[info.skinnedMeshRenderer.Length + j];
                 cache.boneTextureIndex = index;
             }
         }
     }
 }
Exemple #4
0
        public void Attach(string boneName, AnimationInstancing attachment)
        {
            int index    = -1;
            int hashBone = boneName.GetHashCode();

            for (int i = 0; i != allTransforms.Length; ++i)
            {
                if (allTransforms[i].name.GetHashCode() == hashBone)
                {
                    index = i;
                    break;
                }
            }
            //Debug.Assert(index >= 0);
            if (index < 0)
            {
                Debug.LogError("Can't find the bone.");
                return;
            }
            if (attachment.lodInfo[0].meshRenderer.Length == 0 && attachment.lodInfo[0].skinnedMeshRenderer.Length == 0)
            {
                Debug.LogError("The attachment doesn't have a Renderer");
                return;
            }

            attachment.parentInstance = this;
            AnimationInstancingMgr.VertexCache parentCache = AnimationInstancingMgr.Instance.FindVertexCache(lodInfo[0].skinnedMeshRenderer[0].name.GetHashCode());
            listAttachment.Add(attachment);

            int nameCode = boneName.GetHashCode();

            nameCode += attachment.lodInfo[0].meshRenderer.Length > 0? attachment.lodInfo[0].meshRenderer[0].name.GetHashCode(): 0;
            if (attachment.lodInfo[0].meshRenderer.Length == 0)
            {
                //todo, to support the attachment that has skinnedMeshRenderer;
                int skinnedMeshRenderCount = attachment.lodInfo[0].skinnedMeshRenderer.Length;
                nameCode += skinnedMeshRenderCount > 0? attachment.lodInfo[0].skinnedMeshRenderer[0].name.GetHashCode(): 0;
            }
            AnimationInstancingMgr.VertexCache cache = AnimationInstancingMgr.Instance.FindVertexCache(nameCode);
            // if we can reuse the VertexCache, we don't need to create one
            if (cache != null)
            {
                cache.boneTextureIndex = parentCache.boneTextureIndex;
                return;
            }

            AnimationInstancingMgr.Instance.AddMeshVertex(attachment.prototype.name,
                                                          attachment.lodInfo,
                                                          null,
                                                          null,
                                                          attachment.bonePerVertex,
                                                          boneName);

            for (int i = 0; i != attachment.lodInfo.Length; ++i)
            {
                LodInfo info = attachment.lodInfo[i];
                for (int j = 0; j != info.meshRenderer.Length; ++j)
                {
                    cache = info.vertexCacheList[info.skinnedMeshRenderer.Length + j];
                    //Debug.Assert(cache != null);
                    if (cache == null)
                    {
                        Debug.LogError("Can't find the VertexCache.");
                        continue;
                    }
                    //Debug.Assert(cache.boneTextureIndex < 0 || cache.boneIndex[0].x != index);

                    AnimationInstancingMgr.Instance.BindAttachment(parentCache, cache, info.meshFilter[j].sharedMesh, index);
                    AnimationInstancingMgr.Instance.SetupAdditionalData(cache);
                    cache.boneTextureIndex = parentCache.boneTextureIndex;
                }
            }
        }
Exemple #5
0
        public void Attach(string boneName, AnimationInstancing attachment)
        {
            int index    = -1;
            int hashBone = boneName.GetHashCode();

            for (int i = 0; i != allTransforms.Length; ++i)
            {
                if (allTransforms[i].name.GetHashCode() == hashBone)
                {
                    index = i;
                    break;
                }
            }
            Debug.Assert(index >= 0);
            if (index < 0)
            {
                Debug.LogError("Can't find the bone.");
                return;
            }

            attachment.parentInstance = this;

//			string vertexName = lodInfo[0].skinnedMeshRenderer[0].name.GetHashCode() +"_" + lodInfo[0].skinnedMeshRenderer[0].sharedMaterial.name.GetHashCode() + "_0";
            AnimationInstancingMgr.VertexCache parentCache = AnimationInstancingMgr.Instance.FindVertexCache(AnimationInstancingMgr.getVertexName(lodInfo[0].skinnedMeshRenderer[0]));
            listAttachment.Add(attachment);

            int skinnedMeshRenderCount = attachment.lodInfo[0].skinnedMeshRenderer.Length;
//			int nameCode = attachment.lodInfo[0].meshRenderer[0].name.GetHashCode() + boneName.GetHashCode();
            string nameCode = AnimationInstancingMgr.getVertexName(attachment.lodInfo[0].meshRenderer[0]);

            AnimationInstancingMgr.VertexCache cache = AnimationInstancingMgr.Instance.FindVertexCache(nameCode);
            // if we can reuse the VertexCache, we don't need to create one
            if (cache != null && cache.boneTextureIndex >= 0 &&
                cache.boneTextureIndex == parentCache.boneTextureIndex &&
                cache.boneIndex[0].x == index)
            {
                for (int i = 0; i != attachment.lodInfo.Length; ++i)
                {
                    LodInfo info = attachment.lodInfo[i];
                    for (int j = 0; j != info.meshRenderer.Length; ++j)
                    {
                        nameCode = AnimationInstancingMgr.getVertexName(info.meshRenderer[j], boneName.GetHashCode());
                        cache    = AnimationInstancingMgr.Instance.FindVertexCache(nameCode);
                        info.vertexCacheList[info.skinnedMeshRenderer.Length + j] = cache;
                    }
                }
                return;
            }

            AnimationInstancingMgr.Instance.AddMeshVertex(attachment,
                                                          attachment.lodInfo,
                                                          null,
                                                          null,
                                                          attachment.bonePerVertex,
                                                          boneName);

            for (int i = 0; i != attachment.lodInfo.Length; ++i)
            {
                LodInfo info = attachment.lodInfo[i];
                for (int j = 0; j != info.meshRenderer.Length; ++j)
                {
                    cache = info.vertexCacheList[info.skinnedMeshRenderer.Length + j];
                    Debug.Assert(cache != null);
                    if (cache == null)
                    {
                        Debug.LogError("Can't find the VertexCache.");
                        continue;
                    }
                    Debug.Assert(cache.boneTextureIndex < 0 || cache.boneIndex[0].x != index);

                    AnimationInstancingMgr.Instance.BindAttachment(parentCache, info.meshFilter[j].sharedMesh, index);
                    AnimationInstancingMgr.Instance.SetupAdditionalData(cache);
                    cache.boneTextureIndex = parentCache.boneTextureIndex;
                }
            }
        }