OSDN Git Service

Added Assets for main menu
[mindgames/Mindgames_main.git] / Mindgames / Library / PackageCache / com.unity.render-pipelines.high-definition@6.9.0-preview / Runtime / Utilities / DiffusionProfileHashTable.cs
1 #if UNITY_EDITOR
2 using UnityEngine;
3 using UnityEngine.Experimental.Rendering.HDPipeline;
4 using System.Collections.Generic;
5 using UnityEditor;
6
7 namespace UnityEditor.Experimental.Rendering.HDPipeline
8 {
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
11     [InitializeOnLoad]
12     public class DiffusionProfileHashTable
13     {
14         [System.NonSerialized]
15         static Dictionary<int,  uint>           diffusionProfileHashes = new Dictionary<int, uint>();
16
17         static uint GetDiffusionProfileHash(DiffusionProfileSettings asset)
18         {
19             uint hash32 = (uint)AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(asset)).GetHashCode();
20             uint mantissa = hash32 & 0x7FFFFF;
21             uint exponent = 0b10000000; // 0 as exponent
22
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;
26         }
27
28         static uint GenerateUniqueHash(DiffusionProfileSettings asset)
29         {
30             uint hash = GetDiffusionProfileHash(asset);
31             
32             while (diffusionProfileHashes.ContainsValue(hash) || hash == DiffusionProfileConstants.DIFFUSION_PROFILE_NEUTRAL_ID)
33             {
34                 Debug.LogWarning("Collision found in asset: " + asset + ", generating a new hash, previous hash: " + hash);
35                 hash++;
36             }
37
38             return hash;
39         }
40
41         public static void UpdateDiffusionProfileHashNow(DiffusionProfileSettings profile)
42         {
43             uint hash = profile.profile.hash;
44
45             // If the hash is 0, then we need to generate a new one (it means that the profile was just created)
46             if (hash == 0)
47             {
48                 profile.profile.hash = GenerateUniqueHash(profile);
49                 EditorUtility.SetDirty(profile);
50                 // We can't move the asset
51             }
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()))
54             {
55                 uint newHash = GenerateUniqueHash(profile);
56                 if (newHash != profile.profile.hash)
57                 {
58                     profile.profile.hash = newHash;
59                     EditorUtility.SetDirty(profile);
60                 }
61             }
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);
64         }
65     }
66 }
67 #endif