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 jpthek9 · Oct 06, 2014 at 01:21 AM · gameobjectterrainconvertheightmap

How can I turn multiple objects into a single terrain?

I want to convert multiple game objects into a single terrain mesh or heightmap (not in runtime). Object2Terrain definitely won't work because of the number of objects in my map, so I'm hoping there's something I can do to create a terrain from a parent object for my game objects. It's not very important for me to get the individual meshes but at least a heightmap is needed. Note: For technical purposes, I need the single terrain to control all the varying heights of my scene.

Comment
Add comment · Show 4
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 robertbu · Oct 06, 2014 at 01:52 AM 0
Share

Runtime or edit time? Does the map need to be dynamic (i.e. do the objects change position or size at runtime? Can you give is an image of the objects you are talking about?

avatar image jpthek9 · Oct 06, 2014 at 02:01 AM 0
Share

Edit time, sorry for the confusion!

avatar image robertbu · Oct 06, 2014 at 02:15 AM 1
Share

There is the brute force method. Define the square area to sample, and then Raycast() down at a regular spacing to produce a sampling of say 513 x 513. The distance of the hit.point from some base gives you the height for the height map (may have to multiply that by a factor). For some layout of the objects you could get away with a Collider.Raycast() ins$$anonymous$$d of a Physics.Raycast().

avatar image jpthek9 · Oct 06, 2014 at 03:00 AM 0
Share

That sounds perfect! But I've never done something like this before. $$anonymous$$ind sharing some details? (as if you're $$anonymous$$ching a nub cuz you are ;))

1 Reply

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

Answer by robertbu · Oct 06, 2014 at 05:30 PM

Below is a script to get you started. To use:

  • Attach the script to a Unity Quad

  • Rotate the Quad 90 degrees on the 'x' (90,0,0)

  • Position and scale the Quad so that it is above your objects. The Quad defines the snapshot taken of the objects below.

  • Drag and drop a terrain on the 'terrain' variable. Note that if you don't, it will use the active terrain (if any).

  • Set 'maxDist'. This defines how far the floor of the terrain is from the plane.

  • Run the app

Note that the changes to the terrain will persist after the app is run, so you can run the app, then delete the Quad.

So given this layout of objects:

alt text

You will get a terrain that looks like this:

alt text

Note the cylinder at the top. Unity puts a capsule collider on a cylinder, so the terrain has a capsule instead of a cylinder.

 using UnityEngine;
 using System.Collections;
 
 public class MapTheTerrain : MonoBehaviour {
 
     public Terrain terrain;
     public float maxDist = 1.85f; // distance to floor of terrain
 
     void Start () {
         if (terrain == null) terrain = Terrain.activeTerrain;
         if (terrain == null) return; 
 
         int size = (int)terrain.terrainData.heightmapResolution;
 
         float[,] heights = new float[size,size];
 
         float delta = 1.0f / size;
         Vector2 testPos = new Vector2(-0.5f, -0.5f);
 
         for (int i = 0; i < size; i++) {
             for (int j = 0; j < size; j++) {
                 RaycastHit hit;
                 Vector3 pos = transform.TransformPoint(testPos);
                 if (Physics.Raycast(pos+0.001f * Vector3.down, Vector3.down, out hit)) {
                     float dist = Mathf.Clamp(hit.distance, 0.0f, maxDist);
                     heights[i,j] = (maxDist - dist) / maxDist;
                 }
                 else {
                     heights[i,j] = 0.0f;
                 }
                 testPos.x += delta;
             }
             testPos.x = -0.5f;
             testPos.y += delta;
             int row = i + 1;
         }
         terrain.terrainData.SetHeights (0,0,heights);
     }
 }



objtoterrain1.png (80.7 kB)
objtoterrain2.png (139.9 kB)
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 jpthek9 · Oct 06, 2014 at 05:37 PM 0
Share

Omgomgomg thank you so much!

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

27 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 avatar image

Related Questions

Is it possible to generate a high-poly spherical model from a mercator-projection heightmap? 1 Answer

Can I set a non-power-of-2 sized heightmap to terrain? 1 Answer

Why rotating the Terrain Collider does not work? 0 Answers

Convert player world position into terrain detail layer coordinates (GetDetailLayer) 2 Answers

Terrain Layers- Colormaps, and Textures. 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