47static float shaperadius(
const float shape,
const float root,
const float tip,
const float time)
51 float radius = 1.0f - time;
55 radius =
powf(radius, 1.0f + shape);
58 radius =
powf(radius, 1.0f / (1.0f - shape));
61 return (radius * (root - tip)) + tip;
75 if (!(hair && b_mesh && b_ob && CData)) {
79 const Transform tfm = get_transform(b_ob->object_to_world());
92 const int shader =
clamp(b_part->
omat - 1, 0, hair->get_used_shaders().size() - 1);
94 const int totparts = b_psys->
totpart;
95 const int totchild = background ?
97 (int)((
float)b_psys->
totchild * (
float)b_part->
disp / 100.0f);
98 int totcurves = totchild;
100 if (b_part->
childtype == 0 || totchild == 0) {
101 totcurves += totparts;
104 if (totcurves == 0) {
108 int ren_step = (1 << display_step) + 1;
117 const float radius = b_part->
rad_scale * 0.5f;
126 if (!(b_part->
childtype == 0) && totchild != 0) {
130 const int num_add = (totparts + totchild - pa_no);
137 for (; pa_no < totparts + totchild; pa_no++) {
141 float curve_length = 0.0f;
144 for (
int step_no = 0; step_no < ren_step; step_no++) {
145 float3 co_world = prev_co_world;
146 BKE_particle_co_hair(b_psys, b_ob, pa_no, step_no, &co_world.
x);
149 const float step_length =
len(co_object - prev_co_object);
150 curve_length += step_length;
154 prev_co_object = co_object;
155 prev_co_world = co_world;
178 if (!(hair && b_mesh && b_ob && CData)) {
194 const int totparts = b_psys->
totpart;
195 const int totchild = background ?
197 (int)((
float)b_psys->
totchild * (
float)b_part->
disp / 100.0f);
198 int totcurves = totchild;
200 if (b_part->
childtype == 0 || totchild == 0) {
201 totcurves += totparts;
204 if (totcurves == 0) {
209 if (!(b_part->
childtype == 0) && totchild != 0) {
213 const int num_add = (totparts + totchild - pa_no);
218 auto *b_pa = b_psys_particles.
begin();
219 for (; pa_no < totparts + totchild; pa_no++) {
222 BKE_particle_uv_on_emitter(b_psys,
nullptr, psmd, b_pa, pa_no, uv_num, &uv.
x);
225 if (pa_no < totparts && b_pa != b_psys_particles.
end()) {
243 if (!(hair && b_mesh && b_ob && CData)) {
259 const int totparts = b_psys->
totpart;
260 const int totchild = background ?
262 (int)((
float)b_psys->
totchild * (
float)b_part->
disp / 100.0f);
263 int totcurves = totchild;
265 if (b_part->
childtype == 0 || totchild == 0) {
266 totcurves += totparts;
269 if (totcurves == 0) {
274 if (!(b_part->
childtype == 0) && totchild != 0) {
278 const int num_add = (totparts + totchild - pa_no);
283 auto *b_pa = b_psys_particles.
begin();
284 for (; pa_no < totparts + totchild; pa_no++) {
287 BKE_particle_mcol_on_emitter(b_psys,
nullptr, psmd, b_pa, pa_no, vcol_num, &vcol.
x);
290 if (pa_no < totparts && b_pa != b_psys_particles.
end()) {
313 curve < CData->psys_firstcurve[sys] + CData->
psys_curvenum[sys];
324 float *attr_intercept =
nullptr;
325 float *attr_length =
nullptr;
326 float *attr_random =
nullptr;
341 int *curve_first_key = hair->get_curve_first_key().data();
342 int *curve_shader = hair->get_curve_shader().data();
353 curve < CData->psys_firstcurve[sys] + CData->
psys_curvenum[sys];
356 curve_first_key[num_curves] = num_keys;
359 curvekey < CData->curve_firstkey[curve] + CData->
curve_keynum[curve];
365 const float time = (curve_length > 0.0f) ? curve_time / curve_length : 0.0f;
373 curve_keys[num_keys] = ickey_loc;
374 curve_radius[num_keys] = radius;
375 if (attr_intercept) {
376 attr_intercept[num_keys] = time;
388 if (attr_length !=
nullptr) {
392 if (attr_random !=
nullptr) {
396 curve_shader[num_curves] = CData->
psys_shader[sys];
404 hair->tag_curve_first_key_modified();
405 hair->tag_curve_shader_modified();
409 LOG_ERROR <<
"Hair memory allocation failed, clearing data.";
422 const float time = (curve_length > 0.0f) ? curve_time / curve_length : 0.0f;
447 int curvekey = (int)
floorf(curve_key_f);
448 const float remainder = curve_key_f - curvekey;
449 if (remainder == 0.0f) {
452 int curvekey2 = curvekey + 1;
455 curvekey = curvekey2 - 1;
459 return mix(mP, mP2, remainder);
463 const int motion_step,
464 const int num_motion_keys,
469 const int num_keys = hair->
num_keys();
471 if (num_motion_keys != num_keys || !have_motion) {
473 if (num_motion_keys != num_keys) {
474 LOG_DEBUG <<
"Hair topology changed, removing motion attribute.";
479 else if (motion_step > 0) {
487 std::copy_n(hair->
get_radius(), num_keys, mR);
497 bool new_attribute =
false;
502 new_attribute =
true;
506 const int attr_step = motion_step + 1;
509 bool have_motion =
false;
512 const int num_keys = hair->
num_keys();
516 curve < CData->psys_firstcurve[sys] + CData->
psys_curvenum[sys];
520 const int curve_key_end = (num_curves + 1 < (int)hair->get_curve_first_key().
size() ?
521 hair->get_curve_first_key()[num_curves + 1] :
523 const int num_center_curve_keys = curve_key_end - hair->get_curve_first_key()[num_curves];
524 const int is_num_keys_different = CData->
curve_keynum[curve] - num_center_curve_keys;
526 if (!is_num_keys_different) {
528 curvekey < CData->curve_firstkey[curve] + CData->
curve_keynum[curve];
552 const float step_size = num_center_curve_keys > 1 ? 1.0f / (num_center_curve_keys - 1) :
554 for (
int step_index = 0; step_index < num_center_curve_keys; ++step_index) {
555 const float step = step_index * step_size;
569 std::copy_n(hair->
get_position() + i, num_keys - i, mP + i);
570 std::copy_n(hair->
get_radius() + i, num_keys - i, mR + i);
584 for (blender::ModifierData &b_mod : b_ob->
modifiers) {
589 auto *psmd =
reinterpret_cast<blender::ParticleSystemModifierData *
>(&b_mod);
590 blender::ParticleSystem *b_psys = psmd->psys;
591 blender::ParticleSettings *b_part = b_psys->
part;
603void BlenderSync::sync_particle_hair(
Hair *hair,
607 const int motion_step)
612 const blender::Object &b_ob = *b_ob_info.
real_object;
621 ParticleCurveData CData;
639 mesh_texture_space(b_mesh, loc, size);
644 for (
size_t i = 0; i < hair->
num_curves(); i++) {
646 generated[i] = co *
size - loc;
653 blender::Vector<blender::StringRef> vcol_names;
654 b_mesh.attributes().foreach_attribute([&](
const blender::bke::AttributeIter &iter) {
664 for (
const int vcol_num : vcol_names.
index_range()) {
665 const ustring name = ustring(std::string_view(vcol_names[vcol_num]));
680 for (
size_t curve = 0; curve < CData.
curve_vcol.
size(); curve++) {
689 const blender::VectorSet<blender::StringRefNull> uv_names = b_mesh.uv_map_names();
690 const ustring default_name = ustring(std::string_view(b_mesh.default_uv_map_name()));
692 const ustring name = ustring(std::string_view(uv_names[uv_num]));
693 const bool active_render = name == default_name;
714 for (
size_t curve = 0; curve < CData.
curve_uv.
size(); curve++) {
727 const float motion_scale)
729 const int num_curve_keys = hair->
num_keys();
732 hair->set_motion_steps(3);
741 const float motion_times[2] = {-1.0f, 1.0f};
743 const float relative_time = motion_times[
step - 1] * 0.5f * motion_scale;
746 for (
int i = 0; i < num_curve_keys; i++) {
770 const bool need_motion,
771 const float motion_scale)
776 static const ustring u_velocity(
"velocity");
778 bool have_uv =
false;
781 const ustring name{std::string_view(iter.
name)};
786 if (need_motion && name == u_velocity) {
811 if (attributes.
find(name)) {
818 using Converter = typename ccl::AttributeConverter<BlenderT>;
819 using CyclesT = typename Converter::CyclesT;
820 if constexpr (!std::is_void_v<CyclesT>) {
821 const blender::VArray<BlenderT> src_varray = b_attr.varray.typed<BlenderT>();
822 const blender::CommonVArrayInfo info = b_attr.varray.common_info();
824 if (info.type == blender::CommonVArrayInfo::Type::Single) {
825 const auto &single_value = *static_cast<const BlenderT *>(info.data);
826 Attribute *attr = attributes.add(name, Converter::type_desc, ATTR_ELEMENT_MESH);
827 CyclesT *data = reinterpret_cast<CyclesT *>(attr->data_for_write());
828 *data = Converter::convert(single_value);
832 const AttributeElement element = blender_domain_to_attr_element(b_attr.domain);
833 if constexpr (Converter::layout_compatible) {
834 if (src_varray.is_span() && b_attr.sharing_info) {
835 attributes.add_shared(name,
836 Converter::type_desc,
840 b_attr.sharing_info);
845 Attribute *attr = attributes.add(name, Converter::type_desc, element);
846 CyclesT *data = reinterpret_cast<CyclesT *>(attr->data_for_write());
848 const blender::VArraySpan src = src_varray;
849 for (const int i : src.index_range()) {
850 data[i] = Converter::convert(src[i]);
862 b_positions[
index][0], b_positions[
index][1], b_positions[
index][2], 0.0f);
869 const int first_point_index,
870 const int num_points,
873 const float curve_t =
step * (num_points - 1);
874 const int point_a =
clamp((
int)curve_t, 0, num_points - 1);
875 const int point_b =
min(point_a + 1, num_points - 1);
876 const float t = curve_t - (
float)point_a;
885 const bool need_motion,
886 const float motion_scale)
913 std::fill(curve_radius, curve_radius + positions.
size(), 0.005f);
916 int *curve_first_key = hair->get_curve_first_key().data();
917 int *curve_shader = hair->get_curve_shader().data();
920 float *attr_intercept =
nullptr;
921 float *attr_length =
nullptr;
929 b_curves, {point_normals.data(),
int64_t(point_normals.size())});
932 make_float3(point_normals[i][0], point_normals[i][1], point_normals[i][2]));
944 for (
const int i : points_by_curve.
index_range()) {
949 std::copy(points_by_curve.
data().data(),
950 points_by_curve.
data().data() + points_by_curve.
size(),
952 std::fill(curve_shader, curve_shader + points_by_curve.
size(), 0);
955 if (attr_length || attr_intercept) {
956 for (
const int curve : points_by_curve.
index_range()) {
962 for (
const int point : points) {
964 positions[point][0], positions[point][1], positions[point][2]);
966 if (point != points.
first()) {
971 if (attr_intercept) {
972 attr_intercept[point] =
length;
977 if (attr_intercept &&
length > 0.0f) {
978 for (
const int point : points.
drop_front(1)) {
979 attr_intercept[point] /=
length;
985 attr_length[curve] =
length;
1001 for (
const int i : points_by_curve.
index_range()) {
1011 const int motion_step)
1016 bool new_attribute =
false;
1021 new_attribute =
true;
1024 const size_t num_keys = hair->
num_keys();
1025 const int attr_step = motion_step + 1;
1028 bool have_motion =
false;
1029 int num_motion_keys = 0;
1044 std::fill(mR, mR + num_keys, 0.005f);
1051 const float *motion_R = attr_R->
data<
float>(attr_step);
1053 have_motion = (motion_P != center_P &&
1054 std::memcmp(motion_P, center_P, num_keys *
sizeof(
packed_float3)) != 0) ||
1055 (motion_R != center_R &&
1056 std::memcmp(motion_R, center_R, num_keys *
sizeof(
float)) != 0);
1057 num_motion_keys = num_keys;
1064 const size_t num_curves = hair->
num_curves();
1067 int curve_index = 0;
1074 for (
const int i : points_by_curve.
index_range()) {
1076 if (curve_index >= num_curves) {
1086 const int point = points[i];
1088 if (num_motion_keys < num_keys) {
1091 mR[num_motion_keys] = cv.
w;
1095 curve_key.
w = hair->
get_radius()[num_motion_keys];
1096 have_motion = !(cv == curve_key);
1105 const float step_size = curve.
num_keys > 1 ? 1.0f / (curve.
num_keys - 1) : 0.0f;
1106 for (
int i = 0; i < curve.
num_keys; i++) {
1107 const float step = i * step_size;
1109 b_positions, b_radius, points.
start(), points.
size(),
step);
1111 mR[num_motion_keys] = cv.
w;
1120 if (new_attribute) {
1126void BlenderSync::sync_hair(
Hair *hair,
BObjectInfo &b_ob_info,
bool motion,
const int motion_step)
1129 const bool need_motion = object_need_motion_attribute(b_ob_info, scene);
1130 const float motion_scale = (need_motion) ? scene->motion_shutter_time() /
1131 (b_scene->r.frs_sec / b_scene->r.frs_sec_base) :
1135 const blender::bke::CurvesGeometry &b_curves(
1136 blender::id_cast<const blender::Curves *>(b_ob_info.
object_data)->geometry.wrap());
1144 const blender::VArray<int8_t> b_types = b_curves.curve_types();
1158 array<Node *> used_shaders = hair->get_used_shaders();
1161 new_hair.set_used_shaders(used_shaders);
1163 if (view_layer.use_hair) {
1166 sync_hair(&new_hair, b_ob_info,
false);
1173 sync_particle_hair(&new_hair, *b_mesh, b_ob_info,
false);
1174 free_object_to_mesh(b_ob_info, *b_mesh);
1181 new_hair.set_motion_steps(2);
1197 for (
const SocketType &socket : new_hair.
type->
inputs) {
1199 if (socket.
name ==
"use_motion_blur" || socket.
name ==
"used_shaders") {
1202 hair->
set_value(socket, new_hair, socket);
1218void BlenderSync::sync_hair_motion(
BObjectInfo &b_ob_info,
Hair *hair,
const int motion_step)
1226 if (ccl::BKE_object_is_deform_modified(b_ob_info, *b_scene, preview)) {
1229 sync_hair(hair, b_ob_info,
true, motion_step);
1236 sync_particle_hair(hair, *b_mesh, b_ob_info,
true, motion_step);
1237 free_object_to_mesh(b_ob_info, *b_mesh);
Low-level operations for curves.
CustomData interface, see also DNA_customdata_types.h.
blender::float3 packed_float3
bool sync_attribute_from_blender(AttributeSet &attributes, const AttributeStandard std, const blender::bke::AttributeReader< BlenderT > &b_reader, const int size)
bool sync_attribute_motion_step_from_blender(Attribute &attr, const int motion_step, const blender::bke::AttributeReader< BlenderT > &b_reader)
static void ExportCurveSegmentsMotion(Hair *hair, ParticleCurveData *CData, const int motion_step)
static void ExportCurveSegments(Scene *scene, Hair *hair, ParticleCurveData *CData)
static void export_hair_curves_motion(Hair *hair, const blender::bke::CurvesGeometry &b_curves, const int motion_step)
static float4 LerpCurveSegmentMotionCV(ParticleCurveData *CData, const int sys, const int curve, const float step)
static AttributeElement blender_domain_to_attr_element(const blender::bke::AttrDomain b_domain)
static void export_hair_curves(Scene *scene, Hair *hair, const blender::bke::CurvesGeometry &b_curves, const bool need_motion, const float motion_scale)
static float4 curve_point_as_float4(const blender::Span< blender::float3 > b_positions, const blender::Span< float > b_radius, const int index)
static float shaperadius(const float shape, const float root, const float tip, const float time)
static float4 CurveSegmentMotionCV(ParticleCurveData *CData, const int sys, const int curve, const int curvekey)
static void attr_create_motion_from_velocity(Hair *hair, const blender::Span< blender::float3 > src, const float motion_scale)
static bool ObtainCacheParticleData(Hair *hair, const blender::Mesh *b_mesh, const blender::Object *b_ob, ParticleCurveData *CData, bool background)
static float4 interpolate_curve_points(const blender::Span< blender::float3 > b_positions, const blender::Span< float > b_radius, const int first_point_index, const int num_points, const float step)
static void export_hair_motion_validate_attribute(Hair *hair, const int motion_step, const int num_motion_keys, bool have_motion)
static bool ObtainCacheParticleUV(Hair *hair, const blender::Mesh *b_mesh, const blender::Object *b_ob, ParticleCurveData *CData, bool background, const int uv_num)
static void attr_create_generic(Scene *scene, Hair *hair, const blender::bke::CurvesGeometry &b_curves, const bool need_motion, const float motion_scale)
static bool hair_topology_matches(const Hair *hair, const blender::bke::CurvesGeometry &b_curves)
static bool ObtainCacheParticleVcol(Hair *hair, const blender::Mesh *b_mesh, const blender::Object *b_ob, ParticleCurveData *CData, bool background, const int vcol_num)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
void update(AttributeSet &&new_attributes)
Attribute * find(ustring name) const
Attribute * add(ustring name, const TypeDesc type, AttributeElement element)
void take_motion_from(Attribute &other)
const T * data(const int step=0) const
T * data_for_write(const int step=0)
void add_motion(const Geometry *geom)
bool radius_is_modified() const
void tag_position_modified()
const packed_float3 * get_position() const
void tag_update(Scene *scene, bool rebuild)
float * get_radius_for_write()
bool position_is_modified() const
const float * get_radius() const
bool need_attribute(const Scene *scene, AttributeStandard std)
void tag_radius_modified()
packed_float3 * get_position_for_write()
Curve get_curve(const size_t i) const
size_t num_curves() const
void copy_center_to_motion_step(const int motion_step)
void resize_curves(const int numcurves, const int numkeys)
CurveShapeType curve_shape
void clear(bool preserve_shaders=false) override
array< int > curve_firstkey
array< bool > psys_closetip
array< float > psys_tipradius
array< float > curve_length
array< float4 > curve_vcol
array< float > curvekey_time
array< float > psys_rootradius
array< int > psys_firstcurve
array< int > psys_curvenum
array< float > psys_shape
array< int > curve_keynum
array< float3 > curvekey_co
@ MOTION_PASS_INTERACTIVE
void reserve(const size_t newcapacity)
void push_back_slow(const T &t)
const CPPType & type() const
constexpr int64_t first() const
constexpr int64_t size() const
constexpr int64_t start() const
constexpr IndexRange index_range() const
constexpr IndexRange drop_front(int64_t n) const
constexpr T * end() const
constexpr T * begin() const
constexpr int64_t size() const
constexpr IndexRange index_range() const
constexpr bool is_empty() const
IndexRange index_range() const
void append(const T &value)
IndexRange index_range() const
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
GAttributeReader lookup(const StringRef name) const
GAttributeReader get() const
AttributeAccessor attributes() const
Span< float3 > positions() const
OffsetIndices< int > points_by_curve() const
Span< NewT > constexpr cast() const
IndexRange index_range() const
ccl_device float4 color_srgb_to_linear_v4(const float4 c)
#define CCL_NAMESPACE_END
float hash_uint2_to_float(uint kx, uint ky)
#define assert(assertion)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
VecBase< float, 2 > float2
VecBase< float, 4 > float4
VecBase< float, 3 > float3
@ ATTR_STD_CURVE_INTERCEPT
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
ccl_device_inline dual1 len_squared(const ccl_private dual< T > &a)
CCL_NAMESPACE_BEGIN ccl_device_inline float2 zero_float2()
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
void to_static_type(const CPPType &cpp_type, Fn &&fn)
void curves_normals_point_domain_calc(const CurvesGeometry &curves, MutableSpan< float3 > normals)
int size(const AssetLibraryReference *library_reference)
@ eModifierType_ParticleSystem
VecBase< float, 2 > float2
VecBase< float, 3 > float3
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
bool is_real_object_data() const
blender::ID * object_data
blender::Object * real_object
vector< SocketType, std::allocator< SocketType > > inputs
void set_value(const SocketType &socket, const Node &other, const SocketType &other_socket)
ListBaseT< ModifierData > modifiers
eParticleChildType childtype
eParticleShapeFlag shape_flag