collada.h 14 KB

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