Skip to content

Commit

Permalink
h20 680 (4-19-24)
Browse files Browse the repository at this point in the history
Changes from workstation sideeffects#2
  • Loading branch information
atcarter714 committed Apr 26, 2024
1 parent 1760ec1 commit 746fc7c
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 228 deletions.
2 changes: 2 additions & 0 deletions Plugins/HoudiniEngineUnity/Scripts/Asset/HEU_AssetUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ static HEU_AssetUpdater( ) {
}

static void OnBeforeAssemblyReload( ) {
HEU_Logger.Log( $"{nameof(HEU_AssetUpdater)}.{nameof(OnBeforeAssemblyReload)} :: " +
$"Rebuilding assemblies ..." ) ;
// Save the session before code domain reload so
// that the session file has the latest session state.
HEU_SessionManager.SaveAllSessionData( ) ;
Expand Down
34 changes: 19 additions & 15 deletions Plugins/HoudiniEngineUnity/Scripts/Asset/HEU_HoudiniAssetRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,32 @@
[assembly: InternalsVisibleTo("HoudiniEngineUnityPlayModeTests")]
#endif

namespace HoudiniEngineUnity
{
namespace HoudiniEngineUnity {
/// <summary>
/// The root object of a Houdini Engine asset.
/// Used for organizing hierarchy, and more importantly displaying custom UI.
/// </summary>
[SelectionBase]
[ExecuteInEditMode] // Needed to get OnDestroy callback when deleted in Editor
[SelectionBase, ExecuteInEditMode]
public class HEU_HoudiniAssetRoot: MonoBehaviour {
static readonly HashSet< HEU_HoudiniAssetRoot > _allRootAssets = new( ) ;

// Reference to the actual Houdini Engine asset gamebobject which contains
// all the data and logic to work with Houdini Engine
[SerializeField] internal HEU_HoudiniAsset _houdiniAsset ;
public HEU_HoudiniAsset HoudiniAsset => _houdiniAsset ;
[SerializeField] internal HEU_HoudiniAsset? _houdiniAsset ;
public HEU_HoudiniAsset? HoudiniAsset => _houdiniAsset ;

[SerializeField] internal List< GameObject > _bakeTargets = new( ) ;
public List< GameObject > BakeTargets => _bakeTargets ;


void OnEnable( ) => _allRootAssets.Add( this ) ;

/// <summary>Callback when asset is deleted. Removes assset from Houdini session if in Editor.</summary>
void OnDestroy( ) {
if ( _houdiniAsset && _houdiniAsset.PauseCooking ) return ;

_allRootAssets.Remove( this ) ;
if ( _houdiniAsset && _houdiniAsset!.PauseCooking ) return ;

// Destroy the asset from session or permanently.
// The following checks make sure to only delete if the scene is closing,
// or asset has been user deleted.
Expand All @@ -66,7 +71,7 @@ void OnDestroy( ) {
// Delete mesh data if this asset hasn't been saved and it is a user invoked delete event.
// Otherwise just remove from session.
// TODO: for saved assets, we need to handle case where user deletes but does not save scene after
if ( !_houdiniAsset.IsAssetSavedInScene( ) &&
if ( !_houdiniAsset!.IsAssetSavedInScene( ) &&
( Event.current != null && ( Event.current.commandName.Equals( "Delete" ) ||
Event.current.commandName.Equals( "SoftDelete" ) ) ) ) {
_houdiniAsset.DeleteAssetCacheData( bRegisterUndo: true ) ;
Expand Down Expand Up @@ -94,7 +99,7 @@ internal void RemoveHoudiniEngineAssetData( ) {
// No need to destroy the object, geo nodes, and parts
// since Unity's GC will handle them.

GameObject tempGO = _houdiniAsset.gameObject ;
GameObject tempGO = _houdiniAsset!.gameObject ;
_houdiniAsset = null ;
HEU_GeneralUtility.DestroyImmediate( tempGO, bRegisterUndo: true ) ;
}
Expand All @@ -109,7 +114,7 @@ internal void ClearHoudiniEngineReferences( ) {
_bakeTargets.Clear( ) ;
}

internal static void DestroyRootComponent( HEU_HoudiniAssetRoot assetRoot ) =>
internal static void DestroyRootComponent( HEU_HoudiniAssetRoot assetRoot ) =>
HEU_GeneralUtility.DestroyImmediate( assetRoot, bRegisterUndo: true ) ;

internal void Reset( ) {
Expand All @@ -125,7 +130,6 @@ internal void Reset( ) {
_houdiniAsset!.RequestResetParameters( false ) ;
}
}
}


} // HoudiniEngineUnity

} ;
} // HoudiniEngineUnity

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 19 additions & 32 deletions Plugins/HoudiniEngineUnity/Scripts/Render/HEU_MaterialData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public bool UpdateMaterialFromHoudini( HAPI_MaterialInfo materialInfo, string as

HEU_SessionBase session = HEU_SessionManager.GetOrCreateDefaultSession( ) ;

HAPI_NodeInfo nodeInfo = new HAPI_NodeInfo( ) ;
HAPI_NodeInfo nodeInfo = new( ) ;
if ( !session.GetNodeInfo( materialInfo.nodeId, ref nodeInfo ) ) {
return false ;
}
Expand Down Expand Up @@ -123,17 +123,15 @@ public bool UpdateMaterialFromHoudini( HAPI_MaterialInfo materialInfo, string as
false ) ;
}

Color diffuseColor ;
if ( !HEU_ParameterUtility.GetParameterColor3Value( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_OGL_DIFF_ATTR, Color.white,
out diffuseColor ) ) {
out Color diffuseColor ) ) {
HEU_ParameterUtility.GetParameterColor3Value( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_DIFF_ATTR, Color.white,
out diffuseColor ) ;
}

float alpha ;
GetMaterialAlpha( session, materialInfo.nodeId, parmInfos, 1f, out alpha ) ;
GetMaterialAlpha( session, materialInfo.nodeId, parmInfos, 1f, out float alpha ) ;

if ( isTransparent ) {
int opacityMapParmIndex =
Expand Down Expand Up @@ -162,11 +160,10 @@ public bool UpdateMaterialFromHoudini( HAPI_MaterialInfo materialInfo, string as
_material.SetColor( HEU_Defines.UNITY_SHADER_COLOR, diffuseColor ) ;

if ( HEU_PluginSettings.UseSpecularShader ) {
Color specular ;
Color defaultSpecular = new Color( 0.2f, 0.2f, 0.2f, 1 ) ;
Color defaultSpecular = new( 0.2f, 0.2f, 0.2f, 1 ) ;
if ( !HEU_ParameterUtility.GetParameterColor3Value( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_OGL_SPEC_ATTR, defaultSpecular,
out specular ) ) {
out Color specular ) ) {
HEU_ParameterUtility.GetParameterColor3Value( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_SPEC_ATTR, defaultSpecular,
out specular ) ;
Expand Down Expand Up @@ -196,10 +193,9 @@ public bool UpdateMaterialFromHoudini( HAPI_MaterialInfo materialInfo, string as
}
}
else {
float metallic = 0 ;
if ( !HEU_ParameterUtility.GetParameterFloatValue( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_OGL_METALLIC_ATTR, 0f,
out metallic ) ) {
out float metallic ) ) {
HEU_ParameterUtility.GetParameterFloatValue( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_METALLIC_ATTR, 0f, out metallic ) ;
}
Expand All @@ -218,7 +214,7 @@ public bool UpdateMaterialFromHoudini( HAPI_MaterialInfo materialInfo, string as
}


if ( metallicMapParmIndex >= 0 && metallicMapParmIndex < parmInfos.Length ) {
if ( metallicMapParmIndex > -1 && metallicMapParmIndex < parmInfos.Length ) {
string metallicTextureFileName =
GetTextureFileNameFromMaterialParam( session, materialInfo.nodeId,
parmInfos[ metallicMapParmIndex ] ) ;
Expand Down Expand Up @@ -255,11 +251,10 @@ public bool UpdateMaterialFromHoudini( HAPI_MaterialInfo materialInfo, string as
}

// Emission
Color emission ;
Color defaultEmission = new Color( 0, 0, 0, 0 ) ;
Color defaultEmission = new( 0, 0, 0, 0 ) ;
if ( !HEU_ParameterUtility.GetParameterColor3Value( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_OGL_EMISSIVE_ATTR, defaultEmission,
out emission ) ) {
out Color emission ) ) {
HEU_ParameterUtility.GetParameterColor3Value( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_EMISSIVE_ATTR, defaultEmission,
out emission ) ;
Expand Down Expand Up @@ -289,11 +284,10 @@ public bool UpdateMaterialFromHoudini( HAPI_MaterialInfo materialInfo, string as
}

// Smoothness (need to invert roughness!)
float roughness ;
float defaultRoughness = 0.5f ;
if ( !HEU_ParameterUtility.GetParameterFloatValue( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_OGL_ROUGH_ATTR, defaultRoughness,
out roughness ) ) {
out float roughness ) ) {
HEU_ParameterUtility.GetParameterFloatValue( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_ROUGH_ATTR, defaultRoughness,
out roughness ) ;
Expand All @@ -313,7 +307,7 @@ public bool UpdateMaterialFromHoudini( HAPI_MaterialInfo materialInfo, string as
HEU_Defines.MAT_ROUGH_MAP_ATTR_ENABLED ) ;
}

if ( roughMapParmIndex >= 0 && roughMapParmIndex < parmInfos.Length ) {
if ( roughMapParmIndex > -1 && roughMapParmIndex < parmInfos.Length ) {
string roughTextureFileName =
GetTextureFileNameFromMaterialParam( session, materialInfo.nodeId,
parmInfos[ roughMapParmIndex ] ) ;
Expand Down Expand Up @@ -345,9 +339,7 @@ public bool UpdateMaterialFromHoudini( HAPI_MaterialInfo materialInfo, string as
/// <summary>
/// Returns true if this material was pre-existing in Unity and not generated from Houdini at cook time.
/// </summary>
public bool IsExistingMaterial( ) {
return _materialSource == Source.UNITY || _materialSource == Source.SUBSTANCE ;
}
public bool IsExistingMaterial( ) => _materialSource is Source.UNITY or Source.SUBSTANCE ;


// ===============================================================
Expand All @@ -367,8 +359,8 @@ public bool IsExistingMaterial( ) {
// the hash of the material path on project (eg. Assets/Materials/materialname.mat)
[SerializeField] internal int _materialKey = HEU_Defines.HEU_INVALID_MATERIAL ;

bool UseLegacyShaders( HAPI_MaterialInfo materialInfo, string assetCacheFolderPath,
HEU_SessionBase session, HAPI_NodeInfo nodeInfo, HAPI_ParmInfo[] parmInfos ) {
bool UseLegacyShaders( HAPI_MaterialInfo materialInfo, string assetCacheFolderPath,
HEU_SessionBase session, HAPI_NodeInfo nodeInfo, HAPI_ParmInfo[] parmInfos ) {
// Diffuse texture - render & extract
int diffuseMapParmIndex =
HEU_ParameterUtility.FindTextureParamByNameOrTag( session, nodeInfo.id, parmInfos,
Expand Down Expand Up @@ -426,38 +418,34 @@ bool UseLegacyShaders( HAPI_MaterialInfo materialInfo, string assetCacheF

// Clamp shininess to non-zero as results in very hard shadows. Unity's UI does not allow zero either.

float shininess ;
if ( !HEU_ParameterUtility.GetParameterFloatValue( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_OGL_ROUGH_ATTR, 0f, out shininess ) ) {
HEU_Defines.MAT_OGL_ROUGH_ATTR, 0f, out float shininess ) ) {
HEU_ParameterUtility.GetParameterFloatValue( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_ROUGH_ATTR, 0f, out shininess ) ;
}

_material.SetFloat( HEU_Defines.UNITY_SHADER_SHININESS, Mathf.Max( 0.03f, 1.0f - shininess ) ) ;

Color diffuseColor ;
if ( !HEU_ParameterUtility.GetParameterColor3Value( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_OGL_DIFF_ATTR, Color.white,
out diffuseColor ) ) {
out Color diffuseColor ) ) {
HEU_ParameterUtility.GetParameterColor3Value( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_DIFF_ATTR, Color.white,
out diffuseColor ) ;
}

float alpha ;
if ( !HEU_ParameterUtility.GetParameterFloatValue( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_OGL_ALPHA_ATTR, 1f, out alpha ) ) {
HEU_Defines.MAT_OGL_ALPHA_ATTR, 1f, out float alpha ) ) {
HEU_ParameterUtility.GetParameterFloatValue( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_ALPHA_ATTR, 1f, out alpha ) ;
}

diffuseColor.a = alpha ;
_material.SetColor( HEU_Defines.UNITY_SHADER_COLOR, diffuseColor ) ;

Color specular ;
if ( !HEU_ParameterUtility.GetParameterColor3Value( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_OGL_SPEC_ATTR, Color.black,
out specular ) ) {
out Color specular ) ) {
HEU_ParameterUtility.GetParameterColor3Value( session, materialInfo.nodeId, parmInfos,
HEU_Defines.MAT_SPEC_ATTR, Color.black, out specular ) ;
}
Expand Down Expand Up @@ -525,8 +513,7 @@ internal static string GetTextureFileNameFromMaterialParam( HEU_SessionBase sess
/// <returns>True if the material is transparent</returns>
internal static bool IsTransparentMaterial( HEU_SessionBase session, HAPI_NodeId nodeID,
HAPI_ParmInfo[ ] parameters ) {
float alpha ;
GetMaterialAlpha( session, nodeID, parameters, 1, out alpha ) ;
GetMaterialAlpha( session, nodeID, parameters, 1, out float alpha ) ;
return alpha < 0.95f ;
}

Expand Down
Loading

0 comments on commit 746fc7c

Please sign in to comment.