Advertisement
I_GRIN_I

Untitled

Aug 10th, 2020
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MouseContorls : MonoBehaviour
  6. {
  7.     Camera cam;
  8.     bool go = false;
  9.     MeshFilter filter;
  10.     Mesh mesh;
  11.     [HideInInspector]
  12.     public Vector3[] verticles;
  13.     int target;
  14.     public float pullValue = .3f;
  15.     public float radius = 0.3f;
  16.     Vector3 from;
  17.     Vector3 to;
  18.     Vector3 current;
  19. #pragma warning disable CS0108
  20.     MeshCollider collider;
  21.     public GameObject gameObject;
  22. #pragma warning restore CS0108
  23.     private void Start()
  24.     {
  25.         cam = Camera.main;
  26.         filter = gameObject.GetComponent<MeshFilter>();
  27.         collider = gameObject.GetComponent<MeshCollider>();
  28.         mesh = filter.mesh;
  29.         verticles = mesh.vertices;
  30.     }
  31.     void Update()
  32.     {
  33.         if (Input.GetMouseButtonDown(0))
  34.         {
  35.             RaycastHit hit;
  36.             from = Input.mousePosition;
  37.             from.z = 0;
  38.             if (Physics.Raycast(cam.ScreenPointToRay(from), out hit))
  39.             {
  40.                 target = GetClosestVertex(hit.point - gameObject.transform.position);
  41.                 current = verticles[target];
  42.                 go = true;
  43.             }
  44.         }
  45.         if (go)
  46.         {
  47.             //Debug.DrawLine(gameObject.transform.position+verticles[target], gameObject.transform.position+verticles[target] * 2);
  48.             if (Input.GetMouseButtonUp(0))
  49.             {
  50.                 go = false;
  51.                 collider.sharedMesh = mesh;
  52.             }
  53.             Debug.Log(to - from);
  54.             if(to != Input.mousePosition)
  55.                 DisplaceVertices(verticles[target], to-from);
  56.             to = Input.mousePosition;
  57.             to.z = 0;
  58.         }
  59.     }
  60.     void DisplaceVertices(Vector3 targetVertexPos, Vector3 fromto)
  61.     {
  62.         Vector3 currentVertexPos = Vector3.zero;
  63.         float sqrRadius = radius * radius;
  64.  
  65.         for (int i = 0; i < verticles.Length; i++)
  66.         {
  67.             currentVertexPos = verticles[i];
  68.             float sqrMagnitute = Distance(currentVertexPos,targetVertexPos);
  69.             if (sqrMagnitute > sqrRadius)
  70.             {
  71.                 continue;
  72.             }
  73.             float distance = Mathf.Sqrt(sqrMagnitute);
  74.             float falloff = (radius-distance) /radius;
  75.             Vector3 translate = currentVertexPos + fromto * falloff * Time.deltaTime * 0.1f;
  76.             verticles[i] = translate;
  77.         }
  78.         mesh.vertices = verticles;
  79.         mesh.RecalculateNormals();
  80.     }
  81.     int GetClosestVertex(Vector3 point)
  82.     {
  83.         int closest = -1;
  84.         float currentDistance = 0;
  85.         float cacheDistance = float.MaxValue;
  86.         int i;
  87.         for (i = 0; i < verticles.Length; i++)
  88.         {
  89.             currentDistance = Distance(verticles[i], point);
  90.             if (currentDistance < cacheDistance)
  91.             {
  92.                 closest = i;
  93.                 cacheDistance = currentDistance;
  94.             }
  95.         }
  96.         return closest;
  97.     }
  98.     public float Distance(Vector3 one,Vector3 two) {
  99.         float xD = one.x - two.x;
  100.         float yD = one.y - two.y;
  101.         float zD = one.z - two.z;
  102.         return xD * xD + yD * yD + zD * zD;
  103.     }
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement