collada.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /**************************************************************************/
  2. /* collada.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef COLLADA_H
  31. #define COLLADA_H
  32. #include "core/config/project_settings.h"
  33. #include "core/io/xml_parser.h"
  34. #include "core/templates/rb_map.h"
  35. #include "scene/resources/material.h"
  36. class Collada {
  37. public:
  38. enum ImportFlags {
  39. IMPORT_FLAG_SCENE = 1,
  40. IMPORT_FLAG_ANIMATION = 2
  41. };
  42. struct Image {
  43. String path;
  44. };
  45. struct Material {
  46. String name;
  47. String instance_effect;
  48. };
  49. struct Effect {
  50. String name;
  51. HashMap<String, Variant> params;
  52. struct Channel {
  53. int uv_idx = 0;
  54. String texture;
  55. Color color;
  56. Channel() {}
  57. };
  58. Channel diffuse, specular, emission, bump;
  59. float shininess = 40;
  60. bool found_double_sided = false;
  61. bool double_sided = true;
  62. bool unshaded = false;
  63. String get_texture_path(const String &p_source, Collada &p_state) const;
  64. Effect() {
  65. diffuse.color = Color(1, 1, 1, 1);
  66. }
  67. };
  68. struct CameraData {
  69. enum Mode {
  70. MODE_PERSPECTIVE,
  71. MODE_ORTHOGONAL
  72. };
  73. Mode mode = MODE_PERSPECTIVE;
  74. union {
  75. struct {
  76. float x_fov = 0;
  77. float y_fov = 0;
  78. } perspective;
  79. struct {
  80. float x_mag = 0;
  81. float y_mag = 0;
  82. } orthogonal;
  83. };
  84. float aspect = 1;
  85. float z_near = 0.05;
  86. float z_far = 4000;
  87. CameraData() {}
  88. };
  89. struct LightData {
  90. enum Mode {
  91. MODE_AMBIENT,
  92. MODE_DIRECTIONAL,
  93. MODE_OMNI,
  94. MODE_SPOT
  95. };
  96. Mode mode = MODE_AMBIENT;
  97. Color color = Color(1, 1, 1, 1);
  98. float constant_att = 0;
  99. float linear_att = 0;
  100. float quad_att = 0;
  101. float spot_angle = 45;
  102. float spot_exp = 1;
  103. LightData() {}
  104. };
  105. struct MeshData {
  106. String name;
  107. struct Source {
  108. Vector<float> array;
  109. int stride = 0;
  110. };
  111. HashMap<String, Source> sources;
  112. struct Vertices {
  113. HashMap<String, String> sources;
  114. };
  115. HashMap<String, Vertices> vertices;
  116. struct Primitives {
  117. struct SourceRef {
  118. String source;
  119. int offset = 0;
  120. };
  121. String material;
  122. HashMap<String, SourceRef> sources;
  123. Vector<float> polygons;
  124. Vector<float> indices;
  125. int count = 0;
  126. int vertex_size = 0;
  127. };
  128. Vector<Primitives> primitives;
  129. bool found_double_sided = false;
  130. bool double_sided = true;
  131. MeshData() {}
  132. };
  133. struct CurveData {
  134. String name;
  135. bool closed = false;
  136. struct Source {
  137. Vector<String> sarray;
  138. Vector<float> array;
  139. int stride = 0;
  140. };
  141. HashMap<String, Source> sources;
  142. HashMap<String, String> control_vertices;
  143. CurveData() {}
  144. };
  145. struct SkinControllerData {
  146. String base;
  147. bool use_idrefs = false;
  148. Transform3D bind_shape;
  149. struct Source {
  150. Vector<String> sarray; //maybe for names
  151. Vector<float> array;
  152. int stride = 1;
  153. Source() {}
  154. };
  155. HashMap<String, Source> sources;
  156. struct Joints {
  157. HashMap<String, String> sources;
  158. } joints;
  159. struct Weights {
  160. struct SourceRef {
  161. String source;
  162. int offset = 0;
  163. };
  164. String material;
  165. HashMap<String, SourceRef> sources;
  166. Vector<float> sets;
  167. Vector<float> indices;
  168. int count = 0;
  169. } weights;
  170. HashMap<String, Transform3D> bone_rest_map;
  171. SkinControllerData() {}
  172. };
  173. struct MorphControllerData {
  174. String mesh;
  175. String mode;
  176. struct Source {
  177. int stride = 1;
  178. Vector<String> sarray; //maybe for names
  179. Vector<float> array;
  180. Source() {}
  181. };
  182. HashMap<String, Source> sources;
  183. HashMap<String, String> targets;
  184. MorphControllerData() {}
  185. };
  186. struct Vertex {
  187. int idx = 0;
  188. Vector3 vertex;
  189. Vector3 normal;
  190. Vector3 uv;
  191. Vector3 uv2;
  192. Plane tangent;
  193. Color color;
  194. int uid = 0;
  195. struct Weight {
  196. int bone_idx = 0;
  197. float weight = 0;
  198. bool operator<(const Weight w) const { return weight > w.weight; } //heaviest first
  199. };
  200. Vector<Weight> weights;
  201. void fix_weights() {
  202. weights.sort();
  203. if (weights.size() > 4) {
  204. //cap to 4 and make weights add up 1
  205. weights.resize(4);
  206. float total = 0;
  207. for (int i = 0; i < 4; i++) {
  208. total += weights[i].weight;
  209. }
  210. if (total) {
  211. for (int i = 0; i < 4; i++) {
  212. weights.write[i].weight /= total;
  213. }
  214. }
  215. }
  216. }
  217. void fix_unit_scale(const Collada &p_state);
  218. bool operator<(const Vertex &p_vert) const {
  219. if (uid == p_vert.uid) {
  220. if (vertex == p_vert.vertex) {
  221. if (normal == p_vert.normal) {
  222. if (uv == p_vert.uv) {
  223. if (uv2 == p_vert.uv2) {
  224. if (!weights.is_empty() || !p_vert.weights.is_empty()) {
  225. if (weights.size() == p_vert.weights.size()) {
  226. for (int i = 0; i < weights.size(); i++) {
  227. if (weights[i].bone_idx != p_vert.weights[i].bone_idx) {
  228. return weights[i].bone_idx < p_vert.weights[i].bone_idx;
  229. }
  230. if (weights[i].weight != p_vert.weights[i].weight) {
  231. return weights[i].weight < p_vert.weights[i].weight;
  232. }
  233. }
  234. } else {
  235. return weights.size() < p_vert.weights.size();
  236. }
  237. }
  238. return (color < p_vert.color);
  239. } else {
  240. return (uv2 < p_vert.uv2);
  241. }
  242. } else {
  243. return (uv < p_vert.uv);
  244. }
  245. } else {
  246. return (normal < p_vert.normal);
  247. }
  248. } else {
  249. return vertex < p_vert.vertex;
  250. }
  251. } else {
  252. return uid < p_vert.uid;
  253. }
  254. }
  255. Vertex() {}
  256. };
  257. struct Node {
  258. enum Type {
  259. TYPE_NODE,
  260. TYPE_JOINT,
  261. TYPE_SKELETON, //this bone is not collada, it's added afterwards as optimization
  262. TYPE_LIGHT,
  263. TYPE_CAMERA,
  264. TYPE_GEOMETRY
  265. };
  266. struct XForm {
  267. enum Op {
  268. OP_ROTATE,
  269. OP_SCALE,
  270. OP_TRANSLATE,
  271. OP_MATRIX,
  272. OP_VISIBILITY
  273. };
  274. String id;
  275. Op op = OP_ROTATE;
  276. Vector<float> data;
  277. };
  278. Type type = TYPE_NODE;
  279. String name;
  280. String id;
  281. String empty_draw_type;
  282. bool noname = false;
  283. Vector<XForm> xform_list;
  284. Transform3D default_transform;
  285. Transform3D post_transform;
  286. Vector<Node *> children;
  287. Node *parent = nullptr;
  288. Transform3D compute_transform(const Collada &p_state) const;
  289. Transform3D get_global_transform() const;
  290. Transform3D get_transform() const;
  291. bool ignore_anim = false;
  292. Node() {}
  293. virtual ~Node() {
  294. for (int i = 0; i < children.size(); i++) {
  295. memdelete(children[i]);
  296. }
  297. };
  298. };
  299. struct NodeSkeleton : public Node {
  300. NodeSkeleton() { type = TYPE_SKELETON; }
  301. };
  302. struct NodeJoint : public Node {
  303. NodeSkeleton *owner = nullptr;
  304. String sid;
  305. NodeJoint() {
  306. type = TYPE_JOINT;
  307. }
  308. };
  309. struct NodeGeometry : public Node {
  310. bool controller = false;
  311. String source;
  312. struct Material {
  313. String target;
  314. };
  315. HashMap<String, Material> material_map;
  316. Vector<String> skeletons;
  317. NodeGeometry() { type = TYPE_GEOMETRY; }
  318. };
  319. struct NodeCamera : public Node {
  320. String camera;
  321. NodeCamera() { type = TYPE_CAMERA; }
  322. };
  323. struct NodeLight : public Node {
  324. String light;
  325. NodeLight() { type = TYPE_LIGHT; }
  326. };
  327. struct VisualScene {
  328. String name;
  329. Vector<Node *> root_nodes;
  330. ~VisualScene() {
  331. for (int i = 0; i < root_nodes.size(); i++) {
  332. memdelete(root_nodes[i]);
  333. }
  334. }
  335. };
  336. struct AnimationClip {
  337. String name;
  338. float begin = 0;
  339. float end = 1;
  340. Vector<String> tracks;
  341. AnimationClip() {}
  342. };
  343. struct AnimationTrack {
  344. String id;
  345. String target;
  346. String param;
  347. String component;
  348. bool property = false;
  349. enum InterpolationType {
  350. INTERP_LINEAR,
  351. INTERP_BEZIER
  352. };
  353. struct Key {
  354. enum Type {
  355. TYPE_FLOAT,
  356. TYPE_MATRIX
  357. };
  358. float time = 0;
  359. Vector<float> data;
  360. Point2 in_tangent;
  361. Point2 out_tangent;
  362. InterpolationType interp_type = INTERP_LINEAR;
  363. Key() {}
  364. };
  365. Vector<float> get_value_at_time(float p_time) const;
  366. Vector<Key> keys;
  367. AnimationTrack() {}
  368. };
  369. /****************/
  370. /* IMPORT STATE */
  371. /****************/
  372. struct State {
  373. int import_flags = 0;
  374. float unit_scale = 1.0;
  375. Vector3::Axis up_axis = Vector3::AXIS_Y;
  376. bool z_up = false;
  377. struct Version {
  378. int major = 0, minor = 0, rev = 0;
  379. 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; }
  380. Version(int p_major = 0, int p_minor = 0, int p_rev = 0) {
  381. major = p_major;
  382. minor = p_minor;
  383. rev = p_rev;
  384. }
  385. } version;
  386. HashMap<String, CameraData> camera_data_map;
  387. HashMap<String, MeshData> mesh_data_map;
  388. HashMap<String, LightData> light_data_map;
  389. HashMap<String, CurveData> curve_data_map;
  390. HashMap<String, String> mesh_name_map;
  391. HashMap<String, String> morph_name_map;
  392. HashMap<String, String> morph_ownership_map;
  393. HashMap<String, SkinControllerData> skin_controller_data_map;
  394. HashMap<String, MorphControllerData> morph_controller_data_map;
  395. HashMap<String, Image> image_map;
  396. HashMap<String, Material> material_map;
  397. HashMap<String, Effect> effect_map;
  398. HashMap<String, VisualScene> visual_scene_map;
  399. HashMap<String, Node *> scene_map;
  400. HashSet<String> idref_joints;
  401. HashMap<String, String> sid_to_node_map;
  402. //RBMap<String,NodeJoint*> bone_map;
  403. HashMap<String, Transform3D> bone_rest_map;
  404. String local_path;
  405. String root_visual_scene;
  406. String root_physics_scene;
  407. Vector<AnimationClip> animation_clips;
  408. Vector<AnimationTrack> animation_tracks;
  409. HashMap<String, Vector<int>> referenced_tracks;
  410. HashMap<String, Vector<int>> by_id_tracks;
  411. float animation_length = 0;
  412. State() {}
  413. } state;
  414. Error load(const String &p_path, int p_flags = 0);
  415. Collada();
  416. Transform3D fix_transform(const Transform3D &p_transform);
  417. Transform3D get_root_transform() const;
  418. int get_uv_channel(const String &p_name);
  419. private: // private stuff
  420. HashMap<String, int> channel_map;
  421. void _parse_asset(XMLParser &p_parser);
  422. void _parse_image(XMLParser &p_parser);
  423. void _parse_material(XMLParser &p_parser);
  424. void _parse_effect_material(XMLParser &p_parser, Effect &p_effect, String &p_id);
  425. void _parse_effect(XMLParser &p_parser);
  426. void _parse_camera(XMLParser &p_parser);
  427. void _parse_light(XMLParser &p_parser);
  428. void _parse_animation_clip(XMLParser &p_parser);
  429. void _parse_mesh_geometry(XMLParser &p_parser, const String &p_id, const String &p_name);
  430. void _parse_curve_geometry(XMLParser &p_parser, const String &p_id, const String &p_name);
  431. void _parse_skin_controller(XMLParser &p_parser, const String &p_id);
  432. void _parse_morph_controller(XMLParser &p_parser, const String &p_id);
  433. void _parse_controller(XMLParser &p_parser);
  434. Node *_parse_visual_instance_geometry(XMLParser &p_parser);
  435. Node *_parse_visual_instance_camera(XMLParser &p_parser);
  436. Node *_parse_visual_instance_light(XMLParser &p_parser);
  437. Node *_parse_visual_node_instance_data(XMLParser &p_parser);
  438. Node *_parse_visual_scene_node(XMLParser &p_parser);
  439. void _parse_visual_scene(XMLParser &p_parser);
  440. void _parse_animation(XMLParser &p_parser);
  441. void _parse_scene(XMLParser &p_parser);
  442. void _parse_library(XMLParser &p_parser);
  443. Variant _parse_param(XMLParser &p_parser);
  444. Vector<float> _read_float_array(XMLParser &p_parser);
  445. Vector<String> _read_string_array(XMLParser &p_parser);
  446. Transform3D _read_transform(XMLParser &p_parser);
  447. String _read_empty_draw_type(XMLParser &p_parser);
  448. void _joint_set_owner(Collada::Node *p_node, NodeSkeleton *p_owner);
  449. void _create_skeletons(Collada::Node **p_node, NodeSkeleton *p_skeleton = nullptr);
  450. void _find_morph_nodes(VisualScene *p_vscene, Node *p_node);
  451. bool _remove_node(Node *p_parent, Node *p_node);
  452. void _remove_node(VisualScene *p_vscene, Node *p_node);
  453. void _merge_skeletons2(VisualScene *p_vscene);
  454. void _merge_skeletons(VisualScene *p_vscene, Node *p_node);
  455. bool _optimize_skeletons(VisualScene *p_vscene, Node *p_node);
  456. bool _move_geometry_to_skeletons(VisualScene *p_vscene, Node *p_node, List<Node *> *p_mgeom);
  457. void _optimize();
  458. };
  459. #endif // COLLADA_H