1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| using System.Collections.Generic; using UnityEditor; using UnityEngine;
public class MassSpring : MonoBehaviour { class Spring { public readonly Transform pointA; public readonly Transform pointB; public float length;
public Spring(Transform pointA, Transform pointB) { this.pointA = pointA; this.pointB = pointB; } }
[SerializeField] [Range(0, 2)] float elasticity = 0.75f; [SerializeField] [Range(0, 0.999f)] float drag = 0.01f; [SerializeField] int size = 30; [SerializeField] int unit = 1; [SerializeField] bool ropeOrCloth; [SerializeField] bool useGravity;
readonly List<Spring> allSprings = new List<Spring>(); readonly List<Transform> allPoints = new List<Transform>(); readonly Dictionary<Transform, Vector3> lastPositions = new Dictionary<Transform, Vector3>();
void Start() { if (ropeOrCloth) { Transform[] points = new Transform[size]; for (int i = 0; i < size; i++) { Vector3 position = new Vector3(i * unit, 0, 0); GameObject point = new GameObject(); allPoints.Add(point.transform); lastPositions[point.transform] = position; point.transform.position = position; points[i] = point.transform; }
for (int i = 0; i < size - 1; i++) { allSprings.Add(new Spring(points[i], points[i + 1])); } } else { Transform[,] points = new Transform[size, size]; for (int i = 0, y = 0; i < size; i++, y += unit) for (int j = 0, x = 0; j < size; j++, x += unit) { Vector3 position = new Vector3(x, y, 0); GameObject point = new GameObject(); allPoints.Add(point.transform); lastPositions[point.transform] = position; point.transform.position = position; points[i, j] = point.transform; }
for (int y = 0; y < size; y++) for (int x = 0; x < size; x++) { if (x + 1 < size) allSprings.Add(new Spring(points[y, x], points[y, x + 1])); if (y + 1 < size) allSprings.Add(new Spring(points[y, x], points[y + 1, x])); if (x + 1 < size && y + 1 < size) allSprings.Add(new Spring(points[y, x], points[y + 1, x + 1])); if (x + 1 < size && y - 1 >= 0) allSprings.Add(new Spring(points[y, x], points[y - 1, x + 1])); if (x + 2 < size) allSprings.Add(new Spring(points[y, x], points[y, x + 2])); if (y + 2 < size) allSprings.Add(new Spring(points[y, x], points[y + 2, x])); } }
foreach (Spring spring in allSprings) { spring.length = Vector3.Distance(spring.pointA.position, spring.pointB.position); } }
float GetOneMinusDrag(Transform transform) { return transform == Selection.activeTransform ? 0 : 1 - drag; }
void Update() { foreach (Transform point in allPoints) { Vector3 position = point.position; point.position += (position - lastPositions[point]) * GetOneMinusDrag(point); lastPositions[point] = position; }
if (useGravity) foreach (Transform point in allPoints) { const float Gravity = 0.02f * 0.02f * -9.81f; point.position += new Vector3(0, Gravity, 0) * GetOneMinusDrag(point); }
foreach (Spring spring in allSprings) { Transform pointA = spring.pointA; Transform pointB = spring.pointB;
Vector3 positionA = pointA.position; Vector3 positionB = pointB.position; Vector3 vector = positionA - positionB; if (vector == Vector3.zero) vector = new Vector3(0, float.Epsilon, 0); float distance = vector.magnitude; float tendency = distance - spring.length; Vector3 direction = vector / distance; Vector3 move = elasticity * tendency * direction;
float moveWeight; if (ropeOrCloth && drag > 0.99f) { moveWeight = 1; } else { float oneMinusDragB = GetOneMinusDrag(pointB); float oneMinusDragA = GetOneMinusDrag(pointA); moveWeight = Mathf.Approximately(oneMinusDragB + oneMinusDragA, 0.0f) ? 0.5f : oneMinusDragB / (oneMinusDragB + oneMinusDragA); } pointB.position += moveWeight * move; pointA.position += (1 - moveWeight) * -move; } }
void OnDrawGizmos() { if (allSprings != null) foreach (Spring spring in allSprings) { Gizmos.DrawLine(spring.pointA.position, spring.pointB.position); } } }
C#
|