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 sycozflipzz · Jun 24, 2020 at 08:33 PM · terrainterrain bugterrain-trees

how do i use height map data for use in coding?

I was using a script that would auto generate a terrain and heightmap for that terrain and forest with the trees and bushes placements depending on the heightmap of the terrain but im trying to change it so the forest will auto generate over an already made terrain but the forrest spawns all the trees and bushes at the same height and i dont know how to adjust it to go with the heightmap of the pre made terrain

Here is the current script for the terrain and forest generator

public class TerrainGenerator : MonoBehaviour {

public int terrainSize; public float terrainScale; public float heightMultiplier;

public ForestGenerator Forest;

private float[,] heightMap;

MeshFilter meshFilter; MeshRenderer meshRenderer; Material material;

private void Start() {

  meshRenderer = gameObject.AddComponent<MeshRenderer>();
  meshFilter = gameObject.AddComponent<MeshFilter>();
  material = new Material(Shader.Find("Standard"));
 
  // Validation check to ensure terrainSize is not smaller than forestSize, as this will create an indexOutOfBounds exception.
  if (terrainSize < Forest.forestSize)
      terrainSize = Forest.forestSize;
 
  heightMap = new float[terrainSize, terrainSize];
 
 
 
  GenerateHeightMap();
  Forest.Generate(heightMap);
  gameObject.AddComponent<MeshCollider>();
  Generate();
  

}

float GetHeight(Vector3 position) {

  // Return a float representing the height of the terrain at the given position in world space.
  return Mathf.PerlinNoise((position.x + 0.1f) / terrainSize * terrainScale, (position.z + 0.1f) / terrainSize * terrainScale) * heightMultiplier;
 

}

void GenerateHeightMap() {

  // Loop through each position in our terrain, get the height at that position, and put it in our heightMap array.
  for (int x = 0; x < terrainSize; x++)
  {
      for (int z = 0; z < terrainSize; z++)
      {
 
          heightMap[x, z] = GetHeight(new Vector3(x, 0f, z));
 
      }
  }
 

} // generates the terrain void Generate() {

  // Initialise arrays and indexes we need for creating our mesh.
  Vector3[] vertices = new Vector3[terrainSize * terrainSize];
  int[] triangles = new int[(terrainSize - 1) * (terrainSize - 1) * 6];
  Vector2[] uvs = new Vector2[terrainSize * terrainSize];
  int vertIndex = 0;
  int triIndex = 0;
  // Loop through each position in terrain, create necessary vertexs, indices, and UV coordinates.
  for (int x = 0; x < terrainSize; x++)
  {
      for (int z = 0; z < terrainSize; z++)
      {
          vertices[vertIndex] = new Vector3(x, heightMap[x, z], z);
          uvs[vertIndex] = new Vector2((float)(x / terrainSize), (float)(z / terrainSize));
          // Make sure we're not on the last row/column of the mesh as setting a triangle from there would create an indexOutOfBoundsException.
          if (x < terrainSize - 1 && z < terrainSize - 1)
          {
              triangles[triIndex] = vertIndex;
              triangles[triIndex + 1] = vertIndex + terrainSize + 1;
              triangles[triIndex + 2] = vertIndex + terrainSize;
              triangles[triIndex + 3] = vertIndex + terrainSize + 1;
              triangles[triIndex + 4] = vertIndex;
              triangles[triIndex + 5] = vertIndex + 1;
              triIndex += 6;
          }
          vertIndex++;
      }
  }
  // Create a new mesh and apply our mesh data to it.
  Mesh mesh = new Mesh
  {
      vertices = vertices,
      triangles = triangles,
      uv = uvs
  };
 
  // Finish off the mesh and set our meshFilter to use it.
  mesh.RecalculateNormals();
  meshFilter.mesh = mesh;
  // Create a simple texture. It's only one colour so it doesn't need to be bigger than 1x1.
  Texture2D texture = new Texture2D(1, 1);
  texture.SetPixel(0, 0, Color.green);
  texture.Apply();
  // Apply the texture to our material and apply the material to our MeshRenderer.
  material.mainTexture = texture;
  meshRenderer.material = material;
  

}

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

0 Replies

· Add your reply
  • Sort: 

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

155 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 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 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 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 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 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

Unity tree colliders acting very strange 0 Answers

Renderer puts Speedtrees above terrain somehow 1 Answer

Placing Trees using terrain question 0 Answers

Trees not being placed correctly by script on unity terrain. 2 Answers

How can i get terrain data from Terrain.asset? 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