private float Fade(int index) { float time = _timeParameter.GetValue(index); float targetValue = (_targetValueParameter != null) ? _targetValueParameter.GetValue(index) : 1; return(Fade(time, targetValue)); }
/// <inheritdoc/> protected override void OnInitializeParticles(int startIndex, int count, object emitter) { if (_linearSpeedParameter == null || _directionParameter == null || _biasVelocityParameter == null) { return; } Vector3[] directions = _directionParameter.Values; float[] speeds = _linearSpeedParameter.Values; Vector3[] biases = _biasVelocityParameter.Values; if (directions != null && speeds != null && biases != null) { // Optimized case: Direction, Speed, and Acceleration are varying parameters. for (int i = startIndex; i < startIndex + count; i++) { Vector3 velocity = directions[i] * speeds[i]; velocity += biases[i] * Strength; speeds[i] = velocity.Length; if (!Numeric.IsZero(speeds[i])) { directions[i] = velocity / speeds[i]; } } } else if (directions != null && speeds != null) { // Optimized case: Direction and Speed are varying parameters, Bias is uniform. Vector3 bias = _biasVelocityParameter.DefaultValue * Strength; for (int i = startIndex; i < startIndex + count; i++) { Vector3 velocity = directions[i] * speeds[i]; velocity += bias; speeds[i] = velocity.Length; if (!Numeric.IsZero(speeds[i])) { directions[i] = velocity / speeds[i]; } } } else if (directions != null || speeds != null) { // General case: Either Direction or Speed is varying parameter. // This path does not make sense much sense. - But maybe someone has an idea how to use it. Vector3 bias = _biasVelocityParameter.DefaultValue * Strength; for (int i = startIndex; i < startIndex + count; i++) { Vector3 velocity = _directionParameter.GetValue(i) * _linearSpeedParameter.GetValue(i); velocity += bias; var newSpeed = velocity.Length; _linearSpeedParameter.SetValue(i, newSpeed); if (!Numeric.IsZero(newSpeed)) { _directionParameter.SetValue(i, velocity / newSpeed); } } } }
/// <inheritdoc/> protected override void OnUpdateParticles(TimeSpan deltaTime, int startIndex, int count) { if (_valueParameter == null || _startParameter == null || _endParameter == null || _factorParameter == null) { return; } Vector3[] values = _valueParameter.Values; if (values == null) { // Value is a uniform parameter. Uniform parameters are handled in OnBeginUpdate(). return; } // Value is a varying parameter. Vector3[] starts = _startParameter.Values; Vector3[] ends = _endParameter.Values; float[] factors = _factorParameter.Values; if (starts != null && ends != null && factors != null) { // Optimized case: Start, End, and Factor are varying parameters. for (int i = startIndex; i < startIndex + count; i++) { var f = factors[i]; values[i] = (1 - f) * starts[i] + f * ends[i]; } } else if (starts == null && ends == null && factors != null) { // Optimized case: Start and End are uniform parameters, Factor is varying parameter. Vector3 startValue = _startParameter.DefaultValue; Vector3 endValue = _endParameter.DefaultValue; for (int i = startIndex; i < startIndex + count; i++) { var f = factors[i]; values[i] = (1 - f) * startValue + f * endValue; } } else { // General case: for (int i = startIndex; i < startIndex + count; i++) { var f = _factorParameter.GetValue(i); values[i] = (1 - f) * _startParameter.GetValue(i) + f * _endParameter.GetValue(i); } } }
/// <inheritdoc/> protected override void OnUpdateParticles(TimeSpan deltaTime, int startIndex, int count) { if (_linearSpeedParameter == null || _positionParameter == null || _directionParameter == null) { return; } var positions = _positionParameter.Values; if (positions == null) { // Position is a uniform parameter. Uniform parameters are handled in OnBeginUpdate(). return; } // Value is a varying parameter. Vector3[] directions = _directionParameter.Values; float[] speeds = _linearSpeedParameter.Values; float dt = (float)deltaTime.TotalSeconds; if (directions != null && speeds != null) { // Optimized case: Direction and Speed are varying parameters. for (int i = startIndex; i < startIndex + count; i++) { positions[i] += directions[i] * speeds[i] * dt; } } else if (directions != null) { // Optimized case: Direction is varying parameter and Speed is uniform parameter. Debug.Assert(speeds == null); float speed = _linearSpeedParameter.DefaultValue; for (int i = startIndex; i < startIndex + count; i++) { positions[i] += directions[i] * speed * dt; } } else { // General case: for (int i = startIndex; i < startIndex + count; i++) { positions[i] += _directionParameter.GetValue(i) * _linearSpeedParameter.GetValue(i) * dt; } } }
// This method is called each frame to update particles. protected override void OnUpdateParticles(TimeSpan deltaTime, int startIndex, int count) { // Abort if we are missing particle parameters. if (_positionParameter == null || _linearSpeedParameter == null || _directionParameter == null) { return; } // Get the direction and linear speed particle parameter arrays. for (int i = startIndex; i < startIndex + count; i++) { // Get the particle position and check if the position is behind the plane. Vector3 position = _positionParameter.GetValue(i); if (Vector3.Dot(position, _plane.Normal) > _plane.DistanceFromOrigin) { continue; } // Get the linear velocity of the particle. Vector3 velocity = _directionParameter.GetValue(i) * _linearSpeedParameter.GetValue(i); // Check if the particle is moving into the plane or away. float normalSpeed = Vector3.Dot(velocity, _plane.Normal); if (normalSpeed > 0) { continue; } // Get the restitution. If there is no restitution particle parameter, we use a default value. float restitution = (_restitutionParameter != null) ? _restitutionParameter.GetValue(i) : 0.5f; // Change the velocity to let the particle bounce off. velocity = velocity - (1 + restitution) * _plane.Normal * normalSpeed; // Update LinearSpeed and Direction from the velocity vector. // The speed is the magnitude of the velocity vector. var newSpeed = velocity.Length; _linearSpeedParameter.SetValue(i, newSpeed); // Direction stores the normalized direction of the velocity vector. if (!Numeric.IsZero(newSpeed)) { _directionParameter.SetValue(i, velocity / newSpeed); } } }
/// <inheritdoc/> protected override void OnUpdateParticles(TimeSpan deltaTime, int startIndex, int count) { if (_linearSpeedParameter == null || _directionParameter == null || _linearAccelerationParameter == null) { return; } // Update varying parameters. Vector3[] directions = _directionParameter.Values; float[] speeds = _linearSpeedParameter.Values; Vector3[] accelerations = _linearAccelerationParameter.Values; float dt = (float)deltaTime.TotalSeconds; if (directions != null && speeds != null && accelerations != null) { // Optimized case: Direction, Speed, and Acceleration are varying parameters. for (int i = startIndex; i < startIndex + count; i++) { Vector3 velocity = directions[i] * speeds[i]; velocity += accelerations[i] * dt; speeds[i] = velocity.Length; if (!Numeric.IsZero(speeds[i])) { directions[i] = velocity / speeds[i]; } } } else if (directions != null && speeds != null) { // Optimized case: Direction and Speed are varying parameters, Acceleration is uniform. Vector3 acceleration = _linearAccelerationParameter.DefaultValue; for (int i = startIndex; i < startIndex + count; i++) { Vector3 velocity = directions[i] * speeds[i]; velocity += acceleration * dt; speeds[i] = velocity.Length; if (!Numeric.IsZero(speeds[i])) { directions[i] = velocity / speeds[i]; } } } else if (directions != null || speeds != null) { // General case: Either Direction or Speed is varying parameter. // This path does not make sense much sense. - But maybe someone has an idea how to use it. Vector3 acceleration = _linearAccelerationParameter.DefaultValue; for (int i = startIndex; i < startIndex + count; i++) { Vector3 velocity = _directionParameter.GetValue(i) * _linearSpeedParameter.GetValue(i); velocity += acceleration * dt; var newSpeed = velocity.Length; _linearSpeedParameter.SetValue(i, newSpeed); if (!Numeric.IsZero(newSpeed)) { _directionParameter.SetValue(i, velocity / newSpeed); } } } }
private float Rotate(int index, float deltaTime, float angle) { float speed = _angularSpeedParameter.GetValue(index); return(Rotate(deltaTime, angle, speed)); }