Exemple #1
0
    public void refresh(bool reload_asset)
    {
        if (prObjectControl == null)
        {
            Debug.LogError("Why is my object control null on a refresh?");
            return;
        }

        GameObject geo_node = gameObject;

        // Get Geo info.
        HAPI_GeoInfo geo_info = new HAPI_GeoInfo();

        HoudiniHost.getGeoInfo(prAssetId, prObjectId, prGeoId, out geo_info);

        if (geo_info.type == HAPI_GeoType.HAPI_GEOTYPE_INPUT)
        {
            return;
        }

        if (!reload_asset && !geo_info.hasGeoChanged && !geo_info.hasMaterialChanged)
        {
            return;
        }

        if (reload_asset || geo_info.type == HAPI_GeoType.HAPI_GEOTYPE_CURVE)
        {
            for (int i = 0; i < myParts.Count; ++i)
            {
                HoudiniAssetUtility.destroyGameObject(myParts[i]);
            }
            myParts.Clear();
        }

        if (reload_asset || geo_info.hasGeoChanged)
        {
            // Initialize our geo control.
            init(
                geo_info.nodeId, prGeoId, geo_info.name, (HAPI_GeoType)geo_info.type,
                geo_info.isEditable, geo_info.isDisplayGeo);

            // Set node name.
            geo_node.name = prGeoName;
        }

        if (!geo_info.isDisplayGeo &&
            (geo_info.type != HAPI_GeoType.HAPI_GEOTYPE_CURVE &&
             !myObjectControl.prAsset.prImportTemplatedGeos &&
             geo_info.isTemplated))
        {
            return;
        }

        if (geo_info.type == HAPI_GeoType.HAPI_GEOTYPE_CURVE)
        {
            createAndInitCurve(prNodeId, prObjectId, prGeoId, prIsEditable);
        }
        else
        {
            if (reload_asset || geo_info.hasGeoChanged)
            {
                // Add new geos as needed.
                while (myParts.Count < geo_info.partCount)
                {
                    myParts.Add(createPart(myParts.Count));
                }

                // Remove stale geos.
                while (myParts.Count > geo_info.partCount)
                {
                    HoudiniAssetUtility.destroyGameObject(myParts[geo_info.partCount]);
                    myParts.RemoveAt(geo_info.partCount);
                }
            }

            // Refresh all geos.
            for (int i = 0; i < myParts.Count; ++i)
            {
                myParts[i].GetComponent <HoudiniPartControl>().refresh(
                    reload_asset, geo_info.hasGeoChanged, geo_info.hasMaterialChanged);
            }

            // Handle Edit/Paint Nodes
#if !HAPI_PAINT_SUPPORT
            if (geo_info.type == HAPI_GeoType.HAPI_GEOTYPE_INTERMEDIATE)
            {
                // We are limited to using the first part, always.
                if (myGeoAttributeManager == null && myParts.Count > 0)
                {
                    const int part_id = 0;

                    GameObject         part_gameobject = myParts[part_id];
                    HoudiniPartControl part_control    = part_gameobject.GetComponent <HoudiniPartControl>();
                    MeshFilter         mesh_filter     = part_control.getOrCreateComponent <MeshFilter>();
                    MeshRenderer       mesh_renderer   = part_control.getOrCreateComponent <MeshRenderer>();
                    MeshCollider       mesh_collider   = part_control.getOrCreateComponent <MeshCollider>();
                    Mesh mesh = mesh_filter.sharedMesh;

                    myGeoAttributeManager = ScriptableObject.CreateInstance <HoudiniGeoAttributeManager>();
                    myGeoAttributeManager.init(mesh, mesh_renderer, mesh_collider, part_gameobject.transform);

                    // Fetch all point attributes.
                    string[] point_attribute_names = HoudiniHost.getAttributeNames(
                        prAssetId, prObjectId, prGeoId, part_id, HAPI_AttributeOwner.HAPI_ATTROWNER_POINT);

                    foreach (string point_attribute_name in point_attribute_names)
                    {
                        if (point_attribute_name == "P")
                        {
                            continue;
                        }

                        HAPI_AttributeInfo point_attribute_info = HoudiniHost.getAttributeInfo(
                            prAssetId, prObjectId, prGeoId, part_id, point_attribute_name,
                            HAPI_AttributeOwner.HAPI_ATTROWNER_POINT);

                        if (point_attribute_info.storage == HAPI_StorageType.HAPI_STORAGETYPE_INT)
                        {
                            int[] data = new int[0];
                            HoudiniAssetUtility.getAttribute(
                                prAssetId, prObjectId, prGeoId, part_id,
                                point_attribute_name,
                                ref point_attribute_info,
                                ref data,
                                HoudiniHost.getAttributeIntData);
                            HoudiniGeoAttribute attribute =
                                myGeoAttributeManager.createAttribute(point_attribute_name);
                            attribute.init(
                                mesh, point_attribute_name, HoudiniGeoAttribute.Type.INT,
                                point_attribute_info.tupleSize);
                            attribute.prOriginalAttributeOwner = HAPI_AttributeOwner.HAPI_ATTROWNER_POINT;

                            if (data.Length != attribute.prIntData.Length)
                            {
                                Debug.LogError("Size mis-match in paint tools.");
                            }
                            else
                            {
                                for (int i = 0; i < data.Length; ++i)
                                {
                                    attribute.prIntData[i] = data[i];
                                }
                            }
                        }
                        else if (point_attribute_info.storage == HAPI_StorageType.HAPI_STORAGETYPE_FLOAT)
                        {
                            int     tuple_size = point_attribute_info.tupleSize;
                            float[] data       = new float[0];
                            HoudiniAssetUtility.getAttribute(
                                prAssetId, prObjectId, prGeoId, part_id,
                                point_attribute_name,
                                ref point_attribute_info,
                                ref data,
                                HoudiniHost.getAttributeFloatData);
                            HoudiniGeoAttribute attribute =
                                myGeoAttributeManager.createAttribute(point_attribute_name);
                            attribute.init(
                                mesh, point_attribute_name, HoudiniGeoAttribute.Type.FLOAT,
                                tuple_size);
                            attribute.prOriginalAttributeOwner = HAPI_AttributeOwner.HAPI_ATTROWNER_POINT;

                            // Get Vertex list.
                            HAPI_PartInfo part_info = new HAPI_PartInfo();
                            HoudiniHost.getPartInfo(
                                prAssetId, prObjectId, prGeoId, part_id, out part_info);
                            int[] vertex_list = new int[part_info.vertexCount];
                            HoudiniAssetUtility.getArray4Id(
                                prAssetId, prObjectId, prGeoId, part_id, HoudiniHost.getVertexList,
                                vertex_list, part_info.vertexCount);

                            if (part_info.vertexCount * tuple_size != attribute.prFloatData.Length)
                            {
                                Debug.LogError("Size mis-match in paint tools.");
                            }
                            else
                            {
                                for (int i = 0; i < part_info.vertexCount; ++i)
                                {
                                    for (int tuple = 0; tuple < tuple_size; ++tuple)
                                    {
                                        attribute.prFloatData[i * tuple_size + tuple] =
                                            data[vertex_list[i] * tuple_size + tuple];
                                    }
                                }
                            }
                        }
                        else if (point_attribute_info.storage == HAPI_StorageType.HAPI_STORAGETYPE_STRING)
                        {
                        }
                    }
                }
            }
#endif // !HAPI_PAINT_SUPPORT

            // Handle script attaching.
            if (reload_asset && geo_info.partCount > 0)
            {
                HAPI_AttributeInfo script_attr_info = new HAPI_AttributeInfo("Unity_Script");
                int[] script_attr = new int[0];

                HoudiniAssetUtility.getAttribute(
                    prAssetId, prObjectId, prGeoId, 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_DETAIL)
                {
                    throw new HoudiniErrorIgnorable("I only understand Unity_Script as detail attributes!");
                }

                if (script_attr_info.exists && script_attr.Length > 0)
                {
                    string script_to_attach = HoudiniHost.getString(script_attr[0]);
                    HoudiniAssetUtility.attachScript(geo_node, script_to_attach);
                }
            }
        }
    }
	public bool refresh( bool reload_asset )
	{
		bool needs_recook = false;

		if ( prObjectControl == null )
		{
			Debug.LogError( "Why is my object control null on a refresh?" );
			return needs_recook;
		}

		GameObject geo_node = gameObject;

		// Get Geo info.
		HAPI_GeoInfo geo_info = new HAPI_GeoInfo();
		try
		{
			// If templated geos are off this will error out for templated
			// geos because they woudn't have cooked. But we still need to
			// get the geo info to see that this is a templated geo and skip it.
			HoudiniHost.getGeoInfo( prAssetId, prObjectId, prGeoId, out geo_info );
		}
		catch ( HoudiniErrorInvalidArgument ) {}

		if ( geo_info.type == HAPI_GeoType.HAPI_GEOTYPE_INPUT )
			return needs_recook;

		if ( geo_info.isTemplated && !prAsset.prImportTemplatedGeos && !geo_info.isEditable )
			return needs_recook;

		if ( !reload_asset && !geo_info.hasGeoChanged && !geo_info.hasMaterialChanged )
			return needs_recook;

		if ( reload_asset || geo_info.type == HAPI_GeoType.HAPI_GEOTYPE_CURVE )
		{
			for ( int i = 0; i < myParts.Count; ++i )
				HoudiniAssetUtility.destroyGameObject( myParts[ i ] );
			myParts.Clear();
		}

		if ( reload_asset || geo_info.hasGeoChanged )
		{
			// Initialize our geo control.
			init( 
				geo_info.nodeId, prGeoId, geo_info.name, (HAPI_GeoType) geo_info.type, 
				geo_info.isEditable, geo_info.isDisplayGeo );

			// Set node name.
			geo_node.name = prGeoName;
		}

		if ( !geo_info.isDisplayGeo && 
				(	geo_info.type != HAPI_GeoType.HAPI_GEOTYPE_CURVE && 
					!myObjectControl.prAsset.prImportTemplatedGeos && 
					geo_info.isTemplated ) )
		{
			return needs_recook;
		}

		if ( geo_info.type == HAPI_GeoType.HAPI_GEOTYPE_CURVE )
		{
			createAndInitCurve( prNodeId, prObjectId, prGeoId, prIsEditable );
			needs_recook = true;
		}
		else
		{
			if ( reload_asset || geo_info.hasGeoChanged )
			{
				// Add new geos as needed.
				while ( myParts.Count < geo_info.partCount )
					myParts.Add( createPart( myParts.Count ) );

				// Remove stale geos.
				while ( myParts.Count > geo_info.partCount )
				{
					HoudiniAssetUtility.destroyGameObject( myParts[ geo_info.partCount ] );
					myParts.RemoveAt( geo_info.partCount );
				}
			}
		
			// Refresh all geos.
			for ( int i = 0; i < myParts.Count; ++i )
				myParts[ i ].GetComponent< HoudiniPartControl >().refresh( 
					reload_asset, geo_info.hasGeoChanged, geo_info.hasMaterialChanged );

			// Handle Edit/Paint Nodes
			if ( geo_info.type == HAPI_GeoType.HAPI_GEOTYPE_INTERMEDIATE )
			{
				// Currently, we only support painting on the first part.
				const int part_id = 0;

				GameObject part_gameobject = myParts[ part_id ];
				HoudiniPartControl part_control = part_gameobject.GetComponent< HoudiniPartControl >();
				MeshFilter mesh_filter = part_control.getOrCreateComponent< MeshFilter >();
				MeshRenderer mesh_renderer = part_control.getOrCreateComponent< MeshRenderer >();
				MeshCollider mesh_collider = part_control.getOrCreateComponent< MeshCollider >();
				Mesh mesh = mesh_filter.sharedMesh;

				// We are limited to using the first part, always.
				if ( myGeoAttributeManager == null && myParts.Count > 0 )
				{
					if ( prAsset.prGeoAttributeManagerMap.contains( getRelativePath() ) )
					{
						myGeoAttributeManager = prAsset.prGeoAttributeManagerMap.get( getRelativePath() );
						myGeoAttributeManager.name = getAbsolutePath() + "/GeoAttributeManager";
						myGeoAttributeManager.reInit( mesh, mesh_renderer, mesh_collider, part_gameobject.transform );
					}
					else
					{
						myGeoAttributeManager = ScriptableObject.CreateInstance< HoudiniGeoAttributeManager >();
						myGeoAttributeManager.name = getAbsolutePath() + "/GeoAttributeManager";
						myGeoAttributeManager.init( mesh, mesh_renderer, mesh_collider, part_gameobject.transform );
						prAsset.prGeoAttributeManagerMap.add( getRelativePath(), myGeoAttributeManager );
					}

					// Sync the attributes and see if we need a recook.
					if ( myGeoAttributeManager.syncAttributes( prAssetId, prObjectId, prGeoId, part_id, mesh ) )
					{
						HoudiniAssetUtility.setMesh(
							prAssetId, prObjectId, prGeoId,
							ref mesh, part_control, myGeoAttributeManager );
						needs_recook = true;
					}
				}
				else
				{
					// Just sync the attributes but don't recook. Setting needs_recook to true here would
					// cause infinite cooking.
					myGeoAttributeManager.syncAttributes( prAssetId, prObjectId, prGeoId, part_id, mesh );
				}
			}

			// Handle script attaching.
			if ( reload_asset && geo_info.partCount > 0 )
			{
				HAPI_AttributeInfo script_attr_info = new HAPI_AttributeInfo( "Unity_Script" );
				int[] script_attr = new int[ 0 ];
			
				HoudiniAssetUtility.getAttribute( 
					prAssetId, prObjectId, prGeoId, 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_DETAIL )
					throw new HoudiniErrorIgnorable( "I only understand Unity_Script as detail attributes!" );
			
				if ( script_attr_info.exists && script_attr.Length > 0 )
				{
					string script_to_attach = HoudiniHost.getString( script_attr[ 0 ] );
					HoudiniAssetUtility.attachScript( geo_node, script_to_attach );
				}
			}
		}

		return needs_recook;
	}