Ejemplo n.º 1
0
        public CombinedModel ProcessCluster(MeshRenderer[] allRenderers, ref SceneStreamLoader loader, Dictionary <MeshRenderer, bool> lowLODLevels)
        {
            List <MeshFilter> allFilters = new List <MeshFilter>(allRenderers.Length);
            int sumVertexLength          = 0;

            for (int i = 0; i < allRenderers.Length; ++i)
            {
                if (!lowLODLevels.ContainsKey(allRenderers[i]))
                {
                    MeshFilter filter = allRenderers[i].GetComponent <MeshFilter>();
                    allFilters.Add(filter);
                    sumVertexLength += (int)(filter.sharedMesh.vertexCount * 1.2f);
                }
            }
            NativeList <Point> points            = new NativeList <Point>(sumVertexLength, Allocator.Temp);
            NativeList <int>   triangleMaterials = new NativeList <int>(sumVertexLength / 3, Allocator.Temp);
            var matToIndexDict = VirtualMaterial.GetMaterialsData(allRenderers, ref loader);

            for (int i = 0; i < allFilters.Count; ++i)
            {
                Mesh mesh = allFilters[i].sharedMesh;
                GetPoints(points, triangleMaterials, mesh, allFilters[i].transform, allRenderers[i].sharedMaterials, matToIndexDict);
            }
            float3 less = points[0].vertex;
            float3 more = points[0].vertex;

            for (int i = 1; i < points.Length; ++i)
            {
                float3 current = points[i].vertex;
                if (less.x > current.x)
                {
                    less.x = current.x;
                }
                if (more.x < current.x)
                {
                    more.x = current.x;
                }
                if (less.y > current.y)
                {
                    less.y = current.y;
                }
                if (more.y < current.y)
                {
                    more.y = current.y;
                }
                if (less.z > current.z)
                {
                    less.z = current.z;
                }
                if (more.z < current.z)
                {
                    more.z = current.z;
                }
            }

            float3        center = (less + more) / 2;
            float3        extent = more - center;
            Bounds        b      = new Bounds(center, extent * 2);
            CombinedModel md;

            md.bound       = b;
            md.allPoints   = points;
            md.allMatIndex = triangleMaterials;
            return(md);
        }
Ejemplo n.º 2
0
        public void TryThis()
        {
            bool save = false;

            if (res == null)
            {
                save     = true;
                res      = ScriptableObject.CreateInstance <ClusterMatResources>();
                res.name = "SceneManager";
                res.clusterProperties = new List <SceneStreaming>();
            }

            SceneStreaming    property = new SceneStreaming();
            SceneStreamLoader loader   = new SceneStreamLoader();

            loader.fsm    = new FileStream(ClusterMatResources.infosPath + modelName + ".mpipe", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            property.name = modelName;
            int containIndex = -1;

            for (int i = 0; i < res.clusterProperties.Count; ++i)
            {
                if (property.name == res.clusterProperties[i].name)
                {
                    containIndex = i;
                    break;
                }
            }
            LODGroup[] groups = GetComponentsInChildren <LODGroup>();
            Dictionary <MeshRenderer, bool> lowLevelDict = new Dictionary <MeshRenderer, bool>();

            foreach (var i in groups)
            {
                LOD[] lods = i.GetLODs();
                for (int j = 1; j < lods.Length; ++j)
                {
                    foreach (var k in lods[j].renderers)
                    {
                        if (k.GetType() == typeof(MeshRenderer))
                        {
                            lowLevelDict.Add(k as MeshRenderer, true);
                        }
                    }
                }
            }
            CombinedModel model = ProcessCluster(GetComponentsInChildren <MeshRenderer>(false), ref loader, lowLevelDict);

            loader.clusterCount = ClusterGenerator.GenerateCluster(model.allPoints, model.allMatIndex, model.bound, voxelCount, containIndex < 0 ? res.clusterProperties.Count : containIndex, ref loader);

            res.maximumMaterialCount = Mathf.Max(1, res.maximumMaterialCount);
            res.maximumMaterialCount = Mathf.Max(res.maximumMaterialCount, loader.allProperties.Length);
            if (containIndex < 0)
            {
                res.clusterProperties.Add(property);
            }
            else
            {
                res.clusterProperties[containIndex] = property;
            }
            if (save)
            {
                AssetDatabase.CreateAsset(res, "Assets/SceneManager.asset");
            }
            else
            {
                EditorUtility.SetDirty(res);
            }
            loader.SaveAll();
            loader.Dispose();
        }
Ejemplo n.º 3
0
        /// <returns></returns> Cluster Count
        public static int GenerateCluster(NativeList <Point> pointsFromMesh, NativeList <int> mats, Bounds bd, int voxelCount, int sceneCount, ref SceneStreamLoader loader)
        {
            NativeList <Cluster> boxes; NativeList <Point> points; NativeList <int> outMats;

            GetCluster(pointsFromMesh, mats, bd, out boxes, out points, out outMats, voxelCount);
            loader.cluster      = boxes;
            loader.points       = points;
            loader.triangleMats = outMats;
            //Dispose Native Array
            return(boxes.Length);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        public void TryThis()
        {
            string fileName = ClusterMatResources.infosPath + modelName + ".mpipe";

            if (!property)
            {
                Debug.LogError("Property Empty!");
            }

            if (string.IsNullOrEmpty(modelName))
            {
                Debug.LogError("Name Empty!");
                return;
            }
            if (File.Exists(fileName))
            {
                Debug.LogError("File Already Exists!");
                return;
            }


            bool save = false;

            if (!res)
            {
                res = AssetDatabase.LoadAssetAtPath <ClusterMatResources>("Assets/SceneManager.asset");
            }
            if (!res)
            {
                save     = true;
                res      = ScriptableObject.CreateInstance <ClusterMatResources>();
                res.name = "SceneManager";
            }
            SceneStreamLoader loader = new SceneStreamLoader();

            loader.fsm        = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            property.fileName = modelName;
            LODGroup[] groups = GetComponentsInChildren <LODGroup>();
            Dictionary <MeshRenderer, bool> lowLevelDict = new Dictionary <MeshRenderer, bool>();

            foreach (var i in groups)
            {
                LOD[] lods = i.GetLODs();
                for (int j = 1; j < lods.Length; ++j)
                {
                    foreach (var k in lods[j].renderers)
                    {
                        if (k.GetType() == typeof(MeshRenderer))
                        {
                            lowLevelDict.Add(k as MeshRenderer, true);
                        }
                    }
                }
            }
            CombinedModel model = ProcessCluster(GetComponentsInChildren <MeshRenderer>(false), ref loader, lowLevelDict);

            loader.clusterCount = ClusterGenerator.GenerateCluster(model.allPoints, model.allMatIndex, model.bound, voxelCount, ref loader);

            res.maximumMaterialCount = Mathf.Max(1, res.maximumMaterialCount);
            res.maximumMaterialCount = Mathf.Max(res.maximumMaterialCount, loader.allProperties.Length);
            if (save)
            {
                AssetDatabase.CreateAsset(res, "Assets/SceneManager.asset");
            }
            else
            {
                EditorUtility.SetDirty(res);
            }
            loader.SaveAll();
            loader.Dispose();
        }