private void OnPreviewBTN() { previewBTNClick = true; animationState = AnimationState.Stoped; float deltaTime = -currentAnimaionTime; if (playableGraph != null && playableGraph.IsValid()) { playableGraph.ClearMainMixerInput(); playableGraph.Destroy(); } playableGraph = new PreparingDataPlayableGraph(); playableGraph.Initialize(gameObject); playableGraph.CreateAnimationDataPlayables(editedData); currentAnimaionTime = 0f; }
public static void CalculateImpactPoints( MotionMatchingData data, MotionMatchingContact[] contactPoints, PreparingDataPlayableGraph playableGraph, GameObject gameObject ) { // Normalizacja kierunków kontaktów for (int i = 0; i < data.contactPoints.Count; i++) { MotionMatchingContact cp = data.contactPoints[i]; cp.contactNormal = math.normalize(cp.contactNormal); data.contactPoints[i] = cp; } // Pobrani początkowych wartości game objectu Vector3 startPos = gameObject.transform.position; Quaternion startRot = gameObject.transform.rotation; float deltaTime = data.frameTime; Matrix4x4 frameMatrix; NeedValueToCalculateData[] recordedData = new NeedValueToCalculateData[data.numberOfFrames]; Vector3[] cpPos = new Vector3[contactPoints.Length]; Vector3[] cpNormals = new Vector3[contactPoints.Length]; Vector3[] cpForwards = new Vector3[contactPoints.Length]; if (playableGraph != null) { playableGraph.Destroy(); } playableGraph = new PreparingDataPlayableGraph(); playableGraph.Initialize(gameObject); playableGraph.CreateAnimationDataPlayables(data); // RecordingData float currentTime = 0f; float currentDeltaTime = deltaTime; int contactPointIndex = 0; for (int i = 0; i < data.numberOfFrames; i++) { recordedData[i] = new NeedValueToCalculateData( gameObject.transform.position, gameObject.transform.forward, gameObject.transform.rotation ); currentTime += deltaTime; if (contactPointIndex < contactPoints.Length && currentTime >= contactPoints[contactPointIndex].startTime) { float buforDeltaTime = currentTime - contactPoints[contactPointIndex].startTime; currentDeltaTime = deltaTime - buforDeltaTime; playableGraph.EvaluateMotionMatchgData(data, currentDeltaTime); cpPos[contactPointIndex] = gameObject.transform.TransformPoint(contactPoints[contactPointIndex].position); cpNormals[contactPointIndex] = gameObject.transform.TransformDirection(contactPoints[contactPointIndex].contactNormal); cpForwards[contactPointIndex] = gameObject.transform.forward; contactPointIndex++; playableGraph.EvaluateMotionMatchgData(data, buforDeltaTime); currentDeltaTime = deltaTime; } else { playableGraph.EvaluateMotionMatchgData(data, currentDeltaTime); } } // calcualationData for (int i = 0; i < data.numberOfFrames; i++) { frameMatrix = Matrix4x4.TRS( recordedData[i].position, recordedData[i].rotation, Vector3.one ); FrameData currentFrame = data.frames[i]; for (int impactIndex = 0; impactIndex < data.contactPoints.Count; impactIndex++) { if (data.contactPoints[impactIndex].IsContactInTime(currentFrame.localTime)) { currentFrame.contactPoints = new FrameContact[1]; Vector3 pos = frameMatrix.inverse.MultiplyPoint3x4(cpPos[impactIndex]); Vector3 norDir = frameMatrix.inverse.MultiplyVector(cpNormals[impactIndex]); Vector3 forw = frameMatrix.inverse.MultiplyVector(cpForwards[impactIndex]); FrameContact cp = new FrameContact( pos, norDir //forw ); currentFrame.contactPoints[0] = cp; break; } else { currentFrame.contactPoints = new FrameContact[0]; } } if (data.contactPoints.Count == 0) { currentFrame.contactPoints = new FrameContact[0]; } data.frames[i] = currentFrame; } gameObject.transform.position = startPos; gameObject.transform.rotation = startRot; //if (data.contactPoints.Count >= 2) //{ // Vector3 firstPoint = data.GetContactPoint(0, data.contactPoints[0].startTime).position; // Vector3 secondPoint = data.GetContactPoint(1, data.contactPoints[0].startTime).position; // Vector3 dir = secondPoint - firstPoint; // dir.y = 0; // data.fromFirstToSecondContactRot = Quaternion.FromToRotation(dir, Vector3.forward); //} //else //{ // data.fromFirstToSecondContactRot = Quaternion.identity; //} playableGraph.ClearMainMixerInput(); playableGraph.Destroy(); }
public static void CalculateContactPoints( MotionMatchingData data, MotionMatchingContact[] contactPoints, PreparingDataPlayableGraph playableGraph, GameObject gameObject ) { for (int i = 0; i < data.contactPoints.Count; i++) { MotionMatchingContact cp = data.contactPoints[i]; cp.contactNormal = math.normalize(cp.contactNormal); data.contactPoints[i] = cp; } Vector3 startPos = gameObject.transform.position; Quaternion startRot = gameObject.transform.rotation; float deltaTime = data.frameTime; Matrix4x4 frameMatrix; NeedValueToCalculateData[] recordedData = new NeedValueToCalculateData[data.numberOfFrames]; Vector3[] cpPos = new Vector3[contactPoints.Length]; Vector3[] cpNormals = new Vector3[contactPoints.Length]; Vector3[] cpForwards = new Vector3[contactPoints.Length]; if (playableGraph != null) { playableGraph.Destroy(); } playableGraph = new PreparingDataPlayableGraph(); playableGraph.Initialize(gameObject); playableGraph.CreateAnimationDataPlayables(data); // RecordingData float currentTime = 0f; float currentDeltaTime = deltaTime; int contactPointIndex = 0; for (int i = 0; i < data.numberOfFrames; i++) { recordedData[i] = new NeedValueToCalculateData( gameObject.transform.position, gameObject.transform.forward, gameObject.transform.rotation ); currentTime += deltaTime; if (contactPointIndex < contactPoints.Length && currentTime >= contactPoints[contactPointIndex].startTime) { float buforDeltaTime = currentTime - contactPoints[contactPointIndex].startTime; currentDeltaTime = deltaTime - buforDeltaTime; playableGraph.EvaluateMotionMatchgData(data, currentDeltaTime); cpPos[contactPointIndex] = gameObject.transform.TransformPoint(contactPoints[contactPointIndex].position); cpNormals[contactPointIndex] = gameObject.transform.TransformDirection(contactPoints[contactPointIndex].contactNormal); cpForwards[contactPointIndex] = gameObject.transform.forward; contactPointIndex++; playableGraph.EvaluateMotionMatchgData(data, buforDeltaTime); currentDeltaTime = deltaTime; } else { playableGraph.EvaluateMotionMatchgData(data, currentDeltaTime); } } // calcualationData for (int i = 0; i < data.numberOfFrames; i++) { frameMatrix = Matrix4x4.TRS( recordedData[i].position, recordedData[i].rotation, Vector3.one ); FrameData currentFrame = data.frames[i]; currentFrame.contactPoints = new FrameContact[cpPos.Length]; for (int j = 0; j < cpPos.Length; j++) { Vector3 pos = frameMatrix.inverse.MultiplyPoint3x4(cpPos[j]); Vector3 norDir = frameMatrix.inverse.MultiplyVector(cpNormals[j]); Vector3 forw = frameMatrix.inverse.MultiplyVector(cpForwards[j]); FrameContact cp = new FrameContact( pos, norDir //forw ); currentFrame.contactPoints[j] = cp; } data.frames[i] = currentFrame; } gameObject.transform.position = startPos; gameObject.transform.rotation = startRot; if (data.contactPoints.Count >= 2) { for (int i = 0; i < contactPoints.Length - 1; i++) { Vector3 firstPoint = data.GetContactPointInTime(i, data.contactPoints[i].startTime).position; Vector3 secondPoint = data.GetContactPointInTime(i + 1, data.contactPoints[i].startTime).position; Vector3 dir = secondPoint - firstPoint; dir.y = 0; MotionMatchingContact c = data.contactPoints[i]; c.rotationFromForwardToNextContactDir = Quaternion.FromToRotation(dir, Vector3.forward); data.contactPoints[i] = c; } } if (data.contactPoints.Count >= 2) { Vector3 firstPoint = data.GetContactPointInTime(0, data.contactPoints[0].startTime).position; Vector3 secondPoint = data.GetContactPointInTime(1, data.contactPoints[0].startTime).position; Vector3 dir = secondPoint - firstPoint; dir.y = 0; data.fromFirstToSecondContactRot = Quaternion.FromToRotation( Vector3.ProjectOnPlane(dir, Vector3.up), Vector3.forward ); } else { data.fromFirstToSecondContactRot = Quaternion.identity; } playableGraph.ClearMainMixerInput(); playableGraph.Destroy(); }
private void ContactsButtonOptions() { GUILayout.Space(5); switch (editedData.contactsType) { case ContactStateType.NormalContacts: if (GUILayout.Button("Sort contacts", GUIResources.Button_MD()) && editedData != null) { editedData.contactPoints.Sort(); } if (GUILayout.Button("Calculate Contacts", GUIResources.Button_MD()) && editedData != null && gameObject != null) { if (gameObject == null) { Debug.LogWarning("Game object in MM Data Editor is NULL!"); return; } else { editedData.contactPoints.Sort(); MotionDataCalculator.CalculateContactPoints( editedData, editedData.contactPoints.ToArray(), this.playableGraph, this.gameObject ); playableGraph.Initialize(gameObject); playableGraph.CreateAnimationDataPlayables(editedData, currentAnimaionTime); } } break; case ContactStateType.Impacts: if (GUILayout.Button("Sort impacts", GUIResources.Button_MD()) && editedData != null) { editedData.contactPoints.Sort(); } if (GUILayout.Button("Calculate Impacts", GUIResources.Button_MD()) && editedData != null && gameObject != null) { if (gameObject == null) { Debug.LogWarning("Game object in MM Data Editor is NULL!"); return; } else { editedData.contactPoints.Sort(); MotionDataCalculator.CalculateImpactPoints( editedData, editedData.contactPoints.ToArray(), this.playableGraph, this.gameObject ); playableGraph.Initialize(gameObject); playableGraph.CreateAnimationDataPlayables(editedData, currentAnimaionTime); } } break; } }