- Home /
Change size of Mesh at runtime
Hi,
I've spent the last few hours trying to see if there is a way to change the size of a mesh at runtime. Changing the transform scale is not an option for what I am trying to achieve (I am trying to fix a small Vectrosity issue). I can't seem to find anyway of doing this. Any pointers would be most welcome at this stage!
Thanks!
I'm not sure what you are asking here. You can modify the vertices of the mesh directly. But if Transform.localScale does not solve the issue, I'm not sure how modifying the mesh will help.
Check the Procedural Examples Tutorial on the asset store
Here is a link to the problem discussion robertbu: http://forum.unity3d.com/threads/49941-Vectrosity-fast-and-easy-line-drawing/page101?p=1337945&posted=1#post1338289
I am starting to wonder if I have grabbed the wrong end of the stick ;)
Ahhh, Thanks meat5000, very interesting got it working with a modified version of crumpled mesh! For anyone else:
#pragma strict
// This script is placed in public domain. The author takes no responsibility for any possible harm.
var scale = 1.0;
var recalculateNormals = false;
private var baseVertices : Vector3[];
function Update () {
var mesh : $$anonymous$$esh = GetComponent($$anonymous$$eshFilter).mesh;
if (baseVertices == null)
baseVertices = mesh.vertices;
var vertices = new Vector3[baseVertices.Length];
for (var i=0;i<vertices.Length;i++)
{
var vertex = baseVertices[i];
vertex.x += scale;
vertex.y += scale;
vertex.z += scale;
vertices[i] = vertex;
}
mesh.vertices = vertices;
if (recalculateNormals)
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
No problem. Glad to point you in the right direction and moreso that you got results!
Answer by code-blep · Aug 24, 2013 at 05:31 PM
Fixed Version:
#pragma strict
// This script is placed in public domain. The author takes no responsibility for any possible harm.
var scale = 1.0;
var recalculateNormals = false;
private var baseVertices : Vector3[];
function Update () {
var mesh : Mesh = GetComponent(MeshFilter).mesh;
if (baseVertices == null)
baseVertices = mesh.vertices;
var vertices = new Vector3[baseVertices.Length];
for (var i=0;i<vertices.Length;i++)
{
var vertex = baseVertices[i];
vertex.x = vertex.x * scale;
vertex.y = vertex.y * scale;
vertex.z = vertex.z * scale;
vertices[i] = vertex;
}
mesh.vertices = vertices;
if (recalculateNormals)
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
Up voted for meticulous problem solving when presented with a shred of a lead. Good job.
Hey this will DESTROY your art. Do not use it. You've been warned.
Answer by tapticc · Dec 19, 2016 at 06:27 PM
Found this useful thanks, here's a C# version:
public class Scaler : MonoBehaviour {
public float ScaleX = 1.0f;
public float ScaleY = 1.0f;
public float ScaleZ = 1.0f;
public bool RecalculateNormals = false;
private Vector3[] _baseVertices;
public void Update()
{
var mesh = GetComponent<MeshFilter>().mesh;
if (_baseVertices == null)
_baseVertices = mesh.vertices;
var vertices = new Vector3[_baseVertices.Length];
for (var i = 0; i < vertices.Length; i++)
{
var vertex = _baseVertices[i];
vertex.x = vertex.x * ScaleX;
vertex.y = vertex.y * ScaleY;
vertex.z = vertex.z * ScaleZ;
vertices[i] = vertex;
}
mesh.vertices = vertices;
if (RecalculateNormals)
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
}
Answer by Deaymon · Mar 31, 2019 at 10:33 PM
No one seemed to mention the simpler solution, so I thought I may add it just in case: Put the mesh into a child of that GameObject and scale that child's Transform...
Very simple, straight forward and easy as pie! So simple in fact, I might not even have thought about it. But it works like a charm, thanks very much!
Your answer
Follow this Question
Related Questions
Resize single face of a primitive object like a cube 0 Answers
Dynamic mesh positioning with Raycast intersection... 0 Answers
Generated mesh render order 0 Answers
Creating Irregular 2D Shapes in Runtime 0 Answers
disabling Mesh renderer on runtime. 1 Answer