Skip to content

Instantly share code, notes, and snippets.

@runewake2
Created December 21, 2016 04:42
Show Gist options
  • Save runewake2/a6ba641c9c9c51da62530d6176ee932e to your computer and use it in GitHub Desktop.
Save runewake2/a6ba641c9c9c51da62530d6176ee932e to your computer and use it in GitHub Desktop.
Physically simulated mesh deformation. This file handles the mesh updates.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
[RequireComponent(typeof(GeneratePlaneMesh))]
public class DeformableMesh : MonoBehaviour {
public float maximumDepression;
public List<Vector3> originalVertices;
public List<Vector3> modifiedVertices;
private GeneratePlaneMesh plane;
public void MeshRegenerated()
{
plane = GetComponent<GeneratePlaneMesh>();
plane.mesh.MarkDynamic();
originalVertices = plane.mesh.vertices.ToList();
modifiedVertices = plane.mesh.vertices.ToList();
Debug.Log("Mesh Regenerated");
}
public void AddDepression(Vector3 depressionPoint, float radius)
{
var worldPos4 = this.transform.worldToLocalMatrix * depressionPoint;
var worldPos = new Vector3(worldPos4.x, worldPos4.y, worldPos4.z);
for (int i = 0; i < modifiedVertices.Count; ++i)
{
var distance = (worldPos - (modifiedVertices[i] + Vector3.down * maximumDepression)).magnitude;
if (distance < radius)
{
var newVert = originalVertices[i] + Vector3.down * maximumDepression;
modifiedVertices.RemoveAt(i);
modifiedVertices.Insert(i, newVert);
}
}
plane.mesh.SetVertices(modifiedVertices);
Debug.Log("Mesh Depressed");
}
}
@AlexSherTemplar
Copy link

Why does Unity swear at [RequireComponent (typeof (GeneratePlaneMesh))]?

@diXime
Copy link

diXime commented Oct 3, 2018

Why does Unity swear at [RequireComponent (typeof (GeneratePlaneMesh))]?

Hello,
you probably need this one too:
https://gist.github.com/runewake2/b382ecd3abc3a32b096d08cc69c541fb
It needs another class. Any mesh custom class would do the trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment