Skip to content

Instantly share code, notes, and snippets.

@runewake2
Created December 21, 2016 04:42
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save runewake2/a6ba641c9c9c51da62530d6176ee932e to your computer and use it in GitHub Desktop.
Save runewake2/a6ba641c9c9c51da62530d6176ee932e to your computer and use it in GitHub Desktop.

Revisions

  1. runewake2 created this gist Dec 21, 2016.
    42 changes: 42 additions & 0 deletions DeformableMesh.cs
    @@ -0,0 +1,42 @@
    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");
    }
    }