示例#1
0
    public void unPinAllInstances()
    {
        HoudiniInstancerPersistentData persistent_data = prPersistentData;

        Undo.RecordObject(persistent_data, "Houdini Unpin All Instances");
        persistent_data.overriddenInstances.Clear();
    }
示例#2
0
    private void updateUniqueInstantiatedNames(List <string> unique_instantiated_names)
    {
        HoudiniInstancerPersistentData instancer_data = prPersistentData;

        List <string> existing_unique_names = instancer_data.uniqueNames;
        List <string> names_to_add          = new List <string>();

        for (int ii = 0; ii < unique_instantiated_names.Count; ii++)
        {
            string unique_name = unique_instantiated_names[ii];
            bool   found       = false;
            for (int jj = 0; jj < existing_unique_names.Count; jj++)
            {
                string existing_unique_name = existing_unique_names[jj];
                if (existing_unique_name == unique_name)
                {
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                names_to_add.Add(unique_name);
            }
        }

        foreach (string name_to_add in names_to_add)
        {
            instancer_data.uniqueNames.Add(name_to_add);
            instancer_data.numObjsToInstantiate.Add(1);
            instancer_data.objsToInstantiate.Add(null);
            instancer_data.recalculateVariations.Add(true);
        }
    }
示例#3
0
    public void drawAllPins()
    {
        HoudiniInstancerPersistentData persistent_data = prPersistentData;

        foreach (HoudiniInstancerOverrideInfo override_info in persistent_data.overriddenInstances)
        {
            drawPin(override_info);
        }
    }
示例#4
0
    public void drawPin(int point_index)
    {
        HoudiniInstancerPersistentData persistent_data = prPersistentData;

        foreach (HoudiniInstancerOverrideInfo override_info in persistent_data.overriddenInstances)
        {
            if (override_info.instancePointNumber == point_index)
            {
                drawPin(override_info);
            }
        }
    }
示例#5
0
    public bool isPointOverridden(int point_index)
    {
        HoudiniInstancerPersistentData persistent_data = prPersistentData;

        foreach (HoudiniInstancerOverrideInfo override_info in persistent_data.overriddenInstances)
        {
            if (override_info.instancePointNumber == point_index)
            {
                return(true);
            }
        }

        return(false);
    }
示例#6
0
    private void instanceOverriddenObjects(int total_points, List <int> exclusion_list)
    {
        int current_max_point_index = total_points - 1;

        HoudiniInstancerPersistentData data = prPersistentData;

        for (int ii = 0; ii < data.overriddenInstances.Count; ii++)
        {
            HoudiniInstancerOverrideInfo override_info = data.overriddenInstances[ii];

            Vector3 pos = override_info.translate;

            Vector3 euler = override_info.rotate;

            Vector3 scale = override_info.scale;

            GameObject object_to_instantiate = override_info.objectToInstantiate;
            if (object_to_instantiate == null)
            {
                object_to_instantiate = GameObject.Find(override_info.objectToInstantiatePath);
                if (object_to_instantiate == null)
                {
                    data.overriddenInstances.RemoveAt(ii);
                    continue;
                }
            }

            instanceObject(object_to_instantiate,
                           pos, euler, override_info.instancePointNumber, true, scale, false, "");

            if (override_info.instancePointNumber < total_points)
            {
                exclusion_list.Add(override_info.instancePointNumber);
            }
            else
            {
                if (current_max_point_index >= 0)
                {
                    exclusion_list.Add(current_max_point_index);
                    current_max_point_index--;
                }
            }
        }
    }
示例#7
0
    public bool pinInstance(HoudiniInstancerOverrideInfo info)
    {
        HoudiniInstancerPersistentData persistent_data = prPersistentData;

        Undo.RecordObject(persistent_data, "Houdini Pin Instance");
        int ii = 0;

        for (ii = 0; ii < persistent_data.overriddenInstances.Count; ii++)
        {
            HoudiniInstancerOverrideInfo override_info = persistent_data.overriddenInstances[ii];
            if (override_info.instancePointNumber == info.instancePointNumber)
            {
                persistent_data.overriddenInstances.RemoveAt(ii);
                break;
            }
        }

        persistent_data.overriddenInstances.Add(info);
        return(true);
    }
    public void updateInstancerData(HoudiniInstancer instancer)
    {
        bool foundInstancer = false;

        foreach (HoudiniInstancerPersistentData persistent_data in myInstancersPersistentData)
        {
            if (persistent_data.instancerName == instancer.name)
            {
                foundInstancer = true;
                break;
            }
        }

        if (!foundInstancer)
        {
            HoudiniInstancerPersistentData instancer_data
                = ScriptableObject.CreateInstance <HoudiniInstancerPersistentData>();
            instancer_data.instancerName = instancer.name;
            myInstancersPersistentData.Add(instancer_data);
        }
    }
示例#9
0
    public void unPinInstance(int point_index)
    {
        HoudiniInstancerPersistentData persistent_data = prPersistentData;

        Undo.RecordObject(persistent_data, "Houdini Unpin Instance");
        int index_to_remove = -1;

        for (int ii = 0; ii < persistent_data.overriddenInstances.Count; ii++)
        {
            HoudiniInstancerOverrideInfo override_info = persistent_data.overriddenInstances[ii];
            if (override_info.instancePointNumber == point_index)
            {
                index_to_remove = ii;
                break;
            }
        }

        if (index_to_remove >= 0)
        {
            persistent_data.overriddenInstances.RemoveAt(index_to_remove);
        }
    }
示例#10
0
    public void instanceObjects(HoudiniProgressBar progress_bar)
    {
        try
        {
            destroyChildren();

            HAPI_ObjectInfo object_info = HoudiniHost.getObjectInfo(prObjectId);

            // Get Detail info.
            HAPI_GeoInfo geo_info = HoudiniHost.getDisplayGeoInfo(prObjectId);
            if (geo_info.partCount == 0)
            {
                return;
            }

            cacheNumInstances();

            HAPI_Transform[] instance_transforms = new HAPI_Transform[myNumInstances];
            Utility.getArray2Id(
                geo_info.nodeId, HAPI_RSTOrder.HAPI_SRT,
                HoudiniHost.getInstanceTransforms, instance_transforms, myNumInstances);

            // Get scale point attributes.
            HAPI_AttributeInfo scale_attr_info = new HAPI_AttributeInfo("scale");
            float[]            scale_attr      = new float[0];
            Utility.getAttribute(
                geo_info.nodeId, 0, "scale", ref scale_attr_info, ref scale_attr, HoudiniHost.getAttributeFloatData);

            if (scale_attr_info.exists && scale_attr_info.owner != HAPI_AttributeOwner.HAPI_ATTROWNER_POINT)
            {
                throw new HoudiniErrorIgnorable("I only understand scale as point attributes!");
            }

            if (scale_attr_info.exists && scale_attr.Length != myNumInstances * 3)
            {
                throw new HoudiniError(
                          "Unexpected scale array length found for asset: " + prAsset.prAssetId + "!\n" +
                          "Expected length of: " + myNumInstances * 3 + " but given: " + scale_attr.Length);
            }

            HAPI_AttributeInfo script_attr_info = new HAPI_AttributeInfo("Unity_Script");
            int[] script_attr = new int[0];
            Utility.getAttribute(
                geo_info.nodeId, 0, "Unity_Script", ref script_attr_info,
                ref script_attr, HoudiniHost.getAttributeStringData);

            if (script_attr_info.exists && script_attr_info.owner != HAPI_AttributeOwner.HAPI_ATTROWNER_POINT)
            {
                throw new HoudiniErrorIgnorable("I only understand Unity_Script as point attributes!");
            }

            if (script_attr_info.exists && script_attr.Length != myNumInstances)
            {
                throw new HoudiniError("Unexpected Unity_Script array length found for asset: " + prAsset.prAssetId + "!");
            }

            int[] instance_attr = null;
            int[] name_attr     = null;
            getInstanceAndNameAttrs(out instance_attr, out name_attr);

            progress_bar.prTotal = myNumInstances;

            List <int> exclusion_list = new List <int>();
            instanceOverriddenObjects(myNumInstances, exclusion_list);

            List <string> unique_instantiated_names        = new List <string>();
            HoudiniInstancerPersistentData persistent_data = prPersistentData;
            if (persistent_data.variationChoice.Count != myNumInstances)
            {
                if (myNumInstances > persistent_data.variationChoice.Count)
                {
                    int difference = myNumInstances - persistent_data.variationChoice.Count;
                    for (int ii = 0; ii < difference; ii++)
                    {
                        persistent_data.variationChoice.Add(-1);
                    }
                }
                else
                {
                    int difference = persistent_data.variationChoice.Count - myNumInstances;
                    persistent_data.variationChoice.RemoveRange(
                        persistent_data.variationChoice.Count - difference, difference);
                }
            }

            bool liveTransformPropagationSetting = false;
            bool syncAssetTransformSetting       = false;
            bool enableCooking = true;
            for (int i = 0; i < myNumInstances; ++i)
            {
                if (exclusion_list.Contains(i))
                {
                    continue;
                }

                GameObject obj_to_instance = null;

                if (instance_attr.Length > 0 || name_attr.Length > 0)
                {
                    if (name_attr.Length > 0)
                    {
                        string obj_name     = HoudiniHost.getString(name_attr[i]);
                        int    object_index = prAsset.findObjectByName(obj_name);

                        if (object_index >= 0)
                        {
                            obj_to_instance = prAsset.prGameObjects[object_index];
                        }
                        else
                        {
                            obj_to_instance = prAsset.findPartByName(obj_name, true);
                        }

                        if (obj_to_instance == null)
                        {
                            obj_to_instance = GameObject.Find(obj_name);
                        }
                    }
                    else
                    {
                        string    instanceObjectPath = HoudiniHost.getString(instance_attr[i]);
                        string [] pathItems          = instanceObjectPath.Split('/');
                        string    instanceObjectName = pathItems[pathItems.Length - 1];

                        int objectIndex = prAsset.findObjectByName(instanceObjectName);
                        if (objectIndex >= 0)
                        {
                            obj_to_instance = prAsset.prGameObjects[objectIndex];
                        }
                        else
                        {
                            obj_to_instance = GameObject.Find(instanceObjectName);
                        }
                    }

                    if (obj_to_instance != null)
                    {
                        HoudiniAsset hapi_asset = obj_to_instance.GetComponent <HoudiniAsset>();
                        if (hapi_asset != null)
                        {
                            liveTransformPropagationSetting = hapi_asset.prTransformChangeTriggersCooks;
                            syncAssetTransformSetting       = hapi_asset.prPushUnityTransformToHoudini;
                            enableCooking = hapi_asset.prEnableCooking;
                            hapi_asset.prTransformChangeTriggersCooks = false;
                            hapi_asset.prPushUnityTransformToHoudini  = false;
                            hapi_asset.prEnableCooking = false;
                        }
                    }
                }
                else if (object_info.objectToInstanceId >= 0)
                {
                    int object_to_instance_idx = prAsset.findObjectByNodeId(object_info.objectToInstanceId);
                    if (object_to_instance_idx >= 0)
                    {
                        obj_to_instance = prAsset.prGameObjects[object_to_instance_idx];
                    }
                }

                if (obj_to_instance != null)
                {
                    // Set progress bar information.
                    progress_bar.prCurrentValue = i;
                    progress_bar.prMessage      = "Instancing: " + obj_to_instance.name + " (" + i + " of " + myNumInstances + ")";
                    progress_bar.displayProgressBar();

                    if (!unique_instantiated_names.Contains(obj_to_instance.name))
                    {
                        unique_instantiated_names.Add(obj_to_instance.name);
                    }

                    Vector3 pos = new Vector3();

                    // Apply object transforms.
                    //
                    // Axis and Rotation conversions:
                    // Note that Houdini's X axis points in the opposite direction that Unity's does.  Also, Houdini's
                    // rotation is right handed, whereas Unity is left handed.  To account for this, we need to invert
                    // the x coordinate of the translation, and do the same for the rotations (except for the x rotation,
                    // which doesn't need to be flipped because the change in handedness AND direction of the left x axis
                    // causes a double negative - yeah, I know).

                    pos[0] = -instance_transforms[i].position[0];
                    pos[1] = instance_transforms[i].position[1];
                    pos[2] = instance_transforms[i].position[2];

                    Quaternion quat = new Quaternion(instance_transforms[i].rotationQuaternion[0],
                                                     instance_transforms[i].rotationQuaternion[1],
                                                     instance_transforms[i].rotationQuaternion[2],
                                                     instance_transforms[i].rotationQuaternion[3]);

                    Vector3 euler = quat.eulerAngles;
                    euler.y = -euler.y;
                    euler.z = -euler.z;

                    Vector3 scale = new Vector3(instance_transforms[i].scale[0],
                                                instance_transforms[i].scale[1],
                                                instance_transforms[i].scale[2]);



                    Matrix4x4 local_mat = new Matrix4x4();
                    local_mat.SetTRS(pos, Quaternion.Euler(euler), scale);

                    // TODO: Now this *should* be the transform.localToWorldMatrix
                    // but for some reason, after a scene load, we pick up compensating
                    // factors in the local transform that cancel out the transform on the
                    // asset.  For now just use the asset's transform as the parent matrix.
                    Matrix4x4 parent_mat = prAsset.transform.localToWorldMatrix;
                    Matrix4x4 global_mat = parent_mat * local_mat;


                    euler = HoudiniAssetUtility.getQuaternion(global_mat).eulerAngles;
                    pos   = HoudiniAssetUtility.getPosition(global_mat);
                    scale = HoudiniAssetUtility.getScale(global_mat);


                    //mat.SetTRS( pos,

                    string script_to_attach = "";
                    if (script_attr_info.exists)
                    {
                        script_to_attach = HoudiniHost.getString(script_attr[i]);
                    }
                    instanceObject(obj_to_instance,
                                   pos,
                                   euler,
                                   i,
                                   scale_attr_info.exists,
                                   scale,
                                   script_attr_info.exists,
                                   script_to_attach);


                    HoudiniAsset hapi_asset = obj_to_instance.GetComponent <HoudiniAsset>();
                    if (hapi_asset != null)
                    {
                        hapi_asset.prTransformChangeTriggersCooks = liveTransformPropagationSetting;
                        hapi_asset.prPushUnityTransformToHoudini  = syncAssetTransformSetting;
                        hapi_asset.prEnableCooking = enableCooking;
                    }
                }
            }

            updateUniqueInstantiatedNames(unique_instantiated_names);
        }
        catch (HoudiniError error)
        {
            Debug.LogWarning(error.ToString());
            return;
        }
    }
示例#11
0
    public bool hasOverriddenInstances()
    {
        HoudiniInstancerPersistentData persistent_data = prPersistentData;

        return(persistent_data.overriddenInstances.Count > 0);
    }
示例#12
0
    private void generateAssetInstanceControls()
    {
        HoudiniInstancerManager instancer_manager = myAssetOTL.gameObject.GetComponent <HoudiniInstancerManager>();

        if (instancer_manager == null)
        {
            return;
        }

        List <HoudiniInstancerPersistentData> instancer_persistent_data = instancer_manager.prInstancerPersistentData;

        HoudiniInstancer[] instancers = myAssetOTL.gameObject.GetComponentsInChildren <HoudiniInstancer>();

        foreach (HoudiniInstancer instancer in instancers)
        {
            HoudiniInstancerPersistentData persistent_data = null;

            for (int ii = 0; ii < instancer_persistent_data.Count; ii++)
            {
                HoudiniInstancerPersistentData data = instancer_persistent_data[ii];
                if (data.instancerName == instancer.name)
                {
                    persistent_data = data;
                    break;
                }
            }

            if (persistent_data == null)
            {
                Debug.LogError("Can't find persistent data for instancer: " + instancer.name);
                continue;
            }

            Undo.RecordObject(persistent_data, "Houdini Instancer Change");

            persistent_data.showInstancerGUI = HoudiniGUI.foldout(
                persistent_data.instancerName, persistent_data.showInstancerGUI, true);
            if (persistent_data.showInstancerGUI)
            {
                bool changed = false;

                {
                    Vector3 dummy = new Vector3();
                    changed |= HoudiniGUI.floatField(
                        "RotationOffset", "Rotation Offset",
                        ref persistent_data.rotationalOffset, null, ref dummy);

                    changed |= HoudiniGUI.floatField(
                        "ScaleOffset", "Scale Offset", ref persistent_data.scaleOffset, null, ref dummy);

                    List <string> unique_names = persistent_data.uniqueNames;

                    for (int ii = 0; ii < unique_names.Count; ii++)
                    {
                        string instanced_name = unique_names[ii];
                        int    base_index     = persistent_data.baseIndex(ii);



                        for (int jj = 0; jj < persistent_data.numObjsToInstantiate[ii]; jj++)
                        {
                            Object obj = (Object)persistent_data.objsToInstantiate[base_index + jj];

                            GUILayout.BeginHorizontal();



                            string label = "";
                            if (jj == 0)
                            {
                                label = instanced_name;
                            }

                            changed |= HoudiniGUI.objectField(
                                "object_to_instantiate", label, ref obj, typeof(GameObject));

                            if (changed)
                            {
                                persistent_data.objsToInstantiate[base_index + jj] = (GameObject)obj;
                            }

                            if (GUILayout.Button("+"))
                            {
                                persistent_data.objsToInstantiate.Insert
                                    (base_index + jj, null);
                                persistent_data.numObjsToInstantiate[ii]++;
                                persistent_data.recalculateVariations[ii] = true;
                                changed = true;
                                break;
                            }

                            if (GUILayout.Button("-"))
                            {
                                if (persistent_data.numObjsToInstantiate[ii] == 1)
                                {
                                    persistent_data.objsToInstantiate[base_index] = null;
                                }
                                else
                                {
                                    persistent_data.objsToInstantiate.RemoveAt(base_index + jj);
                                    persistent_data.numObjsToInstantiate[ii]--;
                                }
                                persistent_data.recalculateVariations[ii] = true;
                                changed = true;
                                break;
                            }

                            GUILayout.EndHorizontal();
                        }
                    }

                    if (GUILayout.Button("Recalculate Variations"))
                    {
                        for (int ii = 0; ii < unique_names.Count; ii++)
                        {
                            persistent_data.recalculateVariations[ii] = true;
                        }
                        changed = true;
                    }
                }


                if (instancer.hasOverriddenInstances())
                {
                    if (GUILayout.Button("UnPin All Instances"))
                    {
                        instancer.unPinAllInstances();
                        changed = true;
                    }
                }

                if (changed)
                {
                    HoudiniProgressBar progress_bar = new HoudiniProgressBar();
                    instancer.instanceObjects(progress_bar);
                    progress_bar.clearProgressBar();

                    for (int ii = 0; ii < persistent_data.recalculateVariations.Count; ii++)
                    {
                        persistent_data.recalculateVariations[ii] = false;
                    }
                }
            }

            EditorGUILayout.Separator();
        }
    }