CSGPolygon fixes and features: Added angle simplification, UV tiling distance option, and interval type, which allows distance-based intervals (old) and subdivision-based intervals (new to 3.4).
This commit is contained in:
parent
b301514708
commit
d7af7a9b3b
3 changed files with 308 additions and 189 deletions
|
@ -1796,6 +1796,7 @@ CSGBrush *CSGPolygon::_build_brush() {
|
|||
int extrusion_face_count = shape_sides * 2;
|
||||
int end_count = 0;
|
||||
int shape_face_count = shape_faces.size() / 3;
|
||||
real_t curve_length = 1.0;
|
||||
switch (mode) {
|
||||
case MODE_DEPTH:
|
||||
extrusions = 1;
|
||||
|
@ -1808,7 +1809,12 @@ CSGBrush *CSGPolygon::_build_brush() {
|
|||
}
|
||||
break;
|
||||
case MODE_PATH: {
|
||||
curve_length = curve->get_baked_length();
|
||||
if (path_interval_type == PATH_INTERVAL_DISTANCE) {
|
||||
extrusions = MAX(1, Math::ceil(curve_length / path_interval)) + 1;
|
||||
} else {
|
||||
extrusions = Math::ceil(1.0 * curve->get_point_count() / path_interval);
|
||||
}
|
||||
if (!path_joined) {
|
||||
end_count = 2;
|
||||
extrusions -= 1;
|
||||
|
@ -1831,7 +1837,9 @@ CSGBrush *CSGPolygon::_build_brush() {
|
|||
smooth.resize(face_count);
|
||||
materials.resize(face_count);
|
||||
invert.resize(face_count);
|
||||
int faces_removed = 0;
|
||||
|
||||
{
|
||||
PoolVector<Vector3>::Write facesw = faces.write();
|
||||
PoolVector<Vector2>::Write uvsw = uvs.write();
|
||||
PoolVector<bool>::Write smoothw = smooth.write();
|
||||
|
@ -1842,7 +1850,11 @@ CSGBrush *CSGPolygon::_build_brush() {
|
|||
Transform base_xform;
|
||||
Transform current_xform;
|
||||
Transform previous_xform;
|
||||
Transform previous_previous_xform;
|
||||
double u_step = 1.0 / extrusions;
|
||||
if (path_u_distance > 0.0) {
|
||||
u_step *= curve_length / path_u_distance;
|
||||
}
|
||||
double v_step = 1.0 / shape_sides;
|
||||
double spin_step = Math::deg2rad(spin_degrees / spin_sides);
|
||||
double extrusion_step = 1.0 / extrusions;
|
||||
|
@ -1850,7 +1862,7 @@ CSGBrush *CSGPolygon::_build_brush() {
|
|||
if (path_joined) {
|
||||
extrusion_step = 1.0 / (extrusions - 1);
|
||||
}
|
||||
extrusion_step *= curve->get_baked_length();
|
||||
extrusion_step *= curve_length;
|
||||
}
|
||||
|
||||
if (mode == MODE_PATH) {
|
||||
|
@ -1908,8 +1920,13 @@ CSGBrush *CSGPolygon::_build_brush() {
|
|||
}
|
||||
}
|
||||
|
||||
real_t angle_simplify_dot = Math::cos(Math::deg2rad(path_simplify_angle));
|
||||
Vector3 previous_simplify_dir = Vector3(0, 0, 0);
|
||||
int faces_combined = 0;
|
||||
|
||||
// Add extrusion faces.
|
||||
for (int x0 = 0; x0 < extrusions; x0++) {
|
||||
previous_previous_xform = previous_xform;
|
||||
previous_xform = current_xform;
|
||||
|
||||
switch (mode) {
|
||||
|
@ -1937,6 +1954,18 @@ CSGBrush *CSGPolygon::_build_brush() {
|
|||
Vector3 next_point = curve->interpolate_baked(next_offset);
|
||||
Vector3 current_up = Vector3(0, 1, 0);
|
||||
Vector3 direction = next_point - previous_point;
|
||||
Vector3 current_dir = (current_point - previous_point).normalized();
|
||||
|
||||
// If the angles are similar, remove the previous face and replace it with this one.
|
||||
if (path_simplify_angle > 0.0 && x0 > 0 && previous_simplify_dir.dot(current_dir) > angle_simplify_dot) {
|
||||
faces_combined += 1;
|
||||
previous_xform = previous_previous_xform;
|
||||
face -= extrusion_face_count;
|
||||
faces_removed += extrusion_face_count;
|
||||
} else {
|
||||
faces_combined = 0;
|
||||
previous_simplify_dir = current_dir;
|
||||
}
|
||||
|
||||
switch (path_rotation) {
|
||||
case PATH_ROTATION_POLYGON:
|
||||
|
@ -1954,7 +1983,7 @@ CSGBrush *CSGPolygon::_build_brush() {
|
|||
} break;
|
||||
}
|
||||
|
||||
double u0 = x0 * u_step;
|
||||
double u0 = (x0 - faces_combined) * u_step;
|
||||
double u1 = ((x0 + 1) * u_step);
|
||||
if (mode == MODE_PATH && !path_continuous_u) {
|
||||
u0 = 0.0;
|
||||
|
@ -2036,7 +2065,17 @@ CSGBrush *CSGPolygon::_build_brush() {
|
|||
}
|
||||
}
|
||||
|
||||
face_count -= faces_removed;
|
||||
ERR_FAIL_COND_V_MSG(face != face_count, brush, "Bug: Failed to create the CSGPolygon mesh correctly.");
|
||||
}
|
||||
|
||||
if (faces_removed > 0) {
|
||||
faces.resize(face_count * 3);
|
||||
uvs.resize(face_count * 3);
|
||||
smooth.resize(face_count);
|
||||
materials.resize(face_count);
|
||||
invert.resize(face_count);
|
||||
}
|
||||
|
||||
brush->build_from_faces(faces, uvs, smooth, materials, invert);
|
||||
|
||||
|
@ -2095,9 +2134,15 @@ void CSGPolygon::_bind_methods() {
|
|||
ClassDB::bind_method(D_METHOD("set_path_node", "path"), &CSGPolygon::set_path_node);
|
||||
ClassDB::bind_method(D_METHOD("get_path_node"), &CSGPolygon::get_path_node);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("set_path_interval_type", "interval_type"), &CSGPolygon::set_path_interval_type);
|
||||
ClassDB::bind_method(D_METHOD("get_path_interval_type"), &CSGPolygon::get_path_interval_type);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("set_path_interval", "path_interval"), &CSGPolygon::set_path_interval);
|
||||
ClassDB::bind_method(D_METHOD("get_path_interval"), &CSGPolygon::get_path_interval);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("set_path_simplify_angle", "degrees"), &CSGPolygon::set_path_simplify_angle);
|
||||
ClassDB::bind_method(D_METHOD("get_path_simplify_angle"), &CSGPolygon::get_path_simplify_angle);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("set_path_rotation", "path_rotation"), &CSGPolygon::set_path_rotation);
|
||||
ClassDB::bind_method(D_METHOD("get_path_rotation"), &CSGPolygon::get_path_rotation);
|
||||
|
||||
|
@ -2107,6 +2152,9 @@ void CSGPolygon::_bind_methods() {
|
|||
ClassDB::bind_method(D_METHOD("set_path_continuous_u", "enable"), &CSGPolygon::set_path_continuous_u);
|
||||
ClassDB::bind_method(D_METHOD("is_path_continuous_u"), &CSGPolygon::is_path_continuous_u);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("set_path_u_distance", "distance"), &CSGPolygon::set_path_u_distance);
|
||||
ClassDB::bind_method(D_METHOD("get_path_u_distance"), &CSGPolygon::get_path_u_distance);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("set_path_joined", "enable"), &CSGPolygon::set_path_joined);
|
||||
ClassDB::bind_method(D_METHOD("is_path_joined"), &CSGPolygon::is_path_joined);
|
||||
|
||||
|
@ -2128,10 +2176,13 @@ void CSGPolygon::_bind_methods() {
|
|||
ADD_PROPERTY(PropertyInfo(Variant::REAL, "spin_degrees", PROPERTY_HINT_RANGE, "1,360,0.1"), "set_spin_degrees", "get_spin_degrees");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "spin_sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_spin_sides", "get_spin_sides");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "path_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Path"), "set_path_node", "get_path_node");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::REAL, "path_interval", PROPERTY_HINT_RANGE, "0.1,1.0,0.05"), "set_path_interval", "get_path_interval");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "path_interval_type", PROPERTY_HINT_ENUM, "Distance,Subdivide"), "set_path_interval_type", "get_path_interval_type");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::REAL, "path_interval", PROPERTY_HINT_RANGE, "0.01,1.0,0.01,exp,or_greater"), "set_path_interval", "get_path_interval");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::REAL, "path_simplify_angle", PROPERTY_HINT_EXP_RANGE, "0.0,180.0,0.0,or_greater"), "set_path_simplify_angle", "get_path_simplify_angle");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "path_rotation", PROPERTY_HINT_ENUM, "Polygon,Path,PathFollow"), "set_path_rotation", "get_path_rotation");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_local"), "set_path_local", "is_path_local");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_continuous_u"), "set_path_continuous_u", "is_path_continuous_u");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::REAL, "path_u_distance", PROPERTY_HINT_RANGE, "0.0,10.0,0.01,or_greater"), "set_path_u_distance", "get_path_u_distance");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_joined"), "set_path_joined", "is_path_joined");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "SpatialMaterial,ShaderMaterial"), "set_material", "get_material");
|
||||
|
@ -2143,6 +2194,9 @@ void CSGPolygon::_bind_methods() {
|
|||
BIND_ENUM_CONSTANT(PATH_ROTATION_POLYGON);
|
||||
BIND_ENUM_CONSTANT(PATH_ROTATION_PATH);
|
||||
BIND_ENUM_CONSTANT(PATH_ROTATION_PATH_FOLLOW);
|
||||
|
||||
BIND_ENUM_CONSTANT(PATH_INTERVAL_DISTANCE);
|
||||
BIND_ENUM_CONSTANT(PATH_INTERVAL_SUBDIVIDE);
|
||||
}
|
||||
|
||||
void CSGPolygon::set_polygon(const Vector<Vector2> &p_polygon) {
|
||||
|
@ -2186,6 +2240,16 @@ bool CSGPolygon::is_path_continuous_u() const {
|
|||
return path_continuous_u;
|
||||
}
|
||||
|
||||
void CSGPolygon::set_path_u_distance(real_t p_path_u_distance) {
|
||||
path_u_distance = p_path_u_distance;
|
||||
_make_dirty();
|
||||
update_gizmo();
|
||||
}
|
||||
|
||||
real_t CSGPolygon::get_path_u_distance() const {
|
||||
return path_u_distance;
|
||||
}
|
||||
|
||||
void CSGPolygon::set_spin_degrees(const float p_spin_degrees) {
|
||||
ERR_FAIL_COND(p_spin_degrees < 0.01 || p_spin_degrees > 360);
|
||||
spin_degrees = p_spin_degrees;
|
||||
|
@ -2218,8 +2282,17 @@ NodePath CSGPolygon::get_path_node() const {
|
|||
return path_node;
|
||||
}
|
||||
|
||||
void CSGPolygon::set_path_interval_type(PathIntervalType p_interval_type) {
|
||||
path_interval_type = p_interval_type;
|
||||
_make_dirty();
|
||||
update_gizmo();
|
||||
}
|
||||
|
||||
CSGPolygon::PathIntervalType CSGPolygon::get_path_interval_type() const {
|
||||
return path_interval_type;
|
||||
}
|
||||
|
||||
void CSGPolygon::set_path_interval(float p_interval) {
|
||||
ERR_FAIL_COND_MSG(p_interval <= 0 || p_interval > 1, "Path interval must be greater than 0 and less than or equal to 1.0.");
|
||||
path_interval = p_interval;
|
||||
_make_dirty();
|
||||
update_gizmo();
|
||||
|
@ -2229,6 +2302,16 @@ float CSGPolygon::get_path_interval() const {
|
|||
return path_interval;
|
||||
}
|
||||
|
||||
void CSGPolygon::set_path_simplify_angle(float angle) {
|
||||
path_simplify_angle = angle;
|
||||
_make_dirty();
|
||||
update_gizmo();
|
||||
}
|
||||
|
||||
float CSGPolygon::get_path_simplify_angle() const {
|
||||
return path_simplify_angle;
|
||||
}
|
||||
|
||||
void CSGPolygon::set_path_rotation(PathRotation p_rotation) {
|
||||
path_rotation = p_rotation;
|
||||
_make_dirty();
|
||||
|
@ -2296,10 +2379,13 @@ CSGPolygon::CSGPolygon() {
|
|||
spin_degrees = 360;
|
||||
spin_sides = 8;
|
||||
smooth_faces = false;
|
||||
path_interval_type = PATH_INTERVAL_DISTANCE;
|
||||
path_interval = 1.0;
|
||||
path_simplify_angle = 0.0;
|
||||
path_rotation = PATH_ROTATION_PATH_FOLLOW;
|
||||
path_local = false;
|
||||
path_continuous_u = true;
|
||||
path_u_distance = 1.0;
|
||||
path_joined = false;
|
||||
path = nullptr;
|
||||
}
|
||||
|
|
|
@ -346,6 +346,11 @@ public:
|
|||
MODE_PATH
|
||||
};
|
||||
|
||||
enum PathIntervalType {
|
||||
PATH_INTERVAL_DISTANCE,
|
||||
PATH_INTERVAL_SUBDIVIDE
|
||||
};
|
||||
|
||||
enum PathRotation {
|
||||
PATH_ROTATION_POLYGON,
|
||||
PATH_ROTATION_PATH,
|
||||
|
@ -366,7 +371,9 @@ private:
|
|||
int spin_sides;
|
||||
|
||||
NodePath path_node;
|
||||
PathIntervalType path_interval_type;
|
||||
float path_interval;
|
||||
float path_simplify_angle;
|
||||
PathRotation path_rotation;
|
||||
bool path_local;
|
||||
|
||||
|
@ -374,6 +381,7 @@ private:
|
|||
|
||||
bool smooth_faces;
|
||||
bool path_continuous_u;
|
||||
real_t path_u_distance;
|
||||
bool path_joined;
|
||||
|
||||
bool _is_editable_3d_polygon() const;
|
||||
|
@ -406,9 +414,15 @@ public:
|
|||
void set_path_node(const NodePath &p_path);
|
||||
NodePath get_path_node() const;
|
||||
|
||||
void set_path_interval_type(PathIntervalType p_interval_type);
|
||||
PathIntervalType get_path_interval_type() const;
|
||||
|
||||
void set_path_interval(float p_interval);
|
||||
float get_path_interval() const;
|
||||
|
||||
void set_path_simplify_angle(float p_angle);
|
||||
float get_path_simplify_angle() const;
|
||||
|
||||
void set_path_rotation(PathRotation p_rotation);
|
||||
PathRotation get_path_rotation() const;
|
||||
|
||||
|
@ -418,6 +432,9 @@ public:
|
|||
void set_path_continuous_u(bool p_enable);
|
||||
bool is_path_continuous_u() const;
|
||||
|
||||
void set_path_u_distance(real_t p_path_u_distance);
|
||||
real_t get_path_u_distance() const;
|
||||
|
||||
void set_path_joined(bool p_enable);
|
||||
bool is_path_joined() const;
|
||||
|
||||
|
@ -432,5 +449,6 @@ public:
|
|||
|
||||
VARIANT_ENUM_CAST(CSGPolygon::Mode)
|
||||
VARIANT_ENUM_CAST(CSGPolygon::PathRotation)
|
||||
VARIANT_ENUM_CAST(CSGPolygon::PathIntervalType)
|
||||
|
||||
#endif // CSG_SHAPE_H
|
||||
|
|
|
@ -26,6 +26,9 @@
|
|||
<member name="path_interval" type="float" setter="set_path_interval" getter="get_path_interval">
|
||||
When [member mode] is [constant MODE_PATH], the path interval or ratio of path points to extrusions.
|
||||
</member>
|
||||
<member name="path_interval_type" type="int" setter="set_path_interval_type" getter="get_path_interval_type" enum="CSGPolygon.PathIntervalType">
|
||||
When [member mode] is [constant MODE_PATH], this will determine if the interval should be by distance ([constant PATH_INTERVAL_DISTANCE]) or subdivision fractions ([constant PATH_INTERVAL_SUBDIVIDE]).
|
||||
</member>
|
||||
<member name="path_joined" type="bool" setter="set_path_joined" getter="is_path_joined">
|
||||
When [member mode] is [constant MODE_PATH], if [code]true[/code] the ends of the path are joined, by adding an extrusion between the last and first points of the path.
|
||||
</member>
|
||||
|
@ -38,6 +41,12 @@
|
|||
<member name="path_rotation" type="int" setter="set_path_rotation" getter="get_path_rotation" enum="CSGPolygon.PathRotation">
|
||||
When [member mode] is [constant MODE_PATH], the [enum PathRotation] method used to rotate the [member polygon] as it is extruded.
|
||||
</member>
|
||||
<member name="path_simplify_angle" type="float" setter="set_path_simplify_angle" getter="get_path_simplify_angle">
|
||||
When [member mode] is [constant MODE_PATH], extrusions that are less than this angle, will be merged together to reduce polygon count.
|
||||
</member>
|
||||
<member name="path_u_distance" type="float" setter="set_path_u_distance" getter="get_path_u_distance">
|
||||
When [member mode] is [constant MODE_PATH], this is the distance along the path, in meters, the texture coordinates will tile. When set to 0, texture coordinates will match geometry exactly with no tiling.
|
||||
</member>
|
||||
<member name="polygon" type="PoolVector2Array" setter="set_polygon" getter="get_polygon" default="PoolVector2Array( 0, 0, 0, 1, 1, 1, 1, 0 )">
|
||||
The point array that defines the 2D polygon that is extruded.
|
||||
</member>
|
||||
|
@ -72,5 +81,11 @@
|
|||
<constant name="PATH_ROTATION_PATH_FOLLOW" value="2" enum="PathRotation">
|
||||
The [member polygon] shape follows the path and its rotations around the path axis.
|
||||
</constant>
|
||||
<constant name="PATH_INTERVAL_DISTANCE" value="0" enum="PathIntervalType">
|
||||
When [member mode] is set to [constant MODE_PATH], [member path_interval] will determine the distance, in meters, each interval of the path will extrude.
|
||||
</constant>
|
||||
<constant name="PATH_INTERVAL_SUBDIVIDE" value="1" enum="PathIntervalType">
|
||||
When [member mode] is set to [constant MODE_PATH], [member path_interval] will subdivide the polygons along the path.
|
||||
</constant>
|
||||
</constants>
|
||||
</class>
|
||||
|
|
Loading…
Reference in a new issue