[Bf-blender-cvs] [af46f07ef53] functions: initial bounce test
Jacques Lucke
noreply at git.blender.org
Sun Jun 30 16:01:26 CEST 2019
Commit: af46f07ef5397f916864a55cab93eaa01bb34aed
Author: Jacques Lucke
Date: Sun Jun 30 15:21:43 2019 +0200
Branches: functions
https://developer.blender.org/rBaf46f07ef5397f916864a55cab93eaa01bb34aed
initial bounce test
===================================================================
M source/blender/blenlib/BLI_math.hpp
M source/blender/simulations/bparticles/c_wrapper.cpp
M source/blender/simulations/bparticles/core.hpp
M source/blender/simulations/bparticles/events.cpp
M source/blender/simulations/bparticles/events.hpp
M source/blender/simulations/bparticles/simulate.cpp
===================================================================
diff --git a/source/blender/blenlib/BLI_math.hpp b/source/blender/blenlib/BLI_math.hpp
index e7576656f90..9ba7ea815ca 100644
--- a/source/blender/blenlib/BLI_math.hpp
+++ b/source/blender/blenlib/BLI_math.hpp
@@ -45,6 +45,13 @@ struct float3 {
return len_v3(*this);
}
+ void reflect(float3 normal)
+ {
+ float3 result;
+ reflect_v3_v3v3(result, *this, normal);
+ *this = result;
+ }
+
friend float3 operator+(float3 a, float3 b)
{
return {a.x + b.x, a.y + b.y, a.z + b.z};
diff --git a/source/blender/simulations/bparticles/c_wrapper.cpp b/source/blender/simulations/bparticles/c_wrapper.cpp
index e84df89dff4..4415ca7c4e6 100644
--- a/source/blender/simulations/bparticles/c_wrapper.cpp
+++ b/source/blender/simulations/bparticles/c_wrapper.cpp
@@ -239,8 +239,7 @@ void BParticles_simulate_modifier(NodeParticlesModifierData *npmd,
if (npmd->collision_object) {
BKE_bvhtree_from_mesh_get(
&treedata, (Mesh *)npmd->collision_object->data, BVHTREE_FROM_LOOPTRI, 4);
- type0->m_events.append(new EventActionTest(
- EVENT_mesh_collection(&treedata, npmd->collision_object->obmat), ACTION_explode()));
+ type0->m_events.append(EVENT_mesh_bounce(&treedata, npmd->collision_object->obmat));
}
auto *type1 = new ModifierParticleType();
diff --git a/source/blender/simulations/bparticles/core.hpp b/source/blender/simulations/bparticles/core.hpp
index cf308892d1e..1ab0b1ec5e1 100644
--- a/source/blender/simulations/bparticles/core.hpp
+++ b/source/blender/simulations/bparticles/core.hpp
@@ -401,17 +401,20 @@ class EventExecuteInterface {
ArrayRef<float> m_current_times;
ArrayRef<uint8_t> m_kill_states;
EventStorage &m_event_storage;
+ AttributeArrays m_attribute_offsets;
public:
EventExecuteInterface(ParticleSet particles,
BlockAllocator &block_allocator,
ArrayRef<float> current_times,
- EventStorage &event_storage)
+ EventStorage &event_storage,
+ AttributeArrays attribute_offsets)
: m_particles(particles),
m_block_allocator(block_allocator),
m_current_times(current_times),
m_kill_states(m_particles.attributes().get_byte("Kill State")),
- m_event_storage(event_storage)
+ m_event_storage(event_storage),
+ m_attribute_offsets(attribute_offsets)
{
}
@@ -450,6 +453,11 @@ class EventExecuteInterface {
{
return m_event_storage.get<T>(pindex);
}
+
+ AttributeArrays attribute_offsets()
+ {
+ return m_attribute_offsets;
+ }
};
class Action {
diff --git a/source/blender/simulations/bparticles/events.cpp b/source/blender/simulations/bparticles/events.cpp
index acc8814607f..349f8babe6e 100644
--- a/source/blender/simulations/bparticles/events.cpp
+++ b/source/blender/simulations/bparticles/events.cpp
@@ -34,15 +34,27 @@ class AgeReachedEvent : public EventFilter {
}
};
-class MeshCollisionEvent : public EventFilter {
+class MeshBounceEvent : public Event {
private:
BVHTreeFromMesh *m_treedata;
+ float4x4 m_normal_transform;
float4x4 m_ray_transform;
+ struct CollisionData {
+ float3 normal;
+ };
+
public:
- MeshCollisionEvent(BVHTreeFromMesh *treedata, float4x4 transform)
- : m_treedata(treedata), m_ray_transform(transform.inverted__LocRotScale())
+ MeshBounceEvent(BVHTreeFromMesh *treedata, float4x4 transform)
+ : m_treedata(treedata),
+ m_normal_transform(transform),
+ m_ray_transform(transform.inverted__LocRotScale())
+ {
+ }
+
+ uint storage_size() override
{
+ return sizeof(CollisionData);
}
void filter(EventFilterInterface &interface) override
@@ -71,10 +83,30 @@ class MeshCollisionEvent : public EventFilter {
if (hit.index != -1) {
float time_factor = hit.dist / length;
- interface.trigger_particle(i, time_factor);
+ auto &data = interface.trigger_particle<CollisionData>(i, time_factor);
+ data.normal = m_normal_transform.transform_direction(hit.no).normalized();
}
}
}
+
+ void execute(EventExecuteInterface &interface) override
+ {
+ ParticleSet &particles = interface.particles();
+
+ auto velocities = particles.attributes().get_float3("Velocity");
+ auto positions = particles.attributes().get_float3("Position");
+ auto position_offsets = interface.attribute_offsets().get_float3("Position");
+
+ for (uint pindex : particles.indices()) {
+ auto &data = interface.get_storage<CollisionData>(pindex);
+
+ velocities[pindex].reflect(data.normal);
+ position_offsets[pindex].reflect(data.normal);
+
+ /* Temporary solution to avoid double collision. */
+ positions[pindex] += velocities[pindex] * 0.01f;
+ }
+ }
};
EventFilter *EVENT_age_reached(float age)
@@ -82,9 +114,9 @@ EventFilter *EVENT_age_reached(float age)
return new AgeReachedEvent(age);
}
-EventFilter *EVENT_mesh_collection(BVHTreeFromMesh *treedata, const float4x4 &transform)
+Event *EVENT_mesh_bounce(BVHTreeFromMesh *treedata, const float4x4 &transform)
{
- return new MeshCollisionEvent(treedata, transform);
+ return new MeshBounceEvent(treedata, transform);
}
} // namespace BParticles
diff --git a/source/blender/simulations/bparticles/events.hpp b/source/blender/simulations/bparticles/events.hpp
index 4beea879ee8..109f0b8c6e5 100644
--- a/source/blender/simulations/bparticles/events.hpp
+++ b/source/blender/simulations/bparticles/events.hpp
@@ -7,6 +7,6 @@ struct BVHTreeFromMesh;
namespace BParticles {
EventFilter *EVENT_age_reached(float age);
-EventFilter *EVENT_mesh_collection(struct BVHTreeFromMesh *treedata, const float4x4 &transform);
+Event *EVENT_mesh_bounce(struct BVHTreeFromMesh *treedata, const float4x4 &transform);
} // namespace BParticles
diff --git a/source/blender/simulations/bparticles/simulate.cpp b/source/blender/simulations/bparticles/simulate.cpp
index 3f7664d5387..1c61c8cba10 100644
--- a/source/blender/simulations/bparticles/simulate.cpp
+++ b/source/blender/simulations/bparticles/simulate.cpp
@@ -7,7 +7,7 @@
#include "xmmintrin.h"
-#define USE_THREADING true
+#define USE_THREADING false
#define BLOCK_SIZE 1000
namespace BParticles {
@@ -182,7 +182,8 @@ BLI_NOINLINE static void execute_events(BlockAllocator &block_allocator,
ArrayRef<SmallVector<uint>> particle_indices_per_event,
ArrayRef<SmallVector<float>> current_time_per_particle,
ArrayRef<Event *> events,
- EventStorage &event_storage)
+ EventStorage &event_storage,
+ AttributeArrays attribute_offsets)
{
BLI_assert(events.size() == particle_indices_per_event.size());
BLI_assert(events.size() == current_time_per_particle.size());
@@ -194,8 +195,11 @@ BLI_NOINLINE static void execute_events(BlockAllocator &block_allocator,
continue;
}
- EventExecuteInterface interface(
- particles, block_allocator, current_time_per_particle[event_index], event_storage);
+ EventExecuteInterface interface(particles,
+ block_allocator,
+ current_time_per_particle[event_index],
+ event_storage,
+ attribute_offsets);
event->execute(interface);
}
}
@@ -274,7 +278,8 @@ BLI_NOINLINE static void simulate_to_next_event(FixedArrayAllocator &array_alloc
particles_per_event,
current_time_per_particle,
events,
- event_storage);
+ event_storage,
+ attribute_offsets);
find_unfinished_particles(indices_with_event,
particles.indices(),
@@ -397,7 +402,7 @@ BLI_NOINLINE static void apply_remaining_offsets(ParticleSet particles,
auto offsets = attribute_offsets.get_float3(attribute_index);
if (particles.indices_are_trivial()) {
- add_float3_arrays(values.take_front(particles.size()), offsets);
+ add_float3_arrays(values.take_front(particles.size()), offsets.take_front(particles.size()));
}
else {
for (uint pindex : particles.indices()) {
@@ -435,7 +440,7 @@ BLI_NOINLINE static void simulate_block(FixedArrayAllocator &array_allocator,
uint *indices_array = array_allocator.allocate_array<uint>();
VectorAdaptor<uint> unfinished_particle_indices(indices_array, amount);
- simulate_with_max_n_events(10,
+ simulate_with_max_n_events(1,
array_allocator,
block_allocator,
block,
More information about the Bf-blender-cvs
mailing list