summaryrefslogtreecommitdiff
path: root/modules/csg
diff options
context:
space:
mode:
Diffstat (limited to 'modules/csg')
-rw-r--r--modules/csg/csg.cpp74
-rw-r--r--modules/csg/csg_gizmos.cpp81
-rw-r--r--modules/csg/csg_gizmos.h24
-rw-r--r--modules/csg/csg_shape.cpp28
4 files changed, 112 insertions, 95 deletions
diff --git a/modules/csg/csg.cpp b/modules/csg/csg.cpp
index 4e6e701bfd..87c2caec0d 100644
--- a/modules/csg/csg.cpp
+++ b/modules/csg/csg.cpp
@@ -62,7 +62,7 @@ void CSGBrush::build_from_faces(const PoolVector<Vector3> &p_vertices, const Poo
faces.resize(p_vertices.size() / 3);
for (int i = 0; i < faces.size(); i++) {
- Face &f = faces[i];
+ Face &f = faces.write[i];
f.vertices[0] = rv[i * 3 + 0];
f.vertices[1] = rv[i * 3 + 1];
f.vertices[2] = rv[i * 3 + 2];
@@ -101,7 +101,7 @@ void CSGBrush::build_from_faces(const PoolVector<Vector3> &p_vertices, const Poo
materials.resize(material_map.size());
for (Map<Ref<Material>, int>::Element *E = material_map.front(); E; E = E->next()) {
- materials[E->get()] = E->key();
+ materials.write[E->get()] = E->key();
}
_regen_face_aabbs();
@@ -111,10 +111,10 @@ void CSGBrush::_regen_face_aabbs() {
for (int i = 0; i < faces.size(); i++) {
- faces[i].aabb.position = faces[i].vertices[0];
- faces[i].aabb.expand_to(faces[i].vertices[1]);
- faces[i].aabb.expand_to(faces[i].vertices[2]);
- faces[i].aabb.grow_by(faces[i].aabb.get_longest_axis_size() * 0.001); //make it a tad bigger to avoid num precision erros
+ faces.write[i].aabb.position = faces[i].vertices[0];
+ faces.write[i].aabb.expand_to(faces[i].vertices[1]);
+ faces.write[i].aabb.expand_to(faces[i].vertices[2]);
+ faces.write[i].aabb.grow_by(faces[i].aabb.get_longest_axis_size() * 0.001); //make it a tad bigger to avoid num precision erros
}
}
@@ -125,7 +125,7 @@ void CSGBrush::copy_from(const CSGBrush &p_brush, const Transform &p_xform) {
for (int i = 0; i < faces.size(); i++) {
for (int j = 0; j < 3; j++) {
- faces[i].vertices[j] = p_xform.xform(p_brush.faces[i].vertices[j]);
+ faces.write[i].vertices[j] = p_xform.xform(p_brush.faces[i].vertices[j]);
}
}
@@ -341,7 +341,7 @@ void CSGBrushOperation::BuildPoly::_clip_segment(const CSGBrush *p_brush, int p_
new_edge.points[0] = edges[i].points[0];
new_edge.points[1] = point_idx;
new_edge.outer = edges[i].outer;
- edges[i].points[0] = point_idx;
+ edges.write[i].points[0] = point_idx;
edges.insert(i, new_edge);
i++; //skip newly inserted edge
base_edges++; //will need an extra one in the base triangle
@@ -637,7 +637,7 @@ void CSGBrushOperation::_add_poly_points(const BuildPoly &p_poly, int p_edge, in
int to_point = e.edge_point;
int current_edge = e.edge;
- edge_process[e.edge] = true; //mark as processed
+ edge_process.write[e.edge] = true; //mark as processed
int limit = p_poly.points.size() * 4; //avoid infinite recursion
@@ -708,7 +708,7 @@ void CSGBrushOperation::_add_poly_points(const BuildPoly &p_poly, int p_edge, in
prev_point = to_point;
to_point = next_point;
- edge_process[next_edge] = true; //mark this edge as processed
+ edge_process.write[next_edge] = true; //mark this edge as processed
current_edge = next_edge;
limit--;
@@ -792,13 +792,13 @@ void CSGBrushOperation::_merge_poly(MeshMerge &mesh, int p_face_idx, const Build
//none processed by default
for (int i = 0; i < edge_process.size(); i++) {
- edge_process[i] = false;
+ edge_process.write[i] = false;
}
//put edges in points, so points can go through them
for (int i = 0; i < p_poly.edges.size(); i++) {
- vertex_process[p_poly.edges[i].points[0]].push_back(i);
- vertex_process[p_poly.edges[i].points[1]].push_back(i);
+ vertex_process.write[p_poly.edges[i].points[0]].push_back(i);
+ vertex_process.write[p_poly.edges[i].points[1]].push_back(i);
}
Vector<PolyPoints> polys;
@@ -854,7 +854,7 @@ void CSGBrushOperation::_merge_poly(MeshMerge &mesh, int p_face_idx, const Build
_add_poly_outline(p_poly, p_poly.edges[i].points[0], p_poly.edges[i].points[1], vertex_process, outline);
if (outline.size() > 1) {
- polys[intersect_poly].holes.push_back(outline);
+ polys.write[intersect_poly].holes.push_back(outline);
}
}
_add_poly_points(p_poly, i, p_poly.edges[i].points[0], p_poly.edges[i].points[1], vertex_process, edge_process, polys);
@@ -953,18 +953,18 @@ void CSGBrushOperation::_merge_poly(MeshMerge &mesh, int p_face_idx, const Build
//duplicate point
int insert_at = with_outline_vertex;
- polys[i].points.insert(insert_at, polys[i].points[insert_at]);
+ polys.write[i].points.insert(insert_at, polys[i].points[insert_at]);
insert_at++;
//insert all others, outline should be backwards (must check)
int holesize = polys[i].holes[j].size();
for (int k = 0; k <= holesize; k++) {
int idx = (from_hole_vertex + k) % holesize;
- polys[i].points.insert(insert_at, polys[i].holes[j][idx]);
+ polys.write[i].points.insert(insert_at, polys[i].holes[j][idx]);
insert_at++;
}
added_hole = true;
- polys[i].holes.remove(j);
+ polys.write[i].holes.remove(j);
break; //got rid of hole, break and continue
}
}
@@ -980,7 +980,7 @@ void CSGBrushOperation::_merge_poly(MeshMerge &mesh, int p_face_idx, const Build
Vector<Vector2> vertices;
vertices.resize(polys[i].points.size());
for (int j = 0; j < vertices.size(); j++) {
- vertices[j] = p_poly.points[polys[i].points[j]].point;
+ vertices.write[j] = p_poly.points[polys[i].points[j]].point;
}
Vector<int> indices = Geometry::triangulate_polygon(vertices);
@@ -1267,7 +1267,7 @@ void CSGBrushOperation::MeshMerge::mark_inside_faces() {
int intersections = _bvh_count_intersections(bvh, max_depth, max_alloc - 1, center, target, i);
if (intersections & 1) {
- faces[i].inside = true;
+ faces.write[i].inside = true;
}
}
}
@@ -1419,13 +1419,13 @@ void CSGBrushOperation::merge_brushes(Operation p_operation, const CSGBrush &p_A
if (mesh_merge.faces[i].inside)
continue;
for (int j = 0; j < 3; j++) {
- result.faces[outside_count].vertices[j] = mesh_merge.points[mesh_merge.faces[i].points[j]];
- result.faces[outside_count].uvs[j] = mesh_merge.faces[i].uvs[j];
+ result.faces.write[outside_count].vertices[j] = mesh_merge.points[mesh_merge.faces[i].points[j]];
+ result.faces.write[outside_count].uvs[j] = mesh_merge.faces[i].uvs[j];
}
- result.faces[outside_count].smooth = mesh_merge.faces[i].smooth;
- result.faces[outside_count].invert = mesh_merge.faces[i].invert;
- result.faces[outside_count].material = mesh_merge.faces[i].material_idx;
+ result.faces.write[outside_count].smooth = mesh_merge.faces[i].smooth;
+ result.faces.write[outside_count].invert = mesh_merge.faces[i].invert;
+ result.faces.write[outside_count].material = mesh_merge.faces[i].material_idx;
outside_count++;
}
@@ -1451,13 +1451,13 @@ void CSGBrushOperation::merge_brushes(Operation p_operation, const CSGBrush &p_A
if (!mesh_merge.faces[i].inside)
continue;
for (int j = 0; j < 3; j++) {
- result.faces[inside_count].vertices[j] = mesh_merge.points[mesh_merge.faces[i].points[j]];
- result.faces[inside_count].uvs[j] = mesh_merge.faces[i].uvs[j];
+ result.faces.write[inside_count].vertices[j] = mesh_merge.points[mesh_merge.faces[i].points[j]];
+ result.faces.write[inside_count].uvs[j] = mesh_merge.faces[i].uvs[j];
}
- result.faces[inside_count].smooth = mesh_merge.faces[i].smooth;
- result.faces[inside_count].invert = mesh_merge.faces[i].invert;
- result.faces[inside_count].material = mesh_merge.faces[i].material_idx;
+ result.faces.write[inside_count].smooth = mesh_merge.faces[i].smooth;
+ result.faces.write[inside_count].invert = mesh_merge.faces[i].invert;
+ result.faces.write[inside_count].material = mesh_merge.faces[i].material_idx;
inside_count++;
}
@@ -1489,19 +1489,19 @@ void CSGBrushOperation::merge_brushes(Operation p_operation, const CSGBrush &p_A
continue;
for (int j = 0; j < 3; j++) {
- result.faces[face_count].vertices[j] = mesh_merge.points[mesh_merge.faces[i].points[j]];
- result.faces[face_count].uvs[j] = mesh_merge.faces[i].uvs[j];
+ result.faces.write[face_count].vertices[j] = mesh_merge.points[mesh_merge.faces[i].points[j]];
+ result.faces.write[face_count].uvs[j] = mesh_merge.faces[i].uvs[j];
}
if (mesh_merge.faces[i].from_b) {
//invert facing of insides of B
- SWAP(result.faces[face_count].vertices[1], result.faces[face_count].vertices[2]);
- SWAP(result.faces[face_count].uvs[1], result.faces[face_count].uvs[2]);
+ SWAP(result.faces.write[face_count].vertices[1], result.faces.write[face_count].vertices[2]);
+ SWAP(result.faces.write[face_count].uvs[1], result.faces.write[face_count].uvs[2]);
}
- result.faces[face_count].smooth = mesh_merge.faces[i].smooth;
- result.faces[face_count].invert = mesh_merge.faces[i].invert;
- result.faces[face_count].material = mesh_merge.faces[i].material_idx;
+ result.faces.write[face_count].smooth = mesh_merge.faces[i].smooth;
+ result.faces.write[face_count].invert = mesh_merge.faces[i].invert;
+ result.faces.write[face_count].material = mesh_merge.faces[i].material_idx;
face_count++;
}
@@ -1513,6 +1513,6 @@ void CSGBrushOperation::merge_brushes(Operation p_operation, const CSGBrush &p_A
//updatelist of materials
result.materials.resize(mesh_merge.materials.size());
for (const Map<Ref<Material>, int>::Element *E = mesh_merge.materials.front(); E; E = E->next()) {
- result.materials[E->get()] = E->key();
+ result.materials.write[E->get()] = E->key();
}
}
diff --git a/modules/csg/csg_gizmos.cpp b/modules/csg/csg_gizmos.cpp
index 2150320c4a..f9744c72af 100644
--- a/modules/csg/csg_gizmos.cpp
+++ b/modules/csg/csg_gizmos.cpp
@@ -32,7 +32,16 @@
///////////
-String CSGShapeSpatialGizmo::get_handle_name(int p_idx) const {
+CSGShapeSpatialGizmoPlugin::CSGShapeSpatialGizmoPlugin() {
+
+ Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/csg", Color(0.2, 0.5, 1, 0.1));
+ create_material("shape_material", gizmo_color);
+ create_handle_material("handles");
+}
+
+String CSGShapeSpatialGizmoPlugin::get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
+
+ CSGShape *cs = Object::cast_to<CSGShape>(p_gizmo->get_spatial_node());
if (Object::cast_to<CSGSphere>(cs)) {
@@ -57,7 +66,9 @@ String CSGShapeSpatialGizmo::get_handle_name(int p_idx) const {
return "";
}
-Variant CSGShapeSpatialGizmo::get_handle_value(int p_idx) const {
+Variant CSGShapeSpatialGizmoPlugin::get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const {
+
+ CSGShape *cs = Object::cast_to<CSGShape>(p_gizmo->get_spatial_node());
if (Object::cast_to<CSGSphere>(cs)) {
@@ -89,10 +100,12 @@ Variant CSGShapeSpatialGizmo::get_handle_value(int p_idx) const {
return Variant();
}
-void CSGShapeSpatialGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_point) {
+void CSGShapeSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point) {
+
+ CSGShape *cs = Object::cast_to<CSGShape>(p_gizmo->get_spatial_node());
Transform gt = cs->get_global_transform();
- gt.orthonormalize();
+ //gt.orthonormalize();
Transform gi = gt.affine_inverse();
Vector3 ray_from = p_camera->project_ray_origin(p_point);
@@ -170,7 +183,9 @@ void CSGShapeSpatialGizmo::set_handle(int p_idx, Camera *p_camera, const Point2
s->set_outer_radius(d);
}
}
-void CSGShapeSpatialGizmo::commit_handle(int p_idx, const Variant &p_restore, bool p_cancel) {
+void CSGShapeSpatialGizmoPlugin::commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
+
+ CSGShape *cs = Object::cast_to<CSGShape>(p_gizmo->get_spatial_node());
if (Object::cast_to<CSGSphere>(cs)) {
CSGSphere *s = Object::cast_to<CSGSphere>(cs);
@@ -260,12 +275,26 @@ void CSGShapeSpatialGizmo::commit_handle(int p_idx, const Variant &p_restore, bo
ur->commit_action();
}
}
-void CSGShapeSpatialGizmo::redraw() {
+bool CSGShapeSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
+ return Object::cast_to<CSGSphere>(p_spatial) || Object::cast_to<CSGBox>(p_spatial) || Object::cast_to<CSGCylinder>(p_spatial) || Object::cast_to<CSGTorus>(p_spatial) || Object::cast_to<CSGMesh>(p_spatial) || Object::cast_to<CSGPolygon>(p_spatial);
+}
- clear();
+String CSGShapeSpatialGizmoPlugin::get_name() const {
+ return "CSGShapes";
+}
+
+bool CSGShapeSpatialGizmoPlugin::is_selectable_when_hidden() const {
+ return true;
+}
- Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/csg");
- Ref<Material> material = create_material("shape_material", gizmo_color);
+void CSGShapeSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
+
+ CSGShape *cs = Object::cast_to<CSGShape>(p_gizmo->get_spatial_node());
+
+ p_gizmo->clear();
+
+ Ref<Material> material = get_material("shape_material", p_gizmo);
+ Ref<Material> handles_material = get_material("handles");
PoolVector<Vector3> faces = cs->get_brush_faces();
@@ -278,14 +307,14 @@ void CSGShapeSpatialGizmo::redraw() {
int f = i / 6;
for (int j = 0; j < 3; j++) {
int j_n = (j + 1) % 3;
- lines[i + j * 2 + 0] = r[f * 3 + j];
- lines[i + j * 2 + 1] = r[f * 3 + j_n];
+ lines.write[i + j * 2 + 0] = r[f * 3 + j];
+ lines.write[i + j * 2 + 1] = r[f * 3 + j_n];
}
}
}
- add_lines(lines, material);
- add_collision_segments(lines);
+ p_gizmo->add_lines(lines, material);
+ p_gizmo->add_collision_segments(lines);
if (Object::cast_to<CSGSphere>(cs)) {
CSGSphere *s = Object::cast_to<CSGSphere>(cs);
@@ -293,7 +322,7 @@ void CSGShapeSpatialGizmo::redraw() {
float r = s->get_radius();
Vector<Vector3> handles;
handles.push_back(Vector3(r, 0, 0));
- add_handles(handles);
+ p_gizmo->add_handles(handles, handles_material);
}
if (Object::cast_to<CSGBox>(cs)) {
@@ -303,7 +332,7 @@ void CSGShapeSpatialGizmo::redraw() {
handles.push_back(Vector3(s->get_width(), 0, 0));
handles.push_back(Vector3(0, s->get_height(), 0));
handles.push_back(Vector3(0, 0, s->get_depth()));
- add_handles(handles);
+ p_gizmo->add_handles(handles, handles_material);
}
if (Object::cast_to<CSGCylinder>(cs)) {
@@ -312,7 +341,7 @@ void CSGShapeSpatialGizmo::redraw() {
Vector<Vector3> handles;
handles.push_back(Vector3(s->get_radius(), 0, 0));
handles.push_back(Vector3(0, s->get_height() * 0.5, 0));
- add_handles(handles);
+ p_gizmo->add_handles(handles, handles_material);
}
if (Object::cast_to<CSGTorus>(cs)) {
@@ -321,25 +350,11 @@ void CSGShapeSpatialGizmo::redraw() {
Vector<Vector3> handles;
handles.push_back(Vector3(s->get_inner_radius(), 0, 0));
handles.push_back(Vector3(s->get_outer_radius(), 0, 0));
- add_handles(handles);
- }
-}
-CSGShapeSpatialGizmo::CSGShapeSpatialGizmo(CSGShape *p_cs) {
-
- cs = p_cs;
- set_spatial_node(p_cs);
-}
-
-Ref<SpatialEditorGizmo> EditorPluginCSG::create_spatial_gizmo(Spatial *p_spatial) {
- if (Object::cast_to<CSGSphere>(p_spatial) || Object::cast_to<CSGBox>(p_spatial) || Object::cast_to<CSGCylinder>(p_spatial) || Object::cast_to<CSGTorus>(p_spatial) || Object::cast_to<CSGMesh>(p_spatial) || Object::cast_to<CSGPolygon>(p_spatial)) {
- Ref<CSGShapeSpatialGizmo> csg = memnew(CSGShapeSpatialGizmo(Object::cast_to<CSGShape>(p_spatial)));
- return csg;
+ p_gizmo->add_handles(handles, handles_material);
}
-
- return Ref<SpatialEditorGizmo>();
}
EditorPluginCSG::EditorPluginCSG(EditorNode *p_editor) {
-
- EDITOR_DEF("editors/3d_gizmos/gizmo_colors/csg", Color(0.2, 0.5, 1, 0.1));
+ Ref<CSGShapeSpatialGizmoPlugin> gizmo_plugin = Ref<CSGShapeSpatialGizmoPlugin>(memnew(CSGShapeSpatialGizmoPlugin));
+ SpatialEditor::get_singleton()->register_gizmo_plugin(gizmo_plugin);
}
diff --git a/modules/csg/csg_gizmos.h b/modules/csg/csg_gizmos.h
index 68e916823b..d65d1f58c1 100644
--- a/modules/csg/csg_gizmos.h
+++ b/modules/csg/csg_gizmos.h
@@ -35,25 +35,27 @@
#include "editor/editor_plugin.h"
#include "editor/spatial_editor_gizmos.h"
-class CSGShapeSpatialGizmo : public EditorSpatialGizmo {
+class CSGShapeSpatialGizmoPlugin : public EditorSpatialGizmoPlugin {
- GDCLASS(CSGShapeSpatialGizmo, EditorSpatialGizmo);
-
- CSGShape *cs;
+ GDCLASS(CSGShapeSpatialGizmoPlugin, EditorSpatialGizmoPlugin);
public:
- virtual String get_handle_name(int p_idx) const;
- virtual Variant get_handle_value(int p_idx) const;
- virtual void set_handle(int p_idx, Camera *p_camera, const Point2 &p_point);
- virtual void commit_handle(int p_idx, const Variant &p_restore, bool p_cancel = false);
- void redraw();
- CSGShapeSpatialGizmo(CSGShape *p_cs = NULL);
+ bool has_gizmo(Spatial *p_spatial);
+ String get_name() const;
+ bool is_selectable_when_hidden() const;
+ void redraw(EditorSpatialGizmo *p_gizmo);
+
+ String get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const;
+ Variant get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const;
+ void set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point);
+ void commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel);
+
+ CSGShapeSpatialGizmoPlugin();
};
class EditorPluginCSG : public EditorPlugin {
GDCLASS(EditorPluginCSG, EditorPlugin)
public:
- virtual Ref<SpatialEditorGizmo> create_spatial_gizmo(Spatial *p_spatial);
EditorPluginCSG(EditorNode *p_editor);
};
diff --git a/modules/csg/csg_shape.cpp b/modules/csg/csg_shape.cpp
index 67cc7e1ba2..9f2171a82a 100644
--- a/modules/csg/csg_shape.cpp
+++ b/modules/csg/csg_shape.cpp
@@ -177,7 +177,7 @@ void CSGShape::_update_shape() {
Vector<int> face_count;
face_count.resize(n->materials.size() + 1);
for (int i = 0; i < face_count.size(); i++) {
- face_count[i] = 0;
+ face_count.write[i] = 0;
}
for (int i = 0; i < n->faces.size(); i++) {
@@ -200,7 +200,7 @@ void CSGShape::_update_shape() {
}
}
- face_count[idx]++;
+ face_count.write[idx]++;
}
Vector<ShapeUpdateSurface> surfaces;
@@ -210,18 +210,18 @@ void CSGShape::_update_shape() {
//create arrays
for (int i = 0; i < surfaces.size(); i++) {
- surfaces[i].vertices.resize(face_count[i] * 3);
- surfaces[i].normals.resize(face_count[i] * 3);
- surfaces[i].uvs.resize(face_count[i] * 3);
- surfaces[i].last_added = 0;
+ surfaces.write[i].vertices.resize(face_count[i] * 3);
+ surfaces.write[i].normals.resize(face_count[i] * 3);
+ surfaces.write[i].uvs.resize(face_count[i] * 3);
+ surfaces.write[i].last_added = 0;
if (i != surfaces.size() - 1) {
- surfaces[i].material = n->materials[i];
+ surfaces.write[i].material = n->materials[i];
}
- surfaces[i].verticesw = surfaces[i].vertices.write();
- surfaces[i].normalsw = surfaces[i].normals.write();
- surfaces[i].uvsw = surfaces[i].uvs.write();
+ surfaces.write[i].verticesw = surfaces.write[i].vertices.write();
+ surfaces.write[i].normalsw = surfaces.write[i].normals.write();
+ surfaces.write[i].uvsw = surfaces.write[i].uvs.write();
}
//fill arrays
@@ -281,7 +281,7 @@ void CSGShape::_update_shape() {
surfaces[idx].normalsw[last + order[j]] = normal;
}
- surfaces[idx].last_added += 3;
+ surfaces.write[idx].last_added += 3;
}
}
@@ -290,9 +290,9 @@ void CSGShape::_update_shape() {
for (int i = 0; i < surfaces.size(); i++) {
- surfaces[i].verticesw = PoolVector<Vector3>::Write();
- surfaces[i].normalsw = PoolVector<Vector3>::Write();
- surfaces[i].uvsw = PoolVector<Vector2>::Write();
+ surfaces.write[i].verticesw = PoolVector<Vector3>::Write();
+ surfaces.write[i].normalsw = PoolVector<Vector3>::Write();
+ surfaces.write[i].uvsw = PoolVector<Vector2>::Write();
if (surfaces[i].last_added == 0)
continue;