File size: 4,392 Bytes
41791c1 | 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 | using PhysX5ForUnity;
using System;
using UnityEngine;
[ExecuteInEditMode]
[DisallowMultipleComponent]
[AddComponentMenu("Blood Simulation/Suction Actor")]
public class SuctionActor : MonoBehaviour
{
#region Properties
[SerializeField]
public PhysxParticleActor pxParticleActor;
public float Radius
{
get { return m_radius; }
set { m_radius = value; }
}
public float ConeAngle
{
get { return m_coneAngle; }
set { m_coneAngle = value; }
}
public float RemovalRadius
{
get { return m_removalRadius; }
set { m_removalRadius = value; }
}
public float ForceScale
{
get { return m_forceScale; }
set { m_forceScale = value; }
}
public bool IsActive
{
get { return m_isActive; }
set { m_isActive = value; }
}
public bool InEpisode
{
get { return m_inEpisode; }
set { m_inEpisode = value; }
}
#endregion
#region Messages
private void Start()
{
localCoordinatePosition = transform.position;
}
private void FixedUpdate()
{
UpdateParticles();
}
private void OnValidate()
{
// transform.localScale = new Vector3(m_radius * 2, m_radius * 2, m_radius * 2);
}
#endregion
#region Private
void UpdateParticles()
{
if (Application.isPlaying && m_inEpisode)
{
Vector3 forceOrigin = transform.position;
Vector3 forceDirecion = -transform.up; // the - Y axis of the gameObject
//int[] activeIndices = m_particleDataContainer.ActiveIndices;
//int activeCount = m_particleDataContainer.ActiveCount;
int removalIndexCount = 0;
Vector4 removalTeleportPosition = new Vector4(5.0f + localCoordinatePosition.x, -5.0f + localCoordinatePosition.y, 0.0f + localCoordinatePosition.z, 0.0f);
float dT = Time.fixedDeltaTime;
for (int i = 0; i < pxParticleActor.ParticleData.NumParticles; ++i)
{
//int idx = activeIndices[i];
int idx = i;
Vector4 position = pxParticleActor.ParticleData.PositionInvMass[idx];
Vector3 p = position;
Vector3 delta = p - forceOrigin;
// in case particle drops below the map
if (position.y < -10.0f)
{
pxParticleActor.ParticleData.SetParticle(idx, removalTeleportPosition, true);
continue;
}
float t = Vector3.Dot(delta, forceDirecion);
float distanceToOrigin = delta.magnitude;
if (distanceToOrigin > m_radius)
{
// Early stopping
continue;
}
Vector3 c = Vector3.Cross(delta, forceDirecion);
float norm_c = Mathf.Sqrt(Mathf.Pow(c.x, 2) + Mathf.Pow(c.y, 2) + Mathf.Pow(c.z, 2));
float a = Mathf.Atan2(norm_c, t) * 180 / 3.1415f;
if (distanceToOrigin < m_removalRadius) // using distanceToOrigin is less realistic
{
++removalIndexCount;
pxParticleActor.ParticleData.SetParticle(idx, removalTeleportPosition, true);
}
if (a < m_coneAngle)
{
Vector3 addedVelocity = -m_forceScale * dT * position.w * (delta.normalized / (distanceToOrigin + 0.0001f) - delta.normalized / (m_radius + 0.0001f));
Vector3 velocity = pxParticleActor.ParticleData.Velocity[idx];
pxParticleActor.ParticleData.SetVelocity(idx, velocity + addedVelocity, true);
}
}
pxParticleActor.ParticleData.SyncParticlesSet(false);
}
}
[SerializeField]
float m_radius;
[SerializeField]
float m_removalRadius = 0.5f;
[SerializeField]
float m_coneAngle = 30.0f;
[SerializeField]
float m_forceScale = 1.0f;
[SerializeField]
bool m_isActive = false;
// [SerializeField]
// ParticleDataContainer m_particleDataContainer;
[SerializeField]
bool m_useMapHeightForRemoval;
[NonSerialized]
bool m_inEpisode = true;
[NonSerialized]
Vector3 localCoordinatePosition;
#endregion
}
|