diff --git a/editor/SCsub b/editor/SCsub index efc2384b5..49a2106af 100644 --- a/editor/SCsub +++ b/editor/SCsub @@ -102,7 +102,6 @@ if env["tools"]: env.add_source_files(env.editor_sources, "*.cpp") env.add_source_files(env.editor_sources, "register_exporters.gen.cpp") - SConscript("collada/SCsub") SConscript("doc/SCsub") SConscript("fileserver/SCsub") SConscript("icons/SCsub") diff --git a/editor/collada/SCsub b/editor/collada/SCsub deleted file mode 100644 index 359d04e5d..000000000 --- a/editor/collada/SCsub +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env python - -Import("env") - -env.add_source_files(env.editor_sources, "*.cpp") diff --git a/editor/collada/collada.cpp b/editor/collada/collada.cpp deleted file mode 100644 index d45b280ec..000000000 --- a/editor/collada/collada.cpp +++ /dev/null @@ -1,2405 +0,0 @@ -/*************************************************************************/ -/* collada.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "collada.h" - -#include - -//#define DEBUG_DEFAULT_ANIMATION -//#define DEBUG_COLLADA -#ifdef DEBUG_COLLADA -#define COLLADA_PRINT(m_what) print_line(m_what) -#else -#define COLLADA_PRINT(m_what) -#endif - -#define COLLADA_IMPORT_SCALE_SCENE - -/* HELPERS */ - -String Collada::Effect::get_texture_path(const String &p_source, Collada &state) const { - const String &image = p_source; - ERR_FAIL_COND_V(!state.state.image_map.has(image), ""); - return state.state.image_map[image].path; -} - -Transform Collada::get_root_transform() const { - Transform unit_scale_transform; -#ifndef COLLADA_IMPORT_SCALE_SCENE - unit_scale_transform.scale(Vector3(state.unit_scale, state.unit_scale, state.unit_scale)); -#endif - return unit_scale_transform; -} - -void Collada::Vertex::fix_unit_scale(Collada &state) { -#ifdef COLLADA_IMPORT_SCALE_SCENE - vertex *= state.state.unit_scale; -#endif -} - -static String _uri_to_id(const String &p_uri) { - if (p_uri.begins_with("#")) { - return p_uri.substr(1, p_uri.size() - 1); - } else { - return p_uri; - } -} - -/** HELPER FUNCTIONS **/ - -Transform Collada::fix_transform(const Transform &p_transform) { - Transform tr = p_transform; - -#ifndef NO_UP_AXIS_SWAP - - if (state.up_axis != Vector3::AXIS_Y) { - for (int i = 0; i < 3; i++) { - SWAP(tr.basis[1][i], tr.basis[state.up_axis][i]); - } - for (int i = 0; i < 3; i++) { - SWAP(tr.basis[i][1], tr.basis[i][state.up_axis]); - } - - SWAP(tr.origin[1], tr.origin[state.up_axis]); - - tr.basis[state.up_axis][0] = -tr.basis[state.up_axis][0]; - tr.basis[state.up_axis][1] = -tr.basis[state.up_axis][1]; - tr.basis[0][state.up_axis] = -tr.basis[0][state.up_axis]; - tr.basis[1][state.up_axis] = -tr.basis[1][state.up_axis]; - tr.origin[state.up_axis] = -tr.origin[state.up_axis]; - } -#endif - - //tr.scale(Vector3(state.unit_scale.unit_scale.unit_scale)); - return tr; - //return state.matrix_fix * p_transform; -} - -static Transform _read_transform_from_array(const Vector &array, int ofs = 0) { - Transform tr; - // i wonder why collada matrices are transposed, given that's opposed to opengl.. - tr.basis.elements[0][0] = array[0 + ofs]; - tr.basis.elements[0][1] = array[1 + ofs]; - tr.basis.elements[0][2] = array[2 + ofs]; - tr.basis.elements[1][0] = array[4 + ofs]; - tr.basis.elements[1][1] = array[5 + ofs]; - tr.basis.elements[1][2] = array[6 + ofs]; - tr.basis.elements[2][0] = array[8 + ofs]; - tr.basis.elements[2][1] = array[9 + ofs]; - tr.basis.elements[2][2] = array[10 + ofs]; - tr.origin.x = array[3 + ofs]; - tr.origin.y = array[7 + ofs]; - tr.origin.z = array[11 + ofs]; - return tr; -} - -/* STRUCTURES */ - -Transform Collada::Node::compute_transform(Collada &state) const { - Transform xform; - - for (int i = 0; i < xform_list.size(); i++) { - Transform xform_step; - const XForm &xf = xform_list[i]; - switch (xf.op) { - case XForm::OP_ROTATE: { - if (xf.data.size() >= 4) { - xform_step.rotate(Vector3(xf.data[0], xf.data[1], xf.data[2]), Math::deg2rad(xf.data[3])); - } - } break; - case XForm::OP_SCALE: { - if (xf.data.size() >= 3) { - xform_step.scale(Vector3(xf.data[0], xf.data[1], xf.data[2])); - } - - } break; - case XForm::OP_TRANSLATE: { - if (xf.data.size() >= 3) { - xform_step.origin = Vector3(xf.data[0], xf.data[1], xf.data[2]); - } - - } break; - case XForm::OP_MATRIX: { - if (xf.data.size() >= 16) { - xform_step = _read_transform_from_array(xf.data, 0); - } - - } break; - default: { - } - } - - xform = xform * xform_step; - } - -#ifdef COLLADA_IMPORT_SCALE_SCENE - xform.origin *= state.state.unit_scale; -#endif - return xform; -} - -Transform Collada::Node::get_transform() const { - return default_transform; -} - -Transform Collada::Node::get_global_transform() const { - if (parent) { - return parent->get_global_transform() * default_transform; - } else { - return default_transform; - } -} - -Vector Collada::AnimationTrack::get_value_at_time(float p_time) const { - ERR_FAIL_COND_V(keys.size() == 0, Vector()); - int i = 0; - - for (i = 0; i < keys.size(); i++) { - if (keys[i].time > p_time) { - break; - } - } - - if (i == 0) { - return keys[0].data; - } - if (i == keys.size()) { - return keys[keys.size() - 1].data; - } - - switch (keys[i].interp_type) { - case INTERP_BEZIER: //wait for bezier - case INTERP_LINEAR: { - float c = (p_time - keys[i - 1].time) / (keys[i].time - keys[i - 1].time); - - if (keys[i].data.size() == 16) { - //interpolate a matrix - Transform src = _read_transform_from_array(keys[i - 1].data); - Transform dst = _read_transform_from_array(keys[i].data); - - Transform interp = c < 0.001 ? src : src.interpolate_with(dst, c); - - Vector ret; - ret.resize(16); - Transform tr; - // i wonder why collada matrices are transposed, given that's opposed to opengl.. - ret.write[0] = interp.basis.elements[0][0]; - ret.write[1] = interp.basis.elements[0][1]; - ret.write[2] = interp.basis.elements[0][2]; - ret.write[4] = interp.basis.elements[1][0]; - ret.write[5] = interp.basis.elements[1][1]; - ret.write[6] = interp.basis.elements[1][2]; - ret.write[8] = interp.basis.elements[2][0]; - ret.write[9] = interp.basis.elements[2][1]; - ret.write[10] = interp.basis.elements[2][2]; - ret.write[3] = interp.origin.x; - ret.write[7] = interp.origin.y; - ret.write[11] = interp.origin.z; - ret.write[12] = 0; - ret.write[13] = 0; - ret.write[14] = 0; - ret.write[15] = 1; - - return ret; - } else { - Vector dest; - dest.resize(keys[i].data.size()); - for (int j = 0; j < dest.size(); j++) { - dest.write[j] = keys[i].data[j] * c + keys[i - 1].data[j] * (1.0 - c); - } - return dest; - //interpolate one by one - } - } break; - } - - ERR_FAIL_V(Vector()); -} - -void Collada::_parse_asset(XMLParser &parser) { - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - String name = parser.get_node_name(); - - if (name == "up_axis") { - parser.read(); - if (parser.get_node_data() == "X_UP") { - state.up_axis = Vector3::AXIS_X; - } - if (parser.get_node_data() == "Y_UP") { - state.up_axis = Vector3::AXIS_Y; - } - if (parser.get_node_data() == "Z_UP") { - state.up_axis = Vector3::AXIS_Z; - } - - COLLADA_PRINT("up axis: " + parser.get_node_data()); - } else if (name == "unit") { - state.unit_scale = parser.get_attribute_value("meter").to_double(); - COLLADA_PRINT("unit scale: " + rtos(state.unit_scale)); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "asset") { - break; //end of - } - } -} - -void Collada::_parse_image(XMLParser &parser) { - String id = parser.get_attribute_value("id"); - - if (!(state.import_flags & IMPORT_FLAG_SCENE)) { - if (!parser.is_empty()) { - parser.skip_section(); - } - return; - } - - Image image; - - if (state.version < State::Version(1, 4, 0)) { - /* <1.4 */ - String path = parser.get_attribute_value("source").strip_edges(); - if (path.find("://") == -1 && path.is_rel_path()) { - // path is relative to file being loaded, so convert to a resource path - image.path = ProjectSettings::get_singleton()->localize_path(state.local_path.get_base_dir().plus_file(path.percent_decode())); - } - } else { - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - String name = parser.get_node_name(); - - if (name == "init_from") { - parser.read(); - String path = parser.get_node_data().strip_edges().percent_decode(); - - if (path.find("://") == -1 && path.is_rel_path()) { - // path is relative to file being loaded, so convert to a resource path - path = ProjectSettings::get_singleton()->localize_path(state.local_path.get_base_dir().plus_file(path)); - - } else if (path.find("file:///") == 0) { - path = path.replace_first("file:///", ""); - path = ProjectSettings::get_singleton()->localize_path(path); - } - - image.path = path; - - } else if (name == "data") { - ERR_PRINT("COLLADA Embedded image data not supported!"); - - } else if (name == "extra" && !parser.is_empty()) { - parser.skip_section(); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "image") { - break; //end of - } - } - } - - state.image_map[id] = image; -} - -void Collada::_parse_material(XMLParser &parser) { - if (!(state.import_flags & IMPORT_FLAG_SCENE)) { - if (!parser.is_empty()) { - parser.skip_section(); - } - return; - } - - Material material; - - String id = parser.get_attribute_value("id"); - if (parser.has_attribute("name")) { - material.name = parser.get_attribute_value("name"); - } - - if (state.version < State::Version(1, 4, 0)) { - /* <1.4 */ - ERR_PRINT("Collada Materials < 1.4 are not supported (yet)"); - } else { - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT && parser.get_node_name() == "instance_effect") { - material.instance_effect = _uri_to_id(parser.get_attribute_value("url")); - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "material") { - break; //end of - } - } - } - - state.material_map[id] = material; -} - -//! reads floats from inside of xml element until end of xml element -Vector Collada::_read_float_array(XMLParser &parser) { - if (parser.is_empty()) { - return Vector(); - } - - Vector splitters; - splitters.push_back(" "); - splitters.push_back("\n"); - splitters.push_back("\r"); - splitters.push_back("\t"); - - Vector array; - while (parser.read() == OK) { - // TODO: check for comments inside the element - // and ignore them. - - if (parser.get_node_type() == XMLParser::NODE_TEXT) { - // parse float data - String str = parser.get_node_data(); - array = str.split_floats_mk(splitters, false); - //array=str.split_floats(" ",false); - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END) { - break; // end parsing text - } - } - - return array; -} - -Vector Collada::_read_string_array(XMLParser &parser) { - if (parser.is_empty()) { - return Vector(); - } - - Vector array; - while (parser.read() == OK) { - // TODO: check for comments inside the element - // and ignore them. - - if (parser.get_node_type() == XMLParser::NODE_TEXT) { - // parse String data - String str = parser.get_node_data(); - array = str.split_spaces(); - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END) { - break; // end parsing text - } - } - - return array; -} - -Transform Collada::_read_transform(XMLParser &parser) { - if (parser.is_empty()) { - return Transform(); - } - - Vector array; - while (parser.read() == OK) { - // TODO: check for comments inside the element - // and ignore them. - - if (parser.get_node_type() == XMLParser::NODE_TEXT) { - // parse float data - String str = parser.get_node_data(); - array = str.split_spaces(); - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END) { - break; // end parsing text - } - } - - ERR_FAIL_COND_V(array.size() != 16, Transform()); - Vector farr; - farr.resize(16); - for (int i = 0; i < 16; i++) { - farr.write[i] = array[i].to_double(); - } - - return _read_transform_from_array(farr); -} - -String Collada::_read_empty_draw_type(XMLParser &parser) { - String empty_draw_type = ""; - - if (parser.is_empty()) { - return empty_draw_type; - } - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_TEXT) { - empty_draw_type = parser.get_node_data(); - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END) { - break; // end parsing text - } - } - return empty_draw_type; -} - -Variant Collada::_parse_param(XMLParser &parser) { - if (parser.is_empty()) { - return Variant(); - } - - String from = parser.get_node_name(); - Variant data; - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - if (parser.get_node_name() == "float") { - parser.read(); - if (parser.get_node_type() == XMLParser::NODE_TEXT) { - data = parser.get_node_data().to_double(); - } - } else if (parser.get_node_name() == "float2") { - Vector v2 = _read_float_array(parser); - - if (v2.size() >= 2) { - data = Vector2(v2[0], v2[1]); - } - } else if (parser.get_node_name() == "float3") { - Vector v3 = _read_float_array(parser); - - if (v3.size() >= 3) { - data = Vector3(v3[0], v3[1], v3[2]); - } - } else if (parser.get_node_name() == "float4") { - Vector v4 = _read_float_array(parser); - - if (v4.size() >= 4) { - data = Color(v4[0], v4[1], v4[2], v4[3]); - } - } else if (parser.get_node_name() == "sampler2D") { - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - if (parser.get_node_name() == "source") { - parser.read(); - - if (parser.get_node_type() == XMLParser::NODE_TEXT) { - data = parser.get_node_data(); - } - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "sampler2D") { - break; - } - } - } else if (parser.get_node_name() == "surface") { - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - if (parser.get_node_name() == "init_from") { - parser.read(); - - if (parser.get_node_type() == XMLParser::NODE_TEXT) { - data = parser.get_node_data(); - } - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "surface") { - break; - } - } - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == from) { - break; - } - } - - COLLADA_PRINT("newparam ending " + parser.get_node_name()); - return data; -} - -void Collada::_parse_effect_material(XMLParser &parser, Effect &effect, String &id) { - if (!(state.import_flags & IMPORT_FLAG_SCENE)) { - if (!parser.is_empty()) { - parser.skip_section(); - } - return; - } - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - // first come the tags we descend, but ignore the top-levels - - COLLADA_PRINT("node name: " + parser.get_node_name()); - - if (!parser.is_empty() && (parser.get_node_name() == "profile_COMMON" || parser.get_node_name() == "technique" || parser.get_node_name() == "extra")) { - _parse_effect_material(parser, effect, id); // try again - - } else if (parser.get_node_name() == "newparam") { - String name = parser.get_attribute_value("sid"); - Variant value = _parse_param(parser); - effect.params[name] = value; - COLLADA_PRINT("param: " + name + " value:" + String(value)); - - } else if (parser.get_node_name() == "constant" || - parser.get_node_name() == "lambert" || - parser.get_node_name() == "phong" || - parser.get_node_name() == "blinn") { - COLLADA_PRINT("shade model: " + parser.get_node_name()); - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - String what = parser.get_node_name(); - - if (what == "emission" || - what == "diffuse" || - what == "specular" || - what == "reflective") { - // color or texture types - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - if (parser.get_node_name() == "color") { - Vector colorarr = _read_float_array(parser); - COLLADA_PRINT("colorarr size: " + rtos(colorarr.size())); - - if (colorarr.size() >= 3) { - // alpha strangely not alright? maybe it needs to be multiplied by value as a channel intensity - Color color(colorarr[0], colorarr[1], colorarr[2], 1.0); - if (what == "diffuse") { - effect.diffuse.color = color; - } - if (what == "specular") { - effect.specular.color = color; - } - if (what == "emission") { - effect.emission.color = color; - } - - COLLADA_PRINT(what + " color: " + color); - } - - } else if (parser.get_node_name() == "texture") { - String sampler = parser.get_attribute_value("texture"); - if (!effect.params.has(sampler)) { - ERR_PRINT(String("Couldn't find sampler: " + sampler + " in material:" + id).utf8().get_data()); - } else { - String surface = effect.params[sampler]; - - if (!effect.params.has(surface)) { - ERR_PRINT(String("Couldn't find surface: " + surface + " in material:" + id).utf8().get_data()); - } else { - String uri = effect.params[surface]; - - if (what == "diffuse") { - effect.diffuse.texture = uri; - } else if (what == "specular") { - effect.specular.texture = uri; - } else if (what == "emission") { - effect.emission.texture = uri; - } else if (what == "bump") { - if (parser.has_attribute("bumptype") && parser.get_attribute_value("bumptype") != "NORMALMAP") { - WARN_PRINT("'bump' texture type is not NORMALMAP, only NORMALMAP is supported."); - } - - effect.bump.texture = uri; - } - - COLLADA_PRINT(what + " texture: " + uri); - } - } - } else if (!parser.is_empty()) { - parser.skip_section(); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == what) { - break; - } - } - - } else if (what == "shininess") { - effect.shininess = _parse_param(parser); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && - (parser.get_node_name() == "constant" || - parser.get_node_name() == "lambert" || - parser.get_node_name() == "phong" || - parser.get_node_name() == "blinn")) { - break; - } - } - } else if (parser.get_node_name() == "double_sided" || parser.get_node_name() == "show_double_sided") { // colladamax / google earth - - // 3DS Max / Google Earth double sided extension - parser.read(); - effect.found_double_sided = true; - effect.double_sided = parser.get_node_data().to_int(); - COLLADA_PRINT("double sided: " + itos(parser.get_node_data().to_int())); - } else if (parser.get_node_name() == "unshaded") { - parser.read(); - effect.unshaded = parser.get_node_data().to_int(); - } else if (parser.get_node_name() == "bump") { - // color or texture types - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - if (parser.get_node_name() == "texture") { - String sampler = parser.get_attribute_value("texture"); - if (!effect.params.has(sampler)) { - ERR_PRINT(String("Couldn't find sampler: " + sampler + " in material:" + id).utf8().get_data()); - } else { - String surface = effect.params[sampler]; - - if (!effect.params.has(surface)) { - ERR_PRINT(String("Couldn't find surface: " + surface + " in material:" + id).utf8().get_data()); - } else { - String uri = effect.params[surface]; - - if (parser.has_attribute("bumptype") && parser.get_attribute_value("bumptype") != "NORMALMAP") { - WARN_PRINT("'bump' texture type is not NORMALMAP, only NORMALMAP is supported."); - } - - effect.bump.texture = uri; - COLLADA_PRINT(" bump: " + uri); - } - } - } else if (!parser.is_empty()) { - parser.skip_section(); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "bump") { - break; - } - } - - } else if (!parser.is_empty()) { - parser.skip_section(); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && - (parser.get_node_name() == "effect" || - parser.get_node_name() == "profile_COMMON" || - parser.get_node_name() == "technique" || - parser.get_node_name() == "extra")) { - break; - } - } -} - -void Collada::_parse_effect(XMLParser &parser) { - if (!(state.import_flags & IMPORT_FLAG_SCENE)) { - if (!parser.is_empty()) { - parser.skip_section(); - } - return; - } - - String id = parser.get_attribute_value("id"); - - Effect effect; - if (parser.has_attribute("name")) { - effect.name = parser.get_attribute_value("name"); - } - _parse_effect_material(parser, effect, id); - - state.effect_map[id] = effect; - - COLLADA_PRINT("Effect ID:" + id); -} - -void Collada::_parse_camera(XMLParser &parser) { - if (!(state.import_flags & IMPORT_FLAG_SCENE)) { - if (!parser.is_empty()) { - parser.skip_section(); - } - return; - } - - String id = parser.get_attribute_value("id"); - - state.camera_data_map[id] = CameraData(); - CameraData &camera = state.camera_data_map[id]; - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - String name = parser.get_node_name(); - - if (name == "perspective") { - camera.mode = CameraData::MODE_PERSPECTIVE; - } else if (name == "orthographic") { - camera.mode = CameraData::MODE_ORTHOGONAL; - } else if (name == "xfov") { - parser.read(); - camera.perspective.x_fov = parser.get_node_data().to_double(); - - } else if (name == "yfov") { - parser.read(); - camera.perspective.y_fov = parser.get_node_data().to_double(); - } else if (name == "xmag") { - parser.read(); - camera.orthogonal.x_mag = parser.get_node_data().to_double(); - - } else if (name == "ymag") { - parser.read(); - camera.orthogonal.y_mag = parser.get_node_data().to_double(); - } else if (name == "aspect_ratio") { - parser.read(); - camera.aspect = parser.get_node_data().to_double(); - - } else if (name == "znear") { - parser.read(); - camera.z_near = parser.get_node_data().to_double(); - - } else if (name == "zfar") { - parser.read(); - camera.z_far = parser.get_node_data().to_double(); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "camera") { - break; //end of - } - } - - COLLADA_PRINT("Camera ID:" + id); -} - -void Collada::_parse_light(XMLParser &parser) { - if (!(state.import_flags & IMPORT_FLAG_SCENE)) { - if (!parser.is_empty()) { - parser.skip_section(); - } - return; - } - - String id = parser.get_attribute_value("id"); - - state.light_data_map[id] = LightData(); - LightData &light = state.light_data_map[id]; - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - String name = parser.get_node_name(); - - if (name == "ambient") { - light.mode = LightData::MODE_AMBIENT; - } else if (name == "directional") { - light.mode = LightData::MODE_DIRECTIONAL; - } else if (name == "point") { - light.mode = LightData::MODE_OMNI; - } else if (name == "spot") { - light.mode = LightData::MODE_SPOT; - } else if (name == "color") { - parser.read(); - Vector colorarr = _read_float_array(parser); - COLLADA_PRINT("colorarr size: " + rtos(colorarr.size())); - - if (colorarr.size() >= 4) { - // alpha strangely not alright? maybe it needs to be multiplied by value as a channel intensity - Color color(colorarr[0], colorarr[1], colorarr[2], 1.0); - light.color = color; - } - - } else if (name == "constant_attenuation") { - parser.read(); - light.constant_att = parser.get_node_data().to_double(); - } else if (name == "linear_attenuation") { - parser.read(); - light.linear_att = parser.get_node_data().to_double(); - } else if (name == "quadratic_attenuation") { - parser.read(); - light.quad_att = parser.get_node_data().to_double(); - } else if (name == "falloff_angle") { - parser.read(); - light.spot_angle = parser.get_node_data().to_double(); - - } else if (name == "falloff_exponent") { - parser.read(); - light.spot_exp = parser.get_node_data().to_double(); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "light") { - break; //end of - } - } - - COLLADA_PRINT("Light ID:" + id); -} - -void Collada::_parse_curve_geometry(XMLParser &parser, String p_id, String p_name) { - if (!(state.import_flags & IMPORT_FLAG_SCENE)) { - if (!parser.is_empty()) { - parser.skip_section(); - } - return; - } - - //load everything into a pre dictionary - - state.curve_data_map[p_id] = CurveData(); - - CurveData &curvedata = state.curve_data_map[p_id]; - curvedata.name = p_name; - - COLLADA_PRINT("curve name: " + p_name); - - String current_source; - // handles geometry node and the curve children in this loop - // read sources with arrays and accessor for each curve - if (parser.is_empty()) { - return; - } - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - String section = parser.get_node_name(); - - if (section == "source") { - String id = parser.get_attribute_value("id"); - curvedata.sources[id] = CurveData::Source(); - current_source = id; - COLLADA_PRINT("source data: " + id); - - } else if (section == "float_array" || section == "array") { - // create a new array and read it. - if (curvedata.sources.has(current_source)) { - curvedata.sources[current_source].array = _read_float_array(parser); - COLLADA_PRINT("section: " + current_source + " read " + itos(curvedata.sources[current_source].array.size()) + " values."); - } - } else if (section == "Name_array") { - // create a new array and read it. - if (curvedata.sources.has(current_source)) { - curvedata.sources[current_source].sarray = _read_string_array(parser); - COLLADA_PRINT("section: " + current_source + " read " + itos(curvedata.sources[current_source].array.size()) + " values."); - } - - } else if (section == "technique_common") { - //skip it - } else if (section == "accessor") { // child of source (below a technique tag) - - if (curvedata.sources.has(current_source)) { - curvedata.sources[current_source].stride = parser.get_attribute_value("stride").to_int(); - COLLADA_PRINT("section: " + current_source + " stride " + itos(curvedata.sources[current_source].stride)); - } - } else if (section == "control_vertices") { - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - if (parser.get_node_name() == "input") { - String semantic = parser.get_attribute_value("semantic"); - String source = _uri_to_id(parser.get_attribute_value("source")); - - curvedata.control_vertices[semantic] = source; - - COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section) { - break; - } - } - - } else if (!parser.is_empty()) { - parser.skip_section(); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "spline") { - break; - } - } -} - -void Collada::_parse_mesh_geometry(XMLParser &parser, String p_id, String p_name) { - if (!(state.import_flags & IMPORT_FLAG_SCENE)) { - if (!parser.is_empty()) { - parser.skip_section(); - } - return; - } - - //load everything into a pre dictionary - - state.mesh_data_map[p_id] = MeshData(); - - MeshData &meshdata = state.mesh_data_map[p_id]; - meshdata.name = p_name; - - COLLADA_PRINT("mesh name: " + p_name); - - String current_source; - // handles geometry node and the mesh children in this loop - // read sources with arrays and accessor for each mesh - if (parser.is_empty()) { - return; - } - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - String section = parser.get_node_name(); - - if (section == "source") { - String id = parser.get_attribute_value("id"); - meshdata.sources[id] = MeshData::Source(); - current_source = id; - COLLADA_PRINT("source data: " + id); - - } else if (section == "float_array" || section == "array") { - // create a new array and read it. - if (meshdata.sources.has(current_source)) { - meshdata.sources[current_source].array = _read_float_array(parser); - COLLADA_PRINT("section: " + current_source + " read " + itos(meshdata.sources[current_source].array.size()) + " values."); - } - } else if (section == "technique_common") { - //skip it - } else if (section == "accessor") { // child of source (below a technique tag) - - if (meshdata.sources.has(current_source)) { - meshdata.sources[current_source].stride = parser.get_attribute_value("stride").to_int(); - COLLADA_PRINT("section: " + current_source + " stride " + itos(meshdata.sources[current_source].stride)); - } - } else if (section == "vertices") { - MeshData::Vertices vert; - String id = parser.get_attribute_value("id"); - int last_ref = 0; - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - if (parser.get_node_name() == "input") { - String semantic = parser.get_attribute_value("semantic"); - String source = _uri_to_id(parser.get_attribute_value("source")); - - if (semantic == "TEXCOORD") { - semantic = "TEXCOORD" + itos(last_ref++); - } - - vert.sources[semantic] = source; - - COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section) { - break; - } - } - - meshdata.vertices[id] = vert; - - } else if (section == "triangles" || section == "polylist" || section == "polygons") { - bool polygons = (section == "polygons"); - if (polygons) { - WARN_PRINT("Primitive type \"polygons\" is not well supported (concave shapes may fail). To ensure that the geometry is properly imported, please re-export using \"triangles\" or \"polylist\"."); - } - MeshData::Primitives prim; - - if (parser.has_attribute("material")) { - prim.material = parser.get_attribute_value("material"); - } - prim.count = parser.get_attribute_value("count").to_int(); - prim.vertex_size = 0; - int last_ref = 0; - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - if (parser.get_node_name() == "input") { - String semantic = parser.get_attribute_value("semantic"); - String source = _uri_to_id(parser.get_attribute_value("source")); - - if (semantic == "TEXCOORD") { - /* - if (parser.has_attribute("set"))// a texcoord - semantic+=parser.get_attribute_value("set"); - else - semantic="TEXCOORD0";*/ - semantic = "TEXCOORD" + itos(last_ref++); - } - int offset = parser.get_attribute_value("offset").to_int(); - - MeshData::Primitives::SourceRef sref; - sref.source = source; - sref.offset = offset; - prim.sources[semantic] = sref; - prim.vertex_size = MAX(prim.vertex_size, offset + 1); - - COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source + " offset: " + itos(offset)); - - } else if (parser.get_node_name() == "p") { //indices - - Vector values = _read_float_array(parser); - if (polygons) { - ERR_CONTINUE(prim.vertex_size == 0); - prim.polygons.push_back(values.size() / prim.vertex_size); - int from = prim.indices.size(); - prim.indices.resize(from + values.size()); - for (int i = 0; i < values.size(); i++) { - prim.indices.write[from + i] = values[i]; - } - - } else if (prim.vertex_size > 0) { - prim.indices = values; - } - - COLLADA_PRINT("read " + itos(values.size()) + " index values"); - - } else if (parser.get_node_name() == "vcount") { // primitive - - Vector values = _read_float_array(parser); - prim.polygons = values; - COLLADA_PRINT("read " + itos(values.size()) + " polygon values"); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section) { - break; - } - } - - meshdata.primitives.push_back(prim); - - } else if (parser.get_node_name() == "double_sided") { - parser.read(); - meshdata.found_double_sided = true; - meshdata.double_sided = parser.get_node_data().to_int(); - - } else if (parser.get_node_name() == "polygons") { - ERR_PRINT("Primitive type \"polygons\" not supported, re-export using \"polylist\" or \"triangles\"."); - } else if (!parser.is_empty()) { - parser.skip_section(); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "mesh") { - break; - } - } -} - -void Collada::_parse_skin_controller(XMLParser &parser, String p_id) { - state.skin_controller_data_map[p_id] = SkinControllerData(); - SkinControllerData &skindata = state.skin_controller_data_map[p_id]; - - skindata.base = _uri_to_id(parser.get_attribute_value("source")); - - String current_source; - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - String section = parser.get_node_name(); - - if (section == "bind_shape_matrix") { - skindata.bind_shape = _read_transform(parser); -#ifdef COLLADA_IMPORT_SCALE_SCENE - skindata.bind_shape.origin *= state.unit_scale; - -#endif - COLLADA_PRINT("skeleton bind shape transform: " + skindata.bind_shape); - - } else if (section == "source") { - String id = parser.get_attribute_value("id"); - skindata.sources[id] = SkinControllerData::Source(); - current_source = id; - COLLADA_PRINT("source data: " + id); - - } else if (section == "float_array" || section == "array") { - // create a new array and read it. - if (skindata.sources.has(current_source)) { - skindata.sources[current_source].array = _read_float_array(parser); - COLLADA_PRINT("section: " + current_source + " read " + itos(skindata.sources[current_source].array.size()) + " values."); - } - } else if (section == "Name_array" || section == "IDREF_array") { - // create a new array and read it. - - if (section == "IDREF_array") { - skindata.use_idrefs = true; - } - if (skindata.sources.has(current_source)) { - skindata.sources[current_source].sarray = _read_string_array(parser); - if (section == "IDREF_array") { - Vector sa = skindata.sources[current_source].sarray; - for (int i = 0; i < sa.size(); i++) { - state.idref_joints.insert(sa[i]); - } - } - COLLADA_PRINT("section: " + current_source + " read " + itos(skindata.sources[current_source].array.size()) + " values."); - } - } else if (section == "technique_common") { - //skip it - } else if (section == "accessor") { // child of source (below a technique tag) - - if (skindata.sources.has(current_source)) { - int stride = 1; - if (parser.has_attribute("stride")) { - stride = parser.get_attribute_value("stride").to_int(); - } - - skindata.sources[current_source].stride = stride; - COLLADA_PRINT("section: " + current_source + " stride " + itos(skindata.sources[current_source].stride)); - } - - } else if (section == "joints") { - SkinControllerData::Joints joint; - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - if (parser.get_node_name() == "input") { - String semantic = parser.get_attribute_value("semantic"); - String source = _uri_to_id(parser.get_attribute_value("source")); - - joint.sources[semantic] = source; - - COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section) { - break; - } - } - - skindata.joints = joint; - - } else if (section == "vertex_weights") { - SkinControllerData::Weights weights; - - weights.count = parser.get_attribute_value("count").to_int(); - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - if (parser.get_node_name() == "input") { - String semantic = parser.get_attribute_value("semantic"); - String source = _uri_to_id(parser.get_attribute_value("source")); - - int offset = parser.get_attribute_value("offset").to_int(); - - SkinControllerData::Weights::SourceRef sref; - sref.source = source; - sref.offset = offset; - weights.sources[semantic] = sref; - - COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source + " offset: " + itos(offset)); - - } else if (parser.get_node_name() == "v") { //indices - - Vector values = _read_float_array(parser); - weights.indices = values; - COLLADA_PRINT("read " + itos(values.size()) + " index values"); - - } else if (parser.get_node_name() == "vcount") { // weightsitive - - Vector values = _read_float_array(parser); - weights.sets = values; - COLLADA_PRINT("read " + itos(values.size()) + " polygon values"); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section) { - break; - } - } - - skindata.weights = weights; - } - /* - else if (!parser.is_empty()) - parser.skip_section(); - */ - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "skin") { - break; - } - } - - /* STORE REST MATRICES */ - - Vector rests; - ERR_FAIL_COND(!skindata.joints.sources.has("JOINT")); - ERR_FAIL_COND(!skindata.joints.sources.has("INV_BIND_MATRIX")); - - String joint_arr = skindata.joints.sources["JOINT"]; - String ibm = skindata.joints.sources["INV_BIND_MATRIX"]; - - ERR_FAIL_COND(!skindata.sources.has(joint_arr)); - ERR_FAIL_COND(!skindata.sources.has(ibm)); - - SkinControllerData::Source &joint_source = skindata.sources[joint_arr]; - SkinControllerData::Source &ibm_source = skindata.sources[ibm]; - - ERR_FAIL_COND(joint_source.sarray.size() != ibm_source.array.size() / 16); - - for (int i = 0; i < joint_source.sarray.size(); i++) { - String name = joint_source.sarray[i]; - Transform xform = _read_transform_from_array(ibm_source.array, i * 16); //<- this is a mistake, it must be applied to vertices - xform.affine_invert(); // inverse for rest, because it's an inverse -#ifdef COLLADA_IMPORT_SCALE_SCENE - xform.origin *= state.unit_scale; -#endif - skindata.bone_rest_map[name] = xform; - } -} - -void Collada::_parse_morph_controller(XMLParser &parser, String p_id) { - state.morph_controller_data_map[p_id] = MorphControllerData(); - MorphControllerData &morphdata = state.morph_controller_data_map[p_id]; - - morphdata.mesh = _uri_to_id(parser.get_attribute_value("source")); - morphdata.mode = parser.get_attribute_value("method"); - String current_source; - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - String section = parser.get_node_name(); - - if (section == "source") { - String id = parser.get_attribute_value("id"); - morphdata.sources[id] = MorphControllerData::Source(); - current_source = id; - COLLADA_PRINT("source data: " + id); - - } else if (section == "float_array" || section == "array") { - // create a new array and read it. - if (morphdata.sources.has(current_source)) { - morphdata.sources[current_source].array = _read_float_array(parser); - COLLADA_PRINT("section: " + current_source + " read " + itos(morphdata.sources[current_source].array.size()) + " values."); - } - } else if (section == "Name_array" || section == "IDREF_array") { - // create a new array and read it. - - /* - if (section=="IDREF_array") - morphdata.use_idrefs=true; - */ - if (morphdata.sources.has(current_source)) { - morphdata.sources[current_source].sarray = _read_string_array(parser); - /* - if (section=="IDREF_array") { - Vector sa = morphdata.sources[current_source].sarray; - for(int i=0;icontroller = type == "instance_controller"; - geom->source = _uri_to_id(parser.get_attribute_value_safe("url")); - - if (parser.is_empty()) { //nothing else to parse... - return geom; - } - // try to find also many materials and skeletons! - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - if (parser.get_node_name() == "instance_material") { - String symbol = parser.get_attribute_value("symbol"); - String target = _uri_to_id(parser.get_attribute_value("target")); - - NodeGeometry::Material mat; - mat.target = target; - geom->material_map[symbol] = mat; - COLLADA_PRINT("uses material: '" + target + "' on primitive'" + symbol + "'"); - } else if (parser.get_node_name() == "skeleton") { - parser.read(); - String uri = _uri_to_id(parser.get_node_data()); - if (uri != "") { - geom->skeletons.push_back(uri); - } - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == type) { - break; - } - } - - if (geom->controller) { - if (geom->skeletons.empty()) { - //XSI style - - if (state.skin_controller_data_map.has(geom->source)) { - SkinControllerData *skin = &state.skin_controller_data_map[geom->source]; - //case where skeletons reference bones with IDREF (XSI) - ERR_FAIL_COND_V(!skin->joints.sources.has("JOINT"), geom); - String joint_arr = skin->joints.sources["JOINT"]; - ERR_FAIL_COND_V(!skin->sources.has(joint_arr), geom); - Collada::SkinControllerData::Source &joint_source = skin->sources[joint_arr]; - geom->skeletons = joint_source.sarray; //quite crazy, but should work. - } - } - } - - return geom; -} - -Collada::Node *Collada::_parse_visual_instance_camera(XMLParser &parser) { - NodeCamera *cam = memnew(NodeCamera); - cam->camera = _uri_to_id(parser.get_attribute_value_safe("url")); - - if (state.up_axis == Vector3::AXIS_Z) { //collada weirdness - cam->post_transform.basis.rotate(Vector3(1, 0, 0), -Math_PI * 0.5); - } - - if (parser.is_empty()) { //nothing else to parse... - return cam; - } - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "instance_camera") { - break; - } - } - - return cam; -} - -Collada::Node *Collada::_parse_visual_instance_light(XMLParser &parser) { - NodeLight *cam = memnew(NodeLight); - cam->light = _uri_to_id(parser.get_attribute_value_safe("url")); - - if (state.up_axis == Vector3::AXIS_Z) { //collada weirdness - cam->post_transform.basis.rotate(Vector3(1, 0, 0), -Math_PI * 0.5); - } - - if (parser.is_empty()) { //nothing else to parse... - return cam; - } - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "instance_light") { - break; - } - } - - return cam; -} - -Collada::Node *Collada::_parse_visual_node_instance_data(XMLParser &parser) { - String instance_type = parser.get_node_name(); - - if (instance_type == "instance_geometry" || instance_type == "instance_controller") { - return _parse_visual_instance_geometry(parser); - } else if (instance_type == "instance_camera") { - return _parse_visual_instance_camera(parser); - } else if (instance_type == "instance_light") { - return _parse_visual_instance_light(parser); - } - - if (parser.is_empty()) { //nothing else to parse... - return nullptr; - } - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == instance_type) { - break; - } - } - - return nullptr; -} - -Collada::Node *Collada::_parse_visual_scene_node(XMLParser &parser) { - String name; - - String id = parser.get_attribute_value_safe("id"); - - bool found_name = false; - - if (id == "") { - id = "%NODEID%" + itos(Math::rand()); - - } else { - found_name = true; - } - - Vector xform_list; - Vector children; - - String empty_draw_type = ""; - - Node *node = nullptr; - - name = parser.has_attribute("name") ? parser.get_attribute_value_safe("name") : parser.get_attribute_value_safe("id"); - if (name == "") { - name = id; - } else { - found_name = true; - } - - if ((parser.has_attribute("type") && parser.get_attribute_value("type") == "JOINT") || state.idref_joints.has(name)) { - // handle a bone - - NodeJoint *joint = memnew(NodeJoint); - - if (parser.has_attribute("sid")) { //bones may not have sid - joint->sid = parser.get_attribute_value("sid"); - //state.bone_map[joint->sid]=joint; - } else if (state.idref_joints.has(name)) { - joint->sid = name; //kind of a cheat but.. - } else if (parser.has_attribute("name")) { - joint->sid = parser.get_attribute_value_safe("name"); - } - - if (joint->sid != "") { - state.sid_to_node_map[joint->sid] = id; - } - - node = joint; - } - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - String section = parser.get_node_name(); - - if (section == "translate") { - Node::XForm xf; - if (parser.has_attribute("sid")) { - xf.id = parser.get_attribute_value("sid"); - } - xf.op = Node::XForm::OP_TRANSLATE; - - Vector xlt = _read_float_array(parser); - xf.data = xlt; - xform_list.push_back(xf); - - } else if (section == "rotate") { - Node::XForm xf; - if (parser.has_attribute("sid")) { - xf.id = parser.get_attribute_value("sid"); - } - xf.op = Node::XForm::OP_ROTATE; - - Vector rot = _read_float_array(parser); - xf.data = rot; - - xform_list.push_back(xf); - - } else if (section == "scale") { - Node::XForm xf; - if (parser.has_attribute("sid")) { - xf.id = parser.get_attribute_value("sid"); - } - - xf.op = Node::XForm::OP_SCALE; - - Vector scale = _read_float_array(parser); - - xf.data = scale; - - xform_list.push_back(xf); - - } else if (section == "matrix") { - Node::XForm xf; - if (parser.has_attribute("sid")) { - xf.id = parser.get_attribute_value("sid"); - } - xf.op = Node::XForm::OP_MATRIX; - - Vector matrix = _read_float_array(parser); - - xf.data = matrix; - String mtx; - for (int i = 0; i < matrix.size(); i++) { - mtx += " " + rtos(matrix[i]); - } - - xform_list.push_back(xf); - - } else if (section == "visibility") { - Node::XForm xf; - if (parser.has_attribute("sid")) { - xf.id = parser.get_attribute_value("sid"); - } - xf.op = Node::XForm::OP_VISIBILITY; - - Vector visible = _read_float_array(parser); - - xf.data = visible; - - xform_list.push_back(xf); - - } else if (section == "empty_draw_type") { - empty_draw_type = _read_empty_draw_type(parser); - } else if (section == "technique" || section == "extra") { - } else if (section != "node") { - //usually what defines the type of node - if (section.begins_with("instance_")) { - if (!node) { - node = _parse_visual_node_instance_data(parser); - - } else { - ERR_PRINT("Multiple instance_* not supported."); - } - } - - } else { - /* Found a child node!! what to do..*/ - - Node *child = _parse_visual_scene_node(parser); - children.push_back(child); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "node") { - break; - } - } - - if (!node) { - node = memnew(Node); //generic node, nothing of relevance found - } - - node->noname = !found_name; - node->xform_list = xform_list; - node->children = children; - for (int i = 0; i < children.size(); i++) { - node->children[i]->parent = node; - } - - node->name = name; - node->id = id; - node->empty_draw_type = empty_draw_type; - - if (node->children.size() == 1) { - if (node->children[0]->noname && !node->noname) { - node->children[0]->name = node->name; - node->name = node->name + "-base"; - } - } - - node->default_transform = node->compute_transform(*this); - state.scene_map[id] = node; - - return node; -} - -void Collada::_parse_visual_scene(XMLParser &parser) { - String id = parser.get_attribute_value("id"); - - if (parser.is_empty()) { - return; - } - - state.visual_scene_map[id] = VisualScene(); - VisualScene &vscene = state.visual_scene_map[id]; - - if (parser.has_attribute("name")) { - vscene.name = parser.get_attribute_value("name"); - } - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - String section = parser.get_node_name(); - - if (section == "node") { - vscene.root_nodes.push_back(_parse_visual_scene_node(parser)); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "visual_scene") { - break; - } - } - - COLLADA_PRINT("Scene ID:" + id); -} - -void Collada::_parse_animation(XMLParser &parser) { - if (!(state.import_flags & IMPORT_FLAG_ANIMATION)) { - if (!parser.is_empty()) { - parser.skip_section(); - } - - return; - } - - Map> float_sources; - Map> string_sources; - Map source_strides; - Map> samplers; - Map> source_param_names; - Map> source_param_types; - - String id = ""; - if (parser.has_attribute("id")) { - id = parser.get_attribute_value("id"); - } - - String current_source; - String current_sampler; - Vector channel_sources; - Vector channel_targets; - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - String name = parser.get_node_name(); - if (name == "source") { - current_source = parser.get_attribute_value("id"); - source_param_names[current_source] = Vector(); - source_param_types[current_source] = Vector(); - - } else if (name == "float_array") { - if (current_source != "") { - float_sources[current_source] = _read_float_array(parser); - } - - } else if (name == "Name_array") { - if (current_source != "") { - string_sources[current_source] = _read_string_array(parser); - } - } else if (name == "accessor") { - if (current_source != "" && parser.has_attribute("stride")) { - source_strides[current_source] = parser.get_attribute_value("stride").to_int(); - } - } else if (name == "sampler") { - current_sampler = parser.get_attribute_value("id"); - samplers[current_sampler] = Map(); - } else if (name == "param") { - if (parser.has_attribute("name")) { - source_param_names[current_source].push_back(parser.get_attribute_value("name")); - } else { - source_param_names[current_source].push_back(""); - } - - if (parser.has_attribute("type")) { - source_param_types[current_source].push_back(parser.get_attribute_value("type")); - } else { - source_param_types[current_source].push_back(""); - } - - } else if (name == "input") { - if (current_sampler != "") { - samplers[current_sampler][parser.get_attribute_value("semantic")] = parser.get_attribute_value("source"); - } - - } else if (name == "channel") { - channel_sources.push_back(parser.get_attribute_value("source")); - channel_targets.push_back(parser.get_attribute_value("target")); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "animation") { - break; //end of - } - } - - for (int i = 0; i < channel_sources.size(); i++) { - String source = _uri_to_id(channel_sources[i]); - String target = channel_targets[i]; - ERR_CONTINUE(!samplers.has(source)); - Map &sampler = samplers[source]; - - ERR_CONTINUE(!sampler.has("INPUT")); //no input semantic? wtf? - String input_id = _uri_to_id(sampler["INPUT"]); - COLLADA_PRINT("input id is " + input_id); - ERR_CONTINUE(!float_sources.has(input_id)); - - ERR_CONTINUE(!sampler.has("OUTPUT")); - String output_id = _uri_to_id(sampler["OUTPUT"]); - ERR_CONTINUE(!float_sources.has(output_id)); - - ERR_CONTINUE(!source_param_names.has(output_id)); - - Vector &names = source_param_names[output_id]; - - for (int l = 0; l < names.size(); l++) { - String name = names[l]; - - Vector &time_keys = float_sources[input_id]; - int key_count = time_keys.size(); - - AnimationTrack track; //begin crating track - track.id = id; - - track.keys.resize(key_count); - - for (int j = 0; j < key_count; j++) { - track.keys.write[j].time = time_keys[j]; - state.animation_length = MAX(state.animation_length, time_keys[j]); - } - - //now read actual values - - int stride = 1; - - if (source_strides.has(output_id)) { - stride = source_strides[output_id]; - } - int output_len = stride / names.size(); - - ERR_CONTINUE(output_len == 0); - ERR_CONTINUE(!float_sources.has(output_id)); - - Vector &output = float_sources[output_id]; - - ERR_CONTINUE_MSG((output.size() / stride) != key_count, "Wrong number of keys in output."); - - for (int j = 0; j < key_count; j++) { - track.keys.write[j].data.resize(output_len); - for (int k = 0; k < output_len; k++) { - track.keys.write[j].data.write[k] = output[l + j * stride + k]; //super weird but should work: - } - } - - if (sampler.has("INTERPOLATION")) { - String interp_id = _uri_to_id(sampler["INTERPOLATION"]); - ERR_CONTINUE(!string_sources.has(interp_id)); - Vector &interps = string_sources[interp_id]; - ERR_CONTINUE(interps.size() != key_count); - - for (int j = 0; j < key_count; j++) { - if (interps[j] == "BEZIER") { - track.keys.write[j].interp_type = AnimationTrack::INTERP_BEZIER; - } else { - track.keys.write[j].interp_type = AnimationTrack::INTERP_LINEAR; - } - } - } - - if (sampler.has("IN_TANGENT") && sampler.has("OUT_TANGENT")) { - //bezier control points.. - String intangent_id = _uri_to_id(sampler["IN_TANGENT"]); - ERR_CONTINUE(!float_sources.has(intangent_id)); - Vector &intangents = float_sources[intangent_id]; - - ERR_CONTINUE(intangents.size() != key_count * 2 * names.size()); - - String outangent_id = _uri_to_id(sampler["OUT_TANGENT"]); - ERR_CONTINUE(!float_sources.has(outangent_id)); - Vector &outangents = float_sources[outangent_id]; - ERR_CONTINUE(outangents.size() != key_count * 2 * names.size()); - - for (int j = 0; j < key_count; j++) { - track.keys.write[j].in_tangent = Vector2(intangents[j * 2 * names.size() + 0 + l * 2], intangents[j * 2 * names.size() + 1 + l * 2]); - track.keys.write[j].out_tangent = Vector2(outangents[j * 2 * names.size() + 0 + l * 2], outangents[j * 2 * names.size() + 1 + l * 2]); - } - } - - if (target.find("/") != -1) { //transform component - track.target = target.get_slicec('/', 0); - track.param = target.get_slicec('/', 1); - if (track.param.find(".") != -1) { - track.component = track.param.get_slice(".", 1).to_upper(); - } - track.param = track.param.get_slice(".", 0); - if (names.size() > 1 && track.component == "") { - //this is a guess because the collada spec is ambiguous here... - //i suppose if you have many names (outputs) you can't use a component and i should abide to that. - track.component = name; - } - } else { - track.target = target; - } - - state.animation_tracks.push_back(track); - - if (!state.referenced_tracks.has(target)) { - state.referenced_tracks[target] = Vector(); - } - - state.referenced_tracks[target].push_back(state.animation_tracks.size() - 1); - - if (id != "") { - if (!state.by_id_tracks.has(id)) { - state.by_id_tracks[id] = Vector(); - } - - state.by_id_tracks[id].push_back(state.animation_tracks.size() - 1); - } - - COLLADA_PRINT("loaded animation with " + itos(key_count) + " keys"); - } - } -} - -void Collada::_parse_animation_clip(XMLParser &parser) { - if (!(state.import_flags & IMPORT_FLAG_ANIMATION)) { - if (!parser.is_empty()) { - parser.skip_section(); - } - - return; - } - - AnimationClip clip; - - if (parser.has_attribute("name")) { - clip.name = parser.get_attribute_value("name"); - } else if (parser.has_attribute("id")) { - clip.name = parser.get_attribute_value("id"); - } - if (parser.has_attribute("start")) { - clip.begin = parser.get_attribute_value("start").to_double(); - } - if (parser.has_attribute("end")) { - clip.end = parser.get_attribute_value("end").to_double(); - } - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - String name = parser.get_node_name(); - if (name == "instance_animation") { - String url = _uri_to_id(parser.get_attribute_value("url")); - clip.tracks.push_back(url); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "animation_clip") { - break; //end of - } - } - - state.animation_clips.push_back(clip); -} - -void Collada::_parse_scene(XMLParser &parser) { - if (parser.is_empty()) { - return; - } - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - String name = parser.get_node_name(); - - if (name == "instance_visual_scene") { - state.root_visual_scene = _uri_to_id(parser.get_attribute_value("url")); - } else if (name == "instance_physics_scene") { - state.root_physics_scene = _uri_to_id(parser.get_attribute_value("url")); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "scene") { - break; //end of - } - } -} - -void Collada::_parse_library(XMLParser &parser) { - if (parser.is_empty()) { - return; - } - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - String name = parser.get_node_name(); - COLLADA_PRINT("library name is: " + name); - if (name == "image") { - _parse_image(parser); - } else if (name == "material") { - _parse_material(parser); - } else if (name == "effect") { - _parse_effect(parser); - } else if (name == "camera") { - _parse_camera(parser); - } else if (name == "light") { - _parse_light(parser); - } else if (name == "geometry") { - String id = parser.get_attribute_value("id"); - String name2 = parser.get_attribute_value_safe("name"); - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - if (parser.get_node_name() == "mesh") { - state.mesh_name_map[id] = (name2 != "") ? name2 : id; - _parse_mesh_geometry(parser, id, name2); - } else if (parser.get_node_name() == "spline") { - state.mesh_name_map[id] = (name2 != "") ? name2 : id; - _parse_curve_geometry(parser, id, name2); - } else if (!parser.is_empty()) { - parser.skip_section(); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "geometry") { - break; - } - } - - } else if (name == "controller") { - _parse_controller(parser); - } else if (name == "animation") { - _parse_animation(parser); - } else if (name == "animation_clip") { - _parse_animation_clip(parser); - } else if (name == "visual_scene") { - COLLADA_PRINT("visual scene"); - _parse_visual_scene(parser); - } else if (!parser.is_empty()) { - parser.skip_section(); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name().begins_with("library_")) { - break; //end of - } - } -} - -void Collada::_joint_set_owner(Collada::Node *p_node, NodeSkeleton *p_owner) { - if (p_node->type == Node::TYPE_JOINT) { - NodeJoint *nj = static_cast(p_node); - nj->owner = p_owner; - - for (int i = 0; i < nj->children.size(); i++) { - _joint_set_owner(nj->children.write[i], p_owner); - } - } -} - -void Collada::_create_skeletons(Collada::Node **p_node, NodeSkeleton *p_skeleton) { - Node *node = *p_node; - - if (node->type == Node::TYPE_JOINT) { - if (!p_skeleton) { - // ohohohoohoo it's a joint node, time to work! - NodeSkeleton *sk = memnew(NodeSkeleton); - *p_node = sk; - sk->children.push_back(node); - sk->parent = node->parent; - node->parent = sk; - p_skeleton = sk; - } - - NodeJoint *nj = static_cast(node); - nj->owner = p_skeleton; - } else { - p_skeleton = nullptr; - } - - for (int i = 0; i < node->children.size(); i++) { - _create_skeletons(&node->children.write[i], p_skeleton); - } -} - -bool Collada::_remove_node(Node *p_parent, Node *p_node) { - for (int i = 0; i < p_parent->children.size(); i++) { - if (p_parent->children[i] == p_node) { - p_parent->children.remove(i); - return true; - } - if (_remove_node(p_parent->children[i], p_node)) { - return true; - } - } - - return false; -} - -void Collada::_remove_node(VisualScene *p_vscene, Node *p_node) { - for (int i = 0; i < p_vscene->root_nodes.size(); i++) { - if (p_vscene->root_nodes[i] == p_node) { - p_vscene->root_nodes.remove(i); - return; - } - if (_remove_node(p_vscene->root_nodes[i], p_node)) { - return; - } - } - - ERR_PRINT("ERROR: Not found node to remove?"); -} - -void Collada::_merge_skeletons(VisualScene *p_vscene, Node *p_node) { - if (p_node->type == Node::TYPE_GEOMETRY) { - NodeGeometry *gnode = static_cast(p_node); - if (gnode->controller) { - // recount skeletons used - Set skeletons; - - for (int i = 0; i < gnode->skeletons.size(); i++) { - String nodeid = gnode->skeletons[i]; - - ERR_CONTINUE(!state.scene_map.has(nodeid)); //weird, it should have it... - - NodeJoint *nj = SAFE_CAST(state.scene_map[nodeid]); - ERR_CONTINUE(!nj); //broken collada - ERR_CONTINUE(!nj->owner); //weird, node should have a skeleton owner - - skeletons.insert(nj->owner); - } - - if (skeletons.size() > 1) { - //do the merger!! - Set::Element *E = skeletons.front(); - NodeSkeleton *base = E->get(); - - for (E = E->next(); E; E = E->next()) { - NodeSkeleton *merged = E->get(); - _remove_node(p_vscene, merged); - for (int i = 0; i < merged->children.size(); i++) { - _joint_set_owner(merged->children[i], base); - base->children.push_back(merged->children[i]); - merged->children[i]->parent = base; - } - - merged->children.clear(); //take children from it - memdelete(merged); - } - } - } - } - - for (int i = 0; i < p_node->children.size(); i++) { - _merge_skeletons(p_vscene, p_node->children[i]); - } -} - -void Collada::_merge_skeletons2(VisualScene *p_vscene) { - for (Map::Element *E = state.skin_controller_data_map.front(); E; E = E->next()) { - SkinControllerData &cd = E->get(); - - NodeSkeleton *skeleton = nullptr; - - for (Map::Element *F = cd.bone_rest_map.front(); F; F = F->next()) { - String name; - - if (!state.sid_to_node_map.has(F->key())) { - continue; - } - - name = state.sid_to_node_map[F->key()]; - - ERR_CONTINUE(!state.scene_map.has(name)); - - Node *node = state.scene_map[name]; - ERR_CONTINUE(node->type != Node::TYPE_JOINT); - - NodeSkeleton *sk = nullptr; - - while (node && !sk) { - if (node->type == Node::TYPE_SKELETON) { - sk = static_cast(node); - } - node = node->parent; - } - - ERR_CONTINUE(!sk); - - if (!skeleton) { - skeleton = sk; - continue; - } - - if (skeleton != sk) { - //whoa.. wtf, merge. - _remove_node(p_vscene, sk); - for (int i = 0; i < sk->children.size(); i++) { - _joint_set_owner(sk->children[i], skeleton); - skeleton->children.push_back(sk->children[i]); - sk->children[i]->parent = skeleton; - } - - sk->children.clear(); //take children from it - memdelete(sk); - } - } - } -} - -bool Collada::_optimize_skeletons(VisualScene *p_vscene, Node *p_node) { - Node *node = p_node; - - if (node->type == Node::TYPE_SKELETON && node->parent && node->parent->type == Node::TYPE_NODE && node->parent->children.size() == 1) { - //replace parent by this... - Node *parent = node->parent; - - //i wonder if this is alright.. i think it is since created skeleton (first joint) is already animated by bone.. - node->id = parent->id; - node->name = parent->name; - node->xform_list = parent->xform_list; - node->default_transform = parent->default_transform; - - state.scene_map[node->id] = node; - node->parent = parent->parent; - - if (parent->parent) { - Node *gp = parent->parent; - bool found = false; - for (int i = 0; i < gp->children.size(); i++) { - if (gp->children[i] == parent) { - gp->children.write[i] = node; - found = true; - break; - } - } - if (!found) { - ERR_PRINT("BUG"); - } - } else { - bool found = false; - - for (int i = 0; i < p_vscene->root_nodes.size(); i++) { - if (p_vscene->root_nodes[i] == parent) { - p_vscene->root_nodes.write[i] = node; - found = true; - break; - } - } - if (!found) { - ERR_PRINT("BUG"); - } - } - - parent->children.clear(); - memdelete(parent); - return true; - } - - for (int i = 0; i < node->children.size(); i++) { - if (_optimize_skeletons(p_vscene, node->children[i])) { - return false; //stop processing, go up - } - } - - return false; -} - -bool Collada::_move_geometry_to_skeletons(VisualScene *p_vscene, Node *p_node, List *p_mgeom) { - // Bind Shape Matrix scales the bones and makes them gigantic, so the matrix then shrinks the model? - // Solution: apply the Bind Shape Matrix to the VERTICES, and if the object comes scaled, it seems to be left alone! - - if (p_node->type == Node::TYPE_GEOMETRY) { - NodeGeometry *ng = static_cast(p_node); - if (ng->ignore_anim) { - return false; //already made child of skeleton and processeg - } - - if (ng->controller && ng->skeletons.size()) { - String nodeid = ng->skeletons[0]; - - ERR_FAIL_COND_V(!state.scene_map.has(nodeid), false); //weird, it should have it... - NodeJoint *nj = SAFE_CAST(state.scene_map[nodeid]); - ERR_FAIL_COND_V(!nj, false); - ERR_FAIL_COND_V(!nj->owner, false); //weird, node should have a skeleton owner - - NodeSkeleton *sk = nj->owner; - - Node *p = sk->parent; - bool node_is_parent_of_skeleton = false; - - while (p) { - if (p == p_node) { - node_is_parent_of_skeleton = true; - break; - } - p = p->parent; // try again - } - - ERR_FAIL_COND_V(node_is_parent_of_skeleton, false); - - //this should be correct - ERR_FAIL_COND_V(!state.skin_controller_data_map.has(ng->source), false); - SkinControllerData &skin = state.skin_controller_data_map[ng->source]; - Transform skel_inv = sk->get_global_transform().affine_inverse(); - p_node->default_transform = skel_inv * (skin.bind_shape /* p_node->get_global_transform()*/); // i honestly have no idea what to do with a previous model xform.. most exporters ignore it - - //make rests relative to the skeleton (they seem to be always relative to world) - for (Map::Element *E = skin.bone_rest_map.front(); E; E = E->next()) { - E->get() = skel_inv * E->get(); //make the bone rest local to the skeleton - state.bone_rest_map[E->key()] = E->get(); // make it remember where the bone is globally, now that it's relative - } - - //but most exporters seem to work only if i do this.. - //p_node->default_transform = p_node->get_global_transform(); - - //p_node->default_transform=Transform(); //this seems to be correct, because bind shape makes the object local to the skeleton - p_node->ignore_anim = true; // collada may animate this later, if it does, then this is not supported (redo your original asset and don't animate the base mesh) - p_node->parent = sk; - //sk->children.push_back(0,p_node); //avoid INFINITE loop - p_mgeom->push_back(p_node); - return true; - } - } - - for (int i = 0; i < p_node->children.size(); i++) { - if (_move_geometry_to_skeletons(p_vscene, p_node->children[i], p_mgeom)) { - p_node->children.remove(i); - i--; - } - } - - return false; -} - -void Collada::_find_morph_nodes(VisualScene *p_vscene, Node *p_node) { - if (p_node->type == Node::TYPE_GEOMETRY) { - NodeGeometry *nj = static_cast(p_node); - - if (nj->controller) { - String base = nj->source; - - while (base != "" && !state.mesh_data_map.has(base)) { - if (state.skin_controller_data_map.has(base)) { - SkinControllerData &sk = state.skin_controller_data_map[base]; - base = sk.base; - } else if (state.morph_controller_data_map.has(base)) { - state.morph_ownership_map[base] = nj->id; - break; - } else { - ERR_FAIL_MSG("Invalid scene."); - } - } - } - } - - for (int i = 0; i < p_node->children.size(); i++) { - _find_morph_nodes(p_vscene, p_node->children[i]); - } -} - -void Collada::_optimize() { - for (Map::Element *E = state.visual_scene_map.front(); E; E = E->next()) { - VisualScene &vs = E->get(); - for (int i = 0; i < vs.root_nodes.size(); i++) { - _create_skeletons(&vs.root_nodes.write[i]); - } - - for (int i = 0; i < vs.root_nodes.size(); i++) { - _merge_skeletons(&vs, vs.root_nodes[i]); - } - - _merge_skeletons2(&vs); - - for (int i = 0; i < vs.root_nodes.size(); i++) { - _optimize_skeletons(&vs, vs.root_nodes[i]); - } - - for (int i = 0; i < vs.root_nodes.size(); i++) { - List mgeom; - if (_move_geometry_to_skeletons(&vs, vs.root_nodes[i], &mgeom)) { - vs.root_nodes.remove(i); - i--; - } - - while (!mgeom.empty()) { - Node *n = mgeom.front()->get(); - n->parent->children.push_back(n); - mgeom.pop_front(); - } - } - - for (int i = 0; i < vs.root_nodes.size(); i++) { - _find_morph_nodes(&vs, vs.root_nodes[i]); - } - } -} - -int Collada::get_uv_channel(String p_name) { - if (!channel_map.has(p_name)) { - ERR_FAIL_COND_V(channel_map.size() == 2, 0); - - channel_map[p_name] = channel_map.size(); - } - - return channel_map[p_name]; -} - -Error Collada::load(const String &p_path, int p_flags) { - Ref parserr = memnew(XMLParser); - XMLParser &parser = *parserr.ptr(); - Error err = parser.open(p_path); - ERR_FAIL_COND_V_MSG(err, err, "Cannot open Collada file '" + p_path + "'."); - - state.local_path = ProjectSettings::get_singleton()->localize_path(p_path); - state.import_flags = p_flags; - /* Skip headers */ - while ((err = parser.read()) == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - if (parser.get_node_name() == "COLLADA") { - break; - } else if (!parser.is_empty()) { - parser.skip_section(); // unknown section, likely headers - } - } - } - - ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CORRUPT, "Corrupted Collada file '" + p_path + "'."); - - /* Start loading Collada */ - - { - //version - String version = parser.get_attribute_value("version"); - state.version.major = version.get_slice(".", 0).to_int(); - state.version.minor = version.get_slice(".", 1).to_int(); - state.version.rev = version.get_slice(".", 2).to_int(); - COLLADA_PRINT("Collada VERSION: " + version); - } - - while ((err = parser.read()) == OK) { - /* Read all the main sections.. */ - - if (parser.get_node_type() != XMLParser::NODE_ELEMENT) { - continue; //no idea what this may be, but skipping anyway - } - - String section = parser.get_node_name(); - - COLLADA_PRINT("section: " + section); - - if (section == "asset") { - _parse_asset(parser); - - } else if (section.begins_with("library_")) { - _parse_library(parser); - } else if (section == "scene") { - _parse_scene(parser); - } else if (!parser.is_empty()) { - parser.skip_section(); // unknown section, likely headers - } - } - - _optimize(); - return OK; -} - -Collada::Collada() { -} diff --git a/editor/collada/collada.h b/editor/collada/collada.h deleted file mode 100644 index 40df08c37..000000000 --- a/editor/collada/collada.h +++ /dev/null @@ -1,619 +0,0 @@ -/*************************************************************************/ -/* collada.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef COLLADA_H -#define COLLADA_H - -#include "core/io/xml_parser.h" -#include "core/map.h" -#include "core/project_settings.h" -#include "scene/resources/material.h" - -class Collada { -public: - enum ImportFlags { - IMPORT_FLAG_SCENE = 1, - IMPORT_FLAG_ANIMATION = 2 - }; - - struct Image { - String path; - }; - - struct Material { - String name; - String instance_effect; - }; - - struct Effect { - String name; - Map params; - - struct Channel { - int uv_idx; - String texture; - Color color; - Channel() { uv_idx = 0; } - }; - - Channel diffuse, specular, emission, bump; - float shininess; - bool found_double_sided; - bool double_sided; - bool unshaded; - - String get_texture_path(const String &p_source, Collada &state) const; - - Effect() { - diffuse.color = Color(1, 1, 1, 1); - double_sided = true; - found_double_sided = false; - shininess = 40; - unshaded = false; - } - }; - - struct CameraData { - enum Mode { - MODE_PERSPECTIVE, - MODE_ORTHOGONAL - }; - - Mode mode; - - union { - struct { - float x_fov; - float y_fov; - } perspective; - struct { - float x_mag; - float y_mag; - } orthogonal; - }; - - float aspect; - float z_near; - float z_far; - - CameraData() : - mode(MODE_PERSPECTIVE), - aspect(1), - z_near(0.1), - z_far(100) { - perspective.x_fov = 0; - perspective.y_fov = 0; - } - }; - - struct LightData { - enum Mode { - MODE_AMBIENT, - MODE_DIRECTIONAL, - MODE_OMNI, - MODE_SPOT - }; - - Mode mode; - - Color color; - - float constant_att; - float linear_att; - float quad_att; - - float spot_angle; - float spot_exp; - - LightData() : - mode(MODE_AMBIENT), - color(Color(1, 1, 1, 1)), - constant_att(0), - linear_att(0), - quad_att(0), - spot_angle(45), - spot_exp(1) { - } - }; - - struct MeshData { - String name; - struct Source { - Vector array; - int stride; - }; - - Map sources; - - struct Vertices { - Map sources; - }; - - Map vertices; - - struct Primitives { - struct SourceRef { - String source; - int offset; - }; - - String material; - Map sources; - Vector polygons; - Vector indices; - int count; - int vertex_size; - }; - - Vector primitives; - - bool found_double_sided; - bool double_sided; - - MeshData() { - found_double_sided = false; - double_sided = true; - } - }; - - struct CurveData { - String name; - bool closed; - - struct Source { - Vector sarray; - Vector array; - int stride; - }; - - Map sources; - - Map control_vertices; - - CurveData() { - closed = false; - } - }; - struct SkinControllerData { - String base; - bool use_idrefs; - - Transform bind_shape; - - struct Source { - Vector sarray; //maybe for names - Vector array; - int stride; - Source() { - stride = 1; - } - }; - - Map sources; - - struct Joints { - Map sources; - } joints; - - struct Weights { - struct SourceRef { - String source; - int offset; - }; - - String material; - Map sources; - Vector sets; - Vector indices; - int count; - } weights; - - Map bone_rest_map; - - SkinControllerData() { use_idrefs = false; } - }; - - struct MorphControllerData { - String mesh; - String mode; - - struct Source { - int stride; - Vector sarray; //maybe for names - Vector array; - Source() { stride = 1; } - }; - - Map sources; - - Map targets; - MorphControllerData() {} - }; - - struct Vertex { - int idx; - Vector3 vertex; - Vector3 normal; - Vector3 uv; - Vector3 uv2; - Plane tangent; - Color color; - int uid; - struct Weight { - int bone_idx; - float weight; - bool operator<(const Weight w) const { return weight > w.weight; } //heaviest first - }; - - Vector weights; - - void fix_weights() { - weights.sort(); - if (weights.size() > 4) { - //cap to 4 and make weights add up 1 - weights.resize(4); - float total = 0; - for (int i = 0; i < 4; i++) { - total += weights[i].weight; - } - if (total) { - for (int i = 0; i < 4; i++) { - weights.write[i].weight /= total; - } - } - } - } - - void fix_unit_scale(Collada &state); - - bool operator<(const Vertex &p_vert) const { - if (uid == p_vert.uid) { - if (vertex == p_vert.vertex) { - if (normal == p_vert.normal) { - if (uv == p_vert.uv) { - if (uv2 == p_vert.uv2) { - if (!weights.empty() || !p_vert.weights.empty()) { - if (weights.size() == p_vert.weights.size()) { - for (int i = 0; i < weights.size(); i++) { - if (weights[i].bone_idx != p_vert.weights[i].bone_idx) { - return weights[i].bone_idx < p_vert.weights[i].bone_idx; - } - - if (weights[i].weight != p_vert.weights[i].weight) { - return weights[i].weight < p_vert.weights[i].weight; - } - } - } else { - return weights.size() < p_vert.weights.size(); - } - } - - return (color < p_vert.color); - } else { - return (uv2 < p_vert.uv2); - } - } else { - return (uv < p_vert.uv); - } - } else { - return (normal < p_vert.normal); - } - } else { - return vertex < p_vert.vertex; - } - } else { - return uid < p_vert.uid; - } - } - - Vertex() { - uid = 0; - idx = 0; - } - }; - struct Node { - enum Type { - - TYPE_NODE, - TYPE_JOINT, - TYPE_SKELETON, //this bone is not collada, it's added afterwards as optimization - TYPE_LIGHT, - TYPE_CAMERA, - TYPE_GEOMETRY - }; - - struct XForm { - enum Op { - OP_ROTATE, - OP_SCALE, - OP_TRANSLATE, - OP_MATRIX, - OP_VISIBILITY - }; - - String id; - Op op; - Vector data; - }; - - Type type; - - String name; - String id; - String empty_draw_type; - bool noname; - Vector xform_list; - Transform default_transform; - Transform post_transform; - Vector children; - - Node *parent; - - Transform compute_transform(Collada &state) const; - Transform get_global_transform() const; - Transform get_transform() const; - - bool ignore_anim; - - Node() { - noname = false; - type = TYPE_NODE; - parent = nullptr; - ignore_anim = false; - } - virtual ~Node() { - for (int i = 0; i < children.size(); i++) { - memdelete(children[i]); - } - }; - }; - - struct NodeSkeleton : public Node { - NodeSkeleton() { type = TYPE_SKELETON; } - }; - - struct NodeJoint : public Node { - NodeSkeleton *owner; - String sid; - NodeJoint() { - type = TYPE_JOINT; - owner = nullptr; - } - }; - - struct NodeGeometry : public Node { - bool controller; - String source; - - struct Material { - String target; - }; - - Map material_map; - Vector skeletons; - - NodeGeometry() { type = TYPE_GEOMETRY; } - }; - - struct NodeCamera : public Node { - String camera; - - NodeCamera() { type = TYPE_CAMERA; } - }; - - struct NodeLight : public Node { - String light; - - NodeLight() { type = TYPE_LIGHT; } - }; - - struct VisualScene { - String name; - Vector root_nodes; - - ~VisualScene() { - for (int i = 0; i < root_nodes.size(); i++) { - memdelete(root_nodes[i]); - } - } - }; - - struct AnimationClip { - String name; - float begin; - float end; - Vector tracks; - - AnimationClip() { - begin = 0; - end = 1; - } - }; - - struct AnimationTrack { - String id; - String target; - String param; - String component; - bool property; - - enum InterpolationType { - INTERP_LINEAR, - INTERP_BEZIER - }; - - struct Key { - enum Type { - TYPE_FLOAT, - TYPE_MATRIX - }; - - float time; - Vector data; - Point2 in_tangent; - Point2 out_tangent; - InterpolationType interp_type; - - Key() { interp_type = INTERP_LINEAR; } - }; - - Vector get_value_at_time(float p_time) const; - - Vector keys; - - AnimationTrack() { property = false; } - }; - - /****************/ - /* IMPORT STATE */ - /****************/ - - struct State { - int import_flags; - - float unit_scale; - Vector3::Axis up_axis; - bool z_up; - - struct Version { - int major, minor, rev; - - bool operator<(const Version &p_ver) const { return (major == p_ver.major) ? ((minor == p_ver.minor) ? (rev < p_ver.rev) : minor < p_ver.minor) : major < p_ver.major; } - Version(int p_major = 0, int p_minor = 0, int p_rev = 0) { - major = p_major; - minor = p_minor; - rev = p_rev; - } - } version; - - Map camera_data_map; - Map mesh_data_map; - Map light_data_map; - Map curve_data_map; - - Map mesh_name_map; - Map morph_name_map; - Map morph_ownership_map; - Map skin_controller_data_map; - Map morph_controller_data_map; - - Map image_map; - Map material_map; - Map effect_map; - - Map visual_scene_map; - Map scene_map; - Set idref_joints; - Map sid_to_node_map; - //Map bone_map; - - Map bone_rest_map; - - String local_path; - String root_visual_scene; - String root_physics_scene; - - Vector animation_clips; - Vector animation_tracks; - Map> referenced_tracks; - Map> by_id_tracks; - - float animation_length; - - State() : - import_flags(0), - unit_scale(1.0), - up_axis(Vector3::AXIS_Y), - animation_length(0) { - } - } state; - - Error load(const String &p_path, int p_flags = 0); - - Collada(); - - Transform fix_transform(const Transform &p_transform); - - Transform get_root_transform() const; - - int get_uv_channel(String p_name); - -private: // private stuff - Map channel_map; - - void _parse_asset(XMLParser &parser); - void _parse_image(XMLParser &parser); - void _parse_material(XMLParser &parser); - void _parse_effect_material(XMLParser &parser, Effect &effect, String &id); - void _parse_effect(XMLParser &parser); - void _parse_camera(XMLParser &parser); - void _parse_light(XMLParser &parser); - void _parse_animation_clip(XMLParser &parser); - - void _parse_mesh_geometry(XMLParser &parser, String p_id, String p_name); - void _parse_curve_geometry(XMLParser &parser, String p_id, String p_name); - - void _parse_skin_controller(XMLParser &parser, String p_id); - void _parse_morph_controller(XMLParser &parser, String p_id); - void _parse_controller(XMLParser &parser); - - Node *_parse_visual_instance_geometry(XMLParser &parser); - Node *_parse_visual_instance_camera(XMLParser &parser); - Node *_parse_visual_instance_light(XMLParser &parser); - - Node *_parse_visual_node_instance_data(XMLParser &parser); - Node *_parse_visual_scene_node(XMLParser &parser); - void _parse_visual_scene(XMLParser &parser); - - void _parse_animation(XMLParser &parser); - void _parse_scene(XMLParser &parser); - void _parse_library(XMLParser &parser); - - Variant _parse_param(XMLParser &parser); - Vector _read_float_array(XMLParser &parser); - Vector _read_string_array(XMLParser &parser); - Transform _read_transform(XMLParser &parser); - String _read_empty_draw_type(XMLParser &parser); - - void _joint_set_owner(Collada::Node *p_node, NodeSkeleton *p_owner); - void _create_skeletons(Collada::Node **p_node, NodeSkeleton *p_skeleton = nullptr); - void _find_morph_nodes(VisualScene *p_vscene, Node *p_node); - bool _remove_node(Node *p_parent, Node *p_node); - void _remove_node(VisualScene *p_vscene, Node *p_node); - void _merge_skeletons2(VisualScene *p_vscene); - void _merge_skeletons(VisualScene *p_vscene, Node *p_node); - bool _optimize_skeletons(VisualScene *p_vscene, Node *p_node); - - bool _move_geometry_to_skeletons(VisualScene *p_vscene, Node *p_node, List *p_mgeom); - - void _optimize(); -}; - -#endif // COLLADA_H diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 93809190e..656870e1e 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -92,7 +92,6 @@ #include "editor/export_template_manager.h" #include "editor/fileserver/editor_file_server.h" #include "editor/filesystem_dock.h" -#include "editor/import/editor_import_collada.h" #include "editor/import/resource_importer_bitmask.h" #include "editor/import/resource_importer_csv_translation.h" #include "editor/import/resource_importer_image.h" @@ -5902,10 +5901,6 @@ EditorNode::EditorNode() { ResourceFormatImporter::get_singleton()->add_importer(import_scene); { - Ref import_collada; - import_collada.instance(); - import_scene->add_importer(import_collada); - Ref import_obj2; import_obj2.instance(); import_scene->add_importer(import_obj2); diff --git a/editor/import/editor_import_collada.cpp b/editor/import/editor_import_collada.cpp deleted file mode 100644 index 997f111de..000000000 --- a/editor/import/editor_import_collada.cpp +++ /dev/null @@ -1,1796 +0,0 @@ -/*************************************************************************/ -/* editor_import_collada.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "editor_import_collada.h" - -#include "core/os/os.h" -#include "editor/collada/collada.h" -#include "editor/editor_node.h" -#include "scene/3d/camera.h" -#include "scene/3d/light.h" -#include "scene/3d/mesh_instance.h" -#include "scene/3d/path.h" -#include "scene/3d/skeleton.h" -#include "scene/3d/spatial.h" -#include "scene/animation/animation_player.h" -#include "scene/resources/animation.h" -#include "scene/resources/packed_scene.h" -#include "scene/resources/surface_tool.h" - -struct ColladaImport { - Collada collada; - Spatial *scene; - - Vector> animations; - - struct NodeMap { - //String path; - Spatial *node; - int bone; - List anim_tracks; - - NodeMap() { - node = nullptr; - bone = -1; - } - }; - - bool found_ambient; - Color ambient; - bool found_directional; - bool force_make_tangents; - bool apply_mesh_xform_to_vertices; - bool use_mesh_builtin_materials; - float bake_fps; - - Map node_map; //map from collada node to engine node - Map node_name_map; //map from collada node to engine node - Map> mesh_cache; - Map> curve_cache; - Map> material_cache; - Map skeleton_map; - - Map> skeleton_bone_map; - - Set valid_animated_nodes; - Vector valid_animated_properties; - Map bones_with_animation; - - Error _populate_skeleton(Skeleton *p_skeleton, Collada::Node *p_node, int &r_bone, int p_parent); - Error _create_scene_skeletons(Collada::Node *p_node); - Error _create_scene(Collada::Node *p_node, Spatial *p_parent); - Error _create_resources(Collada::Node *p_node, uint32_t p_use_compression); - Error _create_material(const String &p_target); - Error _create_mesh_surfaces(bool p_optimize, Ref &p_mesh, const Map &p_material_map, const Collada::MeshData &meshdata, const Transform &p_local_xform, const Vector &bone_remap, const Collada::SkinControllerData *p_skin_controller, const Collada::MorphControllerData *p_morph_data, Vector> p_morph_meshes = Vector>(), uint32_t p_use_compression = 0, bool p_use_mesh_material = false); - Error load(const String &p_path, int p_flags, bool p_force_make_tangents = false, uint32_t p_use_compression = 0); - void _fix_param_animation_tracks(); - void create_animation(int p_clip, bool p_make_tracks_in_all_bones, bool p_import_value_tracks); - void create_animations(bool p_make_tracks_in_all_bones, bool p_import_value_tracks); - - Set tracks_in_clips; - Vector missing_textures; - - void _pre_process_lights(Collada::Node *p_node); - - ColladaImport() { - found_ambient = false; - found_directional = false; - force_make_tangents = false; - apply_mesh_xform_to_vertices = true; - bake_fps = 15; - } -}; - -Error ColladaImport::_populate_skeleton(Skeleton *p_skeleton, Collada::Node *p_node, int &r_bone, int p_parent) { - if (p_node->type != Collada::Node::TYPE_JOINT) { - return OK; - } - - Collada::NodeJoint *joint = static_cast(p_node); - - p_skeleton->add_bone(p_node->name); - if (p_parent >= 0) { - p_skeleton->set_bone_parent(r_bone, p_parent); - } - - NodeMap nm; - nm.node = p_skeleton; - nm.bone = r_bone; - node_map[p_node->id] = nm; - node_name_map[p_node->name] = p_node->id; - - skeleton_bone_map[p_skeleton][joint->sid] = r_bone; - - if (collada.state.bone_rest_map.has(joint->sid)) { - p_skeleton->set_bone_rest(r_bone, collada.fix_transform(collada.state.bone_rest_map[joint->sid])); - //should map this bone to something for animation? - } else { - WARN_PRINT("Collada: Joint has no rest."); - } - - int id = r_bone++; - for (int i = 0; i < p_node->children.size(); i++) { - Error err = _populate_skeleton(p_skeleton, p_node->children[i], r_bone, id); - if (err) { - return err; - } - } - - return OK; -} - -void ColladaImport::_pre_process_lights(Collada::Node *p_node) { - if (p_node->type == Collada::Node::TYPE_LIGHT) { - Collada::NodeLight *light = static_cast(p_node); - if (collada.state.light_data_map.has(light->light)) { - Collada::LightData &ld = collada.state.light_data_map[light->light]; - if (ld.mode == Collada::LightData::MODE_AMBIENT) { - found_ambient = true; - ambient = ld.color; - } - if (ld.mode == Collada::LightData::MODE_DIRECTIONAL) { - found_directional = true; - } - } - } - - for (int i = 0; i < p_node->children.size(); i++) { - _pre_process_lights(p_node->children[i]); - } -} - -Error ColladaImport::_create_scene_skeletons(Collada::Node *p_node) { - if (p_node->type == Collada::Node::TYPE_SKELETON) { - Skeleton *sk = memnew(Skeleton); - int bone = 0; - for (int i = 0; i < p_node->children.size(); i++) { - _populate_skeleton(sk, p_node->children[i], bone, -1); - } - sk->localize_rests(); //after creating skeleton, rests must be localized...! - skeleton_map[p_node] = sk; - } - - for (int i = 0; i < p_node->children.size(); i++) { - Error err = _create_scene_skeletons(p_node->children[i]); - if (err) { - return err; - } - } - return OK; -} - -Error ColladaImport::_create_scene(Collada::Node *p_node, Spatial *p_parent) { - Spatial *node = nullptr; - - switch (p_node->type) { - case Collada::Node::TYPE_NODE: { - node = memnew(Spatial); - } break; - case Collada::Node::TYPE_JOINT: { - return OK; // do nothing - } break; - case Collada::Node::TYPE_LIGHT: { - //node = memnew( Light) - Collada::NodeLight *light = static_cast(p_node); - if (collada.state.light_data_map.has(light->light)) { - Collada::LightData &ld = collada.state.light_data_map[light->light]; - - if (ld.mode == Collada::LightData::MODE_AMBIENT) { - if (found_directional) { - return OK; //do nothing not needed - } - - if (!bool(GLOBAL_DEF("collada/use_ambient", false))) { - return OK; - } - //well, it's an ambient light.. - Light *l = memnew(DirectionalLight); - //l->set_color(Light::COLOR_AMBIENT,ld.color); - //l->set_color(Light::COLOR_DIFFUSE,Color(0,0,0)); - //l->set_color(Light::COLOR_SPECULAR,Color(0,0,0)); - node = l; - - } else if (ld.mode == Collada::LightData::MODE_DIRECTIONAL) { - //well, it's an ambient light.. - Light *l = memnew(DirectionalLight); - /* - if (found_ambient) //use it here - l->set_color(Light::COLOR_AMBIENT,ambient); - - l->set_color(Light::COLOR_DIFFUSE,ld.color); - l->set_color(Light::COLOR_SPECULAR,Color(1,1,1)); - */ - node = l; - } else { - Light *l; - - if (ld.mode == Collada::LightData::MODE_OMNI) { - l = memnew(OmniLight); - } else { - l = memnew(SpotLight); - //l->set_parameter(Light::PARAM_SPOT_ANGLE,ld.spot_angle); - //l->set_parameter(Light::PARAM_SPOT_ATTENUATION,ld.spot_exp); - } - - // - //l->set_color(Light::COLOR_DIFFUSE,ld.color); - //l->set_color(Light::COLOR_SPECULAR,Color(1,1,1)); - //l->approximate_opengl_attenuation(ld.constant_att,ld.linear_att,ld.quad_att); - node = l; - } - - } else { - node = memnew(Spatial); - } - } break; - case Collada::Node::TYPE_CAMERA: { - Collada::NodeCamera *cam = static_cast(p_node); - Camera *camera = memnew(Camera); - - if (collada.state.camera_data_map.has(cam->camera)) { - const Collada::CameraData &cd = collada.state.camera_data_map[cam->camera]; - - switch (cd.mode) { - case Collada::CameraData::MODE_ORTHOGONAL: { - if (cd.orthogonal.y_mag) { - camera->set_keep_aspect_mode(Camera::KEEP_HEIGHT); - camera->set_orthogonal(cd.orthogonal.y_mag * 2.0, cd.z_near, cd.z_far); - - } else if (!cd.orthogonal.y_mag && cd.orthogonal.x_mag) { - camera->set_keep_aspect_mode(Camera::KEEP_WIDTH); - camera->set_orthogonal(cd.orthogonal.x_mag * 2.0, cd.z_near, cd.z_far); - } - - } break; - case Collada::CameraData::MODE_PERSPECTIVE: { - if (cd.perspective.y_fov) { - camera->set_perspective(cd.perspective.y_fov, cd.z_near, cd.z_far); - - } else if (!cd.perspective.y_fov && cd.perspective.x_fov) { - camera->set_perspective(cd.perspective.x_fov / cd.aspect, cd.z_near, cd.z_far); - } - - } break; - } - } - - node = camera; - - } break; - case Collada::Node::TYPE_GEOMETRY: { - Collada::NodeGeometry *ng = static_cast(p_node); - - if (collada.state.curve_data_map.has(ng->source)) { - node = memnew(Path); - } else { - //mesh since nothing else - node = memnew(MeshInstance); - } - } break; - case Collada::Node::TYPE_SKELETON: { - ERR_FAIL_COND_V(!skeleton_map.has(p_node), ERR_CANT_CREATE); - Skeleton *sk = skeleton_map[p_node]; - node = sk; - } break; - } - - if (p_node->name != "") { - node->set_name(p_node->name); - } - NodeMap nm; - nm.node = node; - node_map[p_node->id] = nm; - node_name_map[node->get_name()] = p_node->id; - Transform xf = p_node->default_transform; - - xf = collada.fix_transform(xf) * p_node->post_transform; - node->set_transform(xf); - p_parent->add_child(node); - node->set_owner(scene); - - if (p_node->empty_draw_type != "") { - node->set_meta("empty_draw_type", Variant(p_node->empty_draw_type)); - } - - for (int i = 0; i < p_node->children.size(); i++) { - Error err = _create_scene(p_node->children[i], node); - if (err) { - return err; - } - } - return OK; -} - -Error ColladaImport::_create_material(const String &p_target) { - ERR_FAIL_COND_V(material_cache.has(p_target), ERR_ALREADY_EXISTS); - ERR_FAIL_COND_V(!collada.state.material_map.has(p_target), ERR_INVALID_PARAMETER); - Collada::Material &src_mat = collada.state.material_map[p_target]; - ERR_FAIL_COND_V(!collada.state.effect_map.has(src_mat.instance_effect), ERR_INVALID_PARAMETER); - Collada::Effect &effect = collada.state.effect_map[src_mat.instance_effect]; - - Ref material = memnew(SpatialMaterial); - - if (src_mat.name != "") { - material->set_name(src_mat.name); - } else if (effect.name != "") { - material->set_name(effect.name); - } - - // DIFFUSE - - if (effect.diffuse.texture != "") { - String texfile = effect.get_texture_path(effect.diffuse.texture, collada); - if (texfile != "") { - if (texfile.begins_with("/")) { - texfile = texfile.replace_first("/", "res://"); - } - Ref texture = ResourceLoader::load(texfile, "Texture"); - if (texture.is_valid()) { - material->set_texture(SpatialMaterial::TEXTURE_ALBEDO, texture); - material->set_albedo(Color(1, 1, 1, 1)); - //material->set_parameter(SpatialMaterial::PARAM_DIFFUSE,Color(1,1,1,1)); - } else { - missing_textures.push_back(texfile.get_file()); - } - } - } else { - material->set_albedo(effect.diffuse.color); - } - - // SPECULAR - - if (effect.specular.texture != "") { - String texfile = effect.get_texture_path(effect.specular.texture, collada); - if (texfile != "") { - if (texfile.begins_with("/")) { - texfile = texfile.replace_first("/", "res://"); - } - - Ref texture = ResourceLoader::load(texfile, "Texture"); - if (texture.is_valid()) { - material->set_texture(SpatialMaterial::TEXTURE_METALLIC, texture); - material->set_specular(1.0); - - //material->set_texture(SpatialMaterial::PARAM_SPECULAR,texture); - //material->set_parameter(SpatialMaterial::PARAM_SPECULAR,Color(1,1,1,1)); - } else { - missing_textures.push_back(texfile.get_file()); - } - } - - } else { - material->set_metallic(effect.specular.color.get_v()); - } - - // EMISSION - - if (effect.emission.texture != "") { - String texfile = effect.get_texture_path(effect.emission.texture, collada); - if (texfile != "") { - if (texfile.begins_with("/")) { - texfile = texfile.replace_first("/", "res://"); - } - - Ref texture = ResourceLoader::load(texfile, "Texture"); - if (texture.is_valid()) { - material->set_feature(SpatialMaterial::FEATURE_EMISSION, true); - material->set_texture(SpatialMaterial::TEXTURE_EMISSION, texture); - material->set_emission(Color(1, 1, 1, 1)); - - //material->set_parameter(SpatialMaterial::PARAM_EMISSION,Color(1,1,1,1)); - } else { - missing_textures.push_back(texfile.get_file()); - } - } - } else { - if (effect.emission.color != Color()) { - material->set_feature(SpatialMaterial::FEATURE_EMISSION, true); - material->set_emission(effect.emission.color); - } - } - - // NORMAL - - if (effect.bump.texture != "") { - String texfile = effect.get_texture_path(effect.bump.texture, collada); - if (texfile != "") { - if (texfile.begins_with("/")) { - texfile = texfile.replace_first("/", "res://"); - } - - Ref texture = ResourceLoader::load(texfile, "Texture"); - if (texture.is_valid()) { - material->set_feature(SpatialMaterial::FEATURE_NORMAL_MAPPING, true); - material->set_texture(SpatialMaterial::TEXTURE_NORMAL, texture); - //material->set_emission(Color(1,1,1,1)); - - //material->set_texture(SpatialMaterial::PARAM_NORMAL,texture); - } else { - //missing_textures.push_back(texfile.get_file()); - } - } - } - - float roughness = (effect.shininess - 1.0) / 510; - material->set_roughness(roughness); - - if (effect.double_sided) { - material->set_cull_mode(SpatialMaterial::CULL_DISABLED); - } - material->set_flag(SpatialMaterial::FLAG_UNSHADED, effect.unshaded); - - material_cache[p_target] = material; - return OK; -} - -Error ColladaImport::_create_mesh_surfaces(bool p_optimize, Ref &p_mesh, const Map &p_material_map, const Collada::MeshData &meshdata, const Transform &p_local_xform, const Vector &bone_remap, const Collada::SkinControllerData *p_skin_controller, const Collada::MorphControllerData *p_morph_data, Vector> p_morph_meshes, uint32_t p_use_compression, bool p_use_mesh_material) { - bool local_xform_mirror = p_local_xform.basis.determinant() < 0; - - if (p_morph_data) { - //add morphie target - ERR_FAIL_COND_V(!p_morph_data->targets.has("MORPH_TARGET"), ERR_INVALID_DATA); - String mt = p_morph_data->targets["MORPH_TARGET"]; - ERR_FAIL_COND_V(!p_morph_data->sources.has(mt), ERR_INVALID_DATA); - int morph_targets = p_morph_data->sources[mt].sarray.size(); - for (int i = 0; i < morph_targets; i++) { - String target = p_morph_data->sources[mt].sarray[i]; - ERR_FAIL_COND_V(!collada.state.mesh_data_map.has(target), ERR_INVALID_DATA); - String name = collada.state.mesh_data_map[target].name; - - p_mesh->add_blend_shape(name); - } - if (p_morph_data->mode == "RELATIVE") { - p_mesh->set_blend_shape_mode(Mesh::BLEND_SHAPE_MODE_RELATIVE); - } else if (p_morph_data->mode == "NORMALIZED") { - p_mesh->set_blend_shape_mode(Mesh::BLEND_SHAPE_MODE_NORMALIZED); - } - } - - int surface = 0; - for (int p_i = 0; p_i < meshdata.primitives.size(); p_i++) { - const Collada::MeshData::Primitives &p = meshdata.primitives[p_i]; - - /* VERTEX SOURCE */ - ERR_FAIL_COND_V(!p.sources.has("VERTEX"), ERR_INVALID_DATA); - - String vertex_src_id = p.sources["VERTEX"].source; - int vertex_ofs = p.sources["VERTEX"].offset; - - ERR_FAIL_COND_V(!meshdata.vertices.has(vertex_src_id), ERR_INVALID_DATA); - - ERR_FAIL_COND_V(!meshdata.vertices[vertex_src_id].sources.has("POSITION"), ERR_INVALID_DATA); - String position_src_id = meshdata.vertices[vertex_src_id].sources["POSITION"]; - - ERR_FAIL_COND_V(!meshdata.sources.has(position_src_id), ERR_INVALID_DATA); - - const Collada::MeshData::Source *vertex_src = &meshdata.sources[position_src_id]; - - /* NORMAL SOURCE */ - - const Collada::MeshData::Source *normal_src = nullptr; - int normal_ofs = 0; - - { - String normal_source_id = ""; - - if (p.sources.has("NORMAL")) { - normal_source_id = p.sources["NORMAL"].source; - normal_ofs = p.sources["NORMAL"].offset; - } else if (meshdata.vertices[vertex_src_id].sources.has("NORMAL")) { - normal_source_id = meshdata.vertices[vertex_src_id].sources["NORMAL"]; - normal_ofs = vertex_ofs; - } - - if (normal_source_id != "") { - ERR_FAIL_COND_V(!meshdata.sources.has(normal_source_id), ERR_INVALID_DATA); - normal_src = &meshdata.sources[normal_source_id]; - } - } - - const Collada::MeshData::Source *binormal_src = nullptr; - int binormal_ofs = 0; - - { - String binormal_source_id = ""; - - if (p.sources.has("TEXBINORMAL")) { - binormal_source_id = p.sources["TEXBINORMAL"].source; - binormal_ofs = p.sources["TEXBINORMAL"].offset; - } else if (meshdata.vertices[vertex_src_id].sources.has("TEXBINORMAL")) { - binormal_source_id = meshdata.vertices[vertex_src_id].sources["TEXBINORMAL"]; - binormal_ofs = vertex_ofs; - } - - if (binormal_source_id != "") { - ERR_FAIL_COND_V(!meshdata.sources.has(binormal_source_id), ERR_INVALID_DATA); - binormal_src = &meshdata.sources[binormal_source_id]; - } - } - - const Collada::MeshData::Source *tangent_src = nullptr; - int tangent_ofs = 0; - - { - String tangent_source_id = ""; - - if (p.sources.has("TEXTANGENT")) { - tangent_source_id = p.sources["TEXTANGENT"].source; - tangent_ofs = p.sources["TEXTANGENT"].offset; - } else if (meshdata.vertices[vertex_src_id].sources.has("TEXTANGENT")) { - tangent_source_id = meshdata.vertices[vertex_src_id].sources["TEXTANGENT"]; - tangent_ofs = vertex_ofs; - } - - if (tangent_source_id != "") { - ERR_FAIL_COND_V(!meshdata.sources.has(tangent_source_id), ERR_INVALID_DATA); - tangent_src = &meshdata.sources[tangent_source_id]; - } - } - - const Collada::MeshData::Source *uv_src = nullptr; - int uv_ofs = 0; - - { - String uv_source_id = ""; - - if (p.sources.has("TEXCOORD0")) { - uv_source_id = p.sources["TEXCOORD0"].source; - uv_ofs = p.sources["TEXCOORD0"].offset; - } else if (meshdata.vertices[vertex_src_id].sources.has("TEXCOORD0")) { - uv_source_id = meshdata.vertices[vertex_src_id].sources["TEXCOORD0"]; - uv_ofs = vertex_ofs; - } - - if (uv_source_id != "") { - ERR_FAIL_COND_V(!meshdata.sources.has(uv_source_id), ERR_INVALID_DATA); - uv_src = &meshdata.sources[uv_source_id]; - } - } - - const Collada::MeshData::Source *uv2_src = nullptr; - int uv2_ofs = 0; - - { - String uv2_source_id = ""; - - if (p.sources.has("TEXCOORD1")) { - uv2_source_id = p.sources["TEXCOORD1"].source; - uv2_ofs = p.sources["TEXCOORD1"].offset; - } else if (meshdata.vertices[vertex_src_id].sources.has("TEXCOORD1")) { - uv2_source_id = meshdata.vertices[vertex_src_id].sources["TEXCOORD1"]; - uv2_ofs = vertex_ofs; - } - - if (uv2_source_id != "") { - ERR_FAIL_COND_V(!meshdata.sources.has(uv2_source_id), ERR_INVALID_DATA); - uv2_src = &meshdata.sources[uv2_source_id]; - } - } - - const Collada::MeshData::Source *color_src = nullptr; - int color_ofs = 0; - - { - String color_source_id = ""; - - if (p.sources.has("COLOR")) { - color_source_id = p.sources["COLOR"].source; - color_ofs = p.sources["COLOR"].offset; - } else if (meshdata.vertices[vertex_src_id].sources.has("COLOR")) { - color_source_id = meshdata.vertices[vertex_src_id].sources["COLOR"]; - color_ofs = vertex_ofs; - } - - if (color_source_id != "") { - ERR_FAIL_COND_V(!meshdata.sources.has(color_source_id), ERR_INVALID_DATA); - color_src = &meshdata.sources[color_source_id]; - } - } - - //find largest source.. - - /************************/ - /* ADD WEIGHTS IF EXIST */ - /************************/ - - Map> pre_weights; - - bool has_weights = false; - - if (p_skin_controller) { - const Collada::SkinControllerData::Source *weight_src = nullptr; - int weight_ofs = 0; - - if (p_skin_controller->weights.sources.has("WEIGHT")) { - String weight_id = p_skin_controller->weights.sources["WEIGHT"].source; - weight_ofs = p_skin_controller->weights.sources["WEIGHT"].offset; - if (p_skin_controller->sources.has(weight_id)) { - weight_src = &p_skin_controller->sources[weight_id]; - } - } - - int joint_ofs = 0; - - if (p_skin_controller->weights.sources.has("JOINT")) { - joint_ofs = p_skin_controller->weights.sources["JOINT"].offset; - } - - //should be OK, given this was pre-checked. - - int index_ofs = 0; - int wstride = p_skin_controller->weights.sources.size(); - for (int w_i = 0; w_i < p_skin_controller->weights.sets.size(); w_i++) { - int amount = p_skin_controller->weights.sets[w_i]; - - Vector weights; - - for (int a_i = 0; a_i < amount; a_i++) { - Collada::Vertex::Weight w; - - int read_from = index_ofs + a_i * wstride; - ERR_FAIL_INDEX_V(read_from + wstride - 1, p_skin_controller->weights.indices.size(), ERR_INVALID_DATA); - int weight_index = p_skin_controller->weights.indices[read_from + weight_ofs]; - ERR_FAIL_INDEX_V(weight_index, weight_src->array.size(), ERR_INVALID_DATA); - - w.weight = weight_src->array[weight_index]; - - int bone_index = p_skin_controller->weights.indices[read_from + joint_ofs]; - if (bone_index == -1) { - continue; //ignore this weight (refers to bind shape) - } - ERR_FAIL_INDEX_V(bone_index, bone_remap.size(), ERR_INVALID_DATA); - - w.bone_idx = bone_remap[bone_index]; - - weights.push_back(w); - } - - /* FIX WEIGHTS */ - - weights.sort(); - - if (weights.size() > 4) { - //cap to 4 and make weights add up 1 - weights.resize(4); - } - - //make sure weights always add up to 1 - float total = 0; - for (int i = 0; i < weights.size(); i++) { - total += weights[i].weight; - } - if (total) { - for (int i = 0; i < weights.size(); i++) { - weights.write[i].weight /= total; - } - } - - if (weights.size() == 0 || total == 0) { //if nothing, add a weight to bone 0 - //no weights assigned - Collada::Vertex::Weight w; - w.bone_idx = 0; - w.weight = 1.0; - weights.clear(); - weights.push_back(w); - } - - pre_weights[w_i] = weights; - - index_ofs += wstride * amount; - } - - //vertices need to be localized - has_weights = true; - } - - Set vertex_set; //vertex set will be the vertices - List indices_list; //indices will be the indices - - /**************************/ - /* CREATE PRIMITIVE ARRAY */ - /**************************/ - - // The way collada uses indices is more optimal, and friendlier with 3D modelling software, - // because it can index everything, not only vertices (similar to how the WII works). - // This is, however, more incompatible with standard video cards, so arrays must be converted. - // Must convert to GL/DX format. - - int _prim_ofs = 0; - int vertidx = 0; - for (int p_j = 0; p_j < p.count; p_j++) { - int amount; - if (p.polygons.size()) { - ERR_FAIL_INDEX_V(p_j, p.polygons.size(), ERR_INVALID_DATA); - amount = p.polygons[p_j]; - } else { - amount = 3; //triangles; - } - - //COLLADA_PRINT("amount: "+itos(amount)); - - int prev2[2] = { 0, 0 }; - - for (int j = 0; j < amount; j++) { - int src = _prim_ofs; - //_prim_ofs+=p.sources.size() - - ERR_FAIL_INDEX_V(src, p.indices.size(), ERR_INVALID_DATA); - - Collada::Vertex vertex; - if (!p_optimize) { - vertex.uid = vertidx++; - } - - int vertex_index = p.indices[src + vertex_ofs]; //used for index field (later used by controllers) - int vertex_pos = (vertex_src->stride ? vertex_src->stride : 3) * vertex_index; - ERR_FAIL_INDEX_V(vertex_pos + 0, vertex_src->array.size(), ERR_INVALID_DATA); - ERR_FAIL_INDEX_V(vertex_pos + 2, vertex_src->array.size(), ERR_INVALID_DATA); - vertex.vertex = Vector3(vertex_src->array[vertex_pos + 0], vertex_src->array[vertex_pos + 1], vertex_src->array[vertex_pos + 2]); - - if (pre_weights.has(vertex_index)) { - vertex.weights = pre_weights[vertex_index]; - } - - if (normal_src) { - int normal_pos = (normal_src->stride ? normal_src->stride : 3) * p.indices[src + normal_ofs]; - ERR_FAIL_INDEX_V(normal_pos + 0, normal_src->array.size(), ERR_INVALID_DATA); - ERR_FAIL_INDEX_V(normal_pos + 2, normal_src->array.size(), ERR_INVALID_DATA); - vertex.normal = Vector3(normal_src->array[normal_pos + 0], normal_src->array[normal_pos + 1], normal_src->array[normal_pos + 2]); - - if (tangent_src && binormal_src) { - int binormal_pos = (binormal_src->stride ? binormal_src->stride : 3) * p.indices[src + binormal_ofs]; - ERR_FAIL_INDEX_V(binormal_pos + 0, binormal_src->array.size(), ERR_INVALID_DATA); - ERR_FAIL_INDEX_V(binormal_pos + 2, binormal_src->array.size(), ERR_INVALID_DATA); - Vector3 binormal = Vector3(binormal_src->array[binormal_pos + 0], binormal_src->array[binormal_pos + 1], binormal_src->array[binormal_pos + 2]); - - int tangent_pos = (tangent_src->stride ? tangent_src->stride : 3) * p.indices[src + tangent_ofs]; - ERR_FAIL_INDEX_V(tangent_pos + 0, tangent_src->array.size(), ERR_INVALID_DATA); - ERR_FAIL_INDEX_V(tangent_pos + 2, tangent_src->array.size(), ERR_INVALID_DATA); - Vector3 tangent = Vector3(tangent_src->array[tangent_pos + 0], tangent_src->array[tangent_pos + 1], tangent_src->array[tangent_pos + 2]); - - vertex.tangent.normal = tangent; - vertex.tangent.d = vertex.normal.cross(tangent).dot(binormal) > 0 ? 1 : -1; - } - } - - if (uv_src) { - int uv_pos = (uv_src->stride ? uv_src->stride : 2) * p.indices[src + uv_ofs]; - ERR_FAIL_INDEX_V(uv_pos + 0, uv_src->array.size(), ERR_INVALID_DATA); - ERR_FAIL_INDEX_V(uv_pos + 1, uv_src->array.size(), ERR_INVALID_DATA); - vertex.uv = Vector3(uv_src->array[uv_pos + 0], 1.0 - uv_src->array[uv_pos + 1], 0); - } - - if (uv2_src) { - int uv2_pos = (uv2_src->stride ? uv2_src->stride : 2) * p.indices[src + uv2_ofs]; - ERR_FAIL_INDEX_V(uv2_pos + 0, uv2_src->array.size(), ERR_INVALID_DATA); - ERR_FAIL_INDEX_V(uv2_pos + 1, uv2_src->array.size(), ERR_INVALID_DATA); - vertex.uv2 = Vector3(uv2_src->array[uv2_pos + 0], 1.0 - uv2_src->array[uv2_pos + 1], 0); - } - - if (color_src) { - int color_pos = (color_src->stride ? color_src->stride : 3) * p.indices[src + color_ofs]; // colors are RGB in collada.. - ERR_FAIL_INDEX_V(color_pos + 0, color_src->array.size(), ERR_INVALID_DATA); - ERR_FAIL_INDEX_V(color_pos + ((color_src->stride > 3) ? 3 : 2), color_src->array.size(), ERR_INVALID_DATA); - vertex.color = Color(color_src->array[color_pos + 0], color_src->array[color_pos + 1], color_src->array[color_pos + 2], (color_src->stride > 3) ? color_src->array[color_pos + 3] : 1.0); - } - -#ifndef NO_UP_AXIS_SWAP - if (collada.state.up_axis == Vector3::AXIS_Z) { - Vector3 bn = vertex.normal.cross(vertex.tangent.normal) * vertex.tangent.d; - - SWAP(vertex.vertex.z, vertex.vertex.y); - vertex.vertex.z = -vertex.vertex.z; - SWAP(vertex.normal.z, vertex.normal.y); - vertex.normal.z = -vertex.normal.z; - SWAP(vertex.tangent.normal.z, vertex.tangent.normal.y); - vertex.tangent.normal.z = -vertex.tangent.normal.z; - SWAP(bn.z, bn.y); - bn.z = -bn.z; - - vertex.tangent.d = vertex.normal.cross(vertex.tangent.normal).dot(bn) > 0 ? 1 : -1; - } - -#endif - - vertex.fix_unit_scale(collada); - int index = 0; - //COLLADA_PRINT("vertex: "+vertex.vertex); - - if (vertex_set.has(vertex)) { - index = vertex_set.find(vertex)->get().idx; - } else { - index = vertex_set.size(); - vertex.idx = index; - vertex_set.insert(vertex); - } - - //build triangles if needed - if (j == 0) { - prev2[0] = index; - } - - if (j >= 2) { - //insert indices in reverse order (collada uses CCW as frontface) - if (local_xform_mirror) { - indices_list.push_back(prev2[0]); - indices_list.push_back(prev2[1]); - indices_list.push_back(index); - - } else { - indices_list.push_back(prev2[0]); - indices_list.push_back(index); - indices_list.push_back(prev2[1]); - } - } - - prev2[1] = index; - _prim_ofs += p.vertex_size; - } - } - - Vector vertex_array; //there we go, vertex array - - vertex_array.resize(vertex_set.size()); - for (Set::Element *F = vertex_set.front(); F; F = F->next()) { - vertex_array.write[F->get().idx] = F->get(); - } - - if (has_weights) { - //if skeleton, localize - Transform local_xform = p_local_xform; - for (int i = 0; i < vertex_array.size(); i++) { - vertex_array.write[i].vertex = local_xform.xform(vertex_array[i].vertex); - vertex_array.write[i].normal = local_xform.basis.xform(vertex_array[i].normal).normalized(); - vertex_array.write[i].tangent.normal = local_xform.basis.xform(vertex_array[i].tangent.normal).normalized(); - if (local_xform_mirror) { - //i shouldn't do this? wtf? - //vertex_array[i].normal*=-1.0; - //vertex_array[i].tangent.normal*=-1.0; - } - } - } - - /*****************/ - /* MAKE SURFACES */ - /*****************/ - - { - Ref material; - - { - if (p_material_map.has(p.material)) { - String target = p_material_map[p.material].target; - - if (!material_cache.has(target)) { - Error err = _create_material(target); - if (!err) { - material = material_cache[target]; - } - } else { - material = material_cache[target]; - } - - } else if (p.material != "") { - WARN_PRINT("Collada: Unreferenced material in geometry instance: " + p.material); - } - } - - Ref surftool; - surftool.instance(); - surftool->begin(Mesh::PRIMITIVE_TRIANGLES); - - for (int k = 0; k < vertex_array.size(); k++) { - if (normal_src) { - surftool->add_normal(vertex_array[k].normal); - if (binormal_src && tangent_src) { - surftool->add_tangent(vertex_array[k].tangent); - } - } - if (uv_src) { - surftool->add_uv(Vector2(vertex_array[k].uv.x, vertex_array[k].uv.y)); - } - if (uv2_src) { - surftool->add_uv2(Vector2(vertex_array[k].uv2.x, vertex_array[k].uv2.y)); - } - if (color_src) { - surftool->add_color(vertex_array[k].color); - } - - if (has_weights) { - Vector weights; - Vector bones; - weights.resize(VS::ARRAY_WEIGHTS_SIZE); - bones.resize(VS::ARRAY_WEIGHTS_SIZE); - //float sum=0.0; - for (int l = 0; l < VS::ARRAY_WEIGHTS_SIZE; l++) { - if (l < vertex_array[k].weights.size()) { - weights.write[l] = vertex_array[k].weights[l].weight; - bones.write[l] = vertex_array[k].weights[l].bone_idx; - //sum += vertex_array[k].weights[l].weight; - } else { - weights.write[l] = 0; - bones.write[l] = 0; - } - } - - surftool->add_bones(bones); - surftool->add_weights(weights); - } - - surftool->add_vertex(vertex_array[k].vertex); - } - - for (List::Element *E = indices_list.front(); E; E = E->next()) { - surftool->add_index(E->get()); - } - - if (!normal_src) { - //should always be normals - surftool->generate_normals(); - } - - if ((!binormal_src || !tangent_src) && normal_src && uv_src && force_make_tangents) { - surftool->generate_tangents(); - } - - //////////////////////////// - // FINALLY CREATE SUFRACE // - //////////////////////////// - - Array d = surftool->commit_to_arrays(); - d.resize(VS::ARRAY_MAX); - - Array mr; - - //////////////////////////// - // THEN THE MORPH TARGETS // - //////////////////////////// - - for (int mi = 0; mi < p_morph_meshes.size(); mi++) { - Array a = p_morph_meshes[mi]->surface_get_arrays(surface); - //add valid weight and bone arrays if they exist, TODO check if they are unique to shape (generally not) - - if (has_weights) { - a[Mesh::ARRAY_WEIGHTS] = d[Mesh::ARRAY_WEIGHTS]; - a[Mesh::ARRAY_BONES] = d[Mesh::ARRAY_BONES]; - } - - a[Mesh::ARRAY_INDEX] = Variant(); - //a.resize(Mesh::ARRAY_MAX); //no need for index - mr.push_back(a); - } - - p_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, d, mr, p_use_compression); - - if (material.is_valid()) { - if (p_use_mesh_material) { - p_mesh->surface_set_material(surface, material); - } - p_mesh->surface_set_name(surface, material->get_name()); - } - } - - /*****************/ - /* FIND MATERIAL */ - /*****************/ - - surface++; - } - - return OK; -} - -Error ColladaImport::_create_resources(Collada::Node *p_node, uint32_t p_use_compression) { - if (p_node->type == Collada::Node::TYPE_GEOMETRY && node_map.has(p_node->id)) { - Spatial *node = node_map[p_node->id].node; - Collada::NodeGeometry *ng = static_cast(p_node); - - if (Object::cast_to(node)) { - Path *path = Object::cast_to(node); - - if (curve_cache.has(ng->source)) { - path->set_curve(curve_cache[ng->source]); - } else { - Ref c = memnew(Curve3D); - - const Collada::CurveData &cd = collada.state.curve_data_map[ng->source]; - - ERR_FAIL_COND_V(!cd.control_vertices.has("POSITION"), ERR_INVALID_DATA); - ERR_FAIL_COND_V(!cd.control_vertices.has("IN_TANGENT"), ERR_INVALID_DATA); - ERR_FAIL_COND_V(!cd.control_vertices.has("OUT_TANGENT"), ERR_INVALID_DATA); - ERR_FAIL_COND_V(!cd.control_vertices.has("INTERPOLATION"), ERR_INVALID_DATA); - - ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["POSITION"]), ERR_INVALID_DATA); - const Collada::CurveData::Source &vertices = cd.sources[cd.control_vertices["POSITION"]]; - ERR_FAIL_COND_V(vertices.stride != 3, ERR_INVALID_DATA); - - ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["IN_TANGENT"]), ERR_INVALID_DATA); - const Collada::CurveData::Source &in_tangents = cd.sources[cd.control_vertices["IN_TANGENT"]]; - ERR_FAIL_COND_V(in_tangents.stride != 3, ERR_INVALID_DATA); - - ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["OUT_TANGENT"]), ERR_INVALID_DATA); - const Collada::CurveData::Source &out_tangents = cd.sources[cd.control_vertices["OUT_TANGENT"]]; - ERR_FAIL_COND_V(out_tangents.stride != 3, ERR_INVALID_DATA); - - ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["INTERPOLATION"]), ERR_INVALID_DATA); - const Collada::CurveData::Source &interps = cd.sources[cd.control_vertices["INTERPOLATION"]]; - ERR_FAIL_COND_V(interps.stride != 1, ERR_INVALID_DATA); - - const Collada::CurveData::Source *tilts = nullptr; - if (cd.control_vertices.has("TILT") && cd.sources.has(cd.control_vertices["TILT"])) { - tilts = &cd.sources[cd.control_vertices["TILT"]]; - } - - int pc = vertices.array.size() / 3; - for (int i = 0; i < pc; i++) { - Vector3 pos(vertices.array[i * 3 + 0], vertices.array[i * 3 + 1], vertices.array[i * 3 + 2]); - Vector3 in(in_tangents.array[i * 3 + 0], in_tangents.array[i * 3 + 1], in_tangents.array[i * 3 + 2]); - Vector3 out(out_tangents.array[i * 3 + 0], out_tangents.array[i * 3 + 1], out_tangents.array[i * 3 + 2]); - -#ifndef NO_UP_AXIS_SWAP - if (collada.state.up_axis == Vector3::AXIS_Z) { - SWAP(pos.y, pos.z); - pos.z = -pos.z; - SWAP(in.y, in.z); - in.z = -in.z; - SWAP(out.y, out.z); - out.z = -out.z; - } -#endif - pos *= collada.state.unit_scale; - in *= collada.state.unit_scale; - out *= collada.state.unit_scale; - - c->add_point(pos, in - pos, out - pos); - if (tilts) { - c->set_point_tilt(i, tilts->array[i]); - } - } - - curve_cache[ng->source] = c; - path->set_curve(c); - } - } - - if (Object::cast_to(node)) { - Collada::NodeGeometry *ng2 = static_cast(p_node); - - MeshInstance *mi = Object::cast_to(node); - - ERR_FAIL_COND_V(!mi, ERR_BUG); - - Collada::SkinControllerData *skin = nullptr; - Collada::MorphControllerData *morph = nullptr; - String meshid; - Transform apply_xform; - Vector bone_remap; - Vector> morphs; - - if (ng2->controller) { - String ngsource = ng2->source; - - if (collada.state.skin_controller_data_map.has(ngsource)) { - ERR_FAIL_COND_V(!collada.state.skin_controller_data_map.has(ngsource), ERR_INVALID_DATA); - skin = &collada.state.skin_controller_data_map[ngsource]; - - Vector skeletons = ng2->skeletons; - - ERR_FAIL_COND_V(skeletons.empty(), ERR_INVALID_DATA); - - String skname = skeletons[0]; - ERR_FAIL_COND_V(!node_map.has(skname), ERR_INVALID_DATA); - NodeMap nmsk = node_map[skname]; - Skeleton *sk = Object::cast_to(nmsk.node); - ERR_FAIL_COND_V(!sk, ERR_INVALID_DATA); - ERR_FAIL_COND_V(!skeleton_bone_map.has(sk), ERR_INVALID_DATA); - Map &bone_remap_map = skeleton_bone_map[sk]; - - meshid = skin->base; - - if (collada.state.morph_controller_data_map.has(meshid)) { - //it's a morph!! - morph = &collada.state.morph_controller_data_map[meshid]; - ngsource = meshid; - meshid = morph->mesh; - } else { - ngsource = ""; - } - - if (apply_mesh_xform_to_vertices) { - apply_xform = collada.fix_transform(p_node->default_transform); - node->set_transform(Transform()); - } else { - apply_xform = Transform(); - } - - ERR_FAIL_COND_V(!skin->weights.sources.has("JOINT"), ERR_INVALID_DATA); - - String joint_id = skin->weights.sources["JOINT"].source; - ERR_FAIL_COND_V(!skin->sources.has(joint_id), ERR_INVALID_DATA); - - Collada::SkinControllerData::Source *joint_src = &skin->sources[joint_id]; - - bone_remap.resize(joint_src->sarray.size()); - - for (int i = 0; i < bone_remap.size(); i++) { - String str = joint_src->sarray[i]; - ERR_FAIL_COND_V(!bone_remap_map.has(str), ERR_INVALID_DATA); - bone_remap.write[i] = bone_remap_map[str]; - } - } - - if (collada.state.morph_controller_data_map.has(ngsource)) { - //it's a morph!! - morph = &collada.state.morph_controller_data_map[ngsource]; - meshid = morph->mesh; - - if (morph->targets.has("MORPH_TARGET")) { - String target = morph->targets["MORPH_TARGET"]; - bool valid = false; - if (morph->sources.has(target)) { - valid = true; - Vector names = morph->sources[target].sarray; - for (int i = 0; i < names.size(); i++) { - String meshid2 = names[i]; - if (collada.state.mesh_data_map.has(meshid2)) { - Ref mesh = Ref(memnew(ArrayMesh)); - const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid2]; - mesh->set_name(meshdata.name); - Error err = _create_mesh_surfaces(false, mesh, ng2->material_map, meshdata, apply_xform, bone_remap, skin, nullptr, Vector>(), false); - ERR_FAIL_COND_V(err, err); - - morphs.push_back(mesh); - } else { - valid = false; - } - } - } - - if (!valid) { - morphs.clear(); - } - ngsource = ""; - } - } - - ERR_FAIL_COND_V_MSG(ngsource != "", ERR_INVALID_DATA, "Controller instance source '" + ngsource + "' is neither skin or morph!"); - - } else { - meshid = ng2->source; - } - - Ref mesh; - if (mesh_cache.has(meshid)) { - mesh = mesh_cache[meshid]; - } else { - if (collada.state.mesh_data_map.has(meshid)) { - //bleh, must ignore invalid - - ERR_FAIL_COND_V(!collada.state.mesh_data_map.has(meshid), ERR_INVALID_DATA); - mesh = Ref(memnew(ArrayMesh)); - const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid]; - mesh->set_name(meshdata.name); - Error err = _create_mesh_surfaces(morphs.size() == 0, mesh, ng2->material_map, meshdata, apply_xform, bone_remap, skin, morph, morphs, p_use_compression, use_mesh_builtin_materials); - ERR_FAIL_COND_V_MSG(err, err, "Cannot create mesh surface."); - - mesh_cache[meshid] = mesh; - } else { - WARN_PRINT("Collada: Will not import geometry: " + meshid); - } - } - - if (!mesh.is_null()) { - mi->set_mesh(mesh); - if (!use_mesh_builtin_materials) { - const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid]; - - for (int i = 0; i < meshdata.primitives.size(); i++) { - String matname = meshdata.primitives[i].material; - - if (ng2->material_map.has(matname)) { - String target = ng2->material_map[matname].target; - - Ref material; - if (!material_cache.has(target)) { - Error err = _create_material(target); - if (!err) { - material = material_cache[target]; - } - } else { - material = material_cache[target]; - } - - mi->set_surface_material(i, material); - } else if (matname != "") { - WARN_PRINT("Collada: Unreferenced material in geometry instance: " + matname); - } - } - } - } - } - } - - for (int i = 0; i < p_node->children.size(); i++) { - Error err = _create_resources(p_node->children[i], p_use_compression); - if (err) { - return err; - } - } - return OK; -} - -Error ColladaImport::load(const String &p_path, int p_flags, bool p_force_make_tangents, uint32_t p_use_compression) { - Error err = collada.load(p_path, p_flags); - ERR_FAIL_COND_V_MSG(err, err, "Cannot load file '" + p_path + "'."); - - force_make_tangents = p_force_make_tangents; - ERR_FAIL_COND_V(!collada.state.visual_scene_map.has(collada.state.root_visual_scene), ERR_INVALID_DATA); - Collada::VisualScene &vs = collada.state.visual_scene_map[collada.state.root_visual_scene]; - - scene = memnew(Spatial); // root - - //determine what's going on with the lights - for (int i = 0; i < vs.root_nodes.size(); i++) { - _pre_process_lights(vs.root_nodes[i]); - } - //import scene - - for (int i = 0; i < vs.root_nodes.size(); i++) { - Error err2 = _create_scene_skeletons(vs.root_nodes[i]); - if (err2 != OK) { - memdelete(scene); - ERR_FAIL_COND_V(err2, err2); - } - } - - for (int i = 0; i < vs.root_nodes.size(); i++) { - Error err2 = _create_scene(vs.root_nodes[i], scene); - if (err2 != OK) { - memdelete(scene); - ERR_FAIL_COND_V(err2, err2); - } - - Error err3 = _create_resources(vs.root_nodes[i], p_use_compression); - if (err3 != OK) { - memdelete(scene); - ERR_FAIL_COND_V(err3, err3); - } - } - - //optatively, set unit scale in the root - scene->set_transform(collada.get_root_transform()); - - return OK; -} - -void ColladaImport::_fix_param_animation_tracks() { - for (Map::Element *E = collada.state.scene_map.front(); E; E = E->next()) { - Collada::Node *n = E->get(); - switch (n->type) { - case Collada::Node::TYPE_NODE: { - // ? do nothing - } break; - case Collada::Node::TYPE_JOINT: { - } break; - case Collada::Node::TYPE_SKELETON: { - } break; - case Collada::Node::TYPE_LIGHT: { - } break; - case Collada::Node::TYPE_CAMERA: { - } break; - case Collada::Node::TYPE_GEOMETRY: { - Collada::NodeGeometry *ng = static_cast(n); - // test source(s) - String source = ng->source; - - while (source != "") { - if (collada.state.skin_controller_data_map.has(source)) { - const Collada::SkinControllerData &skin = collada.state.skin_controller_data_map[source]; - - //nothing to animate here i think - - source = skin.base; - } else if (collada.state.morph_controller_data_map.has(source)) { - const Collada::MorphControllerData &morph = collada.state.morph_controller_data_map[source]; - - if (morph.targets.has("MORPH_WEIGHT") && morph.targets.has("MORPH_TARGET")) { - String weights = morph.targets["MORPH_WEIGHT"]; - String targets = morph.targets["MORPH_TARGET"]; - //fails here - - if (morph.sources.has(targets) && morph.sources.has(weights)) { - const Collada::MorphControllerData::Source &weight_src = morph.sources[weights]; - const Collada::MorphControllerData::Source &target_src = morph.sources[targets]; - - ERR_FAIL_COND(weight_src.array.size() != target_src.sarray.size()); - - for (int i = 0; i < weight_src.array.size(); i++) { - String track_name = weights + "(" + itos(i) + ")"; - String mesh_name = target_src.sarray[i]; - if (collada.state.mesh_name_map.has(mesh_name) && collada.state.referenced_tracks.has(track_name)) { - const Vector &rt = collada.state.referenced_tracks[track_name]; - - for (int rti = 0; rti < rt.size(); rti++) { - Collada::AnimationTrack *at = &collada.state.animation_tracks.write[rt[rti]]; - - at->target = E->key(); - at->param = "morph/" + collada.state.mesh_name_map[mesh_name]; - at->property = true; - //at->param - } - } - } - } - } - source = morph.mesh; - } else { - source = ""; // for now nothing else supported - } - } - - } break; - } - } -} - -void ColladaImport::create_animations(bool p_make_tracks_in_all_bones, bool p_import_value_tracks) { - _fix_param_animation_tracks(); - for (int i = 0; i < collada.state.animation_clips.size(); i++) { - for (int j = 0; j < collada.state.animation_clips[i].tracks.size(); j++) { - tracks_in_clips.insert(collada.state.animation_clips[i].tracks[j]); - } - } - - for (int i = 0; i < collada.state.animation_tracks.size(); i++) { - const Collada::AnimationTrack &at = collada.state.animation_tracks[i]; - - String node; - - if (!node_map.has(at.target)) { - if (node_name_map.has(at.target)) { - node = node_name_map[at.target]; - } else { - WARN_PRINT("Collada: Couldn't find node: " + at.target); - continue; - } - } else { - node = at.target; - } - - if (at.property) { - valid_animated_properties.push_back(i); - - } else { - node_map[node].anim_tracks.push_back(i); - valid_animated_nodes.insert(node); - } - } - - create_animation(-1, p_make_tracks_in_all_bones, p_import_value_tracks); - for (int i = 0; i < collada.state.animation_clips.size(); i++) { - create_animation(i, p_make_tracks_in_all_bones, p_import_value_tracks); - } -} - -void ColladaImport::create_animation(int p_clip, bool p_make_tracks_in_all_bones, bool p_import_value_tracks) { - Ref animation = Ref(memnew(Animation)); - - if (p_clip == -1) { - animation->set_name("default"); - } else { - animation->set_name(collada.state.animation_clips[p_clip].name); - } - - for (Map::Element *E = node_map.front(); E; E = E->next()) { - if (E->get().bone < 0) { - continue; - } - bones_with_animation[E->key()] = false; - } - //store and validate tracks - - if (p_clip == -1) { - //main anim - } - - Set track_filter; - - if (p_clip == -1) { - for (int i = 0; i < collada.state.animation_clips.size(); i++) { - int tc = collada.state.animation_clips[i].tracks.size(); - for (int j = 0; j < tc; j++) { - String n = collada.state.animation_clips[i].tracks[j]; - if (collada.state.by_id_tracks.has(n)) { - const Vector &ti = collada.state.by_id_tracks[n]; - for (int k = 0; k < ti.size(); k++) { - track_filter.insert(ti[k]); - } - } - } - } - } else { - int tc = collada.state.animation_clips[p_clip].tracks.size(); - for (int j = 0; j < tc; j++) { - String n = collada.state.animation_clips[p_clip].tracks[j]; - if (collada.state.by_id_tracks.has(n)) { - const Vector &ti = collada.state.by_id_tracks[n]; - for (int k = 0; k < ti.size(); k++) { - track_filter.insert(ti[k]); - } - } - } - } - - //animation->set_loop(true); - //create animation tracks - - Vector base_snapshots; - - float f = 0; - float snapshot_interval = 1.0 / bake_fps; //should be customizable somewhere... - - float anim_length = collada.state.animation_length; - if (p_clip >= 0 && collada.state.animation_clips[p_clip].end) { - anim_length = collada.state.animation_clips[p_clip].end; - } - - while (f < anim_length) { - base_snapshots.push_back(f); - - f += snapshot_interval; - - if (f >= anim_length) { - base_snapshots.push_back(anim_length); - } - } - - animation->set_length(anim_length); - - bool tracks_found = false; - - for (Set::Element *E = valid_animated_nodes.front(); E; E = E->next()) { - // take snapshots - - if (!collada.state.scene_map.has(E->get())) { - continue; - } - - NodeMap &nm = node_map[E->get()]; - String path = scene->get_path_to(nm.node); - - if (nm.bone >= 0) { - Skeleton *sk = static_cast(nm.node); - String name = sk->get_bone_name(nm.bone); - path = path + ":" + name; - } - - bool found_anim = false; - - Collada::Node *cn = collada.state.scene_map[E->get()]; - if (cn->ignore_anim) { - continue; - } - - animation->add_track(Animation::TYPE_TRANSFORM); - int track = animation->get_track_count() - 1; - animation->track_set_path(track, path); - animation->track_set_imported(track, true); //helps merging later - - Vector snapshots = base_snapshots; - - if (nm.anim_tracks.size() == 1) { - //use snapshot keys from anim track instead, because this was most likely exported baked - const Collada::AnimationTrack &at = collada.state.animation_tracks[nm.anim_tracks.front()->get()]; - snapshots.clear(); - for (int i = 0; i < at.keys.size(); i++) { - snapshots.push_back(at.keys[i].time); - } - } - - for (int i = 0; i < snapshots.size(); i++) { - for (List::Element *ET = nm.anim_tracks.front(); ET; ET = ET->next()) { - //apply tracks - - if (p_clip == -1) { - if (track_filter.has(ET->get())) { - continue; - } - } else { - if (!track_filter.has(ET->get())) { - continue; - } - } - - found_anim = true; - - const Collada::AnimationTrack &at = collada.state.animation_tracks[ET->get()]; - - int xform_idx = -1; - for (int j = 0; j < cn->xform_list.size(); j++) { - if (cn->xform_list[j].id == at.param) { - xform_idx = j; - break; - } - } - - if (xform_idx == -1) { - WARN_PRINT("Collada: Couldn't find matching node " + at.target + " xform for track " + at.param + "."); - continue; - } - - Vector data = at.get_value_at_time(snapshots[i]); - ERR_CONTINUE(data.empty()); - - Collada::Node::XForm &xf = cn->xform_list.write[xform_idx]; - - if (at.component == "ANGLE") { - ERR_CONTINUE(data.size() != 1); - ERR_CONTINUE(xf.op != Collada::Node::XForm::OP_ROTATE); - ERR_CONTINUE(xf.data.size() < 4); - xf.data.write[3] = data[0]; - } else if (at.component == "X" || at.component == "Y" || at.component == "Z") { - int cn2 = at.component[0] - 'X'; - ERR_CONTINUE(cn2 >= xf.data.size()); - ERR_CONTINUE(data.size() > 1); - xf.data.write[cn2] = data[0]; - } else if (data.size() == xf.data.size()) { - xf.data = data; - } else { - ERR_CONTINUE_MSG(data.size() != xf.data.size(), "Component " + at.component + " has datasize " + itos(data.size()) + ", xfdatasize " + itos(xf.data.size()) + "."); - } - } - - Transform xform = cn->compute_transform(collada); - xform = collada.fix_transform(xform) * cn->post_transform; - - if (nm.bone >= 0) { - //make bone transform relative to rest (in case of skeleton) - Skeleton *sk = Object::cast_to(nm.node); - if (sk) { - xform = sk->get_bone_rest(nm.bone).affine_inverse() * xform; - } else { - ERR_PRINT("Collada: Invalid skeleton"); - } - } - - Vector3 s = xform.basis.get_scale(); - bool singular_matrix = Math::is_equal_approx(s.x, 0.0f) || Math::is_equal_approx(s.y, 0.0f) || Math::is_equal_approx(s.z, 0.0f); - Quat q = singular_matrix ? Quat() : xform.basis.get_rotation_quat(); - Vector3 l = xform.origin; - - animation->transform_track_insert_key(track, snapshots[i], l, q, s); - } - - if (nm.bone >= 0) { - if (found_anim) { - bones_with_animation[E->get()] = true; - } - } - - if (found_anim) { - tracks_found = true; - } else { - animation->remove_track(track); - } - } - - if (p_make_tracks_in_all_bones) { - //some bones may lack animation, but since we don't store pose as a property, we must add keyframes! - for (Map::Element *E = bones_with_animation.front(); E; E = E->next()) { - if (E->get()) { - continue; - } - - NodeMap &nm = node_map[E->key()]; - String path = scene->get_path_to(nm.node); - ERR_CONTINUE(nm.bone < 0); - Skeleton *sk = static_cast(nm.node); - String name = sk->get_bone_name(nm.bone); - path = path + ":" + name; - - Collada::Node *cn = collada.state.scene_map[E->key()]; - if (cn->ignore_anim) { - WARN_PRINT("Collada: Ignoring animation on node: " + path); - continue; - } - - animation->add_track(Animation::TYPE_TRANSFORM); - int track = animation->get_track_count() - 1; - animation->track_set_path(track, path); - animation->track_set_imported(track, true); //helps merging later - - Transform xform = cn->compute_transform(collada); - xform = collada.fix_transform(xform) * cn->post_transform; - - xform = sk->get_bone_rest(nm.bone).affine_inverse() * xform; - - Vector3 s = xform.basis.get_scale(); - bool singular_matrix = Math::is_equal_approx(s.x, 0.0f) || Math::is_equal_approx(s.y, 0.0f) || Math::is_equal_approx(s.z, 0.0f); - Quat q = singular_matrix ? Quat() : xform.basis.get_rotation_quat(); - Vector3 l = xform.origin; - - animation->transform_track_insert_key(track, 0, l, q, s); - - tracks_found = true; - } - } - - if (p_import_value_tracks) { - for (int i = 0; i < valid_animated_properties.size(); i++) { - int ti = valid_animated_properties[i]; - - if (p_clip == -1) { - if (track_filter.has(ti)) { - continue; - } - } else { - if (!track_filter.has(ti)) { - continue; - } - } - - const Collada::AnimationTrack &at = collada.state.animation_tracks[ti]; - - // take snapshots - if (!collada.state.scene_map.has(at.target)) { - continue; - } - - NodeMap &nm = node_map[at.target]; - String path = scene->get_path_to(nm.node); - - animation->add_track(Animation::TYPE_VALUE); - int track = animation->get_track_count() - 1; - - path = path + ":" + at.param; - animation->track_set_path(track, path); - animation->track_set_imported(track, true); //helps merging later - - for (int j = 0; j < at.keys.size(); j++) { - float time = at.keys[j].time; - Variant value; - Vector data = at.keys[j].data; - if (data.size() == 1) { - //push a float - value = data[0]; - - } else if (data.size() == 16) { - //matrix - WARN_PRINT("Collada: Value keys for matrices not supported."); - } else { - WARN_PRINT("Collada: Unexpected amount of value keys: " + itos(data.size())); - } - - animation->track_insert_key(track, time, value); - } - - tracks_found = true; - } - } - - if (tracks_found) { - animations.push_back(animation); - } -} - -/*********************************************************************************/ -/*************************************** SCENE ***********************************/ -/*********************************************************************************/ - -uint32_t EditorSceneImporterCollada::get_import_flags() const { - return IMPORT_SCENE | IMPORT_ANIMATION; -} -void EditorSceneImporterCollada::get_extensions(List *r_extensions) const { - r_extensions->push_back("dae"); -} -Node *EditorSceneImporterCollada::import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, uint32_t p_compress_flags, List *r_missing_deps, Error *r_err) { - ColladaImport state; - uint32_t flags = Collada::IMPORT_FLAG_SCENE; - if (p_flags & IMPORT_ANIMATION) { - flags |= Collada::IMPORT_FLAG_ANIMATION; - } - - state.use_mesh_builtin_materials = !(p_flags & IMPORT_MATERIALS_IN_INSTANCES); - state.bake_fps = p_bake_fps; - - Error err = state.load(p_path, flags, p_flags & EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS, p_compress_flags); - - ERR_FAIL_COND_V_MSG(err != OK, nullptr, "Cannot load scene from file '" + p_path + "'."); - - if (state.missing_textures.size()) { - /* - for(int i=0;ipush_back(state.missing_textures[i]); - } - } - } - - if (p_flags & IMPORT_ANIMATION) { - state.create_animations(p_flags & IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS, p_flags & EditorSceneImporter::IMPORT_ANIMATION_KEEP_VALUE_TRACKS); - AnimationPlayer *ap = memnew(AnimationPlayer); - for (int i = 0; i < state.animations.size(); i++) { - String name; - if (state.animations[i]->get_name() == "") { - name = "default"; - } else { - name = state.animations[i]->get_name(); - } - - if (p_flags & IMPORT_ANIMATION_DETECT_LOOP) { - if (name.begins_with("loop") || name.ends_with("loop") || name.begins_with("cycle") || name.ends_with("cycle")) { - state.animations.write[i]->set_loop(true); - } - } - - ap->add_animation(name, state.animations[i]); - } - state.scene->add_child(ap); - ap->set_owner(state.scene); - } - - return state.scene; -} - -Ref EditorSceneImporterCollada::import_animation(const String &p_path, uint32_t p_flags, int p_bake_fps) { - ColladaImport state; - - state.use_mesh_builtin_materials = false; - - Error err = state.load(p_path, Collada::IMPORT_FLAG_ANIMATION, p_flags & EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS); - ERR_FAIL_COND_V_MSG(err != OK, RES(), "Cannot load animation from file '" + p_path + "'."); - - state.create_animations(p_flags & EditorSceneImporter::IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS, p_flags & EditorSceneImporter::IMPORT_ANIMATION_KEEP_VALUE_TRACKS); - if (state.scene) { - memdelete(state.scene); - } - - if (state.animations.size() == 0) { - return Ref(); - } - Ref anim = state.animations[0]; - String base = p_path.get_basename().to_lower(); - if (p_flags & IMPORT_ANIMATION_DETECT_LOOP) { - if (base.begins_with("loop") || base.ends_with("loop") || base.begins_with("cycle") || base.ends_with("cycle")) { - anim->set_loop(true); - } - } - - return anim; -} - -EditorSceneImporterCollada::EditorSceneImporterCollada() { -} diff --git a/editor/import/editor_import_collada.h b/editor/import/editor_import_collada.h deleted file mode 100644 index 873a622c7..000000000 --- a/editor/import/editor_import_collada.h +++ /dev/null @@ -1,48 +0,0 @@ -/*************************************************************************/ -/* editor_import_collada.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef EDITOR_IMPORT_COLLADA_H -#define EDITOR_IMPORT_COLLADA_H - -#include "editor/import/resource_importer_scene.h" - -class EditorSceneImporterCollada : public EditorSceneImporter { - GDCLASS(EditorSceneImporterCollada, EditorSceneImporter); - -public: - virtual uint32_t get_import_flags() const; - virtual void get_extensions(List *r_extensions) const; - virtual Node *import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, uint32_t p_compress_flags, List *r_missing_deps = nullptr, Error *r_err = nullptr); - virtual Ref import_animation(const String &p_path, uint32_t p_flags, int p_bake_fps); - - EditorSceneImporterCollada(); -}; - -#endif diff --git a/modules/mesh_data_resource/SCsub b/modules/mesh_data_resource/SCsub index 06945f982..457d5bfc0 100644 --- a/modules/mesh_data_resource/SCsub +++ b/modules/mesh_data_resource/SCsub @@ -24,9 +24,6 @@ module_env.add_source_files(env.modules_sources,"mesh_data_resource_collection.c module_env.add_source_files(env.modules_sources,"plugin/mdr_import_plugin_base.cpp") if 'TOOLS_ENABLED' in env["CPPDEFINES"]: - module_env.add_source_files(env.modules_sources,"plugin_collada/editor_import_collada_mdr.cpp") - module_env.add_source_files(env.modules_sources,"plugin_collada/editor_plugin_collada_mdr.cpp") - module_env.add_source_files(env.modules_sources,"plugin_gltf/editor_import_gltf_mdr.cpp") module_env.add_source_files(env.modules_sources,"plugin_gltf/editor_plugin_gltf_mdr.cpp") diff --git a/modules/mesh_data_resource/plugin_collada/editor_import_collada_mdr.cpp b/modules/mesh_data_resource/plugin_collada/editor_import_collada_mdr.cpp deleted file mode 100644 index b495e915a..000000000 --- a/modules/mesh_data_resource/plugin_collada/editor_import_collada_mdr.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/* -Copyright (c) 2019-2022 Péter Magyar - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -#include "editor_import_collada_mdr.h" - -String EditorImportColladaMdr::get_importer_name() const { - return "collada_mdr"; -} - -String EditorImportColladaMdr::get_visible_name() const { - return "Collada MDR"; -} - -void EditorImportColladaMdr::get_recognized_extensions(List *p_extensions) const { - p_extensions->push_back("dae"); -} - -String EditorImportColladaMdr::get_save_extension() const { - return "res"; -} - -String EditorImportColladaMdr::get_resource_type() const { - return "MeshDataResource"; -} - -float EditorImportColladaMdr::get_priority() const { - return 1.0; -} - -int EditorImportColladaMdr::get_preset_count() const { - return 0; -} - -String EditorImportColladaMdr::get_preset_name(int p_idx) const { - return ""; -} - -Error EditorImportColladaMdr::import(const String &p_source_file, const String &p_save_path, const Map &p_options, List *r_platform_variants, List *r_gen_files, Variant *r_metadata) { - //MeshDataResource::ColliderType collider_type = static_cast(static_cast(p_options["collider_type"])); - - Error erri; - - #if VERSION_MAJOR == 3 && VERSION_MINOR > 4 - Node *n = _importer->import_scene(p_source_file, 0, 15, 0, nullptr, &erri); - #else - Node *n = _importer->import_scene(p_source_file, 0, 15, nullptr, &erri); - #endif - - ERR_FAIL_COND_V(!n, Error::ERR_PARSE_ERROR); - - if (erri != Error::OK) { - return erri; - } - - Error err = process_node(n, p_source_file, p_save_path, p_options, r_platform_variants, r_gen_files, r_metadata); - - n->queue_delete(); - return err; -} - -EditorImportColladaMdr::EditorImportColladaMdr() { - _importer.instance(); -} - -EditorImportColladaMdr::~EditorImportColladaMdr() { - _importer.unref(); -} diff --git a/modules/mesh_data_resource/plugin_collada/editor_import_collada_mdr.h b/modules/mesh_data_resource/plugin_collada/editor_import_collada_mdr.h deleted file mode 100644 index 396926e3f..000000000 --- a/modules/mesh_data_resource/plugin_collada/editor_import_collada_mdr.h +++ /dev/null @@ -1,81 +0,0 @@ -/* -Copyright (c) 2019-2022 Péter Magyar - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -#ifndef EDITOR_IMPORT_COLLADA_MDR -#define EDITOR_IMPORT_COLLADA_MDR - -#include "core/version.h" - -#if VERSION_MAJOR > 3 -#include "core/string/ustring.h" -#include "core/variant/array.h" -#else -#include "core/ustring.h" -#include "core/array.h" -#endif - -#include "../plugin/mdr_import_plugin_base.h" - -#include "core/io/resource_saver.h" -#include "core/math/basis.h" -#include "core/math/transform.h" -#include "editor/import/editor_import_plugin.h" -#include "scene/main/node.h" -#include "scene/resources/mesh.h" - -#include "../mesh_data_resource.h" -#include "editor/import/editor_import_collada.h" - -#include "core/version.h" - -#if VERSION_MAJOR < 4 -#include "scene/3d/mesh_instance.h" -#else -#include "scene/3d/mesh_instance_3d.h" - -#define MeshInstance MeshInstance3D -#endif - -class EditorImportColladaMdr : public MDRImportPluginBase { - GDCLASS(EditorImportColladaMdr, MDRImportPluginBase); - -public: - virtual String get_importer_name() const; - virtual String get_visible_name() const; - virtual void get_recognized_extensions(List *p_extensions) const; - virtual String get_save_extension() const; - virtual String get_resource_type() const; - virtual float get_priority() const; - - virtual int get_preset_count() const; - virtual String get_preset_name(int p_idx) const; - - virtual Error import(const String &p_source_file, const String &p_save_path, const Map &p_options, List *r_platform_variants, List *r_gen_files = NULL, Variant *r_metadata = NULL); - - EditorImportColladaMdr(); - ~EditorImportColladaMdr(); - -private: - Ref _importer; -}; - -#endif diff --git a/modules/mesh_data_resource/plugin_collada/editor_plugin_collada_mdr.cpp b/modules/mesh_data_resource/plugin_collada/editor_plugin_collada_mdr.cpp deleted file mode 100644 index 6861d1103..000000000 --- a/modules/mesh_data_resource/plugin_collada/editor_plugin_collada_mdr.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/* -Copyright (c) 2019-2022 Péter Magyar - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -#include "editor_plugin_collada_mdr.h" - -void EditorPluginColladaMdr::_notification(int p_what) { - switch (p_what) { - case NOTIFICATION_ENTER_TREE: - _importer.instance(); - - add_import_plugin(_importer); - - break; - case NOTIFICATION_EXIT_TREE: - remove_import_plugin(_importer); - - _importer.unref(); - - break; - } -} - -EditorPluginColladaMdr::EditorPluginColladaMdr(EditorNode *node) { - _node = node; -} diff --git a/modules/mesh_data_resource/plugin_collada/editor_plugin_collada_mdr.h b/modules/mesh_data_resource/plugin_collada/editor_plugin_collada_mdr.h deleted file mode 100644 index f1a76ae00..000000000 --- a/modules/mesh_data_resource/plugin_collada/editor_plugin_collada_mdr.h +++ /dev/null @@ -1,53 +0,0 @@ -/* -Copyright (c) 2019-2022 Péter Magyar - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -#ifndef EDITOR_PLUGIN_COLLADA_MDR -#define EDITOR_PLUGIN_COLLADA_MDR - -#include "core/version.h" - -#if VERSION_MAJOR > 3 -#include "core/string/ustring.h" -#else -#include "core/ustring.h" -#endif - -#include "editor/editor_plugin.h" - -#include "editor_import_collada_mdr.h" - -class EditorPluginColladaMdr : public EditorPlugin { - - GDCLASS(EditorPluginColladaMdr, EditorPlugin); - -public: - EditorPluginColladaMdr(EditorNode *node); - -protected: - void _notification(int p_what); - -private: - EditorNode *_node; - Ref _importer; -}; - -#endif diff --git a/modules/mesh_data_resource/register_types.cpp b/modules/mesh_data_resource/register_types.cpp index 7816fecb2..38c0a144a 100644 --- a/modules/mesh_data_resource/register_types.cpp +++ b/modules/mesh_data_resource/register_types.cpp @@ -30,8 +30,6 @@ SOFTWARE. #ifdef TOOLS_ENABLED #include "editor/editor_plugin.h" -#include "plugin_collada/editor_plugin_collada_mdr.h" - #include "plugin_gltf/editor_plugin_gltf_mdr.h" #endif @@ -65,8 +63,6 @@ void register_mesh_data_resource_types() { #endif #ifdef TOOLS_ENABLED - EditorPlugins::add_by_type(); - EditorPlugins::add_by_type(); #endif }