Created
December 21, 2016 04:42
-
-
Save runewake2/a6ba641c9c9c51da62530d6176ee932e to your computer and use it in GitHub Desktop.
Physically simulated mesh deformation. This file handles the mesh updates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
Why does Unity swear at [RequireComponent (typeof (GeneratePlaneMesh))]?
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
I think will work better with tessellating terrain deformation shader , I saw something here: https://www.youtube.com/watch?v=UVA5e_Yz2w4