Advertisement
zORg_alex

Mesh Bender

Nov 28th, 2022
55
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | Gaming | 0 0
  1. using BezierCurveZ;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.VisualScripting;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using Utility;
  8.  
  9. [ExecuteAlways]
  10. [SelectionBase]
  11. public class MeshBender : MonoBehaviour
  12. {
  13.     public Mesh originalMesh;
  14.     public float originalLength = 1f;
  15.     public MeshFilter meshFilter;
  16.     [Tooltip("Only first segment will be accounted")]
  17.     [ContextMenuItem("UpdateMesh", "UpdateMesh")]
  18.     public Curve curve;
  19.     private void OnEnable()
  20.     {
  21.         if (originalMesh.IsNullOrDestroyed() && meshFilter) originalMesh = meshFilter.sharedMesh;
  22.         UpdateMesh();
  23.     }
  24.  
  25.     [ContextMenu("UpdateMesh")]
  26.     public void UpdateMesh()
  27.     {
  28.         var mesh = meshFilter.sharedMesh;
  29.         if (mesh == originalMesh)
  30.             mesh = originalMesh.Copy();
  31.  
  32.         Transform mfTransform = meshFilter.transform;
  33.         var thisToMeshMatrix = mfTransform.worldToLocalMatrix * transform.localToWorldMatrix;
  34.         var meshToThisMatrix = transform.worldToLocalMatrix * mfTransform.localToWorldMatrix;
  35.  
  36.         var verts = originalMesh.vertices;
  37.         for (int i = 0; i < verts.Length; i++)
  38.         {
  39.             var v = meshToThisMatrix.MultiplyPoint3x4(verts[i]);
  40.             var t = Mathf.Clamp01(v.z / originalLength);
  41.             var point = curve.VertexData.GetPointFromTime(t);
  42.             var vRelToPoint = v - new Vector3(0, 0, t * originalLength);
  43.             verts[i] = thisToMeshMatrix.MultiplyPoint3x4(point.Position + point.Rotation * vRelToPoint);
  44.         }
  45.         mesh.vertices = verts;
  46.         meshFilter.sharedMesh = mesh;
  47.     }
  48. #if UNITY_EDITOR
  49.     private void OnDrawGizmosSelected()
  50.     {
  51.         var m = Handles.matrix;
  52.         var c = Handles.color;
  53.         Handles.color = new Color(.3f, .3f, 1f);
  54.         Handles.matrix = transform.localToWorldMatrix;
  55.  
  56.         var origTgt = Vector3.forward * originalLength;
  57.         Handles.DrawAAPolyLine(Vector3.zero, origTgt);
  58.         Handles.DrawAAPolyLine(Vector3.right * -.5f, Vector3.right * .5f);
  59.         Handles.DrawAAPolyLine(Vector3.forward * originalLength + Vector3.right * -.5f, Vector3.forward * originalLength + Vector3.right * .5f);
  60.  
  61.         Handles.color = new Color(.3f, 1f, .3f);
  62.         var tgt = curve.Points[curve.GetPointIndex(1)];
  63.         Handles.DrawAAPolyLine(Vector3.right * -.5f, Vector3.right * .5f);
  64.         Handles.DrawAAPolyLine(tgt + tgt.right * -.5f, tgt + tgt.right * .5f);
  65.         Handles.DrawAAPolyLine(curve.VertexDataPoints);
  66.  
  67.         Handles.color = c;
  68.         Handles.matrix = m;
  69.     }
  70. #endif
  71. }
  72.  
Tags: Unity
Advertisement
Comments
  • zORg_alex
    1 year
    # text 0.13 KB | 0 0
    1. Usings are not correct:
    2.  
    3. using BezierCurveZ;
    4. #if UNITY_EDITOR
    5. using UnityEditor;
    6. #endif
    7. using UnityEngine;
    8. using Utility;
Add Comment
Please, Sign In to add comment
Advertisement