public static IEnumerator Create(HLOD hlod) { try { Stopwatch sw = new Stopwatch(); AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); sw.Reset(); sw.Start(); hlod.ConvertedPrefabObjects.Clear(); hlod.GeneratedObjects.Clear(); Bounds bounds = hlod.GetBounds(); List <GameObject> hlodTargets = ObjectUtils.HLODTargets(hlod.gameObject); ISpaceSplitter spliter = new QuadTreeSpaceSplitter(5.0f); SpaceNode rootNode = spliter.CreateSpaceTree(bounds, hlod.ChunkSize, hlod.transform.position, hlodTargets, progress => { EditorUtility.DisplayProgressBar("Bake HLOD", "Splitting space", progress * 0.25f); }); if (hlodTargets.Count == 0) { EditorUtility.DisplayDialog("Empty HLOD sources.", "There are no objects to be included in the HLOD.", "Ok"); yield break; } using (DisposableList <HLODBuildInfo> buildInfos = CreateBuildInfo(rootNode, hlod.MinObjectSize)) { if (buildInfos.Count == 0 || buildInfos[0].WorkingObjects.Count == 0) { EditorUtility.DisplayDialog("Empty HLOD sources.", "There are no objects to be included in the HLOD.", "Ok"); yield break; } Debug.Log("[HLOD] Splite space: " + sw.Elapsed.ToString("g")); sw.Reset(); sw.Start(); ISimplifier simplifier = (ISimplifier)Activator.CreateInstance(hlod.SimplifierType, new object[] { hlod.SimplifierOptions }); for (int i = 0; i < buildInfos.Count; ++i) { yield return(new BranchCoroutine(simplifier.Simplify(buildInfos[i]))); } yield return(new WaitForBranches(progress => { EditorUtility.DisplayProgressBar("Bake HLOD", "Simplify meshes", 0.25f + progress * 0.25f); })); Debug.Log("[HLOD] Simplify: " + sw.Elapsed.ToString("g")); sw.Reset(); sw.Start(); using (IBatcher batcher = (IBatcher)Activator.CreateInstance(hlod.BatcherType, new object[] { hlod.BatcherOptions })) { batcher.Batch(hlod.transform.position, buildInfos, progress => { EditorUtility.DisplayProgressBar("Bake HLOD", "Generating combined static meshes.", 0.5f + progress * 0.25f); }); } Debug.Log("[HLOD] Batch: " + sw.Elapsed.ToString("g")); sw.Reset(); sw.Start(); IStreamingBuilder builder = (IStreamingBuilder)Activator.CreateInstance(hlod.StreamingType, new object[] { hlod, hlod.StreamingOptions }); builder.Build(rootNode, buildInfos, hlod.gameObject, hlod.CullDistance, hlod.LODDistance, false, true, progress => { EditorUtility.DisplayProgressBar("Bake HLOD", "Storing results.", 0.75f + progress * 0.25f); }); Debug.Log("[HLOD] Build: " + sw.Elapsed.ToString("g")); sw.Reset(); sw.Start(); EditorUtility.SetDirty(hlod.gameObject); } } finally { EditorUtility.ClearProgressBar(); } }
public IEnumerator CreateImpl() { try { using (m_queue = new JobQueue(8)) { Stopwatch sw = new Stopwatch(); AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); sw.Reset(); sw.Start(); EditorUtility.DisplayProgressBar("Bake HLOD", "Initialize Bake", 0.0f); TerrainData data = m_hlod.TerrainData; m_size = data.size; m_heightmap = new Heightmap(data.heightmapResolution, data.heightmapResolution, data.size, data.GetHeights(0, 0, data.heightmapResolution, data.heightmapResolution)); string materialPath = AssetDatabase.GUIDToAssetPath(m_hlod.MaterialGUID); m_terrainMaterial = AssetDatabase.LoadAssetAtPath <Material>(materialPath); if (m_terrainMaterial == null) { m_terrainMaterial = new Material(Shader.Find("Standard")); } m_terrainMaterialInstanceId = m_terrainMaterial.GetInstanceID(); m_terrainMaterialName = m_terrainMaterial.name; using (m_alphamaps = new DisposableList <WorkingTexture>()) using (m_layers = new DisposableList <Layer>()) { for (int i = 0; i < data.alphamapTextures.Length; ++i) { m_alphamaps.Add(new WorkingTexture(Allocator.Persistent, data.alphamapTextures[i])); } for (int i = 0; i < data.terrainLayers.Length; ++i) { m_layers.Add(new Layer(data.terrainLayers[i])); } QuadTreeSpaceSplitter splitter = new QuadTreeSpaceSplitter(0.0f); SpaceNode rootNode = splitter.CreateSpaceTree(m_hlod.GetBounds(), m_hlod.ChunkSize * 2.0f, m_hlod.transform.position, null, progress => { }); EditorUtility.DisplayProgressBar("Bake HLOD", "Create mesh", 0.0f); using (DisposableList <HLODBuildInfo> buildInfos = CreateBuildInfo(data, rootNode)) { yield return(m_queue.WaitFinish()); //Write material & textures for (int i = 0; i < buildInfos.Count; ++i) { int curIndex = i; m_queue.EnqueueJob(() => { ISimplifier simplifier = (ISimplifier)Activator.CreateInstance(m_hlod.SimplifierType, new object[] { m_hlod.SimplifierOptions }); simplifier.SimplifyImmidiate(buildInfos[curIndex]); }); } EditorUtility.DisplayProgressBar("Bake HLOD", "Simplify meshes", 0.0f); yield return(m_queue.WaitFinish()); Debug.Log("[TerrainHLOD] Simplify: " + sw.Elapsed.ToString("g")); sw.Reset(); sw.Start(); EditorUtility.DisplayProgressBar("Bake HLOD", "Make border", 0.0f); for (int i = 0; i < buildInfos.Count; ++i) { HLODBuildInfo info = buildInfos[i]; m_queue.EnqueueJob(() => { for (int oi = 0; oi < info.WorkingObjects.Count; ++oi) { WorkingObject o = info.WorkingObjects[oi]; int borderVertexCount = m_hlod.BorderVertexCount * Mathf.RoundToInt(Mathf.Pow(2.0f, (float)info.Distances[oi])); using (WorkingMesh m = MakeBorder(o.Mesh, info.Heightmap, borderVertexCount)) { ReampUV(m, info.Heightmap); o.SetMesh(MakeFillHoleMesh(m)); } } }); } yield return(m_queue.WaitFinish()); Debug.Log("[TerrainHLOD] Make Border: " + sw.Elapsed.ToString("g")); sw.Reset(); sw.Start(); for (int i = 0; i < buildInfos.Count; ++i) { SpaceNode node = buildInfos[i].Target; HLODBuildInfo info = buildInfos[i]; if (node.HasChild() == false) { SpaceNode parent = node.ParentNode; node.ParentNode = null; GameObject go = new GameObject(buildInfos[i].Name); for (int wi = 0; wi < info.WorkingObjects.Count; ++wi) { WorkingObject wo = info.WorkingObjects[wi]; GameObject targetGO = null; if (wi == 0) { targetGO = go; } else { targetGO = new GameObject(wi.ToString()); targetGO.transform.SetParent(go.transform, false); } List <Material> materials = new List <Material>(); for (int mi = 0; mi < wo.Materials.Count; ++mi) { WorkingMaterial wm = wo.Materials[mi]; if (wm.NeedWrite() == false) { materials.Add(wm.ToMaterial()); continue; } Material mat = new Material(wm.ToMaterial()); string[] textureNames = wm.GetTextureNames(); for (int ti = 0; ti < textureNames.Length; ++ti) { WorkingTexture wt = wm.GetTexture(textureNames[ti]); Texture2D tex = wt.ToTexture(); tex.wrapMode = wt.WrapMode; mat.name = targetGO.name + "_Mat"; mat.SetTexture(textureNames[ti], tex); } mat.EnableKeyword("_NORMALMAP"); materials.Add(mat); } targetGO.AddComponent <MeshFilter>().sharedMesh = wo.Mesh.ToMesh(); targetGO.AddComponent <MeshRenderer>().sharedMaterials = materials.ToArray(); } go.transform.SetParent(m_hlod.transform, false); m_hlod.AddGeneratedResource(go); parent.Objects.Add(go); buildInfos.RemoveAt(i); i -= 1; } } //controller IStreamingBuilder builder = (IStreamingBuilder)Activator.CreateInstance(m_hlod.StreamingType, new object[] { m_hlod, m_hlod.StreamingOptions }); builder.Build(rootNode, buildInfos, m_hlod.gameObject, m_hlod.CullDistance, m_hlod.LODDistance, true, false, progress => { EditorUtility.DisplayProgressBar("Bake HLOD", "Storing results.", 0.75f + progress * 0.25f); }); Debug.Log("[TerrainHLOD] Build: " + sw.Elapsed.ToString("g")); } } EditorUtility.SetDirty(m_hlod.gameObject); } } finally { EditorUtility.ClearProgressBar(); GC.Collect(); } }
public static IEnumerator Create(HLOD hlod) { try { Stopwatch sw = new Stopwatch(); AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); sw.Reset(); sw.Start(); Bounds bounds = hlod.GetBounds(); List <GameObject> hlodTargets = ObjectUtils.HLODTargets(hlod.gameObject); ISpaceSplitter spliter = new QuadTreeSpaceSplitter(hlod.transform.position, 5.0f, hlod.MinSize); SpaceNode rootNode = spliter.CreateSpaceTree(bounds, hlodTargets, progress => { EditorUtility.DisplayProgressBar("Bake HLOD", "Splitting space", progress * 0.25f); }); List <HLODBuildInfo> buildInfos = CreateBuildInfo(rootNode, hlod.ThresholdSize); Debug.Log("[HLOD] Splite space: " + sw.Elapsed.ToString("g")); sw.Reset(); sw.Start(); ISimplifier simplifier = (ISimplifier)Activator.CreateInstance(hlod.SimplifierType, new object[] { hlod }); for (int i = 0; i < buildInfos.Count; ++i) { yield return(new BranchCoroutine(simplifier.Simplify(buildInfos[i]))); } yield return(new WaitForBranches(progress => { EditorUtility.DisplayProgressBar("Bake HLOD", "Simplify meshes", 0.25f + progress * 0.25f); })); Debug.Log("[HLOD] Simplify: " + sw.Elapsed.ToString("g")); sw.Reset(); sw.Start(); IBatcher batcher = (IBatcher)Activator.CreateInstance(hlod.BatcherType, new object[] { hlod }); batcher.Batch(buildInfos, progress => { EditorUtility.DisplayProgressBar("Bake HLOD", "Generating combined static meshes.", 0.5f + progress * 0.25f); }); Debug.Log("[HLOD] Batch: " + sw.Elapsed.ToString("g")); sw.Reset(); sw.Start(); try { AssetDatabase.StartAssetEditing(); IStreamingBuilder builder = (IStreamingBuilder)Activator.CreateInstance(hlod.StreamingType, new object[] { hlod }); builder.Build(rootNode, buildInfos, progress => { EditorUtility.DisplayProgressBar("Bake HLOD", "Storing results.", 0.75f + progress * 0.25f); }); Debug.Log("[HLOD] Build: " + sw.Elapsed.ToString("g")); sw.Reset(); sw.Start(); } finally { AssetDatabase.StopAssetEditing(); Debug.Log("[HLOD] Importing: " + sw.Elapsed.ToString("g")); } } finally { EditorUtility.ClearProgressBar(); } //hlod.Root = rootNode; }