public static void OnGUI(MaterialProperty diffusionProfileAsset, MaterialProperty diffusionProfileHash) { string guid = HDUtils.ConvertVector4ToGUID(diffusionProfileAsset.vectorValue); DiffusionProfileSettings diffusionProfile = AssetDatabase.LoadAssetAtPath <DiffusionProfileSettings>(AssetDatabase.GUIDToAssetPath(guid)); // is it okay to do this every frame ? EditorGUI.BeginChangeCheck(); diffusionProfile = (DiffusionProfileSettings)EditorGUILayout.ObjectField("Diffusion Profile", diffusionProfile, typeof(DiffusionProfileSettings), false); if (EditorGUI.EndChangeCheck()) { Vector4 newGuid = Vector4.zero; float hash = 0; if (diffusionProfile != null) { guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(diffusionProfile)); newGuid = HDUtils.ConvertGUIDToVector4(guid); hash = HDShadowUtils.Asfloat(diffusionProfile.profile.hash); } // encode back GUID and it's hash diffusionProfileAsset.vectorValue = newGuid; diffusionProfileHash.floatValue = hash; } }
public static void OnGUI(MaterialEditor materialEditor, MaterialProperty diffusionProfileAsset, MaterialProperty diffusionProfileHash, int profileIndex) { // We can't cache these fields because of several edge cases like undo/redo or pressing escape in the object picker string guid = HDUtils.ConvertVector4ToGUID(diffusionProfileAsset.vectorValue); DiffusionProfileSettings diffusionProfile = AssetDatabase.LoadAssetAtPath <DiffusionProfileSettings>(AssetDatabase.GUIDToAssetPath(guid)); // is it okay to do this every frame ? EditorGUI.BeginChangeCheck(); diffusionProfile = (DiffusionProfileSettings)EditorGUILayout.ObjectField("Diffusion Profile", diffusionProfile, typeof(DiffusionProfileSettings), false); if (EditorGUI.EndChangeCheck()) { Vector4 newGuid = Vector4.zero; float hash = 0; if (diffusionProfile != null) { guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(diffusionProfile)); newGuid = HDUtils.ConvertGUIDToVector4(guid); hash = HDShadowUtils.Asfloat(diffusionProfile.profile.hash); } // encode back GUID and it's hash diffusionProfileAsset.vectorValue = newGuid; diffusionProfileHash.floatValue = hash; // Update external reference. foreach (var target in materialEditor.targets) { MaterialExternalReferences matExternalRefs = MaterialExternalReferences.GetMaterialExternalReferences(target as Material); matExternalRefs.SetDiffusionProfileReference(profileIndex, diffusionProfile); } } DrawDiffusionProfileWarning(diffusionProfile); }
public static void UpdateDiffusionProfileHashNow(DiffusionProfileSettings profile) { uint hash = profile.profile.hash; // If the hash is 0, then we need to generate a new one (it means that the profile was just created) if (hash == 0) { profile.profile.hash = GenerateUniqueHash(profile); EditorUtility.SetDirty(profile); // We can't move the asset } // If the asset is not in the list, we regenerate it's hash using the GUID (which leads to the same result every time) else if (!diffusionProfileHashes.ContainsKey(profile.GetInstanceID())) { uint newHash = GenerateUniqueHash(profile); if (newHash != profile.profile.hash) { profile.profile.hash = newHash; EditorUtility.SetDirty(profile); } } else // otherwise, no issue, we don't change the hash and we keep it to check for collisions { diffusionProfileHashes.Add(profile.GetInstanceID(), profile.profile.hash); } }
public override void ReadFromString(ref string[] nodeParams) { if (UIUtils.CurrentShaderVersion() > 17004) { base.ReadFromString(ref nodeParams); } else { ParentReadFromString(ref nodeParams); } string defaultGuid = GetCurrentParam(ref nodeParams); if (defaultGuid.Length > 1) { m_defaultValue = AssetDatabase.LoadAssetAtPath <DiffusionProfileSettings>(AssetDatabase.GUIDToAssetPath(defaultGuid)); } if (UIUtils.CurrentShaderVersion() > 17004) { string materialGuid = GetCurrentParam(ref nodeParams); if (materialGuid.Length > 1) { m_materialValue = AssetDatabase.LoadAssetAtPath <DiffusionProfileSettings>(AssetDatabase.GUIDToAssetPath(materialGuid)); } } }
internal static void DrawDiffusionProfileWarning(DiffusionProfileSettings materialProfile) { if (materialProfile != null && !HDRenderPipelineGlobalSettings.instance.diffusionProfileSettingsList.Any(d => d == materialProfile)) { CoreEditorUtils.DrawFixMeBox(diffusionProfileNotInHDRPAsset, "Fix", () => HDRenderPipelineGlobalSettings.instance.AddDiffusionProfile(materialProfile)); } }
public override void CopyValuesFrom(MaterialSlot foundSlot) { var slot = foundSlot as DiffusionProfileInputMaterialSlot; if (slot != null) { m_DiffusionProfileAsset = slot.m_DiffusionProfileAsset; } }
static uint GetDiffusionProfileHash(DiffusionProfileSettings asset) { uint hash32 = (uint)AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(asset)).GetHashCode(); uint mantissa = hash32 & 0x7FFFFF; uint exponent = 0b10000000; // 0 as exponent // only store the first 23 bits so when the hash is converted to float, it doesn't write into // the exponent part of the float (which avoids having NaNs, inf or precisions issues) return((exponent << 23) | mantissa); }
// This function go through all loaded materials so it can be used to update any materials that is // not serialized as an asset (i.e materials saved in scenes) static void UpgradeSceneMaterials() { #pragma warning disable 618 var hdAsset = GraphicsSettings.renderPipelineAsset as HDRenderPipelineAsset; // For each loaded materials foreach (var mat in Resources.FindObjectsOfTypeAll <Material>()) { DiffusionProfileSettings.UpgradeMaterial(mat, hdAsset.diffusionProfileSettings); } #pragma warning restore 618 }
public override void ForceUpdateFromMaterial(Material material) { if (UIUtils.IsProperty(m_currentParameterType) && material.HasProperty(m_propertyName + "_asset")) { var guid = HDUtils.ConvertVector4ToGUID(material.GetVector(m_propertyName + "_asset")); var profile = AssetDatabase.LoadAssetAtPath <DiffusionProfileSettings>(AssetDatabase.GUIDToAssetPath(guid)); if (profile != null) { m_materialValue = profile; } } }
static uint GenerateUniqueHash(DiffusionProfileSettings asset) { uint hash = GetDiffusionProfileHash(asset); while (diffusionProfileHashes.ContainsValue(hash) || hash == DiffusionProfileConstants.DIFFUSION_PROFILE_NEUTRAL_ID) { Debug.LogWarning("Collision found in asset: " + asset + ", generating a new hash, previous hash: " + hash); hash++; } return(hash); }
static uint GenerateUniqueHash(DiffusionProfileSettings asset) { uint hash = GetDiffusionProfileHash(asset); while (diffusionProfileHashes.ContainsValue(hash)) { Debug.LogWarning("Collision found in asset: " + asset + ", generating a new hash, previous hash: " + hash); hash++; } return(hash); }
public override void DrawMaterialProperties() { if (m_materialMode) { EditorGUI.BeginChangeCheck(); } m_materialValue = EditorGUILayoutObjectField(Constants.MaterialValueLabel, m_materialValue, typeof(DiffusionProfileSettings), true) as DiffusionProfileSettings; if (m_materialMode && EditorGUI.EndChangeCheck()) { m_requireMaterialUpdate = true; } }
public void SetDiffusionProfileReference(int index, DiffusionProfileSettings profile) { if (index >= m_DiffusionProfileReferences.Length) { var newList = new DiffusionProfileSettings[index + 1]; for (int i = 0; i < m_DiffusionProfileReferences.Length; ++i) { newList[i] = m_DiffusionProfileReferences[i]; } m_DiffusionProfileReferences = newList; } m_DiffusionProfileReferences[index] = profile; EditorUtility.SetDirty(this); }
static void DrawDiffusionProfileWarning(DiffusionProfileSettings materialProfile) { if (materialProfile != null && !HDRenderPipeline.defaultAsset.diffusionProfileSettingsList.Any(d => d == materialProfile)) { using (new EditorGUILayout.HorizontalScope(EditorStyles.helpBox)) { GUIStyle wordWrap = new GUIStyle(EditorStyles.label); wordWrap.wordWrap = true; EditorGUILayout.LabelField(diffusionProfileNotInHDRPAsset, wordWrap); if (GUILayout.Button("Fix", GUILayout.ExpandHeight(true))) { HDRenderPipeline.defaultAsset.AddDiffusionProfile(materialProfile); } } } }
static uint GetDiffusionProfileHash(DiffusionProfileSettings asset) { string assetPath = AssetDatabase.GetAssetPath(asset); // In case the diffusion profile is not yet saved on the disk, we don't generate the hash if (String.IsNullOrEmpty(assetPath)) { return(0); } uint hash32 = (uint)AssetDatabase.AssetPathToGUID(assetPath).GetHashCode(); uint mantissa = hash32 & 0x7FFFFF; uint exponent = 0b10000000; // 0 as exponent // only store the first 23 bits so when the hash is converted to float, it doesn't write into // the exponent part of the float (which avoids having NaNs, inf or precisions issues) return((exponent << 23) | mantissa); }
private static void SetDefaultDiffusionProfile(Material mat) { string matDiffProfile = HDUtils.ConvertVector4ToGUID(mat.GetVector("Diffusion_Profile_Asset")); string guid = ""; long localID; uint diffusionProfileHash = 0; foreach (var diffusionProfileAsset in HDRenderPipelineGlobalSettings.instance.diffusionProfileSettingsList) { if (diffusionProfileAsset != null) { bool gotGuid = AssetDatabase.TryGetGUIDAndLocalFileIdentifier <DiffusionProfileSettings>(diffusionProfileAsset, out guid, out localID); if (gotGuid && (diffusionProfileAsset.name.Equals(kDefaultDiffusionProfileName) || guid.Equals(matDiffProfile))) { diffusionProfileHash = diffusionProfileAsset.profile.hash; break; } } } if (diffusionProfileHash == 0) { // If the user doesn't have a foliage diffusion profile defined, grab the foliage diffusion profile that comes with HD. // This won't work until the user adds it to their default diffusion profiles list, // but there is a nice "fix" button on the material to help with that. DiffusionProfileSettings foliageSettings = AssetDatabase.LoadAssetAtPath <DiffusionProfileSettings>(HDUtils.GetHDRenderPipelinePath() + kFoliageDiffusionProfilePath); if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier <DiffusionProfileSettings>(foliageSettings, out guid, out localID)) { diffusionProfileHash = foliageSettings.profile.hash; } } if (diffusionProfileHash != 0) { mat.SetVector("Diffusion_Profile_Asset", HDUtils.ConvertGUIDToVector4(guid)); mat.SetFloat("Diffusion_Profile", HDShadowUtils.Asfloat(diffusionProfileHash)); } }
internal VisualElement CreateGUI( ValueChangedCallback valueChangedCallback, DiffusionProfileSettings fieldToDraw, string labelName, out VisualElement propertyColorField, int indentLevel = 0) { var objectField = new ObjectField { value = fieldToDraw, objectType = typeof(DiffusionProfileSettings) }; if (valueChangedCallback != null) { objectField.RegisterValueChangedCallback(evt => { valueChangedCallback((DiffusionProfileSettings)evt.newValue); }); } propertyColorField = objectField; var defaultRow = new PropertyRow(PropertyDrawerUtils.CreateLabel(labelName, indentLevel)); defaultRow.Add(propertyColorField); defaultRow.styleSheets.Add(Resources.Load <StyleSheet>("Styles/PropertyRow")); return(defaultRow); }
public static void UpdateUniqueHash(DiffusionProfileSettings asset) { // Defere the generation of the hash because we can't call AssetDatabase functions outside of editor scope diffusionProfileToUpdate.Enqueue(asset); }
void ShaderSSSAndTransmissionInputGUI() { var hdPipeline = RenderPipelineManager.currentPipeline as HDRenderPipeline; if (hdPipeline == null) { return; } using (var scope = new EditorGUI.ChangeCheckScope()) { using (new EditorGUILayout.HorizontalScope()) { // We can't cache these fields because of several edge cases like undo/redo or pressing escape in the object picker string guid = HDUtils.ConvertVector4ToGUID(diffusionProfileAsset[m_LayerIndex].vectorValue); DiffusionProfileSettings diffusionProfile = AssetDatabase.LoadAssetAtPath <DiffusionProfileSettings>(AssetDatabase.GUIDToAssetPath(guid)); // is it okay to do this every frame ? using (var changeScope = new EditorGUI.ChangeCheckScope()) { diffusionProfile = (DiffusionProfileSettings)EditorGUILayout.ObjectField(Styles.diffusionProfileText, diffusionProfile, typeof(DiffusionProfileSettings), false); if (changeScope.changed) { Vector4 newGuid = Vector4.zero; float hash = 0; if (diffusionProfile != null) { guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(diffusionProfile)); newGuid = HDUtils.ConvertGUIDToVector4(guid); hash = HDShadowUtils.Asfloat(diffusionProfile.profile.hash); } // encode back GUID and it's hash diffusionProfileAsset[m_LayerIndex].vectorValue = newGuid; diffusionProfileHash[m_LayerIndex].floatValue = hash; } } } } // TODO: does not work with multi-selection if ((int)materialID.floatValue == (int)MaterialId.LitSSS && materials[0].GetSurfaceType() != SurfaceType.Transparent) { materialEditor.ShaderProperty(subsurfaceMask[m_LayerIndex], Styles.subsurfaceMaskText); materialEditor.TexturePropertySingleLine(Styles.subsurfaceMaskMapText, subsurfaceMaskMap[m_LayerIndex]); } if ((int)materialID.floatValue == (int)MaterialId.LitTranslucent || ((int)materialID.floatValue == (int)MaterialId.LitSSS && transmissionEnable.floatValue > 0.0f)) { materialEditor.TexturePropertySingleLine(Styles.thicknessMapText, thicknessMap[m_LayerIndex]); if (thicknessMap[m_LayerIndex].textureValue != null) { // Display the remap of texture values. Vector2 remap = thicknessRemap[m_LayerIndex].vectorValue; EditorGUI.BeginChangeCheck(); EditorGUILayout.MinMaxSlider(Styles.thicknessRemapText, ref remap.x, ref remap.y, 0.0f, 1.0f); if (EditorGUI.EndChangeCheck()) { thicknessRemap[m_LayerIndex].vectorValue = remap; } } else { // Allow the user to set the constant value of thickness if no thickness map is provided. materialEditor.ShaderProperty(thickness[m_LayerIndex], Styles.thicknessText); } } }
public override void DrawSubProperties() { m_defaultValue = EditorGUILayoutObjectField(Constants.DefaultValueLabel, m_defaultValue, typeof(DiffusionProfileSettings), true) as DiffusionProfileSettings; }
public override void CopyDefaultsToMaterial() { m_materialValue = m_defaultValue; }