- Home /
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.
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
AFAI$$anonymous$$ vertices is in local coordinates... but no, *=2 does not work!! :-(
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
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...
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
Your answer
Follow this Question
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