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 += normals _* Mathf.Sin(Time.time) ;_
 _//Debug.Log(vertices*);*_
 _*}*_
 _*mesh.vertices = vertices;*_
_*}*_
_*
*_

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


I tried vertices+=normals_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.

*_

Something simple like

vertices _*= 2;_
*```*
*<p>should do it, though depends whether the vertices are centered around 0,0,0 or not</p>*
*<p>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</p>*

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