Exemple #1
0
        public bool Bind(Transform root, string prefix)
        {
            this.root   = root;
            this.prefix = prefix;
            int bound_count = NeuronHelper.Bind(root, transforms, prefix, false);

            boundTransforms = bound_count >= (int)NeuronBones.NumOfBones;
            UpdateOffset();
            return(boundTransforms);
        }
        void InitPhysicalContext()
        {
            if (physicalReference.Init(root, prefix, transforms, physicalReferenceOverride))
            {
                // break original object's hierachy of transforms, so we can use MovePosition() and MoveRotation() to set transform
                NeuronHelper.BreakHierarchy(transforms);
            }

            CheckRigidbodySettings();
        }
Exemple #3
0
        bool InitPhysicalContext()
        {
            if (physicalReference.Init(root, prefix, transforms))
            {
                // break original object's hierachy of transforms, so we can use MovePosition() and MoveRotation() to set transform
                NeuronHelper.BreakHierarchy(transforms);
                return(true);
            }

            return(false);
        }
Exemple #4
0
 public bool Bind(Transform root, string prefix)
 {
     this.root   = root;
     this.prefix = prefix;
     //int bound_count =
     NeuronHelper.Bind(root, transforms, prefix, false, useNewRig ? NeuronBoneVersion.V2 : NeuronBoneVersion.V1);
     boundTransforms = true; // bound_count >= (int)NeuronBones.NumOfBones;
     UpdateOffset();
     CaluateOrignalRot();
     return(boundTransforms);
 }
        public bool Init(Transform root, string prefix, Transform[] bound_transforms)
        {
            if (root == null)
            {
                Debug.LogError("[NeuronTransformsPhysicalReference] Invalid root Transform");
                return(false);
            }

            // check if there is enough Rigidbody Component on the bones,
            // if not return false to prevent init reference_object
            if (!CheckRigidbodies(bound_transforms))
            {
                Debug.LogError(string.Format(
                                   "[NeuronTransformsPhysicalReference] Trying to use physics update but no Rigidbody Component in Actor \"{0}\". Did you forget to add Rigidbody Component?",
                                   root.gameObject.name), root);
                return(false);
            }

            // duplicate bound object as reference object,
            // we only use this reference object's transforms to get world transforms
            referenceObject      = (GameObject)GameObject.Instantiate(root.gameObject, root.position, root.rotation);
            referenceObject.name = string.Format("{0} (neuron reference)", root.gameObject.name);
            referenceTransforms  = new Transform[(int)NeuronBones.NumOfBones];
            NeuronHelper.Bind(referenceObject.transform, referenceTransforms, prefix, false);

            NeuronTransformsInstance referenceInstance = referenceObject.GetComponent <NeuronTransformsInstance>();

            if (referenceInstance != null)
            {
                referenceInstance.physicalUpdate    = false;
                referenceInstance.commandServerPort = -1;
            }

            // remove all unnecessary components, this will prevent rendering and any unexpected behaviour from custom scripts
            Component[] components = referenceObject.GetComponentsInChildren <Component>();
            for (int i = 0; i < components.Length; ++i)
            {
                Component c = components[i];
                if (c.GetType() != typeof(Transform) &&
                    components[i].GetType() != typeof(NeuronTransformsInstance))
                {
                    GameObject.DestroyImmediate(c);
                }
            }

            return(true);
        }
Exemple #6
0
        public static void RemoveSkeletonColliderRenderers(Transform root, string prefix)
        {
            Transform[] bones       = new Transform[(int)NeuronBones.NumOfBones];
            int         bound_count = NeuronHelper.Bind(root, bones, prefix, false);

            if (bound_count < (int)NeuronBones.NumOfBones)
            {
                Debug.LogError(string.Format("[NeuronBoneColliders] Can not bind neuron bones to {0}", root.name), root);
                return;
            }

            int counter = 0;

            for (int i = 0; i < (int)NeuronBones.NumOfBones; ++i)
            {
                Transform bone_t = bones[i];
                if (bone_t != null)
                {
                    for (int child_index = 0; child_index < bone_t.childCount; ++child_index)
                    {
                        Collider collider = bone_t.GetChild(child_index).GetComponent <Collider>();
                        if (collider != null)
                        {
                            MeshFilter   mesh_filter   = collider.GetComponent <MeshFilter>();
                            MeshRenderer mesh_renderer = collider.GetComponent <MeshRenderer>();

                            if (mesh_renderer != null)
                            {
                                GameObject.DestroyImmediate(mesh_renderer);
                            }
                            if (mesh_filter != null)
                            {
                                GameObject.DestroyImmediate(mesh_filter);
                            }

                            ++counter;
                        }
                    }
                }
            }

            Debug.Log(string.Format("[NeuronBoneColliders] {0} Collider renderers removed from {1}", counter, root.name), root);
        }
Exemple #7
0
        public static void RemoveSkeletonColliders(Transform root, string prefix)
        {
            Transform[] bones       = new Transform[(int)NeuronBones.NumOfBones];
            int         bound_count = NeuronHelper.Bind(root, bones, prefix, false);

            if (bound_count < (int)NeuronBones.NumOfBones)
            {
                Debug.LogError(string.Format("[NeuronBoneColliders] Can not bind neuron bones to {0}", root.name), root);
                return;
            }

            int counter = 0;

            for (int i = 0; i < (int)NeuronBones.NumOfBones; ++i)
            {
                Transform bone_t = bones[i];
                if (bone_t != null)
                {
                    List <GameObject> to_delete = new List <GameObject>();
                    for (int child_index = 0; child_index < bone_t.childCount; ++child_index)
                    {
                        Collider collider = bone_t.GetChild(child_index).GetComponent <Collider>();
                        if (collider != null)
                        {
                            to_delete.Add(collider.gameObject);
                        }
                    }

                    counter += to_delete.Count;
                    foreach (GameObject obj in to_delete)
                    {
                        GameObject.DestroyImmediate(obj);
                    }
                }
            }

            Debug.Log(string.Format("[NeuronBoneColliders] {0} Colliders removed from {1}.", counter, root.name), root);
        }
Exemple #8
0
        public static void AddSkeletonColliderRenderers(Transform root, string prefix, Color color)
        {
            RemoveSkeletonColliderRenderers(root, prefix);

            Transform[] dest_bones  = new Transform[(int)NeuronBones.NumOfBones];
            int         bound_count = NeuronHelper.Bind(root, dest_bones, prefix, false);

            if (bound_count < (int)NeuronBones.NumOfBones)
            {
                Debug.LogError(string.Format("[NeuronBoneColliders] Can not bind neuron bones to {0}", root.name), root);
                return;
            }

            Material mat = UnityEditor.AssetDatabase.LoadAssetAtPath <Material>("Assets/Neuron/Resources/Materials/BoneColliderMaterial.mat");

            if (mat != null)
            {
                mat.color = color;
            }
            else
            {
                Debug.LogError(string.Format("[NeuronBonColliders] Can not find \"Assets/Neuron/Resources/Materials/BoneColliderMaterial.mat\"."));
            }

            int        counter          = 0;
            GameObject mesh_capsule_obj = GameObject.CreatePrimitive(PrimitiveType.Capsule);
            GameObject mesh_cube_obj    = GameObject.CreatePrimitive(PrimitiveType.Cube);

            Mesh mesh_capsule = mesh_capsule_obj.GetComponent <MeshFilter>().sharedMesh;
            Mesh mesh_cube    = mesh_cube_obj.GetComponent <MeshFilter>().sharedMesh;

            for (int i = 0; i < (int)NeuronBones.NumOfBones; ++i)
            {
                Transform bone_t = dest_bones[i];
                if (bone_t != null)
                {
                    for (int child_index = 0; child_index < bone_t.childCount; ++child_index)
                    {
                        Collider collider = bone_t.GetChild(child_index).GetComponent <Collider>();
                        if (collider != null)
                        {
                            MeshFilter mesh_filter = collider.gameObject.AddComponent <MeshFilter>();
                            if (collider.GetType() == typeof(CapsuleCollider))
                            {
                                mesh_filter.mesh = mesh_capsule;
                            }

                            if (collider.GetType() == typeof(BoxCollider))
                            {
                                mesh_filter.mesh = mesh_cube;
                            }

                            MeshRenderer mesh_renderer = collider.gameObject.AddComponent <MeshRenderer>();
                            mesh_renderer.material = mat;

                            ++counter;
                        }
                    }
                }
            }

            Debug.Log(string.Format("[NeuronBoneColliders] {0} Collider renderers added to {1}", counter, root.name), root);

            GameObject.DestroyImmediate(mesh_capsule_obj);
            GameObject.DestroyImmediate(mesh_cube_obj);
        }
Exemple #9
0
        public static void AddSkeletonColliders(Transform root, string prefix, string prefab_path, int layer)
        {
            RemoveSkeletonColliders(root, prefix);

            // validate input
            Transform[] dest_bones  = new Transform[(int)NeuronBones.NumOfBones];
            int         bound_count = NeuronHelper.Bind(root, dest_bones, prefix, false);

            if (bound_count < (int)NeuronBones.NumOfBones)
            {
                Debug.LogError(string.Format("[NeuronBoneColliders] Can not bind neuron bones to {0}", root.name), root);
                return;
            }

            // load prefab
            GameObject template = UnityEditor.AssetDatabase.LoadAssetAtPath <GameObject>(prefab_path);

            if (template == null)
            {
                Debug.LogError(string.Format("[NeuronBoneColliders] Can not load asset \"{0}\"", prefab_path));
                return;
            }

            Animator template_animator = template.GetComponent <Animator>();

            if (template_animator == null)
            {
                Debug.LogError(string.Format("[NeuronBoneColliders] Can not find valid animator from template"));
                return;
            }

            // prepare source object
            GameObject src_object = (GameObject)GameObject.Instantiate(template, root.position, root.rotation);

            Transform[] src_bones = new Transform[(int)NeuronBones.NumOfBones];
            bound_count = NeuronHelper.Bind(src_object.transform, src_bones, "Robot_", false);
            if (bound_count < (int)NeuronBones.NumOfBones)
            {
                Debug.LogError(string.Format("[NeuronBoneColliders] Can not bind neuron bones to template {0}", src_object.name), src_object);
            }

            // move colliders
            int counter = 0;

            for (int i = 0; i < (int)NeuronBones.NumOfBones; ++i)
            {
                Transform src_t  = src_bones[i];
                Transform dest_t = dest_bones[i];

                if (src_t != null && dest_t != null)
                {
                    Collider src_c = null;
                    for (int child_index = 0; child_index < src_t.childCount; ++child_index)
                    {
                        Transform t = src_t.GetChild(child_index);
                        src_c = t.GetComponent <Collider>();
                        if (src_c != null)
                        {
                            Vector3    cache_pos = src_c.transform.localPosition;
                            Quaternion cache_rot = src_c.transform.localRotation;

                            src_c.transform.parent        = dest_t;
                            src_c.transform.localPosition = cache_pos;
                            src_c.transform.localRotation = cache_rot;
                            src_c.gameObject.layer        = layer;
                            src_c.gameObject.name         = string.Format("COL_{0}", dest_t.gameObject.name);

                            ++counter;

                            --child_index;
                        }
                    }
                }
            }

            Debug.Log(string.Format("[NeuronBoneColliders] {0} Colliders added to {1}.", counter, root.name), root);

            // clean source object
            GameObject.DestroyImmediate(src_object);
            Resources.UnloadUnusedAssets();
        }
        // bind transform hierachy to bones
        public static int Bind(Transform root, Transform[] bones, string prefix, bool verbose)
        {
            if (root == null)
            {
                Debug.LogError("[NeuronSkeletonHelper] Root is null, bind failed.");
                return(0);
            }

            int counter             = 0;
            Stack <Transform> stack = new Stack <Transform>();

            stack.Push(root);

            while (stack.Count > 0)
            {
                Transform t = stack.Pop();
                if (t.name.StartsWith(prefix))
                {
                    string bone_name = t.name.Substring(t.name.IndexOf(prefix) + prefix.Length);
                    int    index     = NeuronHelper.GetBoneIndex(bone_name);
                    if (index >= 0)
                    {
                        bones[index] = t;
                        ++counter;
                    }
                    else if (verbose)
                    {
                        Debug.LogWarning(string.Format("[NeuronSkeletonHelper] Ignore bone {0}", bone_name), t);
                    }
                }
                else if (verbose)
                {
                    Debug.LogWarning(string.Format("[NeuronSkeletonHelper] Ignore transform {0}", t.name), t);
                }

                for (int i = 0; i < t.childCount; ++i)
                {
                    stack.Push(t.GetChild(i));
                }
            }

            if (verbose)
            {
                if (counter < (int)NeuronBones.NumOfBones)
                {
                    for (int i = 0; i < (int)NeuronBones.NumOfBones; ++i)
                    {
                        if (bones[i] == null)
                        {
                            Debug.LogError(string.Format("[NeuronSkeletonHelper] Bone \"{0}\" not bound", (NeuronBones)i));
                        }
                    }
                }
                else
                {
                    Debug.Log(string.Format("[NeuronSkeletonHelper] {0} bones bounded", counter), root);
                }
            }

            return(counter);
        }