Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Klasmic · Jan 03, 2017 at 10:01 PM · treesprocedural meshprocedural-generation

Read a meshes vertices y

ive been working on procedural generation but have reached a stump i am trying to read the meshes vertices y axis and place the tree there however it is not working as although there is some minor height difference t is generating trees in rock or trees that float my only explanation is that its reading of my mesh prefab instead of the mesh its on here's my tree generator script

using System.Collections; using System.Collections.Generic; using UnityEngine; public class TreeGenerator : MonoBehaviour { %|989621565_1|% %|-928628789_2|% %|862334518_3|% %|1311841752_4|% %|1291387829_5|% %|1287716956_6|% %|22742717_7|% %|-1875811060_8|% Bounds bounds = mesh.bounds; %|1443603913_10|% %|-480323926_11|% %|-837706877_12|% uvs[i] = new Vector2(vertices[i].x / bounds.size.x, vertices[i].z / bounds.size.x); %|1354973747_14|% } %|-691866681_16|% %|90604035_17|% %|1464498411_18|% %|-190301410_19|% %|1908611606_20|% %|-1415211618_21|% %|-1634019256_22|% %|-1673780791_23|% %|110908501_24|% %|152907145_25|% %|-1231979335_26|% // Update is called once per frame %|842214724_28|% %|-636683182_29|% }

Comment
Add comment · Show 1
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 Klasmic · Jan 02, 2017 at 05:34 PM 0
Share

ok since my script didn't show up properly here's my actual script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TreeGenerator : $$anonymous$$onoBehaviour {
 
     public GameObject tree;
     public int instanceCount;
 
 
 
 
     // Use this for initialization
     void Start()
     {
 
         $$anonymous$$esh mesh = GetComponent<$$anonymous$$eshFilter>().mesh;
         Vector3[] vertices = mesh.vertices;
         Vector2[] uvs = new Vector2[vertices.Length];
         Bounds bounds = mesh.bounds;
         int i = 0;
         while (i < uvs.Length)
         {
             uvs[i] = new Vector2(vertices[i].x / bounds.size.x, vertices[i].z / bounds.size.x);
             i++;
         }
         mesh.uv = uvs;
 
         for (int v = 0; v < instanceCount; v++)
         {
 
 
 
             if (vertices[v].y < 25)
             {
 
                 Vector3 treePos = new Vector3(UnityEngine.Random.Range(-1000, 1000), mesh.vertices[v].y, UnityEngine.Random.Range(-1000, 1000));
                 Instantiate(tree, treePos, Quaternion.identity);
                 
 
 
             }
 
         }
     }
 
 
 
             // Update is called once per frame
             void Update () {
 
 
 
     }
 }

 

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by gagehembach · Jan 08, 2017 at 06:39 PM

Scrap all this. This is much more simple. However might not be something you want to do. Basically what I did was make a treeGen script which I attached to the terrainchunks on start with this line:

var fbss = meshObject.AddComponent(); Add that line inside the endlessterrain script in the terrain chunk class in your public terrain chunk.

Make a script first which generates trees

public class example : MonoBehaviour { int i = 0;

 public void Start()
 {
     StartCoroutine(GenerateTree());
 }

 IEnumerator GenerateTree()
 {
     yield return new WaitForSeconds(.2f);
     GameObject tree1 = GameObject.FindGameObjectWithTag("Tree");

     Mesh mesh = GetComponent<MeshFilter>().mesh; //Get the mesh filter of the gameobject we are connected to (Should be the terrain.)
     Vector3[] vertices = mesh.vertices; // Create an array and reference it to all of the vertices in the terrain. (basically create an array that lists all of the vertices)

     for (int j = 0; j < vertices.Length; j++) // For every single vertex in array (and by extension the terrain)
     {
         int treerange = Random.Range(0, 45);

         Vector3 position = transform.TransformPoint(vertices[j]); // Get the position of the vertex we're on.
             if (position.y > 2 && position.y < 10)
             {
                     Instantiate(tree1, position, Quaternion.identity);
             }
         }
         yield break;
 }

THIS MAY NOT BE THE IDEAL METHOD. BECAUSE DEPENDENT ON YOUR LOD Is how many trees can spawn. The lower the lod the less vertices the map has. THIS CAN BE A PROBLEM. GLITCHY BUT WORKS :D Hit me up and maybe we can work together on this problem.

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

Answer by Cherno · Jan 08, 2017 at 06:04 PM

The vertex positions of a mesh are in local space, with the GameObject's transform.position (or origin, pivot point, however you want to call it) as the 0,0,0. That means that the vertex positions are all offset by the transform.position xyz values. With procedural mesh generation, I find it easiest to just make the generated mesh have it's GameObject be at world position 0,0,0 so all (local) vertex positions correspond to world positions.

If you don't want to do this, you just have to offset the y value of the tree position by the GameObject's transform.position.y value. Of course, the y - 25 condition you are checking also needs to have that offset applied.

   if (vertices[v].y + transform.position.y < 25)
          {
              Vector3 treePos = new Vector3(UnityEngine.Random.Range(-1000, 1000), mesh.vertices[v].y + transform.position.y, UnityEngine.Random.Range(-1000, 1000));
              Instantiate(tree, treePos, Quaternion.identity);
          }
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

6 People are following this question.

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

Related Questions

Bake mesh collider on separate thread? 0 Answers

What is wrong with this mesh editing code? 0 Answers

How to apply PBR material to Procedurally generated Mesh? 1 Answer

Mesh generation issue (C#) 1 Answer

How to cut a hole in a wall? 3 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