Collison geo for multiple pieces

   3902   9   2
User Avatar
Member
897 posts
Joined: July 2018
Offline
New to Unity but not to Houdini. I want to create a fracture setup and get the pieces to get their respective collision geo. Naming them “piece0”, “piece0_collision_geo” etc doesn't work. It results in a object list as in the attachment, all collision geo merged to one piece and without any connection to their piece.

What's the correct way to do this?

Thanks!

Attachments:
unityEnginePartsAndCollisions.PNG (6.4 KB)

B.Henriksson, DICE
User Avatar
Member
402 posts
Joined: March 2013
Offline
You'll have to rename the pieces themselves to have the “_rendered_collision_geo” postfix in their name to in order to have the collision component be on the same GameObject as the part. There is no way currently in Unity to associate a collision geo with a different visible mesh on the same GameObject.
User Avatar
Member
897 posts
Joined: July 2018
Offline
I see.

I guess it's possible to hook up collision/render meshes by scripts?
B.Henriksson, DICE
User Avatar
Member
402 posts
Joined: March 2013
Offline
I guess. But do the collision meshes really need to be different than the visible piece meshes? Why can't the piece mesh also be the collision mesh?
User Avatar
Member
897 posts
Joined: July 2018
Offline
I aim to have much higher resloution on the render geo than the collision.
B.Henriksson, DICE
User Avatar
Member
402 posts
Joined: March 2013
Offline
Hmm, ya, that makes sense. Maybe submit an RFE to support@sidefx.com?
User Avatar
Member
897 posts
Joined: July 2018
Offline
Going to do some tests with scripts and obj networks first to understand the problem better.
B.Henriksson, DICE
User Avatar
Member
897 posts
Joined: July 2018
Offline
Ready to use script for this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class importHoudiniRBD : MonoBehaviour {
    static void attachCollider(Transform renderXform, Transform colliderXform){
        GameObject colObj = colliderXform.gameObject;
        GameObject renderObj = renderXform.gameObject;
        //print("Attaching collider: " + colObj.name);
        MeshCollider collider= colObj.AddComponent<MeshCollider>(); //add collider
        collider.convex = true;
        colliderXform.SetParent(renderXform);
        MeshRenderer colRend = colliderXform.GetComponent<MeshRenderer>();
        colRend.enabled = false;
        //check if allready processed
        if (null==renderObj.GetComponent<Rigidbody>())
        {
            renderObj.AddComponent<Rigidbody>();
        }
    }
    static int getChildIndexByName(Transform parent, string name, int numChildren)
    {
        int i =-1;
        for (int ii = 0; ii < numChildren; ii++)
        {
            string currentName = parent.GetChild(ii).name;
            //print(nameSplit[0]);
            if (currentName.Contains(name+'_'))//'_' and a suffix int is added to the Houdini name
            {
                return ii; //found the correct i for current piece
            }
        }
        return i;
    }
    public float test = 0;
    // Adds a sphere collider to all children that doesn't have a collider
    void Start()
    {
        //print("init" + this.gameObject.name);
        //print("Components: " + this.gameObject.GetComponents(typeof(GameObject)));
        Transform houObjNode = this.gameObject.transform;
        int nChildren = houObjNode.childCount; //keep track of number of children as this number gets obsolete as we parent objects 
        print (nChildren);
        int i = 0;
        int j = 0;
        int k = 0;
        
        //List<string> pieces = new List<string>();
        List<string> colliders = new List<string>();
        //get all pieces to attach
        foreach (Transform childXform in houObjNode)
        {
            //if (j > houObjNode.childCount) { continue; } //safety break
            if (childXform.name.Contains("Collider")) { colliders.Add(childXform.name); }
            //else { pieces.Add(childXform.name); }
        }
        
        foreach (string colliderName in colliders)
        {
            k++;
            if (k > 1000) { break;} //safety break
            i = -1;
            j = -1;
            string[] nameSplit = colliderName.Split(new string[] { "Collider" }, System.StringSplitOptions.None);
            string renderObjName = nameSplit[0];
            nameSplit = colliderName.Split(new string[] { "Collider" }, System.StringSplitOptions.None);
            
            //get transform indices
            i = getChildIndexByName(houObjNode, renderObjName, nChildren);
            //check if we found render piece
            if (i < 0)
            {
                Debug.LogError("Couldn't find render mesh: " + renderObjName);
                //print("i:" + i +", j:" + j);
                break;
            }
            
            j = getChildIndexByName(houObjNode, colliderName.Split('_')[0], nChildren);
            if (j < 0) {
                Debug.LogError("Can't process " + colliderName);
            }
            else {
                Transform renderXform = houObjNode.GetChild(i);
                Transform colXform = houObjNode.GetChild(j);
                attachCollider(renderXform, colXform);
                //print("Render object: " + renderXform);
                nChildren--; //reduce loop size
            }
        }
    }
}
Edited by kahuna031 - March 8, 2017 14:40:30
B.Henriksson, DICE
User Avatar
Member
402 posts
Joined: March 2013
Offline
Nice. I'll see if we can incorporate this into the plugin proper, but no promises.
User Avatar
Member
897 posts
Joined: July 2018
Offline
Please do. I'm updating the script above though, learned that parts can have multiple collision meshes so I'm changing the parenting order to allow that.
B.Henriksson, DICE
  • Quick Links