surface_tool.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /*************************************************************************/
  2. /* surface_tool.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 "surface_tool.h"
  30. #define _VERTEX_SNAP 0.0001
  31. #define EQ_VERTEX_DIST 0.00001
  32. bool SurfaceTool::Vertex::operator==(const Vertex& p_b) const {
  33. if (vertex!=p_b.vertex)
  34. return false;
  35. if (uv!=p_b.uv)
  36. return false;
  37. if (uv2!=p_b.uv2)
  38. return false;
  39. if (normal!=p_b.normal)
  40. return false;
  41. if (binormal!=p_b.binormal)
  42. return false;
  43. if (color!=p_b.color)
  44. return false;
  45. if (bones.size()!=p_b.bones.size())
  46. return false;
  47. for(int i=0;i<bones.size();i++) {
  48. if (bones[i]!=p_b.bones[i])
  49. return false;
  50. }
  51. for(int i=0;i<weights.size();i++) {
  52. if (weights[i]!=p_b.weights[i])
  53. return false;
  54. }
  55. return true;
  56. }
  57. uint32_t SurfaceTool::VertexHasher::hash(const Vertex &p_vtx) {
  58. uint32_t h = hash_djb2_buffer((const uint8_t*)&p_vtx.vertex,sizeof(real_t)*3);
  59. h = hash_djb2_buffer((const uint8_t*)&p_vtx.normal,sizeof(real_t)*3,h);
  60. h = hash_djb2_buffer((const uint8_t*)&p_vtx.binormal,sizeof(real_t)*3,h);
  61. h = hash_djb2_buffer((const uint8_t*)&p_vtx.tangent,sizeof(real_t)*3,h);
  62. h = hash_djb2_buffer((const uint8_t*)&p_vtx.uv,sizeof(real_t)*2,h);
  63. h = hash_djb2_buffer((const uint8_t*)&p_vtx.uv2,sizeof(real_t)*2,h);
  64. h = hash_djb2_buffer((const uint8_t*)&p_vtx.color,sizeof(real_t)*4,h);
  65. h = hash_djb2_buffer((const uint8_t*)p_vtx.bones.ptr(),p_vtx.bones.size()*sizeof(int),h);
  66. h = hash_djb2_buffer((const uint8_t*)p_vtx.weights.ptr(),p_vtx.weights.size()*sizeof(float),h);
  67. return h;
  68. }
  69. void SurfaceTool::begin(Mesh::PrimitiveType p_primitive) {
  70. clear();
  71. primitive=p_primitive;
  72. begun=true;
  73. first=true;
  74. }
  75. void SurfaceTool::add_vertex( const Vector3& p_vertex) {
  76. ERR_FAIL_COND(!begun);
  77. Vertex vtx;
  78. vtx.vertex=p_vertex;
  79. vtx.color=last_color;
  80. vtx.normal=last_normal;
  81. vtx.uv=last_uv;
  82. vtx.weights=last_weights;
  83. vtx.bones=last_bones;
  84. vtx.tangent=last_tangent.normal;
  85. vtx.binormal=last_tangent.normal.cross(last_normal).normalized() * last_tangent.d;
  86. vertex_array.push_back(vtx);
  87. first=false;
  88. format|=Mesh::ARRAY_FORMAT_VERTEX;
  89. }
  90. void SurfaceTool::add_color( Color p_color ) {
  91. ERR_FAIL_COND(!begun);
  92. ERR_FAIL_COND( !first && !(format&Mesh::ARRAY_FORMAT_COLOR));
  93. format|=Mesh::ARRAY_FORMAT_COLOR;
  94. last_color=p_color;
  95. }
  96. void SurfaceTool::add_normal( const Vector3& p_normal) {
  97. ERR_FAIL_COND(!begun);
  98. ERR_FAIL_COND( !first && !(format&Mesh::ARRAY_FORMAT_NORMAL));
  99. format|=Mesh::ARRAY_FORMAT_NORMAL;
  100. last_normal=p_normal;
  101. }
  102. void SurfaceTool::add_tangent( const Plane& p_tangent ) {
  103. ERR_FAIL_COND(!begun);
  104. ERR_FAIL_COND( !first && !(format&Mesh::ARRAY_FORMAT_TANGENT));
  105. format|=Mesh::ARRAY_FORMAT_TANGENT;
  106. last_tangent=p_tangent;
  107. }
  108. void SurfaceTool::add_uv( const Vector2& p_uv) {
  109. ERR_FAIL_COND(!begun);
  110. ERR_FAIL_COND( !first && !(format&Mesh::ARRAY_FORMAT_TEX_UV));
  111. format|=Mesh::ARRAY_FORMAT_TEX_UV;
  112. last_uv=p_uv;
  113. }
  114. void SurfaceTool::add_uv2( const Vector2& p_uv2) {
  115. ERR_FAIL_COND(!begun);
  116. ERR_FAIL_COND( !first && !(format&Mesh::ARRAY_FORMAT_TEX_UV2));
  117. format|=Mesh::ARRAY_FORMAT_TEX_UV2;
  118. last_uv2=p_uv2;
  119. }
  120. void SurfaceTool::add_bones( const Vector<int>& p_bones) {
  121. ERR_FAIL_COND(!begun);
  122. ERR_FAIL_COND(p_bones.size()!=4);
  123. ERR_FAIL_COND( !first && !(format&Mesh::ARRAY_FORMAT_BONES));
  124. format|=Mesh::ARRAY_FORMAT_BONES;
  125. last_bones=p_bones;
  126. }
  127. void SurfaceTool::add_weights( const Vector<float>& p_weights) {
  128. ERR_FAIL_COND(!begun);
  129. ERR_FAIL_COND(p_weights.size()!=4);
  130. ERR_FAIL_COND( !first && !(format&Mesh::ARRAY_FORMAT_WEIGHTS));
  131. format|=Mesh::ARRAY_FORMAT_WEIGHTS;
  132. last_weights=p_weights;
  133. }
  134. void SurfaceTool::add_smooth_group(bool p_smooth) {
  135. ERR_FAIL_COND(!begun);
  136. if (index_array.size()) {
  137. smooth_groups[index_array.size()]=p_smooth;
  138. } else {
  139. smooth_groups[vertex_array.size()]=p_smooth;
  140. }
  141. }
  142. void SurfaceTool::add_index( int p_index) {
  143. ERR_FAIL_COND(!begun);
  144. format|=Mesh::ARRAY_FORMAT_INDEX;
  145. index_array.push_back(p_index);
  146. }
  147. Ref<Mesh> SurfaceTool::commit(const Ref<Mesh>& p_existing) {
  148. Ref<Mesh> mesh;
  149. if (p_existing.is_valid())
  150. mesh=p_existing;
  151. else
  152. mesh= Ref<Mesh>( memnew( Mesh ) );
  153. int varr_len=vertex_array.size();
  154. if (varr_len==0)
  155. return mesh;
  156. int surface = mesh->get_surface_count();
  157. Array a;
  158. a.resize(Mesh::ARRAY_MAX);
  159. for (int i=0;i<Mesh::ARRAY_MAX;i++) {
  160. switch(format&(1<<i)) {
  161. case Mesh::ARRAY_FORMAT_VERTEX:
  162. case Mesh::ARRAY_FORMAT_NORMAL: {
  163. DVector<Vector3> array;
  164. array.resize(varr_len);
  165. DVector<Vector3>::Write w = array.write();
  166. int idx=0;
  167. for(List< Vertex >::Element *E=vertex_array.front();E;E=E->next(),idx++) {
  168. const Vertex &v=E->get();
  169. switch(i) {
  170. case Mesh::ARRAY_VERTEX: {
  171. w[idx]=v.vertex;
  172. } break;
  173. case Mesh::ARRAY_NORMAL: {
  174. w[idx]=v.normal;
  175. } break;
  176. }
  177. }
  178. w=DVector<Vector3>::Write();
  179. a[i]=array;
  180. } break;
  181. case Mesh::ARRAY_FORMAT_TEX_UV:
  182. case Mesh::ARRAY_FORMAT_TEX_UV2: {
  183. DVector<Vector2> array;
  184. array.resize(varr_len);
  185. DVector<Vector2>::Write w = array.write();
  186. int idx=0;
  187. for(List< Vertex >::Element *E=vertex_array.front();E;E=E->next(),idx++) {
  188. const Vertex &v=E->get();
  189. switch(i) {
  190. case Mesh::ARRAY_TEX_UV: {
  191. w[idx]=v.uv;
  192. } break;
  193. case Mesh::ARRAY_TEX_UV2: {
  194. w[idx]=v.uv2;
  195. } break;
  196. }
  197. }
  198. w=DVector<Vector2>::Write();
  199. a[i]=array;
  200. } break;
  201. case Mesh::ARRAY_FORMAT_TANGENT: {
  202. DVector<float> array;
  203. array.resize(varr_len*4);
  204. DVector<float>::Write w = array.write();
  205. int idx=0;
  206. for(List< Vertex >::Element *E=vertex_array.front();E;E=E->next(),idx+=4) {
  207. const Vertex &v=E->get();
  208. w[idx+0]=v.tangent.x;
  209. w[idx+1]=v.tangent.y;
  210. w[idx+2]=v.tangent.z;
  211. float d = v.binormal.dot(v.normal.cross(v.tangent));
  212. w[idx+3]=d<0 ? -1 : 1;
  213. }
  214. w=DVector<float>::Write();
  215. a[i]=array;
  216. } break;
  217. case Mesh::ARRAY_FORMAT_COLOR: {
  218. DVector<Color> array;
  219. array.resize(varr_len);
  220. DVector<Color>::Write w = array.write();
  221. int idx=0;
  222. for(List< Vertex >::Element *E=vertex_array.front();E;E=E->next(),idx++) {
  223. const Vertex &v=E->get();
  224. w[idx]=v.color;
  225. }
  226. w=DVector<Color>::Write();
  227. a[i]=array;
  228. } break;
  229. case Mesh::ARRAY_FORMAT_BONES:
  230. case Mesh::ARRAY_FORMAT_WEIGHTS: {
  231. DVector<float> array;
  232. array.resize(varr_len*4);
  233. DVector<float>::Write w = array.write();
  234. int idx=0;
  235. for(List< Vertex >::Element *E=vertex_array.front();E;E=E->next(),idx+=4) {
  236. const Vertex &v=E->get();
  237. for(int j=0;j<4;j++) {
  238. switch(i) {
  239. case Mesh::ARRAY_WEIGHTS: {
  240. ERR_CONTINUE( v.weights.size()!=4 );
  241. w[idx+j]=v.weights[j];
  242. } break;
  243. case Mesh::ARRAY_BONES: {
  244. ERR_CONTINUE( v.bones.size()!=4 );
  245. w[idx+j]=v.bones[j];
  246. } break;
  247. }
  248. }
  249. }
  250. w=DVector<float>::Write();
  251. a[i]=array;
  252. } break;
  253. case Mesh::ARRAY_FORMAT_INDEX: {
  254. ERR_CONTINUE( index_array.size() ==0 );
  255. DVector<int> array;
  256. array.resize(index_array.size());
  257. DVector<int>::Write w = array.write();
  258. int idx=0;
  259. for(List< int>::Element *E=index_array.front();E;E=E->next(),idx++) {
  260. w[idx]=E->get();
  261. }
  262. w=DVector<int>::Write();
  263. a[i]=array;
  264. } break;
  265. default: {}
  266. }
  267. }
  268. mesh->add_surface(primitive,a);
  269. if (material.is_valid())
  270. mesh->surface_set_material(surface,material);
  271. return mesh;
  272. }
  273. void SurfaceTool::index() {
  274. if (index_array.size())
  275. return; //already indexed
  276. HashMap<Vertex,int,VertexHasher> indices;
  277. List<Vertex> new_vertices;
  278. for(List< Vertex >::Element *E=vertex_array.front();E;E=E->next()) {
  279. int *idxptr=indices.getptr(E->get());
  280. int idx;
  281. if (!idxptr) {
  282. idx=indices.size();
  283. new_vertices.push_back(E->get());
  284. indices[E->get()]=idx;
  285. } else {
  286. idx=*idxptr;
  287. }
  288. index_array.push_back(idx);
  289. }
  290. vertex_array.clear();
  291. vertex_array=new_vertices;
  292. format|=Mesh::ARRAY_FORMAT_INDEX;
  293. }
  294. void SurfaceTool::deindex() {
  295. if (index_array.size()==0)
  296. return; //nothing to deindex
  297. Vector< Vertex > varr;
  298. varr.resize(vertex_array.size());
  299. int idx=0;
  300. for (List< Vertex >::Element *E=vertex_array.front();E;E=E->next()) {
  301. varr[idx++]=E->get();
  302. }
  303. vertex_array.clear();
  304. for (List<int>::Element *E=index_array.front();E;E=E->next()) {
  305. ERR_FAIL_INDEX(E->get(),varr.size());
  306. vertex_array.push_back(varr[E->get()]);
  307. }
  308. format&=~Mesh::ARRAY_FORMAT_INDEX;
  309. }
  310. void SurfaceTool::_create_list(const Ref<Mesh>& p_existing, int p_surface, List<Vertex> *r_vertex, List<int> *r_index, int& lformat) {
  311. Array arr = p_existing->surface_get_arrays(p_surface);
  312. ERR_FAIL_COND( arr.size() !=VS::ARRAY_MAX );
  313. DVector<Vector3> varr = arr[VS::ARRAY_VERTEX];
  314. DVector<Vector3> narr = arr[VS::ARRAY_NORMAL];
  315. DVector<float> tarr = arr[VS::ARRAY_TANGENT];
  316. DVector<Color> carr = arr[VS::ARRAY_COLOR];
  317. DVector<Vector2> uvarr = arr[VS::ARRAY_TEX_UV];
  318. DVector<Vector2> uv2arr = arr[VS::ARRAY_TEX_UV2];
  319. DVector<int> barr = arr[VS::ARRAY_BONES];
  320. DVector<float> warr = arr[VS::ARRAY_WEIGHTS];
  321. int vc = varr.size();
  322. if (vc==0)
  323. return;
  324. lformat=0;
  325. DVector<Vector3>::Read rv;
  326. if (varr.size()) {
  327. lformat|=VS::ARRAY_FORMAT_VERTEX;
  328. rv=varr.read();
  329. }
  330. DVector<Vector3>::Read rn;
  331. if (narr.size()) {
  332. lformat|=VS::ARRAY_FORMAT_NORMAL;
  333. rn=narr.read();
  334. }
  335. DVector<float>::Read rt;
  336. if (tarr.size()) {
  337. lformat|=VS::ARRAY_FORMAT_TANGENT;
  338. rt=tarr.read();
  339. }
  340. DVector<Color>::Read rc;
  341. if (carr.size()) {
  342. lformat|=VS::ARRAY_FORMAT_COLOR;
  343. rc=carr.read();
  344. }
  345. DVector<Vector2>::Read ruv;
  346. if (uvarr.size()) {
  347. lformat|=VS::ARRAY_FORMAT_TEX_UV;
  348. ruv=uvarr.read();
  349. }
  350. DVector<Vector2>::Read ruv2;
  351. if (uv2arr.size()) {
  352. lformat|=VS::ARRAY_FORMAT_TEX_UV2;
  353. ruv2=uv2arr.read();
  354. }
  355. DVector<int>::Read rb;
  356. if (barr.size()) {
  357. lformat|=VS::ARRAY_FORMAT_BONES;
  358. rb=barr.read();
  359. }
  360. DVector<float>::Read rw;
  361. if (warr.size()) {
  362. lformat|=VS::ARRAY_FORMAT_WEIGHTS;
  363. rw=warr.read();
  364. }
  365. for(int i=0;i<vc;i++) {
  366. Vertex v;
  367. if (lformat&VS::ARRAY_FORMAT_VERTEX)
  368. v.vertex=varr[i];
  369. if (lformat&VS::ARRAY_FORMAT_NORMAL)
  370. v.normal=narr[i];
  371. if (lformat&VS::ARRAY_FORMAT_TANGENT) {
  372. Plane p( tarr[i*4+0], tarr[i*4+1], tarr[i*4+2], tarr[i*4+3] );
  373. v.tangent=p.normal;
  374. v.binormal=p.normal.cross(last_normal).normalized() * p.d;
  375. }
  376. if (lformat&VS::ARRAY_FORMAT_COLOR)
  377. v.color=carr[i];
  378. if (lformat&VS::ARRAY_FORMAT_TEX_UV)
  379. v.uv=uvarr[i];
  380. if (lformat&VS::ARRAY_FORMAT_TEX_UV2)
  381. v.uv2=uv2arr[i];
  382. if (lformat&VS::ARRAY_FORMAT_BONES) {
  383. Vector<int> b;
  384. b.resize(4);
  385. b[0]=barr[i*4+0];
  386. b[1]=barr[i*4+1];
  387. b[2]=barr[i*4+2];
  388. b[3]=barr[i*4+3];
  389. v.bones=b;
  390. }
  391. if (lformat&VS::ARRAY_FORMAT_WEIGHTS) {
  392. Vector<float> w;
  393. w.resize(4);
  394. w[0]=warr[i*4+0];
  395. w[1]=warr[i*4+1];
  396. w[2]=warr[i*4+2];
  397. w[3]=warr[i*4+3];
  398. v.weights=w;
  399. }
  400. r_vertex->push_back(v);
  401. }
  402. //indices
  403. DVector<int> idx= arr[VS::ARRAY_INDEX];
  404. int is = idx.size();
  405. if (is) {
  406. lformat|=VS::ARRAY_FORMAT_INDEX;
  407. DVector<int>::Read iarr=idx.read();
  408. for(int i=0;i<is;i++) {
  409. r_index->push_back(iarr[i]);
  410. }
  411. }
  412. }
  413. void SurfaceTool::create_from(const Ref<Mesh>& p_existing, int p_surface) {
  414. clear();
  415. primitive=p_existing->surface_get_primitive_type(p_surface);
  416. _create_list(p_existing,p_surface,&vertex_array,&index_array,format);
  417. }
  418. void SurfaceTool::append_from(const Ref<Mesh>& p_existing, int p_surface,const Transform& p_xform) {
  419. if (vertex_array.size()==0) {
  420. primitive=p_existing->surface_get_primitive_type(p_surface);
  421. format=0;
  422. }
  423. int nformat;
  424. List<Vertex> nvertices;
  425. List<int> nindices;
  426. _create_list(p_existing,p_surface,&nvertices,&nindices,nformat);
  427. format|=nformat;
  428. int vfrom = vertex_array.size();
  429. for(List<Vertex>::Element *E=nvertices.front();E;E=E->next()) {
  430. Vertex v=E->get();
  431. v.vertex=p_xform.xform(v.vertex);
  432. if (nformat&VS::ARRAY_FORMAT_NORMAL) {
  433. v.normal=p_xform.basis.xform(v.normal);
  434. }
  435. if (nformat&VS::ARRAY_FORMAT_TANGENT) {
  436. v.tangent=p_xform.basis.xform(v.tangent);
  437. v.binormal=p_xform.basis.xform(v.binormal);
  438. }
  439. vertex_array.push_back(v);
  440. }
  441. for(List<int>::Element *E=nindices.front();E;E=E->next()) {
  442. int dst_index = E->get()+vfrom;
  443. //if (dst_index <0 || dst_index>=vertex_array.size()) {
  444. // print_line("invalid index!");
  445. //}
  446. index_array.push_back(dst_index);
  447. }
  448. if (index_array.size()%3)
  449. print_line("IA not div of 3?");
  450. }
  451. void SurfaceTool::generate_tangents() {
  452. ERR_FAIL_COND(!(format&Mesh::ARRAY_FORMAT_TEX_UV));
  453. ERR_FAIL_COND(!(format&Mesh::ARRAY_FORMAT_NORMAL));
  454. if (index_array.size()) {
  455. Vector<List<Vertex>::Element*> vtx;
  456. vtx.resize(vertex_array.size());
  457. int idx=0;
  458. for (List<Vertex>::Element *E=vertex_array.front();E;E=E->next()) {
  459. vtx[idx++]=E;
  460. E->get().binormal=Vector3();
  461. E->get().tangent=Vector3();
  462. }
  463. for (List<int>::Element *E=index_array.front();E;) {
  464. int i[3];
  465. i[0]=E->get();
  466. E=E->next();
  467. ERR_FAIL_COND(!E);
  468. i[1]=E->get();
  469. E=E->next();
  470. ERR_FAIL_COND(!E);
  471. i[2]=E->get();
  472. E=E->next();
  473. ERR_FAIL_COND(!E);
  474. Vector3 v1 = vtx[ i[0] ]->get().vertex;
  475. Vector3 v2 = vtx[ i[1] ]->get().vertex;
  476. Vector3 v3 = vtx[ i[2] ]->get().vertex;
  477. Vector2 w1 = vtx[ i[0] ]->get().uv;
  478. Vector2 w2 = vtx[ i[1] ]->get().uv;
  479. Vector2 w3 = vtx[ i[2] ]->get().uv;
  480. float x1 = v2.x - v1.x;
  481. float x2 = v3.x - v1.x;
  482. float y1 = v2.y - v1.y;
  483. float y2 = v3.y - v1.y;
  484. float z1 = v2.z - v1.z;
  485. float z2 = v3.z - v1.z;
  486. float s1 = w2.x - w1.x;
  487. float s2 = w3.x - w1.x;
  488. float t1 = w2.y - w1.y;
  489. float t2 = w3.y - w1.y;
  490. float r = (s1 * t2 - s2 * t1);
  491. Vector3 binormal,tangent;
  492. if (r==0) {
  493. binormal=Vector3(0,0,0);
  494. tangent=Vector3(0,0,0);
  495. } else {
  496. tangent = Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r,
  497. (t2 * z1 - t1 * z2) * r);
  498. binormal = Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r,
  499. (s1 * z2 - s2 * z1) * r);
  500. }
  501. tangent.normalize();
  502. binormal.normalize();
  503. Vector3 normal=Plane( v1, v2, v3 ).normal;
  504. Vector3 tangentp = tangent - normal * normal.dot( tangent );
  505. Vector3 binormalp = binormal - normal * (normal.dot(binormal)) - tangent * (tangent.dot(binormal));
  506. tangentp.normalize();
  507. binormalp.normalize();
  508. for (int j=0;j<3;j++) {
  509. vtx[ i[j] ]->get().binormal+=binormalp;
  510. vtx[ i[j] ]->get().tangent+=tangentp;
  511. }
  512. }
  513. for (List<Vertex>::Element *E=vertex_array.front();E;E=E->next()) {
  514. E->get().binormal.normalize();
  515. E->get().tangent.normalize();
  516. }
  517. } else {
  518. for (List<Vertex>::Element *E=vertex_array.front();E;) {
  519. List< Vertex >::Element *v[3];
  520. v[0]=E;
  521. v[1]=v[0]->next();
  522. ERR_FAIL_COND(!v[1]);
  523. v[2]=v[1]->next();
  524. ERR_FAIL_COND(!v[2]);
  525. E=v[2]->next();
  526. Vector3 v1 = v[0]->get().vertex;
  527. Vector3 v2 = v[1]->get().vertex;
  528. Vector3 v3 = v[2]->get().vertex;
  529. Vector2 w1 = v[0]->get().uv;
  530. Vector2 w2 = v[1]->get().uv;
  531. Vector2 w3 = v[2]->get().uv;
  532. float x1 = v2.x - v1.x;
  533. float x2 = v3.x - v1.x;
  534. float y1 = v2.y - v1.y;
  535. float y2 = v3.y - v1.y;
  536. float z1 = v2.z - v1.z;
  537. float z2 = v3.z - v1.z;
  538. float s1 = w2.x - w1.x;
  539. float s2 = w3.x - w1.x;
  540. float t1 = w2.y - w1.y;
  541. float t2 = w3.y - w1.y;
  542. float r = (s1 * t2 - s2 * t1);
  543. Vector3 binormal,tangent;
  544. if (r==0) {
  545. binormal=Vector3(0,0,0);
  546. tangent=Vector3(0,0,0);
  547. } else {
  548. tangent = Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r,
  549. (t2 * z1 - t1 * z2) * r);
  550. binormal = Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r,
  551. (s1 * z2 - s2 * z1) * r);
  552. }
  553. tangent.normalize();
  554. binormal.normalize();
  555. Vector3 normal=Plane( v1, v2, v3 ).normal;
  556. Vector3 tangentp = tangent - normal * normal.dot( tangent );
  557. Vector3 binormalp = binormal - normal * (normal.dot(binormal)) - tangent * (tangent.dot(binormal));
  558. tangentp.normalize();
  559. binormalp.normalize();
  560. for (int j=0;j<3;j++) {
  561. v[j]->get().binormal=binormalp;
  562. v[j]->get().tangent=tangentp;
  563. }
  564. }
  565. }
  566. format|=Mesh::ARRAY_FORMAT_TANGENT;
  567. }
  568. void SurfaceTool::generate_normals() {
  569. ERR_FAIL_COND(primitive!=Mesh::PRIMITIVE_TRIANGLES);
  570. bool was_indexed=index_array.size();
  571. deindex();
  572. HashMap<Vertex,Vector3,VertexHasher> vertex_hash;
  573. int count=0;
  574. bool smooth=false;
  575. if (smooth_groups.has(0))
  576. smooth=smooth_groups[0];
  577. print_line("SMOOTH BEGIN? "+itos(smooth));
  578. List< Vertex >::Element *B=vertex_array.front();
  579. for(List< Vertex >::Element *E=B;E;) {
  580. List< Vertex >::Element *v[3];
  581. v[0]=E;
  582. v[1]=v[0]->next();
  583. ERR_FAIL_COND(!v[1]);
  584. v[2]=v[1]->next();
  585. ERR_FAIL_COND(!v[2]);
  586. E=v[2]->next();
  587. Vector3 normal = Plane(v[0]->get().vertex,v[1]->get().vertex,v[2]->get().vertex).normal;
  588. if (smooth) {
  589. for(int i=0;i<3;i++) {
  590. Vector3 *lv=vertex_hash.getptr(v[i]->get());
  591. if (!lv) {
  592. vertex_hash.set(v[i]->get(),normal);
  593. } else {
  594. (*lv)+=normal;
  595. }
  596. }
  597. } else {
  598. for(int i=0;i<3;i++) {
  599. v[i]->get().normal=normal;
  600. }
  601. }
  602. count+=3;
  603. if (smooth_groups.has(count) || !E) {
  604. if (vertex_hash.size()) {
  605. while (B!=E) {
  606. Vector3* lv=vertex_hash.getptr(B->get());
  607. if (lv) {
  608. B->get().normal=lv->normalized();
  609. }
  610. B=B->next();
  611. }
  612. } else {
  613. B=E;
  614. }
  615. vertex_hash.clear();
  616. if (E) {
  617. smooth=smooth_groups[count];
  618. print_line("SMOOTH AT "+itos(count)+": "+itos(smooth));
  619. }
  620. }
  621. }
  622. format|=Mesh::ARRAY_FORMAT_NORMAL;
  623. if (was_indexed) {
  624. index();
  625. smooth_groups.clear();
  626. }
  627. }
  628. void SurfaceTool::set_material(const Ref<Material>& p_material) {
  629. material=p_material;
  630. }
  631. void SurfaceTool::clear() {
  632. begun=false;
  633. primitive=Mesh::PRIMITIVE_LINES;
  634. format=0;
  635. last_bones.clear();;
  636. last_weights.clear();
  637. index_array.clear();
  638. vertex_array.clear();
  639. smooth_groups.clear();
  640. }
  641. void SurfaceTool::_bind_methods() {
  642. ObjectTypeDB::bind_method(_MD("begin","primitive"),&SurfaceTool::begin);
  643. ObjectTypeDB::bind_method(_MD("add_vertex","vertex"),&SurfaceTool::add_vertex);
  644. ObjectTypeDB::bind_method(_MD("add_color","color"),&SurfaceTool::add_color);
  645. ObjectTypeDB::bind_method(_MD("add_normal","normal"),&SurfaceTool::add_normal);
  646. ObjectTypeDB::bind_method(_MD("add_tangent","tangent"),&SurfaceTool::add_tangent);
  647. ObjectTypeDB::bind_method(_MD("add_uv","uv"),&SurfaceTool::add_uv);
  648. ObjectTypeDB::bind_method(_MD("add_uv2","uv2"),&SurfaceTool::add_uv2);
  649. ObjectTypeDB::bind_method(_MD("add_bones","bones"),&SurfaceTool::add_bones);
  650. ObjectTypeDB::bind_method(_MD("add_weights","weights"),&SurfaceTool::add_weights);
  651. ObjectTypeDB::bind_method(_MD("add_smooth_group","smooth"),&SurfaceTool::add_smooth_group);
  652. ObjectTypeDB::bind_method(_MD("set_material","material:Material"),&SurfaceTool::set_material);
  653. ObjectTypeDB::bind_method(_MD("index"),&SurfaceTool::index);
  654. ObjectTypeDB::bind_method(_MD("deindex"),&SurfaceTool::deindex);
  655. ///ObjectTypeDB::bind_method(_MD("generate_flat_normals"),&SurfaceTool::generate_flat_normals);
  656. ObjectTypeDB::bind_method(_MD("generate_normals"),&SurfaceTool::generate_normals);
  657. ObjectTypeDB::bind_method(_MD("commit:Mesh","existing:Mesh"),&SurfaceTool::commit,DEFVAL( RefPtr() ));
  658. ObjectTypeDB::bind_method(_MD("clear"),&SurfaceTool::clear);
  659. }
  660. SurfaceTool::SurfaceTool() {
  661. first=false;
  662. begun=false;
  663. primitive=Mesh::PRIMITIVE_LINES;
  664. format=0;
  665. }