public void OnEnable()
        {
            skel = (MoveenSkelWithBones)target;

//            if (skel.wanteds == null) {
            initWanteds(skel);
//            }
        }
        public static void updateProtoModel(Transform skelParent, Transform modelParent, Action <Bone, Transform> perBone)
        {
            if (modelParent.childCount < 1)
            {
                newObject(modelParent);
            }
            Transform forBones = modelParent.GetChild(0);

            forBones.name = modelName + "_forBones";
            MoveenSkelWithBones[] boness = skelParent.GetComponents <MoveenSkelWithBones>();
            int curBoneGeo = 0;

            for (int boneComponentIndex = 0; boneComponentIndex < boness.Length; boneComponentIndex++)
            {
                MoveenSkelWithBones bones = boness[boneComponentIndex];

                for (int boneIndex = 0; boneIndex < bones.bones.Count; boneIndex++)
                {
                    if (forBones.childCount <= curBoneGeo)
                    {
                        newObject(forBones);
                    }
                    Transform boneGeo = forBones.GetChild(curBoneGeo);
                    boneGeo.name = modelName + "_bone_" + boneIndex;
                    curBoneGeo++;

                    Bone bone = bones.bones[boneIndex];
                    boneGeo.SetPositionAndRotation(bone.origin.getPos(), bone.origin.getRot());
                    if (bone.attachedGeometry == null || bone.attachedGeometry.name.StartsWith(modelName))
                    {
                        bones.bonesGeometry[boneIndex] = boneGeo;
                        bone.attachedGeometry          = boneGeo;
                        perBone(bone, boneGeo);
                    }
                }
            }

            for (int i = 0; i < skelParent.childCount; i++)
            {
                Transform skelChild = skelParent.GetChild(i);
                if (modelParent.childCount <= i + 1)
                {
                    newObject(modelParent);
                }
                Transform modelChild = modelParent.GetChild(i + 1);
                modelChild.name = modelName + "_" + skelChild.name;

                if (hierarchyContainsBones(skelChild))
                {
                    updateProtoModel(skelChild, modelChild, perBone);
                }
            }
        }
//        [DrawGizmo(GizmoType.InSelectionHierarchy | GizmoType.Active)]
//        [DrawGizmo(GizmoType.InSelectionHierarchy | GizmoType.NonSelected | GizmoType.Active)]
        public static void OnDrawGizmos(MoveenSkelWithBones component)
        {
            if (!component.isActiveAndEnabled)
            {
                return;
            }
            Gizmos.color = BONE_COLOR;
            for (int i = 0; i < component.bones.Count; i++)
            {
                Bone bone = component.bones[i];
                bone.origin.tick(); //TODO ensure tick in root
                if (bone.origin.getRot().magnitude() < 0.3f)
                {
                    MUtil.log(component, "wrong quaternion: " + bone.origin.getRot());
                    //Debug.Log("  " + i);
                    continue;
                }
                UnityEditorUtils.diamond(bone.origin.getPos(), bone.origin.getRot().normalized(), bone.r);
            }

            Type type = component.GetType();

            Gizmos.color = Color.green;
            for (int index = 0; index < type.GetFields().Length; index++)
            {
                FieldInfo field = type.GetFields()[index];
                if (field.FieldType == typeof(Vector3))
                {
                    if (field.GetCustomAttributes(typeof(CustomSkelResultAttribute), true).Length != 0)
                    {
                        Vector3 cur = (Vector3)field.GetValue(component);
                        Gizmos.DrawWireSphere(cur, 0.04f);
                    }
                }
                if (field.FieldType == typeof(P <Vector3>))
                {
                    if (field.GetCustomAttributes(typeof(CustomSkelResultAttribute), true).Length != 0)
                    {
                        P <Vector3> cur = (P <Vector3>)field.GetValue(component);
                        Gizmos.DrawWireSphere(cur.v, 0.04f);
                    }
                }
            }
        }
 public static void addBonesButton(MoveenSkelWithBones skel)
 {
     EditorGUILayout.LabelField("");
     GUI.enabled = skel.transform.childCount < skel.bones.Count;
     if (GUILayout.Button(new GUIContent("Add missing bones",
                                         "Create absent child bones. They will be affected by this limb even in the editor." +
                                         "\nNote, you can do without child bones if you connect external bones.")))
     {
         for (int i = skel.transform.childCount; i < skel.bones.Count; i++)
         {
             if (i < skel.bones.Count - 1)
             {
                 new GameObject("bone " + (i + 1)).transform.parent = skel.transform;
             }
             else
             {
                 new GameObject("bone foot").transform.parent = skel.transform;
             }
         }
     }
     GUI.enabled = true;
 }
        public void OnSceneGUI()
        {
            if (Application.isPlaying)
            {
                return;
            }
            Transform withStepper = MoveenStep2Editor.withStepper(component.transform);

            if (withStepper == null)
            {
                return;
            }
            MoveenSkelWithBones skel = component.transform.GetComponent <MoveenSkelWithBones>();


            if (skel == null || component.step.detachedComfortPosRel)
            {
                Vector3 oldAbs = withStepper.TransformPoint(component.step.comfortPosRel);
//            Vector3 oldAbs = component.transform.parent.TransformPoint(component.step.bestTargetRel);
                Handles.Label(oldAbs, "comfort position");

                Undo.RecordObject(component, "Moveen target position");
                component.step.comfortPosRel = withStepper.InverseTransformPoint(Handles.PositionHandle(oldAbs, Quaternion.identity));
                //Undo.FlushUndoRecordObjects();seems like done automatically
            }

            if (skel != null && !component.step.detachedComfortPosRel)
            {
                Vector3 tipAbs = skel.transform.TransformPoint(skel.targetPosRel);
                component.step.comfortPosRel = withStepper.rotation.conjug().rotate(tipAbs - withStepper.position);//can't use Inverse, because in the enine - only rotation and translation is used
                component.step.reset(withStepper.position, withStepper.rotation);
            }

            if (GUI.changed)  //to react on comfort position handler (when detached)
            {
                component.step.reset(withStepper.position, withStepper.rotation);
            }
        }
Esempio n. 6
0
        public void createRagdoll()
        {
            foreach (var l in component.stepper.GetComponentsInChildren <MoveenSkelWithBones>())
            {
                l.createRagdoll();
            }
            Transform top = null;

            if (component.stepper.body != null)
            {
                top = component.stepper.body;
                component.usedByStepper = top.GetComponent <Rigidbody>();
            }
            if (top == null)
            {
                MoveenTransformCopier copier = component.stepper.GetComponent <MoveenTransformCopier>();
                if (copier != null)
                {
                    top = copier.target;
                }
                //don't need component.toConnect in case of copier
            }
            if (top == null)
            {
                top = component.transform; //in case everything is in moveen hierarchy
                component.usedByStepper = top.GetComponent <Rigidbody>();
            }
            MoveenSkelWithBones.createCapsule2(top, 0.3f, 0.3f);//TODO better r

            Transform hipT = null;

            component.jointsToConnect = new List <Joint>();
            if (component.stepper.hip != null)
            {
                MoveenTransformCopier connector = component.stepper.hip.GetComponent <MoveenTransformCopier>();
                if (connector != null)
                {
                    hipT = connector.target;
                }
                if (hipT == null)
                {
                    hipT = component.stepper.hip;
                }
                MoveenSkelWithBones.createCapsule(hipT, Quaternion.identity, 1);//TODO better r
                CharacterJoint joint = MoveenSkelWithBones.connectToParentCharacterJoint(hipT, top);
                top = hipT;
                if (component.usedByStepper != null && joint != null)
                {
                    component.jointsToConnect.Add(joint);
                }
            }

            MoveenSkelWithBones[] steps = component.stepper.GetComponentsInChildren <MoveenSkelWithBones>();
            foreach (var limb in steps)
            {
                Transform affected = MoveenSkelWithBones.getAffected(limb, 0);
                if (affected != null)
                {
                    CharacterJoint joint = MoveenSkelWithBones.connectToParentCharacterJoint(affected, top);
                    if (component.usedByStepper != null && joint != null && hipT == null)
                    {
                        component.jointsToConnect.Add(joint);
                    }
                }
            }
        }