public AssetReference GetReference(ref int4x4 guid) { if (!referenceCacheDict.isCreated) { referenceCacheDict = new NativeDictionary <int4x4, int, Int4x4Equal>(100, Allocator.Persistent, new Int4x4Equal()); } int index; if (referenceCacheDict.Get(guid, out index)) { return(allReferenceCache[index]); } string guidCache = new string((char *)guid.Ptr(), 0, 32); referenceCacheDict.Add(guid, allReferenceCache.Count); AssetReference aref = new AssetReference(guidCache); allReferenceCache.Add(aref); return(aref); }
public unsafe static Dictionary <Material, int> GetMaterialsData(MeshRenderer[] allRenderers, ref SceneStreamLoader loader) { float3 ColorToVector(Color c) { return(pow(float3(c.r, c.g, c.b), 2.2f)); } float2 GetVector2(float4 vec) { return(float2(vec.x, vec.y)); } var dict = new Dictionary <Material, int>(allRenderers.Length); loader.allProperties = new NativeList <MaterialProperties>(allRenderers.Length, Unity.Collections.Allocator.Persistent); var albedoTexs = new List <Texture>(allRenderers.Length); var normalTexs = new List <Texture>(allRenderers.Length); var smoTexs = new List <Texture>(allRenderers.Length); var emissionTex = new List <Texture>(allRenderers.Length); var heightTex = new List <Texture>(allRenderers.Length); var secondAlbedoTex = new List <Texture>(allRenderers.Length); var secondBumpTex = new List <Texture>(allRenderers.Length); var secondSpecTex = new List <Texture>(allRenderers.Length); var albedoDict = new Dictionary <Texture, int>(allRenderers.Length); var normalDict = new Dictionary <Texture, int>(allRenderers.Length); var smoDict = new Dictionary <Texture, int>(allRenderers.Length); var emissionDict = new Dictionary <Texture, int>(allRenderers.Length); var heightDict = new Dictionary <Texture, int>(allRenderers.Length); var secondAlbedoDict = new Dictionary <Texture, int>(allRenderers.Length); var secondBumpDict = new Dictionary <Texture, int>(allRenderers.Length); var secondSpecDict = new Dictionary <Texture, int>(allRenderers.Length); int len = 0; int GetTextureIndex(List <Texture> lst, Dictionary <Texture, int> texDict, Texture tex) { int ind = -1; if (tex) { if (!texDict.TryGetValue(tex, out ind)) { ind = lst.Count; lst.Add(tex); texDict.Add(tex, ind); } } return(ind); } foreach (var r in allRenderers) { var ms = r.sharedMaterials; foreach (var m in ms) { if (!m) { throw new System.Exception(r.name + " Has Null Mat"); } if (!dict.ContainsKey(m)) { dict.Add(m, len); Texture albedo = m.GetTexture("_MainTex"); Texture normal = m.GetTexture("_BumpMap"); Texture smo = m.GetTexture("_SpecularMap"); Texture emission = m.GetTexture("_EmissionMap"); Texture height = m.GetTexture("_HeightMap"); Texture secondBump = m.GetTexture("_SecondaryBumpMap"); Texture secondAlbedo = m.GetTexture("_SecondaryMainTex"); Texture secondSpec = m.GetTexture("_SecondarySpecularMap"); int albedoIndex = GetTextureIndex(albedoTexs, albedoDict, albedo); int normalIndex = GetTextureIndex(normalTexs, normalDict, normal); int smoIndex = GetTextureIndex(smoTexs, smoDict, smo); int emissionIndex = GetTextureIndex(emissionTex, emissionDict, emission); int heightIndex = GetTextureIndex(heightTex, heightDict, height); int secondBumpIndex = GetTextureIndex(secondBumpTex, secondBumpDict, secondBump); int secondAlbedoIndex = GetTextureIndex(secondAlbedoTex, secondAlbedoDict, secondAlbedo); int secondSpecIndex = GetTextureIndex(secondSpecTex, secondSpecDict, secondSpec); loader.allProperties.Add(new MaterialProperties { _Color = ColorToVector(m.GetColor("_Color")), _Glossiness = m.GetFloat("_Glossiness"), _DecalLayer = (uint)m.GetInt("_DecalLayer"), _EmissionColor = ColorToVector(m.GetColor("_EmissionColor") * m.GetFloat("_EmissionMultiplier")), _MetallicIntensity = m.GetFloat("_MetallicIntensity"), _SpecularIntensity = m.GetFloat("_SpecularIntensity"), _Occlusion = m.GetFloat("_Occlusion"), _NormalIntensity = GetVector2(m.GetVector("_NormalIntensity")), _TileOffset = m.GetVector("_TileOffset"), _BumpMap = normalIndex, _EmissionMap = emissionIndex, _MainTex = albedoIndex, _SpecularMap = smoIndex, _HeightMap = heightIndex, _HeightMapIntensity = m.GetFloat("_HeightmapIntensity"), _SecondaryBumpMap = secondBumpIndex, _SecondaryMainTex = secondAlbedoIndex, _SecondarySpecularMap = secondSpecIndex, _SecondaryTileOffset = m.GetVector("_SecondaryTileOffset") }); len++; } } } ComputeShader readRTDataShader = Resources.Load <ComputeShader>("ReadRTData"); void GetGUIDs(out NativeList <int4x4> strs, List <Texture> texs, int typeIndex) { strs = new NativeList <int4x4>(texs.Count, Allocator.Persistent); for (int i = 0; i < texs.Count; ++i) { string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(texs[i])); MEditorLib.SetObjectAddressable(texs[i], guid); int4x4 value = 0; fixed(char *c = guid) { UnsafeUtility.MemCpy(value.Ptr(), c, sizeof(int4x4)); } strs.Add(value); } } GetGUIDs(out loader.albedoGUIDs, albedoTexs, 0); GetGUIDs(out loader.secondAlbedoGUIDs, secondAlbedoTex, 0); GetGUIDs(out loader.normalGUIDs, normalTexs, 1); GetGUIDs(out loader.secondNormalGUIDs, secondBumpTex, 1); GetGUIDs(out loader.smoGUIDs, smoTexs, 0); GetGUIDs(out loader.secondSpecGUIDs, secondSpecTex, 0); GetGUIDs(out loader.emissionGUIDs, emissionTex, 2); GetGUIDs(out loader.heightGUIDs, heightTex, 3); EditorUtility.SetDirty(AddressableAssetSettingsDefaultObject.Settings); return(dict); }