diff options
-rw-r--r-- | core/color.h | 10 | ||||
-rw-r--r-- | editor/editor_node.cpp | 9 | ||||
-rw-r--r-- | editor/editor_run.cpp | 22 | ||||
-rw-r--r-- | editor/import/editor_scene_importer_gltf.cpp | 1581 | ||||
-rw-r--r-- | editor/import/editor_scene_importer_gltf.h | 258 | ||||
-rw-r--r-- | editor/project_manager.cpp | 6 | ||||
-rw-r--r-- | main/main.cpp | 178 | ||||
-rw-r--r-- | methods.py | 1 | ||||
-rw-r--r-- | scene/2d/tile_map.cpp | 15 | ||||
-rw-r--r-- | scene/2d/tile_map.h | 1 | ||||
-rw-r--r-- | scene/3d/arvr_nodes.cpp | 113 | ||||
-rw-r--r-- | scene/3d/arvr_nodes.h | 37 | ||||
-rw-r--r-- | scene/gui/item_list.cpp | 8 | ||||
-rw-r--r-- | scene/register_scene_types.cpp | 1 | ||||
-rw-r--r-- | scene/resources/material.cpp | 109 | ||||
-rw-r--r-- | scene/resources/material.h | 31 | ||||
-rw-r--r-- | scene/resources/surface_tool.cpp | 75 | ||||
-rw-r--r-- | scene/resources/surface_tool.h | 3 | ||||
-rw-r--r-- | servers/arvr/arvr_positional_tracker.cpp | 33 | ||||
-rw-r--r-- | servers/arvr/arvr_positional_tracker.h | 8 | ||||
-rw-r--r-- | thirdparty/misc/base64.h | 5 |
21 files changed, 2349 insertions, 155 deletions
diff --git a/core/color.h b/core/color.h index c83dcda4b4..9074a0e6d6 100644 --- a/core/color.h +++ b/core/color.h @@ -140,8 +140,16 @@ struct Color { b < 0.04045 ? b * (1.0 / 12.92) : Math::pow((b + 0.055) * (1.0 / (1 + 0.055)), 2.4), a); } + _FORCE_INLINE_ Color to_srgb() const { - static Color hex(uint32_t p_hex); + return Color( + r < 0.0031308 ? 12.92 * r : (1.0 + 0.055) * Math::pow(r, 1.0f / 2.4f) - 0.055, + g < 0.0031308 ? 12.92 * g : (1.0 + 0.055) * Math::pow(g, 1.0f / 2.4f) - 0.055, + b < 0.0031308 ? 12.92 * b : (1.0 + 0.055) * Math::pow(b, 1.0f / 2.4f) - 0.055, a); + } + + static Color + hex(uint32_t p_hex); static Color html(const String &p_color); static bool html_is_valid(const String &p_color); static Color named(const String &p_name); diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 4e2687449e..4d5dd14172 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -110,6 +110,7 @@ // end #include "editor_settings.h" #include "import/editor_import_collada.h" +#include "import/editor_scene_importer_gltf.h" #include "io_plugins/editor_bitmask_import_plugin.h" #include "io_plugins/editor_export_scene.h" #include "io_plugins/editor_font_import_plugin.h" @@ -2803,9 +2804,9 @@ void EditorNode::_discard_changes(const String &p_str) { String exec = OS::get_singleton()->get_executable_path(); List<String> args; - args.push_back("--path"); + args.push_back("-path"); args.push_back(exec.get_base_dir()); - args.push_back("--project-manager"); + args.push_back("-pm"); OS::ProcessID pid = 0; Error err = OS::get_singleton()->execute(exec, args, false, &pid); @@ -5151,6 +5152,10 @@ EditorNode::EditorNode() { Ref<EditorOBJImporter> import_obj; import_obj.instance(); import_scene->add_importer(import_obj); + + Ref<EditorSceneImporterGLTF> import_gltf; + import_gltf.instance(); + import_scene->add_importer(import_gltf); } } diff --git a/editor/editor_run.cpp b/editor/editor_run.cpp index fee2d0ba4d..aa97dd237b 100644 --- a/editor/editor_run.cpp +++ b/editor/editor_run.cpp @@ -45,24 +45,24 @@ Error EditorRun::run(const String &p_scene, const String p_custom_args, const Li int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port"); if (resource_path != "") { - args.push_back("--path"); + args.push_back("-path"); args.push_back(resource_path.replace(" ", "%20")); } if (true) { - args.push_back("--remote-debug"); + args.push_back("-rdebug"); args.push_back(remote_host + ":" + String::num(remote_port)); } - args.push_back("--editor-pid"); + args.push_back("-epid"); args.push_back(String::num(OS::get_singleton()->get_process_ID())); if (debug_collisions) { - args.push_back("--debug-collision"); + args.push_back("-debugcol"); } if (debug_navigation) { - args.push_back("--debug-navigation"); + args.push_back("-debugnav"); } int screen = EditorSettings::get_singleton()->get("run/window_placement/screen"); @@ -101,33 +101,33 @@ Error EditorRun::run(const String &p_scene, const String p_custom_args, const Li case 1: { // centered Vector2 pos = screen_rect.position + ((screen_rect.size - desired_size) / 2).floor(); args.push_back("-p"); - args.push_back(itos(pos.x) + "," + itos(pos.y)); + args.push_back(itos(pos.x) + "x" + itos(pos.y)); } break; case 2: { // custom pos Vector2 pos = EditorSettings::get_singleton()->get("run/window_placement/rect_custom_position"); pos += screen_rect.position; args.push_back("-p"); - args.push_back(itos(pos.x) + "," + itos(pos.y)); + args.push_back(itos(pos.x) + "x" + itos(pos.y)); } break; case 3: { // force maximized Vector2 pos = screen_rect.position; args.push_back("-p"); - args.push_back(itos(pos.x) + "," + itos(pos.y)); - args.push_back("-m"); + args.push_back(itos(pos.x) + "x" + itos(pos.y)); + args.push_back("-mx"); } break; case 4: { // force fullscreen Vector2 pos = screen_rect.position; args.push_back("-p"); - args.push_back(itos(pos.x) + "," + itos(pos.y)); + args.push_back(itos(pos.x) + "x" + itos(pos.y)); args.push_back("-f"); } break; } if (p_breakpoints.size()) { - args.push_back("-b"); + args.push_back("-bp"); String bpoints; for (const List<String>::Element *E = p_breakpoints.front(); E; E = E->next()) { diff --git a/editor/import/editor_scene_importer_gltf.cpp b/editor/import/editor_scene_importer_gltf.cpp new file mode 100644 index 0000000000..35b31b56b4 --- /dev/null +++ b/editor/import/editor_scene_importer_gltf.cpp @@ -0,0 +1,1581 @@ +#include "editor_scene_importer_gltf.h" +#include "io/json.h" +#include "os/file_access.h" +#include "os/os.h" +#include "scene/3d/camera.h" +#include "scene/3d/mesh_instance.h" +#include "scene/resources/surface_tool.h" +#include "thirdparty/misc/base64.h" + +uint32_t EditorSceneImporterGLTF::get_import_flags() const { + + return IMPORT_SCENE | IMPORT_ANIMATION; +} +void EditorSceneImporterGLTF::get_extensions(List<String> *r_extensions) const { + + r_extensions->push_back("gltf"); + r_extensions->push_back("gfb"); +} + +Error EditorSceneImporterGLTF::_parse_json(const String &p_path, GLTFState &state) { + + Error err; + FileAccessRef f = FileAccess::open(p_path, FileAccess::READ, &err); + if (!f) { + return err; + } + + Vector<uint8_t> array; + array.resize(f->get_len()); + f->get_buffer(array.ptr(), array.size()); + String text; + text.parse_utf8((const char *)array.ptr(), array.size()); + + String err_txt; + int err_line; + Variant v; + err = JSON::parse(text, v, err_txt, err_line); + if (err != OK) { + _err_print_error("", p_path.utf8().get_data(), err_line, err_txt.utf8().get_data(), ERR_HANDLER_SCRIPT); + return err; + } + state.json = v; + + return OK; +} + +static Vector3 _arr_to_vec3(const Array &p_array) { + ERR_FAIL_COND_V(p_array.size() != 3, Vector3()); + return Vector3(p_array[0], p_array[1], p_array[2]); +} + +static Quat _arr_to_quat(const Array &p_array) { + ERR_FAIL_COND_V(p_array.size() != 4, Quat()); + return Quat(p_array[0], p_array[1], p_array[2], p_array[3]); +} + +static Transform _arr_to_xform(const Array &p_array) { + ERR_FAIL_COND_V(p_array.size() != 16, Transform()); + + Transform xform; + xform.basis.set_axis(Vector3::AXIS_X, Vector3(p_array[0], p_array[1], p_array[2])); + xform.basis.set_axis(Vector3::AXIS_Y, Vector3(p_array[4], p_array[5], p_array[6])); + xform.basis.set_axis(Vector3::AXIS_Z, Vector3(p_array[8], p_array[9], p_array[10])); + xform.set_origin(Vector3(p_array[12], p_array[13], p_array[14])); + + return xform; +} + +String EditorSceneImporterGLTF::_gen_unique_name(GLTFState &state, const String &p_name) { + + int index = 1; + + String name; + while (true) { + + name = p_name; + if (index > 1) { + name += " " + itos(index); + } + if (!state.unique_names.has(name)) { + break; + } + index++; + } + + state.unique_names.insert(name); + + return name; +} + +Error EditorSceneImporterGLTF::_parse_scenes(GLTFState &state) { + + ERR_FAIL_COND_V(!state.json.has("scenes"), ERR_FILE_CORRUPT); + Array scenes = state.json["scenes"]; + for (int i = 0; i < 1; i++) { //only first scene is imported + Dictionary s = scenes[i]; + ERR_FAIL_COND_V(!s.has("nodes"), ERR_UNAVAILABLE); + Array nodes = s["nodes"]; + for (int j = 0; j < nodes.size(); j++) { + state.root_nodes.push_back(nodes[j]); + } + + if (s.has("name")) { + state.scene_name = s["name"]; + } + } + + return OK; +} + +Error EditorSceneImporterGLTF::_parse_nodes(GLTFState &state) { + + ERR_FAIL_COND_V(!state.json.has("nodes"), ERR_FILE_CORRUPT); + Array nodes = state.json["nodes"]; + for (int i = 0; i < nodes.size(); i++) { + + GLTFNode *node = memnew(GLTFNode); + Dictionary n = nodes[i]; + + print_line("node " + itos(i) + ": " + String(Variant(n))); + if (n.has("name")) { + node->name = n["name"]; + } + if (n.has("camera")) { + node->camera = n["camera"]; + } + if (n.has("mesh")) { + node->mesh = n["mesh"]; + } + if (n.has("skin")) { + node->skin = n["skin"]; + if (!state.skin_users.has(node->skin)) { + state.skin_users[node->skin] = Vector<int>(); + } + + state.skin_users[node->skin].push_back(i); + } + if (n.has("matrix")) { + node->xform = _arr_to_xform(n["matrix"]); + + } else { + + if (n.has("translation")) { + node->translation = _arr_to_vec3(n["translation"]); + } + if (n.has("rotation")) { + node->rotation = _arr_to_quat(n["rotation"]); + } + if (n.has("scale")) { + node->scale = _arr_to_vec3(n["scale"]); + } + + node->xform.basis = Basis(node->rotation); + node->xform.basis.scale(node->scale); + node->xform.origin = node->translation; + } + + if (n.has("children")) { + Array children = n["children"]; + for (int i = 0; i < children.size(); i++) { + node->children.push_back(children[i]); + } + } + + state.nodes.push_back(node); + } + + //build the hierarchy + + for (int i = 0; i < state.nodes.size(); i++) { + + for (int j = 0; j < state.nodes[i]->children.size(); j++) { + int child = state.nodes[i]->children[j]; + ERR_FAIL_INDEX_V(child, state.nodes.size(), ERR_FILE_CORRUPT); + ERR_CONTINUE(state.nodes[child]->parent != -1); //node already has a parent, wtf. + + state.nodes[child]->parent = i; + } + } + + return OK; +} + +static Vector<uint8_t> _parse_base64_uri(const String &uri) { + + int start = uri.find(","); + ERR_FAIL_COND_V(start == -1, Vector<uint8_t>()); + + CharString substr = uri.right(start + 1).ascii(); + + int strlen = substr.length(); + + Vector<uint8_t> buf; + buf.resize(strlen / 4 * 3 + 1 + 1); + + int len = base64_decode((char *)buf.ptr(), (char *)substr.get_data(), strlen); + + buf.resize(len); + + return buf; +} + +Error EditorSceneImporterGLTF::_parse_buffers(GLTFState &state, const String &p_base_path) { + + if (!state.json.has("buffers")) + return OK; + + Array buffers = state.json["buffers"]; + for (int i = 0; i < buffers.size(); i++) { + + if (i == 0 && state.gfb_data.size()) { + state.buffers.push_back(state.gfb_data); + + } else { + Dictionary buffer = buffers[i]; + if (buffer.has("uri")) { + + Vector<uint8_t> buffer_data; + String uri = buffer["uri"]; + + if (uri.findn("data:application/octet-stream;base64") == 0) { + //embedded data + buffer_data = _parse_base64_uri(uri); + } else { + + uri = p_base_path.plus_file(uri).replace("\\", "/"); //fix for windows + buffer_data = FileAccess::get_file_as_array(uri); + ERR_FAIL_COND_V(buffer.size() == 0, ERR_PARSE_ERROR); + } + + ERR_FAIL_COND_V(!buffer.has("byteLength"), ERR_PARSE_ERROR); + int byteLength = buffer["byteLength"]; + ERR_FAIL_COND_V(byteLength < buffer_data.size(), ERR_PARSE_ERROR); + state.buffers.push_back(buffer_data); + } + } + } + + print_line("total buffers: " + itos(state.buffers.size())); + + return OK; +} + +Error EditorSceneImporterGLTF::_parse_buffer_views(GLTFState &state) { + + ERR_FAIL_COND_V(!state.json.has("bufferViews"), ERR_FILE_CORRUPT); + Array buffers = state.json["bufferViews"]; + for (int i = 0; i < buffers.size(); i++) { + + Dictionary d = buffers[i]; + + GLTFBufferView buffer_view; + + ERR_FAIL_COND_V(!d.has("buffer"), ERR_PARSE_ERROR); + buffer_view.buffer = d["buffer"]; + ERR_FAIL_COND_V(!d.has("byteLength"), ERR_PARSE_ERROR); + buffer_view.byte_length = d["byteLength"]; + + if (d.has("byteOffset")) { + buffer_view.byte_offset = d["byteOffset"]; + } + + if (d.has("byteStride")) { + buffer_view.byte_stride = d["byteStride"]; + } + + if (d.has("target")) { + int target = d["target"]; + buffer_view.indices = target == ELEMENT_ARRAY_BUFFER; + } + + state.buffer_views.push_back(buffer_view); + } + + print_line("total buffer views: " + itos(state.buffer_views.size())); + + return OK; +} + +EditorSceneImporterGLTF::GLTFType EditorSceneImporterGLTF::_get_type_from_str(const String &p_string) { + + if (p_string == "SCALAR") + return TYPE_SCALAR; + + if (p_string == "VEC2") + return TYPE_VEC2; + if (p_string == "VEC3") + return TYPE_VEC3; + if (p_string == "VEC4") + return TYPE_VEC4; + + if (p_string == "MAT2") + return TYPE_MAT2; + if (p_string == "MAT3") + return TYPE_MAT3; + if (p_string == "MAT4") + return TYPE_MAT4; + + ERR_FAIL_V(TYPE_SCALAR); +} + +Error EditorSceneImporterGLTF::_parse_accessors(GLTFState &state) { + + ERR_FAIL_COND_V(!state.json.has("accessors"), ERR_FILE_CORRUPT); + Array accessors = state.json["accessors"]; + for (int i = 0; i < accessors.size(); i++) { + + Dictionary d = accessors[i]; + + GLTFAccessor accessor; + + ERR_FAIL_COND_V(!d.has("componentType"), ERR_PARSE_ERROR); + accessor.component_type = d["componentType"]; + ERR_FAIL_COND_V(!d.has("count"), ERR_PARSE_ERROR); + accessor.count = d["count"]; + ERR_FAIL_COND_V(!d.has("type"), ERR_PARSE_ERROR); + accessor.type = _get_type_from_str(d["type"]); + + if (d.has("bufferView")) { + accessor.buffer_view = d["bufferView"]; //optional because it may be sparse... + } + + if (d.has("byteOffset")) { + accessor.byte_offset = d["byteOffset"]; + } + + if (d.has("max")) { + accessor.max = d["max"]; + } + + if (d.has("min")) { + accessor.min = d["min"]; + } + + if (d.has("sparse")) { + //eeh.. + + Dictionary s = d["sparse"]; + + ERR_FAIL_COND_V(!d.has("count"), ERR_PARSE_ERROR); + accessor.sparse_count = d["count"]; + ERR_FAIL_COND_V(!d.has("indices"), ERR_PARSE_ERROR); + Dictionary si = d["indices"]; + + ERR_FAIL_COND_V(!si.has("bufferView"), ERR_PARSE_ERROR); + accessor.sparse_indices_buffer_view = si["bufferView"]; + ERR_FAIL_COND_V(!si.has("componentType"), ERR_PARSE_ERROR); + accessor.sparse_indices_component_type = si["componentType"]; + + if (si.has("byteOffset")) { + accessor.sparse_indices_byte_offset = si["byteOffset"]; + } + + ERR_FAIL_COND_V(!d.has("values"), ERR_PARSE_ERROR); + Dictionary sv = d["values"]; + + ERR_FAIL_COND_V(!sv.has("bufferView"), ERR_PARSE_ERROR); + accessor.sparse_values_buffer_view = sv["bufferView"]; + if (sv.has("byteOffset")) { + accessor.sparse_values_byte_offset = sv["byteOffset"]; + } + } + + state.accessors.push_back(accessor); + } + + print_line("total accessors: " + itos(state.accessors.size())); + + return OK; +} + +String EditorSceneImporterGLTF::_get_component_type_name(uint32_t p_component) { + + switch (p_component) { + case COMPONENT_TYPE_BYTE: return "Byte"; + case COMPONENT_TYPE_UNSIGNED_BYTE: return "UByte"; + case COMPONENT_TYPE_SHORT: return "Short"; + case COMPONENT_TYPE_UNSIGNED_SHORT: return "UShort"; + case COMPONENT_TYPE_INT: return "Int"; + case COMPONENT_TYPE_FLOAT: return "Float"; + } + + return "<Error>"; +} + +String EditorSceneImporterGLTF::_get_type_name(GLTFType p_component) { + + static const char *names[] = { + "float", + "vec2", + "vec3", + "vec4", + "mat2", + "mat3", + "mat4" + }; + + return names[p_component]; +} + +Error EditorSceneImporterGLTF::_decode_buffer_view(GLTFState &state, int p_buffer_view, double *dst, int skip_every, int skip_bytes, int element_size, int count, GLTFType type, int component_count, int component_type, int component_size, bool normalized, int byte_offset, bool for_vertex) { + + const GLTFBufferView &bv = state.buffer_views[p_buffer_view]; + + int stride = bv.byte_stride ? bv.byte_stride : element_size; + if (for_vertex && stride % 4) { + stride += 4 - (stride % 4); //according to spec must be multiple of 4 + } + + ERR_FAIL_INDEX_V(bv.buffer, state.buffers.size(), ERR_PARSE_ERROR); + + uint32_t offset = bv.byte_offset + byte_offset; + Vector<uint8_t> buffer = state.buffers[bv.buffer]; //copy on write, so no performance hit + + //use to debug + print_line("type " + _get_type_name(type) + " component type: " + _get_component_type_name(component_type) + " stride: " + itos(stride) + " amount " + itos(count)); + print_line("accessor offset" + itos(byte_offset) + " view offset: " + itos(bv.byte_offset) + " total buffer len: " + itos(buffer.size()) + " view len " + itos(bv.byte_length)); + + int buffer_end = (stride * (count - 1)) + element_size; + ERR_FAIL_COND_V(buffer_end > bv.byte_length, ERR_PARSE_ERROR); + + ERR_FAIL_COND_V((offset + buffer_end) > buffer.size(), ERR_PARSE_ERROR); + + //fill everything as doubles + + for (int i = 0; i < count; i++) { + + const uint8_t *src = &buffer[offset + i * stride]; + + for (int j = 0; j < component_count; j++) { + + if (skip_every && j > 0 && (j % skip_every) == 0) { + src += skip_bytes; + } + + double d = 0; + + switch (component_type) { + case COMPONENT_TYPE_BYTE: { + int8_t b = int8_t(*src); + if (normalized) { + d = (double(b) / 128.0); + } else { + d = double(b); + } + } break; + case COMPONENT_TYPE_UNSIGNED_BYTE: { + uint8_t b = *src; + if (normalized) { + d = (double(b) / 255.0); + } else { + d = double(b); + } + } break; + case COMPONENT_TYPE_SHORT: { + int16_t s = *(int16_t *)src; + if (normalized) { + d = (double(s) / 32768.0); + } else { + d = double(s); + } + } break; + case COMPONENT_TYPE_UNSIGNED_SHORT: { + uint16_t s = *(uint16_t *)src; + if (normalized) { + d = (double(s) / 65535.0); + } else { + d = double(s); + } + + } break; + case COMPONENT_TYPE_INT: { + d = *(int *)src; + } break; + case COMPONENT_TYPE_FLOAT: { + d = *(float *)src; + } break; + } + + *dst++ = d; + src += component_size; + } + } + + return OK; +} + +int EditorSceneImporterGLTF::_get_component_type_size(int component_type) { + + switch (component_type) { + case COMPONENT_TYPE_BYTE: return 1; break; + case COMPONENT_TYPE_UNSIGNED_BYTE: return 1; break; + case COMPONENT_TYPE_SHORT: return 2; break; + case COMPONENT_TYPE_UNSIGNED_SHORT: return 2; break; + case COMPONENT_TYPE_INT: return 4; break; + case COMPONENT_TYPE_FLOAT: return 4; break; + default: { ERR_FAIL_V(0); } + } + return 0; +} + +Vector<double> EditorSceneImporterGLTF::_decode_accessor(GLTFState &state, int p_accessor, bool p_for_vertex) { + + //spec, for reference: + //https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#data-alignment + + ERR_FAIL_INDEX_V(p_accessor, state.accessors.size(), Vector<double>()); + + const GLTFAccessor &a = state.accessors[p_accessor]; + + int component_count_for_type[7] = { + 1, 2, 3, 4, 4, 9, 16 + }; + + int component_count = component_count_for_type[a.type]; + int component_size = _get_component_type_size(a.component_type); + ERR_FAIL_COND_V(component_size == 0, Vector<double>()); + int element_size = component_count * component_size; + + int skip_every = 0; + int skip_bytes = 0; + //special case of alignments, as described in spec + switch (a.component_type) { + case COMPONENT_TYPE_BYTE: + case COMPONENT_TYPE_UNSIGNED_BYTE: { + + if (a.type == TYPE_MAT2) { + skip_every = 2; + skip_bytes = 2; + element_size = 8; //override for this case + } + if (a.type == TYPE_MAT3) { + skip_every = 3; + skip_bytes = 1; + element_size = 12; //override for this case + } + + } break; + case COMPONENT_TYPE_SHORT: + case COMPONENT_TYPE_UNSIGNED_SHORT: { + if (a.type == TYPE_MAT3) { + skip_every = 6; + skip_bytes = 4; + element_size = 16; //override for this case + } + } break; + default: {} + } + + Vector<double> dst_buffer; + dst_buffer.resize(component_count * a.count); + double *dst = dst_buffer.ptr(); + + if (a.buffer_view >= 0) { + + ERR_FAIL_INDEX_V(a.buffer_view, state.buffer_views.size(), Vector<double>()); + + Error err = _decode_buffer_view(state, a.buffer_view, dst, skip_every, skip_bytes, element_size, a.count, a.type, component_count, a.component_type, component_size, a.normalized, a.byte_offset, p_for_vertex); + if (err != OK) + return Vector<double>(); + + } else { + //fill with zeros, as bufferview is not defined. + for (int i = 0; i < (a.count * component_count); i++) { + dst_buffer[i] = 0; + } + } + + if (a.sparse_count > 0) { + // I could not find any file using this, so this code is so far untested + Vector<double> indices; + indices.resize(a.sparse_count); + int indices_component_size = _get_component_type_size(a.sparse_indices_component_type); + + Error err = _decode_buffer_view(state, a.sparse_indices_buffer_view, indices.ptr(), 0, 0, indices_component_size, a.sparse_count, TYPE_SCALAR, 1, a.sparse_indices_component_type, indices_component_size, false, a.sparse_indices_byte_offset, false); + if (err != OK) + return Vector<double>(); + + Vector<double> data; + data.resize(component_count * a.sparse_count); + err = _decode_buffer_view(state, a.sparse_values_buffer_view, data.ptr(), skip_every, skip_bytes, element_size, a.sparse_count, a.type, component_count, a.component_type, component_size, a.normalized, a.sparse_values_byte_offset, p_for_vertex); + if (err != OK) + return Vector<double>(); + + for (int i = 0; i < indices.size(); i++) { + int write_offset = int(indices[i]) * component_count; + + for (int j = 0; j < component_count; j++) { + dst[write_offset + j] = data[i * component_count + j]; + } + } + } + + return dst_buffer; +} + +PoolVector<int> EditorSceneImporterGLTF::_decode_accessor_as_ints(GLTFState &state, int p_accessor, bool p_for_vertex) { + + Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex); + PoolVector<int> ret; + if (attribs.size() == 0) + return ret; + const double *attribs_ptr = attribs.ptr(); + int ret_size = attribs.size(); + ret.resize(ret_size); + { + PoolVector<int>::Write w = ret.write(); + for (int i = 0; i < ret_size; i++) { + w[i] = int(attribs_ptr[i]); + } + } + return ret; +} + +PoolVector<float> EditorSceneImporterGLTF::_decode_accessor_as_floats(GLTFState &state, int p_accessor, bool p_for_vertex) { + + Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex); + PoolVector<float> ret; + if (attribs.size() == 0) + return ret; + const double *attribs_ptr = attribs.ptr(); + int ret_size = attribs.size(); + ret.resize(ret_size); + { + PoolVector<float>::Write w = ret.write(); + for (int i = 0; i < ret_size; i++) { + w[i] = float(attribs_ptr[i]); + } + } + return ret; +} + +PoolVector<Vector2> EditorSceneImporterGLTF::_decode_accessor_as_vec2(GLTFState &state, int p_accessor, bool p_for_vertex) { + + Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex); + PoolVector<Vector2> ret; + if (attribs.size() == 0) + return ret; + ERR_FAIL_COND_V(attribs.size() % 2 != 0, ret); + const double *attribs_ptr = attribs.ptr(); + int ret_size = attribs.size() / 2; + ret.resize(ret_size); + { + PoolVector<Vector2>::Write w = ret.write(); + for (int i = 0; i < ret_size; i++) { + w[i] = Vector2(attribs_ptr[i * 2 + 0], attribs_ptr[i * 2 + 1]); + } + } + return ret; +} + +PoolVector<Vector3> EditorSceneImporterGLTF::_decode_accessor_as_vec3(GLTFState &state, int p_accessor, bool p_for_vertex) { + + Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex); + PoolVector<Vector3> ret; + if (attribs.size() == 0) + return ret; + ERR_FAIL_COND_V(attribs.size() % 3 != 0, ret); + const double *attribs_ptr = attribs.ptr(); + int ret_size = attribs.size() / 3; + ret.resize(ret_size); + { + PoolVector<Vector3>::Write w = ret.write(); + for (int i = 0; i < ret_size; i++) { + w[i] = Vector3(attribs_ptr[i * 3 + 0], attribs_ptr[i * 3 + 1], attribs_ptr[i * 3 + 2]); + } + } + return ret; +} +PoolVector<Color> EditorSceneImporterGLTF::_decode_accessor_as_color(GLTFState &state, int p_accessor, bool p_for_vertex) { + + Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex); + PoolVector<Color> ret; + if (attribs.size() == 0) + return ret; + ERR_FAIL_COND_V(attribs.size() % 4 != 0, ret); + const double *attribs_ptr = attribs.ptr(); + int ret_size = attribs.size() / 4; + ret.resize(ret_size); + { + PoolVector<Color>::Write w = ret.write(); + for (int i = 0; i < ret_size; i++) { + w[i] = Color(attribs_ptr[i * 4 + 0], attribs_ptr[i * 4 + 1], attribs_ptr[i * 4 + 2], attribs_ptr[i * 4 + 3]); + } + } + return ret; +} +Vector<Transform2D> EditorSceneImporterGLTF::_decode_accessor_as_xform2d(GLTFState &state, int p_accessor, bool p_for_vertex) { + + Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex); + Vector<Transform2D> ret; + if (attribs.size() == 0) + return ret; + ERR_FAIL_COND_V(attribs.size() % 4 != 0, ret); + ret.resize(attribs.size() / 4); + for (int i = 0; i < ret.size(); i++) { + ret[i][0] = Vector2(attribs[i * 4 + 0], attribs[i * 4 + 1]); + ret[i][1] = Vector2(attribs[i * 4 + 2], attribs[i * 4 + 3]); + } + return ret; +} + +Vector<Basis> EditorSceneImporterGLTF::_decode_accessor_as_basis(GLTFState &state, int p_accessor, bool p_for_vertex) { + + Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex); + Vector<Basis> ret; + if (attribs.size() == 0) + return ret; + ERR_FAIL_COND_V(attribs.size() % 9 != 0, ret); + ret.resize(attribs.size() / 9); + for (int i = 0; i < ret.size(); i++) { + ret[i].set_axis(0, Vector3(attribs[i * 9 + 0], attribs[i * 9 + 1], attribs[i * 9 + 2])); + ret[i].set_axis(1, Vector3(attribs[i * 9 + 3], attribs[i * 9 + 4], attribs[i * 9 + 5])); + ret[i].set_axis(2, Vector3(attribs[i * 9 + 6], attribs[i * 9 + 7], attribs[i * 9 + 8])); + } + return ret; +} +Vector<Transform> EditorSceneImporterGLTF::_decode_accessor_as_xform(GLTFState &state, int p_accessor, bool p_for_vertex) { + + Vector<double> attribs = _decode_accessor(state, p_accessor, p_for_vertex); + Vector<Transform> ret; + if (attribs.size() == 0) + return ret; + ERR_FAIL_COND_V(attribs.size() % 16 != 0, ret); + ret.resize(attribs.size() / 16); + for (int i = 0; i < ret.size(); i++) { + ret[i].basis.set_axis(0, Vector3(attribs[i * 16 + 0], attribs[i * 16 + 1], attribs[i * 16 + 2])); + ret[i].basis.set_axis(1, Vector3(attribs[i * 16 + 4], attribs[i * 16 + 5], attribs[i * 16 + 6])); + ret[i].basis.set_axis(2, Vector3(attribs[i * 16 + 8], attribs[i * 16 + 9], attribs[i * 16 + 10])); + ret[i].set_origin(Vector3(attribs[i * 16 + 12], attribs[i * 16 + 13], attribs[i * 16 + 14])); + } + return ret; +} + +Error EditorSceneImporterGLTF::_parse_meshes(GLTFState &state) { + + if (!state.json.has("meshes")) + return OK; + + Array meshes = state.json["meshes"]; + for (int i = 0; i < meshes.size(); i++) { + + Dictionary d = meshes[i]; + + GLTFMesh mesh; + mesh.mesh.instance(); + + ERR_FAIL_COND_V(!d.has("primitives"), ERR_PARSE_ERROR); + + Array primitives = d["primitives"]; + + for (int j = 0; j < primitives.size(); j++) { + + Dictionary p = primitives[j]; + + Array array; + array.resize(Mesh::ARRAY_MAX); + + ERR_FAIL_COND_V(!p.has("attributes"), ERR_PARSE_ERROR); + + Dictionary a = p["attributes"]; + + Mesh::PrimitiveType primitive = Mesh::PRIMITIVE_TRIANGLES; + if (p.has("mode")) { + int mode = p["mode"]; + ERR_FAIL_INDEX_V(mode, 7, ERR_FILE_CORRUPT); + static const Mesh::PrimitiveType primitives[7] = { + Mesh::PRIMITIVE_POINTS, + Mesh::PRIMITIVE_LINES, + Mesh::PRIMITIVE_LINE_LOOP, + Mesh::PRIMITIVE_LINE_STRIP, + Mesh::PRIMITIVE_TRIANGLES, + Mesh::PRIMITIVE_TRIANGLE_STRIP, + Mesh::PRIMITIVE_TRIANGLE_FAN, + }; + + primitive = primitives[mode]; + } + + if (a.has("POSITION")) { + array[Mesh::ARRAY_VERTEX] = _decode_accessor_as_vec3(state, a["POSITION"], true); + } + if (a.has("NORMAL")) { + array[Mesh::ARRAY_NORMAL] = _decode_accessor_as_vec3(state, a["NORMAL"], true); + } + if (a.has("TANGENT")) { + array[Mesh::ARRAY_TANGENT] = _decode_accessor_as_floats(state, a["TANGENT"], true); + } + if (a.has("TEXCOORD_0")) { + array[Mesh::ARRAY_TEX_UV] = _decode_accessor_as_vec2(state, a["TEXCOORD_0"], true); + } + if (a.has("TEXCOORD_1")) { + array[Mesh::ARRAY_TEX_UV2] = _decode_accessor_as_vec2(state, a["TEXCOORD_1"], true); + } + if (a.has("COLOR_0")) { + array[Mesh::ARRAY_COLOR] = _decode_accessor_as_color(state, a["COLOR_0"], true); + } + if (a.has("JOINTS_0")) { + array[Mesh::ARRAY_BONES] = _decode_accessor_as_ints(state, a["JOINTS_0"], true); + } + if (a.has("WEIGHTS_0")) { + PoolVector<float> weights = _decode_accessor_as_floats(state, a["WEIGHTS_0"], true); + { //gltf does not seem to normalize the weights for some reason.. + int wc = weights.size(); + PoolVector<float>::Write w = weights.write(); + for (int i = 0; i < wc; i += 4) { + float total = 0.0; + total += w[i + 0]; + total += w[i + 1]; + total += w[i + 2]; + total += w[i + 3]; + if (total > 0.0) { + w[i + 0] /= total; + w[i + 1] /= total; + w[i + 2] /= total; + w[i + 3] /= total; + } + } + } + array[Mesh::ARRAY_WEIGHTS] = weights; + } + + if (p.has("indices")) { + + PoolVector<int> indices = _decode_accessor_as_ints(state, p["indices"], false); + + if (primitive == Mesh::PRIMITIVE_TRIANGLES) { + //swap around indices, convert ccw to cw for front face + + int is = indices.size(); + PoolVector<int>::Write w = indices.write(); + for (int i = 0; i < is; i += 3) { + SWAP(w[i + 1], w[i + 2]); + } + } + array[Mesh::ARRAY_INDEX] = indices; + } else if (primitive == Mesh::PRIMITIVE_TRIANGLES) { + //generate indices because they need to be swapped for CW/CCW + PoolVector<Vector3> vertices = array[Mesh::ARRAY_VERTEX]; + ERR_FAIL_COND_V(vertices.size() == 0, ERR_PARSE_ERROR); + PoolVector<int> indices; + int vs = vertices.size(); + indices.resize(vs); + { + PoolVector<int>::Write w = indices.write(); + for (int i = 0; i < vs; i += 3) { + w[i] = i; + w[i + 1] = i + 2; + w[i + 2] = i + 1; + } + } + array[Mesh::ARRAY_INDEX] = indices; + } + + bool generated_tangents = false; + Variant erased_indices; + + if (primitive == Mesh::PRIMITIVE_TRIANGLES && !a.has("TANGENT") && a.has("TEXCOORD_0") && a.has("NORMAL")) { + //must generate mikktspace tangents.. ergh.. + Ref<SurfaceTool> st; + st.instance(); + st->create_from_triangle_arrays(array); + if (p.has("targets")) { + //morph targets should not be reindexed, as array size might differ + //removing indices is the best bet here + st->deindex(); + erased_indices = a[Mesh::ARRAY_INDEX]; + a[Mesh::ARRAY_INDEX] = Variant(); + } + st->generate_tangents(); + array = st->commit_to_arrays(); + generated_tangents = true; + } + + Array morphs; + //blend shapes + if (p.has("targets")) { + print_line("has targets!"); + Array targets = p["targets"]; + + if (j == 0) { + for (int k = 0; k < targets.size(); k++) { + mesh.mesh->add_blend_shape(String("morph_") + itos(k)); + } + } + + for (int k = 0; k < targets.size(); k++) { + + Dictionary t = targets[k]; + + Array array_copy; + array_copy.resize(Mesh::ARRAY_MAX); + + for (int l = 0; l < Mesh::ARRAY_MAX; l++) { + array_copy[l] = array[l]; + } + + array_copy[Mesh::ARRAY_INDEX] = Variant(); + + if (t.has("POSITION")) { + array_copy[Mesh::ARRAY_VERTEX] = _decode_accessor_as_vec3(state, t["POSITION"], true); + } + if (t.has("NORMAL")) { + array_copy[Mesh::ARRAY_NORMAL] = _decode_accessor_as_vec3(state, t["NORMAL"], true); + } + if (t.has("TANGENT")) { + PoolVector<Vector3> tangents_v3 = _decode_accessor_as_vec3(state, t["TANGENT"], true); + PoolVector<float> tangents_v4; + PoolVector<float> src_tangents = array[Mesh::ARRAY_TANGENT]; + ERR_FAIL_COND_V(src_tangents.size() == 0, ERR_PARSE_ERROR); + + { + + int size4 = src_tangents.size(); + tangents_v4.resize(size4); + PoolVector<float>::Write w4 = tangents_v4.write(); + + PoolVector<Vector3>::Read r3 = tangents_v3.read(); + PoolVector<float>::Read r4 = src_tangents.read(); + + for (int l = 0; l < size4 / 4; l++) { + + w4[l * 4 + 0] = r3[l].x; + w4[l * 4 + 1] = r3[l].y; + w4[l * 4 + 2] = r3[l].z; + w4[l * 4 + 3] = r4[l * 4 + 3]; //copy flip value + } + } + + array_copy[Mesh::ARRAY_TANGENT] = tangents_v4; + } + + if (generated_tangents) { + Ref<SurfaceTool> st; + st.instance(); + array_copy[Mesh::ARRAY_INDEX] = erased_indices; //needed for tangent generation, erased by deindex + st->create_from_triangle_arrays(array_copy); + st->deindex(); + st->generate_tangents(); + array_copy = st->commit_to_arrays(); + } + + morphs.push_back(array_copy); + } + } + + //just add it + mesh.mesh->add_surface_from_arrays(primitive, array, morphs); + + if (p.has("material")) { + int material = p["material"]; + ERR_FAIL_INDEX_V(material, state.materials.size(), ERR_FILE_CORRUPT); + Ref<Material> mat = state.materials[material]; + + mesh.mesh->surface_set_material(mesh.mesh->get_surface_count() - 1, mat); + } + } + + if (d.has("weights")) { + Array weights = d["weights"]; + ERR_FAIL_COND_V(mesh.mesh->get_blend_shape_count() != weights.size(), ERR_PARSE_ERROR); + mesh.blend_weights.resize(weights.size()); + for (int j = 0; j < weights.size(); j++) { + mesh.blend_weights[j] = weights[j]; + } + } + + state.meshes.push_back(mesh); + } + + print_line("total meshes: " + itos(state.meshes.size())); + + return OK; +} + +Error EditorSceneImporterGLTF::_parse_images(GLTFState &state, const String &p_base_path) { + + if (!state.json.has("images")) + return OK; + + Array images = state.json["images"]; + for (int i = 0; i < images.size(); i++) { + + Dictionary d = images[i]; + + String mimetype; + if (d.has("mimeType")) { + mimetype = d["mimeType"]; + } + + Vector<uint8_t> data; + const uint8_t *data_ptr = NULL; + int data_size = 0; + + if (d.has("uri")) { + String uri = d["uri"]; + + if (uri.findn("data:application/octet-stream;base64") == 0) { + //embedded data + data = _parse_base64_uri(uri); + data_ptr = data.ptr(); + data_size = data.size(); + } else { + + uri = p_base_path.plus_file(uri).replace("\\", "/"); //fix for windows + Ref<Texture> texture = ResourceLoader::load(uri); + state.images.push_back(texture); + continue; + } + } + + if (d.has("bufferView")) { + int bvi = d["bufferView"]; + + ERR_FAIL_INDEX_V(bvi, state.buffer_views.size(), ERR_PARAMETER_RANGE_ERROR); + + GLTFBufferView &bv = state.buffer_views[bvi]; + + int bi = bv.buffer; + ERR_FAIL_INDEX_V(bi, state.buffers.size(), ERR_PARAMETER_RANGE_ERROR); + + ERR_FAIL_COND_V(bv.byte_offset + bv.byte_length > state.buffers[bi].size(), ERR_FILE_CORRUPT); + + data_ptr = &state.buffers[bi][bv.byte_offset]; + data_size = bv.byte_length; + } + + ERR_FAIL_COND_V(mimetype == "", ERR_FILE_CORRUPT); + + if (mimetype.findn("png") != -1) { + //is a png + Ref<Image> img = Image::_png_mem_loader_func(data_ptr, data_size); + + ERR_FAIL_COND_V(img.is_null(), ERR_FILE_CORRUPT); + + Ref<ImageTexture> t; + t.instance(); + t->create_from_image(img); + + state.images.push_back(t); + continue; + } + + if (mimetype.findn("jpg") != -1) { + //is a jpg + Ref<Image> img = Image::_jpg_mem_loader_func(data_ptr, data_size); + + ERR_FAIL_COND_V(img.is_null(), ERR_FILE_CORRUPT); + + Ref<ImageTexture> t; + t.instance(); + t->create_from_image(img); + + state.images.push_back(t); + + continue; + } + + ERR_FAIL_V(ERR_FILE_CORRUPT); + } + + print_line("total images: " + itos(state.images.size())); + + return OK; +} + +Error EditorSceneImporterGLTF::_parse_textures(GLTFState &state) { + + if (!state.json.has("textures")) + return OK; + + Array textures = state.json["textures"]; + for (int i = 0; i < textures.size(); i++) { + + Dictionary d = textures[i]; + + ERR_FAIL_COND_V(!d.has("source"), ERR_PARSE_ERROR); + + GLTFTexture t; + t.src_image = d["source"]; + state.textures.push_back(t); + } + + return OK; +} + +Ref<Texture> EditorSceneImporterGLTF::_get_texture(GLTFState &state, int p_texture) { + ERR_FAIL_INDEX_V(p_texture, state.textures.size(), Ref<Texture>()); + int image = state.textures[p_texture].src_image; + + ERR_FAIL_INDEX_V(image, state.images.size(), Ref<Texture>()); + + return state.images[image]; +} + +Error EditorSceneImporterGLTF::_parse_materials(GLTFState &state) { + + if (!state.json.has("materials")) + return OK; + + Array materials = state.json["materials"]; + for (int i = 0; i < materials.size(); i++) { + + Dictionary d = materials[i]; + + Ref<SpatialMaterial> material; + material.instance(); + if (d.has("name")) { + material->set_name(d["name"]); + } + + if (d.has("pbrMetallicRoughness")) { + + Dictionary mr = d["pbrMetallicRoughness"]; + if (mr.has("baseColorFactor")) { + Array arr = mr["baseColorFactor"]; + ERR_FAIL_COND_V(arr.size() != 4, ERR_PARSE_ERROR); + Color c = Color(arr[0], arr[1], arr[2], arr[3]).to_srgb(); + + material->set_albedo(c); + } + + if (mr.has("baseColorTexture")) { + Dictionary bct = mr["baseColorTexture"]; + if (bct.has("index")) { + material->set_texture(SpatialMaterial::TEXTURE_ALBEDO, _get_texture(state, bct["index"])); + } + if (!mr.has("baseColorFactor")) { + material->set_albedo(Color(1, 1, 1)); + } + } + + if (mr.has("metallicFactor")) { + + material->set_metallic(mr["metallicFactor"]); + } + if (mr.has("roughnessFactor")) { + + material->set_roughness(mr["roughnessFactor"]); + } + + if (mr.has("metallicRoughnessTexture")) { + Dictionary bct = mr["metallicRoughnessTexture"]; + if (bct.has("index")) { + Ref<Texture> t = _get_texture(state, bct["index"]); + material->set_texture(SpatialMaterial::TEXTURE_METALLIC, t); + material->set_metallic_texture_channel(SpatialMaterial::TEXTURE_CHANNEL_RED); + material->set_texture(SpatialMaterial::TEXTURE_ROUGHNESS, t); + material->set_roughness_texture_channel(SpatialMaterial::TEXTURE_CHANNEL_GREEN); + if (!mr.has("metallicFactor")) { + material->set_metallic(1); + } + if (!mr.has("roughnessFactor")) { + material->set_roughness(1); + } + } + } + } + + if (d.has("normalTexture")) { + Dictionary bct = d["normalTexture"]; + if (bct.has("index")) { + material->set_texture(SpatialMaterial::TEXTURE_NORMAL, _get_texture(state, bct["index"])); + material->set_feature(SpatialMaterial::FEATURE_NORMAL_MAPPING, true); + } + if (bct.has("scale")) { + material->set_normal_scale(bct["scale"]); + } + } + if (d.has("occlusionTexture")) { + Dictionary bct = d["occlusionTexture"]; + if (bct.has("index")) { + material->set_texture(SpatialMaterial::TEXTURE_AMBIENT_OCCLUSION, _get_texture(state, bct["index"])); + material->set_feature(SpatialMaterial::FEATURE_AMBIENT_OCCLUSION, true); + } + } + + if (d.has("emissiveFactor")) { + Array arr = d["emissiveFactor"]; + ERR_FAIL_COND_V(arr.size() != 3, ERR_PARSE_ERROR); + Color c = Color(arr[0], arr[1], arr[2]).to_srgb(); + material->set_feature(SpatialMaterial::FEATURE_EMISSION, true); + + material->set_emission(c); + } + + if (d.has("emissiveTexture")) { + Dictionary bct = d["emissiveTexture"]; + if (bct.has("index")) { + material->set_texture(SpatialMaterial::TEXTURE_EMISSION, _get_texture(state, bct["index"])); + material->set_feature(SpatialMaterial::FEATURE_EMISSION, true); + material->set_emission(Color(0, 0, 0)); + } + } + + if (d.has("doubleSided")) { + bool ds = d["doubleSided"]; + if (ds) { + material->set_cull_mode(SpatialMaterial::CULL_DISABLED); + } + } + + if (d.has("alphaMode")) { + String am = d["alphaMode"]; + if (am != "OPAQUE") { + material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true); + } + } + + state.materials.push_back(material); + } + + print_line("total materials: " + itos(state.materials.size())); + + return OK; +} + +Error EditorSceneImporterGLTF::_parse_skins(GLTFState &state) { + + if (!state.json.has("skins")) + return OK; + + Array skins = state.json["skins"]; + for (int i = 0; i < skins.size(); i++) { + + Dictionary d = skins[i]; + + GLTFSkin skin; + + ERR_FAIL_COND_V(!d.has("joints"), ERR_PARSE_ERROR); + + Array joints = d["joints"]; + Vector<Transform> bind_matrices; + + if (d.has("inverseBindMatrices")) { + bind_matrices = _decode_accessor_as_xform(state, d["inverseBindMatrices"], false); + ERR_FAIL_COND_V(bind_matrices.size() != joints.size(), ERR_PARSE_ERROR); + } + + for (int j = 0; j < joints.size(); j++) { + int index = joints[j]; + ERR_FAIL_INDEX_V(index, state.nodes.size(), ERR_PARSE_ERROR); + state.nodes[index]->joint_skin = state.skins.size(); + state.nodes[index]->joint_bone = j; + GLTFSkin::Bone bone; + bone.node = index; + if (bind_matrices.size()) { + bone.inverse_bind = bind_matrices[j]; + } + + skin.bones.push_back(bone); + } + + print_line("skin has skeleton? " + itos(d.has("skeleton"))); + if (d.has("skeleton")) { + int skeleton = d["skeleton"]; + ERR_FAIL_INDEX_V(skeleton, state.nodes.size(), ERR_PARSE_ERROR); + state.nodes[skeleton]->skeleton_skin = state.skins.size(); + print_line("setting skeleton skin to" + itos(skeleton)); + skin.skeleton = skeleton; + } + + if (d.has("name")) { + skin.name = d["name"]; + } + + //locate the right place to put a Skeleton node + + if (state.skin_users.has(i)) { + Vector<int> users = state.skin_users[i]; + int skin_node = -1; + for (int j = 0; j < users.size(); j++) { + int user = state.nodes[users[j]]->parent; //always go from parent + if (j == 0) { + skin_node = user; + } else if (skin_node != -1) { + bool found = false; + while (skin_node >= 0) { + + int cuser = user; + while (cuser != -1) { + if (cuser == skin_node) { + found = true; + break; + } + cuser = state.nodes[skin_node]->parent; + } + if (found) + break; + skin_node = state.nodes[skin_node]->parent; + } + + if (!found) { + skin_node = -1; //just leave where it is + } + + //find a common parent + } + } + + if (skin_node != -1) { + for (int j = 0; j < users.size(); j++) { + state.nodes[users[j]]->child_of_skeleton = i; + } + + state.nodes[skin_node]->skeleton_children.push_back(i); + } + state.skins.push_back(skin); + } + } + print_line("total skins: " + itos(state.skins.size())); + + //now + + return OK; +} + +Error EditorSceneImporterGLTF::_parse_cameras(GLTFState &state) { + + if (!state.json.has("cameras")) + return OK; + + Array cameras = state.json["cameras"]; + + for (int i = 0; i < cameras.size(); i++) { + + Dictionary d = cameras[i]; + + GLTFCamera camera; + ERR_FAIL_COND_V(!d.has("type"), ERR_PARSE_ERROR); + String type = d["type"]; + if (type == "orthographic") { + + camera.perspective = false; + if (d.has("orthographic")) { + Dictionary og = d["orthographic"]; + camera.fov_size = og["ymag"]; + camera.zfar = og["zfar"]; + camera.znear = og["znear"]; + } else { + camera.fov_size = 10; + } + + } else if (type == "perspective") { + + camera.perspective = true; + if (d.has("perspective")) { + Dictionary ppt = d["perspective"]; + camera.fov_size = ppt["yfov"]; + camera.zfar = ppt["zfar"]; + camera.znear = ppt["znear"]; + } else { + camera.fov_size = 10; + } + } else { + ERR_EXPLAIN("Camera should be in 'orthographic' or 'perspective'"); + ERR_FAIL_V(ERR_PARSE_ERROR); + } + + state.cameras.push_back(camera); + } +} + +void EditorSceneImporterGLTF::_assign_scene_names(GLTFState &state) { + + for (int i = 0; i < state.nodes.size(); i++) { + GLTFNode *n = state.nodes[i]; + if (n->name == "") { + if (n->mesh >= 0) { + n->name = "Mesh"; + } else if (n->joint_skin >= 0) { + n->name = "Bone"; + } else { + n->name = "Node"; + } + } + + n->name = _gen_unique_name(state, n->name); + } +} + +void EditorSceneImporterGLTF::_generate_node(GLTFState &state, int p_node, Node *p_parent, Node *p_owner, Vector<Skeleton *> &skeletons) { + ERR_FAIL_INDEX(p_node, state.nodes.size()); + + GLTFNode *n = state.nodes[p_node]; + Spatial *node; + + if (n->mesh >= 0) { + ERR_FAIL_INDEX(n->mesh, state.meshes.size()); + MeshInstance *mi = memnew(MeshInstance); + const GLTFMesh &mesh = state.meshes[n->mesh]; + mi->set_mesh(mesh.mesh); + for (int i = 0; i < mesh.blend_weights.size(); i++) { + mi->set("blend_shapes/" + mesh.mesh->get_blend_shape_name(i), mesh.blend_weights[i]); + } + + node = mi; + } else if (n->camera >= 0) { + ERR_FAIL_INDEX(n->camera, state.cameras.size()); + Camera *camera = memnew(Camera); + + const GLTFCamera &c = state.cameras[n->camera]; + if (c.perspective) { + camera->set_perspective(c.fov_size, c.znear, c.znear); + } else { + camera->set_orthogonal(c.fov_size, c.znear, c.znear); + } + + node = camera; + } else { + node = memnew(Spatial); + } + + node->set_name(n->name); + + if (n->child_of_skeleton >= 0) { + //move skeleton around and place it on node, as the node _is_ a skeleton. + Skeleton *s = skeletons[n->child_of_skeleton]; + p_parent = s; + } + + p_parent->add_child(node); + node->set_owner(p_owner); + node->set_transform(n->xform); + + for (int i = 0; i < n->skeleton_children.size(); i++) { + + Skeleton *s = skeletons[n->skeleton_children[i]]; + s->get_parent()->remove_child(s); + node->add_child(s); + s->set_owner(p_owner); + } + + for (int i = 0; i < n->children.size(); i++) { + if (state.nodes[n->children[i]]->joint_skin >= 0) { + _generate_bone(state, n->children[i], skeletons, -1); + } else { + _generate_node(state, n->children[i], node, p_owner, skeletons); + } + } +} + +void EditorSceneImporterGLTF::_generate_bone(GLTFState &state, int p_node, Vector<Skeleton *> &skeletons, int p_parent_bone) { + ERR_FAIL_INDEX(p_node, state.nodes.size()); + + GLTFNode *n = state.nodes[p_node]; + + ERR_FAIL_COND(n->joint_skin < 0); + + int bone_index = skeletons[n->joint_skin]->get_bone_count(); + skeletons[n->joint_skin]->add_bone(n->name); + if (p_parent_bone >= 0) { + skeletons[n->joint_skin]->set_bone_parent(bone_index, p_parent_bone); + } + skeletons[n->joint_skin]->set_bone_rest(bone_index, state.skins[n->joint_skin].bones[n->joint_bone].inverse_bind.affine_inverse()); + + for (int i = 0; i < n->children.size(); i++) { + ERR_CONTINUE(state.nodes[n->children[i]]->joint_skin < 0); + _generate_bone(state, n->children[i], skeletons, bone_index); + } +} + +Spatial *EditorSceneImporterGLTF::_generate_scene(GLTFState &state) { + + Spatial *root = memnew(Spatial); + root->set_name(state.scene_name); + //generate skeletons + Vector<Skeleton *> skeletons; + for (int i = 0; i < state.skins.size(); i++) { + Skeleton *s = memnew(Skeleton); + String name = state.skins[i].name; + if (name == "") { + name = _gen_unique_name(state, "Skeleton"); + } + s->set_name(name); + root->add_child(s); + s->set_owner(root); + skeletons.push_back(s); + } + for (int i = 0; i < state.root_nodes.size(); i++) { + if (state.nodes[state.root_nodes[i]]->joint_skin >= 0) { + _generate_bone(state, state.root_nodes[i], skeletons, -1); + } else { + _generate_node(state, state.root_nodes[i], root, root, skeletons); + } + } + + for (int i = 0; i < skeletons.size(); i++) { + skeletons[i]->localize_rests(); + } + + return root; +} + +Node *EditorSceneImporterGLTF::import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, List<String> *r_missing_deps, Error *r_err) { + + GLTFState state; + Error err = _parse_json(p_path, state); + if (err) + return NULL; + + ERR_FAIL_COND_V(!state.json.has("asset"), NULL); + + Dictionary asset = state.json["asset"]; + + ERR_FAIL_COND_V(!asset.has("version"), NULL); + + String version = asset["version"]; + + state.major_version = version.get_slice(".", 0).to_int(); + state.minor_version = version.get_slice(".", 1).to_int(); + + /* STEP 0 PARSE SCENE */ + err = _parse_scenes(state); + if (err != OK) + return NULL; + + /* STEP 1 PARSE NODES */ + err = _parse_nodes(state); + if (err != OK) + return NULL; + + /* STEP 2 PARSE BUFFERS */ + err = _parse_buffers(state, p_path.get_base_dir()); + if (err != OK) + return NULL; + + /* STEP 3 PARSE BUFFER VIEWS */ + err = _parse_buffer_views(state); + if (err != OK) + return NULL; + + /* STEP 4 PARSE ACCESSORS */ + err = _parse_accessors(state); + if (err != OK) + return NULL; + + /* STEP 5 PARSE IMAGES */ + err = _parse_images(state, p_path.get_base_dir()); + if (err != OK) + return NULL; + + /* STEP 6 PARSE TEXTURES */ + err = _parse_textures(state); + if (err != OK) + return NULL; + + /* STEP 7 PARSE TEXTURES */ + err = _parse_materials(state); + if (err != OK) + return NULL; + + /* STEP 8 PARSE MESHES (we have enough info now) */ + err = _parse_meshes(state); + if (err != OK) + return NULL; + + /* STEP 9 PARSE SKINS */ + err = _parse_skins(state); + if (err != OK) + return NULL; + + /* STEP 10 PARSE CAMERAS */ + err = _parse_cameras(state); + if (err != OK) + return NULL; + + _assign_scene_names(state); + + Spatial *scene = _generate_scene(state); + + return scene; +} + +Ref<Animation> EditorSceneImporterGLTF::import_animation(const String &p_path, uint32_t p_flags) { + + return Ref<Animation>(); +} + +EditorSceneImporterGLTF::EditorSceneImporterGLTF() { +} diff --git a/editor/import/editor_scene_importer_gltf.h b/editor/import/editor_scene_importer_gltf.h new file mode 100644 index 0000000000..ab6dc6939a --- /dev/null +++ b/editor/import/editor_scene_importer_gltf.h @@ -0,0 +1,258 @@ +#ifndef EDITOR_SCENE_IMPORTER_GLTF_H +#define EDITOR_SCENE_IMPORTER_GLTF_H + +#include "editor/import/resource_importer_scene.h" +#include "scene/3d/skeleton.h" +#include "scene/3d/spatial.h" + +class EditorSceneImporterGLTF : public EditorSceneImporter { + + GDCLASS(EditorSceneImporterGLTF, EditorSceneImporter); + + enum { + ARRAY_BUFFER = 34962, + ELEMENT_ARRAY_BUFFER = 34963, + + TYPE_BYTE = 5120, + TYPE_UNSIGNED_BYTE = 5121, + TYPE_SHORT = 5122, + TYPE_UNSIGNED_SHORT = 5123, + TYPE_UNSIGNED_INT = 5125, + TYPE_FLOAT = 5126, + + COMPONENT_TYPE_BYTE = 5120, + COMPONENT_TYPE_UNSIGNED_BYTE = 5121, + COMPONENT_TYPE_SHORT = 5122, + COMPONENT_TYPE_UNSIGNED_SHORT = 5123, + COMPONENT_TYPE_INT = 5125, + COMPONENT_TYPE_FLOAT = 5126, + + }; + + String _get_component_type_name(uint32_t p_component); + int _get_component_type_size(int component_type); + + enum GLTFType { + TYPE_SCALAR, + TYPE_VEC2, + TYPE_VEC3, + TYPE_VEC4, + TYPE_MAT2, + TYPE_MAT3, + TYPE_MAT4, + }; + + String _get_type_name(GLTFType p_component); + + struct GLTFNode { + //matrices need to be transformed to this + int parent; + + Transform xform; + String name; + + int mesh; + int camera; + int skin; + int skeleton_skin; + int child_of_skeleton; // put as children of skeleton + Vector<int> skeleton_children; //skeleton put as children of this + + int joint_skin; + int joint_bone; + + //keep them for animation + Vector3 translation; + Quat rotation; + Vector3 scale; + + Vector<int> children; + + GLTFNode() { + joint_skin = -1; + joint_bone = -1; + child_of_skeleton = -1; + skeleton_skin = -1; + mesh = -1; + camera = -1; + parent = -1; + scale = Vector3(1, 1, 1); + } + }; + + struct GLTFBufferView { + + int buffer; + int byte_offset; + int byte_length; + int byte_stride; + bool indices; + //matrices need to be transformed to this + + GLTFBufferView() { + buffer = 0; + byte_offset = 0; + byte_length = 0; + byte_stride = 0; + indices = false; + } + }; + + struct GLTFAccessor { + + int buffer_view; + int byte_offset; + int component_type; + bool normalized; + int count; + GLTFType type; + float min; + float max; + int sparse_count; + int sparse_indices_buffer_view; + int sparse_indices_byte_offset; + int sparse_indices_component_type; + int sparse_values_buffer_view; + int sparse_values_byte_offset; + + //matrices need to be transformed to this + + GLTFAccessor() { + buffer_view = 0; + byte_offset = 0; + component_type = 0; + normalized = false; + count = 0; + min = 0; + max = 0; + sparse_count = 0; + sparse_indices_byte_offset = 0; + sparse_values_byte_offset = 0; + } + }; + struct GLTFTexture { + int src_image; + }; + + struct GLTFSkin { + + String name; + struct Bone { + Transform inverse_bind; + int node; + }; + + int skeleton; + Vector<Bone> bones; + + //matrices need to be transformed to this + + GLTFSkin() { + skeleton = -1; + } + }; + + struct GLTFMesh { + Ref<ArrayMesh> mesh; + Vector<float> blend_weights; + }; + + struct GLTFCamera { + + bool perspective; + float fov_size; + float zfar; + float znear; + + GLTFCamera() { + perspective = true; + fov_size = 65; + zfar = 500; + znear = 0.1; + } + }; + + struct GLTFState { + + Dictionary json; + int major_version; + int minor_version; + Vector<uint8_t> gfb_data; + + Vector<GLTFNode *> nodes; + Vector<Vector<uint8_t> > buffers; + Vector<GLTFBufferView> buffer_views; + Vector<GLTFAccessor> accessors; + + Vector<GLTFMesh> meshes; //meshes are loaded directly, no reason not to. + Vector<Ref<Material> > materials; + + String scene_name; + Vector<int> root_nodes; + + Vector<GLTFTexture> textures; + Vector<Ref<Texture> > images; + + Vector<GLTFSkin> skins; + Vector<GLTFCamera> cameras; + + Set<String> unique_names; + + Map<int, Vector<int> > skin_users; //cache skin users + + ~GLTFState() { + for (int i = 0; i < nodes.size(); i++) { + memdelete(nodes[i]); + } + } + }; + + String _gen_unique_name(GLTFState &state, const String &p_name); + + Ref<Texture> _get_texture(GLTFState &state, int p_texture); + + Error _parse_json(const String &p_path, GLTFState &state); + + Error _parse_scenes(GLTFState &state); + Error _parse_nodes(GLTFState &state); + Error _parse_buffers(GLTFState &state, const String &p_base_path); + Error _parse_buffer_views(GLTFState &state); + GLTFType _get_type_from_str(const String &p_string); + Error _parse_accessors(GLTFState &state); + Error _decode_buffer_view(GLTFState &state, int p_buffer_view, double *dst, int skip_every, int skip_bytes, int element_size, int count, GLTFType type, int component_count, int component_type, int component_size, bool normalized, int byte_offset, bool for_vertex); + Vector<double> _decode_accessor(GLTFState &state, int p_accessor, bool p_for_vertex); + PoolVector<float> _decode_accessor_as_floats(GLTFState &state, int p_accessor, bool p_for_vertex); + PoolVector<int> _decode_accessor_as_ints(GLTFState &state, int p_accessor, bool p_for_vertex); + PoolVector<Vector2> _decode_accessor_as_vec2(GLTFState &state, int p_accessor, bool p_for_vertex); + PoolVector<Vector3> _decode_accessor_as_vec3(GLTFState &state, int p_accessor, bool p_for_vertex); + PoolVector<Color> _decode_accessor_as_color(GLTFState &state, int p_accessor, bool p_for_vertex); + Vector<Transform2D> _decode_accessor_as_xform2d(GLTFState &state, int p_accessor, bool p_for_vertex); + Vector<Basis> _decode_accessor_as_basis(GLTFState &state, int p_accessor, bool p_for_vertex); + Vector<Transform> _decode_accessor_as_xform(GLTFState &state, int p_accessor, bool p_for_vertex); + + void _generate_bone(GLTFState &state, int p_node, Vector<Skeleton *> &skeletons, int p_parent_bone); + void _generate_node(GLTFState &state, int p_node, Node *p_parent, Node *p_owner, Vector<Skeleton *> &skeletons); + Spatial *_generate_scene(GLTFState &state); + + Error _parse_meshes(GLTFState &state); + Error _parse_images(GLTFState &state, const String &p_base_path); + Error _parse_textures(GLTFState &state); + + Error _parse_materials(GLTFState &state); + + Error _parse_skins(GLTFState &state); + + Error _parse_cameras(GLTFState &state); + + void _assign_scene_names(GLTFState &state); + +public: + virtual uint32_t get_import_flags() const; + virtual void get_extensions(List<String> *r_extensions) const; + virtual Node *import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, List<String> *r_missing_deps = NULL, Error *r_err = NULL); + virtual Ref<Animation> import_animation(const String &p_path, uint32_t p_flags); + + EditorSceneImporterGLTF(); +}; + +#endif // EDITOR_SCENE_IMPORTER_GLTF_H diff --git a/editor/project_manager.cpp b/editor/project_manager.cpp index 87340b1fe8..82f17b80d5 100644 --- a/editor/project_manager.cpp +++ b/editor/project_manager.cpp @@ -935,10 +935,10 @@ void ProjectManager::_open_project_confirm() { List<String> args; - args.push_back("--path"); + args.push_back("-path"); args.push_back(path); - args.push_back("--editor"); + args.push_back("-editor"); String exec = OS::get_singleton()->get_executable_path(); @@ -977,7 +977,7 @@ void ProjectManager::_run_project_confirm() { List<String> args; - args.push_back("--path"); + args.push_back("-path"); args.push_back(path); String exec = OS::get_singleton()->get_executable_path(); diff --git a/main/main.cpp b/main/main.cpp index c2058ee8d4..ed6ed019f4 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -127,62 +127,62 @@ static String unescape_cmdline(const String &p_str) { void Main::print_help(const char *p_binary) { OS::get_singleton()->print(VERSION_FULL_NAME " (c) 2008-2017 Juan Linietsky, Ariel Manzur.\n"); - OS::get_singleton()->print("Usage: %s [options] [path to scene or 'project.godot' file]\n", p_binary); + OS::get_singleton()->print("Usage: %s [options] [scene]\n", p_binary); OS::get_singleton()->print("Options:\n"); - OS::get_singleton()->print(" -h, --help Display this help message.\n"); - OS::get_singleton()->print(" --path <directory> Path to the project (<directory> must contain a 'project.godot' file).\n"); + OS::get_singleton()->print("\t-path [dir] : Path to a game, containing project.godot\n"); #ifdef TOOLS_ENABLED - OS::get_singleton()->print(" -e, --editor Bring up the editor instead of running the scene.\n"); + OS::get_singleton()->print("\t-e,-editor : Bring up the editor instead of running the scene.\n"); #endif - OS::get_singleton()->print(" --test <test> Run a test ("); + OS::get_singleton()->print("\t-test [test] : Run a test.\n"); + OS::get_singleton()->print("\t\t("); const char **test_names = tests_get_names(); const char *coma = ""; while (*test_names) { - OS::get_singleton()->print("%s'%s'", coma, *test_names); + OS::get_singleton()->print("%s%s", coma, *test_names); test_names++; coma = ", "; } - OS::get_singleton()->print(").\n"); - - OS::get_singleton()->print(" -r, --resolution <W>x<H> Request window resolution.\n"); - OS::get_singleton()->print(" -p, --position <X>,<Y> Request window position.\n"); - OS::get_singleton()->print(" -f, --fullscreen Request fullscreen mode.\n"); - OS::get_singleton()->print(" -m, --maximized Request a maximized window.\n"); - OS::get_singleton()->print(" -w, --windowed Request windowed mode.\n"); - OS::get_singleton()->print(" --video-driver <driver> Video driver ("); + OS::get_singleton()->print(")\n"); + + OS::get_singleton()->print("\t-r WIDTHxHEIGHT\t : Request Window Resolution\n"); + OS::get_singleton()->print("\t-p XxY\t : Request Window Position\n"); + OS::get_singleton()->print("\t-f\t\t : Request Fullscreen\n"); + OS::get_singleton()->print("\t-mx\t\t Request Maximized\n"); + OS::get_singleton()->print("\t-w\t\t Request Windowed\n"); + OS::get_singleton()->print("\t-vd DRIVER\t : Video Driver ("); for (int i = 0; i < OS::get_singleton()->get_video_driver_count(); i++) { if (i != 0) OS::get_singleton()->print(", "); - OS::get_singleton()->print("'%s'", OS::get_singleton()->get_video_driver_name(i)); + OS::get_singleton()->print("%s", OS::get_singleton()->get_video_driver_name(i)); } - OS::get_singleton()->print(").\n"); - OS::get_singleton()->print(" --low-dpi Force low-DPI mode (macOS only).\n"); + OS::get_singleton()->print(")\n"); + OS::get_singleton()->print("\t-ldpi\t : Force low-dpi mode (OSX Only)\n"); - OS::get_singleton()->print(" --audio-driver <driver> Audio driver ("); + OS::get_singleton()->print("\t-ad DRIVER\t : Audio Driver ("); for (int i = 0; i < OS::get_singleton()->get_audio_driver_count(); i++) { if (i != 0) OS::get_singleton()->print(", "); - OS::get_singleton()->print("'%s'", OS::get_singleton()->get_audio_driver_name(i)); + OS::get_singleton()->print("%s", OS::get_singleton()->get_audio_driver_name(i)); } - OS::get_singleton()->print(").\n"); - OS::get_singleton()->print(" --render-thread <mode> Render thread mode ('unsafe', 'safe', 'separate').\n"); - OS::get_singleton()->print(" -s, --script <script> Run a script.\n"); - OS::get_singleton()->print(" -d, --debug Debug (local stdout debugger).\n"); - OS::get_singleton()->print(" -r, --remote-debug <address> Remote debug (<ip>:<port> host address).\n"); - OS::get_singleton()->print(" --frame-delay <ms> Simulate high CPU load (delay each frame by <ms> milliseconds).\n"); - OS::get_singleton()->print(" --time-scale <scale> Force time scale (higher values are faster, 1.0 is normal speed).\n"); - OS::get_singleton()->print(" -b, --breakpoints Breakpoint list as source::line comma-separated pairs, no spaces (use %%20, %%2C, ... instead).\n"); - OS::get_singleton()->print(" -v, --verbose Use verbose stdout mode.\n"); - OS::get_singleton()->print(" -l, --language <locale> Use a specific locale (<locale> being a two-letter code).\n"); - OS::get_singleton()->print(" --remote-fs <host/IP>[:<port>] Remote filesystem.\n"); - OS::get_singleton()->print(" --remote-fs-password <password> Password for remote filesystem.\n"); + OS::get_singleton()->print(")\n"); + OS::get_singleton()->print("\t-rthread <mode>\t : Render Thread Mode ('unsafe', 'safe', 'separate').\n"); + OS::get_singleton()->print("\t-s,-script [script] : Run a script.\n"); + OS::get_singleton()->print("\t-d,-debug : Debug (local stdout debugger).\n"); + OS::get_singleton()->print("\t-rdebug ADDRESS : Remote debug (<ip>:<port> host address).\n"); + OS::get_singleton()->print("\t-fdelay [msec]: Simulate high CPU load (delay each frame by [msec]).\n"); + OS::get_singleton()->print("\t-timescale [msec]: Simulate high CPU load (delay each frame by [msec]).\n"); + OS::get_singleton()->print("\t-bp : breakpoint list as source::line comma separated pairs, no spaces (%%20,%%2C,etc instead).\n"); + OS::get_singleton()->print("\t-v : Verbose stdout mode\n"); + OS::get_singleton()->print("\t-lang [locale]: Use a specific locale\n"); + OS::get_singleton()->print("\t-rfs <host/ip>[:<port>] : Remote FileSystem.\n"); + OS::get_singleton()->print("\t-rfs_pass <password> : Password for Remote FileSystem.\n"); #ifdef TOOLS_ENABLED - OS::get_singleton()->print(" --doctool <file> Dump the whole engine API to <file> in XML format. If <file> exists, it will be merged.\n"); - OS::get_singleton()->print(" --no-docbase Disallow dumping the base types (used with --doctool).\n"); - OS::get_singleton()->print(" --export <target> Export the project using the given export target.\n"); + OS::get_singleton()->print("\t-doctool FILE: Dump the whole engine api to FILE in XML format. If FILE exists, it will be merged.\n"); + OS::get_singleton()->print("\t-nodocbase: Disallow dump the base types (used with -doctool).\n"); + OS::get_singleton()->print("\t-export [target] Export the project using given export target.\n"); #endif } @@ -281,14 +281,14 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph List<String>::Element *N = I->next(); - if (I->get() == "--noop") { + if (I->get() == "-noop") { // no op - } else if (I->get() == "-h" || I->get() == "--help" || I->get() == "/?") { // display help + } else if (I->get() == "-h" || I->get() == "--help" || I->get() == "/?") { // resolution goto error; - } else if (I->get() == "-r" || I->get() == "--resolution") { // force resolution + } else if (I->get() == "-r") { // resolution if (I->next()) { @@ -296,7 +296,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph if (vm.find("x") == -1) { // invalid parameter format - OS::get_singleton()->print("Invalid resolution argument: %s\n", vm.utf8().get_data()); + OS::get_singleton()->print("Invalid -r argument: %s\n", vm.utf8().get_data()); goto error; } @@ -305,7 +305,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph if (w == 0 || h == 0) { - OS::get_singleton()->print("Invalid resolution, width and height must be above 0\n"); + OS::get_singleton()->print("Invalid -r resolution, x and y must be >0\n"); goto error; } @@ -315,66 +315,66 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph N = I->next()->next(); } else { - OS::get_singleton()->print("Invalid resolution argument, needs resolution\n"); + OS::get_singleton()->print("Invalid -p argument, needs resolution\n"); goto error; } - } else if (I->get() == "-p" || I->get() == "--position") { // position + } else if (I->get() == "-p") { // position if (I->next()) { String vm = I->next()->get(); - if (vm.find(",") == -1) { // invalid parameter format + if (vm.find("x") == -1) { // invalid parameter format - OS::get_singleton()->print("Invalid position argument: %s\n", vm.utf8().get_data()); + OS::get_singleton()->print("Invalid -p argument: %s\n", vm.utf8().get_data()); goto error; } - int x = vm.get_slice(",", 0).to_int(); - int y = vm.get_slice(",", 1).to_int(); + int x = vm.get_slice("x", 0).to_int(); + int y = vm.get_slice("x", 1).to_int(); init_custom_pos = Point2(x, y); init_use_custom_pos = true; N = I->next()->next(); } else { - OS::get_singleton()->print("Invalid position argument, needs position\n"); + OS::get_singleton()->print("Invalid -r argument, needs position\n"); goto error; } - } else if (I->get() == "-m" || I->get() == "--maximized") { // force maximized window + } else if (I->get() == "-mx") { // video driver init_maximized = true; - } else if (I->get() == "-w" || I->get() == "--windowed") { // force windowed window + } else if (I->get() == "-w") { // video driver init_windowed = true; - } else if (I->get() == "--profile") { // enable profiler + } else if (I->get() == "-profile") { // video driver use_debug_profiler = true; - } else if (I->get() == "--video-driver") { // force video driver + } else if (I->get() == "-vd") { // video driver if (I->next()) { video_driver = I->next()->get(); N = I->next()->next(); } else { - OS::get_singleton()->print("Invalid --video-driver argument, needs driver name\n"); + OS::get_singleton()->print("Invalid -cd argument, needs driver name\n"); goto error; } - } else if (I->get() == "-l" || I->get() == "--language") { // language + } else if (I->get() == "-lang") { // language if (I->next()) { locale = I->next()->get(); N = I->next()->next(); } else { - OS::get_singleton()->print("Invalid language argument, needs language code\n"); + OS::get_singleton()->print("Invalid -lang argument, needs language code\n"); goto error; } - } else if (I->get() == "--low-dpi") { // force low DPI + } else if (I->get() == "-ldpi") { // language force_lowdpi = true; - } else if (I->get() == "--remote-fs") { // remote filesystem + } else if (I->get() == "-rfs") { // language if (I->next()) { @@ -383,7 +383,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } else { goto error; } - } else if (I->get() == "--remote-fs-password") { // remote filesystem password + } else if (I->get() == "-rfs_pass") { // language if (I->next()) { @@ -392,7 +392,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } else { goto error; } - } else if (I->get() == "--render-thread") { // rendering thread + } else if (I->get() == "-rthread") { // language if (I->next()) { @@ -408,7 +408,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph goto error; } - } else if (I->get() == "--audio-driver") { // audio driver + } else if (I->get() == "-ad") { // video driver if (I->next()) { @@ -418,22 +418,22 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph goto error; } - } else if (I->get() == "-f" || I->get() == "--fullscreen") { // force fullscreen + } else if (I->get() == "-f") { // fullscreen //video_mode.fullscreen=false; init_fullscreen = true; - } else if (I->get() == "-e" || I->get() == "--editor") { // starts editor + } else if (I->get() == "-e" || I->get() == "-editor") { // fonud editor editor = true; - } else if (I->get() == "--no-window") { // disable window creation + } else if (I->get() == "-nowindow") { // fullscreen OS::get_singleton()->set_no_window_mode(true); - } else if (I->get() == "--quiet") { // quieter output + } else if (I->get() == "-quiet") { // fullscreen quiet_stdout = true; - } else if (I->get() == "-v" || I->get() == "--verbose") { // verbose output + } else if (I->get() == "-v") { // fullscreen OS::get_singleton()->_verbose_stdout = true; - } else if (I->get() == "--path") { // set path of project to start or edit + } else if (I->get() == "-path") { // resolution if (I->next()) { @@ -464,7 +464,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph #ifdef TOOLS_ENABLED editor = true; #endif - } else if (I->get() == "-b" || I->get() == "--breakpoints") { // add breakpoints + } else if (I->get() == "-bp") { // /breakpoints if (I->next()) { @@ -475,7 +475,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph goto error; } - } else if (I->get() == "--frame-delay") { // force frame delay + } else if (I->get() == "-fdelay") { // resolution if (I->next()) { @@ -485,7 +485,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph goto error; } - } else if (I->get() == "--time-scale") { // force time scale + } else if (I->get() == "-timescale") { // resolution if (I->next()) { @@ -495,7 +495,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph goto error; } - } else if (I->get() == "--pack") { + } else if (I->get() == "-pack") { if (I->next()) { @@ -506,7 +506,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph goto error; }; - } else if (I->get() == "--main-pack") { + } else if (I->get() == "-main_pack") { if (I->next()) { @@ -517,15 +517,15 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph goto error; }; - } else if (I->get() == "--debug" || I->get() == "-d") { + } else if (I->get() == "-debug" || I->get() == "-d") { debug_mode = "local"; #ifdef DEBUG_ENABLED - } else if (I->get() == "--debug-collision") { + } else if (I->get() == "-debugcol" || I->get() == "-dc") { debug_collisions = true; - } else if (I->get() == "--debug-navigation") { + } else if (I->get() == "-debugnav" || I->get() == "-dn") { debug_navigation = true; #endif - } else if (I->get() == "--editor-scene") { + } else if (I->get() == "-editor_scene") { if (I->next()) { @@ -534,7 +534,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph goto error; } - } else if (I->get() == "-r" || I->get() == "--remote-debug") { + } else if (I->get() == "-rdebug") { if (I->next()) { debug_mode = "remote"; @@ -547,7 +547,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } else { goto error; } - } else if (I->get() == "--editor-pid") { + } else if (I->get() == "-epid") { if (I->next()) { int editor_pid = I->next()->get().to_int(); @@ -660,7 +660,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } if (editor) { - main_args.push_back("--editor"); + main_args.push_back("-editor"); init_maximized = true; use_custom_res = false; } @@ -1057,13 +1057,13 @@ bool Main::start() { List<String> args = OS::get_singleton()->get_cmdline_args(); for (int i = 0; i < args.size(); i++) { //parameters that do not have an argument to the right - if (args[i] == "--no-docbase") { + if (args[i] == "-nodocbase") { doc_base = false; - } else if (args[i] == "--no-quit") { + } else if (args[i] == "-noquit") { noquit = true; - } else if (args[i] == "--editor" || args[i] == "-e") { + } else if (args[i] == "-editor" || args[i] == "-e") { editor = true; - } else if (args[i] == "-P" || args[i] == "--project-manager") { + } else if (args[i] == "-pm" || args[i] == "-project_manager") { project_manager_request = true; } else if (args[i].length() && args[i][0] != '-' && game_path == "") { game_path = args[i]; @@ -1071,27 +1071,27 @@ bool Main::start() { //parameters that have an argument to the right else if (i < (args.size() - 1)) { bool parsed_pair = true; - if (args[i] == "--doctool") { + if (args[i] == "-doctool") { doc_tool = args[i + 1]; for (int j = i + 2; j < args.size(); j++) removal_docs.push_back(args[j]); - } else if (args[i] == "--script" || args[i] == "-s") { + } else if (args[i] == "-script" || args[i] == "-s") { script = args[i + 1]; - } else if (args[i] == "--level" || args[i] == "-l") { + } else if (args[i] == "-level" || args[i] == "-l") { Engine::get_singleton()->_custom_level = args[i + 1]; - } else if (args[i] == "--test") { + } else if (args[i] == "-test") { test = args[i + 1]; - } else if (args[i] == "--export") { + } else if (args[i] == "-export") { editor = true; //needs editor _export_platform = args[i + 1]; - } else if (args[i] == "--export-debug") { + } else if (args[i] == "-export_debug") { editor = true; //needs editor _export_platform = args[i + 1]; export_debug = true; - } else if (args[i] == "--import") { + } else if (args[i] == "-import") { editor = true; //needs editor _import = args[i + 1]; - } else if (args[i] == "--import-script") { + } else if (args[i] == "-import_script") { editor = true; //needs editor _import_script = args[i + 1]; } else { @@ -1139,7 +1139,7 @@ bool Main::start() { if (_export_platform != "") { if (game_path == "") { String err = "Command line param "; - err += export_debug ? "--export-debug" : "--export"; + err += export_debug ? "-export_debug" : "-export"; err += " passed but no destination path given.\n"; err += "Please specify the binary's file path to export to. Aborting export."; ERR_PRINT(err.utf8().get_data()); diff --git a/methods.py b/methods.py index abd87c07d7..30a1f3caed 100644 --- a/methods.py +++ b/methods.py @@ -244,6 +244,7 @@ def build_glsl_header(filename): fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Color& p_color) { _FU GLfloat col[4]={p_color.r,p_color.g,p_color.b,p_color.a}; glUniform4fv(get_uniform(p_uniform),1,col); }\n\n") fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Vector2& p_vec2) { _FU GLfloat vec2[2]={p_vec2.x,p_vec2.y}; glUniform2fv(get_uniform(p_uniform),1,vec2); }\n\n") fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Vector3& p_vec3) { _FU GLfloat vec3[3]={p_vec3.x,p_vec3.y,p_vec3.z}; glUniform3fv(get_uniform(p_uniform),1,vec3); }\n\n") + fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Plane& p_plane) { _FU GLfloat plane[4]={p_plane.normal.x,p_plane.normal.y,p_plane.normal.z,p_plane.d}; glUniform4fv(get_uniform(p_uniform),1,plane); }\n\n") fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, float p_a, float p_b) { _FU glUniform2f(get_uniform(p_uniform),p_a,p_b); }\n\n") fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, float p_a, float p_b, float p_c) { _FU glUniform3f(get_uniform(p_uniform),p_a,p_b,p_c); }\n\n") fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, float p_a, float p_b, float p_c, float p_d) { _FU glUniform4f(get_uniform(p_uniform),p_a,p_b,p_c,p_d); }\n\n") diff --git a/scene/2d/tile_map.cpp b/scene/2d/tile_map.cpp index 9d70b75027..02dcc7d059 100644 --- a/scene/2d/tile_map.cpp +++ b/scene/2d/tile_map.cpp @@ -1144,6 +1144,20 @@ Array TileMap::get_used_cells() const { return a; } +Array TileMap::get_used_cells_by_id(int p_id) const { + + Array a; + for (Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) { + + if (E->value().id == p_id) { + Vector2 p(E->key().x, E->key().y); + a.push_back(p); + } + } + + return a; +} + Rect2 TileMap::get_used_rect() { // Not const because of cache if (used_size_cache_dirty) { @@ -1262,6 +1276,7 @@ void TileMap::_bind_methods() { ClassDB::bind_method(D_METHOD("clear"), &TileMap::clear); ClassDB::bind_method(D_METHOD("get_used_cells"), &TileMap::get_used_cells); + ClassDB::bind_method(D_METHOD("get_used_cells_by_id", "id"), &TileMap::get_used_cells_by_id); ClassDB::bind_method(D_METHOD("get_used_rect"), &TileMap::get_used_rect); ClassDB::bind_method(D_METHOD("map_to_world", "mappos", "ignore_half_ofs"), &TileMap::map_to_world, DEFVAL(false)); diff --git a/scene/2d/tile_map.h b/scene/2d/tile_map.h index 3468854a61..082e9d1018 100644 --- a/scene/2d/tile_map.h +++ b/scene/2d/tile_map.h @@ -270,6 +270,7 @@ public: bool is_y_sort_mode_enabled() const; Array get_used_cells() const; + Array get_used_cells_by_id(int p_id) const; Rect2 get_used_rect(); // Not const because of cache void set_occluder_light_mask(int p_mask); diff --git a/scene/3d/arvr_nodes.cpp b/scene/3d/arvr_nodes.cpp index 5f2a720748..3c99f7fb3a 100644 --- a/scene/3d/arvr_nodes.cpp +++ b/scene/3d/arvr_nodes.cpp @@ -98,6 +98,7 @@ void ARVRController::_notification(int p_what) { is_active = false; button_states = 0; } else { + is_active = true; set_transform(tracker->get_transform(true)); int joy_id = tracker->get_joy_id(); @@ -231,6 +232,118 @@ ARVRController::~ARVRController(){ //////////////////////////////////////////////////////////////////////////////////////////////////// +void ARVRAnchor::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_ENTER_TREE: { + set_process_internal(true); + }; break; + case NOTIFICATION_EXIT_TREE: { + set_process_internal(false); + }; break; + case NOTIFICATION_INTERNAL_PROCESS: { + // get our ARVRServer + ARVRServer *arvr_server = ARVRServer::get_singleton(); + ERR_FAIL_NULL(arvr_server); + + // find the tracker for our anchor + ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_ANCHOR, anchor_id); + if (tracker == NULL) { + // this anchor is currently not available + is_active = false; + } else { + is_active = true; + Transform transform; + + // we'll need our world_scale + real_t world_scale = arvr_server->get_world_scale(); + + // get our info from our tracker + transform.basis = tracker->get_orientation(); + transform.origin = tracker->get_position(); // <-- already adjusted to world scale + + // our basis is scaled to the size of the plane the anchor is tracking + // extract the size from our basis and reset the scale + size = transform.basis.get_scale() * world_scale; + transform.basis.set_scale(Vector3(1.0, 1.0, 1.0)); + + // apply our reference frame and set our transform + set_transform(arvr_server->get_reference_frame() * transform); + }; + }; break; + default: + break; + }; +}; + +void ARVRAnchor::_bind_methods() { + + ClassDB::bind_method(D_METHOD("set_anchor_id", "anchor_id"), &ARVRAnchor::set_anchor_id); + ClassDB::bind_method(D_METHOD("get_anchor_id"), &ARVRAnchor::get_anchor_id); + ADD_PROPERTY(PropertyInfo(Variant::INT, "anchor_id"), "set_anchor_id", "get_anchor_id"); + ClassDB::bind_method(D_METHOD("get_anchor_name"), &ARVRAnchor::get_anchor_name); + + ClassDB::bind_method(D_METHOD("get_is_active"), &ARVRAnchor::get_is_active); + ClassDB::bind_method(D_METHOD("get_size"), &ARVRAnchor::get_size); +}; + +void ARVRAnchor::set_anchor_id(int p_anchor_id) { + // we don't check any bounds here, this anchor may not yet be active and just be a place holder until it is. + anchor_id = p_anchor_id; +}; + +int ARVRAnchor::get_anchor_id(void) const { + return anchor_id; +}; + +Vector3 ARVRAnchor::get_size() const { + return size; +}; + +String ARVRAnchor::get_anchor_name(void) const { + // get our ARVRServer + ARVRServer *arvr_server = ARVRServer::get_singleton(); + ERR_FAIL_NULL_V(arvr_server, String()); + + ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_ANCHOR, anchor_id); + if (tracker == NULL) { + return String("Not connected"); + }; + + return tracker->get_name(); +}; + +bool ARVRAnchor::get_is_active() const { + return is_active; +}; + +String ARVRAnchor::get_configuration_warning() const { + if (!is_visible() || !is_inside_tree()) + return String(); + + // must be child node of ARVROrigin! + ARVROrigin *origin = get_parent()->cast_to<ARVROrigin>(); + if (origin == NULL) { + return TTR("ARVRAnchor must have an ARVROrigin node as its parent"); + }; + + if (anchor_id == 0) { + return TTR("The anchor id must not be 0 or this anchor will not be bound to an actual anchor"); + }; + + return String(); +}; + +ARVRAnchor::ARVRAnchor() { + anchor_id = 0; + is_active = true; +}; + +ARVRAnchor::~ARVRAnchor(){ + // nothing to do here yet for now.. +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + String ARVROrigin::get_configuration_warning() const { if (!is_visible() || !is_inside_tree()) return String(); diff --git a/scene/3d/arvr_nodes.h b/scene/3d/arvr_nodes.h index 3dab263317..936519126b 100644 --- a/scene/3d/arvr_nodes.h +++ b/scene/3d/arvr_nodes.h @@ -39,7 +39,7 @@ **/ /* - ARVRCamera is a subclass of camera which will register itself with its parent ARVROrigin and as a result is automatically positioned + ARVRCamera is a subclass of camera which will register itself with its parent ARVROrigin and as a result is automatically positioned */ class ARVRCamera : public Camera { @@ -56,9 +56,9 @@ public: }; /* - ARVRController is a helper node that automatically updates it's position based on tracker data. + ARVRController is a helper node that automatically updates it's position based on tracker data. - It must be a child node of our ARVROrigin node + It must be a child node of our ARVROrigin node */ class ARVRController : public Spatial { @@ -92,6 +92,37 @@ public: }; /* + ARVRAnchor is a helper node that automatically updates it's position based on anchor data, it represents a real world location. + It must be a child node of our ARVROrigin node +*/ + +class ARVRAnchor : public Spatial { + GDCLASS(ARVRAnchor, Spatial); + +private: + int anchor_id; + bool is_active; + Vector3 size; + +protected: + void _notification(int p_what); + static void _bind_methods(); + +public: + void set_anchor_id(int p_anchor_id); + int get_anchor_id(void) const; + String get_anchor_name(void) const; + + bool get_is_active() const; + Vector3 get_size() const; + + String get_configuration_warning() const; + + ARVRAnchor(); + ~ARVRAnchor(); +}; + +/* ARVROrigin is special spatial node that acts as our origin point mapping our real world center of our tracking volume into our virtual world. It is this point that you will move around the world as the player 'moves while standing still', i.e. the player moves through teleporting or controller inputs as opposed to physically moving. diff --git a/scene/gui/item_list.cpp b/scene/gui/item_list.cpp index 97f49da2be..34533375b2 100644 --- a/scene/gui/item_list.cpp +++ b/scene/gui/item_list.cpp @@ -431,7 +431,7 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { if (mb.is_valid() && (mb->get_button_index() == BUTTON_LEFT || (allow_rmb_select && mb->get_button_index() == BUTTON_RIGHT)) && mb->is_pressed()) { search_string = ""; //any mousepress cancels - Vector2 pos(mb->get_position().x, mb->get_position().y); + Vector2 pos = mb->get_position(); Ref<StyleBox> bg = get_stylebox("bg"); pos -= bg->get_offset(); pos.y += scroll_bar->get_value(); @@ -475,7 +475,7 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { if (mb->get_button_index() == BUTTON_RIGHT) { - emit_signal("item_rmb_selected", i, Vector2(mb->get_position().x, mb->get_position().y)); + emit_signal("item_rmb_selected", i, pos); } } else { @@ -486,7 +486,7 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { if (items[i].selected && mb->get_button_index() == BUTTON_RIGHT) { - emit_signal("item_rmb_selected", i, Vector2(mb->get_position().x, mb->get_position().y)); + emit_signal("item_rmb_selected", i, pos); } else { bool selected = !items[i].selected; @@ -501,7 +501,7 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { if (mb->get_button_index() == BUTTON_RIGHT) { - emit_signal("item_rmb_selected", i, Vector2(mb->get_position().x, mb->get_position().y)); + emit_signal("item_rmb_selected", i, pos); } else if (/*select_mode==SELECT_SINGLE &&*/ mb->is_doubleclick()) { emit_signal("item_activated", i); diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index f286bfb81a..3e6d80d314 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -409,6 +409,7 @@ void register_scene_types() { ClassDB::register_class<Listener>(); ClassDB::register_class<ARVRCamera>(); ClassDB::register_class<ARVRController>(); + ClassDB::register_class<ARVRAnchor>(); ClassDB::register_class<ARVROrigin>(); ClassDB::register_class<InterpolatedCamera>(); ClassDB::register_class<MeshInstance>(); diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp index 24ec39afe3..5a79e49240 100644 --- a/scene/resources/material.cpp +++ b/scene/resources/material.cpp @@ -234,6 +234,14 @@ void SpatialMaterial::init_shaders() { shader_names->grow = "grow"; + shader_names->metallic_texture_channel = "metallic_texture_channel"; + shader_names->roughness_texture_channel = "roughness_texture_channel"; + shader_names->ao_texture_channel = "ao_texture_channel"; + shader_names->clearcoat_texture_channel = "clearcoat_texture_channel"; + shader_names->rim_texture_channel = "rim_texture_channel"; + shader_names->depth_texture_channel = "depth_texture_channel"; + shader_names->refraction_texture_channel = "refraction_texture_channel"; + shader_names->texture_names[TEXTURE_ALBEDO] = "texture_albedo"; shader_names->texture_names[TEXTURE_METALLIC] = "texture_metallic"; shader_names->texture_names[TEXTURE_ROUGHNESS] = "texture_roughness"; @@ -354,7 +362,9 @@ void SpatialMaterial::_update_shader() { code += "uniform float roughness : hint_range(0,1);\n"; code += "uniform float point_size : hint_range(0,128);\n"; code += "uniform sampler2D texture_metallic : hint_white;\n"; + code += "uniform vec4 metallic_texture_channel;\n"; code += "uniform sampler2D texture_roughness : hint_white;\n"; + code += "uniform vec4 roughness_texture_channel;\n"; if (billboard_mode == BILLBOARD_PARTICLES) { code += "uniform int particles_anim_h_frames;\n"; code += "uniform int particles_anim_v_frames;\n"; @@ -371,6 +381,7 @@ void SpatialMaterial::_update_shader() { if (features[FEATURE_REFRACTION]) { code += "uniform sampler2D texture_refraction;\n"; code += "uniform float refraction : hint_range(-16,16);\n"; + code += "uniform vec4 refraction_texture_channel;\n"; } if (features[FEATURE_NORMAL_MAPPING]) { @@ -393,6 +404,7 @@ void SpatialMaterial::_update_shader() { } if (features[FEATURE_AMBIENT_OCCLUSION]) { code += "uniform sampler2D texture_ambient_occlusion : hint_white;\n"; + code += "uniform vec4 ao_texture_channel;\n"; } if (features[FEATURE_DETAIL]) { @@ -617,15 +629,15 @@ void SpatialMaterial::_update_shader() { code += "\tALBEDO = albedo.rgb * albedo_tex.rgb;\n"; if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += "\tfloat metallic_tex = triplanar_texture(texture_metallic,uv1_power_normal,uv1_world_pos).r;\n"; + code += "\tfloat metallic_tex = dot(triplanar_texture(texture_metallic,uv1_power_normal,uv1_world_pos),metallic_texture_channel);\n"; } else { - code += "\tfloat metallic_tex = texture(texture_metallic,base_uv).r;\n"; + code += "\tfloat metallic_tex = dot(texture(texture_metallic,base_uv),metallic_texture_channel);\n"; } code += "\tMETALLIC = metallic_tex * metallic;\n"; if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += "\tfloat roughness_tex = triplanar_texture(texture_roughness,uv1_power_normal,uv1_world_pos).r;\n"; + code += "\tfloat roughness_tex = dot(triplanar_texture(texture_roughness,uv1_power_normal,uv1_world_pos),roughness_texture_channel);\n"; } else { - code += "\tfloat roughness_tex = texture(texture_roughness,base_uv).r;\n"; + code += "\tfloat roughness_tex = dot(texture(texture_roughness,base_uv),roughness_texture_channel);\n"; } code += "\tROUGHNESS = roughness_tex * roughness;\n"; code += "\tSPECULAR = specular;\n"; @@ -656,7 +668,7 @@ void SpatialMaterial::_update_shader() { code += "\tvec3 ref_normal = NORMAL;\n"; } - code += "\tvec2 ref_ofs = SCREEN_UV - ref_normal.xy * texture(texture_refraction,base_uv).r * refraction;\n"; + code += "\tvec2 ref_ofs = SCREEN_UV - ref_normal.xy * dot(texture(texture_refraction,base_uv),refraction_texture_channel) * refraction;\n"; code += "\tfloat ref_amount = 1.0 - albedo.a * albedo_tex.a;\n"; code += "\tEMISSION += textureLod(SCREEN_TEXTURE,ref_ofs,ROUGHNESS * 8.0).rgb * ref_amount;\n"; code += "\tALBEDO *= 1.0 - ref_amount;\n"; @@ -699,15 +711,15 @@ void SpatialMaterial::_update_shader() { if (features[FEATURE_AMBIENT_OCCLUSION]) { if (flags[FLAG_AO_ON_UV2]) { if (flags[FLAG_UV2_USE_TRIPLANAR]) { - code += "\tAO = triplanar_texture(texture_ambient_occlusion,uv2_power_normal,uv2_world_pos).r;\n"; + code += "\tAO = dot(triplanar_texture(texture_ambient_occlusion,uv2_power_normal,uv2_world_pos),ao_texture_channel);\n"; } else { - code += "\tAO = texture(texture_ambient_occlusion,base_uv2).r;\n"; + code += "\tAO = dot(texture(texture_ambient_occlusion,base_uv2),ao_texture_channel);\n"; } } else { if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += "\tAO = triplanar_texture(texture_ambient_occlusion,uv1_power_normal,uv1_world_pos).r;\n"; + code += "\tAO = dot(triplanar_texture(texture_ambient_occlusion,uv1_power_normal,uv1_world_pos),ao_texture_channel);\n"; } else { - code += "\tAO = texture(texture_ambient_occlusion,base_uv).r;\n"; + code += "\tAO = dot(texture(texture_ambient_occlusion,base_uv),ao_texture_channel);\n"; } } } @@ -1327,6 +1339,58 @@ float SpatialMaterial::get_grow() const { return grow; } +static Plane _get_texture_mask(SpatialMaterial::TextureChannel p_channel) { + static const Plane masks[5] = { + Plane(1, 0, 0, 0), + Plane(0, 1, 0, 0), + Plane(0, 0, 1, 0), + Plane(0, 0, 0, 1), + Plane(0.3333333, 0.3333333, 0.3333333, 0), + }; + + return masks[p_channel]; +} + +void SpatialMaterial::set_metallic_texture_channel(TextureChannel p_channel) { + + metallic_texture_channel = p_channel; + VS::get_singleton()->material_set_param(_get_material(), shader_names->metallic_texture_channel, _get_texture_mask(p_channel)); +} + +SpatialMaterial::TextureChannel SpatialMaterial::get_metallic_texture_channel() const { + return metallic_texture_channel; +} + +void SpatialMaterial::set_roughness_texture_channel(TextureChannel p_channel) { + + roughness_texture_channel = p_channel; + VS::get_singleton()->material_set_param(_get_material(), shader_names->roughness_texture_channel, _get_texture_mask(p_channel)); +} + +SpatialMaterial::TextureChannel SpatialMaterial::get_roughness_texture_channel() const { + return roughness_texture_channel; +} + +void SpatialMaterial::set_ao_texture_channel(TextureChannel p_channel) { + + ao_texture_channel = p_channel; + VS::get_singleton()->material_set_param(_get_material(), shader_names->ao_texture_channel, _get_texture_mask(p_channel)); +} + +SpatialMaterial::TextureChannel SpatialMaterial::get_ao_texture_channel() const { + return ao_texture_channel; +} + +void SpatialMaterial::set_refraction_texture_channel(TextureChannel p_channel) { + + refraction_texture_channel = p_channel; + VS::get_singleton()->material_set_param(_get_material(), shader_names->refraction_texture_channel, _get_texture_mask(p_channel)); +} + +SpatialMaterial::TextureChannel SpatialMaterial::get_refraction_texture_channel() const { + return refraction_texture_channel; +} + void SpatialMaterial::_bind_methods() { ClassDB::bind_method(D_METHOD("set_albedo", "albedo"), &SpatialMaterial::set_albedo); @@ -1455,6 +1519,18 @@ void SpatialMaterial::_bind_methods() { ClassDB::bind_method(D_METHOD("set_grow_enabled", "enable"), &SpatialMaterial::set_grow_enabled); ClassDB::bind_method(D_METHOD("is_grow_enabled"), &SpatialMaterial::is_grow_enabled); + ClassDB::bind_method(D_METHOD("set_metallic_texture_channel", "channel"), &SpatialMaterial::set_metallic_texture_channel); + ClassDB::bind_method(D_METHOD("get_metallic_texture_channel"), &SpatialMaterial::get_metallic_texture_channel); + + ClassDB::bind_method(D_METHOD("set_roughness_texture_channel", "channel"), &SpatialMaterial::set_roughness_texture_channel); + ClassDB::bind_method(D_METHOD("get_roughness_texture_channel"), &SpatialMaterial::get_roughness_texture_channel); + + ClassDB::bind_method(D_METHOD("set_ao_texture_channel", "channel"), &SpatialMaterial::set_ao_texture_channel); + ClassDB::bind_method(D_METHOD("get_ao_texture_channel"), &SpatialMaterial::get_ao_texture_channel); + + ClassDB::bind_method(D_METHOD("set_refraction_texture_channel", "channel"), &SpatialMaterial::set_refraction_texture_channel); + ClassDB::bind_method(D_METHOD("get_refraction_texture_channel"), &SpatialMaterial::get_refraction_texture_channel); + ADD_GROUP("Flags", "flags_"); ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_transparent"), "set_feature", "get_feature", FEATURE_TRANSPARENT); ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_unshaded"), "set_flag", "get_flag", FLAG_UNSHADED); @@ -1490,10 +1566,12 @@ void SpatialMaterial::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::REAL, "metallic", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_metallic", "get_metallic"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "metallic_specular", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_specular", "get_specular"); ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "metallic_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_METALLIC); + ADD_PROPERTY(PropertyInfo(Variant::INT, "metallic_texture_channel", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Gray"), "set_metallic_texture_channel", "get_metallic_texture_channel"); ADD_GROUP("Roughness", "roughness_"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "roughness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_roughness", "get_roughness"); ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "roughness_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_ROUGHNESS); + ADD_PROPERTY(PropertyInfo(Variant::INT, "roughness_texture_channel", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Gray"), "set_roughness_texture_channel", "get_roughness_texture_channel"); ADD_GROUP("Emission", "emission_"); ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "emission_enabled"), "set_feature", "get_feature", FEATURE_EMISSION); @@ -1527,6 +1605,7 @@ void SpatialMaterial::_bind_methods() { ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "ao_enabled"), "set_feature", "get_feature", FEATURE_AMBIENT_OCCLUSION); ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "ao_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_AMBIENT_OCCLUSION); ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "ao_on_uv2"), "set_flag", "get_flag", FLAG_AO_ON_UV2); + ADD_PROPERTY(PropertyInfo(Variant::INT, "ao_texture_channel", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Gray"), "set_ao_texture_channel", "get_ao_texture_channel"); ADD_GROUP("Depth", "depth_"); ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "depth_enabled"), "set_feature", "get_feature", FEATURE_DEPTH_MAPPING); @@ -1545,6 +1624,7 @@ void SpatialMaterial::_bind_methods() { ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "refraction_enabled"), "set_feature", "get_feature", FEATURE_REFRACTION); ADD_PROPERTY(PropertyInfo(Variant::REAL, "refraction_scale", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_refraction", "get_refraction"); ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "refraction_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_REFRACTION); + ADD_PROPERTY(PropertyInfo(Variant::INT, "refraction_texture_channel", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Gray"), "set_refraction_texture_channel", "get_refraction_texture_channel"); ADD_GROUP("Detail", "detail_"); ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "detail_enabled"), "set_feature", "get_feature", FEATURE_DETAIL); @@ -1638,6 +1718,12 @@ void SpatialMaterial::_bind_methods() { BIND_CONSTANT(BILLBOARD_ENABLED); BIND_CONSTANT(BILLBOARD_FIXED_Y); BIND_CONSTANT(BILLBOARD_PARTICLES); + + BIND_CONSTANT(TEXTURE_CHANNEL_RED); + BIND_CONSTANT(TEXTURE_CHANNEL_GREEN); + BIND_CONSTANT(TEXTURE_CHANNEL_BLUE); + BIND_CONSTANT(TEXTURE_CHANNEL_ALPHA); + BIND_CONSTANT(TEXTURE_CHANNEL_GRAYSCALE); } SpatialMaterial::SpatialMaterial() @@ -1672,6 +1758,11 @@ SpatialMaterial::SpatialMaterial() set_particles_anim_v_frames(1); set_particles_anim_loop(false); + set_metallic_texture_channel(TEXTURE_CHANNEL_RED); + set_roughness_texture_channel(TEXTURE_CHANNEL_RED); + set_ao_texture_channel(TEXTURE_CHANNEL_RED); + set_refraction_texture_channel(TEXTURE_CHANNEL_RED); + grow_enabled = false; set_grow(0.0); diff --git a/scene/resources/material.h b/scene/resources/material.h index 7587fc7927..1484b79fc6 100644 --- a/scene/resources/material.h +++ b/scene/resources/material.h @@ -190,6 +190,14 @@ public: BILLBOARD_PARTICLES, }; + enum TextureChannel { + TEXTURE_CHANNEL_RED, + TEXTURE_CHANNEL_GREEN, + TEXTURE_CHANNEL_BLUE, + TEXTURE_CHANNEL_ALPHA, + TEXTURE_CHANNEL_GRAYSCALE + }; + private: union MaterialKey { @@ -283,6 +291,14 @@ private: StringName uv2_blend_sharpness; StringName grow; + StringName metallic_texture_channel; + StringName roughness_texture_channel; + StringName ao_texture_channel; + StringName clearcoat_texture_channel; + StringName rim_texture_channel; + StringName depth_texture_channel; + StringName refraction_texture_channel; + StringName texture_names[TEXTURE_MAX]; }; @@ -342,6 +358,11 @@ private: DiffuseMode diffuse_mode; BillboardMode billboard_mode; + TextureChannel metallic_texture_channel; + TextureChannel roughness_texture_channel; + TextureChannel ao_texture_channel; + TextureChannel refraction_texture_channel; + bool features[FEATURE_MAX]; Ref<Texture> textures[TEXTURE_MAX]; @@ -478,6 +499,15 @@ public: void set_grow(float p_grow); float get_grow() const; + void set_metallic_texture_channel(TextureChannel p_channel); + TextureChannel get_metallic_texture_channel() const; + void set_roughness_texture_channel(TextureChannel p_channel); + TextureChannel get_roughness_texture_channel() const; + void set_ao_texture_channel(TextureChannel p_channel); + TextureChannel get_ao_texture_channel() const; + void set_refraction_texture_channel(TextureChannel p_channel); + TextureChannel get_refraction_texture_channel() const; + static void init_shaders(); static void finish_shaders(); static void flush_changes(); @@ -496,6 +526,7 @@ VARIANT_ENUM_CAST(SpatialMaterial::Flags) VARIANT_ENUM_CAST(SpatialMaterial::DiffuseMode) VARIANT_ENUM_CAST(SpatialMaterial::SpecularMode) VARIANT_ENUM_CAST(SpatialMaterial::BillboardMode) +VARIANT_ENUM_CAST(SpatialMaterial::TextureChannel) ////////////////////// diff --git a/scene/resources/surface_tool.cpp b/scene/resources/surface_tool.cpp index 8478432a04..3932917ebe 100644 --- a/scene/resources/surface_tool.cpp +++ b/scene/resources/surface_tool.cpp @@ -224,30 +224,22 @@ void SurfaceTool::add_index(int p_index) { index_array.push_back(p_index); } -Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) { - - Ref<ArrayMesh> mesh; - if (p_existing.is_valid()) - mesh = p_existing; - else - mesh.instance(); +Array SurfaceTool::commit_to_arrays() { int varr_len = vertex_array.size(); - if (varr_len == 0) - return mesh; - - int surface = mesh->get_surface_count(); - Array a; a.resize(Mesh::ARRAY_MAX); for (int i = 0; i < Mesh::ARRAY_MAX; i++) { - switch (format & (1 << i)) { + if (!(format & (1 << i))) + continue; //not in format + + switch (i) { - case Mesh::ARRAY_FORMAT_VERTEX: - case Mesh::ARRAY_FORMAT_NORMAL: { + case Mesh::ARRAY_VERTEX: + case Mesh::ARRAY_NORMAL: { PoolVector<Vector3> array; array.resize(varr_len); @@ -273,8 +265,8 @@ Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) { } break; - case Mesh::ARRAY_FORMAT_TEX_UV: - case Mesh::ARRAY_FORMAT_TEX_UV2: { + case Mesh::ARRAY_TEX_UV: + case Mesh::ARRAY_TEX_UV2: { PoolVector<Vector2> array; array.resize(varr_len); @@ -299,7 +291,7 @@ Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) { w = PoolVector<Vector2>::Write(); a[i] = array; } break; - case Mesh::ARRAY_FORMAT_TANGENT: { + case Mesh::ARRAY_TANGENT: { PoolVector<float> array; array.resize(varr_len * 4); @@ -323,7 +315,7 @@ Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) { a[i] = array; } break; - case Mesh::ARRAY_FORMAT_COLOR: { + case Mesh::ARRAY_COLOR: { PoolVector<Color> array; array.resize(varr_len); @@ -339,7 +331,7 @@ Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) { w = PoolVector<Color>::Write(); a[i] = array; } break; - case Mesh::ARRAY_FORMAT_BONES: { + case Mesh::ARRAY_BONES: { PoolVector<int> array; array.resize(varr_len * 4); @@ -361,7 +353,7 @@ Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) { a[i] = array; } break; - case Mesh::ARRAY_FORMAT_WEIGHTS: { + case Mesh::ARRAY_WEIGHTS: { PoolVector<float> array; array.resize(varr_len * 4); @@ -383,7 +375,7 @@ Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) { a[i] = array; } break; - case Mesh::ARRAY_FORMAT_INDEX: { + case Mesh::ARRAY_INDEX: { ERR_CONTINUE(index_array.size() == 0); @@ -398,6 +390,7 @@ Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) { } w = PoolVector<int>::Write(); + a[i] = array; } break; @@ -405,6 +398,26 @@ Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) { } } + return a; +} + +Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) { + + Ref<ArrayMesh> mesh; + if (p_existing.is_valid()) + mesh = p_existing; + else + mesh.instance(); + + int varr_len = vertex_array.size(); + + if (varr_len == 0) + return mesh; + + int surface = mesh->get_surface_count(); + + Array a = commit_to_arrays(); + mesh->add_surface_from_arrays(primitive, a); if (material.is_valid()) mesh->surface_set_material(surface, material); @@ -459,12 +472,17 @@ void SurfaceTool::deindex() { vertex_array.push_back(varr[E->get()]); } format &= ~Mesh::ARRAY_FORMAT_INDEX; + index_array.clear(); } void SurfaceTool::_create_list(const Ref<Mesh> &p_existing, int p_surface, List<Vertex> *r_vertex, List<int> *r_index, int &lformat) { Array arr = p_existing->surface_get_arrays(p_surface); ERR_FAIL_COND(arr.size() != VS::ARRAY_MAX); + _create_list_from_arrays(arr, r_vertex, r_index, lformat); +} + +void SurfaceTool::_create_list_from_arrays(Array arr, List<Vertex> *r_vertex, List<int> *r_index, int &lformat) { PoolVector<Vector3> varr = arr[VS::ARRAY_VERTEX]; PoolVector<Vector3> narr = arr[VS::ARRAY_NORMAL]; @@ -536,7 +554,7 @@ void SurfaceTool::_create_list(const Ref<Mesh> &p_existing, int p_surface, List< if (lformat & VS::ARRAY_FORMAT_TANGENT) { Plane p(tarr[i * 4 + 0], tarr[i * 4 + 1], tarr[i * 4 + 2], tarr[i * 4 + 3]); v.tangent = p.normal; - v.binormal = p.normal.cross(last_normal).normalized() * p.d; + v.binormal = p.normal.cross(v.tangent).normalized() * p.d; } if (lformat & VS::ARRAY_FORMAT_COLOR) v.color = carr[i]; @@ -580,6 +598,13 @@ void SurfaceTool::_create_list(const Ref<Mesh> &p_existing, int p_surface, List< } } +void SurfaceTool::create_from_triangle_arrays(const Array &p_arrays) { + + clear(); + primitive = Mesh::PRIMITIVE_TRIANGLES; + _create_list_from_arrays(p_arrays, &vertex_array, &index_array, format); +} + void SurfaceTool::create_from(const Ref<Mesh> &p_existing, int p_surface) { clear(); @@ -711,8 +736,9 @@ void SurfaceTool::generate_tangents() { ERR_FAIL_COND(!res); format |= Mesh::ARRAY_FORMAT_TANGENT; - if (indexed) + if (indexed) { index(); + } } void SurfaceTool::generate_normals() { @@ -784,7 +810,6 @@ void SurfaceTool::generate_normals() { vertex_hash.clear(); if (E) { smooth = smooth_groups[count]; - print_line("SMOOTH AT " + itos(count) + ": " + itos(smooth)); } } } diff --git a/scene/resources/surface_tool.h b/scene/resources/surface_tool.h index 753c3626b8..d02e170b02 100644 --- a/scene/resources/surface_tool.h +++ b/scene/resources/surface_tool.h @@ -80,6 +80,7 @@ private: Vector<float> last_weights; Plane last_tangent; + void _create_list_from_arrays(Array arr, List<Vertex> *r_vertex, List<int> *r_index, int &lformat); void _create_list(const Ref<Mesh> &p_existing, int p_surface, List<Vertex> *r_vertex, List<int> *r_index, int &lformat); //mikktspace callbacks @@ -123,6 +124,8 @@ public: List<Vertex> &get_vertex_array() { return vertex_array; } + void create_from_triangle_arrays(const Array &p_arrays); + Array commit_to_arrays(); void create_from(const Ref<Mesh> &p_existing, int p_surface); void append_from(const Ref<Mesh> &p_existing, int p_surface, const Transform &p_xform); Ref<ArrayMesh> commit(const Ref<ArrayMesh> &p_existing = Ref<ArrayMesh>()); diff --git a/servers/arvr/arvr_positional_tracker.cpp b/servers/arvr/arvr_positional_tracker.cpp index 4215363d16..9f3d01267b 100644 --- a/servers/arvr/arvr_positional_tracker.cpp +++ b/servers/arvr/arvr_positional_tracker.cpp @@ -40,6 +40,13 @@ void ARVRPositionalTracker::_bind_methods() { ClassDB::bind_method(D_METHOD("get_tracks_position"), &ARVRPositionalTracker::get_tracks_position); ClassDB::bind_method(D_METHOD("get_position"), &ARVRPositionalTracker::get_position); ClassDB::bind_method(D_METHOD("get_transform", "adjust_by_reference_frame"), &ARVRPositionalTracker::get_transform); + + // these functions we don't want to expose to normal users but do need to be callable from GDNative + ClassDB::bind_method(D_METHOD("_set_type", "type"), &ARVRPositionalTracker::set_type); + ClassDB::bind_method(D_METHOD("_set_name", "name"), &ARVRPositionalTracker::set_name); + ClassDB::bind_method(D_METHOD("_set_joy_id", "joy_id"), &ARVRPositionalTracker::set_joy_id); + ClassDB::bind_method(D_METHOD("_set_orientation", "orientation"), &ARVRPositionalTracker::set_orientation); + ClassDB::bind_method(D_METHOD("_set_rw_position", "rw_position"), &ARVRPositionalTracker::set_rw_position); }; void ARVRPositionalTracker::set_type(ARVRServer::TrackerType p_type) { @@ -102,14 +109,36 @@ bool ARVRPositionalTracker::get_tracks_position() const { void ARVRPositionalTracker::set_position(const Vector3 &p_position) { _THREAD_SAFE_METHOD_ + ARVRServer *arvr_server = ARVRServer::get_singleton(); + ERR_FAIL_NULL(arvr_server); + real_t world_scale = arvr_server->get_world_scale(); + ERR_FAIL_COND(world_scale == 0); + tracks_position = true; // obviously we have this - position = p_position; + rw_position = p_position / world_scale; }; Vector3 ARVRPositionalTracker::get_position() const { _THREAD_SAFE_METHOD_ - return position; + ARVRServer *arvr_server = ARVRServer::get_singleton(); + ERR_FAIL_NULL_V(arvr_server, rw_position); + real_t world_scale = arvr_server->get_world_scale(); + + return rw_position * world_scale; +}; + +void ARVRPositionalTracker::set_rw_position(const Vector3 &p_rw_position) { + _THREAD_SAFE_METHOD_ + + tracks_position = true; // obviously we have this + rw_position = p_rw_position; +}; + +Vector3 ARVRPositionalTracker::get_rw_position() const { + _THREAD_SAFE_METHOD_ + + return rw_position; }; Transform ARVRPositionalTracker::get_transform(bool p_adjust_by_reference_frame) const { diff --git a/servers/arvr/arvr_positional_tracker.h b/servers/arvr/arvr_positional_tracker.h index e8c613b29d..dba203b73c 100644 --- a/servers/arvr/arvr_positional_tracker.h +++ b/servers/arvr/arvr_positional_tracker.h @@ -56,7 +56,7 @@ private: bool tracks_orientation; // do we track orientation? Basis orientation; // our orientation bool tracks_position; // do we track position? - Vector3 position; // our position + Vector3 rw_position; // our position "in the real world, so without world_scale applied" protected: static void _bind_methods(); @@ -73,8 +73,10 @@ public: void set_orientation(const Basis &p_orientation); Basis get_orientation() const; bool get_tracks_position() const; - void set_position(const Vector3 &p_position); - Vector3 get_position() const; + void set_position(const Vector3 &p_position); // set position with world_scale applied + Vector3 get_position() const; // get position with world_scale applied + void set_rw_position(const Vector3 &p_rw_position); + Vector3 get_rw_position() const; Transform get_transform(bool p_adjust_by_reference_frame) const; diff --git a/thirdparty/misc/base64.h b/thirdparty/misc/base64.h index 456ef1811b..4c300382c1 100644 --- a/thirdparty/misc/base64.h +++ b/thirdparty/misc/base64.h @@ -11,9 +11,8 @@ extern "C" { -uint32_t base64_encode (char* to, char* from, uint32_t len); -uint32_t base64_decode (char* to, char* from, uint32_t len); - +uint32_t base64_encode(char *to, char *from, uint32_t len); +uint32_t base64_decode(char *to, char *from, uint32_t len); }; #endif /* BASE64_H */ |