collada.h 14 KB

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