Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. // visually scale this object in global space around some arbitrary vector
  6. public class SquashA : MonoBehaviour
  7. {
  8.     // A reference mesh that will never be deformed
  9.     public Transform MeshObject;
  10.     Mesh origMesh;
  11.     Vector3[] origVerts;
  12.  
  13.     // copy that mesh to this mesh but deform it
  14.     Mesh mesh;
  15.     Vector3[] verts;
  16.  
  17.     //  The direction along which the stretch/squash is performed...
  18.     public Vector3 squashAxis = Vector3.up;
  19.     //  ...and the scaling along that axis.
  20.     public float scale = 0.5f;
  21.  
  22.     void Start()
  23.     {
  24.         origMesh = MeshObject.GetComponent<MeshFilter>().mesh;
  25.         mesh = GetComponent<MeshFilter>().mesh;
  26.     }
  27.  
  28.     void Update()
  29.     {
  30.         // This mesh follows the invisible reference mesh (which is not deformed)
  31.         transform.position = MeshObject.position;
  32.         transform.rotation = MeshObject.rotation;
  33.  
  34.         origVerts = origMesh.vertices;
  35.         verts = origMesh.vertices;
  36.         LocalToWorld();
  37.  
  38.         //  Create a new local coordinate space where one axis is the stretch/squash direction
  39.         Vector3 r = squashAxis;
  40.         Vector3 g = Vector3.zero;
  41.         Vector3 b = Vector3.zero;
  42.         Vector3.OrthoNormalize(ref r, ref g, ref b);
  43.         DrawAxes(r, g, b);
  44.  
  45.         //A matrix transforms points into new coordinate space.
  46.         Matrix4x4 mat = new Matrix4x4();
  47.         mat.SetRow(0, r);
  48.         mat.SetRow(1, g);
  49.         mat.SetRow(2, b);
  50.         mat.SetRow(3, new Vector4(0, 0, 0, 1));
  51.         Matrix4x4 inv = mat.inverse;
  52.  
  53.         for (int i = 0; i < origVerts.Length; i++)
  54.         {
  55.             //  Convert the original unaltered vertex to the new coordinate space.
  56.             Vector3 vert = mat.MultiplyPoint3x4(origVerts[i]);
  57.  
  58.             //  Change the vertex's position in the new space.
  59.             vert.x *= scale;
  60.  
  61.             //  Convert it back to the usual XYZ space.
  62.             verts[i] = inv.MultiplyPoint3x4(vert);
  63.         }
  64.  
  65.         //  Set the updated vertex array.
  66.         mesh.vertices = verts;
  67.  
  68.         //  The normals have probably changed, so let Unity update them.
  69.         mesh.RecalculateNormals();
  70.     }
  71.  
  72.     void LocalToWorld()
  73.     {
  74.         Matrix4x4 localToWorld = MeshObject.localToWorldMatrix;
  75.         for (int i = 0; i < origVerts.Length; ++i)
  76.         {
  77.             Vector3 world_v = localToWorld.MultiplyPoint3x4(origVerts[i]);
  78.             origVerts[i] = world_v;
  79.             Debug.DrawRay(world_v, Vector3.up * 0.01f, Color.magenta, 0.001f);
  80.         }
  81.     }
  82.  
  83.     void DrawAxes(Vector3 R, Vector3 B, Vector3 G)
  84.     {
  85.         Debug.DrawRay(transform.position, R, Color.red, 0.05f);
  86.         Debug.DrawRay(transform.position, G, Color.green, 0.05f);
  87.         Debug.DrawRay(transform.position, B, Color.blue, 0.05f);
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement