3 using UnityEngine.Experimental.Rendering.HDPipeline;
4 using System.Collections.Generic;
7 namespace UnityEditor.Experimental.Rendering.HDPipeline
9 // This class keep track of every diffusion profile in the project so it can generate unique uint hashes
10 // for every asset, which are used to differentiate diffusion profiles in the shader
12 public class DiffusionProfileHashTable
14 [System.NonSerialized]
15 static Dictionary<int, uint> diffusionProfileHashes = new Dictionary<int, uint>();
17 static uint GetDiffusionProfileHash(DiffusionProfileSettings asset)
19 uint hash32 = (uint)AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(asset)).GetHashCode();
20 uint mantissa = hash32 & 0x7FFFFF;
21 uint exponent = 0b10000000; // 0 as exponent
23 // only store the first 23 bits so when the hash is converted to float, it doesn't write into
24 // the exponent part of the float (which avoids having NaNs, inf or precisions issues)
25 return (exponent << 23) | mantissa;
28 static uint GenerateUniqueHash(DiffusionProfileSettings asset)
30 uint hash = GetDiffusionProfileHash(asset);
32 while (diffusionProfileHashes.ContainsValue(hash) || hash == DiffusionProfileConstants.DIFFUSION_PROFILE_NEUTRAL_ID)
34 Debug.LogWarning("Collision found in asset: " + asset + ", generating a new hash, previous hash: " + hash);
41 public static void UpdateDiffusionProfileHashNow(DiffusionProfileSettings profile)
43 uint hash = profile.profile.hash;
45 // If the hash is 0, then we need to generate a new one (it means that the profile was just created)
48 profile.profile.hash = GenerateUniqueHash(profile);
49 EditorUtility.SetDirty(profile);
50 // We can't move the asset
52 // If the asset is not in the list, we regenerate it's hash using the GUID (which leads to the same result every time)
53 else if (!diffusionProfileHashes.ContainsKey(profile.GetInstanceID()))
55 uint newHash = GenerateUniqueHash(profile);
56 if (newHash != profile.profile.hash)
58 profile.profile.hash = newHash;
59 EditorUtility.SetDirty(profile);
62 else // otherwise, no issue, we don't change the hash and we keep it to check for collisions
63 diffusionProfileHashes.Add(profile.GetInstanceID(), profile.profile.hash);