MouseManipulator.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using UnityEngine;
  2. using System.Collections;
  3. [AddComponentMenu("Cocuy/Mouse Manipulator")]
  4. public class MouseManipulator : MonoBehaviour {
  5. private Vector3 m_previousMousePosition;
  6. [HideInInspector]
  7. public float m_velocityStrength = 10f;
  8. [HideInInspector]
  9. public float m_velocityRadius = 5f;
  10. [HideInInspector]
  11. public float m_particlesStrength = 1f;
  12. [HideInInspector]
  13. public float m_particlesRadius = 5f;
  14. public FluidSimulator m_fluid;
  15. public ParticlesArea m_particlesArea;
  16. public bool m_alwaysOn = false;
  17. void DrawGizmo()
  18. {
  19. float col = m_particlesStrength / 10000.0f;
  20. Gizmos.color = Color.Lerp(Color.yellow, Color.red, col);
  21. Gizmos.DrawWireSphere(transform.position, m_particlesRadius);
  22. Gizmos.color = Color.cyan;
  23. Gizmos.DrawWireSphere(transform.position, m_velocityRadius);
  24. }
  25. void OnDrawGizmosSelected()
  26. {
  27. DrawGizmo();
  28. }
  29. void OnDrawGizmos()
  30. {
  31. DrawGizmo();
  32. }
  33. void LateUpdate()
  34. {
  35. if (Input.GetMouseButton(0) || m_alwaysOn)
  36. {
  37. Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f));
  38. RaycastHit hitInfo = new RaycastHit();
  39. if (m_particlesArea.GetComponent<Collider>().Raycast(ray, out hitInfo, 100))
  40. {
  41. float fWidth = m_particlesArea.GetComponent<Renderer>().bounds.extents.x * 2f;
  42. float fRadius = (m_particlesRadius * m_particlesArea.GetWidth()) / fWidth;
  43. m_particlesArea.AddParticles(hitInfo.textureCoord, fRadius, m_particlesStrength * Time.deltaTime);
  44. }
  45. }
  46. if (Input.GetMouseButtonDown(1))
  47. {
  48. m_previousMousePosition = Input.mousePosition;
  49. }
  50. if (Input.GetMouseButton(1) || m_alwaysOn)
  51. {
  52. Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f));
  53. RaycastHit hitInfo = new RaycastHit();
  54. if (m_fluid.GetComponent<Collider>().Raycast(ray, out hitInfo, 100))
  55. {
  56. Vector3 direction = (Input.mousePosition - m_previousMousePosition) * m_velocityStrength * Time.deltaTime;
  57. float fWidth = m_fluid.GetComponent<Renderer>().bounds.extents.x * 2f;
  58. float fRadius = (m_velocityRadius * m_fluid.GetWidth()) / fWidth;
  59. if (Input.GetMouseButton(0))
  60. {
  61. m_fluid.AddVelocity(hitInfo.textureCoord, -direction, fRadius);
  62. }
  63. else
  64. {
  65. m_fluid.AddVelocity(hitInfo.textureCoord, direction, fRadius);
  66. }
  67. }
  68. m_previousMousePosition = Input.mousePosition;
  69. }
  70. }
  71. }