Blender V5.3
gpu_shader_material_align_rotation_to_vector.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
11
13{
14 const float3 v_plane_n = normalize(v_plane);
15 const float mul = dot(p, v_plane_n);
16 return p - v_plane_n * mul;
17}
18
20{
21 const float3 axis_n = normalize(axis);
22 const float3 v1_proj = project_plane_normalized_v3_v3v3(v1, axis_n);
23 const float3 v2_proj = project_plane_normalized_v3_v3v3(v2, axis_n);
24
25 float angle = angle_normalized(v1_proj, v2_proj);
26
27 const float3 tproj = cross(v2_proj, v1_proj);
28 if (dot(tproj, axis) < 0.0f) {
29 angle = M_PI * 2.0f - angle;
30 }
31 return angle;
32}
33
34[[node]]
36 float factor,
37 float3 input_vector,
38 float3 local_main_axis,
39 float4 &rotation)
40{
41 if (is_zero(input_vector)) {
42 rotation = rotation_in;
43 return;
44 }
45
46 const Quaternion quat_in = Quaternion::from_float4(rotation_in);
47 const float3 old_axis = transform_point_by_quaternion(quat_in, local_main_axis);
48 const float3 new_axis = normalize(input_vector);
49
50 float3 rotation_axis = cross(old_axis, new_axis);
51 if (is_zero(rotation_axis)) {
52 /* The vectors are linearly dependent, so we fall back to another axis. */
53 rotation_axis = cross(old_axis, float3(1.0f, 0.0f, 0.0f));
54 if (is_zero(rotation_axis)) {
55 /* This is now guaranteed to not be zero. */
56 rotation_axis = cross(old_axis, float3(0.0f, 1.0f, 0.0f));
57 }
58 }
59
60 const float full_angle = angle_normalized(old_axis, new_axis);
61 const float angle = factor * full_angle;
62
63 AxisAngle aa;
64 aa.axis = normalize(rotation_axis);
65 aa.angle = angle;
66
67 rotation = math_quaternion_multiply(to_quaternion(aa), quat_in).as_float4();
68}
69
70[[node]]
72 float factor,
73 float3 input_vector,
74 float3 local_main_axis,
75 float3 local_pivot_axis,
76 float4 &rotation)
77{
78 if (all(equal(local_main_axis, local_pivot_axis))) {
79 /* Can't compute any meaningful rotation angle in this case. */
80 rotation = rotation_in;
81 return;
82 }
83 if (is_zero(input_vector)) {
84 rotation = rotation_in;
85 return;
86 }
87
88 const Quaternion quat_in = Quaternion::from_float4(rotation_in);
89 const float3 old_axis = transform_point_by_quaternion(quat_in, local_main_axis);
90 const float3 pivot_axis = transform_point_by_quaternion(quat_in, local_pivot_axis);
91
92 float full_angle = angle_signed_on_axis_v3v3_v3(input_vector, old_axis, pivot_axis);
93 if (full_angle > M_PI) {
94 /* Make sure the point is rotated as little as possible. */
95 full_angle -= 2.0f * M_PI;
96 }
97
98 const float angle = factor * full_angle;
99
100 AxisAngle aa;
101 aa.axis = normalize(pivot_axis);
102 aa.angle = angle;
103
104 rotation = math_quaternion_multiply(to_quaternion(aa), quat_in).as_float4();
105}
#define M_PI
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
bool all(VecOp< bool, D >) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
float3 project_plane_normalized_v3_v3v3(float3 p, float3 v_plane)
float angle_signed_on_axis_v3v3_v3(float3 v1, float3 v2, float3 axis)
void align_rotation_to_vector_fixed_pivot(float4 rotation_in, float factor, float3 input_vector, float3 local_main_axis, float3 local_pivot_axis, float4 &rotation)
void align_rotation_to_vector_auto_pivot(float4 rotation_in, float factor, float3 input_vector, float3 local_main_axis, float4 &rotation)
bool is_zero(float3x3 a)
float3 transform_point_by_quaternion(Quaternion q, float3 v)
Quaternion math_quaternion_multiply(Quaternion a, Quaternion b)
Quaternion to_quaternion(EulerXYZ eul)
float angle_normalized(VecT v1, VecT v2)
static Quaternion from_float4(float4 v)