public static TerrainChunk CreateChunk(MarchingRegion region, MarchingTerrain terrain, Material material, bool generate = true) { var chunkGameObject = new GameObject($"chunk({region.X},{region.Y},{region.Z})"); var chunk = chunkGameObject.AddComponent <TerrainChunk>(); chunkGameObject.transform.position = new Vector3(region.X, region.Y, region.Z); chunk.x = region.X / region.Width; chunk.y = region.Y / region.Height; chunk.z = region.Z / region.Length; chunk.Terrain = terrain; chunk.region = region; chunk.marching = new Marching(); chunk.meshFilter = chunk.AddComponentIfNotAdded <MeshFilter>(); chunk.meshRenderer = chunk.AddComponentIfNotAdded <MeshRenderer>(); chunk.meshRenderer.material = material; if (generate) { chunk.Generate(); } return(chunk); }
private static void NoiseCell(int x, int y, int z, float power, MarchingTerrain terrain) { float s0 = terrain.terrainConfiguration.terrainMap.GetSurface(x, y, z); float surface = s0 + Random.value * power; terrain.terrainConfiguration.terrainMap.SetSurface(x, y, z, surface); }
public static void Execute(MarchingTerrain terrain, BrushProps props, bool invert) { Vector3 point = props.point; float radius = props.brushRadius; float opacity = props.brushOpacity; int start_x = Mathf.RoundToInt(point.x - radius); int start_y = Mathf.RoundToInt(point.y - radius); int start_z = Mathf.RoundToInt(point.z - radius); int diameter = Mathf.RoundToInt(radius * 2); List <TerrainChunk> chunks = new List <TerrainChunk>(); for (int x = start_x; x < start_x + diameter; x++) { for (int y = start_y; y < start_y + diameter; y++) { for (int z = start_z; z < start_z + diameter; z++) { int c_x = x / MarchingTerrain.m_chunkSize; int c_y = y / MarchingTerrain.m_chunkSize; int c_z = z / MarchingTerrain.m_chunkSize; if (!chunks.Find(_chunk => _chunk.X == c_x && _chunk.Y == c_y && _chunk.Z == c_z)) { var temp = terrain.GetChunk(c_x, c_y, c_z); if (temp != null) { chunks.Add(temp); } } float power = Vector3.Distance(point, new Vector3(x, y, z)) / radius; power = Mathf.Clamp01((1F - power) * opacity); if (invert) { terrain.terrainConfiguration.terrainMap.AddSurface(x, y, z, -power); } else { terrain.terrainConfiguration.terrainMap.AddSurface(x, y, z, power); } } } } chunks.ForEach(x => x?.Generate()); }
private static void SmoothCell(int x, int y, int z, float power, MarchingTerrain terrain) { float s0 = terrain.terrainConfiguration.terrainMap.GetSurface(x, y, z); float s1 = terrain.terrainConfiguration.terrainMap.GetSurface(x + 1, y, z); float s2 = terrain.terrainConfiguration.terrainMap.GetSurface(x - 1, y, z); float s3 = terrain.terrainConfiguration.terrainMap.GetSurface(x, y, z + 1); float s4 = terrain.terrainConfiguration.terrainMap.GetSurface(x, y, z - 1); float s5 = terrain.terrainConfiguration.terrainMap.GetSurface(x, y + 1, z); float s6 = terrain.terrainConfiguration.terrainMap.GetSurface(x, y - 1, z); float surface = (s0 + s1 + s2 + s3 + s4 + s5 + s6) / 7; terrain.terrainConfiguration.terrainMap.SetSurface(x, y, z, Mathf.Lerp(s0, surface, power)); }
private void InitializeEditor() { MarchingTerrain terrain = (MarchingTerrain)target; m_width = terrain.Width; m_height = terrain.Height; m_length = terrain.Length; if (m_hotkeyUtility == null) { m_hotkeyUtility = new HotkeyUtility(); } if (m_terrainEditorUtility == null) { m_terrainEditorUtility = new TerrainEditorUtility(); } }
private void UseTool(MarchingTerrain terrain) { if (!IsReady()) { return; } switch (m_terrainEditorUtility.currentProps.currentToolType) { case ToolType.CircleBrush: CircleBrush.Execute(terrain, m_terrainEditorUtility.currentProps, m_hotkeyUtility.IsInvertedMode()); break; case ToolType.SphereBrush: SphereBrush.Execute(terrain, m_terrainEditorUtility.currentProps, m_hotkeyUtility.IsInvertedMode()); break; case ToolType.Smooth: SmoothBrush.Execute(terrain, m_terrainEditorUtility.currentProps, m_hotkeyUtility.IsInvertedMode()); break; case ToolType.Noise: NoiseBrush.Execute(terrain, m_terrainEditorUtility.currentProps, m_hotkeyUtility.IsInvertedMode()); break; case ToolType.Paint: PaintBrush.Execute(terrain, m_terrainEditorUtility.currentProps, m_hotkeyUtility.IsInvertedMode()); break; } }
public static void Execute(MarchingTerrain terrain, BrushProps props, bool invert) { float radius = props.brushRadius; float opacity = props.brushOpacity; Vector3 point = props.point; int start_x = (int)(point.x - radius); int start_y = (int)(point.y - radius); int start_z = (int)(point.z - radius); int diameter = (int)(radius * 2); List <TerrainChunk> chunks = new List <TerrainChunk>(); for (int x = start_x; x < start_x + diameter; x++) { for (int y = start_y; y < start_y + diameter; y++) { for (int z = start_z; z < start_z + diameter; z++) { int c_x = x / MarchingTerrain.m_chunkSize; int c_y = y / MarchingTerrain.m_chunkSize; int c_z = z / MarchingTerrain.m_chunkSize; if (!chunks.Find(_chunk => _chunk.X == c_x && _chunk.Y == c_y && _chunk.Z == c_z)) { var temp = terrain.GetChunk(c_x, c_y, c_z); if (temp != null) { chunks.Add(temp); } } terrain.terrainConfiguration.terrainMap.SetColor(x, y, z, props.color); } } } chunks.ForEach(x => x?.Generate()); }
private void OnTerrainDown(MarchingTerrain terrain) { UseTool(terrain); }
private void OnTerrainMove(MarchingTerrain terrain) { }
private void OnTerrainUp(MarchingTerrain terrain) { }
public override void OnInspectorGUI() { if (!IsReady()) { InitializeEditor(); return; } MarchingTerrain terrain = (MarchingTerrain)target; var configuration = (TerrainConfiguration)EditorGUILayout.ObjectField("Configuration", terrain.terrainConfiguration, typeof(TerrainConfiguration), false); if (terrain.terrainConfiguration != configuration) { terrain.terrainConfiguration = configuration; ProcessingUtility.StartProcessing(terrain.Rebuild()); } if (terrain.terrainConfiguration != null) { var level = EditorGUILayout.IntField("Level", terrain.Level); m_width = EditorGUILayout.IntField("Width", m_width); m_height = EditorGUILayout.IntField("Height", m_height); m_length = EditorGUILayout.IntField("Length", m_length); m_terrainEditorUtility.currentProps.currentToolType = (ToolType)EditorGUILayout.EnumPopup("Tool", m_terrainEditorUtility.currentProps.currentToolType); m_terrainEditorUtility.currentProps.brushRadius = EditorGUILayout.Slider("Radius", m_terrainEditorUtility.currentProps.brushRadius, 1, 25); m_terrainEditorUtility.currentProps.brushOpacity = EditorGUILayout.Slider("Opacity", m_terrainEditorUtility.currentProps.brushOpacity, 0.001F, 1F); if (m_terrainEditorUtility.currentProps.currentToolType == ToolType.Paint) { m_terrainEditorUtility.currentProps.color = EditorGUILayout.ColorField("Color", m_terrainEditorUtility.currentProps.color); } if (GUILayout.Button("Fill")) { terrain.terrainConfiguration.terrainMap.Fill(0, 0, 0, terrain.Width, terrain.Height - 1, terrain.Length, 1); ProcessingUtility.StartProcessing(terrain.Rebuild()); } level = Mathf.Clamp(level, 1, MarchingTerrain.m_chunkSize / 2); if (terrain.Level != level) { terrain.Level = level; ProcessingUtility.StartProcessing(terrain.SetLevel(level)); } if (terrain.Width != m_width || terrain.Height != m_height || terrain.Length != m_length) { if (GUILayout.Button("Resize")) { terrain.terrainConfiguration.Resize(m_width, m_height, m_length); ProcessingUtility.StartProcessing(terrain.Rebuild()); } } if (GUILayout.Button("Rebuild")) { ProcessingUtility.StartProcessing(terrain.Build()); } if (GUILayout.Button("Export")) { Mesh mesh = ProcessingUtility.StartProcessing(terrain.BuildMesh()) as Mesh; if (mesh != null) { string path = EditorUtility.SaveFilePanel("Exporting mesh", "Assets", "terrain", "obj"); MeshExporter.MeshToFile(mesh, path); } } if (GUILayout.Button("Save")) { terrain.terrainConfiguration.Save(); } } }