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 DayzSergeant · Sep 17, 2017 at 09:19 AM · meshverticesboundsprocedural meshorigin

Getting mesh length (x,z)

So I am currently trying to spawn world objects onto my procedural mesh but the two solutions that I have come up with work but not the way I want them to.

Example 1. Getting all the vertices associated with the mesh.

         ![alt text][1]Mesh mesh = meshObject.GetComponent<MeshFilter>().sharedMesh;
         RaycastHit hit;
         for (int i = 0; i < mesh.vertices.Length; i++)
         {
             if (Physics.Raycast(new Vector3(mesh.vertices[i].x, 500f, mesh.vertices[i].z), Vector3.down, out hit, Mathf.Infinity))
             {
                 if (hit.point.y > foliage[0].minHeight && hit.point.y < foliage[0].maxHeight)
                 {
                     spawnLocations.Add(hit.point);
                 }
             }
         }

This works perfectly except that it causes a great amount of lag for a bit of time. I would not have to worry about the lag if it wasn't noticeable or was very short but it is noticeable and it's not short so I sadly can't use this method along side generating my terrain.

Example 2. Getting the bounds of the mesh

     Mesh mesh = meshObject.GetComponent<MeshFilter>().sharedMesh;
     RaycastHit hit;
     for (int x = 0; x < mesh.bounds.size.x; x++)
     {
         for (int z = 0; z < mesh.bounds.size.z; z++)
         {
             if (Physics.Raycast(new Vector3(x, 500f, z), Vector3.down, out hit, Mathf.Infinity))
             {
                 if (hit.point.y > foliage[0].minHeight && hit.point.y < foliage[0].maxHeight)
                 {
                     spawnLocations.Add(hit.point);
                 }
             }
         }
     }

Again this works but this as well, but the result of this is not in anyway what I want out of this, it seems to be getting the origin of the mesh and getting the bounds starting from there. This would not be a problem but I don't know how to change the pivot point of a script generated mesh.

[1]: /storage/temp/102020-1.png

I've tried everything I know to no avail and I will continue to keep trying but if I could get some help/advice with this it would be greatly appreciated.

1.png (67.5 kB)
Comment
Add comment · Show 3
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 Cherno · Sep 17, 2017 at 09:36 AM 1
Share

I don't know how to change the pivot point of a script generated mesh

You can't. It's not possible in Unity, at least not without some custom functionality that edits the model file directly.

It would help if you would state what exactly are you trying to do. Also, have you tried bounds.$$anonymous$$ and .max?

avatar image DayzSergeant Cherno · Sep 17, 2017 at 09:54 AM 0
Share

Explain more of what I want to do, ok. I am trying to spawn a gameobject onto my terrain that I have created within script, I have two ways to do it but one causes great lag in the editor and in the game. And the other one starts from the center of my terrain mesh and looping from there giving me a incorrect bounds, it should start at the edge and start looping inward giving me the result that I want which is why I was wondering about the pivot change, but since that's not possible that's off the table I guess.

And as for the bounds.$$anonymous$$ and .max I don't see a way to loop through them since they are two values and not one. I need to get the x,z "bounds" of the entire mesh + that gives a smaller number than before so I still would not loop through all of the mesh. looping through the bounds.size gives me 94 this gives me 47

avatar image DayzSergeant DayzSergeant · Sep 17, 2017 at 09:58 AM 0
Share

So I just did bounds.$$anonymous$$.x and bounds.$$anonymous$$.z and the bounds still starts in the center bounds.max gives me the same values as $$anonymous$$ so that's no beuno

1 Reply

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

Answer by Bunny83 · Sep 17, 2017 at 10:15 AM

Ok lets tackle your questions one by one as they seem to aim for something very different.

Is there any particular reason why you want the spawn position at the vertices of the mesh?

If you want to use the vertex positions you have to store the vertices array in a local variable. Currently you access the vertices property three times each iteration. Every time you read the vertices property Unity will allocate a new array with all vertices. So you end up with tons of garbage that need to be collected. Also the allocation is quite slow and unnecessary.

So you can do this:

 Vector3[] verts = mesh.vertices; // allocate array once
 for (int i = 0; i < verts.Length; i++)
 {
     if (Physics.Raycast(new Vector3(verts[i].x, 500f, verts[i].z), Vector3.down, out hit, Mathf.Infinity))
     // ....

This should probably improve your performance by a factor of 100.

If you want to use the bounds instead, don't use the Mesh.bounds. Mesh.bounds is defined in the local space of the mesh. Use Renderer.bounds.

Also you shouldn't start your x and y at "0" but at bounds.min and let it end at bounds.max. Specifically something like this:

 Bounds bounds = meshObject.GetComponent<Renderer>().bounds;
 Vector3 min = bounds.min;
 Vector3 max = bounds.max;
 for (float x = min.x; x < max.x; x++)
 {
     for (float z = min.z; z < max.z; z++)
     {

Keep in mind that "x++" increments in whole world units. You may want to define some sort of step size to control how fine your grid is. Be careful to not use a too small step size. Imagine the mesh has a bounds of min.x = -5 and max.x = 5. If you use a step of "0.1" for example you get "100" steps along the x axis. If you have the same for z you end up with 100*100 == 10000 points. While using a step size of "1" you just have 10 * 10 == 100 points across the mesh. Tweak it as you need it.

 for (float x = min.x; x < max.x; x+=xStep)
 {
     for (float z = min.z; z < max.z; z+=zStep)
     {
Comment
Add comment · Show 3 · 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 Bunny83 · Sep 17, 2017 at 10:25 AM 0
Share

ps: you actually have another "error" with your vertices approach. The vertices are defined in local space but you directly use them as world space coordinates. This would only work when the actual mesh object is located at the world origin (has a position of 0,0,0) and it must not be rotated (so local and world space are identical).

The bounds approach works fine if the mesh isn't located at 0,0,0 but it also must not be rotated. If you may need it rotated you have to use $$anonymous$$esh.bounds which gives you local space coordinates and then transform every local space coordinate into world space. Either with transform.TransformPoint or by using the localToWorld matrix of the object.

avatar image DayzSergeant Bunny83 · Sep 17, 2017 at 10:37 AM 0
Share

I'm not really wanting to use the vertices method as it freezes up the editor and the game it was just a method I found when trying to spawn gameobjects onto this mesh. I will try out your solution to see if it works but thanks for posting :)

avatar image DayzSergeant Bunny83 · Sep 17, 2017 at 10:45 AM 0
Share

The bounds version works great and is what I was looking for thanks so much for the help :)

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

79 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

Related Questions

Do the Bounds of an Mesh Change When you Change its Vertices Positions? 1 Answer

Mesh Deformation 2 Answers

Manipulating vertices through colliders 1 Answer

How can I remove redundant vertices in custom mesh? 0 Answers

Holes in procedural mesh 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