/// <summary> /// Adds the transform into the skeleton. /// </summary> /// <param name="parentHash">Hash of parent transform name.</param> /// <param name="hash">Hash of transform name.</param> /// <param name="transform">Transform.</param> public virtual void AddBone(int parentHash, int hash, Transform transform) { BoneData newBone = new BoneData() { accessedFrame = frame, parentBoneNameHash = parentHash, boneNameHash = hash, boneTransform = transform, umaTransform = new UMATransform(transform, hash, parentHash), }; boneHashData.Add(hash, newBone); }
static Bone GetExtractionBone () { if (extractionBone != null) return extractionBone; SkeletonData skelData = new SkeletonData(); BoneData data = new BoneData("temp", null); data.ScaleX = 1; data.ScaleY = 1; data.Length = 100; skelData.Bones.Add(data); Skeleton skeleton = new Skeleton(skelData); Bone bone = new Bone(data, skeleton, null); bone.UpdateWorldTransform(); extractionBone = bone; return extractionBone; }
static string GetPathRecurse (BoneData b) { if (b == null) { return ""; } return GetPathRecurse(b.Parent) + "/" + b.Name; }
static string GetPath (BoneData b) { return GetPathRecurse(b).Substring(1); }
/// <summary> /// Adds the transform into the skeleton. /// </summary> /// <param name="transform">Transform.</param> public virtual void AddBone(UMATransform transform) { var go = new GameObject(transform.name); BoneData newBone = new BoneData() { accessedFrame = -1, parentBoneNameHash = transform.parent, boneNameHash = transform.hash, boneTransform = go.transform, umaTransform = transform.Duplicate(), }; boneHashDataBackup.Add(newBone); boneHashData.Add(transform.hash, newBone); }
private void AddBonesRecursive(Transform transform) { var hash = UMAUtils.StringToHash(transform.name); var parentHash = transform.parent != null ? UMAUtils.StringToHash(transform.parent.name) : 0; BoneData data = new BoneData() { parentBoneNameHash = parentHash, boneNameHash = hash, accessedFrame = frame, boneTransform = transform, umaTransform = new UMATransform(transform, hash, parentHash) }; boneHashData.Add(hash, data); boneHashDataBackup.Add(data); for (int i = 0; i < transform.childCount; i++) { var child = transform.GetChild(i); AddBonesRecursive(child); } }
/// <summary> /// Add a new bone to the chain at the selected location or at the end if no location is selected. /// </summary> void AddBone() { BoneData newBone = new BoneData (); newBone.Length = 50; newBone.Weight = 1; // insert at the end if no bone is selected //if (_boneList.SelectedIndex == -1) Bones.Add (newBone); //else // Bones.Insert (_boneList.SelectedIndex, newBone); }
private void ReadSkeletonBones(SkeletonData skeletonData, JObject data, float scale) { foreach (JToken bone in data["bones"].Children()) { BoneData parent = null; var parentName = (string) bone["parent"]; if (parentName != null) { parent = skeletonData.FindBone(parentName); if (parent == null) { throw new SerializationException("Parent bone not found: " + parentName); } } var boneData = new BoneData((String) bone["name"], parent); boneData.Length = Read<float>(bone, "length", 0)*scale; boneData.X = Read<float>(bone, "x", 0)*scale; boneData.Y = Read<float>(bone, "y", 0)*scale; boneData.Rotation = Read<float>(bone, "rotation", 0); boneData.ScaleX = Read<float>(bone, "scaleX", 1); boneData.ScaleY = Read<float>(bone, "scaleY", 1); skeletonData.AddBone(boneData); } }
private void HandleGameTimer(int param) { // Every so often, notify what our actual framerate is if ((this.frameCount % 100) == 0) { this.myFallingThings.SetFramerate(1000.0 / this.actualFrameTime); } // Advance animations, save current skeleton state, and do hit testing. for (int i = 0; i < NumIntraFrames; ++i) { BoneData rightShoulderData = new BoneData(); foreach (var pair in this.players) // Although our game is designed for one player, this gives us the potential for multiplayer { Bone rightArm = new Bone(JointType.WristRight, JointType.ElbowRight); BoneData rightArmData; pair.Value.Segments.TryGetValue(rightArm, out rightArmData); Bone rightShoulder = new Bone(JointType.ShoulderCenter, JointType.ShoulderRight); pair.Value.Segments.TryGetValue(rightShoulder, out rightShoulderData); this.rightWristXPosition.RemoveAt(0); this.rightWristXPosition.Add(rightArmData.Segment.X1); this.rightWristXVelocity.RemoveAt(0); this.rightWristXVelocity.Add(rightArmData.XVelocity); this.rightWristYPosition.RemoveAt(0); this.rightWristYPosition.Add(rightArmData.Segment.Y1); this.rightWristYVelocity.RemoveAt(0); this.rightWristYVelocity.Add(rightArmData.YVelocity); this.shoulderCenterXPosition.RemoveAt(0); this.shoulderCenterXPosition.Add(rightShoulderData.Segment.X1); this.shoulderCenterYPosition.RemoveAt(0); this.shoulderCenterYPosition.Add(rightShoulderData.Segment.Y1); // Decrement protegoDuration each frame until 0, at which point if (this.protegoDuration > 0) { this.protegoDuration -= 1; } else { this.myFallingThings.RemoveShape(PolyType.Circle); } bool hit = this.myFallingThings.CheckPlayerHit(pair.Value.Segments, pair.Value.GetId()); if (hit) { // Game over this.myFallingThings.PauseGame(); this.gamePaused = true; FlyingText.NewFlyingText(this.screenRect.Width / 30, new Point(this.screenRect.Width / 2, this.screenRect.Height / 2), "Game over!", System.Windows.Media.Color.FromArgb(255, 255, 0, 0)); this.squeezeSound.Play(); } } this.myFallingThings.AdvanceFrame(rightShoulderData.Segment.X1, rightShoulderData.Segment.Y1); } // Draw new Wpf scene by adding all objects to canvas playfield.Children.Clear(); this.myFallingThings.DrawFrame(this.playfield.Children); foreach (var player in this.players) { player.Value.Draw(playfield.Children); } FlyingText.Draw(playfield.Children); this.CheckPlayers(); }
static ArmatureData DecodeArmature(JSONClass parent, JSONClass json) { ArmatureData data = new ArmatureData(); data.name = json["name"].Value; data.frameRate = TryGetInt(json, "frameRate", parent["frameRate"].AsInt); data.type = TryGetString(json, "type", "Armature"); data.vertexDatas = new VertexData(); //bone JSONArray bones = json["bone"].AsArray; BoneData[] boneDatas = new BoneData[bones.Count]; for (int i = 0; i < bones.Count; i++) { boneDatas[i] = DecodeBone(bones[i].AsObject); } data.boneDatas = boneDatas; //slot JSONArray slots = json["slot"].AsArray; SlotData[] slotDatas = new SlotData[slots.Count]; for (int i = 0; i < slots.Count; i++) { slotDatas[i] = DecodeSlot(slots[i].AsObject); } data.slotDatas = slotDatas; JSONArray skins = json["skin"].AsArray; List <DisplayData> displayData = new List <DisplayData>(); for (int i = 0; i < skins.Count; i++) { JSONClass skin = skins[i].AsObject; JSONArray d_slots = skin["slot"].AsArray; for (int j = 0; j < d_slots.Count; j++) { JSONClass d_slot = d_slots[j].AsObject; string d_slot_name = d_slot["name"]; JSONArray d_displays = d_slot["display"].AsArray; for (int k = 0; k < d_displays.Count; k++) { displayData.Add(DecodeDisplay(d_displays[k].AsObject, d_slot_name)); } } } data.displayDatas = displayData.ToArray(); data.vertexDatas.vertices = vertices.ToArray(); data.vertexDatas.uvs = uvs.ToArray(); data.vertexDatas.triangles = triangles.ToArray(); data.vertexDatas.weights = weights.ToArray(); JSONArray animations = json["animation"].AsArray; data.animations = new AnimationData[animations.Count]; for (int i = 0; i < animations.Count; i++) { data.animations[i] = DecodeAnimation(animations[i].AsObject); } return(data); }
private void Awake() { defMotionSpeed = MotionSpeed; int c = 0; /* * if(headBone){ * headBoneList.Add(headBone); * * BoneData b = new BoneData(headBone); * b.defaultRotation = headBone.localEulerAngles; * headBoneData.Add(b); * * Transform t = headBone.GetChild(0); * while(true){ * if(!t || c > 20){ * break; * }else{ * c++; * headBoneList.Add(t); * * b = new BoneData(t); * b.defaultRotation = t.localEulerAngles; * * headBoneData.Add(b); * * Transform oldT = t; * foreach(Transform child in t){ * t = child; * break; * } * if(t == oldT) * break; * } * } * } */ c = 0; if (bodyBone) { bodyBoneList.Add(bodyBone); BoneData b = new BoneData(model); b.defaultRotation = model.localEulerAngles; bodyBoneData.Add(b); bodyBoneList.Add(model); b = new BoneData(bodyBone); b.defaultRotation = bodyBone.localEulerAngles; bodyBoneData.Add(b); bodyBoneList.Add(bodyBone); Transform t = bodyBone.GetChild(0); while (true) { if (!t || c > 20) { break; } else { c++; bodyBoneList.Add(t); b = new BoneData(t); b.defaultRotation = t.localEulerAngles; bodyBoneData.Add(b); Transform oldT = t; foreach (Transform child in t) { t = child; break; } if (t == oldT) { break; } } } } if (model) { GetColliders(model); StartCoroutine(SprashEffect()); } }
public static MotionMatchingData CalculateAnimationSequenceData( string name, AnimationsSequence seq, GameObject go, PreparingDataPlayableGraph graph, List <Transform> bonesMask, List <Vector2> bonesWeights, int sampling, bool loop, Transform root, List <float> trajectoryStepTimes, bool blendToYourself, bool findInYourself ) { if (!graph.IsValid()) { graph.Initialize(go); } go.transform.position = Vector3.zero; go.transform.rotation = Quaternion.identity; seq.CalculateLength(); #region need floats float frameTime = 1f / (float)sampling; int numberOfFrames = Mathf.FloorToInt(seq.length / frameTime) + 1; #endregion MotionMatchingData data = new MotionMatchingData( seq.clips.ToArray(), seq.neededInfo.ToArray(), sampling, name, loop, seq.length, findInYourself, blendToYourself, AnimationDataType.AnimationSequence ); FrameData frameBuffer; BoneData boneBuffer; PoseData poseBuffor; Trajectory trajectoryBuffor; NeedValueToCalculateData[] previuData = new NeedValueToCalculateData[bonesMask.Count]; NeedValueToCalculateData[] nextData = new NeedValueToCalculateData[bonesMask.Count]; int seqDeltaSampling = 3; //seq.CreatePlayableGraph(playableGraph, go); //seq.Update(-frameTime, playableGraph, seqDeltaSampling); seq.CreateAnimationsInTime(0f, graph); graph.Evaluate(frameTime); seq.Update(graph, frameTime); int frameIndex = 0; for (; frameIndex < numberOfFrames; frameIndex++) { for (int i = 0; i < bonesMask.Count; i++) { previuData[i] = GetValuesFromTransform(bonesMask[i], root); } graph.Evaluate(frameTime); seq.Update(graph, frameTime); //Debug.Log((float)animator.GetMixerInputTime(0) - clip.length); for (int i = 0; i < bonesMask.Count; i++) { nextData[i] = GetValuesFromTransform(bonesMask[i], root); } poseBuffor = new PoseData(bonesMask.Count); for (int i = 0; i < bonesMask.Count; i++) { float2 boneWeight = bonesWeights[i]; float3 velocity = BoneData.CalculateVelocity(previuData[i].position, nextData[i].position, frameTime); float3 localPosition = previuData[i].position; quaternion orientation = previuData[i].rotation; boneBuffer = new BoneData(localPosition, velocity); poseBuffor.SetBone(boneBuffer, i); } trajectoryBuffor = new Trajectory(trajectoryStepTimes.Count); frameBuffer = new FrameData( frameIndex, frameIndex * frameTime, trajectoryBuffor, poseBuffor, new FrameSections(true) ); data.AddFrame(frameBuffer); } float clipGlobalStart; Vector2 clipStartAndStop; float recordingClipTime; if (trajectoryStepTimes[0] < 0) { clipGlobalStart = trajectoryStepTimes[0]; clipStartAndStop = new Vector2(-clipGlobalStart, -clipGlobalStart + seq.length); } else { clipGlobalStart = 0; clipStartAndStop = new Vector2(0, seq.length); } if (trajectoryStepTimes[trajectoryStepTimes.Count - 1] > 0) { recordingClipTime = clipStartAndStop.y + trajectoryStepTimes[trajectoryStepTimes.Count - 1] + 0.1f; } else { recordingClipTime = clipStartAndStop.y + 0.1f; } int samplesPerSecond = 100; float deltaTime = 1f / (float)samplesPerSecond; int dataCount = Mathf.CeilToInt(recordingClipTime / deltaTime); NeedValueToCalculateData[] recordData = new NeedValueToCalculateData[dataCount]; go.transform.position = Vector3.zero; go.transform.rotation = Quaternion.identity; //seq.Update(clipGlobalStart, playableGraph); graph.ClearMainMixerInput(); seq.CreateAnimationsInTime(clipGlobalStart, graph); recordData[0] = new NeedValueToCalculateData( go.transform.position, go.transform.forward, go.transform.rotation ); for (int i = 0; i < dataCount; i++) { graph.Evaluate(deltaTime); seq.Update(graph, deltaTime); recordData[i] = new NeedValueToCalculateData( go.transform.position, go.transform.forward, go.transform.rotation ); } //clearing graph from all animations graph.ClearMainMixerInput(); MotionDataCalculator.CalculateTrajectoryPointsFromRecordData( data, recordData, recordingClipTime, deltaTime, clipStartAndStop, trajectoryStepTimes ); data.usedFrameCount = data.numberOfFrames; data.trajectoryPointsTimes = new List <float>(); for (int i = 0; i < trajectoryStepTimes.Count; i++) { data.trajectoryPointsTimes.Add(trajectoryStepTimes[i]); } return(data); }
public static MotionMatchingData CalculateBlendTreeData( string name, GameObject go, PreparingDataPlayableGraph graph, AnimationClip[] clips, List <Transform> bonesMask, List <Vector2> bonesWeights, Transform root, List <float> trajectoryStepTimes, float[] weightsForClips, int sampling, bool loop, bool blendToYourself, bool findInYourself ) { if (!graph.IsValid()) { graph.Initialize(go); } go.transform.position = Vector3.zero; go.transform.rotation = Quaternion.identity; #region need floats float frameTime = 1f / (float)sampling; int numberOfFrames = Mathf.FloorToInt(clips[0].length / frameTime) + 1; #endregion float weightSum = 0f; for (int i = 0; i < weightsForClips.Length; i++) { weightSum += weightsForClips[i]; } for (int i = 0; i < weightsForClips.Length; i++) { weightsForClips[i] = weightsForClips[i] / weightSum; } MotionMatchingData data = new MotionMatchingData( clips, weightsForClips, sampling, name, loop, clips[0].length, findInYourself, blendToYourself, AnimationDataType.BlendTree ); FrameData frameBuffer; BoneData boneBuffer; PoseData poseBuffor; Trajectory trajectoryBuffor; NeedValueToCalculateData[] previewBoneData = new NeedValueToCalculateData[bonesMask.Count]; NeedValueToCalculateData[] nextBoneData = new NeedValueToCalculateData[bonesMask.Count]; for (int i = 0; i < clips.Length; i++) { graph.AddClipPlayable(clips[i]); graph.SetMixerInputTime(i, 0f); graph.SetMixerInputWeight(i, weightsForClips[i]); } graph.Evaluate(frameTime); int frameIndex = 0; float currentCheckingTime = 0f; // FramesCalculation for (; frameIndex < numberOfFrames; frameIndex++) { for (int i = 0; i < bonesMask.Count; i++) { previewBoneData[i] = GetValuesFromTransform(bonesMask[i], root); } graph.Evaluate(frameTime); currentCheckingTime = frameIndex * frameTime; for (int i = 0; i < bonesMask.Count; i++) { nextBoneData[i] = GetValuesFromTransform(bonesMask[i], root); } poseBuffor = new PoseData(bonesMask.Count); for (int i = 0; i < bonesMask.Count; i++) { float2 boneWeight = bonesWeights[i]; float3 velocity = BoneData.CalculateVelocity(previewBoneData[i].position, nextBoneData[i].position, frameTime); float3 localPosition = previewBoneData[i].position; quaternion orientation = previewBoneData[i].rotation; boneBuffer = new BoneData(localPosition, velocity); poseBuffor.SetBone(boneBuffer, i); } trajectoryBuffor = new Trajectory(trajectoryStepTimes.Count); frameBuffer = new FrameData( frameIndex, currentCheckingTime, trajectoryBuffor, poseBuffor, new FrameSections(true) ); data.AddFrame(frameBuffer); } // Trajectory calculations float clipGlobalStart; Vector2 clipStartAndStop; float recordingClipTime; if (trajectoryStepTimes[0] < 0) { clipGlobalStart = trajectoryStepTimes[0]; clipStartAndStop = new Vector2(-clipGlobalStart, -clipGlobalStart + clips[0].length); } else { clipGlobalStart = 0; clipStartAndStop = new Vector2(0, clips[0].length); } if (trajectoryStepTimes[trajectoryStepTimes.Count - 1] > 0) { recordingClipTime = clipStartAndStop.y + trajectoryStepTimes[trajectoryStepTimes.Count - 1] + 0.1f; } else { recordingClipTime = clipStartAndStop.y + 0.1f; } int samplesPerSecond = 100; float deltaTime = 1f / (float)samplesPerSecond; int dataCount = Mathf.CeilToInt(recordingClipTime / deltaTime); NeedValueToCalculateData[] recordData = new NeedValueToCalculateData[dataCount]; go.transform.position = Vector3.zero; go.transform.rotation = Quaternion.identity; for (int i = 0; i < graph.GetMixerInputCount(); i++) { graph.SetMixerInputTimeInPlace(i, clipGlobalStart); } recordData[0] = new NeedValueToCalculateData( go.transform.position, go.transform.forward, go.transform.rotation ); for (int i = 0; i < dataCount; i++) { graph.Evaluate(deltaTime); recordData[i] = new NeedValueToCalculateData( go.transform.position, go.transform.forward, go.transform.rotation ); } //clearing graph from all animations graph.ClearMainMixerInput(); MotionDataCalculator.CalculateTrajectoryPointsFromRecordData( data, recordData, recordingClipTime, deltaTime, clipStartAndStop, trajectoryStepTimes ); data.usedFrameCount = data.numberOfFrames; data.trajectoryPointsTimes = new List <float>(); for (int i = 0; i < trajectoryStepTimes.Count; i++) { data.trajectoryPointsTimes.Add(trajectoryStepTimes[i]); } return(data); }