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
|
#include "BLI_noise.h"
#include "forces.hpp"
namespace BParticles {
Force::~Force()
{
}
void GravityForce::add_force(ForceInterface &interface)
{
MutableArrayRef<float3> destination = interface.combined_destination();
auto inputs = m_compute_inputs->compute(interface);
TemporaryArray<float> weights(destination.size());
m_falloff->compute(interface.attributes(), interface.pindices(), weights);
for (uint pindex : interface.pindices()) {
float3 acceleration = inputs->get<float3>("Direction", 0, pindex);
float weight = weights[pindex];
destination[pindex] += acceleration * weight;
}
};
void TurbulenceForce::add_force(ForceInterface &interface)
{
MutableArrayRef<float3> destination = interface.combined_destination();
auto positions = interface.attributes().get<float3>("Position");
auto inputs = m_compute_inputs->compute(interface);
for (uint pindex : interface.pindices()) {
float3 pos = positions[pindex];
float3 strength = inputs->get<float3>("Strength", 0, pindex);
float x = (BLI_gNoise(0.5f, pos.x, pos.y, pos.z + 1000.0f, false, 1) - 0.5f) * strength.x;
float y = (BLI_gNoise(0.5f, pos.x, pos.y + 1000.0f, pos.z, false, 1) - 0.5f) * strength.y;
float z = (BLI_gNoise(0.5f, pos.x + 1000.0f, pos.y, pos.z, false, 1) - 0.5f) * strength.z;
destination[pindex] += {x, y, z};
}
}
void DragForce::add_force(ForceInterface &interface)
{
MutableArrayRef<float3> destination = interface.combined_destination();
auto velocities = interface.attributes().get<float3>("Velocity");
auto inputs = m_compute_inputs->compute(interface);
TemporaryArray<float> weights(destination.size());
m_falloff->compute(interface.attributes(), interface.pindices(), weights);
for (uint pindex : interface.pindices()) {
float3 velocity = velocities[pindex];
float strength = inputs->get<float>("Strength", 0, pindex);
float weight = weights[pindex];
destination[pindex] -= velocity * strength * weight;
}
}
void MeshForce::add_force(ForceInterface &interface)
{
MutableArrayRef<float3> destination = interface.combined_destination();
auto positions = interface.attributes().get<float3>("Position");
auto inputs = m_compute_inputs->compute(interface);
for (uint pindex : interface.pindices()) {
float3 position = positions[pindex];
float3 local_position = m_world_to_local.transform_position(position);
BVHTreeNearest nearest = {0};
nearest.dist_sq = 10000.0f;
nearest.index = -1;
BLI_bvhtree_find_nearest(m_bvhtree_data.tree,
local_position,
&nearest,
m_bvhtree_data.nearest_callback,
(void *)&m_bvhtree_data);
if (nearest.index == -1) {
continue;
}
float3 difference_local = float3(nearest.co) - local_position;
float3 difference = m_local_to_world.transform_direction(difference_local);
float distance_squared = difference.length_squared();
float factor = 1 / std::max(0.1f, distance_squared);
float strength = inputs->get<float>("Strength", 1, pindex);
destination[pindex] += difference * strength * factor;
}
}
} // namespace BParticles
|