123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using UnityEngine;
- using System.Collections;
- [AddComponentMenu("Cocuy/Velocity Manipulator")]
- [ExecuteInEditMode]
- public class VelocityManipulator : MonoBehaviour
- {
- public FluidSimulator m_fluid;
- [HideInInspector]
- public bool m_useScaleAsSize = true;
-
- [HideInInspector]
- public bool m_velocityFromMovement = false;
- [HideInInspector]
- public float m_fluidVelocitySpeed = 1f;
- [HideInInspector]
- public float m_scaleVelocity = 1f;
- [HideInInspector]
- public float m_radius = 0.1f;
- [HideInInspector]
- public bool m_showGizmo = false;
- Vector3 m_direction;
- Vector3 m_speed;
- Vector3 m_prevPosition;
- void Start()
- {
- m_prevPosition = transform.position;
- m_direction = GetDirection();
- }
- public float GetRadius()
- {
- if (m_useScaleAsSize)
- {
- return Mathf.Max(transform.lossyScale.x, transform.lossyScale.y);
- }
- return m_radius;
- }
- public Vector3 GetDirection()
- {
- if (m_velocityFromMovement)
- {
- return transform.position - m_prevPosition;
- }
- return transform.rotation * Vector3.down;
- }
- void UpdateValues()
- {
- m_direction = GetDirection();
- if (m_direction != Vector3.zero)
- {
- m_direction.Normalize();
- m_speed = m_direction * m_fluidVelocitySpeed * Time.deltaTime;
- }
- else
- {
- m_speed = Vector3.zero;
- }
- m_prevPosition = transform.position;
- }
- void Update()
- {
- UpdateValues();
- }
- void LateUpdate()
- {
- if (m_fluid)
- {
- Vector3 currentPosition = transform.position;
- if (m_speed != Vector3.zero)
- {
- Ray ray = new Ray(currentPosition, new Vector3(0, 0, 1));
- RaycastHit hitInfo = new RaycastHit();
- if (m_fluid.GetComponent<Collider>().Raycast(ray, out hitInfo, 10))
- {
- float fWidth = m_fluid.GetComponent<Renderer>().bounds.extents.x * 2f;
- //float fHeight = m_fluid.renderer.bounds.extents.z * 2f;
- float fRadius = (GetRadius() * m_fluid.GetWidth()) / fWidth;
- m_fluid.AddVelocity(hitInfo.textureCoord, -m_speed, fRadius);
- }
- }
- }
- }
- void DrawGizmo()
- {
- Gizmos.color = Color.cyan;
- Gizmos.DrawWireSphere(transform.position, GetRadius());
- if (!m_velocityFromMovement || Application.isPlaying)
- {
- Vector3 end_pos = transform.position - (m_direction*(2f + (m_fluidVelocitySpeed/500f)*5f));
- Gizmos.color = Color.cyan;
- Gizmos.DrawLine(transform.position, end_pos);
- Vector3 back_dir = (transform.position - end_pos);
- back_dir.Normalize();
- float angle = 25 * Mathf.Deg2Rad;
- float cos = Mathf.Cos(angle);
- float sin = Mathf.Sin(angle);
- Vector3 arrow = new Vector3(back_dir.x * cos - back_dir.y * sin,
- back_dir.x * sin + back_dir.y * cos, 0f);
- Gizmos.DrawLine(end_pos, end_pos + arrow * 0.5f);
- cos = Mathf.Cos(-angle);
- sin = Mathf.Sin(-angle);
- arrow = new Vector3(back_dir.x * cos - back_dir.y * sin,
- back_dir.x * sin + back_dir.y * cos, 0f);
- Gizmos.DrawLine(end_pos, end_pos + arrow * 0.5f);
- }
- }
- void OnDrawGizmosSelected()
- {
- DrawGizmo();
- }
- void OnDrawGizmos()
- {
- if (m_showGizmo)
- {
- DrawGizmo();
- }
- }
- }
|