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
1
Question by ina · Dec 30, 2010 at 08:08 AM · meshproceduralvertices

How do you scale a sphere or object by 2x using Mesh.vertices

So, from the Mesh manual page, a function like this will "puffer" or sinusoidally increase and decrease your object.

function puffer(){

 var mesh : Mesh = GetComponent(MeshFilter).mesh;
 var vertices : Vector3[] = mesh.vertices;
 var normals : Vector3[] = mesh.normals;

 for (var i = 0; i < vertices.Length; i++){
     vertices[i] += normals[i] * Mathf.Sin(Time.time) ;
     //Debug.Log(vertices[i]);
     }



 mesh.vertices = vertices;

}

How do you use mesh, vertices, normals and stuff to simply double the size of your mesh?

I tried vertices[i]+=normals[i]*2; in the above, but that does not do anything... It looks like the Time.time is a major part of producing any change in the procedural geometry - why is that?

Note: I realize this is overkill for such an operation, but it's just a learning snippet to try to understand mesh, vertices, and how they relate to Unity geometry.

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

2 Replies

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

Answer by Mike 3 · Dec 30, 2010 at 08:27 AM

Something simple like

vertices[i] *= 2;

should do it, though depends whether the vertices are centered around 0,0,0 or not

If a vertex on the right hand side is at 10,0,0, then doubling that will put it at 20,0,0 and if done to the other vertices, should make it look double the size

Comment
Add comment · Show 15 · 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 ina · Dec 30, 2010 at 09:11 AM 0
Share

I tried that as well - nope!!

avatar image ina · Dec 30, 2010 at 09:12 AM 0
Share

AFAI$$anonymous$$ vertices is in local coordinates... but no, *=2 does not work!! :-(

avatar image Mike 3 · Dec 30, 2010 at 09:47 AM 0
Share

Works fine for me. Are you asking how to make it sinusoidally change between 1x and 2x scale? As the code I provided just doubles the size of the mesh when called

avatar image ina · Dec 30, 2010 at 10:04 AM 0
Share

i don't notice a change in the geometry of the sphere - it's not 2x bigger in the camera, and no values change in inspector...

avatar image ina · Dec 30, 2010 at 10:05 AM 0
Share

tried it in both start() and update() ... no effect!

Show more comments
avatar image
0

Answer by HarvesteR · Apr 05, 2011 at 10:20 PM

Unity's normals aren't what you would expect of them... they're not actually pointing up, but forward. They're in tangent space, so they're not much use to 'push' an object like that.

The easiest way is to just multiply the vertex vectors, like Mike said.

Mesh mesh = GetComponent<MeshFilter>().mesh; // grab a reference to your mesh
Vector3[] vertices = mesh.vertices;     // copy it onto an array
for(int v = 0; v < mesh.vertexCount; v++)
{
    vertices[v] *= maxExtension * scalarValue; // displace them
}
mesh.vertices = vertices; // put them back in the mesh

here, maxExtent would be the maximum amount of vertex displacement. values lower than one would contract the mesh, and values above one would expand it.

the scalarValue is a value ranging from zero to one, like Mathf.Sin(Time.time) it regulates the amount of displacement.

This code should, of course, be running on Update(). (except for the GetComponent bit, that can be put in Start, provided that mesh is declared outside any functions)

Cheers

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

2 People are following this question.

avatar image avatar image

Related Questions

UV-ing a sphere procedurally 1 Answer

Selecting a single polygon / face at runtime 1 Answer

Highmap on a cube. 0 Answers

mesh-morph scaling badly 1 Answer

reading vertices from a mesh always returns Vector3.zero 2 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