collada.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*************************************************************************/
  2. /* collada.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #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. if (total)
  236. for (int i = 0; i < 4; i++)
  237. weights.write[i].weight /= total;
  238. }
  239. }
  240. void fix_unit_scale(Collada &state);
  241. bool operator<(const Vertex &p_vert) const {
  242. if (uid == p_vert.uid) {
  243. if (vertex == p_vert.vertex) {
  244. if (normal == p_vert.normal) {
  245. if (uv == p_vert.uv) {
  246. if (uv2 == p_vert.uv2) {
  247. if (!weights.empty() || !p_vert.weights.empty()) {
  248. if (weights.size() == p_vert.weights.size()) {
  249. for (int i = 0; i < weights.size(); i++) {
  250. if (weights[i].bone_idx != p_vert.weights[i].bone_idx)
  251. return weights[i].bone_idx < p_vert.weights[i].bone_idx;
  252. if (weights[i].weight != p_vert.weights[i].weight)
  253. return weights[i].weight < p_vert.weights[i].weight;
  254. }
  255. } else {
  256. return weights.size() < p_vert.weights.size();
  257. }
  258. }
  259. return (color < p_vert.color);
  260. } else
  261. return (uv2 < p_vert.uv2);
  262. } else
  263. return (uv < p_vert.uv);
  264. } else
  265. return (normal < p_vert.normal);
  266. } else
  267. return vertex < p_vert.vertex;
  268. } else
  269. return uid < p_vert.uid;
  270. }
  271. Vertex() {
  272. uid = 0;
  273. idx = 0;
  274. }
  275. };
  276. struct Node {
  277. enum Type {
  278. TYPE_NODE,
  279. TYPE_JOINT,
  280. TYPE_SKELETON, //this bone is not collada, it's added afterwards as optimization
  281. TYPE_LIGHT,
  282. TYPE_CAMERA,
  283. TYPE_GEOMETRY
  284. };
  285. struct XForm {
  286. enum Op {
  287. OP_ROTATE,
  288. OP_SCALE,
  289. OP_TRANSLATE,
  290. OP_MATRIX,
  291. OP_VISIBILITY
  292. };
  293. String id;
  294. Op op;
  295. Vector<float> data;
  296. };
  297. Type type;
  298. String name;
  299. String id;
  300. String empty_draw_type;
  301. bool noname;
  302. Vector<XForm> xform_list;
  303. Transform default_transform;
  304. Transform post_transform;
  305. Vector<Node *> children;
  306. Node *parent;
  307. Transform compute_transform(Collada &state) const;
  308. Transform get_global_transform() const;
  309. Transform get_transform() const;
  310. bool ignore_anim;
  311. Node() {
  312. noname = false;
  313. type = TYPE_NODE;
  314. parent = NULL;
  315. ignore_anim = false;
  316. }
  317. virtual ~Node() {
  318. for (int i = 0; i < children.size(); i++)
  319. memdelete(children[i]);
  320. };
  321. };
  322. struct NodeSkeleton : public Node {
  323. NodeSkeleton() { type = TYPE_SKELETON; }
  324. };
  325. struct NodeJoint : public Node {
  326. NodeSkeleton *owner;
  327. String sid;
  328. NodeJoint() {
  329. type = TYPE_JOINT;
  330. owner = NULL;
  331. }
  332. };
  333. struct NodeGeometry : public Node {
  334. bool controller;
  335. String source;
  336. struct Material {
  337. String target;
  338. };
  339. Map<String, Material> material_map;
  340. Vector<String> skeletons;
  341. NodeGeometry() { type = TYPE_GEOMETRY; }
  342. };
  343. struct NodeCamera : public Node {
  344. String camera;
  345. NodeCamera() { type = TYPE_CAMERA; }
  346. };
  347. struct NodeLight : public Node {
  348. String light;
  349. NodeLight() { type = TYPE_LIGHT; }
  350. };
  351. struct VisualScene {
  352. String name;
  353. Vector<Node *> root_nodes;
  354. ~VisualScene() {
  355. for (int i = 0; i < root_nodes.size(); i++)
  356. memdelete(root_nodes[i]);
  357. }
  358. };
  359. struct AnimationClip {
  360. String name;
  361. float begin;
  362. float end;
  363. Vector<String> tracks;
  364. AnimationClip() {
  365. begin = 0;
  366. end = 1;
  367. }
  368. };
  369. struct AnimationTrack {
  370. String id;
  371. String target;
  372. String param;
  373. String component;
  374. bool property;
  375. enum InterpolationType {
  376. INTERP_LINEAR,
  377. INTERP_BEZIER
  378. };
  379. struct Key {
  380. enum Type {
  381. TYPE_FLOAT,
  382. TYPE_MATRIX
  383. };
  384. float time;
  385. Vector<float> data;
  386. Point2 in_tangent;
  387. Point2 out_tangent;
  388. InterpolationType interp_type;
  389. Key() { interp_type = INTERP_LINEAR; }
  390. };
  391. Vector<float> get_value_at_time(float p_time) const;
  392. Vector<Key> keys;
  393. AnimationTrack() { property = false; }
  394. };
  395. /****************/
  396. /* IMPORT STATE */
  397. /****************/
  398. struct State {
  399. int import_flags;
  400. float unit_scale;
  401. Vector3::Axis up_axis;
  402. bool z_up;
  403. struct Version {
  404. int major, minor, rev;
  405. 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; }
  406. Version(int p_major = 0, int p_minor = 0, int p_rev = 0) {
  407. major = p_major;
  408. minor = p_minor;
  409. rev = p_rev;
  410. }
  411. } version;
  412. Map<String, CameraData> camera_data_map;
  413. Map<String, MeshData> mesh_data_map;
  414. Map<String, LightData> light_data_map;
  415. Map<String, CurveData> curve_data_map;
  416. Map<String, String> mesh_name_map;
  417. Map<String, String> morph_name_map;
  418. Map<String, String> morph_ownership_map;
  419. Map<String, SkinControllerData> skin_controller_data_map;
  420. Map<String, MorphControllerData> morph_controller_data_map;
  421. Map<String, Image> image_map;
  422. Map<String, Material> material_map;
  423. Map<String, Effect> effect_map;
  424. Map<String, VisualScene> visual_scene_map;
  425. Map<String, Node *> scene_map;
  426. Set<String> idref_joints;
  427. Map<String, String> sid_to_node_map;
  428. //Map<String,NodeJoint*> bone_map;
  429. Map<String, Transform> bone_rest_map;
  430. String local_path;
  431. String root_visual_scene;
  432. String root_physics_scene;
  433. Vector<AnimationClip> animation_clips;
  434. Vector<AnimationTrack> animation_tracks;
  435. Map<String, Vector<int> > referenced_tracks;
  436. Map<String, Vector<int> > by_id_tracks;
  437. float animation_length;
  438. State() :
  439. import_flags(0),
  440. unit_scale(1.0),
  441. up_axis(Vector3::AXIS_Y),
  442. animation_length(0) {
  443. }
  444. } state;
  445. Error load(const String &p_path, int p_flags = 0);
  446. Collada();
  447. Transform fix_transform(const Transform &p_transform);
  448. Transform get_root_transform() const;
  449. int get_uv_channel(String p_name);
  450. private: // private stuff
  451. Map<String, int> channel_map;
  452. void _parse_asset(XMLParser &parser);
  453. void _parse_image(XMLParser &parser);
  454. void _parse_material(XMLParser &parser);
  455. void _parse_effect_material(XMLParser &parser, Effect &effect, String &id);
  456. void _parse_effect(XMLParser &parser);
  457. void _parse_camera(XMLParser &parser);
  458. void _parse_light(XMLParser &parser);
  459. void _parse_animation_clip(XMLParser &parser);
  460. void _parse_mesh_geometry(XMLParser &parser, String p_id, String p_name);
  461. void _parse_curve_geometry(XMLParser &parser, String p_id, String p_name);
  462. void _parse_skin_controller(XMLParser &parser, String p_id);
  463. void _parse_morph_controller(XMLParser &parser, String p_id);
  464. void _parse_controller(XMLParser &parser);
  465. Node *_parse_visual_instance_geometry(XMLParser &parser);
  466. Node *_parse_visual_instance_camera(XMLParser &parser);
  467. Node *_parse_visual_instance_light(XMLParser &parser);
  468. Node *_parse_visual_node_instance_data(XMLParser &parser);
  469. Node *_parse_visual_scene_node(XMLParser &parser);
  470. void _parse_visual_scene(XMLParser &parser);
  471. void _parse_animation(XMLParser &parser);
  472. void _parse_scene(XMLParser &parser);
  473. void _parse_library(XMLParser &parser);
  474. Variant _parse_param(XMLParser &parser);
  475. Vector<float> _read_float_array(XMLParser &parser);
  476. Vector<String> _read_string_array(XMLParser &parser);
  477. Transform _read_transform(XMLParser &parser);
  478. String _read_empty_draw_type(XMLParser &parser);
  479. void _joint_set_owner(Collada::Node *p_node, NodeSkeleton *p_owner);
  480. void _create_skeletons(Collada::Node **p_node, NodeSkeleton *p_skeleton = NULL);
  481. void _find_morph_nodes(VisualScene *p_vscene, Node *p_node);
  482. bool _remove_node(Node *p_parent, Node *p_node);
  483. void _remove_node(VisualScene *p_vscene, Node *p_node);
  484. void _merge_skeletons2(VisualScene *p_vscene);
  485. void _merge_skeletons(VisualScene *p_vscene, Node *p_node);
  486. bool _optimize_skeletons(VisualScene *p_vscene, Node *p_node);
  487. bool _move_geometry_to_skeletons(VisualScene *p_vscene, Node *p_node, List<Node *> *p_mgeom);
  488. void _optimize();
  489. };
  490. #endif // COLLADA_H