Enable material editor preview to be rotated.

(cherry picked from commit d4ee903004637b6ba12eef004a4d9006462c83b6)
This commit is contained in:
Anilforextra 2022-09-07 11:02:57 +05:45 committed by Relintai
parent 9225e78f68
commit 522c324760
2 changed files with 38 additions and 9 deletions

View File

@ -65,6 +65,19 @@
class EditorNode;
void MaterialEditor::_gui_input(const Ref<InputEvent> &p_event) {
ERR_FAIL_COND(p_event.is_null());
Ref<InputEventMouseMotion> mm = p_event;
if (mm.is_valid() && (mm->get_button_mask() & BUTTON_MASK_LEFT)) {
rot.x -= mm->get_relative().y * 0.01;
rot.y -= mm->get_relative().x * 0.01;
rot.x = CLAMP(rot.x, -Math_PI / 2, Math_PI / 2);
_update_rotation();
}
}
void MaterialEditor::_notification(int p_what) {
if (p_what == NOTIFICATION_READY) {
//get_scene()->connect("node_removed",this,"_node_removed");
@ -94,6 +107,13 @@ void MaterialEditor::_notification(int p_what) {
}
}
void MaterialEditor::_update_rotation() {
Transform t;
t.basis.rotate(Vector3(0, 1, 0), -rot.y);
t.basis.rotate(Vector3(1, 0, 0), -rot.x);
rotation->set_transform(t);
}
void MaterialEditor::edit(Ref<Material> p_material, const Ref<Environment3D> &p_env) {
material = p_material;
camera->set_environment(p_env);
@ -103,6 +123,10 @@ void MaterialEditor::edit(Ref<Material> p_material, const Ref<Environment3D> &p_
} else {
hide();
}
rot.x = Math::deg2rad(-15.0);
rot.y = Math::deg2rad(30.0);
_update_rotation();
}
void MaterialEditor::_button_pressed(Node *p_button) {
@ -132,6 +156,7 @@ void MaterialEditor::_button_pressed(Node *p_button) {
}
void MaterialEditor::_bind_methods() {
ClassDB::bind_method(D_METHOD("_gui_input"), &MaterialEditor::_gui_input);
ClassDB::bind_method(D_METHOD("_button_pressed"), &MaterialEditor::_button_pressed);
}
@ -150,7 +175,7 @@ MaterialEditor::MaterialEditor() {
viewport->set_msaa(Viewport::MSAA_4X);
camera = memnew(Camera);
camera->set_transform(Transform(Basis(), Vector3(0, 0, 3)));
camera->set_transform(Transform(Basis(), Vector3(0, 0, 1.1)));
camera->set_perspective(45, 0.1, 10);
camera->make_current();
viewport->add_child(camera);
@ -164,18 +189,17 @@ MaterialEditor::MaterialEditor() {
light2->set_color(Color(0.7, 0.7, 0.7));
viewport->add_child(light2);
rotation = memnew(Spatial);
viewport->add_child(rotation);
sphere_instance = memnew(MeshInstance);
viewport->add_child(sphere_instance);
rotation->add_child(sphere_instance);
box_instance = memnew(MeshInstance);
viewport->add_child(box_instance);
rotation->add_child(box_instance);
Transform box_xform;
box_xform.basis.rotate(Vector3(1, 0, 0), Math::deg2rad(25.0));
box_xform.basis = box_xform.basis * Basis().rotated(Vector3(0, 1, 0), Math::deg2rad(-25.0));
box_xform.basis.scale(Vector3(0.8, 0.8, 0.8));
box_xform.origin.y = 0.2;
box_instance->set_transform(box_xform);
box_instance->set_transform(Transform(Basis() * 0.25, Vector3() * 0.25));
sphere_instance->set_transform(Transform(Basis() * 0.375, Vector3() * 0.375));
sphere_mesh.instance();
sphere_instance->set_mesh(sphere_mesh);

View File

@ -57,8 +57,11 @@ class Viewport;
class MaterialEditor : public Control {
GDCLASS(MaterialEditor, Control);
Vector2 rot = Vector2();
ViewportContainer *vc;
Viewport *viewport;
Spatial *rotation;
MeshInstance *sphere_instance;
MeshInstance *box_instance;
DirectionalLight *light1;
@ -81,6 +84,8 @@ class MaterialEditor : public Control {
protected:
void _notification(int p_what);
void _gui_input(const Ref<InputEvent> &p_event);
void _update_rotation();
static void _bind_methods();