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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by theAfrican · Jun 13, 2013 at 01:24 AM · terrainmeshdecalmesh-deformation

Real time terrain deformation after explosion

hi guys,

could some one point me in the right direction. i was creating an explosion after effect by placing a decal on the impact surface. similar to what is done in the boot camp demo. however, its no accurate when the terrain is non uniform.

i want to know how i would apply a terrain deformation approx 8m on the terrain and possibly apply a decal texture on it. what is the workflow?

check out demo here

UPDATE +++++++ to fly use the w - fly up s - go down a - tilt left d - tilt right

up - nose down down - ass up left - turn left right - turn right

tab - change view(important toggle) e - fire missile

if you go out of range notice the bf3 like effects

alt text

demo

helo.jpg (86.1 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by trs9556 · Jun 13, 2013 at 01:46 AM

I have no experience with this plugin, but check out http://blog.almostlogical.com/2010/06/10/real-time-terrain-deformation-in-unity3d/ Someone has already accomplished this.

Comment
Add comment · Show 1 · 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
avatar image theAfrican · Jun 13, 2013 at 02:36 AM 0
Share

WOW, lemme check this out. thx.

avatar image
2
Wiki

Answer by theAfrican · Jun 14, 2013 at 04:40 AM

Hi guys, i finally got round to doing this within unity. check out demo here , instructions on how to fly can be seen in first post. use 'e' to fire missile . im going to post the code and how its done. i modified a bit of David Reimers(Thx btw) code to allow for blast textures to be more prominent. i changed the method to DestroyTerrain to have another variable for increasing texture strength.

Now how do you perform terrain deformation?

1) attach the modified TerrainDeformer.cs to your current terrain. give your terrain a tag eg "Terrain";

2) add a texture in the terrain tools. remember that these are zero based. so the first texture would be 0 and so on.

3)in the oncollision of your rocket or bomb etc. add this code.

 var TextureDamageRadius:float=5f;
 var TerrainDamageRadius:float=5f;
         if (c.transform.tag=="Terrain")
             {
             c.gameObject.GetComponent("TerrainDeformer").DestroyTerrain(c.contacts[0].point,TerrainDamageRadius,TextureDamageRadius);
             }

Modified TerrainDeformer / Modified Method --DestroyTerrain() to include textureStrength

 // TerrainDeformer - Demonstrating a method modifying terrain in real-time. Changing height and texture
 //
 // released under MIT License
 // http://www.opensource.org/licenses/mit-license.php
 //
 //@author        Devin Reimer
 //@website         http://blog.almostlogical.com
 //Copyright (c) 2010 Devin Reimer
 /*
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 */
 using UnityEngine;
 using System.Collections;
 
 public class TerrainDeformer : MonoBehaviour
 {
     public int terrainDeformationTextureNum = 1;
     private Terrain terr; // terrain to modify
     protected int hmWidth; // heightmap width
     protected int hmHeight; // heightmap height
     protected int alphaMapWidth;
     protected int alphaMapHeight;
     protected int numOfAlphaLayers;
     protected const float DEPTH_METER_CONVERT=0.05f;
     protected const float TEXTURE_SIZE_MULTIPLIER = 1.25f;
     private float[,] heightMapBackup;
     private float[, ,] alphaMapBackup;
 
     void Start()
     {
         terr = this.GetComponent<Terrain>();
         hmWidth = terr.terrainData.heightmapWidth;
         hmHeight = terr.terrainData.heightmapHeight;
         alphaMapWidth = terr.terrainData.alphamapWidth;
         alphaMapHeight = terr.terrainData.alphamapHeight;
         numOfAlphaLayers = terr.terrainData.alphamapLayers;
         if (Debug.isDebugBuild)
         {
             heightMapBackup = terr.terrainData.GetHeights(0, 0, hmWidth, hmHeight);
             alphaMapBackup = terr.terrainData.GetAlphamaps(0, 0, alphaMapWidth, alphaMapHeight);   
         } 
     }
 
     //this has to be done because terrains for some reason or another terrains don't reset after you run the app
     void OnApplicationQuit()
     {
         if (Debug.isDebugBuild)
         {
             terr.terrainData.SetHeights(0, 0, heightMapBackup);
             terr.terrainData.SetAlphamaps(0, 0, alphaMapBackup);
         }
     }
 
     public void DestroyTerrain(Vector3 pos, float craterSizeInMeters,float textureSize)
     {
         DeformTerrain(pos,craterSizeInMeters);
         TextureDeformation(pos, craterSizeInMeters*textureSize);
     }
     
     protected void DeformTerrain(Vector3 pos, float craterSizeInMeters)
     {
         //get the heights only once keep it and reuse, precalculate as much as possible
         Vector3 terrainPos = GetRelativeTerrainPositionFromPos(pos,terr,hmWidth,hmHeight);//terr.terrainData.heightmapResolution/terr.terrainData.heightmapWidth
         int heightMapCraterWidth = (int)(craterSizeInMeters * (hmWidth / terr.terrainData.size.x));
         int heightMapCraterLength = (int)(craterSizeInMeters * (hmHeight / terr.terrainData.size.z));
         int heightMapStartPosX = (int)(terrainPos.x - (heightMapCraterWidth / 2));
         int heightMapStartPosZ = (int)(terrainPos.z - (heightMapCraterLength / 2));
 
         float[,] heights = terr.terrainData.GetHeights(heightMapStartPosX, heightMapStartPosZ, heightMapCraterWidth, heightMapCraterLength);
         float circlePosX;
         float circlePosY;
         float distanceFromCenter;
         float depthMultiplier;
 
         float deformationDepth = (craterSizeInMeters / 3.0f) / terr.terrainData.size.y;
 
         // we set each sample of the terrain in the size to the desired height
         for (int i = 0; i < heightMapCraterLength; i++) //width
         {
             for (int j = 0; j < heightMapCraterWidth; j++) //height
             {
                 circlePosX = (j - (heightMapCraterWidth / 2)) / (hmWidth / terr.terrainData.size.x);
                 circlePosY = (i - (heightMapCraterLength / 2)) / (hmHeight / terr.terrainData.size.z);
                 distanceFromCenter = Mathf.Abs(Mathf.Sqrt(circlePosX * circlePosX + circlePosY * circlePosY));
                 //convert back to values without skew
                 
                 if (distanceFromCenter < (craterSizeInMeters / 2.0f))
                 {
                     depthMultiplier = ((craterSizeInMeters / 2.0f - distanceFromCenter) / (craterSizeInMeters / 2.0f));
 
                     depthMultiplier += 0.1f;
                     depthMultiplier += Random.value * .1f;
 
                     depthMultiplier = Mathf.Clamp(depthMultiplier, 0, 1);
                     heights[i, j] = Mathf.Clamp(heights[i, j] - deformationDepth * depthMultiplier, 0, 1);
                 }
 
             }
         }
 
         // set the new height
        terr.terrainData.SetHeights(heightMapStartPosX, heightMapStartPosZ, heights);
     }
 
     protected void TextureDeformation(Vector3 pos, float craterSizeInMeters)
     {
         Vector3 alphaMapTerrainPos = GetRelativeTerrainPositionFromPos(pos, terr, alphaMapWidth, alphaMapHeight);
         int alphaMapCraterWidth = (int)(craterSizeInMeters * (alphaMapWidth / terr.terrainData.size.x));
         int alphaMapCraterLength = (int)(craterSizeInMeters * (alphaMapHeight / terr.terrainData.size.z));
 
         int alphaMapStartPosX = (int)(alphaMapTerrainPos.x - (alphaMapCraterWidth / 2));
         int alphaMapStartPosZ = (int)(alphaMapTerrainPos.z - (alphaMapCraterLength/2));
 
         float[, ,] alphas = terr.terrainData.GetAlphamaps(alphaMapStartPosX, alphaMapStartPosZ, alphaMapCraterWidth, alphaMapCraterLength);
 
         float circlePosX;
         float circlePosY;
         float distanceFromCenter;
 
         for (int i = 0; i < alphaMapCraterLength; i++) //width
         {
             for (int j = 0; j < alphaMapCraterWidth; j++) //height
             {
                 circlePosX = (j - (alphaMapCraterWidth / 2)) / (alphaMapWidth / terr.terrainData.size.x);
                 circlePosY = (i - (alphaMapCraterLength / 2)) / (alphaMapHeight / terr.terrainData.size.z);
 
                 //convert back to values without skew
                 distanceFromCenter = Mathf.Abs(Mathf.Sqrt(circlePosX * circlePosX + circlePosY * circlePosY));
 
                 
                 if (distanceFromCenter < (craterSizeInMeters / 2.0f))
                 {
                     for (int layerCount = 0; layerCount < numOfAlphaLayers; layerCount++)
                     {
                         //could add blending here in the future
                         if (layerCount == terrainDeformationTextureNum)
                         {
                             alphas[i, j, layerCount] = 1;
                         }
                         else
                         {
                             alphas[i, j, layerCount] = 0;
                         }
                     }
                 }
             }
         } 
 
        terr.terrainData.SetAlphamaps(alphaMapStartPosX, alphaMapStartPosZ, alphas);
     }
 
     protected Vector3 GetNormalizedPositionRelativeToTerrain(Vector3 pos, Terrain terrain)
     {
         //code based on: http://answers.unity3d.com/questions/3633/modifying-terrain-height-under-a-gameobject-at-runtime
         // get the normalized position of this game object relative to the terrain
         Vector3 tempCoord = (pos - terrain.gameObject.transform.position);
         Vector3 coord;
         coord.x = tempCoord.x / terr.terrainData.size.x;
         coord.y = tempCoord.y / terr.terrainData.size.y;
         coord.z = tempCoord.z / terr.terrainData.size.z;
 
         return coord;
     }
 
     protected Vector3 GetRelativeTerrainPositionFromPos(Vector3 pos,Terrain terrain, int mapWidth, int mapHeight)
     {
         Vector3 coord = GetNormalizedPositionRelativeToTerrain(pos, terrain);
         // get the position of the terrain heightmap where this game object is
         return new Vector3((coord.x * mapWidth), 0, (coord.z * mapHeight));
     }     
 }
 


Comment
Add comment · Show 1 · 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
avatar image TCD · Jul 21, 2014 at 09:15 PM 0
Share

Demo looks broken because it does not start.

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

18 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

Related Questions

Decaling! how to! Help! 2 Answers

Set Vertices to Ground 1 Answer

Project vertices on surface, make a shape 1 Answer

Placed Trees in Terrain are white planes? 1 Answer

Rivers on procedurally terrain 0 Answers


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