Example #1
0
        void InitBones()
        {
            BoneInfo[]       boneInfos = SharedData.skinningData.boneInfos;
            List <Transform> allTs     = new List <Transform>();

            GPUAnimUtils.GetAllChildren(srcBoneRoot, allTs);

            trDatas = new TRData[boneInfos.Length];
            for (int i = 0; i < trDatas.Length; i++)
            {
                TRData trData = new TRData();
                trData.name        = boneInfos[i].name;
                trData.bindPoseInv = boneInfos[i].bindPose.inverse;
                foreach (var t in allTs)
                {
                    if (t.name == trData.name)
                    {
                        trData.oriT = t;
                        break;
                    }
                }

                trDatas[i] = trData;
            }

            _pixelPerFrame = boneInfos.Length * 3;
        }
Example #2
0
        /// <summary>
        /// 创建 GPUSkin 所需的 Mesh, 强制每个顶点只有两根骨骼
        ///
        /// 由于 Unity 没有开放 BLENDINDICES 和 BLENDWEIGHT 语义,我们又不想修改资源内的原始mesh,只能自己创建一个 mesh 来存储,
        /// 缺点就是每个顶点多出了 4 * 4 个字节的体积, 假设每个模型 4000 个顶点,共缓存了 30 套模型,那么将多出
        /// 16 * 4000 * 30 = 1920000 = 1.83MB, 可以接受
        /// </summary>
        /// <param name="smr"></param>
        /// <returns></returns>
        private void CreateSkinMesh(SkinnedMeshRenderer smr, GPURendererRes res)
        {
            int[] boneIdxMap = GPUAnimUtils.CalcBoneIdxMap(smr, res.skinningData);

            Mesh smrMesh = smr.sharedMesh;

            Mesh addMesh = new Mesh();

            BoneWeight[]   oriBoneWeights = smrMesh.boneWeights;
            int            weightCount    = oriBoneWeights.Length;
            List <Vector4> boneIndices    = new List <Vector4>(weightCount);
            List <Vector4> boneWeights    = new List <Vector4>(weightCount);

            for (int i = 0; i < weightCount; i++)
            {
                BoneWeight weight  = oriBoneWeights[i];
                Vector4    indices = new Vector4();
                indices.x = boneIdxMap[weight.boneIndex0]; // 骨骼索引重新映射下
                indices.y = boneIdxMap[weight.boneIndex1];
                indices.z = boneIdxMap[weight.boneIndex2];
                indices.w = boneIdxMap[weight.boneIndex3];
                boneIndices.Add(indices);

                Vector4 weights = new Vector4();
                weights.x = weight.weight0;
                weights.y = weight.weight1;
                weights.z = weight.weight2;
                weights.w = weight.weight3;
                boneWeights.Add(weights);

                //float sum = weight.weight0 + weight.weight1;
                //blendWeights[i].x = weight.weight0 / sum;
                //blendWeights[i].y = weight.weight1 /sum;
            }

            addMesh.vertices = smrMesh.vertices; // 由于 Unity 有判断要求其它 channel 长度必须与 vertices 相等,这个内存只能浪费掉了
            addMesh.SetUVs(2, boneIndices);
            addMesh.SetUVs(3, boneWeights);
            //addMesh.uv3      = blendIndices;
            //addMesh.uv4      = blendWeights;
            addMesh.UploadMeshData(true); // warning!, DeviceLost 时可能无法恢复数据

            res.additionalMesh = addMesh;
        }
Example #3
0
 public static bool operator !=(RotationKeyFrame lhs, RotationKeyFrame rhs)
 {
     return(!GPUAnimUtils.IsQuaternionEqual(lhs.value, rhs.value) ||
            !GPUAnimUtils.IsQuaternionEqual(lhs.inSlope, rhs.inSlope) ||
            !GPUAnimUtils.IsQuaternionEqual(lhs.outSlope, rhs.outSlope));
 }
Example #4
0
 public static bool operator !=(Vector3KeyFrame lhs, Vector3KeyFrame rhs)
 {
     return(!GPUAnimUtils.IsVector3Equal(lhs.value, rhs.value) ||
            !GPUAnimUtils.IsVector3Equal(lhs.inSlope, rhs.inSlope) ||
            !GPUAnimUtils.IsVector3Equal(lhs.outSlope, rhs.outSlope));
 }
Example #5
0
        /// <summary>
        /// 将除 mesh 节点,挂点(将其提到最顶级),rootMotion 之外的所有节点移除
        /// </summary>
        private void ProcessNode()
        {
            _rootMotionNode = transform.Find(Consts.ROOT_MOTION_NAME);

            List <Transform> allChildren = new List <Transform>();

            GPUAnimUtils.GetAllChildren(transform, allChildren);

            List <SkinnedMeshRenderer> smrs = new List <SkinnedMeshRenderer>();

            foreach (var node in allChildren)
            {
                if (node == transform)
                {
                    continue;
                }

                SkinnedMeshRenderer smr = node.GetComponent <SkinnedMeshRenderer>();
                if (smr != null)
                {
                    /*
                     *  调试代码,发布时把 if 去掉
                     *  (挂载的这个武器并不能使用模型的 Animation, 并且其 Bone 数量也为0,因此应该是用 MeshRenderer 而不是 SkinnedMeshRenderer)
                     */
                    if (node.name != "right_weapon")
                    {
                        // 某些 smr 不位于模型直接子节点,将其提升以避免被移除掉(显示不会受到影响)
                        if (node.parent != transform)
                        {
                            node.parent = transform;
                        }
                        smrs.Add(smr);
                        continue;
                    }
                }

                if (node == _rootMotionNode)
                {
                    continue;
                }

                bool isJoint = false;
                //foreach (var name in skinningData.jointNames)
                //{
                //    if (name == node.name)
                //    {
                //        node.parent = transform;
                //        isJoint = true;
                //        break;
                //    }
                //}

                if (!isJoint)
                {
                    // 不要使用 DestroyImmediate, 否则循环中的其它元素可能无法访问并且下面的 bsmr.Init 无法获取到 bones
                    Destroy(node.gameObject);
                }
            }

            // 原始的 SkinnedMeshRenderer 都创建对应的两种 Renderer
            foreach (var smr in smrs)
            {
                GPURendererRes res = GPUSkinRuntimeResMgr.Instance.GetOrCreateRes(smr, skinningData);
                _bakedGPUAnimation.AddMeshRenderer(res);
                _GPUAnimation.AddMeshRenderer(res);
                DestroyImmediate(smr);
            }

            Animation oriAnimation = gameObject.GetComponent <Animation>();

            if (oriAnimation != null)
            {
                DestroyImmediate(oriAnimation);
            }

            // 初始化绑点
            //_jointTrans = new Transform[skinningData.jointNames.Length];
            //for (int i = 0; i < _jointTrans.Length; i++)
            //{
            //    Transform t = transform.Find(skinningData.jointNames[i]);
            //    if (t == null)
            //    {
            //        Debug.LogErrorFormat("can not find join {0}", skinningData.jointNames[i]);
            //        return;
            //    }

            //    _jointTrans[i] = t;
            //}

            _bakedGPUAnimation.SetJointTransforms(_jointTrans);
            _GPUAnimation.SetJointTransforms(_jointTrans);
        }