Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/3d/multimesh_editor_plugin.cpp
9903 views
1
/**************************************************************************/
2
/* multimesh_editor_plugin.cpp */
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
#include "multimesh_editor_plugin.h"
32
33
#include "editor/editor_node.h"
34
#include "editor/editor_string_names.h"
35
#include "editor/scene/3d/node_3d_editor_plugin.h"
36
#include "editor/scene/scene_tree_editor.h"
37
#include "scene/3d/mesh_instance_3d.h"
38
#include "scene/gui/box_container.h"
39
#include "scene/gui/menu_button.h"
40
#include "scene/gui/option_button.h"
41
42
void MultiMeshEditor::_node_removed(Node *p_node) {
43
if (p_node == node) {
44
node = nullptr;
45
hide();
46
}
47
}
48
49
void MultiMeshEditor::_populate() {
50
if (!node) {
51
return;
52
}
53
54
Ref<Mesh> mesh;
55
56
if (mesh_source->get_text().is_empty()) {
57
Ref<MultiMesh> multimesh;
58
multimesh = node->get_multimesh();
59
if (multimesh.is_null()) {
60
err_dialog->set_text(TTR("No mesh source specified (and no MultiMesh set in node)."));
61
err_dialog->popup_centered();
62
return;
63
}
64
if (multimesh->get_mesh().is_null()) {
65
err_dialog->set_text(TTR("No mesh source specified (and MultiMesh contains no Mesh)."));
66
err_dialog->popup_centered();
67
return;
68
}
69
70
mesh = multimesh->get_mesh();
71
} else {
72
Node *ms_node = node->get_node(mesh_source->get_text());
73
74
if (!ms_node) {
75
err_dialog->set_text(TTR("Mesh source is invalid (invalid path)."));
76
err_dialog->popup_centered();
77
return;
78
}
79
80
MeshInstance3D *ms_instance = Object::cast_to<MeshInstance3D>(ms_node);
81
82
if (!ms_instance) {
83
err_dialog->set_text(TTR("Mesh source is invalid (not a MeshInstance3D)."));
84
err_dialog->popup_centered();
85
return;
86
}
87
88
mesh = ms_instance->get_mesh();
89
90
if (mesh.is_null()) {
91
err_dialog->set_text(TTR("Mesh source is invalid (contains no Mesh resource)."));
92
err_dialog->popup_centered();
93
return;
94
}
95
}
96
97
if (surface_source->get_text().is_empty()) {
98
err_dialog->set_text(TTR("No surface source specified."));
99
err_dialog->popup_centered();
100
return;
101
}
102
103
Node *ss_node = node->get_node(surface_source->get_text());
104
105
if (!ss_node) {
106
err_dialog->set_text(TTR("Surface source is invalid (invalid path)."));
107
err_dialog->popup_centered();
108
return;
109
}
110
111
MeshInstance3D *ss_instance = Object::cast_to<MeshInstance3D>(ss_node);
112
113
if (!ss_instance || ss_instance->get_mesh().is_null()) {
114
err_dialog->set_text(TTR("Surface source is invalid (no geometry)."));
115
err_dialog->popup_centered();
116
return;
117
}
118
119
Transform3D geom_xform = node->get_global_transform().affine_inverse() * ss_instance->get_global_transform();
120
121
Vector<Face3> geometry = ss_instance->get_mesh()->get_faces();
122
123
if (geometry.is_empty()) {
124
err_dialog->set_text(TTR("Surface source is invalid (no faces)."));
125
err_dialog->popup_centered();
126
return;
127
}
128
129
//make all faces local
130
131
int gc = geometry.size();
132
Face3 *w = geometry.ptrw();
133
134
for (int i = 0; i < gc; i++) {
135
for (int j = 0; j < 3; j++) {
136
w[i].vertex[j] = geom_xform.xform(w[i].vertex[j]);
137
}
138
}
139
140
Vector<Face3> faces = geometry;
141
int facecount = faces.size();
142
ERR_FAIL_COND_MSG(!facecount, "Parent has no solid faces to populate.");
143
144
const Face3 *r = faces.ptr();
145
146
float area_accum = 0;
147
RBMap<float, int> triangle_area_map;
148
for (int i = 0; i < facecount; i++) {
149
float area = r[i].get_area();
150
if (area < CMP_EPSILON) {
151
continue;
152
}
153
triangle_area_map[area_accum] = i;
154
area_accum += area;
155
}
156
157
ERR_FAIL_COND_MSG(triangle_area_map.is_empty(), "Couldn't map area.");
158
ERR_FAIL_COND_MSG(area_accum == 0, "Couldn't map area.");
159
160
Ref<MultiMesh> multimesh = memnew(MultiMesh);
161
multimesh->set_mesh(mesh);
162
163
int instance_count = populate_amount->get_value();
164
165
multimesh->set_transform_format(MultiMesh::TRANSFORM_3D);
166
multimesh->set_use_colors(false);
167
multimesh->set_instance_count(instance_count);
168
169
float _tilt_random = populate_tilt_random->get_value();
170
float _rotate_random = populate_rotate_random->get_value();
171
float _scale_random = populate_scale_random->get_value();
172
float _scale = populate_scale->get_value();
173
int axis = populate_axis->get_selected();
174
175
Transform3D axis_xform;
176
if (axis == Vector3::AXIS_Z) {
177
axis_xform.rotate(Vector3(1, 0, 0), -Math::PI * 0.5);
178
}
179
if (axis == Vector3::AXIS_X) {
180
axis_xform.rotate(Vector3(0, 0, 1), -Math::PI * 0.5);
181
}
182
183
for (int i = 0; i < instance_count; i++) {
184
float areapos = Math::random(0.0f, area_accum);
185
186
RBMap<float, int>::Iterator E = triangle_area_map.find_closest(areapos);
187
ERR_FAIL_COND(!E);
188
int index = E->value;
189
ERR_FAIL_INDEX(index, facecount);
190
191
// ok FINALLY get face
192
Face3 face = r[index];
193
//now compute some position inside the face...
194
195
Vector3 pos = face.get_random_point_inside();
196
Vector3 normal = face.get_plane().normal;
197
Vector3 op_axis = (face.vertex[0] - face.vertex[1]).normalized();
198
199
Transform3D xform;
200
201
xform.set_look_at(pos, pos + op_axis, normal);
202
xform = xform * axis_xform;
203
204
Basis post_xform;
205
206
post_xform.rotate(xform.basis.get_column(1), -Math::random(-_rotate_random, _rotate_random) * Math::PI);
207
post_xform.rotate(xform.basis.get_column(2), -Math::random(-_tilt_random, _tilt_random) * Math::PI);
208
post_xform.rotate(xform.basis.get_column(0), -Math::random(-_tilt_random, _tilt_random) * Math::PI);
209
210
xform.basis = post_xform * xform.basis;
211
//xform.basis.orthonormalize();
212
213
xform.basis.scale(Vector3(1, 1, 1) * (_scale + Math::random(-_scale_random, _scale_random)));
214
215
multimesh->set_instance_transform(i, xform);
216
}
217
218
node->set_multimesh(multimesh);
219
}
220
221
void MultiMeshEditor::_browsed(const NodePath &p_path) {
222
NodePath path = node->get_path_to(get_node(p_path));
223
224
if (browsing_source) {
225
mesh_source->set_text(String(path));
226
} else {
227
surface_source->set_text(String(path));
228
}
229
}
230
231
void MultiMeshEditor::_menu_option(int p_option) {
232
switch (p_option) {
233
case MENU_OPTION_POPULATE: {
234
if (_last_pp_node != node) {
235
surface_source->set_text("..");
236
mesh_source->set_text("..");
237
populate_axis->select(1);
238
populate_rotate_random->set_value(0);
239
populate_tilt_random->set_value(0);
240
populate_scale_random->set_value(0);
241
populate_scale->set_value(1);
242
populate_amount->set_value(128);
243
244
_last_pp_node = node;
245
}
246
populate_dialog->popup_centered(Size2(250, 380));
247
248
} break;
249
}
250
}
251
252
void MultiMeshEditor::edit(MultiMeshInstance3D *p_multimesh) {
253
node = p_multimesh;
254
}
255
256
void MultiMeshEditor::_browse(bool p_source) {
257
browsing_source = p_source;
258
Node *browsed_node = nullptr;
259
if (p_source) {
260
browsed_node = node->get_node_or_null(mesh_source->get_text());
261
std->set_title(TTR("Select a Source Mesh:"));
262
} else {
263
browsed_node = node->get_node_or_null(surface_source->get_text());
264
std->set_title(TTR("Select a Target Surface:"));
265
}
266
std->popup_scenetree_dialog(browsed_node);
267
}
268
269
MultiMeshEditor::MultiMeshEditor() {
270
options = memnew(MenuButton);
271
options->set_switch_on_hover(true);
272
Node3DEditor::get_singleton()->add_control_to_menu_panel(options);
273
274
options->set_text("MultiMesh");
275
options->set_button_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("MultiMeshInstance3D"), EditorStringName(EditorIcons)));
276
options->set_flat(false);
277
options->set_theme_type_variation("FlatMenuButton");
278
279
options->get_popup()->add_item(TTR("Populate Surface"));
280
options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &MultiMeshEditor::_menu_option));
281
282
populate_dialog = memnew(ConfirmationDialog);
283
populate_dialog->set_title(TTR("Populate MultiMesh"));
284
add_child(populate_dialog);
285
286
VBoxContainer *vbc = memnew(VBoxContainer);
287
populate_dialog->add_child(vbc);
288
//populate_dialog->set_child_rect(vbc);
289
290
HBoxContainer *hbc = memnew(HBoxContainer);
291
292
surface_source = memnew(LineEdit);
293
hbc->add_child(surface_source);
294
surface_source->set_h_size_flags(SIZE_EXPAND_FILL);
295
surface_source->set_accessibility_name(TTRC("Target Surface:"));
296
Button *b = memnew(Button);
297
hbc->add_child(b);
298
b->set_accessibility_name(TTRC("Browse"));
299
b->set_text("..");
300
b->connect(SceneStringName(pressed), callable_mp(this, &MultiMeshEditor::_browse).bind(false));
301
302
vbc->add_margin_child(TTR("Target Surface:"), hbc);
303
304
hbc = memnew(HBoxContainer);
305
mesh_source = memnew(LineEdit);
306
hbc->add_child(mesh_source);
307
mesh_source->set_h_size_flags(SIZE_EXPAND_FILL);
308
mesh_source->set_accessibility_name(TTRC("Source Mesh:"));
309
b = memnew(Button);
310
hbc->add_child(b);
311
b->set_accessibility_name(TTRC("Browse"));
312
b->set_text("..");
313
vbc->add_margin_child(TTR("Source Mesh:"), hbc);
314
b->connect(SceneStringName(pressed), callable_mp(this, &MultiMeshEditor::_browse).bind(true));
315
316
populate_axis = memnew(OptionButton);
317
populate_axis->set_accessibility_name(TTRC("Mesh Up Axis:"));
318
populate_axis->add_item(TTR("X-Axis"));
319
populate_axis->add_item(TTR("Y-Axis"));
320
populate_axis->add_item(TTR("Z-Axis"));
321
populate_axis->select(2);
322
vbc->add_margin_child(TTR("Mesh Up Axis:"), populate_axis);
323
324
populate_rotate_random = memnew(HSlider);
325
populate_rotate_random->set_max(1);
326
populate_rotate_random->set_step(0.01);
327
populate_rotate_random->set_accessibility_name(TTRC("Random Rotation:"));
328
vbc->add_margin_child(TTR("Random Rotation:"), populate_rotate_random);
329
330
populate_tilt_random = memnew(HSlider);
331
populate_tilt_random->set_max(1);
332
populate_tilt_random->set_step(0.01);
333
populate_tilt_random->set_accessibility_name(TTRC("Random Tilt:"));
334
vbc->add_margin_child(TTR("Random Tilt:"), populate_tilt_random);
335
336
populate_scale_random = memnew(SpinBox);
337
populate_scale_random->set_min(0);
338
populate_scale_random->set_max(1);
339
populate_scale_random->set_value(0);
340
populate_scale_random->set_step(0.01);
341
populate_scale_random->set_accessibility_name(TTRC("Random Scale:"));
342
vbc->add_margin_child(TTR("Random Scale:"), populate_scale_random);
343
344
populate_scale = memnew(SpinBox);
345
populate_scale->set_min(0.001);
346
populate_scale->set_max(4096);
347
populate_scale->set_value(1);
348
populate_scale->set_step(0.01);
349
populate_scale->set_accessibility_name(TTRC("Scale:"));
350
vbc->add_margin_child(TTR("Scale:"), populate_scale);
351
352
populate_amount = memnew(SpinBox);
353
populate_amount->set_anchor(SIDE_RIGHT, ANCHOR_END);
354
populate_amount->set_begin(Point2(20, 232));
355
populate_amount->set_end(Point2(-5, 237));
356
populate_amount->set_min(1);
357
populate_amount->set_max(65536);
358
populate_amount->set_value(128);
359
populate_amount->set_accessibility_name(TTRC("Amount:"));
360
vbc->add_margin_child(TTR("Amount:"), populate_amount);
361
362
populate_dialog->set_ok_button_text(TTR("Populate"));
363
364
populate_dialog->get_ok_button()->connect(SceneStringName(pressed), callable_mp(this, &MultiMeshEditor::_populate));
365
std = memnew(SceneTreeDialog);
366
Vector<StringName> valid_types;
367
valid_types.push_back("MeshInstance3D");
368
std->set_valid_types(valid_types);
369
populate_dialog->add_child(std);
370
std->connect("selected", callable_mp(this, &MultiMeshEditor::_browsed));
371
372
_last_pp_node = nullptr;
373
374
err_dialog = memnew(AcceptDialog);
375
add_child(err_dialog);
376
}
377
378
void MultiMeshEditorPlugin::edit(Object *p_object) {
379
multimesh_editor->edit(Object::cast_to<MultiMeshInstance3D>(p_object));
380
}
381
382
bool MultiMeshEditorPlugin::handles(Object *p_object) const {
383
return p_object->is_class("MultiMeshInstance3D");
384
}
385
386
void MultiMeshEditorPlugin::make_visible(bool p_visible) {
387
if (p_visible) {
388
multimesh_editor->options->show();
389
} else {
390
multimesh_editor->options->hide();
391
multimesh_editor->edit(nullptr);
392
}
393
}
394
395
MultiMeshEditorPlugin::MultiMeshEditorPlugin() {
396
multimesh_editor = memnew(MultiMeshEditor);
397
EditorNode::get_singleton()->get_gui_base()->add_child(multimesh_editor);
398
399
multimesh_editor->options->hide();
400
}
401
402