Use Vector3 instead of 3 floats for CSGBox3D dimensions

This commit is contained in:
Marcel Admiraal 2020-12-07 18:54:12 +00:00
parent 43c9106806
commit 4da4feed18
5 changed files with 32 additions and 119 deletions

View File

@ -13,7 +13,7 @@
</methods> </methods>
<members> <members>
<member name="size" type="Vector3" setter="set_size" getter="get_size" default="Vector3( 2, 2, 2 )"> <member name="size" type="Vector3" setter="set_size" getter="get_size" default="Vector3( 2, 2, 2 )">
Size of the box mesh. The box's width, height and depth.
</member> </member>
<member name="subdivide_depth" type="int" setter="set_subdivide_depth" getter="get_subdivide_depth" default="0"> <member name="subdivide_depth" type="int" setter="set_subdivide_depth" getter="get_subdivide_depth" default="0">
Number of extra edge loops inserted along the Z axis. Number of extra edge loops inserted along the Z axis.

View File

@ -56,8 +56,7 @@ String CSGShape3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo,
} }
if (Object::cast_to<CSGBox3D>(cs)) { if (Object::cast_to<CSGBox3D>(cs)) {
static const char *hname[3] = { "Width", "Height", "Depth" }; return "Size";
return hname[p_idx];
} }
if (Object::cast_to<CSGCylinder3D>(cs)) { if (Object::cast_to<CSGCylinder3D>(cs)) {
@ -81,14 +80,7 @@ Variant CSGShape3DGizmoPlugin::get_handle_value(EditorNode3DGizmo *p_gizmo, int
if (Object::cast_to<CSGBox3D>(cs)) { if (Object::cast_to<CSGBox3D>(cs)) {
CSGBox3D *s = Object::cast_to<CSGBox3D>(cs); CSGBox3D *s = Object::cast_to<CSGBox3D>(cs);
switch (p_idx) { return s->get_size();
case 0:
return s->get_width();
case 1:
return s->get_height();
case 2:
return s->get_depth();
}
} }
if (Object::cast_to<CSGCylinder3D>(cs)) { if (Object::cast_to<CSGCylinder3D>(cs)) {
@ -149,17 +141,9 @@ void CSGShape3DGizmoPlugin::set_handle(EditorNode3DGizmo *p_gizmo, int p_idx, Ca
d = 0.001; d = 0.001;
} }
switch (p_idx) { Vector3 h = s->get_size();
case 0: h[p_idx] = d * 2;
s->set_width(d * 2); s->set_size(h);
break;
case 1:
s->set_height(d * 2);
break;
case 2:
s->set_depth(d * 2);
break;
}
} }
if (Object::cast_to<CSGCylinder3D>(cs)) { if (Object::cast_to<CSGCylinder3D>(cs)) {
@ -229,38 +213,14 @@ void CSGShape3DGizmoPlugin::commit_handle(EditorNode3DGizmo *p_gizmo, int p_idx,
if (Object::cast_to<CSGBox3D>(cs)) { if (Object::cast_to<CSGBox3D>(cs)) {
CSGBox3D *s = Object::cast_to<CSGBox3D>(cs); CSGBox3D *s = Object::cast_to<CSGBox3D>(cs);
if (p_cancel) { if (p_cancel) {
switch (p_idx) { s->set_size(p_restore);
case 0:
s->set_width(p_restore);
break;
case 1:
s->set_height(p_restore);
break;
case 2:
s->set_depth(p_restore);
break;
}
return; return;
} }
UndoRedo *ur = Node3DEditor::get_singleton()->get_undo_redo(); UndoRedo *ur = Node3DEditor::get_singleton()->get_undo_redo();
ur->create_action(TTR("Change Box Shape Extents")); ur->create_action(TTR("Change Box Shape Size"));
static const char *method[3] = { "set_width", "set_height", "set_depth" }; ur->add_do_method(s, "set_size", s->get_size());
float current = 0; ur->add_undo_method(s, "set_size", p_restore);
switch (p_idx) {
case 0:
current = s->get_width();
break;
case 1:
current = s->get_height();
break;
case 2:
current = s->get_depth();
break;
}
ur->add_do_method(s, method[p_idx], current);
ur->add_undo_method(s, method[p_idx], p_restore);
ur->commit_action(); ur->commit_action();
} }
@ -408,9 +368,13 @@ void CSGShape3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
CSGBox3D *s = Object::cast_to<CSGBox3D>(cs); CSGBox3D *s = Object::cast_to<CSGBox3D>(cs);
Vector<Vector3> handles; Vector<Vector3> handles;
handles.push_back(Vector3(s->get_width() * 0.5, 0, 0));
handles.push_back(Vector3(0, s->get_height() * 0.5, 0)); for (int i = 0; i < 3; i++) {
handles.push_back(Vector3(0, 0, s->get_depth() * 0.5)); Vector3 h;
h[i] = s->get_size()[i] / 2;
handles.push_back(h);
}
p_gizmo->add_handles(handles, handles_material); p_gizmo->add_handles(handles, handles_material);
} }

View File

@ -1125,7 +1125,7 @@ CSGBrush *CSGBox3D::_build_brush() {
int face = 0; int face = 0;
Vector3 vertex_mul(width * 0.5, height * 0.5, depth * 0.5); Vector3 vertex_mul = size / 2;
{ {
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
@ -1194,55 +1194,25 @@ CSGBrush *CSGBox3D::_build_brush() {
} }
void CSGBox3D::_bind_methods() { void CSGBox3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_width", "width"), &CSGBox3D::set_width); ClassDB::bind_method(D_METHOD("set_size", "size"), &CSGBox3D::set_size);
ClassDB::bind_method(D_METHOD("get_width"), &CSGBox3D::get_width); ClassDB::bind_method(D_METHOD("get_size"), &CSGBox3D::get_size);
ClassDB::bind_method(D_METHOD("set_height", "height"), &CSGBox3D::set_height);
ClassDB::bind_method(D_METHOD("get_height"), &CSGBox3D::get_height);
ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CSGBox3D::set_depth);
ClassDB::bind_method(D_METHOD("get_depth"), &CSGBox3D::get_depth);
ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGBox3D::set_material); ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGBox3D::set_material);
ClassDB::bind_method(D_METHOD("get_material"), &CSGBox3D::get_material); ClassDB::bind_method(D_METHOD("get_material"), &CSGBox3D::get_material);
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "width", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_width", "get_width"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size"), "set_size", "get_size");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_height", "get_height");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_depth", "get_depth");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "StandardMaterial3D,ShaderMaterial"), "set_material", "get_material"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "StandardMaterial3D,ShaderMaterial"), "set_material", "get_material");
} }
void CSGBox3D::set_width(const float p_width) { void CSGBox3D::set_size(const Vector3 &p_size) {
width = p_width; size = p_size;
_make_dirty(); _make_dirty();
update_gizmo(); update_gizmo();
_change_notify("width"); _change_notify("size");
} }
float CSGBox3D::get_width() const { Vector3 CSGBox3D::get_size() const {
return width; return size;
}
void CSGBox3D::set_height(const float p_height) {
height = p_height;
_make_dirty();
update_gizmo();
_change_notify("height");
}
float CSGBox3D::get_height() const {
return height;
}
void CSGBox3D::set_depth(const float p_depth) {
depth = p_depth;
_make_dirty();
update_gizmo();
_change_notify("depth");
}
float CSGBox3D::get_depth() const {
return depth;
} }
void CSGBox3D::set_material(const Ref<Material> &p_material) { void CSGBox3D::set_material(const Ref<Material> &p_material) {
@ -1255,13 +1225,6 @@ Ref<Material> CSGBox3D::get_material() const {
return material; return material;
} }
CSGBox3D::CSGBox3D() {
// defaults
width = 2.0;
height = 2.0;
depth = 2.0;
}
/////////////// ///////////////
CSGBrush *CSGCylinder3D::_build_brush() { CSGBrush *CSGCylinder3D::_build_brush() {

View File

@ -240,27 +240,19 @@ class CSGBox3D : public CSGPrimitive3D {
virtual CSGBrush *_build_brush() override; virtual CSGBrush *_build_brush() override;
Ref<Material> material; Ref<Material> material;
float width; Vector3 size = Vector3(2, 2, 2);
float height;
float depth;
protected: protected:
static void _bind_methods(); static void _bind_methods();
public: public:
void set_width(const float p_width); void set_size(const Vector3 &p_size);
float get_width() const; Vector3 get_size() const;
void set_height(const float p_height);
float get_height() const;
void set_depth(const float p_depth);
float get_depth() const;
void set_material(const Ref<Material> &p_material); void set_material(const Ref<Material> &p_material);
Ref<Material> get_material() const; Ref<Material> get_material() const;
CSGBox3D(); CSGBox3D() {}
}; };
class CSGCylinder3D : public CSGPrimitive3D { class CSGCylinder3D : public CSGPrimitive3D {

View File

@ -11,17 +11,11 @@
<methods> <methods>
</methods> </methods>
<members> <members>
<member name="depth" type="float" setter="set_depth" getter="get_depth" default="2.0">
Depth of the box measured from the center of the box.
</member>
<member name="height" type="float" setter="set_height" getter="get_height" default="2.0">
Height of the box measured from the center of the box.
</member>
<member name="material" type="Material" setter="set_material" getter="get_material"> <member name="material" type="Material" setter="set_material" getter="get_material">
The material used to render the box. The material used to render the box.
</member> </member>
<member name="width" type="float" setter="set_width" getter="get_width" default="2.0"> <member name="size" type="Vector3" setter="set_size" getter="get_size" default="Vector3( 2, 2, 2 )">
Width of the box measured from the center of the box. The box's width, height and depth.
</member> </member>
</members> </members>
<constants> <constants>