I’m using a script from this tutorial to create a “cube sphere” and saved its mesh. And then use its MeshDeformer script to deform my mesh during runtime. The mesh sort of needs to be detailed when I have only one ball on the screen. But when I’ve got multiple balls and the camera a bit further away, I’d like to reduce the mesh size of these balls. But as the script stores vertices and stuff for the deformed mesh I don’t know how to change the mesh size without deleting all the mesh deformation info.
I’m not that good at mesh related stuff so I’d appreciate if someone can explain to me how to do it or show me some code.
Also If you’ve got any suggestions for improving the AddDeformingForce or UpdateVertex functions from the MeshDeformation Script it would help me out a lot in terms of performance.
Below is the entire MeshDeformer script to make it easier.
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(MeshFilter))]
public class MeshDeformer : MonoBehaviour {
public float springForce = 20f;
public float damping = 5f;
public float uniformScale = 1f;
public float speed = 10f;
public float snapDistance = 1.0f;
public float blobAnimForce = -100f;
Mesh deformingMesh;
Vector3[] originalVertices, displacedVertices;
Vector3[] vertexVelocities;
Vector3 blobSpikeStart;
Vector3 blobSpikeEnd;
Vector3 slerpedVec3;
void Start () {
deformingMesh = GetComponent<MeshFilter>().mesh;
originalVertices = deformingMesh.vertices;
displacedVertices = new Vector3[originalVertices.Length];
for (int i = 0; i < originalVertices.Length; i++) {
displacedVertices[i] = originalVertices[i];
}
vertexVelocities = new Vector3[originalVertices.Length];
blobSpikeStart = Random.onUnitSphere + transform.position;
}
void Update () {
if (Vector3.Distance(slerpedVec3, Vector3.Slerp(blobSpikeStart, blobSpikeEnd, Time.time * speed)) < snapDistance){
blobSpikeEnd = Random.onUnitSphere + transform.position;
blobSpikeStart = slerpedVec3;
}
slerpedVec3 = Vector3.Slerp(blobSpikeStart, blobSpikeEnd, Time.time * speed);
//Quaternion.Slerp(blobSpikeStart, blobSpikeEnd, Time.time * speed);
AddDeformingForce((slerpedVec3 * this.transform.localScale.x), blobAnimForce);
uniformScale = transform.localScale.x;
for (int i = 0; i < displacedVertices.Length; i++) {
UpdateVertex(i);
}
deformingMesh.vertices = displacedVertices;
deformingMesh.RecalculateNormals();
}
void UpdateVertex (int i) {
Vector3 velocity = vertexVelocities[i];
Vector3 displacement = displacedVertices[i] - originalVertices[i];
displacement *= uniformScale;
velocity -= displacement * springForce * Time.deltaTime;
velocity *= 1f - damping * Time.deltaTime;
vertexVelocities[i] = velocity;
displacedVertices[i] += velocity * (Time.deltaTime / uniformScale);
}
public void AddDeformingForce (Vector3 point, float force) {
point = transform.InverseTransformPoint(point);
for (int i = 0; i < displacedVertices.Length; i++) {
AddForceToVertex(i, point, force);
}
}
void AddForceToVertex (int i, Vector3 point, float force) {
Vector3 pointToVertex = displacedVertices[i] - point;
pointToVertex *= uniformScale;
float attenuatedForce = force / (1f + pointToVertex.sqrMagnitude);
float velocity = attenuatedForce * Time.deltaTime;
vertexVelocities[i] += pointToVertex.normalized * velocity;
}
}