- Home /
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|% }
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 () {
}
}
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.
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);
}
Your answer
Follow this Question
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