private void ComputeMotionsBestCost() { float bestMotionCost = float.MaxValue; bestMotionName = ""; bestMotionFrameIndex = 0; for (int i = 0; i < motionsData.motionDataList.Length; i++) { MotionData motionData = motionsData.motionDataList[i]; if (motionMatcherSettings.EnableDebugText) { AddDebugContent("motion: " + motionData.motionName); } for (int j = 0; j < motionData.motionFrameDataList.Length; j++) { MotionDebugData motionDebugData = new MotionDebugData(); float motionCost = 0f; float bonesCost = 0f; float bonePosCost = 0f; float boneRotCost = 0f; float trajectoryPosCost = 0f; float trajectoryVelCost = 0f; float trajectoryDirCost = 0f; float trajectorysCost = 0f; float rootMotionCost = 0f; MotionFrameData motionFrameData = motionData.motionFrameDataList[j]; for (int k = 0; k < motionFrameData.motionBoneDataList.Length; k++) { MotionBoneData motionBoneData = motionFrameData.motionBoneDataList[k]; MotionBoneData currentMotionBoneData = currentMotionFrameData.motionBoneDataList[k]; float BonePosCost = Vector3.SqrMagnitude(motionBoneData.localPosition - currentMotionBoneData.localPosition); Quaternion BonePosError = Quaternion.Inverse(motionBoneData.localRotation) * currentMotionBoneData.localRotation; float BoneRotCost = Mathf.Abs(BonePosError.x) + Mathf.Abs(BonePosError.y) + Mathf.Abs(BonePosError.z) + (1 - Mathf.Abs(BonePosError.w)); //float BoneVelocityCost = Vector3.SqrMagnitude(motionBoneData.velocity - currentMotionBoneData.velocity); bonePosCost += BonePosCost * motionCostFactorSettings.bonePosFactor; boneRotCost += BoneRotCost * motionCostFactorSettings.boneRotFactor /* + BoneVelocityCost * motionCostFactorSettings.boneVelFactor*/; //AddDebugContent("BonePosCost: " + BonePosCost); //AddDebugContent("BoneRotCost: " + BoneRotCost); //AddDebugContent("BoneVelocityCost: " + BoneVelocityCost); } bonesCost = bonePosCost + boneRotCost; motionDebugData.bonePosCost = bonePosCost; motionDebugData.boneRotCost = boneRotCost; motionDebugData.bonesCost = bonesCost; if (motionMatcherSettings.EnableDebugText) { AddDebugContent("bonesTotalCost: " + bonesCost); } for (int l = 0; l < motionFrameData.motionTrajectoryDataList.Length; l++) { MotionTrajectoryData motionTrajectoryData = motionFrameData.motionTrajectoryDataList[l]; MotionTrajectoryData currentMotionTrajectoryData = currentMotionFrameData.motionTrajectoryDataList[l]; trajectoryPosCost += Vector3.SqrMagnitude(motionTrajectoryData.localPosition - currentMotionTrajectoryData.localPosition) * motionCostFactorSettings.predictionTrajectoryPosFactor; //trajectoryVelCost += Vector3.SqrMagnitude(motionTrajectoryData.velocity - currentMotionTrajectoryData.velocity) * motionCostFactorSettings.predictionTrajectoryVelFactor; trajectoryDirCost += Vector3.Dot(motionTrajectoryData.direction, currentMotionTrajectoryData.direction) * motionCostFactorSettings.predictionTrajectoryDirFactor; //AddDebugContent("trajectoryPosCost: " + trajectoryPosCost); //AddDebugContent("trajectoryVelCost: " + trajectoryVelCost); //AddDebugContent("trajectoryDirCost: " + trajectoryDirCost); } trajectorysCost = trajectoryPosCost + trajectoryVelCost + trajectoryDirCost; motionDebugData.trajectoryPosCost = trajectoryPosCost; motionDebugData.trajectoryVelCost = trajectoryVelCost; motionDebugData.trajectoryDirCost = trajectoryDirCost; motionDebugData.trajectorysCost = trajectorysCost; if (motionMatcherSettings.EnableDebugText) { AddDebugContent("trajectorysToatalCost: " + trajectorysCost); } rootMotionCost = Mathf.Abs(motionFrameData.velocity - currentMotionFrameData.velocity) * motionCostFactorSettings.rootMotionVelFactor; motionDebugData.rootMotionCost = rootMotionCost; if (motionMatcherSettings.EnableDebugText) { AddDebugContent("rootMotionCost: " + rootMotionCost); } motionCost = bonesCost + trajectorysCost + rootMotionCost; motionDebugData.motionCost = motionCost; if (motionMatcherSettings.EnableDebugText) { AddDebugContent("motionTotalCost: " + motionCost); } //Debug.LogFormat("ComputeMotionsBestCost motionName {0} motionCost {1} ", motionData.motionName, motionCost); if (bestMotionCost > motionCost) { bestMotionCost = motionCost; bestMotionFrameIndex = j; bestMotionName = motionData.motionName; bestMotionDebugData = motionDebugData; motionDebugData.motionName = motionData.motionName; motionDebugData.motionFrameIndex = j; motionDebugDataList.Add(motionDebugData); } } } }
private void onDropdownChanged(int arg0) { costTextContainer.SetActive(true); hideCostTextContainerBtn.gameObject.SetActive(true); MotionData[] motionDataList = playerMotionMatcher.motionsData.motionDataList; MotionData motionData = motionDataList[arg0]; targetMotionFrameData = motionData.motionFrameDataList[0]; currentMotionFrameData = playerMotionMatcher.currentMotionFrameData; MotionDebugData motionDebugData = new MotionDebugData(); AddDebugContent("----------------------" + motionData.motionName); float motionCost = 0f; float bonesCost = 0f; float bonePosCost = 0f; float boneRotCost = 0f; float trajectoryPosCost = 0f; float trajectoryVelCost = 0f; float trajectoryDirCost = 0f; float trajectorysCost = 0f; float rootMotionCost = 0f; for (int k = 0; k < targetMotionFrameData.motionBoneDataList.Length; k++) { MotionBoneData motionBoneData = targetMotionFrameData.motionBoneDataList[k]; MotionBoneData currentMotionBoneData = currentMotionFrameData.motionBoneDataList[k]; float BonePosCost = Vector3.SqrMagnitude(motionBoneData.localPosition - currentMotionBoneData.localPosition); Quaternion BonePosError = Quaternion.Inverse(motionBoneData.localRotation) * currentMotionBoneData.localRotation; float BoneRotCost = Mathf.Abs(BonePosError.x) + Mathf.Abs(BonePosError.y) + Mathf.Abs(BonePosError.z) + (1 - Mathf.Abs(BonePosError.w)); //float BoneVelocityCost = Vector3.SqrMagnitude(motionBoneData.velocity - currentMotionBoneData.velocity); bonePosCost += BonePosCost * playerMotionMatcher.motionCostFactorSettings.bonePosFactor; boneRotCost += BoneRotCost * playerMotionMatcher.motionCostFactorSettings.boneRotFactor /* + BoneVelocityCost * motionCostFactorSettings.boneVelFactor*/; //AddDebugContent("BonePosCost: " + BonePosCost); //AddDebugContent("BoneRotCost: " + BoneRotCost); //AddDebugContent("BoneVelocityCost: " + BoneVelocityCost); } bonesCost = bonePosCost + boneRotCost; motionDebugData.bonePosCost = bonePosCost; motionDebugData.boneRotCost = boneRotCost; motionDebugData.bonesCost = bonesCost; AddDebugContent("bonesTotalCost: " + bonesCost); for (int l = 0; l < targetMotionFrameData.motionTrajectoryDataList.Length; l++) { MotionTrajectoryData motionTrajectoryData = targetMotionFrameData.motionTrajectoryDataList[l]; MotionTrajectoryData currentMotionTrajectoryData = currentMotionFrameData.motionTrajectoryDataList[l]; trajectoryPosCost += Vector3.SqrMagnitude(motionTrajectoryData.localPosition - currentMotionTrajectoryData.localPosition) * playerMotionMatcher.motionCostFactorSettings.predictionTrajectoryPosFactor; //trajectoryVelCost += Vector3.SqrMagnitude(motionTrajectoryData.velocity - currentMotionTrajectoryData.velocity) * motionCostFactorSettings.predictionTrajectoryVelFactor; trajectoryDirCost += Vector3.Dot(motionTrajectoryData.direction, currentMotionTrajectoryData.direction) * playerMotionMatcher.motionCostFactorSettings.predictionTrajectoryDirFactor; //AddDebugContent("trajectoryPosCost: " + trajectoryPosCost); //AddDebugContent("trajectoryVelCost: " + trajectoryVelCost); //AddDebugContent("trajectoryDirCost: " + trajectoryDirCost); } trajectorysCost = trajectoryPosCost + trajectoryVelCost + trajectoryDirCost; motionDebugData.trajectoryPosCost = trajectoryPosCost; motionDebugData.trajectoryVelCost = trajectoryVelCost; motionDebugData.trajectoryDirCost = trajectoryDirCost; motionDebugData.trajectorysCost = trajectorysCost; AddDebugContent("trajectoryPosCost: " + trajectoryPosCost); AddDebugContent("trajectoryVelCost: " + trajectoryVelCost); AddDebugContent("trajectoryDirCost: " + trajectoryDirCost); AddDebugContent("trajectorysToatalCost: " + trajectorysCost); rootMotionCost = Mathf.Abs(targetMotionFrameData.velocity - currentMotionFrameData.velocity) * playerMotionMatcher.motionCostFactorSettings.rootMotionVelFactor; motionDebugData.rootMotionCost = rootMotionCost; AddDebugContent("rootMotionCost: " + rootMotionCost); motionCost = bonesCost + trajectorysCost + rootMotionCost; motionDebugData.motionCost = motionCost; AddDebugContent("motionTotalCost: " + motionCost); }