So the title kind of explains it. I have a mesh deformation script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.ProBuilder;
using UnityEngine.UIElements;
public class MeshesDeformation : MonoBehaviour
{
[SerializeField] private MeshFilter[] meshFilters = default;
[SerializeField] private MeshCollider[] colliders = default;
[SerializeField] private float impactDamage = 1f;
[SerializeField] private float deformationRadius = 0.5f;
[SerializeField] private float maxDeformation = 0.5f;
[SerializeField] private float minVelocity = 2f;
private float delayTimeDeform = 0.5f;
private float minVertsDistanceToRestore = 0.05f;
private float vertsRestoreSpeed = 5f;
private Vector3[][] originalVertices;
private float nextTimeDeform = 1f;
private bool isRepairing = false;
private bool isRepaired = false;
private Texture2D noiseTex;
float sample;
float xCoord;
float yCoord;
private void Start()
{
noiseTex = new Texture2D(24, 24);
originalVertices = new Vector3[meshFilters.Length][];
for (int i = 0; i < meshFilters.Length; i++)
{
originalVertices _= meshFilters*.mesh.vertices;*_
meshFilters*.mesh.MarkDynamic();*
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.U))
{
if (!isRepairing)
{
isRepairing = true;
}
}
RestoreMesh();
}
private void DeformationMesh(Mesh mesh, Transform localTransform, Vector3 contactPoint, Vector3 contactVelocity, int i)
{
bool hasDeformated = false;
Vector3 localContactPoint = localTransform.InverseTransformPoint(contactPoint);
Vector3 localContactForce = localTransform.InverseTransformDirection(contactVelocity);
Vector3[] vertices = mesh.vertices;
for (int j = 0; j < vertices.Length; j++)
{
float distance = (localContactPoint - vertices[j]).magnitude;
if (distance <= deformationRadius)
{
vertices[j] += (localContactForce) * (deformationRadius - distance) * impactDamage;
Vector3 deformation = vertices[j] - originalVertices*[j];*
if (deformation.magnitude > maxDeformation)
{
vertices[j] = originalVertices_[j] + deformation.normalized * maxDeformation;
}_
hasDeformated = true;
}
}
if (hasDeformated)
{
mesh.vertices = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
if (colliders.Length > 0)
{
if (colliders != null)
{
colliders*.sharedMesh = mesh;*
}
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (Time.time > nextTimeDeform)
{
if (collision.relativeVelocity.magnitude > minVelocity)
{
isRepaired = false;
Vector3 contactPoint = collision.contacts[0].point;
Vector3 contactVelocity = collision.relativeVelocity * 0.01f;
for (int i = 0; i < meshFilters.Length; i++)
{
if (meshFilters != null)
{
DeformationMesh(meshFilters_.mesh, meshFilters*.transform, contactPoint, contactVelocity, i);
}
}*_
nextTimeDeform = Time.time + delayTimeDeform;
}
}
}
private void RestoreMesh()
{
if (!isRepaired && isRepairing)
{
isRepaired = true;
for (int i = 0; i < meshFilters.Length; i++)
{
Mesh mesh = meshFilters*.mesh;*
Vector3[] vertices = mesh.vertices;
Vector3[] origVerts = originalVertices*;*
for (int j = 0; j < vertices.Length; j++)
{
vertices[j] += (origVerts[j] - vertices[j]) * Time.deltaTime * vertsRestoreSpeed;
if ((origVerts[j] - vertices[j]).magnitude > minVertsDistanceToRestore)
{
isRepaired = false;
}
}
mesh.vertices = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
if (colliders != null)
{
colliders*.sharedMesh = mesh;*
}
}
if (isRepaired)
{
isRepairing = false;
for (int i = 0; i < meshFilters.Length; i++)
{
if (colliders != null)
{
colliders_.sharedMesh = meshFilters*.mesh;
}
}
}
}
}
}*
and I want to add some noise to the deformed area of the mesh. making the effect more realistic. I am shore its simple enough but I cant do it. Thanks._