mesh.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /*************************************************************************/
  2. /* mesh.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "mesh.h"
  30. #include "scene/resources/concave_polygon_shape.h"
  31. #include "scene/resources/convex_polygon_shape.h"
  32. static const char*_array_name[]={
  33. "vertex_array",
  34. "normal_array",
  35. "tangent_array",
  36. "color_array",
  37. "tex_uv_array",
  38. "tex_uv2_array",
  39. "bone_array",
  40. "weights_array",
  41. "index_array",
  42. NULL
  43. };
  44. static const Mesh::ArrayType _array_types[]={
  45. Mesh::ARRAY_VERTEX,
  46. Mesh::ARRAY_NORMAL,
  47. Mesh::ARRAY_TANGENT,
  48. Mesh::ARRAY_COLOR,
  49. Mesh::ARRAY_TEX_UV,
  50. Mesh::ARRAY_TEX_UV2,
  51. Mesh::ARRAY_BONES,
  52. Mesh::ARRAY_WEIGHTS,
  53. Mesh::ARRAY_INDEX
  54. };
  55. /* compatibility */
  56. static const int _format_translate[]={
  57. Mesh::ARRAY_FORMAT_VERTEX,
  58. Mesh::ARRAY_FORMAT_NORMAL,
  59. Mesh::ARRAY_FORMAT_TANGENT,
  60. Mesh::ARRAY_FORMAT_COLOR,
  61. Mesh::ARRAY_FORMAT_TEX_UV,
  62. Mesh::ARRAY_FORMAT_TEX_UV2,
  63. Mesh::ARRAY_FORMAT_BONES,
  64. Mesh::ARRAY_FORMAT_WEIGHTS,
  65. Mesh::ARRAY_FORMAT_INDEX,
  66. };
  67. bool Mesh::_set(const StringName& p_name, const Variant& p_value) {
  68. String sname=p_name;
  69. if (p_name=="morph_target/names") {
  70. DVector<String> sk=p_value;
  71. int sz = sk.size();
  72. DVector<String>::Read r = sk.read();
  73. for(int i=0;i<sz;i++)
  74. add_morph_target(r[i]);
  75. return true;
  76. }
  77. if (p_name=="morph_target/mode") {
  78. set_morph_target_mode(MorphTargetMode(int(p_value)));
  79. return true;
  80. }
  81. if (sname.begins_with("surface_")) {
  82. int sl=sname.find("/");
  83. if (sl==-1)
  84. return false;
  85. int idx=sname.substr(8,sl-8).to_int()-1;
  86. String what = sname.get_slice("/",1);
  87. if (what=="material")
  88. surface_set_material(idx,p_value);
  89. else if (what=="name")
  90. surface_set_name(idx,p_value);
  91. return true;
  92. }
  93. if (sname=="custom_aabb/custom_aabb") {
  94. set_custom_aabb(p_value);
  95. return true;
  96. }
  97. if (!sname.begins_with("surfaces"))
  98. return false;
  99. int idx=sname.get_slice("/",1).to_int();
  100. String what=sname.get_slice("/",2);
  101. if (idx==surfaces.size()) {
  102. if (what=="custom") {
  103. add_custom_surface(p_value);
  104. return true;
  105. }
  106. //create
  107. Dictionary d=p_value;
  108. ERR_FAIL_COND_V(!d.has("primitive"),false);
  109. ERR_FAIL_COND_V(!d.has("arrays"),false);
  110. ERR_FAIL_COND_V(!d.has("morph_arrays"),false);
  111. bool alphasort = d.has("alphasort") && bool(d["alphasort"]);
  112. add_surface(PrimitiveType(int(d["primitive"])),d["arrays"],d["morph_arrays"],alphasort);
  113. if (d.has("material")) {
  114. surface_set_material(idx,d["material"]);
  115. }
  116. if (d.has("name")) {
  117. surface_set_name(idx,d["name"]);
  118. }
  119. return true;
  120. }
  121. if (what=="custom_aabb") {
  122. surface_set_custom_aabb(idx,p_value);
  123. return true;
  124. }
  125. return false;
  126. }
  127. bool Mesh::_get(const StringName& p_name,Variant &r_ret) const {
  128. String sname=p_name;
  129. if (p_name=="morph_target/names") {
  130. DVector<String> sk;
  131. for(int i=0;i<morph_targets.size();i++)
  132. sk.push_back(morph_targets[i]);
  133. r_ret=sk;
  134. return true;
  135. } else if (p_name=="morph_target/mode") {
  136. r_ret = get_morph_target_mode();
  137. return true;
  138. } else if (sname.begins_with("surface_")) {
  139. int sl=sname.find("/");
  140. if (sl==-1)
  141. return false;
  142. int idx=sname.substr(8,sl-8).to_int()-1;
  143. String what = sname.get_slice("/",1);
  144. if (what=="material")
  145. r_ret=surface_get_material(idx);
  146. else if (what=="name")
  147. r_ret=surface_get_name(idx);
  148. return true;
  149. } else if (sname=="custom_aabb/custom_aabb") {
  150. r_ret=custom_aabb;
  151. return true;
  152. } else if (!sname.begins_with("surfaces"))
  153. return false;
  154. int idx=sname.get_slice("/",1).to_int();
  155. ERR_FAIL_INDEX_V(idx,surfaces.size(),false);
  156. Dictionary d;
  157. d["primitive"]=surface_get_primitive_type(idx);
  158. d["arrays"]=surface_get_arrays(idx);
  159. d["morph_arrays"]=surface_get_morph_arrays(idx);
  160. d["alphasort"]=surface_is_alpha_sorting_enabled(idx);
  161. Ref<Material> m = surface_get_material(idx);
  162. if (m.is_valid())
  163. d["material"]=m;
  164. String n = surface_get_name(idx);
  165. if (n!="")
  166. d["name"]=n;
  167. r_ret=d;
  168. return true;
  169. }
  170. void Mesh::_get_property_list( List<PropertyInfo> *p_list) const {
  171. if (morph_targets.size()) {
  172. p_list->push_back(PropertyInfo(Variant::STRING_ARRAY,"morph_target/names",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR));
  173. p_list->push_back(PropertyInfo(Variant::INT,"morph_target/mode",PROPERTY_HINT_ENUM,"Normalized,Relative"));
  174. }
  175. for (int i=0;i<surfaces.size();i++) {
  176. p_list->push_back( PropertyInfo( Variant::DICTIONARY,"surfaces/"+itos(i), PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR ) );
  177. p_list->push_back( PropertyInfo( Variant::STRING,"surface_"+itos(i+1)+"/name", PROPERTY_HINT_NONE,"",PROPERTY_USAGE_EDITOR ) );
  178. p_list->push_back( PropertyInfo( Variant::OBJECT,"surface_"+itos(i+1)+"/material", PROPERTY_HINT_RESOURCE_TYPE,"Material",PROPERTY_USAGE_EDITOR ) );
  179. }
  180. p_list->push_back( PropertyInfo( Variant::_AABB,"custom_aabb/custom_aabb" ) );
  181. }
  182. void Mesh::_recompute_aabb() {
  183. // regenerate AABB
  184. aabb=AABB();
  185. for (int i=0;i<surfaces.size();i++) {
  186. if (i==0)
  187. aabb=surfaces[i].aabb;
  188. else
  189. aabb.merge_with(surfaces[i].aabb);
  190. }
  191. }
  192. void Mesh::add_surface(PrimitiveType p_primitive,const Array& p_arrays,const Array& p_blend_shapes,bool p_alphasort) {
  193. ERR_FAIL_COND(p_arrays.size()!=ARRAY_MAX);
  194. Surface s;
  195. VisualServer::get_singleton()->mesh_add_surface(mesh,(VisualServer::PrimitiveType)p_primitive, p_arrays,p_blend_shapes,p_alphasort);
  196. surfaces.push_back(s);
  197. /* make aABB? */ {
  198. DVector<Vector3> vertices=p_arrays[ARRAY_VERTEX];
  199. int len=vertices.size();
  200. ERR_FAIL_COND(len==0);
  201. DVector<Vector3>::Read r=vertices.read();
  202. const Vector3 *vtx=r.ptr();
  203. // check AABB
  204. AABB aabb;
  205. for (int i=0;i<len;i++) {
  206. if (i==0)
  207. aabb.pos=vtx[i];
  208. else
  209. aabb.expand_to(vtx[i]);
  210. }
  211. surfaces[surfaces.size()-1].aabb=aabb;
  212. surfaces[surfaces.size()-1].alphasort=p_alphasort;
  213. _recompute_aabb();
  214. }
  215. triangle_mesh=Ref<TriangleMesh>();
  216. _change_notify();
  217. }
  218. Array Mesh::surface_get_arrays(int p_surface) const {
  219. ERR_FAIL_INDEX_V(p_surface,surfaces.size(),Array());
  220. return VisualServer::get_singleton()->mesh_get_surface_arrays(mesh,p_surface);
  221. }
  222. Array Mesh::surface_get_morph_arrays(int p_surface) const {
  223. ERR_FAIL_INDEX_V(p_surface,surfaces.size(),Array());
  224. return VisualServer::get_singleton()->mesh_get_surface_morph_arrays(mesh,p_surface);
  225. }
  226. void Mesh::add_custom_surface(const Variant& p_data) {
  227. Surface s;
  228. s.aabb=AABB();
  229. VisualServer::get_singleton()->mesh_add_custom_surface(mesh,p_data);
  230. surfaces.push_back(s);
  231. triangle_mesh=Ref<TriangleMesh>();
  232. _change_notify();
  233. }
  234. int Mesh::get_surface_count() const {
  235. return surfaces.size();
  236. }
  237. void Mesh::add_morph_target(const StringName& p_name) {
  238. if (surfaces.size()) {
  239. ERR_EXPLAIN("Can't add a shape key count if surfaces are already created.");
  240. ERR_FAIL_COND(surfaces.size());
  241. }
  242. StringName name=p_name;
  243. if (morph_targets.find(name)!=-1 ) {
  244. int count=2;
  245. do {
  246. name = String(p_name) + " " + itos(count);
  247. count++;
  248. } while(morph_targets.find(name)!=-1);
  249. }
  250. morph_targets.push_back(name);
  251. VS::get_singleton()->mesh_set_morph_target_count(mesh,morph_targets.size());
  252. }
  253. int Mesh::get_morph_target_count() const {
  254. return morph_targets.size();
  255. }
  256. StringName Mesh::get_morph_target_name(int p_index) const {
  257. ERR_FAIL_INDEX_V( p_index, morph_targets.size(),StringName() );
  258. return morph_targets[p_index];
  259. }
  260. void Mesh::clear_morph_targets() {
  261. if (surfaces.size()) {
  262. ERR_EXPLAIN("Can't set shape key count if surfaces are already created.");
  263. ERR_FAIL_COND(surfaces.size());
  264. }
  265. morph_targets.clear();
  266. }
  267. void Mesh::set_morph_target_mode(MorphTargetMode p_mode) {
  268. morph_target_mode=p_mode;
  269. VS::get_singleton()->mesh_set_morph_target_mode(mesh,(VS::MorphTargetMode)p_mode);
  270. }
  271. Mesh::MorphTargetMode Mesh::get_morph_target_mode() const {
  272. return morph_target_mode;
  273. }
  274. void Mesh::surface_remove(int p_idx) {
  275. ERR_FAIL_INDEX(p_idx, surfaces.size() );
  276. VisualServer::get_singleton()->mesh_remove_surface(mesh,p_idx);
  277. surfaces.remove(p_idx);
  278. triangle_mesh=Ref<TriangleMesh>();
  279. _recompute_aabb();
  280. _change_notify();
  281. }
  282. int Mesh::surface_get_array_len(int p_idx) const {
  283. ERR_FAIL_INDEX_V( p_idx, surfaces.size(), -1 );
  284. return VisualServer::get_singleton()->mesh_surface_get_array_len( mesh, p_idx );
  285. }
  286. int Mesh::surface_get_array_index_len(int p_idx) const {
  287. ERR_FAIL_INDEX_V( p_idx, surfaces.size(), -1 );
  288. return VisualServer::get_singleton()->mesh_surface_get_array_index_len( mesh, p_idx );
  289. }
  290. uint32_t Mesh::surface_get_format(int p_idx) const {
  291. ERR_FAIL_INDEX_V( p_idx, surfaces.size(), 0 );
  292. return VisualServer::get_singleton()->mesh_surface_get_format( mesh, p_idx );
  293. }
  294. Mesh::PrimitiveType Mesh::surface_get_primitive_type(int p_idx) const {
  295. ERR_FAIL_INDEX_V( p_idx, surfaces.size(), PRIMITIVE_LINES );
  296. return (PrimitiveType)VisualServer::get_singleton()->mesh_surface_get_primitive_type( mesh, p_idx );
  297. }
  298. bool Mesh::surface_is_alpha_sorting_enabled(int p_idx) const {
  299. ERR_FAIL_INDEX_V( p_idx, surfaces.size(), 0 );
  300. return surfaces[p_idx].alphasort;
  301. }
  302. void Mesh::surface_set_material(int p_idx, const Ref<Material>& p_material) {
  303. ERR_FAIL_INDEX( p_idx, surfaces.size() );
  304. if (surfaces[p_idx].material==p_material)
  305. return;
  306. surfaces[p_idx].material=p_material;
  307. VisualServer::get_singleton()->mesh_surface_set_material(mesh, p_idx, p_material.is_null()?RID():p_material->get_rid());
  308. _change_notify("material");
  309. }
  310. void Mesh::surface_set_name(int p_idx, const String& p_name) {
  311. ERR_FAIL_INDEX( p_idx, surfaces.size() );
  312. surfaces[p_idx].name=p_name;
  313. }
  314. String Mesh::surface_get_name(int p_idx) const{
  315. ERR_FAIL_INDEX_V( p_idx, surfaces.size(),String() );
  316. return surfaces[p_idx].name;
  317. }
  318. void Mesh::surface_set_custom_aabb(int p_idx,const AABB& p_aabb) {
  319. ERR_FAIL_INDEX( p_idx, surfaces.size() );
  320. surfaces[p_idx].aabb=p_aabb;
  321. // set custom aabb too?
  322. }
  323. Ref<Material> Mesh::surface_get_material(int p_idx) const {
  324. ERR_FAIL_INDEX_V( p_idx, surfaces.size(), Ref<Material>() );
  325. return surfaces[p_idx].material;
  326. }
  327. void Mesh::add_surface_from_mesh_data(const Geometry::MeshData& p_mesh_data) {
  328. VisualServer::get_singleton()->mesh_add_surface_from_mesh_data( mesh, p_mesh_data );
  329. AABB aabb;
  330. for (int i=0;i<p_mesh_data.vertices.size();i++) {
  331. if (i==0)
  332. aabb.pos=p_mesh_data.vertices[i];
  333. else
  334. aabb.expand_to(p_mesh_data.vertices[i]);
  335. }
  336. Surface s;
  337. s.aabb=aabb;
  338. if (surfaces.size()==0)
  339. aabb=s.aabb;
  340. else
  341. aabb.merge_with(s.aabb);
  342. triangle_mesh=Ref<TriangleMesh>();
  343. surfaces.push_back(s);
  344. _change_notify();
  345. }
  346. RID Mesh::get_rid() const {
  347. return mesh;
  348. }
  349. AABB Mesh::get_aabb() const {
  350. return aabb;
  351. }
  352. void Mesh::set_custom_aabb(const AABB& p_custom) {
  353. custom_aabb=p_custom;
  354. VS::get_singleton()->mesh_set_custom_aabb(mesh,custom_aabb);
  355. }
  356. AABB Mesh::get_custom_aabb() const {
  357. return custom_aabb;
  358. }
  359. DVector<Face3> Mesh::get_faces() const {
  360. Ref<TriangleMesh> tm = generate_triangle_mesh();
  361. if (tm.is_valid())
  362. return tm->get_faces();
  363. return DVector<Face3>();
  364. /*
  365. for (int i=0;i<surfaces.size();i++) {
  366. if (VisualServer::get_singleton()->mesh_surface_get_primitive_type( mesh, i ) != VisualServer::PRIMITIVE_TRIANGLES )
  367. continue;
  368. DVector<int> indices;
  369. DVector<Vector3> vertices;
  370. vertices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_VERTEX);
  371. int len=VisualServer::get_singleton()->mesh_surface_get_array_index_len(mesh, i);
  372. bool has_indices;
  373. if (len>0) {
  374. indices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_INDEX);
  375. has_indices=true;
  376. } else {
  377. len=vertices.size();
  378. has_indices=false;
  379. }
  380. if (len<=0)
  381. continue;
  382. DVector<int>::Read indicesr = indices.read();
  383. const int *indicesptr = indicesr.ptr();
  384. DVector<Vector3>::Read verticesr = vertices.read();
  385. const Vector3 *verticesptr = verticesr.ptr();
  386. int old_faces=faces.size();
  387. int new_faces=old_faces+(len/3);
  388. faces.resize(new_faces);
  389. DVector<Face3>::Write facesw = faces.write();
  390. Face3 *facesptr=facesw.ptr();
  391. for (int i=0;i<len/3;i++) {
  392. Face3 face;
  393. for (int j=0;j<3;j++) {
  394. int idx=i*3+j;
  395. face.vertex[j] = has_indices ? verticesptr[ indicesptr[ idx ] ] : verticesptr[idx];
  396. }
  397. facesptr[i+old_faces]=face;
  398. }
  399. }
  400. */
  401. }
  402. Ref<Shape> Mesh::create_convex_shape() const {
  403. DVector<Vector3> vertices;
  404. for(int i=0;i<get_surface_count();i++) {
  405. Array a = surface_get_arrays(i);
  406. DVector<Vector3> v=a[ARRAY_VERTEX];
  407. vertices.append_array(v);
  408. }
  409. Ref<ConvexPolygonShape> shape = memnew( ConvexPolygonShape );
  410. shape->set_points(vertices);
  411. return shape;
  412. }
  413. Ref<Shape> Mesh::create_trimesh_shape() const {
  414. DVector<Face3> faces = get_faces();
  415. if (faces.size()==0)
  416. return Ref<Shape>();
  417. DVector<Vector3> face_points;
  418. face_points.resize( faces.size()*3 );
  419. for (int i=0;i<face_points.size();i++) {
  420. Face3 f = faces.get( i/3 );
  421. face_points.set(i, f.vertex[i%3] );
  422. }
  423. Ref<ConcavePolygonShape> shape = memnew( ConcavePolygonShape );
  424. shape->set_faces(face_points);
  425. return shape;
  426. }
  427. void Mesh::center_geometry() {
  428. /*
  429. Vector3 ofs = aabb.pos+aabb.size*0.5;
  430. for(int i=0;i<get_surface_count();i++) {
  431. DVector<Vector3> geom = surface_get_array(i,ARRAY_VERTEX);
  432. int gc =geom.size();
  433. DVector<Vector3>::Write w = geom.write();
  434. surfaces[i].aabb.pos-=ofs;
  435. for(int i=0;i<gc;i++) {
  436. w[i]-=ofs;
  437. }
  438. w = DVector<Vector3>::Write();
  439. surface_set_array(i,ARRAY_VERTEX,geom);
  440. }
  441. aabb.pos-=ofs;
  442. */
  443. }
  444. Ref<TriangleMesh> Mesh::generate_triangle_mesh() const {
  445. if (triangle_mesh.is_valid())
  446. return triangle_mesh;
  447. int facecount=0;
  448. for(int i=0;i<get_surface_count();i++) {
  449. if (surface_get_primitive_type(i)!=PRIMITIVE_TRIANGLES)
  450. continue;
  451. if (surface_get_format(i)&ARRAY_FORMAT_INDEX) {
  452. facecount+=surface_get_array_index_len(i);
  453. } else {
  454. facecount+=surface_get_array_len(i);
  455. }
  456. }
  457. if (facecount==0 || (facecount%3)!=0)
  458. return triangle_mesh;
  459. DVector<Vector3> faces;
  460. faces.resize(facecount);
  461. DVector<Vector3>::Write facesw=faces.write();
  462. int widx=0;
  463. for(int i=0;i<get_surface_count();i++) {
  464. if (surface_get_primitive_type(i)!=PRIMITIVE_TRIANGLES)
  465. continue;
  466. Array a = surface_get_arrays(i);
  467. int vc = surface_get_array_len(i);
  468. DVector<Vector3> vertices = a[ARRAY_VERTEX];
  469. DVector<Vector3>::Read vr=vertices.read();
  470. if (surface_get_format(i)&ARRAY_FORMAT_INDEX) {
  471. int ic=surface_get_array_index_len(i);
  472. DVector<int> indices = a[ARRAY_INDEX];
  473. DVector<int>::Read ir = indices.read();
  474. for(int i=0;i<ic;i++)
  475. facesw[widx++]=vr[ ir[i] ];
  476. } else {
  477. for(int i=0;i<vc;i++)
  478. facesw[widx++]=vr[ i ];
  479. }
  480. }
  481. facesw=DVector<Vector3>::Write();
  482. triangle_mesh = Ref<TriangleMesh>( memnew( TriangleMesh ));
  483. triangle_mesh->create(faces);
  484. return triangle_mesh;
  485. }
  486. void Mesh::_bind_methods() {
  487. ObjectTypeDB::bind_method(_MD("add_morph_target","name"),&Mesh::add_morph_target);
  488. ObjectTypeDB::bind_method(_MD("get_morph_target_count"),&Mesh::get_morph_target_count);
  489. ObjectTypeDB::bind_method(_MD("get_morph_target_name","index"),&Mesh::get_morph_target_name);
  490. ObjectTypeDB::bind_method(_MD("clear_morph_targets"),&Mesh::clear_morph_targets);
  491. ObjectTypeDB::bind_method(_MD("set_morph_target_mode","mode"),&Mesh::set_morph_target_mode);
  492. ObjectTypeDB::bind_method(_MD("get_morph_target_mode"),&Mesh::get_morph_target_mode);
  493. ObjectTypeDB::bind_method(_MD("add_surface","primitive","arrays","morph_arrays"),&Mesh::add_surface,DEFVAL(Array()));
  494. ObjectTypeDB::bind_method(_MD("get_surface_count"),&Mesh::get_surface_count);
  495. ObjectTypeDB::bind_method(_MD("surface_remove","surf_idx"),&Mesh::surface_remove);
  496. ObjectTypeDB::bind_method(_MD("surface_get_array_len","surf_idx"),&Mesh::surface_get_array_len);
  497. ObjectTypeDB::bind_method(_MD("surface_get_array_index_len","surf_idx"),&Mesh::surface_get_array_index_len);
  498. ObjectTypeDB::bind_method(_MD("surface_get_format","surf_idx"),&Mesh::surface_get_format);
  499. ObjectTypeDB::bind_method(_MD("surface_get_primitive_type","surf_idx"),&Mesh::surface_get_primitive_type);
  500. ObjectTypeDB::bind_method(_MD("surface_set_material","surf_idx","material:Material"),&Mesh::surface_set_material);
  501. ObjectTypeDB::bind_method(_MD("surface_get_material:Material","surf_idx"),&Mesh::surface_get_material);
  502. ObjectTypeDB::bind_method(_MD("surface_set_name","surf_idx","name"),&Mesh::surface_set_name);
  503. ObjectTypeDB::bind_method(_MD("surface_get_name","surf_idx"),&Mesh::surface_get_name);
  504. ObjectTypeDB::bind_method(_MD("center_geometry"),&Mesh::center_geometry);
  505. ObjectTypeDB::set_method_flags(get_type_static(),_SCS("center_geometry"),METHOD_FLAGS_DEFAULT|METHOD_FLAG_EDITOR);
  506. ObjectTypeDB::bind_method(_MD("set_custom_aabb","aabb"),&Mesh::set_custom_aabb);
  507. ObjectTypeDB::bind_method(_MD("get_custom_aabb"),&Mesh::get_custom_aabb);
  508. BIND_CONSTANT( NO_INDEX_ARRAY );
  509. BIND_CONSTANT( ARRAY_WEIGHTS_SIZE );
  510. BIND_CONSTANT( ARRAY_VERTEX );
  511. BIND_CONSTANT( ARRAY_NORMAL );
  512. BIND_CONSTANT( ARRAY_TANGENT );
  513. BIND_CONSTANT( ARRAY_COLOR );
  514. BIND_CONSTANT( ARRAY_TEX_UV );
  515. BIND_CONSTANT( ARRAY_TEX_UV2 );
  516. BIND_CONSTANT( ARRAY_BONES );
  517. BIND_CONSTANT( ARRAY_WEIGHTS );
  518. BIND_CONSTANT( ARRAY_INDEX );
  519. BIND_CONSTANT( ARRAY_FORMAT_VERTEX );
  520. BIND_CONSTANT( ARRAY_FORMAT_NORMAL );
  521. BIND_CONSTANT( ARRAY_FORMAT_TANGENT );
  522. BIND_CONSTANT( ARRAY_FORMAT_COLOR );
  523. BIND_CONSTANT( ARRAY_FORMAT_TEX_UV );
  524. BIND_CONSTANT( ARRAY_FORMAT_TEX_UV2 );
  525. BIND_CONSTANT( ARRAY_FORMAT_BONES );
  526. BIND_CONSTANT( ARRAY_FORMAT_WEIGHTS );
  527. BIND_CONSTANT( ARRAY_FORMAT_INDEX );
  528. BIND_CONSTANT( PRIMITIVE_POINTS );
  529. BIND_CONSTANT( PRIMITIVE_LINES );
  530. BIND_CONSTANT( PRIMITIVE_LINE_STRIP );
  531. BIND_CONSTANT( PRIMITIVE_LINE_LOOP );
  532. BIND_CONSTANT( PRIMITIVE_TRIANGLES );
  533. BIND_CONSTANT( PRIMITIVE_TRIANGLE_STRIP );
  534. BIND_CONSTANT( PRIMITIVE_TRIANGLE_FAN );
  535. }
  536. Mesh::Mesh() {
  537. mesh=VisualServer::get_singleton()->mesh_create();
  538. morph_target_mode=MORPH_MODE_RELATIVE;
  539. }
  540. Mesh::~Mesh() {
  541. VisualServer::get_singleton()->free(mesh);
  542. }