2016-05-23 20:10:26 +00:00
|
|
|
#include "material_editor_plugin.h"
|
|
|
|
#include "scene/main/viewport.h"
|
|
|
|
|
|
|
|
void MaterialEditor::_input_event(InputEvent p_event) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaterialEditor::_notification(int p_what) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_what == NOTIFICATION_FIXED_PROCESS) {
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_what == NOTIFICATION_READY) {
|
2016-05-23 20:10:26 +00:00
|
|
|
|
|
|
|
//get_scene()->connect("node_removed",this,"_node_removed");
|
|
|
|
|
|
|
|
if (first_enter) {
|
|
|
|
//it's in propertyeditor so.. could be moved around
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
light_1_switch->set_normal_texture(get_icon("MaterialPreviewLight1", "EditorIcons"));
|
|
|
|
light_1_switch->set_pressed_texture(get_icon("MaterialPreviewLight1Off", "EditorIcons"));
|
|
|
|
light_2_switch->set_normal_texture(get_icon("MaterialPreviewLight2", "EditorIcons"));
|
|
|
|
light_2_switch->set_pressed_texture(get_icon("MaterialPreviewLight2Off", "EditorIcons"));
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
sphere_switch->set_normal_texture(get_icon("MaterialPreviewSphereOff", "EditorIcons"));
|
|
|
|
sphere_switch->set_pressed_texture(get_icon("MaterialPreviewSphere", "EditorIcons"));
|
|
|
|
box_switch->set_normal_texture(get_icon("MaterialPreviewCubeOff", "EditorIcons"));
|
|
|
|
box_switch->set_pressed_texture(get_icon("MaterialPreviewCube", "EditorIcons"));
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
first_enter = false;
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_what == NOTIFICATION_DRAW) {
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<Texture> checkerboard = get_icon("Checkerboard", "EditorIcons");
|
2016-05-23 20:10:26 +00:00
|
|
|
Size2 size = get_size();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaterialEditor::edit(Ref<Material> p_material) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
material = p_material;
|
2016-05-23 20:10:26 +00:00
|
|
|
|
|
|
|
if (!material.is_null()) {
|
2017-03-18 23:36:26 +00:00
|
|
|
sphere_mesh->surface_set_material(0, material);
|
|
|
|
box_mesh->surface_set_material(0, material);
|
2016-05-23 20:10:26 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void MaterialEditor::_button_pressed(Node *p_button) {
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_button == light_1_switch) {
|
2016-05-23 20:10:26 +00:00
|
|
|
light1->set_enabled(!light_1_switch->is_pressed());
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_button == light_2_switch) {
|
2016-05-23 20:10:26 +00:00
|
|
|
light2->set_enabled(!light_2_switch->is_pressed());
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_button == box_switch) {
|
2016-05-23 20:10:26 +00:00
|
|
|
box_instance->show();
|
|
|
|
sphere_instance->hide();
|
|
|
|
box_switch->set_pressed(true);
|
|
|
|
sphere_switch->set_pressed(false);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_button == sphere_switch) {
|
2016-05-23 20:10:26 +00:00
|
|
|
box_instance->hide();
|
|
|
|
sphere_instance->show();
|
|
|
|
box_switch->set_pressed(false);
|
|
|
|
sphere_switch->set_pressed(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaterialEditor::_bind_methods() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("_input_event"), &MaterialEditor::_input_event);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_button_pressed"), &MaterialEditor::_button_pressed);
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MaterialEditor::MaterialEditor() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
viewport = memnew(Viewport);
|
2016-05-23 20:10:26 +00:00
|
|
|
Ref<World> world;
|
|
|
|
world.instance();
|
|
|
|
viewport->set_world(world); //use own world
|
|
|
|
add_child(viewport);
|
2016-08-16 15:10:44 +00:00
|
|
|
viewport->set_disable_input(true);
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
camera = memnew(Camera);
|
|
|
|
camera->set_transform(Transform(Matrix3(), Vector3(0, 0, 3)));
|
|
|
|
camera->set_perspective(45, 0.1, 10);
|
2016-05-23 20:10:26 +00:00
|
|
|
viewport->add_child(camera);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
light1 = memnew(DirectionalLight);
|
|
|
|
light1->set_transform(Transform().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
|
2016-05-23 20:10:26 +00:00
|
|
|
viewport->add_child(light1);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
light2 = memnew(DirectionalLight);
|
|
|
|
light2->set_transform(Transform().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
|
|
|
|
light2->set_color(Light::COLOR_DIFFUSE, Color(0.7, 0.7, 0.7));
|
|
|
|
light2->set_color(Light::COLOR_SPECULAR, Color(0.7, 0.7, 0.7));
|
2016-05-23 20:10:26 +00:00
|
|
|
viewport->add_child(light2);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
sphere_instance = memnew(MeshInstance);
|
2016-05-23 20:10:26 +00:00
|
|
|
viewport->add_child(sphere_instance);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
box_instance = memnew(MeshInstance);
|
2016-05-23 20:10:26 +00:00
|
|
|
viewport->add_child(box_instance);
|
|
|
|
|
|
|
|
Transform box_xform;
|
2017-03-18 23:36:26 +00:00
|
|
|
box_xform.basis.rotate(Vector3(1, 0, 0), Math::deg2rad(-25));
|
|
|
|
box_xform.basis = box_xform.basis * Matrix3().rotated(Vector3(0, 1, 0), Math::deg2rad(-25));
|
|
|
|
box_xform.basis.scale(Vector3(0.8, 0.8, 0.8));
|
2016-05-23 20:10:26 +00:00
|
|
|
box_instance->set_transform(box_xform);
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
sphere_mesh.instance();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int lats = 32;
|
|
|
|
int lons = 32;
|
|
|
|
float radius = 1.0;
|
2016-05-23 20:10:26 +00:00
|
|
|
|
|
|
|
DVector<Vector3> vertices;
|
|
|
|
DVector<Vector3> normals;
|
|
|
|
DVector<Vector2> uvs;
|
|
|
|
DVector<float> tangents;
|
2017-03-18 23:36:26 +00:00
|
|
|
Matrix3 tt = Matrix3(Vector3(0, 1, 0), Math_PI * 0.5);
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 1; i <= lats; i++) {
|
|
|
|
double lat0 = Math_PI * (-0.5 + (double)(i - 1) / lats);
|
|
|
|
double z0 = Math::sin(lat0);
|
|
|
|
double zr0 = Math::cos(lat0);
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
double lat1 = Math_PI * (-0.5 + (double)i / lats);
|
2016-05-23 20:10:26 +00:00
|
|
|
double z1 = Math::sin(lat1);
|
|
|
|
double zr1 = Math::cos(lat1);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = lons; j >= 1; j--) {
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
double lng0 = 2 * Math_PI * (double)(j - 1) / lons;
|
2016-05-23 20:10:26 +00:00
|
|
|
double x0 = Math::cos(lng0);
|
|
|
|
double y0 = Math::sin(lng0);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
double lng1 = 2 * Math_PI * (double)(j) / lons;
|
2016-05-23 20:10:26 +00:00
|
|
|
double x1 = Math::cos(lng1);
|
|
|
|
double y1 = Math::sin(lng1);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector3 v[4] = {
|
|
|
|
Vector3(x1 * zr0, z0, y1 * zr0),
|
|
|
|
Vector3(x1 * zr1, z1, y1 * zr1),
|
|
|
|
Vector3(x0 * zr1, z1, y0 * zr1),
|
|
|
|
Vector3(x0 * zr0, z0, y0 * zr0)
|
2016-05-23 20:10:26 +00:00
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
#define ADD_POINT(m_idx) \
|
|
|
|
normals.push_back(v[m_idx]); \
|
|
|
|
vertices.push_back(v[m_idx] * radius); \
|
|
|
|
{ \
|
|
|
|
Vector2 uv(Math::atan2(v[m_idx].x, v[m_idx].z), Math::atan2(-v[m_idx].y, v[m_idx].z)); \
|
|
|
|
uv /= Math_PI; \
|
|
|
|
uv *= 4.0; \
|
|
|
|
uv = uv * 0.5 + Vector2(0.5, 0.5); \
|
|
|
|
uvs.push_back(uv); \
|
|
|
|
} \
|
|
|
|
{ \
|
|
|
|
Vector3 t = tt.xform(v[m_idx]); \
|
|
|
|
tangents.push_back(t.x); \
|
|
|
|
tangents.push_back(t.y); \
|
|
|
|
tangents.push_back(t.z); \
|
|
|
|
tangents.push_back(1.0); \
|
|
|
|
}
|
2016-05-23 20:10:26 +00:00
|
|
|
|
|
|
|
ADD_POINT(0);
|
|
|
|
ADD_POINT(1);
|
|
|
|
ADD_POINT(2);
|
|
|
|
|
|
|
|
ADD_POINT(2);
|
|
|
|
ADD_POINT(3);
|
|
|
|
ADD_POINT(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Array arr;
|
|
|
|
arr.resize(VS::ARRAY_MAX);
|
2017-03-18 23:36:26 +00:00
|
|
|
arr[VS::ARRAY_VERTEX] = vertices;
|
|
|
|
arr[VS::ARRAY_NORMAL] = normals;
|
|
|
|
arr[VS::ARRAY_TANGENT] = tangents;
|
|
|
|
arr[VS::ARRAY_TEX_UV] = uvs;
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
sphere_mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, arr);
|
2016-05-23 20:10:26 +00:00
|
|
|
|
|
|
|
sphere_instance->set_mesh(sphere_mesh);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
|
|
|
|
box_mesh.instance();
|
|
|
|
|
|
|
|
DVector<Vector3> vertices;
|
|
|
|
DVector<Vector3> normals;
|
|
|
|
DVector<float> tangents;
|
|
|
|
DVector<Vector3> uvs;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int vtx_idx = 0;
|
|
|
|
#define ADD_VTX(m_idx) \
|
|
|
|
; \
|
|
|
|
vertices.push_back(face_points[m_idx]); \
|
|
|
|
normals.push_back(normal_points[m_idx]); \
|
|
|
|
tangents.push_back(normal_points[m_idx][1]); \
|
|
|
|
tangents.push_back(normal_points[m_idx][2]); \
|
|
|
|
tangents.push_back(normal_points[m_idx][0]); \
|
|
|
|
tangents.push_back(1.0); \
|
|
|
|
uvs.push_back(Vector3(uv_points[m_idx * 2 + 0], uv_points[m_idx * 2 + 1], 0)); \
|
|
|
|
vtx_idx++;
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < 6; i++) {
|
2016-05-23 20:10:26 +00:00
|
|
|
|
|
|
|
Vector3 face_points[4];
|
|
|
|
Vector3 normal_points[4];
|
2017-03-18 23:36:26 +00:00
|
|
|
float uv_points[8] = { 0, 0, 0, 1, 1, 1, 1, 0 };
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = 0; j < 4; j++) {
|
2016-05-23 20:10:26 +00:00
|
|
|
|
|
|
|
float v[3];
|
2017-03-18 23:36:26 +00:00
|
|
|
v[0] = 1.0;
|
|
|
|
v[1] = 1 - 2 * ((j >> 1) & 1);
|
|
|
|
v[2] = v[1] * (1 - 2 * (j & 1));
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int k = 0; k < 3; k++) {
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (i < 3)
|
|
|
|
face_points[j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
|
2016-05-23 20:10:26 +00:00
|
|
|
else
|
2017-03-18 23:36:26 +00:00
|
|
|
face_points[3 - j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
normal_points[j] = Vector3();
|
|
|
|
normal_points[j][i % 3] = (i >= 3 ? -1 : 1);
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
//tri 1
|
2016-05-23 20:10:26 +00:00
|
|
|
ADD_VTX(0);
|
|
|
|
ADD_VTX(1);
|
|
|
|
ADD_VTX(2);
|
2017-03-18 23:36:26 +00:00
|
|
|
//tri 2
|
2016-05-23 20:10:26 +00:00
|
|
|
ADD_VTX(2);
|
|
|
|
ADD_VTX(3);
|
|
|
|
ADD_VTX(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Array d;
|
|
|
|
d.resize(VS::ARRAY_MAX);
|
2017-03-18 23:36:26 +00:00
|
|
|
d[VisualServer::ARRAY_NORMAL] = normals;
|
|
|
|
d[VisualServer::ARRAY_TANGENT] = tangents;
|
|
|
|
d[VisualServer::ARRAY_TEX_UV] = uvs;
|
|
|
|
d[VisualServer::ARRAY_VERTEX] = vertices;
|
2016-05-23 20:10:26 +00:00
|
|
|
|
|
|
|
DVector<int> indices;
|
|
|
|
indices.resize(vertices.size());
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < vertices.size(); i++)
|
|
|
|
indices.set(i, i);
|
|
|
|
d[VisualServer::ARRAY_INDEX] = indices;
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
box_mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, d);
|
2016-05-23 20:10:26 +00:00
|
|
|
box_instance->set_mesh(box_mesh);
|
|
|
|
box_instance->hide();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
set_custom_minimum_size(Size2(1, 150) * EDSCALE);
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
HBoxContainer *hb = memnew(HBoxContainer);
|
2016-05-23 20:10:26 +00:00
|
|
|
add_child(hb);
|
|
|
|
hb->set_area_as_parent_rect(2);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
VBoxContainer *vb_shape = memnew(VBoxContainer);
|
2016-05-23 20:10:26 +00:00
|
|
|
hb->add_child(vb_shape);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
sphere_switch = memnew(TextureButton);
|
2016-05-23 20:10:26 +00:00
|
|
|
sphere_switch->set_toggle_mode(true);
|
|
|
|
sphere_switch->set_pressed(true);
|
|
|
|
vb_shape->add_child(sphere_switch);
|
2017-03-18 23:36:26 +00:00
|
|
|
sphere_switch->connect("pressed", this, "_button_pressed", varray(sphere_switch));
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
box_switch = memnew(TextureButton);
|
2016-05-23 20:10:26 +00:00
|
|
|
box_switch->set_toggle_mode(true);
|
|
|
|
box_switch->set_pressed(false);
|
|
|
|
vb_shape->add_child(box_switch);
|
2017-03-18 23:36:26 +00:00
|
|
|
box_switch->connect("pressed", this, "_button_pressed", varray(box_switch));
|
2016-05-23 20:10:26 +00:00
|
|
|
|
|
|
|
hb->add_spacer();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
VBoxContainer *vb_light = memnew(VBoxContainer);
|
2016-05-23 20:10:26 +00:00
|
|
|
hb->add_child(vb_light);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
light_1_switch = memnew(TextureButton);
|
2016-05-23 20:10:26 +00:00
|
|
|
light_1_switch->set_toggle_mode(true);
|
|
|
|
vb_light->add_child(light_1_switch);
|
2017-03-18 23:36:26 +00:00
|
|
|
light_1_switch->connect("pressed", this, "_button_pressed", varray(light_1_switch));
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
light_2_switch = memnew(TextureButton);
|
2016-05-23 20:10:26 +00:00
|
|
|
light_2_switch->set_toggle_mode(true);
|
|
|
|
vb_light->add_child(light_2_switch);
|
2017-03-18 23:36:26 +00:00
|
|
|
light_2_switch->connect("pressed", this, "_button_pressed", varray(light_2_switch));
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
first_enter = true;
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MaterialEditorPlugin::edit(Object *p_object) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Material *s = p_object->cast_to<Material>();
|
2016-05-23 20:10:26 +00:00
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
|
|
|
material_editor->edit(Ref<Material>(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MaterialEditorPlugin::handles(Object *p_object) const {
|
|
|
|
|
|
|
|
return p_object->is_type("Material");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaterialEditorPlugin::make_visible(bool p_visible) {
|
|
|
|
|
|
|
|
if (p_visible) {
|
|
|
|
material_editor->show();
|
2017-03-18 23:36:26 +00:00
|
|
|
// material_editor->set_process(true);
|
2016-05-23 20:10:26 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
material_editor->hide();
|
2017-03-18 23:36:26 +00:00
|
|
|
// material_editor->set_process(false);
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MaterialEditorPlugin::MaterialEditorPlugin(EditorNode *p_node) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
editor = p_node;
|
|
|
|
material_editor = memnew(MaterialEditor);
|
|
|
|
add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM, material_editor);
|
2016-05-23 20:10:26 +00:00
|
|
|
material_editor->hide();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
MaterialEditorPlugin::~MaterialEditorPlugin() {
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|