Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Stephanides · Nov 25, 2014 at 05:46 PM · terrainbrush

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);
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges