private void DoImportSplats() { GSplatPrototypeGroup splatGroup = DesData.Shading.Splats; if (splatGroup == null || splatGroup == GGriffinSettings.Instance.TerrainDataDefault.Shading.Splats) { CreateNewSplatPrototypesGroup = true; } if (CreateNewSplatPrototypesGroup) { splatGroup = ScriptableObject.CreateInstance <GSplatPrototypeGroup>(); #if UNITY_EDITOR string path = AssetDatabase.GetAssetPath(DesData); string directory = Path.GetDirectoryName(path); string filePath = Path.Combine(directory, string.Format("Splats_{0}_{1}.asset", DesData.Id, System.DateTime.Now.Ticks)); AssetDatabase.CreateAsset(splatGroup, filePath); #endif DesData.Shading.Splats = splatGroup; } List <LPTSplatInfo> layers = SrcData.SurfaceSettings.Splats; GSplatPrototype[] prototypes = new GSplatPrototype[layers.Count]; for (int i = 0; i < layers.Count; ++i) { GSplatPrototype p = (GSplatPrototype)layers[i]; prototypes[i] = p; } splatGroup.Prototypes = new List <GSplatPrototype>(prototypes); int controlResolution = SrcData.SurfaceSettings.ControlResolution; DesData.Shading.SplatControlResolution = controlResolution; Texture[] alphaMaps = SrcData.SurfaceSettings.GetSplatControlTextures(); int maxControlCount = Mathf.Min(alphaMaps.Length, DesData.Shading.SplatControlMapCount); RenderTexture rt = new RenderTexture(controlResolution, controlResolution, 0, RenderTextureFormat.ARGB32); for (int i = 0; i < maxControlCount; ++i) { Texture2D controlMap = DesData.Shading.Internal_GetSplatControl(i); GCommon.CopyToRT(alphaMaps[i], rt); GCommon.CopyFromRT(controlMap, rt); controlMap.filterMode = alphaMaps[i].filterMode; } rt.Release(); GUtilities.DestroyObject(rt); DesData.SetDirty(GTerrainData.DirtyFlags.Shading); GC.Collect(); }
public static void Setup(HashSet <GStylizedTerrain> terrains) { GSplatPrototypeGroup splats = null; IEnumerator <GStylizedTerrain> iTerrains = terrains.GetEnumerator(); while (iTerrains.MoveNext()) { GStylizedTerrain t = iTerrains.Current; if (t == null) { continue; } Setup(t); if (t.TerrainData != null && t.TerrainData.Shading.Splats != null) { if (splats == null) { splats = t.TerrainData.Shading.Splats; } } } if (GMicroSplatIntegrationSettings.Instance.InitTextureEntries) { if (splats != null && currentTAConfig != null) { currentTAConfig.sourceTextures.Clear(); currentTAConfig.sourceTextures2.Clear(); currentTAConfig.sourceTextures3.Clear(); foreach (GSplatPrototype p in splats.Prototypes) { TextureArrayConfig.TextureEntry entry = new TextureArrayConfig.TextureEntry(); entry.diffuse = p.Texture; entry.normal = p.NormalMap; currentTAConfig.sourceTextures.Add(entry); currentTAConfig.sourceTextures2.Add(entry); currentTAConfig.sourceTextures3.Add(entry); } TextureArrayConfigEditor.CompileConfig(currentTAConfig); } } iTerrains.Reset(); while (iTerrains.MoveNext()) { GStylizedTerrain t = iTerrains.Current; if (t == null) { continue; } t.PushControlTexturesToMicroSplat(); } }
private void CreateSharedSplats() { SplatGroups.Clear(); SplatGroupIndices.Clear(); for (int i = 0; i < Terrains.Count; ++i) { Terrain t = Terrains[i]; #if UNITY_2018_3_OR_NEWER TerrainLayer[] layers = t.terrainData.terrainLayers; #else SplatPrototype[] layers = t.terrainData.splatPrototypes; #endif int splatGroupIndex = -1; for (int j = 0; j < SplatGroups.Count; ++j) { if (SplatGroups[j].Equals(layers)) { splatGroupIndex = j; break; } } if (splatGroupIndex >= 0) { SplatGroupIndices.Add(splatGroupIndex); } else { GSplatPrototypeGroup group = GSplatPrototypeGroup.Create(layers); SplatGroups.Add(group); SplatGroupIndices.Add(SplatGroups.Count - 1); } } #if UNITY_EDITOR if (!Application.isPlaying) { GUtilities.EnsureDirectoryExists(DataDirectory); for (int i = 0; i < SplatGroups.Count; ++i) { GSplatPrototypeGroup group = SplatGroups[i]; group.name = string.Format("{0}{1}_{2}", "SharedSplats", i.ToString(), ConversionName); string assetPath = string.Format("{0}.asset", Path.Combine(DataDirectory, group.name)); AssetDatabase.CreateAsset(group, assetPath); } } #endif }
private static void InjectTerrainGUI(GStylizedTerrain terrain, int order) { if (order != 3) { return; } string label = "MicroSplat Integration"; string id = "terrain-gui-microsplat-integration"; GEditorCommon.Foldout(label, false, id, () => { bool useMS = terrain.TerrainData.Shading.ShadingSystem == GShadingSystem.MicroSplat; useMS = EditorGUILayout.Toggle("Enable", useMS); terrain.TerrainData.Shading.ShadingSystem = useMS ? GShadingSystem.MicroSplat : GShadingSystem.Polaris; GEditorCommon.Separator(); EditorGUILayout.LabelField("Setup", EditorStyles.boldLabel); GMicroSplatIntegrationSettings settings = GMicroSplatIntegrationSettings.Instance; string dir = settings.DataDirectory; GEditorCommon.BrowseFolderMiniButton("Data Directory", ref dir); settings.DataDirectory = dir; settings.ShaderNamePrefix = EditorGUILayout.TextField("Shader Name Prefix", settings.ShaderNamePrefix); if (GUILayout.Button("Setup")) { MicroSplatPolarisMesh pm = terrain.gameObject.GetComponent <MicroSplatPolarisMesh>(); if (pm != null) { GUtilities.DestroyObject(pm); } MeshRenderer[] renderers = terrain.Internal_ChunkRoot.GetComponentsInChildren <MeshRenderer>(); List <MeshRenderer> rendererList = new List <MeshRenderer>(); rendererList.AddRange(renderers); MicroSplatPolarisMeshEditor.PolarisData data = new MicroSplatPolarisMeshEditor.PolarisData(); data.basePath = settings.DataDirectory; data.name = string.Format("{0}_{1}", settings.ShaderNamePrefix, terrain.TerrainData.Id); data.additionalKeywords = new string[0]; data.rootObject = terrain.gameObject; data.renderers = rendererList; MicroSplatPolarisMeshEditor.Setup(data); terrain.PushControlTexturesToMicroSplat(); if (terrain.TerrainData.Shading.Splats == null || terrain.TerrainData.Shading.Splats.Prototypes.Count == 0) { return; } pm = terrain.gameObject.GetComponent <MicroSplatPolarisMesh>(); if (pm == null) { return; } string materialPath = AssetDatabase.GetAssetPath(pm.templateMaterial); string directory = Path.GetDirectoryName(materialPath); string configPath = string.Format("{0}/MicroSplatConfig.asset", directory); TextureArrayConfig config = AssetDatabase.LoadAssetAtPath <TextureArrayConfig>(configPath); if (config != null) { terrain.TerrainData.Shading.MicroSplatTextureArrayConfig = config; GSplatPrototypeGroup splats = terrain.TerrainData.Shading.Splats; List <GSplatPrototype> prototypes = splats.Prototypes; for (int i = 0; i < prototypes.Count; ++i) { TextureArrayConfig.TextureEntry entry = new TextureArrayConfig.TextureEntry(); entry.diffuse = prototypes[i].Texture; entry.normal = prototypes[i].NormalMap; entry.aoChannel = TextureArrayConfig.TextureChannel.G; entry.heightChannel = TextureArrayConfig.TextureChannel.G; entry.smoothnessChannel = TextureArrayConfig.TextureChannel.G; entry.alphaChannel = TextureArrayConfig.TextureChannel.G; config.sourceTextures.Add(entry); } TextureArrayConfigEditor.CompileConfig(config); terrain.PullMaterialAndSplatMapsFromMicroSplat(); } } }); }
private void InitializeAssets() { if (CreateNewAsset) { DesData = new TerrainData(); DesData.SetDetailResolution(512, 32); #if UNITY_EDITOR GUtilities.EnsureDirectoryExists(DataDirectory); string fileName = string.Format("TerrainData_{0}.asset", SrcData.Id); string filePath = Path.Combine(DataDirectory, fileName); AssetDatabase.CreateAsset(DesData, filePath); #endif } if (ExportSplats) { #if !UNITY_2018_1 && !UNITY_2018_2 TerrainLayer[] layers = DesData.terrainLayers; DesData.terrainLayers = null; GSplatPrototypeGroup group = SrcData.Shading.Splats; if (group != null) { List <GSplatPrototype> prototypes = group.Prototypes; List <TerrainLayer> newLayers = new List <TerrainLayer>(); List <TerrainLayer> layerToSave = new List <TerrainLayer>(); for (int i = 0; i < prototypes.Count; ++i) { if (i < layers.Length && layers[i] != null && OverwriteSplatLayers) { prototypes[i].CopyTo(layers[i]); newLayers.Add(layers[i]); } else { TerrainLayer l = (TerrainLayer)prototypes[i]; layerToSave.Add(l); newLayers.Add(l); } } DesData.terrainLayers = newLayers.ToArray(); #if UNITY_EDITOR if (layerToSave.Count > 0) { GUtilities.EnsureDirectoryExists(DataDirectory); for (int i = 0; i < layerToSave.Count; ++i) { string fileName = string.Format("TerrainLayer_{0}_{1}.asset", layerToSave[i].diffuseTexture != null ? layerToSave[i].diffuseTexture.name : i.ToString(), System.DateTime.Now.Ticks); string filePath = Path.Combine(DataDirectory, fileName); AssetDatabase.CreateAsset(layerToSave[i], filePath); } } #endif } #else DesData.splatPrototypes = null; GSplatPrototypeGroup group = SrcData.Shading.Splats; if (group != null) { SplatPrototype[] layers = new SplatPrototype[group.Prototypes.Count]; for (int i = 0; i < layers.Length; ++i) { layers[i] = (SplatPrototype)group.Prototypes[i]; } DesData.splatPrototypes = layers; } #endif } }
private void DoImportSplats() { if (!ImportSplatControlMapsOnly) { GSplatPrototypeGroup splatGroup = DesData.Shading.Splats; if (splatGroup == null || splatGroup == GRuntimeSettings.Instance.shadingDefault.splats) { CreateNewSplatPrototypesGroup = true; } if (CreateNewSplatPrototypesGroup) { splatGroup = ScriptableObject.CreateInstance <GSplatPrototypeGroup>(); #if UNITY_EDITOR if (!Application.isPlaying) { string path = AssetDatabase.GetAssetPath(DesData); string directory = Path.GetDirectoryName(path); string filePath = Path.Combine(directory, string.Format("Splats_{0}_{1}.asset", DesData.Id, System.DateTime.Now.Ticks)); AssetDatabase.CreateAsset(splatGroup, filePath); } #endif DesData.Shading.Splats = splatGroup; } TerrainLayer[] layers = SrcData.terrainLayers; GSplatPrototype[] prototypes = new GSplatPrototype[layers.Length]; for (int i = 0; i < layers.Length; ++i) { if (layers[i] != null) { GSplatPrototype p = (GSplatPrototype)layers[i]; prototypes[i] = p; } else { prototypes[i] = new GSplatPrototype(); } } splatGroup.Prototypes = new List <GSplatPrototype>(prototypes); GCommon.SetDirty(splatGroup); } if (ImportSplatControlMapResolution) { DesData.Shading.SplatControlResolution = SrcData.alphamapResolution; } Texture2D[] alphaMaps = SrcData.alphamapTextures; int alphaMapCount = Mathf.Min(alphaMaps.Length, DesData.Shading.SplatControlMapCount); for (int i = 0; i < alphaMapCount; ++i) { try { Texture2D controlMap = DesData.Shading.GetSplatControl(i); //controlMap.SetPixels(alphaMaps[i].GetPixels()); //controlMap.Apply(); GCommon.CopyTexture(alphaMaps[i], controlMap); controlMap.filterMode = alphaMaps[i].filterMode; } catch (System.Exception e) { Debug.LogError(string.Format("Skip import splat alpha map {0}, error: {1}", alphaMaps[i].name, e.ToString())); } } if (ImportSplatsAsAlbedo) { DesData.Shading.ConvertSplatsToAlbedo(); } DesData.SetDirty(GTerrainData.DirtyFlags.Shading); //GC.Collect(); }
private void ImportDataAndCreateTerrain() { GameObject terrainRoot = new GameObject(string.Format("{0}-{1}", Root.name, ConversionName)); terrainRoot.transform.parent = Root.transform.parent; terrainRoot.transform.position = Root.transform.position; for (int i = 0; i < Terrains.Count; ++i) { #if UNITY_EDITOR GCommonGUI.CancelableProgressBar("Converting", "Converting " + Terrains[i].name, 1f); #endif GTerrainData data = ScriptableObject.CreateInstance <GTerrainData>(); if (DataTemplate != null) { DataTemplate.Geometry.CopyTo(data.Geometry); DataTemplate.Shading.CopyTo(data.Shading); DataTemplate.Rendering.CopyTo(data.Rendering); DataTemplate.Foliage.CopyTo(data.Foliage); DataTemplate.Mask.CopyTo(data.Mask); } else { if (Application.isPlaying) //Reset() only called in edit mode { data.Reset(); data.Geometry.Reset(); data.Shading.Reset(); data.Rendering.Reset(); data.Foliage.Reset(); data.Mask.Reset(); } } #if UNITY_EDITOR if (!Application.isPlaying) { string assetName = "TerrainData_" + data.Id; AssetDatabase.CreateAsset(data, string.Format("{0}.asset", Path.Combine(DataDirectory, assetName))); } #endif Material templateMaterial = null; if (DataTemplate != null) { templateMaterial = DataTemplate.Shading.CustomMaterial; } Material material = null; if (templateMaterial != null) { material = new Material(templateMaterial.shader); } else { if (ImportSplats) { if (ImportSplatsAsAlbedo && AlbedoUsage == GAlbedoUsage.ColorMap) { material = GRuntimeSettings.Instance.terrainRendering.GetClonedMaterial(GCommon.CurrentRenderPipeline, GLightingModel.PBR, GTexturingModel.ColorMap); } else if (ImportSplatsAsAlbedo && AlbedoUsage == GAlbedoUsage.VertexColor) { material = GRuntimeSettings.Instance.terrainRendering.GetClonedMaterial(GCommon.CurrentRenderPipeline, GLightingModel.PBR, GTexturingModel.VertexColor); data.Geometry.AlbedoToVertexColorMode = GAlbedoToVertexColorMode.Sharp; } else { GSplatPrototypeGroup splats = DataTemplate ? DataTemplate.Shading.Splats : SplatGroups[SplatGroupIndices[i]]; GSplatsModel splatModel = (splats == null || splats.Prototypes.Count <= 4) ? GSplatsModel.Splats4Normals4 : GSplatsModel.Splats8; material = GRuntimeSettings.Instance.terrainRendering.GetClonedMaterial(GCommon.CurrentRenderPipeline, GLightingModel.PBR, GTexturingModel.Splat, splatModel); } } else { material = GRuntimeSettings.Instance.terrainRendering.GetClonedMaterial(GCommon.CurrentRenderPipeline, GLightingModel.PBR, GTexturingModel.Splat, GSplatsModel.Splats4); } } data.Shading.CustomMaterial = material; #if UNITY_EDITOR if (!Application.isPlaying && material != null) { string matName = "TerrainMaterial_" + data.Id; material.name = matName; AssetDatabase.CreateAsset(material, string.Format("{0}.mat", Path.Combine(DataDirectory, matName))); } #endif if (ImportSplats) { data.Shading.Splats = DataTemplate ? DataTemplate.Shading.Splats : SplatGroups[SplatGroupIndices[i]]; data.Shading.UpdateMaterials(); } if (ImportTrees) { data.Foliage.Trees = DataTemplate ? DataTemplate.Foliage.Trees : TreeGroups[TreeGroupIndices[i]]; } if (ImportGrasses) { data.Foliage.Grasses = DataTemplate ? DataTemplate.Foliage.Grasses : GrassGroups[GrassGroupIndices[i]]; } GUnityTerrainDataImporter importer = new GUnityTerrainDataImporter(); importer.SrcData = Terrains[i].terrainData; importer.SrcTerrain = Terrains[i]; importer.DesData = data; importer.DesTerrain = null; importer.ImportGeometry = ImportGeometry; importer.UseUnityTerrainSize = true; importer.ImportSplats = ImportSplats; importer.ImportSplatsAsAlbedo = ImportSplatsAsAlbedo; importer.ImportSplatControlMapsOnly = DataTemplate != null && DataTemplate.Shading.Splats != null; importer.ImportSplatControlMapResolution = DataTemplate == null; importer.CreateNewSplatPrototypesGroup = false; importer.ImportTrees = ImportTrees; importer.ImportTreeInstancesOnly = DataTemplate != null && DataTemplate.Foliage.Trees != null; importer.CreateNewTreePrototypesGroup = false; importer.ImportGrasses = ImportGrasses; importer.ImportGrassInstancesOnly = DataTemplate != null && DataTemplate.Foliage.Grasses != null; importer.CreateNewGrassPrototypesGroup = false; importer.GrassDensity = 1; importer.SkipFoliageSnap = SkipFoliageSnap; importer.Import(); GStylizedTerrain t = CreateTerrain(); t.transform.parent = terrainRoot.transform; t.transform.position = Terrains[i].transform.position; t.name = Terrains[i].name; #if UNITY_2018_3_OR_NEWER t.GroupId = Terrains[i].groupingID; #endif t.TerrainData = data; if (ImportTrees || ImportGrasses) { data.Foliage.SetTreeRegionDirty(GCommon.UnitRect); data.Foliage.SetGrassRegionDirty(GCommon.UnitRect); t.UpdateTreesPosition(); t.UpdateGrassPatches(); data.Foliage.ClearTreeDirtyRegions(); data.Foliage.ClearGrassDirtyRegions(); } ConvertedTerrains.Add(t); #if UNITY_EDITOR //SaveAssets(); GCommonGUI.ClearProgressBar(); #endif } }