RamBuoyancy.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [RequireComponent(typeof(Rigidbody))]
  5. public class RamBuoyancy : MonoBehaviour
  6. {
  7. public float buoyancy = 30;
  8. public float viscosity = 2;
  9. public float viscosityAngular = 0.4f;
  10. public LayerMask layer = 16;
  11. public new Collider collider;
  12. [Range(2,10)]
  13. public int pointsInAxis = 2;
  14. new Rigidbody rigidbody;
  15. static RamSpline[] ramSplines;
  16. static LakePolygon[] lakePolygons;
  17. List<Vector3> vertices = new List<Vector3>();
  18. Vector3[] verticesMatrix;
  19. Vector3 lowestPoint;
  20. Vector3 center = Vector3.zero;
  21. public bool debug = false;
  22. void Start()
  23. {
  24. rigidbody = GetComponent<Rigidbody>();
  25. if (ramSplines == null)
  26. ramSplines = FindObjectsOfType<RamSpline>();
  27. if (lakePolygons == null)
  28. lakePolygons = FindObjectsOfType<LakePolygon>();
  29. if (collider == null)
  30. collider = GetComponent<Collider>();
  31. if (collider == null)
  32. {
  33. Debug.LogError("Buoyancy doesn't have collider");
  34. this.enabled = false;
  35. return;
  36. }
  37. Vector3 size = collider.bounds.size;
  38. Vector3 min = collider.bounds.min;
  39. Vector3 step = new Vector3(size.x / (float)pointsInAxis, size.y / (float)pointsInAxis, size.z / (float)pointsInAxis);
  40. for (int x = 0; x <= pointsInAxis; x++)
  41. {
  42. for (int y = 0; y <= pointsInAxis; y++)
  43. {
  44. for (int z = 0; z <= pointsInAxis; z++)
  45. {
  46. Vector3 vertice = new Vector3(min.x + x * step.x, min.y + y * step.y, min.z + z * step.z);
  47. Vector3 closestPoint = collider.ClosestPoint(vertice);
  48. if (Vector3.Distance(closestPoint, vertice) < float.Epsilon)
  49. {
  50. vertices.Add(transform.InverseTransformPoint(vertice));
  51. }
  52. }
  53. }
  54. }
  55. verticesMatrix = new Vector3[vertices.Count];
  56. //Debug.Log(gameObject.name + " " + vertices.Count);
  57. }
  58. private void FixedUpdate()
  59. {
  60. WaterPhysics();
  61. }
  62. public void WaterPhysics()
  63. {
  64. Ray ray = new Ray();
  65. ray.direction = Vector3.up;
  66. RaycastHit hit;
  67. bool backFace = Physics.queriesHitBackfaces;
  68. Physics.queriesHitBackfaces = true;
  69. var thisMatrix = transform.localToWorldMatrix;
  70. lowestPoint = vertices[0];
  71. float minY = float.MaxValue;
  72. for (int i = 0; i < vertices.Count; i++)
  73. {
  74. verticesMatrix[i] = thisMatrix.MultiplyPoint3x4(vertices[i]);
  75. if (minY > verticesMatrix[i].y)
  76. {
  77. lowestPoint = verticesMatrix[i];
  78. minY = lowestPoint.y;
  79. }
  80. }
  81. ray.origin = lowestPoint;
  82. center = Vector3.zero;
  83. if (Physics.Raycast(ray, out hit, 100, layer))
  84. {
  85. float width = Mathf.Max(collider.bounds.size.x, collider.bounds.size.z);
  86. int verticesCount = 0;
  87. Vector3 velocity = rigidbody.velocity;
  88. Vector3 velocityDirection = velocity.normalized;
  89. minY = hit.point.y;
  90. for (int i = 0; i < verticesMatrix.Length; i++)
  91. {
  92. if (verticesMatrix[i].y <= minY)
  93. {
  94. center += verticesMatrix[i];
  95. verticesCount++;
  96. }
  97. }
  98. center /= verticesCount;
  99. //Debug.Log(minY - center.y);
  100. rigidbody.AddForceAtPosition(Vector3.up * buoyancy * (minY - center.y), center);
  101. rigidbody.AddForce(velocity * -1 * viscosity);
  102. if (velocity.magnitude > 0.01f)
  103. {
  104. Vector3 v1 = Vector3.Cross(velocity, new Vector3(1, 1, 1)).normalized;
  105. Vector3 v2 = Vector3.Cross(velocity, v1).normalized;
  106. Vector3 pointFront = velocity.normalized * 10;
  107. Ray rayCollider;
  108. //int test = 0;
  109. RaycastHit hitCollider;
  110. foreach (var item in verticesMatrix)
  111. {
  112. Vector3 start = pointFront + item;
  113. rayCollider = new Ray(start, -velocityDirection);
  114. //Debug.Log(start + " " + v1 + " " + v2);
  115. //Debug.DrawRay(start, -velocityDirection * 50, Color.cyan);
  116. if (collider.Raycast(rayCollider, out hitCollider, 50))
  117. {
  118. Vector3 pointVelocity = rigidbody.GetPointVelocity(hitCollider.point);
  119. rigidbody.AddForceAtPosition(-pointVelocity * viscosityAngular, hitCollider.point);
  120. //Debug.Log(hitCollider.point);
  121. if (debug)
  122. Debug.DrawRay(hitCollider.point, -pointVelocity * viscosityAngular, Color.red, 0.1f);
  123. }
  124. }
  125. //verticesMatrix
  126. //for (float x = -width; x < width; x += width * 0.1f)
  127. //{
  128. // for (float y = -width; y < width; y += width * 0.1f)
  129. // {
  130. // test++;
  131. // Vector3 start = pointFront + (v1 * x) + (v2 * y);
  132. // rayCollider = new Ray(start, -velocityDirection);
  133. // //Debug.Log(start + " " + v1 + " " + v2);
  134. // //Debug.DrawRay(start, -velocityDirection*50, Color.cyan);
  135. // if (collider.Raycast(rayCollider, out hitCollider, 50))
  136. // {
  137. // Vector3 pointVelocity = rigidbody.GetPointVelocity(hitCollider.point);
  138. // rigidbody.AddForceAtPosition(-pointVelocity * viscosityAngular, hitCollider.point);
  139. // //Debug.Log(hitCollider.point);
  140. // if (debug)
  141. // Debug.DrawRay(hitCollider.point, -pointVelocity * viscosityAngular, Color.red, 0.1f);
  142. // }
  143. // }
  144. //}
  145. //Debug.Log(test);
  146. }
  147. RamSpline ramSpline = hit.collider.GetComponent<RamSpline>();
  148. LakePolygon lakePolygon = hit.collider.GetComponent<LakePolygon>();
  149. if (ramSpline != null)
  150. {
  151. Mesh meshRam = ramSpline.meshfilter.sharedMesh;
  152. int verticeId1 = meshRam.triangles[hit.triangleIndex * 3];
  153. Vector3 verticeDirection = ramSpline.verticeDirection[verticeId1];
  154. Vector2 uv4 = meshRam.uv4[verticeId1];
  155. verticeDirection = verticeDirection * uv4.y - new Vector3(verticeDirection.z, verticeDirection.y, -verticeDirection.x) * uv4.x;
  156. rigidbody.AddForce(new Vector3(verticeDirection.x, 0, verticeDirection.z) * ramSpline.floatSpeed);
  157. if (debug)
  158. Debug.DrawRay(center, Vector3.up * buoyancy * (minY - center.y) * 5, Color.blue);
  159. if (debug)
  160. Debug.DrawRay(transform.position, velocity * -1 * viscosity * 5, Color.magenta);
  161. if (debug)
  162. Debug.DrawRay(transform.position, velocity * 5, Color.grey);
  163. if (debug)
  164. Debug.DrawRay(transform.position, rigidbody.angularVelocity * 5, Color.black);
  165. }
  166. else if (lakePolygon != null)
  167. {
  168. Mesh meshLake = lakePolygon.meshfilter.sharedMesh;
  169. int verticeId1 = meshLake.triangles[hit.triangleIndex * 3];
  170. Vector2 uv4 = -meshLake.uv4[verticeId1];
  171. //Debug.Log(uv4);
  172. Vector3 verticeDirection = new Vector3(uv4.x, 0, uv4.y);// Vector3.forward * uv4.y + new Vector3(0, 0, 1) * uv4.x;
  173. rigidbody.AddForce(new Vector3(verticeDirection.x, 0, verticeDirection.z) * lakePolygon.floatSpeed);
  174. if (debug)
  175. Debug.DrawRay(transform.position + Vector3.up, verticeDirection * 5, Color.red);
  176. if (debug)
  177. Debug.DrawRay(center, Vector3.up * buoyancy * (minY - center.y) * 5, Color.blue);
  178. if (debug)
  179. Debug.DrawRay(transform.position, velocity * -1 * viscosity * 5, Color.magenta);
  180. if (debug)
  181. Debug.DrawRay(transform.position, velocity * 5, Color.grey);
  182. if (debug)
  183. Debug.DrawRay(transform.position, rigidbody.angularVelocity * 5, Color.black);
  184. }
  185. }
  186. Physics.queriesHitBackfaces = backFace;
  187. }
  188. void OnDrawGizmosSelected()
  189. {
  190. if (!debug)
  191. return;
  192. if (collider != null && verticesMatrix != null)
  193. {
  194. var thisMatrix = transform.localToWorldMatrix;
  195. foreach (var item in verticesMatrix)
  196. {
  197. Gizmos.color = Color.red;
  198. Gizmos.DrawSphere(item, .08f);
  199. }
  200. }
  201. if (lowestPoint != null)
  202. {
  203. Gizmos.color = Color.blue;
  204. Gizmos.DrawSphere(lowestPoint, .08f);
  205. }
  206. if (center != null)
  207. {
  208. Gizmos.color = Color.green;
  209. Gizmos.DrawSphere(center, .08f);
  210. }
  211. }
  212. }