Skip to content

Commit d9d705d

Browse files
committed
Merge pull request godotengine#94479 from juanjp600/opt-in-path-rotation-tangent-fix
Add flag to enable use of accurate path tangents for polygon rotation in `CSGPolygon3D`
2 parents bacf8d1 + dd7bbcc commit d9d705d

File tree

4 files changed

+179
-26
lines changed

4 files changed

+179
-26
lines changed

modules/csg/csg_shape.cpp

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,24 +2234,35 @@ CSGBrush *CSGPolygon3D::_build_brush() {
22342234
base_xform = path->get_global_transform();
22352235
}
22362236

2237-
Vector3 current_point = curve->sample_baked(0);
2238-
Vector3 next_point = curve->sample_baked(extrusion_step);
2237+
Vector3 current_point;
22392238
Vector3 current_up = Vector3(0, 1, 0);
2240-
Vector3 direction = next_point - current_point;
2241-
2242-
if (path_joined) {
2243-
Vector3 last_point = curve->sample_baked(curve->get_baked_length());
2244-
direction = next_point - last_point;
2245-
}
2239+
Vector3 direction;
22462240

22472241
switch (path_rotation) {
22482242
case PATH_ROTATION_POLYGON:
2243+
current_point = curve->sample_baked(0);
22492244
direction = Vector3(0, 0, -1);
22502245
break;
22512246
case PATH_ROTATION_PATH:
2252-
break;
22532247
case PATH_ROTATION_PATH_FOLLOW:
2254-
current_up = curve->sample_baked_up_vector(0, true);
2248+
if (!path_rotation_accurate) {
2249+
current_point = curve->sample_baked(0);
2250+
Vector3 next_point = curve->sample_baked(extrusion_step);
2251+
direction = next_point - current_point;
2252+
2253+
if (path_joined) {
2254+
Vector3 last_point = curve->sample_baked(curve->get_baked_length());
2255+
direction = next_point - last_point;
2256+
}
2257+
} else {
2258+
Transform3D current_sample_xform = curve->sample_baked_with_rotation(0);
2259+
current_point = current_sample_xform.get_origin();
2260+
direction = current_sample_xform.get_basis().xform(Vector3(0, 0, -1));
2261+
}
2262+
2263+
if (path_rotation == PATH_ROTATION_PATH_FOLLOW) {
2264+
current_up = curve->sample_baked_up_vector(0, true);
2265+
}
22552266
break;
22562267
}
22572268

@@ -2307,42 +2318,48 @@ CSGBrush *CSGPolygon3D::_build_brush() {
23072318
case MODE_PATH: {
23082319
double previous_offset = x0 * extrusion_step;
23092320
double current_offset = (x0 + 1) * extrusion_step;
2310-
double next_offset = (x0 + 2) * extrusion_step;
2311-
if (x0 == extrusions - 1) {
2312-
if (path_joined) {
2313-
current_offset = 0;
2314-
next_offset = extrusion_step;
2315-
} else {
2316-
next_offset = current_offset;
2317-
}
2321+
if (path_joined && x0 == extrusions - 1) {
2322+
current_offset = 0;
23182323
}
23192324

23202325
Vector3 previous_point = curve->sample_baked(previous_offset);
2321-
Vector3 current_point = curve->sample_baked(current_offset);
2322-
Vector3 next_point = curve->sample_baked(next_offset);
2326+
Transform3D current_sample_xform = curve->sample_baked_with_rotation(current_offset);
2327+
Vector3 current_point = current_sample_xform.get_origin();
23232328
Vector3 current_up = Vector3(0, 1, 0);
2324-
Vector3 direction = next_point - previous_point;
2325-
Vector3 current_dir = (current_point - previous_point).normalized();
2329+
Vector3 current_extrusion_dir = (current_point - previous_point).normalized();
2330+
Vector3 direction;
23262331

23272332
// If the angles are similar, remove the previous face and replace it with this one.
2328-
if (path_simplify_angle > 0.0 && x0 > 0 && previous_simplify_dir.dot(current_dir) > angle_simplify_dot) {
2333+
if (path_simplify_angle > 0.0 && x0 > 0 && previous_simplify_dir.dot(current_extrusion_dir) > angle_simplify_dot) {
23292334
faces_combined += 1;
23302335
previous_xform = previous_previous_xform;
23312336
face -= extrusion_face_count;
23322337
faces_removed += extrusion_face_count;
23332338
} else {
23342339
faces_combined = 0;
2335-
previous_simplify_dir = current_dir;
2340+
previous_simplify_dir = current_extrusion_dir;
23362341
}
23372342

23382343
switch (path_rotation) {
23392344
case PATH_ROTATION_POLYGON:
23402345
direction = Vector3(0, 0, -1);
23412346
break;
23422347
case PATH_ROTATION_PATH:
2343-
break;
23442348
case PATH_ROTATION_PATH_FOLLOW:
2345-
current_up = curve->sample_baked_up_vector(current_offset, true);
2349+
if (!path_rotation_accurate) {
2350+
double next_offset = (x0 + 2) * extrusion_step;
2351+
if (x0 == extrusions - 1) {
2352+
next_offset = path_joined ? extrusion_step : current_offset;
2353+
}
2354+
Vector3 next_point = curve->sample_baked(next_offset);
2355+
direction = next_point - previous_point;
2356+
} else {
2357+
direction = current_sample_xform.get_basis().xform(Vector3(0, 0, -1));
2358+
}
2359+
2360+
if (path_rotation == PATH_ROTATION_PATH_FOLLOW) {
2361+
current_up = curve->sample_baked_up_vector(current_offset, true);
2362+
}
23462363
break;
23472364
}
23482365

@@ -2512,6 +2529,9 @@ void CSGPolygon3D::_bind_methods() {
25122529
ClassDB::bind_method(D_METHOD("set_path_rotation", "path_rotation"), &CSGPolygon3D::set_path_rotation);
25132530
ClassDB::bind_method(D_METHOD("get_path_rotation"), &CSGPolygon3D::get_path_rotation);
25142531

2532+
ClassDB::bind_method(D_METHOD("set_path_rotation_accurate", "enable"), &CSGPolygon3D::set_path_rotation_accurate);
2533+
ClassDB::bind_method(D_METHOD("get_path_rotation_accurate"), &CSGPolygon3D::get_path_rotation_accurate);
2534+
25152535
ClassDB::bind_method(D_METHOD("set_path_local", "enable"), &CSGPolygon3D::set_path_local);
25162536
ClassDB::bind_method(D_METHOD("is_path_local"), &CSGPolygon3D::is_path_local);
25172537

@@ -2543,6 +2563,7 @@ void CSGPolygon3D::_bind_methods() {
25432563
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_interval", PROPERTY_HINT_RANGE, "0.01,1.0,0.01,exp,or_greater"), "set_path_interval", "get_path_interval");
25442564
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_simplify_angle", PROPERTY_HINT_RANGE, "0.0,180.0,0.1"), "set_path_simplify_angle", "get_path_simplify_angle");
25452565
ADD_PROPERTY(PropertyInfo(Variant::INT, "path_rotation", PROPERTY_HINT_ENUM, "Polygon,Path,PathFollow"), "set_path_rotation", "get_path_rotation");
2566+
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_rotation_accurate"), "set_path_rotation_accurate", "get_path_rotation_accurate");
25462567
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_local"), "set_path_local", "is_path_local");
25472568
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_continuous_u"), "set_path_continuous_u", "is_path_continuous_u");
25482569
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_u_distance", PROPERTY_HINT_RANGE, "0.0,10.0,0.01,or_greater,suffix:m"), "set_path_u_distance", "get_path_u_distance");
@@ -2685,6 +2706,16 @@ CSGPolygon3D::PathRotation CSGPolygon3D::get_path_rotation() const {
26852706
return path_rotation;
26862707
}
26872708

2709+
void CSGPolygon3D::set_path_rotation_accurate(bool p_enabled) {
2710+
path_rotation_accurate = p_enabled;
2711+
_make_dirty();
2712+
update_gizmos();
2713+
}
2714+
2715+
bool CSGPolygon3D::get_path_rotation_accurate() const {
2716+
return path_rotation_accurate;
2717+
}
2718+
26882719
void CSGPolygon3D::set_path_local(bool p_enable) {
26892720
path_local = p_enable;
26902721
_make_dirty();
@@ -2746,6 +2777,7 @@ CSGPolygon3D::CSGPolygon3D() {
27462777
path_interval = 1.0;
27472778
path_simplify_angle = 0.0;
27482779
path_rotation = PATH_ROTATION_PATH_FOLLOW;
2780+
path_rotation_accurate = false;
27492781
path_local = false;
27502782
path_continuous_u = true;
27512783
path_u_distance = 1.0;

modules/csg/csg_shape.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ class CSGPolygon3D : public CSGPrimitive3D {
401401
float path_interval;
402402
float path_simplify_angle;
403403
PathRotation path_rotation;
404+
bool path_rotation_accurate;
404405
bool path_local;
405406

406407
Path3D *path = nullptr;
@@ -452,6 +453,9 @@ class CSGPolygon3D : public CSGPrimitive3D {
452453
void set_path_rotation(PathRotation p_rotation);
453454
PathRotation get_path_rotation() const;
454455

456+
void set_path_rotation_accurate(bool p_enable);
457+
bool get_path_rotation_accurate() const;
458+
455459
void set_path_local(bool p_enable);
456460
bool is_path_local() const;
457461

modules/csg/doc_classes/CSGPolygon3D.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
<member name="path_rotation" type="int" setter="set_path_rotation" getter="get_path_rotation" enum="CSGPolygon3D.PathRotation">
4242
When [member mode] is [constant MODE_PATH], the path rotation method used to rotate the [member polygon] as it is extruded.
4343
</member>
44+
<member name="path_rotation_accurate" type="bool" setter="set_path_rotation_accurate" getter="get_path_rotation_accurate">
45+
When [member mode] is [constant MODE_PATH], if [code]true[/code] the polygon will be rotated according to the proper tangent of the path at the sampled points. If [code]false[/code] an approximation is used, which decreases in accuracy as the number of subdivisions decreases.
46+
</member>
4447
<member name="path_simplify_angle" type="float" setter="set_path_simplify_angle" getter="get_path_simplify_angle">
4548
When [member mode] is [constant MODE_PATH], extrusions that are less than this angle, will be merged together to reduce polygon count.
4649
</member>

modules/csg/tests/test_csg.h

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**************************************************************************/
2+
/* test_csg.h */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#ifndef TEST_CSG_H
32+
#define TEST_CSG_H
33+
34+
#include "../csg.h"
35+
#include "../csg_shape.h"
36+
37+
#include "tests/test_macros.h"
38+
39+
namespace TestCSG {
40+
41+
TEST_CASE("[SceneTree][CSG] CSGPolygon3D") {
42+
SUBCASE("[SceneTree][CSG] CSGPolygon3D: using accurate path tangent for polygon rotation") {
43+
const float polygon_radius = 10.0f;
44+
45+
const Vector3 expected_min_bounds = Vector3(-polygon_radius, -polygon_radius, 0);
46+
const Vector3 expected_max_bounds = Vector3(100 + polygon_radius, polygon_radius, 100);
47+
const AABB expected_aabb = AABB(expected_min_bounds, expected_max_bounds - expected_min_bounds);
48+
49+
Ref<Curve3D> curve;
50+
curve.instantiate();
51+
curve->add_point(
52+
// p_position
53+
Vector3(0, 0, 0),
54+
// p_in
55+
Vector3(),
56+
// p_out
57+
Vector3(0, 0, 60));
58+
curve->add_point(
59+
// p_position
60+
Vector3(100, 0, 100),
61+
// p_in
62+
Vector3(0, 0, -60),
63+
// p_out
64+
Vector3());
65+
66+
Path3D *path = memnew(Path3D);
67+
path->set_curve(curve);
68+
69+
CSGPolygon3D *csg_polygon_3d = memnew(CSGPolygon3D);
70+
SceneTree::get_singleton()->get_root()->add_child(csg_polygon_3d);
71+
72+
csg_polygon_3d->add_child(path);
73+
csg_polygon_3d->set_path_node(csg_polygon_3d->get_path_to(path));
74+
csg_polygon_3d->set_mode(CSGPolygon3D::Mode::MODE_PATH);
75+
76+
PackedVector2Array polygon;
77+
polygon.append(Vector2(-polygon_radius, 0));
78+
polygon.append(Vector2(0, polygon_radius));
79+
polygon.append(Vector2(polygon_radius, 0));
80+
polygon.append(Vector2(0, -polygon_radius));
81+
csg_polygon_3d->set_polygon(polygon);
82+
83+
csg_polygon_3d->set_path_rotation(CSGPolygon3D::PathRotation::PATH_ROTATION_PATH);
84+
csg_polygon_3d->set_path_rotation_accurate(true);
85+
86+
// Minimize the number of extrusions.
87+
// This decreases the number of samples taken from the curve.
88+
// Having fewer samples increases the inaccuracy of the line between samples as an approximation of the tangent of the curve.
89+
// With correct polygon orientation, the bounding box for the given curve should be independent of the number of extrusions.
90+
csg_polygon_3d->set_path_interval_type(CSGPolygon3D::PathIntervalType::PATH_INTERVAL_DISTANCE);
91+
csg_polygon_3d->set_path_interval(1000.0f);
92+
93+
// Call get_brush_faces to force the bounding box to update.
94+
csg_polygon_3d->get_brush_faces();
95+
96+
CHECK(csg_polygon_3d->get_aabb().is_equal_approx(expected_aabb));
97+
98+
// Perform the bounding box check again with a greater number of extrusions.
99+
csg_polygon_3d->set_path_interval(1.0f);
100+
csg_polygon_3d->get_brush_faces();
101+
102+
CHECK(csg_polygon_3d->get_aabb().is_equal_approx(expected_aabb));
103+
104+
csg_polygon_3d->remove_child(path);
105+
SceneTree::get_singleton()->get_root()->remove_child(csg_polygon_3d);
106+
107+
memdelete(csg_polygon_3d);
108+
memdelete(path);
109+
}
110+
}
111+
112+
} // namespace TestCSG
113+
114+
#endif // TEST_CSG_H

0 commit comments

Comments
 (0)