Search Unity

Woobly triangle mesh

Discussion in 'General Graphics' started by supernamey923834, Sep 16, 2021.

  1. supernamey923834

    supernamey923834

    Joined:
    Jul 27, 2017
    Posts:
    10
    You could do this in a shader and should really, but heres how to do it in a script.

    Makes a triangle mesh and moves the points around in a woobly Persona 5 fashion

    EqyCiESCCp.gif

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. #if UNITY_EDITOR
    5. using UnityEditor;
    6.  
    7. #endif
    8.  
    9.  
    10. namespace Narf
    11. {
    12.  
    13. [ExecuteAlways]
    14. public class MeshTriMaker : MonoBehaviour
    15. {
    16.     public float width = 1;
    17.     public float height = 1;
    18.  
    19.     public Vector2 startingDisMinMax = new Vector2(-1f,1f);
    20.  
    21.     public Vector3 p0v;
    22.     public Vector3 p1v;
    23.     public Vector3 p2v;
    24.  
    25.     public bool canUpdate = true;
    26.  
    27.     public MeshRenderer meshRenderer;
    28.     public MeshFilter meshFilter;
    29.  
    30.     Vector3[] cachePositions;
    31.     Vector3[] tempPositions;
    32.  
    33.     Material _Material;
    34.  
    35.     public bool canUseNoise = false;
    36.  
    37.     void Start(){
    38.         Rebuild();
    39.     }
    40.  
    41.     void Update(){
    42.         if (canUpdate)
    43.         {
    44.             MorphPoints();
    45.         }
    46.         if (canUseNoise)
    47.         {
    48.             UseNoise();
    49.         }
    50.     }
    51.  
    52.     public void MorphPoints(){
    53.         tempPositions[0] = cachePositions[0] + p0v;
    54.         tempPositions[1] = cachePositions[1] + p1v;
    55.         tempPositions[2] = cachePositions[2] + p2v;
    56.  
    57.         // recompute centroid
    58.  
    59.         meshFilter.mesh.vertices = tempPositions;
    60.     }
    61.  
    62.     public Vector3 NoiseScale = new Vector2(1,1);
    63.     public float speed = 0.5f;
    64.     Vector3[] tempVerts;
    65.  
    66.     public float sample0;
    67.     public float sample1;
    68.     public float sample2;
    69.  
    70.     public float offset0 = 0;
    71.     public float offset1 = 10;
    72.     public float offset2 = 20;
    73.  
    74.     public float noiseMin = -1f;
    75.     public float noiseMax = 1f;
    76.  
    77.     public float seed = 0;
    78.  
    79.     public void UseNoise(){
    80.         sample0 = Mathf.PerlinNoise( (Time.time + seed) * speed * (NoiseScale.x + offset0), NoiseScale.y);
    81.         sample0 = Mathf.Lerp(noiseMin,noiseMax,sample0);
    82.         tempVerts[0].x = cachePositions[0].x + sample0;
    83.         tempVerts[0].y = cachePositions[0].y + sample0;
    84.  
    85.         sample1 = Mathf.PerlinNoise( (Time.time + seed) * speed * (NoiseScale.x + offset1), NoiseScale.y);
    86.         sample1 = Mathf.Lerp(noiseMin,noiseMax,sample1);
    87.         tempVerts[1].x = cachePositions[1].x + sample1;
    88.         tempVerts[1].y = cachePositions[1].y + sample1;
    89.  
    90.         sample2 = Mathf.PerlinNoise( (Time.time + seed) * speed * (NoiseScale.x + offset2), NoiseScale.y);
    91.         sample2 = Mathf.Lerp(noiseMin,noiseMax,sample2);
    92.         tempVerts[2].x = cachePositions[2].x + sample2;
    93.         tempVerts[2].y = cachePositions[2].y + sample2;
    94.  
    95.    
    96.  
    97.         meshFilter.mesh.vertices = tempVerts;
    98.     }
    99.  
    100.  
    101.     // wrong
    102.     public void ComputeCentroid(){
    103.         Vector3[] verts = meshFilter.mesh.vertices;
    104.         // Vector3 center = (verts[0] + verts[1] + verts[2]) / 3;
    105.         Vector3 center = (verts[0] + verts[1] + verts[2]) * 0.5f;
    106.         for (int i = 0; i < verts.Length; i++)
    107.         {
    108.             // verts[i] += center * 0.5f;
    109.             verts[i] += center;
    110.             // vertices[i].x -= 0.5f;
    111.             // vertices[i].y -= 0.5f;
    112.         }
    113.  
    114.         meshFilter.mesh.vertices = verts;
    115.     }
    116.  
    117.     public void SetStartRandomPositions(){
    118.         p0v.x = Random.Range(startingDisMinMax.x,startingDisMinMax.y);
    119.         p0v.y = Random.Range(startingDisMinMax.x,startingDisMinMax.y);
    120.         p0v.z = Random.Range(startingDisMinMax.x,startingDisMinMax.y);
    121.         p1v.x = Random.Range(startingDisMinMax.x,startingDisMinMax.y);
    122.         p1v.y = Random.Range(startingDisMinMax.x,startingDisMinMax.y);
    123.         p1v.z = Random.Range(startingDisMinMax.x,startingDisMinMax.y);
    124.         p2v.x = Random.Range(startingDisMinMax.x,startingDisMinMax.y);
    125.         p2v.y = Random.Range(startingDisMinMax.x,startingDisMinMax.y);
    126.         p2v.z = Random.Range(startingDisMinMax.x,startingDisMinMax.y);
    127.         MorphPoints();
    128.     }
    129.  
    130.     public void CachePositions(){
    131.         cachePositions = meshFilter.mesh.vertices;
    132.         tempPositions = meshFilter.mesh.vertices;
    133.         tempVerts = meshFilter.mesh.vertices;
    134.     }
    135.  
    136.     public void Rebuild()
    137.     {
    138.  
    139.         meshRenderer = gameObject.GetComponent<MeshRenderer>();
    140.         if (meshRenderer == null)
    141.         {
    142.             meshRenderer = gameObject.AddComponent<MeshRenderer>();      
    143.         }
    144.         if (meshRenderer.sharedMaterial == null)
    145.         {
    146.             meshRenderer.sharedMaterial = new Material(Shader.Find("Standard"));
    147.         }
    148.  
    149.    
    150.         meshFilter = gameObject.GetComponent<MeshFilter>();
    151.         if (meshFilter == null)
    152.         {
    153.             meshFilter = gameObject.AddComponent<MeshFilter>();
    154.        
    155.         }
    156.  
    157.  
    158.         Mesh mesh = new Mesh();
    159.  
    160.  
    161.         Vector3[] vertices = new Vector3[3]
    162.         {
    163.             new Vector3(-0.25f, -0.25f, 0),
    164.             new Vector3(0.5f, 0, 0),
    165.             new Vector3(0, 0.5f, 0)
    166.         };
    167.         // Vector3 center = (vertices[0] + vertices[1] + vertices[2]) / 3;
    168.         // // Vector3 center = (vertices[0] + vertices[1] + vertices[2]) * 0.5f;
    169.         for (int i = 0; i < vertices.Length; i++)
    170.         {
    171.             // vertices[i] += center * 0.5f;
    172.             vertices[i].x -= 0.5f;
    173.             vertices[i].y -= 0.5f;
    174.         }
    175.  
    176.         mesh.vertices = vertices;
    177.  
    178.         int[] tris = new int[3]
    179.         {
    180.             // lower left triangle
    181.             0, 2, 1
    182.             // upper right triangle
    183.             // 2, 3, 1
    184.         };
    185.         mesh.triangles = tris;
    186.  
    187.         Vector3[] normals = new Vector3[3]
    188.         {
    189.             -Vector3.forward,
    190.             -Vector3.forward,
    191.             -Vector3.forward
    192.         };
    193.         mesh.normals = normals;
    194.  
    195.         Vector2[] uv = new Vector2[3]
    196.         {
    197.             new Vector2(0, 0),
    198.             new Vector2(1, 0),
    199.             new Vector2(0, 1)
    200.         };
    201.         mesh.uv = uv;
    202.  
    203.         meshFilter.mesh = mesh;
    204.  
    205.         meshFilter.mesh.RecalculateBounds();
    206.         meshFilter.mesh.RecalculateNormals();
    207.  
    208.         CachePositions();
    209.     }
    210. }
    211.  
    212.  
    213.  
    214. // android errors and does not compile if this is not wrapping the editor
    215. #if UNITY_EDITOR
    216.  
    217. [CustomEditor(typeof(MeshTriMaker))]
    218. public class MeshTriMakerEditor : Editor
    219. {
    220.     public override void OnInspectorGUI()
    221.     {
    222.         DrawDefaultInspector();
    223.    
    224.         MeshTriMaker pick = (MeshTriMaker)target;
    225.  
    226.  
    227.            
    228.         if (GUILayout.Button("Rebuild", GUILayout.Height(30))) {
    229.             pick.Rebuild();
    230.         }
    231.                
    232.         if (GUILayout.Button("MorphPoints", GUILayout.Height(30))) {
    233.             pick.MorphPoints();
    234.         }
    235.                        
    236.         if (GUILayout.Button("SetStartRandomPositions", GUILayout.Height(30))) {
    237.             pick.SetStartRandomPositions();
    238.         }
    239.                                
    240.         if (GUILayout.Button("CachePositions", GUILayout.Height(30))) {
    241.             pick.CachePositions();
    242.         }
    243.                                        
    244.         if (GUILayout.Button("ComputeCentroid", GUILayout.Height(30))) {
    245.             pick.ComputeCentroid();
    246.         }
    247.    
    248.     }
    249.  
    250. }
    251.  
    252.  
    253. #endif
    254.  
    255. }
     
  2. BrandyStarbrite

    BrandyStarbrite

    Joined:
    Aug 4, 2013
    Posts:
    2,076
    Wow! Nice work there.