mesh.cpp 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /*************************************************************************/
  2. /* mesh.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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. #include "surface_tool.h"
  33. static const char*_array_name[]={
  34. "vertex_array",
  35. "normal_array",
  36. "tangent_array",
  37. "color_array",
  38. "tex_uv_array",
  39. "tex_uv2_array",
  40. "bone_array",
  41. "weights_array",
  42. "index_array",
  43. NULL
  44. };
  45. static const Mesh::ArrayType _array_types[]={
  46. Mesh::ARRAY_VERTEX,
  47. Mesh::ARRAY_NORMAL,
  48. Mesh::ARRAY_TANGENT,
  49. Mesh::ARRAY_COLOR,
  50. Mesh::ARRAY_TEX_UV,
  51. Mesh::ARRAY_TEX_UV2,
  52. Mesh::ARRAY_BONES,
  53. Mesh::ARRAY_WEIGHTS,
  54. Mesh::ARRAY_INDEX
  55. };
  56. /* compatibility */
  57. static const int _format_translate[]={
  58. Mesh::ARRAY_FORMAT_VERTEX,
  59. Mesh::ARRAY_FORMAT_NORMAL,
  60. Mesh::ARRAY_FORMAT_TANGENT,
  61. Mesh::ARRAY_FORMAT_COLOR,
  62. Mesh::ARRAY_FORMAT_TEX_UV,
  63. Mesh::ARRAY_FORMAT_TEX_UV2,
  64. Mesh::ARRAY_FORMAT_BONES,
  65. Mesh::ARRAY_FORMAT_WEIGHTS,
  66. Mesh::ARRAY_FORMAT_INDEX,
  67. };
  68. bool Mesh::_set(const StringName& p_name, const Variant& p_value) {
  69. String sname=p_name;
  70. if (p_name=="morph_target/names") {
  71. DVector<String> sk=p_value;
  72. int sz = sk.size();
  73. DVector<String>::Read r = sk.read();
  74. for(int i=0;i<sz;i++)
  75. add_morph_target(r[i]);
  76. return true;
  77. }
  78. if (p_name=="morph_target/mode") {
  79. set_morph_target_mode(MorphTargetMode(int(p_value)));
  80. return true;
  81. }
  82. if (sname.begins_with("surface_")) {
  83. int sl=sname.find("/");
  84. if (sl==-1)
  85. return false;
  86. int idx=sname.substr(8,sl-8).to_int()-1;
  87. String what = sname.get_slicec('/',1);
  88. if (what=="material")
  89. surface_set_material(idx,p_value);
  90. else if (what=="name")
  91. surface_set_name(idx,p_value);
  92. return true;
  93. }
  94. if (sname=="custom_aabb/custom_aabb") {
  95. set_custom_aabb(p_value);
  96. return true;
  97. }
  98. if (!sname.begins_with("surfaces"))
  99. return false;
  100. int idx=sname.get_slicec('/',1).to_int();
  101. String what=sname.get_slicec('/',2);
  102. if (idx==surfaces.size()) {
  103. if (what=="custom") {
  104. add_custom_surface(p_value);
  105. return true;
  106. }
  107. //create
  108. Dictionary d=p_value;
  109. ERR_FAIL_COND_V(!d.has("primitive"),false);
  110. ERR_FAIL_COND_V(!d.has("arrays"),false);
  111. ERR_FAIL_COND_V(!d.has("morph_arrays"),false);
  112. bool alphasort = d.has("alphasort") && bool(d["alphasort"]);
  113. add_surface(PrimitiveType(int(d["primitive"])),d["arrays"],d["morph_arrays"],alphasort);
  114. if (d.has("material")) {
  115. surface_set_material(idx,d["material"]);
  116. }
  117. if (d.has("name")) {
  118. surface_set_name(idx,d["name"]);
  119. }
  120. return true;
  121. }
  122. if (what=="custom_aabb") {
  123. surface_set_custom_aabb(idx,p_value);
  124. return true;
  125. }
  126. return false;
  127. }
  128. bool Mesh::_get(const StringName& p_name,Variant &r_ret) const {
  129. String sname=p_name;
  130. if (p_name=="morph_target/names") {
  131. DVector<String> sk;
  132. for(int i=0;i<morph_targets.size();i++)
  133. sk.push_back(morph_targets[i]);
  134. r_ret=sk;
  135. return true;
  136. } else if (p_name=="morph_target/mode") {
  137. r_ret = get_morph_target_mode();
  138. return true;
  139. } else if (sname.begins_with("surface_")) {
  140. int sl=sname.find("/");
  141. if (sl==-1)
  142. return false;
  143. int idx=sname.substr(8,sl-8).to_int()-1;
  144. String what = sname.get_slicec('/',1);
  145. if (what=="material")
  146. r_ret=surface_get_material(idx);
  147. else if (what=="name")
  148. r_ret=surface_get_name(idx);
  149. return true;
  150. } else if (sname=="custom_aabb/custom_aabb") {
  151. r_ret=custom_aabb;
  152. return true;
  153. } else if (!sname.begins_with("surfaces"))
  154. return false;
  155. int idx=sname.get_slicec('/',1).to_int();
  156. ERR_FAIL_INDEX_V(idx,surfaces.size(),false);
  157. Dictionary d;
  158. d["primitive"]=surface_get_primitive_type(idx);
  159. d["arrays"]=surface_get_arrays(idx);
  160. d["morph_arrays"]=surface_get_morph_arrays(idx);
  161. d["alphasort"]=surface_is_alpha_sorting_enabled(idx);
  162. Ref<Material> m = surface_get_material(idx);
  163. if (m.is_valid())
  164. d["material"]=m;
  165. String n = surface_get_name(idx);
  166. if (n!="")
  167. d["name"]=n;
  168. r_ret=d;
  169. return true;
  170. }
  171. void Mesh::_get_property_list( List<PropertyInfo> *p_list) const {
  172. if (morph_targets.size()) {
  173. p_list->push_back(PropertyInfo(Variant::STRING_ARRAY,"morph_target/names",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR));
  174. p_list->push_back(PropertyInfo(Variant::INT,"morph_target/mode",PROPERTY_HINT_ENUM,"Normalized,Relative"));
  175. }
  176. for (int i=0;i<surfaces.size();i++) {
  177. p_list->push_back( PropertyInfo( Variant::DICTIONARY,"surfaces/"+itos(i), PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR ) );
  178. p_list->push_back( PropertyInfo( Variant::STRING,"surface_"+itos(i+1)+"/name", PROPERTY_HINT_NONE,"",PROPERTY_USAGE_EDITOR ) );
  179. p_list->push_back( PropertyInfo( Variant::OBJECT,"surface_"+itos(i+1)+"/material", PROPERTY_HINT_RESOURCE_TYPE,"Material",PROPERTY_USAGE_EDITOR ) );
  180. }
  181. p_list->push_back( PropertyInfo( Variant::_AABB,"custom_aabb/custom_aabb" ) );
  182. }
  183. void Mesh::_recompute_aabb() {
  184. // regenerate AABB
  185. aabb=AABB();
  186. for (int i=0;i<surfaces.size();i++) {
  187. if (i==0)
  188. aabb=surfaces[i].aabb;
  189. else
  190. aabb.merge_with(surfaces[i].aabb);
  191. }
  192. }
  193. void Mesh::add_surface(PrimitiveType p_primitive,const Array& p_arrays,const Array& p_blend_shapes,bool p_alphasort) {
  194. ERR_FAIL_COND(p_arrays.size()!=ARRAY_MAX);
  195. Surface s;
  196. VisualServer::get_singleton()->mesh_add_surface(mesh,(VisualServer::PrimitiveType)p_primitive, p_arrays,p_blend_shapes,p_alphasort);
  197. surfaces.push_back(s);
  198. /* make aABB? */ {
  199. DVector<Vector3> vertices=p_arrays[ARRAY_VERTEX];
  200. int len=vertices.size();
  201. ERR_FAIL_COND(len==0);
  202. DVector<Vector3>::Read r=vertices.read();
  203. const Vector3 *vtx=r.ptr();
  204. // check AABB
  205. AABB aabb;
  206. for (int i=0;i<len;i++) {
  207. if (i==0)
  208. aabb.pos=vtx[i];
  209. else
  210. aabb.expand_to(vtx[i]);
  211. }
  212. surfaces[surfaces.size()-1].aabb=aabb;
  213. surfaces[surfaces.size()-1].alphasort=p_alphasort;
  214. _recompute_aabb();
  215. }
  216. triangle_mesh=Ref<TriangleMesh>();
  217. _change_notify();
  218. }
  219. Array Mesh::surface_get_arrays(int p_surface) const {
  220. ERR_FAIL_INDEX_V(p_surface,surfaces.size(),Array());
  221. return VisualServer::get_singleton()->mesh_get_surface_arrays(mesh,p_surface);
  222. }
  223. Array Mesh::surface_get_morph_arrays(int p_surface) const {
  224. ERR_FAIL_INDEX_V(p_surface,surfaces.size(),Array());
  225. return VisualServer::get_singleton()->mesh_get_surface_morph_arrays(mesh,p_surface);
  226. }
  227. void Mesh::add_custom_surface(const Variant& p_data) {
  228. Surface s;
  229. s.aabb=AABB();
  230. VisualServer::get_singleton()->mesh_add_custom_surface(mesh,p_data);
  231. surfaces.push_back(s);
  232. triangle_mesh=Ref<TriangleMesh>();
  233. _change_notify();
  234. }
  235. int Mesh::get_surface_count() const {
  236. return surfaces.size();
  237. }
  238. void Mesh::add_morph_target(const StringName& p_name) {
  239. if (surfaces.size()) {
  240. ERR_EXPLAIN("Can't add a shape key count if surfaces are already created.");
  241. ERR_FAIL_COND(surfaces.size());
  242. }
  243. StringName name=p_name;
  244. if (morph_targets.find(name)!=-1 ) {
  245. int count=2;
  246. do {
  247. name = String(p_name) + " " + itos(count);
  248. count++;
  249. } while(morph_targets.find(name)!=-1);
  250. }
  251. morph_targets.push_back(name);
  252. VS::get_singleton()->mesh_set_morph_target_count(mesh,morph_targets.size());
  253. }
  254. int Mesh::get_morph_target_count() const {
  255. return morph_targets.size();
  256. }
  257. StringName Mesh::get_morph_target_name(int p_index) const {
  258. ERR_FAIL_INDEX_V( p_index, morph_targets.size(),StringName() );
  259. return morph_targets[p_index];
  260. }
  261. void Mesh::clear_morph_targets() {
  262. if (surfaces.size()) {
  263. ERR_EXPLAIN("Can't set shape key count if surfaces are already created.");
  264. ERR_FAIL_COND(surfaces.size());
  265. }
  266. morph_targets.clear();
  267. }
  268. void Mesh::set_morph_target_mode(MorphTargetMode p_mode) {
  269. morph_target_mode=p_mode;
  270. VS::get_singleton()->mesh_set_morph_target_mode(mesh,(VS::MorphTargetMode)p_mode);
  271. }
  272. Mesh::MorphTargetMode Mesh::get_morph_target_mode() const {
  273. return morph_target_mode;
  274. }
  275. void Mesh::surface_remove(int p_idx) {
  276. ERR_FAIL_INDEX(p_idx, surfaces.size() );
  277. VisualServer::get_singleton()->mesh_remove_surface(mesh,p_idx);
  278. surfaces.remove(p_idx);
  279. triangle_mesh=Ref<TriangleMesh>();
  280. _recompute_aabb();
  281. _change_notify();
  282. }
  283. int Mesh::surface_get_array_len(int p_idx) const {
  284. ERR_FAIL_INDEX_V( p_idx, surfaces.size(), -1 );
  285. return VisualServer::get_singleton()->mesh_surface_get_array_len( mesh, p_idx );
  286. }
  287. int Mesh::surface_get_array_index_len(int p_idx) const {
  288. ERR_FAIL_INDEX_V( p_idx, surfaces.size(), -1 );
  289. return VisualServer::get_singleton()->mesh_surface_get_array_index_len( mesh, p_idx );
  290. }
  291. uint32_t Mesh::surface_get_format(int p_idx) const {
  292. ERR_FAIL_INDEX_V( p_idx, surfaces.size(), 0 );
  293. return VisualServer::get_singleton()->mesh_surface_get_format( mesh, p_idx );
  294. }
  295. Mesh::PrimitiveType Mesh::surface_get_primitive_type(int p_idx) const {
  296. ERR_FAIL_INDEX_V( p_idx, surfaces.size(), PRIMITIVE_LINES );
  297. return (PrimitiveType)VisualServer::get_singleton()->mesh_surface_get_primitive_type( mesh, p_idx );
  298. }
  299. bool Mesh::surface_is_alpha_sorting_enabled(int p_idx) const {
  300. ERR_FAIL_INDEX_V( p_idx, surfaces.size(), 0 );
  301. return surfaces[p_idx].alphasort;
  302. }
  303. void Mesh::surface_set_material(int p_idx, const Ref<Material>& p_material) {
  304. ERR_FAIL_INDEX( p_idx, surfaces.size() );
  305. if (surfaces[p_idx].material==p_material)
  306. return;
  307. surfaces[p_idx].material=p_material;
  308. VisualServer::get_singleton()->mesh_surface_set_material(mesh, p_idx, p_material.is_null()?RID():p_material->get_rid());
  309. _change_notify("material");
  310. }
  311. void Mesh::surface_set_name(int p_idx, const String& p_name) {
  312. ERR_FAIL_INDEX( p_idx, surfaces.size() );
  313. surfaces[p_idx].name=p_name;
  314. }
  315. String Mesh::surface_get_name(int p_idx) const{
  316. ERR_FAIL_INDEX_V( p_idx, surfaces.size(),String() );
  317. return surfaces[p_idx].name;
  318. }
  319. void Mesh::surface_set_custom_aabb(int p_idx,const AABB& p_aabb) {
  320. ERR_FAIL_INDEX( p_idx, surfaces.size() );
  321. surfaces[p_idx].aabb=p_aabb;
  322. // set custom aabb too?
  323. }
  324. Ref<Material> Mesh::surface_get_material(int p_idx) const {
  325. ERR_FAIL_INDEX_V( p_idx, surfaces.size(), Ref<Material>() );
  326. return surfaces[p_idx].material;
  327. }
  328. void Mesh::add_surface_from_mesh_data(const Geometry::MeshData& p_mesh_data) {
  329. VisualServer::get_singleton()->mesh_add_surface_from_mesh_data( mesh, p_mesh_data );
  330. AABB aabb;
  331. for (int i=0;i<p_mesh_data.vertices.size();i++) {
  332. if (i==0)
  333. aabb.pos=p_mesh_data.vertices[i];
  334. else
  335. aabb.expand_to(p_mesh_data.vertices[i]);
  336. }
  337. Surface s;
  338. s.aabb=aabb;
  339. if (surfaces.size()==0)
  340. aabb=s.aabb;
  341. else
  342. aabb.merge_with(s.aabb);
  343. triangle_mesh=Ref<TriangleMesh>();
  344. surfaces.push_back(s);
  345. _change_notify();
  346. }
  347. RID Mesh::get_rid() const {
  348. return mesh;
  349. }
  350. AABB Mesh::get_aabb() const {
  351. return aabb;
  352. }
  353. void Mesh::set_custom_aabb(const AABB& p_custom) {
  354. custom_aabb=p_custom;
  355. VS::get_singleton()->mesh_set_custom_aabb(mesh,custom_aabb);
  356. }
  357. AABB Mesh::get_custom_aabb() const {
  358. return custom_aabb;
  359. }
  360. DVector<Face3> Mesh::get_faces() const {
  361. Ref<TriangleMesh> tm = generate_triangle_mesh();
  362. if (tm.is_valid())
  363. return tm->get_faces();
  364. return DVector<Face3>();
  365. /*
  366. for (int i=0;i<surfaces.size();i++) {
  367. if (VisualServer::get_singleton()->mesh_surface_get_primitive_type( mesh, i ) != VisualServer::PRIMITIVE_TRIANGLES )
  368. continue;
  369. DVector<int> indices;
  370. DVector<Vector3> vertices;
  371. vertices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_VERTEX);
  372. int len=VisualServer::get_singleton()->mesh_surface_get_array_index_len(mesh, i);
  373. bool has_indices;
  374. if (len>0) {
  375. indices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_INDEX);
  376. has_indices=true;
  377. } else {
  378. len=vertices.size();
  379. has_indices=false;
  380. }
  381. if (len<=0)
  382. continue;
  383. DVector<int>::Read indicesr = indices.read();
  384. const int *indicesptr = indicesr.ptr();
  385. DVector<Vector3>::Read verticesr = vertices.read();
  386. const Vector3 *verticesptr = verticesr.ptr();
  387. int old_faces=faces.size();
  388. int new_faces=old_faces+(len/3);
  389. faces.resize(new_faces);
  390. DVector<Face3>::Write facesw = faces.write();
  391. Face3 *facesptr=facesw.ptr();
  392. for (int i=0;i<len/3;i++) {
  393. Face3 face;
  394. for (int j=0;j<3;j++) {
  395. int idx=i*3+j;
  396. face.vertex[j] = has_indices ? verticesptr[ indicesptr[ idx ] ] : verticesptr[idx];
  397. }
  398. facesptr[i+old_faces]=face;
  399. }
  400. }
  401. */
  402. }
  403. Ref<Shape> Mesh::create_convex_shape() const {
  404. DVector<Vector3> vertices;
  405. for(int i=0;i<get_surface_count();i++) {
  406. Array a = surface_get_arrays(i);
  407. DVector<Vector3> v=a[ARRAY_VERTEX];
  408. vertices.append_array(v);
  409. }
  410. Ref<ConvexPolygonShape> shape = memnew( ConvexPolygonShape );
  411. shape->set_points(vertices);
  412. return shape;
  413. }
  414. Ref<Shape> Mesh::create_trimesh_shape() const {
  415. DVector<Face3> faces = get_faces();
  416. if (faces.size()==0)
  417. return Ref<Shape>();
  418. DVector<Vector3> face_points;
  419. face_points.resize( faces.size()*3 );
  420. for (int i=0;i<face_points.size();i++) {
  421. Face3 f = faces.get( i/3 );
  422. face_points.set(i, f.vertex[i%3] );
  423. }
  424. Ref<ConcavePolygonShape> shape = memnew( ConcavePolygonShape );
  425. shape->set_faces(face_points);
  426. return shape;
  427. }
  428. void Mesh::center_geometry() {
  429. /*
  430. Vector3 ofs = aabb.pos+aabb.size*0.5;
  431. for(int i=0;i<get_surface_count();i++) {
  432. DVector<Vector3> geom = surface_get_array(i,ARRAY_VERTEX);
  433. int gc =geom.size();
  434. DVector<Vector3>::Write w = geom.write();
  435. surfaces[i].aabb.pos-=ofs;
  436. for(int i=0;i<gc;i++) {
  437. w[i]-=ofs;
  438. }
  439. w = DVector<Vector3>::Write();
  440. surface_set_array(i,ARRAY_VERTEX,geom);
  441. }
  442. aabb.pos-=ofs;
  443. */
  444. }
  445. void Mesh::regen_normalmaps() {
  446. Vector< Ref<SurfaceTool> > surfs;
  447. for(int i=0;i<get_surface_count();i++) {
  448. Ref<SurfaceTool> st = memnew( SurfaceTool );
  449. st->create_from(Ref<Mesh>(this),i);
  450. surfs.push_back(st);
  451. }
  452. while (get_surface_count()) {
  453. surface_remove(0);
  454. }
  455. for(int i=0;i<surfs.size();i++) {
  456. surfs[i]->generate_tangents();
  457. surfs[i]->commit(Ref<Mesh>(this));
  458. }
  459. }
  460. Ref<TriangleMesh> Mesh::generate_triangle_mesh() const {
  461. if (triangle_mesh.is_valid())
  462. return triangle_mesh;
  463. int facecount=0;
  464. for(int i=0;i<get_surface_count();i++) {
  465. if (surface_get_primitive_type(i)!=PRIMITIVE_TRIANGLES)
  466. continue;
  467. if (surface_get_format(i)&ARRAY_FORMAT_INDEX) {
  468. facecount+=surface_get_array_index_len(i);
  469. } else {
  470. facecount+=surface_get_array_len(i);
  471. }
  472. }
  473. if (facecount==0 || (facecount%3)!=0)
  474. return triangle_mesh;
  475. DVector<Vector3> faces;
  476. faces.resize(facecount);
  477. DVector<Vector3>::Write facesw=faces.write();
  478. int widx=0;
  479. for(int i=0;i<get_surface_count();i++) {
  480. if (surface_get_primitive_type(i)!=PRIMITIVE_TRIANGLES)
  481. continue;
  482. Array a = surface_get_arrays(i);
  483. int vc = surface_get_array_len(i);
  484. DVector<Vector3> vertices = a[ARRAY_VERTEX];
  485. DVector<Vector3>::Read vr=vertices.read();
  486. if (surface_get_format(i)&ARRAY_FORMAT_INDEX) {
  487. int ic=surface_get_array_index_len(i);
  488. DVector<int> indices = a[ARRAY_INDEX];
  489. DVector<int>::Read ir = indices.read();
  490. for(int i=0;i<ic;i++)
  491. facesw[widx++]=vr[ ir[i] ];
  492. } else {
  493. for(int i=0;i<vc;i++)
  494. facesw[widx++]=vr[ i ];
  495. }
  496. }
  497. facesw=DVector<Vector3>::Write();
  498. triangle_mesh = Ref<TriangleMesh>( memnew( TriangleMesh ));
  499. triangle_mesh->create(faces);
  500. return triangle_mesh;
  501. }
  502. Ref<Mesh> Mesh::create_outline(float p_margin) const {
  503. Array arrays;
  504. int index_accum=0;
  505. for(int i=0;i<get_surface_count();i++) {
  506. if (surface_get_primitive_type(i)!=PRIMITIVE_TRIANGLES)
  507. continue;
  508. Array a = surface_get_arrays(i);
  509. int vcount=0;
  510. if (i==0) {
  511. arrays=a;
  512. DVector<Vector3> v=a[ARRAY_VERTEX];
  513. index_accum+=v.size();
  514. } else {
  515. for(int j=0;j<arrays.size();j++) {
  516. if (arrays[j].get_type()==Variant::NIL || a[j].get_type()==Variant::NIL) {
  517. //mismatch, do not use
  518. arrays[j]=Variant();
  519. continue;
  520. }
  521. switch(j) {
  522. case ARRAY_VERTEX:
  523. case ARRAY_NORMAL: {
  524. DVector<Vector3> dst = arrays[j];
  525. DVector<Vector3> src = a[j];
  526. if (j==ARRAY_VERTEX)
  527. vcount=src.size();
  528. if (dst.size()==0 || src.size()==0) {
  529. arrays[j]=Variant();
  530. continue;
  531. }
  532. dst.append_array(src);
  533. arrays[j]=dst;
  534. } break;
  535. case ARRAY_TANGENT:
  536. case ARRAY_BONES:
  537. case ARRAY_WEIGHTS: {
  538. DVector<real_t> dst = arrays[j];
  539. DVector<real_t> src = a[j];
  540. if (dst.size()==0 || src.size()==0) {
  541. arrays[j]=Variant();
  542. continue;
  543. }
  544. dst.append_array(src);
  545. arrays[j]=dst;
  546. } break;
  547. case ARRAY_COLOR: {
  548. DVector<Color> dst = arrays[j];
  549. DVector<Color> src = a[j];
  550. if (dst.size()==0 || src.size()==0) {
  551. arrays[j]=Variant();
  552. continue;
  553. }
  554. dst.append_array(src);
  555. arrays[j]=dst;
  556. } break;
  557. case ARRAY_TEX_UV:
  558. case ARRAY_TEX_UV2: {
  559. DVector<Vector2> dst = arrays[j];
  560. DVector<Vector2> src = a[j];
  561. if (dst.size()==0 || src.size()==0) {
  562. arrays[j]=Variant();
  563. continue;
  564. }
  565. dst.append_array(src);
  566. arrays[j]=dst;
  567. } break;
  568. case ARRAY_INDEX: {
  569. DVector<int> dst = arrays[j];
  570. DVector<int> src = a[j];
  571. if (dst.size()==0 || src.size()==0) {
  572. arrays[j]=Variant();
  573. continue;
  574. }
  575. {
  576. int ss = src.size();
  577. DVector<int>::Write w = src.write();
  578. for(int k=0;k<ss;k++) {
  579. w[k]+=index_accum;
  580. }
  581. }
  582. dst.append_array(src);
  583. arrays[j]=dst;
  584. index_accum+=vcount;
  585. } break;
  586. }
  587. }
  588. }
  589. }
  590. {
  591. int tc=0;
  592. DVector<int>::Write ir;
  593. DVector<int> indices =arrays[ARRAY_INDEX];
  594. bool has_indices=false;
  595. DVector<Vector3> vertices =arrays[ARRAY_VERTEX];
  596. int vc = vertices.size();
  597. ERR_FAIL_COND_V(!vc,Ref<Mesh>());
  598. DVector<Vector3>::Write r=vertices.write();
  599. if (indices.size()) {
  600. vc=indices.size();
  601. ir=indices.write();
  602. has_indices=true;
  603. }
  604. Map<Vector3,Vector3> normal_accum;
  605. //fill normals with triangle normals
  606. for(int i=0;i<vc;i+=3) {
  607. Vector3 t[3];
  608. if (has_indices) {
  609. t[0]=r[ir[i+0]];
  610. t[1]=r[ir[i+1]];
  611. t[2]=r[ir[i+2]];
  612. } else {
  613. t[0]=r[i+0];
  614. t[1]=r[i+1];
  615. t[2]=r[i+2];
  616. }
  617. Vector3 n = Plane(t[0],t[1],t[2]).normal;
  618. for(int j=0;j<3;j++) {
  619. Map<Vector3,Vector3>::Element *E=normal_accum.find(t[j]);
  620. if (!E) {
  621. normal_accum[t[j]]=n;
  622. } else {
  623. float d = n.dot(E->get());
  624. if (d<1.0)
  625. E->get()+=n*(1.0-d);
  626. //E->get()+=n;
  627. }
  628. }
  629. }
  630. //normalize
  631. for (Map<Vector3,Vector3>::Element *E=normal_accum.front();E;E=E->next()) {
  632. E->get().normalize();
  633. }
  634. //displace normals
  635. int vc2 = vertices.size();
  636. for(int i=0;i<vc2;i++) {
  637. Vector3 t=r[i];
  638. Map<Vector3,Vector3>::Element *E=normal_accum.find(t);
  639. ERR_CONTINUE(!E);
  640. t+=E->get()*p_margin;
  641. r[i]=t;
  642. }
  643. r = DVector<Vector3>::Write();
  644. arrays[ARRAY_VERTEX]=vertices;
  645. if (!has_indices) {
  646. DVector<int> new_indices;
  647. new_indices.resize(vertices.size());
  648. DVector<int>::Write iw = new_indices.write();
  649. for(int j=0;j<vc2;j+=3) {
  650. iw[j]=j;
  651. iw[j+1]=j+2;
  652. iw[j+2]=j+1;
  653. }
  654. iw=DVector<int>::Write();
  655. arrays[ARRAY_INDEX]=new_indices;
  656. } else {
  657. for(int j=0;j<vc;j+=3) {
  658. SWAP(ir[j+1],ir[j+2]);
  659. }
  660. ir=DVector<int>::Write();
  661. arrays[ARRAY_INDEX]=indices;
  662. }
  663. }
  664. Ref<Mesh> newmesh = memnew( Mesh );
  665. newmesh->add_surface(PRIMITIVE_TRIANGLES,arrays);
  666. return newmesh;
  667. }
  668. void Mesh::_bind_methods() {
  669. ObjectTypeDB::bind_method(_MD("add_morph_target","name"),&Mesh::add_morph_target);
  670. ObjectTypeDB::bind_method(_MD("get_morph_target_count"),&Mesh::get_morph_target_count);
  671. ObjectTypeDB::bind_method(_MD("get_morph_target_name","index"),&Mesh::get_morph_target_name);
  672. ObjectTypeDB::bind_method(_MD("clear_morph_targets"),&Mesh::clear_morph_targets);
  673. ObjectTypeDB::bind_method(_MD("set_morph_target_mode","mode"),&Mesh::set_morph_target_mode);
  674. ObjectTypeDB::bind_method(_MD("get_morph_target_mode"),&Mesh::get_morph_target_mode);
  675. ObjectTypeDB::bind_method(_MD("add_surface","primitive","arrays","morph_arrays","alphasort"),&Mesh::add_surface,DEFVAL(Array()),DEFVAL(false));
  676. ObjectTypeDB::bind_method(_MD("get_surface_count"),&Mesh::get_surface_count);
  677. ObjectTypeDB::bind_method(_MD("surface_remove","surf_idx"),&Mesh::surface_remove);
  678. ObjectTypeDB::bind_method(_MD("surface_get_array_len","surf_idx"),&Mesh::surface_get_array_len);
  679. ObjectTypeDB::bind_method(_MD("surface_get_array_index_len","surf_idx"),&Mesh::surface_get_array_index_len);
  680. ObjectTypeDB::bind_method(_MD("surface_get_format","surf_idx"),&Mesh::surface_get_format);
  681. ObjectTypeDB::bind_method(_MD("surface_get_primitive_type","surf_idx"),&Mesh::surface_get_primitive_type);
  682. ObjectTypeDB::bind_method(_MD("surface_set_material","surf_idx","material:Material"),&Mesh::surface_set_material);
  683. ObjectTypeDB::bind_method(_MD("surface_get_material:Material","surf_idx"),&Mesh::surface_get_material);
  684. ObjectTypeDB::bind_method(_MD("surface_set_name","surf_idx","name"),&Mesh::surface_set_name);
  685. ObjectTypeDB::bind_method(_MD("surface_get_name","surf_idx"),&Mesh::surface_get_name);
  686. ObjectTypeDB::bind_method(_MD("center_geometry"),&Mesh::center_geometry);
  687. ObjectTypeDB::set_method_flags(get_type_static(),_SCS("center_geometry"),METHOD_FLAGS_DEFAULT|METHOD_FLAG_EDITOR);
  688. ObjectTypeDB::bind_method(_MD("regen_normalmaps"),&Mesh::regen_normalmaps);
  689. ObjectTypeDB::set_method_flags(get_type_static(),_SCS("regen_normalmaps"),METHOD_FLAGS_DEFAULT|METHOD_FLAG_EDITOR);
  690. ObjectTypeDB::bind_method(_MD("set_custom_aabb","aabb"),&Mesh::set_custom_aabb);
  691. ObjectTypeDB::bind_method(_MD("get_custom_aabb"),&Mesh::get_custom_aabb);
  692. BIND_CONSTANT( NO_INDEX_ARRAY );
  693. BIND_CONSTANT( ARRAY_WEIGHTS_SIZE );
  694. BIND_CONSTANT( ARRAY_VERTEX );
  695. BIND_CONSTANT( ARRAY_NORMAL );
  696. BIND_CONSTANT( ARRAY_TANGENT );
  697. BIND_CONSTANT( ARRAY_COLOR );
  698. BIND_CONSTANT( ARRAY_TEX_UV );
  699. BIND_CONSTANT( ARRAY_TEX_UV2 );
  700. BIND_CONSTANT( ARRAY_BONES );
  701. BIND_CONSTANT( ARRAY_WEIGHTS );
  702. BIND_CONSTANT( ARRAY_INDEX );
  703. BIND_CONSTANT( ARRAY_FORMAT_VERTEX );
  704. BIND_CONSTANT( ARRAY_FORMAT_NORMAL );
  705. BIND_CONSTANT( ARRAY_FORMAT_TANGENT );
  706. BIND_CONSTANT( ARRAY_FORMAT_COLOR );
  707. BIND_CONSTANT( ARRAY_FORMAT_TEX_UV );
  708. BIND_CONSTANT( ARRAY_FORMAT_TEX_UV2 );
  709. BIND_CONSTANT( ARRAY_FORMAT_BONES );
  710. BIND_CONSTANT( ARRAY_FORMAT_WEIGHTS );
  711. BIND_CONSTANT( ARRAY_FORMAT_INDEX );
  712. BIND_CONSTANT( PRIMITIVE_POINTS );
  713. BIND_CONSTANT( PRIMITIVE_LINES );
  714. BIND_CONSTANT( PRIMITIVE_LINE_STRIP );
  715. BIND_CONSTANT( PRIMITIVE_LINE_LOOP );
  716. BIND_CONSTANT( PRIMITIVE_TRIANGLES );
  717. BIND_CONSTANT( PRIMITIVE_TRIANGLE_STRIP );
  718. BIND_CONSTANT( PRIMITIVE_TRIANGLE_FAN );
  719. }
  720. Mesh::Mesh() {
  721. mesh=VisualServer::get_singleton()->mesh_create();
  722. morph_target_mode=MORPH_MODE_RELATIVE;
  723. }
  724. Mesh::~Mesh() {
  725. VisualServer::get_singleton()->free(mesh);
  726. }