void ApplyImpulses(FlexContainer.ParticleData _particleData) { if (m_currentAsset && m_instanceHandle) { FlexExt.Instance instance = m_instanceHandle.instance; int[] indices = new int[instance.numParticles]; FlexUtils.FastCopy(instance.particleIndices, indices); foreach (var info in m_impulses) { if (info.impulse.sqrMagnitude < float.Epsilon) { continue; } float mass = 0; foreach (var index in indices) { if (info.particle == -1 || info.particle == index) { Vector4 particle = _particleData.GetParticle(index); mass += 1.0f / particle.w; } } if (mass < float.Epsilon) { continue; } Vector3 velocityChange = info.impulse / mass; foreach (var index in indices) { _particleData.SetVelocity(index, _particleData.GetVelocity(index) + velocityChange); } } } }
void TeleportParticles(FlexContainer.ParticleData _particleData) { Matrix4x4 offset = Matrix4x4.TRS(m_teleportPosition, Quaternion.identity, Vector3.one) * Matrix4x4.TRS(Vector3.zero, m_teleportRotation, Vector3.one) * Matrix4x4.TRS(Vector3.zero, Quaternion.Inverse(transform.rotation), Vector3.one) * Matrix4x4.TRS(-transform.position, Quaternion.identity, Vector3.one); if (m_currentAsset && m_instanceHandle) { FlexExt.Instance instance = m_instanceHandle.instance; int[] indices = new int[instance.numParticles]; FlexUtils.FastCopy(instance.particleIndices, indices); foreach (var index in indices) { Vector4 particle = _particleData.GetParticle(index); float storeW = particle.w; particle = offset.MultiplyPoint3x4(particle); particle.w = storeW; _particleData.SetParticle(index, particle); _particleData.SetVelocity(index, Vector3.zero); } } transform.position = m_teleportPosition; transform.rotation = m_teleportRotation; transform.hasChanged = false; }
//This function is to set the initial velocity of the fluid bool PresetVelocities(FlexContainer.ParticleData _particleData) { Vector3 initVelocity = new Vector3(0, 0, -2.0f); //Change the Vector to adjust the intial velocity FlexExt.Instance instance = m_instanceHandle.instance; int[] indices = new int[instance.numParticles]; FlexUtils.FastCopy(instance.particleIndices, indices); foreach (var index in indices) { _particleData.SetVelocity(index, initVelocity); } return(true); }
void UpdateParticles(FlexContainer.ParticleData _particleData) { if (handle && m_indices != null && m_indices.Length > 0) { float dT = Application.isPlaying ? Time.fixedDeltaTime : 0; int newCount = FlexUtils.UpdateSourceParticles(ref m_indices[0], ref m_ages[0], m_count, _particleData.particleData.particles, dT, massScale); if (newCount != m_count) { m_currentContainer.FreeParticles(m_indices, newCount, m_count - newCount); m_count = newCount; } if (m_isActive && m_startSpeed > 0) { m_spawnTime += dT; while (m_spawnTime > 0) { int spawnCount = Mathf.Min(m_asset.nozzleCount, m_asset.maxParticles - m_count); if (spawnCount > 0) { newCount += m_currentContainer.AllocParticles(m_indices, m_count, spawnCount); for (int i = m_count; i < newCount; ++i) { int index = m_indices[i]; int nozzle = i - m_count; _particleData.SetPhase(index, m_phase); Vector3 direction = transform.TransformDirection(m_asset.nozzleDirections[nozzle]); Vector3 velocity = direction * m_startSpeed; _particleData.SetVelocity(index, velocity); Vector3 position = transform.TransformPoint(m_asset.nozzlePositions[nozzle]); position -= velocity * (2.0f * dT - m_spawnTime); Vector4 particle = position; particle.w = 0.000001f;//1.0f / massScale;// _particleData.SetParticle(index, particle); _particleData.SetRestParticle(index, Vector3.zero); m_ages[i] = m_lifeTime; } m_count = newCount; } float dist = /*fluid ? m_currentContainer.fluidRest :*/ m_currentContainer.solidRest; m_spawnTime -= dist / m_startSpeed; } } SetIndices(m_indices, m_count); } }