Beispiel #1
0
    public void pinObject(GameObject pin_object, bool pin)
    {
        HoudiniInstance instance = pin_object.GetComponent <HoudiniInstance>();

        if (instance == null)
        {
            //The user might be moving the part instead of the object, so we should
            //try to pin that in this case.
            Transform parent = pin_object.transform.parent;
            while (parent != null)
            {
                instance = parent.gameObject.GetComponent <HoudiniInstance>();
                if (instance != null)
                {
                    break;
                }
                parent = parent.parent;
            }

            if (instance == null)
            {
                return;
            }
        }

        if (!pin)
        {
            unPinInstance(instance.prInstancePointNumber);
        }
        else
        {
            Transform game_object_xform = pin_object.transform;

            HoudiniInstancerOverrideInfo override_info =
                ScriptableObject.CreateInstance <HoudiniInstancerOverrideInfo>();

            override_info.translate = game_object_xform.position;
            override_info.rotate    = game_object_xform.rotation.eulerAngles;

            Vector3 scale = game_object_xform.localScale;

            Transform parent = game_object_xform.parent;
            while (parent != null)
            {
                scale.x *= parent.localScale.x;
                scale.y *= parent.localScale.y;
                scale.z *= parent.localScale.z;
                parent   = parent.parent;
            }

            override_info.scale = scale;
            override_info.objectToInstantiate     = instance.prObjectToInstantiate;
            override_info.objectToInstantiatePath = getGameObjectPath(instance.prObjectToInstantiate);
            override_info.instancePointNumber     = instance.prInstancePointNumber;

            pinInstance(override_info);
        }
    }
Beispiel #2
0
    private HoudiniInstance findInstanceControlInParent()
    {
        Transform parent = myPartControl.gameObject.transform.parent;

        while (parent != null)
        {
            HoudiniInstance instance = parent.gameObject.GetComponent <HoudiniInstance>();
            if (instance != null)
            {
                return(instance);
            }
            parent = parent.parent;
        }
        return(null);
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Public

    public void OnSceneGUI()
    {
        HoudiniInstance instance = target as HoudiniInstance;


        HoudiniInstancer instancer = instance.prInstancer;

        if (instancer == null)
        {
            return;
        }

        bool is_overridden = instancer.isPointOverridden(instance.prInstancePointNumber);

        if (is_overridden)
        {
            instancer.drawPin(instance.prInstancePointNumber);
        }

        Event curr_event = Event.current;

        if (
            curr_event.isMouse && curr_event.type == EventType.MouseDown &&
            HoudiniHost.prAutoPinInstances)
        {
            instance.prTransformChanged = false;
        }
        else if (
            curr_event.isMouse && curr_event.type == EventType.MouseUp &&
            HoudiniHost.prAutoPinInstances && instance.prTransformChanged)
        {
            instancer.pinObject(instance.gameObject, true);
            instance.prTransformChanged = false;
            Repaint();
        }
    }
Beispiel #4
0
    private void instanceObject(GameObject objToInstantiate,
                                Vector3 pos,
                                Vector3 euler,
                                int point_index,
                                bool scale_exists,
                                Vector3 scale,
                                bool attach_script_exists,
                                string attach_script)
    {
        if (objToInstantiate == null)
        {
            Debug.LogError("No object to instantiate for instancer '" + this.name + "' and point index " + point_index + "!");
            return;
        }

        GameObject obj = null;

#if UNITY_EDITOR
        GameObject user_instance =
            prPersistentData.getUserObjToInstantiateFromName(objToInstantiate.name, point_index);

        // See if object is a prefab instance.
        var prefab_parent = PrefabUtility.GetPrefabParent(user_instance) as GameObject;
        if (prefab_parent != null)
        {
            user_instance = prefab_parent;
        }

        if (user_instance != null)
        {
            obj = PrefabUtility.InstantiatePrefab(user_instance) as GameObject;
            if (obj == null)
            {
                bool liveTransformPropagationSetting = false;
                bool syncAssetTransformSetting       = false;
                bool enableCooking = true;

                HoudiniAsset hapi_asset = user_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;
                }

                obj = Instantiate(user_instance, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
                HoudiniAsset hapi_asset_on_clone = obj.GetComponent <HoudiniAsset>();
                if (hapi_asset_on_clone != null)
                {
                    Destroy(hapi_asset_on_clone);
                }

                if (hapi_asset != null)
                {
                    hapi_asset.prTransformChangeTriggersCooks = liveTransformPropagationSetting;
                    hapi_asset.prPushUnityTransformToHoudini  = syncAssetTransformSetting;
                    hapi_asset.prEnableCooking = enableCooking;
                }
            }

            HoudiniInstance instance = obj.AddComponent <HoudiniInstance>();
            instance.prInstancePointNumber = point_index;
            instance.prObjectToInstantiate = user_instance;
            instance.prInstancer           = this;

            obj.transform.localPosition = pos;

            // Rotation order is important here. Bug: #63304
            Quaternion user_angle_rot = user_instance.transform.localRotation;
            Quaternion offset_rot     = Quaternion.Euler(prPersistentData.rotationalOffset);
            Quaternion instance_rot   = Quaternion.Euler(euler);
            obj.transform.localRotation = instance_rot * offset_rot * user_angle_rot;

            if (scale_exists)
            {
                obj.transform.localScale = new Vector3(
                    prPersistentData.scaleOffset.x * user_instance.transform.localScale.x * scale.x,
                    prPersistentData.scaleOffset.y * user_instance.transform.localScale.y * scale.y,
                    prPersistentData.scaleOffset.z * user_instance.transform.localScale.z * scale.z);
            }
            else
            {
                obj.transform.localScale = new Vector3(
                    prPersistentData.scaleOffset.x * user_instance.transform.localScale.x,
                    prPersistentData.scaleOffset.y * user_instance.transform.localScale.y,
                    prPersistentData.scaleOffset.z * user_instance.transform.localScale.z);
            }
        }
        else
#endif // UNITY_EDITOR
        {
            obj = Instantiate(objToInstantiate, pos, Quaternion.Euler(euler)) as GameObject;

            HoudiniInstance instance = obj.AddComponent <HoudiniInstance>();

            instance.prInstancePointNumber = point_index;
            instance.prObjectToInstantiate = objToInstantiate;
            instance.prInstancer           = this;

            if (scale_exists)
            {
                if (Mathf.Approximately(0.0f, scale.x) ||
                    Mathf.Approximately(0.0f, scale.y) ||
                    Mathf.Approximately(0.0f, scale.z))
                {
                    Debug.LogWarning("Instance " + point_index + ": Scale has a zero component!");
                }
                obj.transform.localScale = scale;
            }

            // The original object is probably set to be invisible because it just contains
            // the raw geometry with no transforms applied. We need to set the newly instanced
            // object's childrens' mesh renderers to be enabled otherwise the instanced
            // objects will also be invisible. :)
            MeshCollider[] mesh_colliders = obj.GetComponentsInChildren <MeshCollider>();
            foreach (MeshCollider mesh_collider in mesh_colliders)
            {
                mesh_collider.enabled = true;
            }
            MeshRenderer[] mesh_renderers = obj.GetComponentsInChildren <MeshRenderer>();
            foreach (MeshRenderer mesh_renderer in mesh_renderers)
            {
                mesh_renderer.enabled = true;
            }
        }

        obj.transform.parent = transform;

        if (attach_script_exists)
        {
            HoudiniAssetUtility.attachScript(obj, attach_script);
        }
    }
Beispiel #5
0
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Public

    public void OnSceneGUI()
    {
        HoudiniPartControl control = target as HoudiniPartControl;

        if (control.prShowPointNumbers)
        {
            // Get position attributes.
            HAPI_AttributeInfo pos_attr_info = new HAPI_AttributeInfo(HoudiniConstants.HAPI_ATTRIB_POSITION);
            float[]            pos_attr      = new float[0];
            HoudiniAssetUtility.getAttribute(
                myPartControl.prAssetId, myPartControl.prObjectId, myPartControl.prGeoId,
                myPartControl.prPartId, HoudiniConstants.HAPI_ATTRIB_POSITION,
                ref pos_attr_info, ref pos_attr, HoudiniHost.getAttributeFloatData);
            if (!pos_attr_info.exists)
            {
                throw new HoudiniError("No position attribute found.");
            }

            int point_count = pos_attr.Length / 3;
            // Determine which control point was pressed for modification.
            for (int i = 0; i < point_count; ++i)
            {
                Vector3 position = new Vector3(
                    -pos_attr[i * 3 + 0], pos_attr[i * 3 + 1], pos_attr[i * 3 + 2]);
                position = myPartControl.transform.TransformPoint(position);

                Color    original_color = GUI.color;
                GUIStyle style          = new GUIStyle(GUI.skin.label);

                GUI.color       = Color.yellow;
                style.fontStyle = FontStyle.Normal;
                style.fontSize  = 12;
                Handles.Label(position, new GUIContent("" + i), style);

                GUI.color = original_color;
            }
        }

        /*
         * {
         *      try
         *      {
         *              HAPI_AttributeInfo pos_attr_info = new HAPI_AttributeInfo( HoudiniConstants.HAPI_ATTRIB_POSITION );
         *              float[] pos_attr = new float[ 0 ];
         *              HoudiniAssetUtility.getAttribute(
         *                      myPartControl.prAssetId, myPartControl.prObjectId, myPartControl.prGeoId,
         *                      myPartControl.prPartId, HoudiniConstants.HAPI_ATTRIB_POSITION,
         *                      ref pos_attr_info, ref pos_attr, HoudiniHost.getAttributeFloatData );
         *              if ( !pos_attr_info.exists )
         *                      throw new HoudiniError( "No position attribute found." );
         *
         *              bool[] membership = HoudiniHost.getGroupMembership(
         *                      myPartControl.prAssetId, myPartControl.prObjectId, myPartControl.prGeoId,
         *                      myPartControl.prPartId, HAPI_GroupType.HAPI_GROUPTYPE_POINT, "exteriorPoints" );
         *
         *              int point_count = pos_attr.Length / 3;
         *
         *              if ( membership.Length != point_count )
         *                      Debug.LogError( "WTF" );
         *
         *              // Determine which control point was pressed for modification.
         *              for ( int i = 0; i < point_count; ++i )
         *              {
         *                      if ( membership[ i ] )
         *                      {
         *                              Vector3 position = new Vector3( -pos_attr[ i * 3 + 0 ], pos_attr[ i * 3 + 1 ], pos_attr[ i * 3 + 2 ] );
         *                              Handles.Label( position, new GUIContent("" + i ) );
         *                      }
         *              }
         *      }
         *      catch
         *      {}
         *
         *      try
         *      {
         *              bool[] membership = HoudiniHost.getGroupMembership(
         *                      myPartControl.prAssetId, myPartControl.prObjectId, myPartControl.prGeoId,
         *                      myPartControl.prPartId, HAPI_GroupType.HAPI_GROUPTYPE_PRIM, "LG_4" );
         *
         *              Mesh mesh = myPartControl.GetComponent< MeshFilter >().sharedMesh;
         *              if ( membership.Length != mesh.triangles.Length / 3 )
         *                      Debug.LogError( "WTF" );
         *
         *              for ( int i = 0; i < mesh.triangles.Length / 3; ++i )
         *              {
         *                      if ( membership[ i ] )
         *                      {
         *                              Vector3[] vects = new Vector3[ 4 ];
         *                              vects[ 0 ] = mesh.vertices[ mesh.triangles[ i * 3 + 0 ] ];
         *                              vects[ 1 ] = mesh.vertices[ mesh.triangles[ i * 3 + 0 ] ];
         *                              vects[ 2 ] = mesh.vertices[ mesh.triangles[ i * 3 + 1 ] ];
         *                              vects[ 3 ] = mesh.vertices[ mesh.triangles[ i * 3 + 2 ] ];
         *                              Handles.DrawSolidRectangleWithOutline( vects, Color.red, Color.yellow );
         *                      }
         *              }
         *      }
         *      catch
         *      {}
         * }*/

        HoudiniInstance instance = findInstanceControlInParent();

        if (instance == null)
        {
            return;
        }

        bool is_overridden = instance.prInstancer.isPointOverridden(instance.prInstancePointNumber);

        if (is_overridden)
        {
            instance.prInstancer.drawPin(instance.prInstancePointNumber);
        }

        Event curr_event = Event.current;

        if (
            curr_event.isMouse && curr_event.type == EventType.MouseDown &&
            HoudiniHost.prAutoPinInstances)
        {
            control.prTransformChanged = false;
        }
        else if (
            curr_event.isMouse && curr_event.type == EventType.MouseUp &&
            HoudiniHost.prAutoPinInstances && control.prTransformChanged)
        {
            instance.prInstancer.pinObject(control.gameObject, true);
            control.prTransformChanged = false;
            Repaint();
        }
    }