private void OnBeforeDrag(BaseHandle handle) { m_isDragging = m_selectedHandles != null; if (m_isDragging) { handle.EnableUndo = false; m_oldHeightmap = GetHeightmap(); m_oldState = m_state.Save(); } }
public void CutHoles() { GameObject[] objects = m_terrainHandlesSelection.Selection.gameObjects; if (objects != null && objects.Length > 0) { float[,] heightMap = GetHeightmap(); TerrainToolState.Record state = m_state.Save(); objects = CreateAndApplyCutoutTexture(objects); RecordState(heightMap, state); } }
private void RecordState(float[,] oldHeightmap, TerrainToolState.Record oldState) { Terrain terrain = Terrain; float[,] newHeightmap = GetHeightmap(); TerrainToolState.Record newState = m_state.Save(); m_oldHeightmap = null; m_oldState = null; m_editor.Undo.CreateRecord(redoRecord => { if (terrain.terrainData != null) { terrain.SetHeights(0, 0, newHeightmap); } if (m_state != null) { m_state.Load(newState); } UpdateHandlePositions(); InitAdditiveAndInterpolatedHeights(); return(true); }, undoRecord => { if (terrain.terrainData != null) { terrain.SetHeights(0, 0, oldHeightmap); } if (m_state != null) { m_state.Load(oldState); } UpdateHandlePositions(); InitAdditiveAndInterpolatedHeights(); return(true); }); }
public bool Refresh(Action redo = null, Action undo = null) { TerrainData data = TerrainData; if (m_state == null) { return(false); } if (m_state.HeightMap.Length == data.heightmapResolution * data.heightmapResolution && m_state.Grid.Length == Mathf.FloorToInt(m_state.ZSize / ZSpacing + 1) * Mathf.FloorToInt(m_state.XSize / XSpacing + 1) && m_state.ZSize == data.size.z && m_state.XSize == data.size.x) { return(false); } m_terrainHandlesSelection.Selection.activeGameObject = null; TerrainToolState.Record oldState = m_state.Save(); m_state.ZSize = data.size.z; m_state.XSize = data.size.x; m_xCount = m_state.XSize / XSpacing + 1; m_zCount = m_state.ZSize / ZSpacing + 1; m_state.Grid = new float[Mathf.FloorToInt(m_zCount) * Mathf.FloorToInt(m_xCount)]; m_state.HeightMap = new float[data.heightmapResolution * data.heightmapResolution]; m_state.CutoutTexture = m_cutoutMaskRenderer.CreateMask(data, null); m_state.ZSpacing = m_state.ZSize / (m_zCount - 1); m_state.XSpacing = m_state.XSize / (m_xCount - 1); InitAdditiveAndInterpolatedHeights(); if (IsEnabled) { CreateHandles(); } TerrainToolState.Record newState = m_state.Save(); m_editor.Undo.CreateRecord(redoRecord => { if (redo != null) { redo(); } if (m_state != null) { m_state.Load(newState); } m_terrainHandlesSelection.Selection.activeGameObject = null; XSpacing = m_state.XSpacing; ZSpacing = m_state.ZSpacing; m_xCount = m_state.XSize / XSpacing + 1; m_zCount = m_state.ZSize / ZSpacing + 1; InitAdditiveAndInterpolatedHeights(); if (IsEnabled) { CreateHandles(); } return(true); }, undoRecord => { if (undo != null) { undo(); } if (m_state != null) { m_state.Load(oldState); } m_terrainHandlesSelection.Selection.activeGameObject = null; XSpacing = m_state.XSpacing; ZSpacing = m_state.ZSpacing; m_xCount = m_state.XSize / XSpacing + 1; m_zCount = m_state.ZSize / ZSpacing + 1; InitAdditiveAndInterpolatedHeights(); if (IsEnabled) { CreateHandles(); } return(true); }); return(true); }