public void SaveData()
        {
            Dictionary <TexturePack, int> allContainedTexturePack = new Dictionary <TexturePack, int>(32);
            int count = 0;

            int GetValue(TexturePack pack)
            {
                int result;

                if (allContainedTexturePack.TryGetValue(pack, out result))
                {
                    return(result);
                }
                result = count;
                count++;
                allContainedTexturePack.Add(pack, result);
                return(result);
            }

            List <MTerrainData.HeightBlendMaterial> allMats = new List <MTerrainData.HeightBlendMaterial>(allContainedTexturePack.Count);

            foreach (var i in allMaterials)
            {
                MTerrainData.HeightBlendMaterial hb = new MTerrainData.HeightBlendMaterial
                {
                    materialIndex = GetValue(i.frontPack),
                };
                foreach (var j in i.blendWeights)
                {
                    hb.smoothness  = j.smoothness;
                    hb.metallic    = j.metallic;
                    hb.occlusion   = j.occlusion;
                    hb.normalScale = j.normalScale;
                    hb.albedoColor = j.albedoColor;
                    hb.antiRepeat  = j.antiRepeat;
                    allMats.Add(hb);
                }
            }
            targetData.textures = new MTerrain.PBRTexture[allContainedTexturePack.Count];
            foreach (var i in allContainedTexturePack)
            {
                targetData.textures[i.Value] = new MTerrain.PBRTexture
                {
                    albedoOccTex = MEditorLib.SetObjectAddressable(i.Key.albedo),
                    normalTex    = MEditorLib.SetObjectAddressable(i.Key.normal),
                    SMTex        = MEditorLib.SetObjectAddressable(i.Key.smo)
                };
            }
            targetData.allMaterials = allMats.ToArray();
            EditorUtility.SetDirty(targetData);
            AssetDatabase.SaveAssets();
        }
        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);
        }