- Home /
Raise and Dig Terrain
Hellou, i have a litlle problem. I have method for Raise terrain. It works good if i have terrain width 1000 and terrain length 1000, if i make terrain for example 5000x5000 brush is same but hill is 5 x bigger or if i make terrain 200x200 brush is same but hill is 5 x smaller.. I add here two methods where i make hills or dig but i dont use terraindata.size or something like that, so i dont understand. Thanks for all help
private float ApplyBrush(float height, float brushStrength, int x, int y)
{
if (this.mode == TerrainEditMode.Height)
return height + (brush.shiftDown == false ? brushStrength : +brushStrength);
else if (this.mode == TerrainEditMode.Kopanie) {
return height - (brush.shiftDown == false ? brushStrength : - brushStrength);
}
else if (this.mode == TerrainEditMode.Flatten)
{
if (this.normalizedTargetHeight > height)
{
height += brushStrength;
height = Mathf.Min(height, this.normalizedTargetHeight);
return height;
}
else
{
// height = Mathf.Lerp(height, this.normalizedTargetHeight, Time.deltaTime);
height -= brushStrength;
height = Mathf.Max(height, this.normalizedTargetHeight);
return height;
}
}
else if (this.mode == TerrainEditMode.Smooth) {
float smoothed = Mathf.Lerp(height, this.Smooth(x, y), brushStrength);
return smoothed;
}
else
return height;
}
/// <summary>
/// Paints the height. returns if edited or not
/// </summary>
/// <returns><c>true</c>, if height was painted, <c>false</c> otherwise.</returns>
/// <param name="xCenter">X center.</param>
/// <param name="yCenter">Y center.</param>
public bool PaintHeight(float xCenter, float yCenter)
{
int xSize = (int)xCenter;
int ySize = (int)yCenter;
int halfSize = brush.Size / 2;
int remainder = brush.Size % 2;
bool changedHeight = false;
int xBase = Mathf.Clamp(xSize - halfSize , 0, this.terrainData.heightmapWidth);
int yBase = Mathf.Clamp(ySize - halfSize , 0, this.terrainData.heightmapHeight);
int xEnd = Mathf.Clamp(xSize + halfSize + remainder, 0, this.terrainData.heightmapWidth);
int yEnd = Mathf.Clamp(ySize + halfSize + remainder, 0, this.terrainData.heightmapHeight);
int width = xEnd - xBase;
int height = yEnd - yBase;
if (width == 0 || height == 0)
{
return (xBase == (0 | terrainData.heightmapWidth - 1) || yBase == (0 | terrainData.heightmapWidth - 1) || xEnd == (0 | terrainData.heightmapWidth - 1) || yEnd == (0 | terrainData.heightmapWidth - 1));
}
float[,] heights = terrainData.GetHeights(xBase, yBase, width, height);
for (int yIndex = 0; yIndex < height; ++yIndex)
{
for (int xIndex = 0; xIndex < width; ++xIndex)
{
float strengthInt = this.brush.GetStrengthInt(xBase + xIndex - (xSize - halfSize), yBase + yIndex - (ySize - halfSize));
float strength = strengthInt * brush.strength * .5f;
if(mode != TerrainEditMode.Smooth)
strength *= Time.deltaTime;
float newHeight = this.ApplyBrush(heights[yIndex, xIndex], strength, xIndex + xBase, yIndex + yBase);
if(changedHeight == false && heights[yIndex, xIndex] != newHeight)
changedHeight = true;
heights[yIndex, xIndex] = newHeight; //why unity why?
}
}
this.terrainData.SetHeights(xBase, yBase, heights);
if (mode == TerrainEditMode.Flatten && changedHeight == false) //fixes seam problems
{
changedHeight = (xBase == (0 | terrainData.heightmapWidth - 1) || yBase == (0 | terrainData.heightmapWidth - 1) || xEnd == (0 | terrainData.heightmapWidth - 1) || yEnd == (0 | terrainData.heightmapWidth - 1));
Debug.Log(xBase + " " + yBase + " " + xEnd + " " + yEnd);
}
return changedHeight;
}
public void EditHeights(Terrain terrain, Vector3 mousePos)
{
TerrainUndoManager.AddActionToUndo(terrain, terrain.terrainData.GetHeights(0, 0, terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapHeight));
int xRes = (terrain.terrainData.heightmapWidth);
int zRes = (terrain.terrainData.heightmapHeight);
float x =(((mousePos.x - terrain.transform.position.x) / terrain.terrainData.size.x) * xRes);
float z =(((mousePos.z - terrain.transform.position.z) / terrain.terrainData.size.z) * zRes);
terrainData = terrain.terrainData;
PaintHeight((int)x, (int)z);
}
Answer by Stephanides · Nov 25, 2014 at 06:02 PM
I know that if i change resolution to 1000 it works good but then i cant dig into terrain, in resolution 250 i can make dig
Your answer
Follow this Question
Related Questions
Terrain brush not matching with cursor. 1 Answer
No terrain brushes 2 Answers
Terrain brush weirdness 3 Answers
Where I can change default brush size fo terrain editor? 1 Answer
I cannot paint textures on Terrain except the first one. 1 Answer