/// <summary> /// Calculates the smallest Y/Z position based on the raycast hit and brushsettings /// </summary> /// <param name="hit">Raycast hit position</param> /// <param name="brush">The brush used</param> int CalculateYbase(Vector3 hit, TerrainBrush brush) { // get position of the hit as percentile of the terrain Y axis float tempYCoord = (hit - m_Terrain.gameObject.transform.position).z / m_Terrain.terrainData.size.z; // get the position of the terrain heightmap where the hit is int posYInTerrain = (int)(tempYCoord * m_HmHeight); int posYrest = 0; bool posYoverflow = false; if (posYInTerrain + m_Brush.m_BrushHeight / 2 > m_HmHeight) { posYrest = (posYInTerrain + m_Brush.m_BrushHeight / 2) % m_HmHeight; posYoverflow = true; } else if (posYInTerrain - m_Brush.m_BrushHeight / 2 < 0) { posYrest = (posYInTerrain - m_Brush.m_BrushHeight / 2) % m_HmHeight; } brush.m_BrushHeight = Mathf.Abs(m_ModificationDiameter - Mathf.Abs(posYrest)); int offsetY = brush.m_BrushHeight / 2; return(Mathf.Clamp(posYoverflow ? posYInTerrain + offsetY : posYInTerrain - offsetY, 0, m_HmHeight - brush.m_BrushHeight)); }
/// <summary> /// Calculates the smallest X position based on the raycast hit and brushsettings /// </summary> /// <param name="hit">Raycast hit position</param> /// <param name="brush">The brush used</param> /// <returns></returns> int CalculateXbase(Vector3 hit, TerrainBrush brush) { // get position of the hit as percentile of the terrain X axis float tempXCoord = (hit - m_Terrain.gameObject.transform.position).x / m_Terrain.terrainData.size.x; // get the position of the terrain heightmap where the hit is int posXInTerrain = (int)(tempXCoord * m_HmWidth); int posXrest = 0; bool posXoverflow = false; if (posXInTerrain + m_Brush.m_BrushWidth / 2 > m_HmWidth) { posXrest = (posXInTerrain + m_Brush.m_BrushWidth / 2) % m_HmWidth; posXoverflow = true; } else if (posXInTerrain - m_Brush.m_BrushWidth / 2 < 0) { posXrest = (posXInTerrain - m_Brush.m_BrushWidth / 2) % m_HmWidth; } brush.m_BrushWidth = Mathf.Abs(m_ModificationDiameter - Mathf.Abs(posXrest)); int offsetX = brush.m_BrushWidth / 2; return(Mathf.Clamp(posXoverflow ? posXInTerrain + offsetX : posXInTerrain - offsetX, 0, m_HmWidth - brush.m_BrushWidth)); }
/// <summary> /// Initialize the variables /// </summary> private void Awake() { m_Terrain = Terrain.activeTerrain; m_HmWidth = m_Terrain.terrainData.heightmapWidth; m_HmHeight = m_Terrain.terrainData.heightmapHeight; m_AssignSplatMap = GetComponent <AssignSplatMap>(); m_OldTerrainData = m_Terrain.terrainData.GetHeights(0, 0, m_HmWidth, m_HmHeight); m_Brush = new TerrainBrush(m_MinHeight, m_MaxHeight); m_TerrainWater = transform.parent.Find("Terrain").Find("Water").GetComponent <Water>(); }