public void OnEnable() { Puppet2D_IKHandle myTarget = (Puppet2D_IKHandle)target; //myTarget.startTransform = myTarget.topJointTransform; myTarget.endTransform = myTarget.bottomJointTransform; }
public void resetIK(Puppet2D_IKHandle myTarget) { myTarget.enabled = false; //myTarget.transform.localPosition = Vector3.zero; for (int i = 0; i < 100; i++) { for (int j = 0; j < myTarget.bindBones.Count; j++) { myTarget.bindBones[j].localRotation = Quaternion.Euler(myTarget.bindPose[j]); } } myTarget.enabled = true; }
public void TraverseHierarchy(Transform root) { foreach (Transform child in root) { GameObject Go = child.gameObject; SpriteRenderer spriteRenderer = Go.transform.GetComponent <SpriteRenderer>(); if (spriteRenderer && spriteRenderer.sprite) { if (spriteRenderer.sprite.name.Contains("Control")) { _Controls.Add(spriteRenderer); } else if (spriteRenderer.sprite.name.Contains("ffd")) { _FFDControls.Add(spriteRenderer); } } Puppet2D_Bone boneRender = Go.transform.GetComponent <Puppet2D_Bone>(); if (boneRender) { _Bones.Add(boneRender); } Puppet2D_ParentControl newParentCtrl = Go.transform.GetComponent <Puppet2D_ParentControl>(); if (newParentCtrl) { _ParentControls.Add(newParentCtrl); } Puppet2D_IKHandle newIKCtrl = Go.transform.GetComponent <Puppet2D_IKHandle>(); if (newIKCtrl) { _Ikhandles.Add(newIKCtrl); } Puppet2D_FFDLineDisplay ffdCtrl = Go.transform.GetComponent <Puppet2D_FFDLineDisplay>(); if (ffdCtrl) { _ffdControls.Add(ffdCtrl); } Puppet2D_SplineControl splineCtrl = Go.transform.GetComponent <Puppet2D_SplineControl>(); if (splineCtrl) { _SplineControls.Add(splineCtrl); } TraverseHierarchy(child); } }
public static void IKCreateTool(bool worldSpace = false) { GameObject bone = Selection.activeObject as GameObject; if (bone) { if (bone.GetComponent <SpriteRenderer>()) { if (!bone.GetComponent <SpriteRenderer>().sprite.name.Contains("Bone")) { Debug.LogWarning("This is not a Puppet2D Bone"); return; } } else { Debug.LogWarning("This is not a Puppet2D Bone"); return; } } else { Debug.LogWarning("This is not a Puppet2D Bone"); return; } GameObject globalCtrl = CreateGlobalControl(); foreach (Puppet2D_ParentControl parentControl in globalCtrl.GetComponent <Puppet2D_GlobalControl>()._ParentControls) { if ((parentControl.bone.transform == bone.transform) || (parentControl.bone.transform == bone.transform.parent.transform)) { Debug.LogWarning("Can't create a IK Control on Bone; it alreay has an Parent Control"); return; } } foreach (Puppet2D_IKHandle ikhandle in globalCtrl.GetComponent <Puppet2D_GlobalControl>()._Ikhandles) { if ((ikhandle.bottomJointTransform == bone.transform) || (ikhandle.middleJointTransform == bone.transform) || (ikhandle.topJointTransform == bone.transform)) { Debug.LogWarning("Can't create a IK Control on Bone; it alreay has an IK handle"); return; } } GameObject IKRoot = null; if (bone.transform.parent && bone.transform.parent.transform.parent && bone.transform.parent.transform.parent.GetComponent <SpriteRenderer>() && bone.transform.parent.transform.parent.GetComponent <SpriteRenderer>().sprite != null && bone.transform.parent.transform.parent.GetComponent <SpriteRenderer>().sprite.name.Contains("Bone")) { IKRoot = bone.transform.parent.transform.parent.gameObject; } if (IKRoot == null) { Debug.LogWarning("You need to select the end of a chain of three bones"); return; } // CHECK IF TOP BONE HAS AN IK ATTACHED Puppet2D_GlobalControl[] globalCtrls = GameObject.FindObjectsOfType <Puppet2D_GlobalControl>(); foreach (Puppet2D_GlobalControl glblCtrl in globalCtrls) { foreach (Puppet2D_IKHandle ik in glblCtrl._Ikhandles) { if (ik.topJointTransform == bone.transform.parent.transform.parent) { Debug.LogWarning(bone.transform.parent.transform.parent.name + " already has an IK control"); return; } } foreach (Puppet2D_SplineControl splineCtrl in glblCtrl._SplineControls) { foreach (GameObject splineBone in splineCtrl.bones) { if (splineBone.transform == bone.transform.parent.transform.parent) { Debug.LogWarning(bone.transform.parent.transform.parent.name + " has a Spline control attached, please make sure there are at least 3 bones after the spline bone"); return; } } } } // CHECK TO SEE IF THE BOTTOM BONE IS POINTING AT THE MIDDLE BONE if (bone.transform.parent.transform.parent.rotation != Quaternion.LookRotation(bone.transform.parent.transform.position - bone.transform.parent.transform.parent.position, Vector3.forward) * Quaternion.AngleAxis(90, Vector3.right)) { //if(bone.transform.parent.transform.parent); Puppet2D_BoneCreation.sortOutBoneHierachy(bone.transform.parent.gameObject, true); } if (bone.transform.parent.rotation != Quaternion.LookRotation(bone.transform.position - bone.transform.parent.position, Vector3.forward) * Quaternion.AngleAxis(90, Vector3.right)) { //if(bone.transform.parent.transform.parent); Puppet2D_BoneCreation.sortOutBoneHierachy(bone, true); } GameObject control = new GameObject(); Undo.RegisterCreatedObjectUndo(control, "Created control"); control.name = (bone.name + "_CTRL"); GameObject controlGroup = new GameObject(); Undo.RegisterCreatedObjectUndo(controlGroup, "new control grp"); controlGroup.name = (bone.name + "_CTRL_GRP"); control.transform.parent = controlGroup.transform; controlGroup.transform.position = bone.transform.position; if (!worldSpace) { controlGroup.transform.rotation = bone.transform.rotation; } GameObject poleVector = new GameObject(); Undo.RegisterCreatedObjectUndo(poleVector, "Created polevector"); poleVector.name = (bone.name + "_POLE"); SpriteRenderer spriteRenderer = control.AddComponent <SpriteRenderer>(); string path = (Puppet2D_Editor._puppet2DPath + "/Textures/GUI/IKControl.psd"); Sprite sprite = AssetDatabase.LoadAssetAtPath(path, typeof(Sprite)) as Sprite; spriteRenderer.sprite = sprite; spriteRenderer.sortingLayerName = Puppet2D_Editor._controlSortingLayer; // store middle bone position to check if it needs flipping Vector3 middleBonePos = bone.transform.parent.transform.position; Puppet2D_IKHandle ikHandle = control.AddComponent <Puppet2D_IKHandle>(); ikHandle.topJointTransform = IKRoot.transform; ikHandle.middleJointTransform = bone.transform.parent.transform; ikHandle.bottomJointTransform = bone.transform; ikHandle.poleVector = poleVector.transform; ikHandle.scaleStart[0] = IKRoot.transform.localScale; ikHandle.scaleStart[1] = IKRoot.transform.GetChild(0).localScale; ikHandle.OffsetScale = bone.transform.localScale; if (worldSpace) { ikHandle.Offset = ikHandle.bottomJointTransform.rotation; } if (bone.GetComponent <SpriteRenderer>().sprite.name.Contains("Bone")) { ikHandle.AimDirection = Vector3.forward; ikHandle.UpDirection = Vector3.right; } else { Debug.LogWarning("This is not a Puppet2D Bone"); ikHandle.AimDirection = Vector3.right; ikHandle.UpDirection = Vector3.up; } //if (bone.transform.parent.transform.position.x < IKRoot.transform.position.x) Selection.activeObject = ikHandle; controlGroup.transform.parent = globalCtrl.transform; poleVector.transform.parent = globalCtrl.transform; if (globalCtrl.GetComponent <Puppet2D_GlobalControl>().AutoRefresh) { globalCtrl.GetComponent <Puppet2D_GlobalControl>().Init(); } else { globalCtrl.GetComponent <Puppet2D_GlobalControl>()._Ikhandles.Add(ikHandle); } //fix from now on for 180 flip globalCtrl.GetComponent <Puppet2D_GlobalControl>()._flipCorrection = -1; globalCtrl.GetComponent <Puppet2D_GlobalControl>().Run(); if ((Vector3.Distance(bone.transform.parent.transform.position, middleBonePos) > 0.0001f)) { ikHandle.Flip = true; } }
public override void OnInspectorGUI() { Puppet2D_IKHandle myTarget = (Puppet2D_IKHandle)target; GUI.changed = false; // DrawDefaultInspector(); EditorGUI.BeginChangeCheck(); myTarget.numberIkBonesIndex = EditorGUILayout.Popup("IK Type", myTarget.numberIkBonesIndex, IkType); //The popup menu is displayed simple as that if (EditorGUI.EndChangeCheck()) { if (myTarget.numberIkBonesIndex == 0) { resetIK(myTarget); } else { myTarget.bindPose = new List <Vector3>(); myTarget.bindBones = new List <Transform>(); myTarget.bindPose.Clear(); myTarget.bindBones.Clear(); Transform node = myTarget.bottomJointTransform; while (true) { if (node == null) { break; } myTarget.bindBones.Add(node); myTarget.bindPose.Add(node.localEulerAngles); node = node.parent; } setEndBone(myTarget); } } if ((target as Puppet2D_IKHandle).numberIkBonesIndex == 0) { EditorGUI.BeginChangeCheck(); bool f = EditorGUILayout.Toggle("Flip", myTarget.Flip); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(myTarget, "Toggle Flip"); myTarget.Flip = f; } myTarget.SquashAndStretch = EditorGUILayout.Toggle("SquashAndStretch", myTarget.SquashAndStretch); myTarget.Scale = EditorGUILayout.Toggle("Scale", myTarget.Scale); } else { EditorGUI.BeginChangeCheck(); myTarget.numberOfBones = EditorGUILayout.IntField("Number of Bones", myTarget.numberOfBones); if (EditorGUI.EndChangeCheck()) { setEndBone(myTarget); } myTarget.iterations = EditorGUILayout.IntField("Iterations", myTarget.iterations); myTarget.damping = EditorGUILayout.Slider("Dampening", myTarget.damping, 0f, 1f); EditorGUI.BeginChangeCheck(); myTarget.limitBones = EditorGUILayout.Toggle("Limits", myTarget.limitBones); if (EditorGUI.EndChangeCheck()) { setEndBone(myTarget); } if (GUILayout.Button("Reset")) { resetIK(myTarget); myTarget.transform.localPosition = Vector3.zero; } } if (GUI.changed) { if (_SquashAndStretch && !myTarget.SquashAndStretch) { myTarget.topJointTransform.localScale = myTarget.scaleStart[0]; } _SquashAndStretch = myTarget.SquashAndStretch; EditorUtility.SetDirty(myTarget); } }
public void setEndBone(Puppet2D_IKHandle myTarget) { myTarget.angleLimits.Clear(); myTarget.angleLimitTransform.Clear(); if (myTarget.numberOfBones < 2) { myTarget.numberOfBones = 2; } Puppet2D_GlobalControl[] globalCtrlScripts = Transform.FindObjectsOfType <Puppet2D_GlobalControl>(); myTarget.endTransform = myTarget.bottomJointTransform; myTarget.startTransform = myTarget.endTransform; bool unlockedBone = true; for (int i = 0; i < myTarget.numberOfBones - 1; i++) { if (myTarget.startTransform.parent != null) { for (int j = 0; j < globalCtrlScripts.Length; j++) { if (myTarget.startTransform.parent.GetComponent <Puppet2D_GlobalControl>()) { myTarget.numberOfBones = i + 1; unlockedBone = false; } foreach (Puppet2D_ParentControl ParentControl in globalCtrlScripts[j]._ParentControls) { if (ParentControl.bone.transform == myTarget.startTransform.parent) { myTarget.numberOfBones = i + 1; unlockedBone = false; } } foreach (Puppet2D_SplineControl splineCtrl in globalCtrlScripts[j]._SplineControls) { foreach (GameObject bone in splineCtrl.bones) { if (bone.transform == myTarget.startTransform.parent) { myTarget.numberOfBones = i + 1; unlockedBone = false; } } } } if (unlockedBone) { if (myTarget.startTransform != myTarget.endTransform && myTarget.limitBones) { Vector2 limit = new Vector2(); Transform limitTransform = myTarget.startTransform; Vector3 newEulerAngle = new Vector3(limitTransform.localEulerAngles.x % 360, limitTransform.localEulerAngles.y % 360, limitTransform.localEulerAngles.z % 360); if (newEulerAngle.x < 0) { newEulerAngle.x += 360; } if (newEulerAngle.y < 0) { newEulerAngle.y += 360; } if (newEulerAngle.z < 0) { newEulerAngle.z += 360; } myTarget.startTransform.localEulerAngles = newEulerAngle; float rangedVal = limitTransform.localEulerAngles.z % 360; if (rangedVal > 0 && rangedVal < 180) { limit = new Vector2(0, 180); myTarget.angleLimits.Add(limit); myTarget.angleLimitTransform.Add(limitTransform); } else if (rangedVal > 180 && rangedVal < 360) { limit = new Vector2(180, 360); myTarget.angleLimits.Add(limit); myTarget.angleLimitTransform.Add(limitTransform); } else if (rangedVal > -180 && rangedVal < 0) { limit = new Vector2(-180, 0); myTarget.angleLimits.Add(limit); myTarget.angleLimitTransform.Add(limitTransform); } else if (rangedVal > -360 && rangedVal < -180) { limit = new Vector2(-360, -180); myTarget.angleLimits.Add(limit); myTarget.angleLimitTransform.Add(limitTransform); } } myTarget.startTransform = myTarget.startTransform.parent; } } else { myTarget.numberOfBones = i + 1; } } //CreateNodeCache (myTarget); // Debug.Log ("start " + myTarget.startTransform + " end " + myTarget.endTransform); }