Mesh Part.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. /******************************************************************************/
  5. #define LEAF_RANDOM_BEND_RANGE 1024
  6. /******************************************************************************/
  7. static void SetLeafAttachment(MeshBase &mesh, C Vec2 &tex, Memc<Int> &face)
  8. {
  9. // find face which has tex coords nearest 'tex'
  10. Flt dist;
  11. Int found=-1;
  12. REPA(face)
  13. {
  14. Int f=face[i];
  15. if( f&SIGN_BIT)
  16. {
  17. VecI4 ind =mesh.quad.ind(f^SIGN_BIT);
  18. Quad2 quad(mesh.vtx.tex0(ind.x), mesh.vtx.tex0(ind.y), mesh.vtx.tex0(ind.z), mesh.vtx.tex0(ind.w));
  19. Flt d =Dist(tex, quad);
  20. if(found==-1 || d<dist){found=f; dist=d;} // compare to -1 and not <0 because 'found' can have SIGN_BIT
  21. }else
  22. {
  23. VecI ind=mesh.tri.ind(f);
  24. Tri2 tri(mesh.vtx.tex0(ind.x), mesh.vtx.tex0(ind.y), mesh.vtx.tex0(ind.z));
  25. Flt d =Dist(tex, tri);
  26. if(found==-1 || d<dist){found=f; dist=d;} // compare to -1 and not <0 because 'found' can have SIGN_BIT
  27. }
  28. }
  29. // calculate UV coordinates of face
  30. Tri2 tex_tri;
  31. Tri pos_tri;
  32. if(found&SIGN_BIT)
  33. {
  34. VecI4 ind=mesh.quad.ind(found^SIGN_BIT);
  35. tex_tri.set(mesh.vtx.tex0(ind.x), mesh.vtx.tex0(ind.y), mesh.vtx.tex0(ind.w));
  36. pos_tri.set(mesh.vtx.pos (ind.x), mesh.vtx.pos (ind.y), mesh.vtx.pos (ind.w));
  37. }else
  38. {
  39. VecI ind=mesh.tri.ind(found);
  40. tex_tri.set(mesh.vtx.tex0(ind.x), mesh.vtx.tex0(ind.y), mesh.vtx.tex0(ind.z));
  41. pos_tri.set(mesh.vtx.pos (ind.x), mesh.vtx.pos (ind.y), mesh.vtx.pos (ind.z));
  42. }
  43. Vec2 base =tex_tri.p[0] ,
  44. u_dir=tex_tri.p[1]-base,
  45. v_dir=tex_tri.p[2]-base;
  46. /* tex=base + u*u_dir + v*v_dir
  47. tex.x = base.x + u*u_dir.x + v*v_dir.x
  48. tex.y = base.y + u*u_dir.y + v*v_dir.y
  49. u_dir.x*u + v_dir.x*v = tex.x - base.x
  50. u_dir.y*u + v_dir.y*v = tex.y - base.y
  51. */
  52. Vec pos;
  53. Flt u, v;
  54. if(Solve(u_dir.x, u_dir.y, v_dir.x, v_dir.y, tex.x-base.x, tex.y-base.y, u, v)!=1)pos=pos_tri.center();
  55. else pos=pos_tri.p[0] + u*(pos_tri.p[1]-pos_tri.p[0]) + v*(pos_tri.p[2]-pos_tri.p[0]);
  56. REPA(face)
  57. {
  58. Int f=face[i];
  59. if( f&SIGN_BIT){VecI4 ind=mesh.quad.ind(f^SIGN_BIT); REPA(ind)mesh.vtx.hlp(ind.c[i])=pos;}
  60. else {VecI ind=mesh.tri .ind(f ); REPA(ind)mesh.vtx.hlp(ind.c[i])=pos;}
  61. }
  62. }
  63. static void SetLeafAttachment(MeshBase &mesh, C Vec2 &tex)
  64. {
  65. if(mesh.vtx.tex0())
  66. {
  67. mesh.setVtxDup().setAdjacencies(true, false).include(VTX_HLP);
  68. Memc<Int> face;
  69. Byte * tri_done=AllocZero<Byte>(mesh. tris()),
  70. *quad_done=AllocZero<Byte>(mesh.quads());
  71. REPA(mesh.tri)if(!tri_done[i])
  72. {
  73. face.add(i); tri_done[i]=true;
  74. for(Int last=0; last!=face.elms(); )
  75. {
  76. Int new_last=face.elms();
  77. for(Int i=face.elms(); --i>=last; ) // search recently added, order is important because face will receive new elements continuously
  78. {
  79. VecI4 a;
  80. Int f=face[i];
  81. if( f&SIGN_BIT)a= mesh.quad.adjFace(f^SIGN_BIT) ; // get all adjacent faces
  82. else a.set(mesh. tri.adjFace(f ), -1); // get all adjacent faces
  83. REPA(a) // iterate through all adjacent faces
  84. {
  85. Int af =a.c[i];
  86. if( af!=-1) // if adjacent face exists, compare to -1 and not >=0 because it can have SIGN_BIT
  87. {
  88. if(af&SIGN_BIT){if(!quad_done[af^SIGN_BIT]){quad_done[af^SIGN_BIT]=true; face.add(af);}}
  89. else {if(! tri_done[af ]){ tri_done[af ]=true; face.add(af);}}
  90. }
  91. }
  92. }
  93. last=new_last;
  94. }
  95. SetLeafAttachment(mesh, tex, face);
  96. face.clear();
  97. }
  98. REPA(mesh.quad)if(!quad_done[i])
  99. {
  100. face.add(i^SIGN_BIT); quad_done[i]=true;
  101. for(Int last=0; last!=face.elms(); )
  102. {
  103. Int new_last=face.elms();
  104. for(Int i=face.elms(); --i>=last; ) // search recently added, order is important because face will receive new elements continuously
  105. {
  106. VecI4 a;
  107. Int f=face[i];
  108. if( f&SIGN_BIT)a= mesh.quad.adjFace(f^SIGN_BIT) ; // get all adjacent faces
  109. else a.set(mesh. tri.adjFace(f ), -1); // get all adjacent faces
  110. REPA(a) // iterate through all adjacent faces
  111. {
  112. Int af =a.c[i];
  113. if( af!=-1) // if adjacent face exists, compare to -1 and not >=0 because it can have SIGN_BIT
  114. {
  115. if(af&SIGN_BIT){if(!quad_done[af^SIGN_BIT]){quad_done[af^SIGN_BIT]=true; face.add(af);}}
  116. else {if(! tri_done[af ]){ tri_done[af ]=true; face.add(af);}}
  117. }
  118. }
  119. }
  120. last=new_last;
  121. }
  122. SetLeafAttachment(mesh, tex, face);
  123. face.clear();
  124. }
  125. Free( tri_done);
  126. Free(quad_done);
  127. }
  128. }
  129. /******************************************************************************/
  130. static void SetRandomColor(MeshBase &mesh, Memc<Int> &face, C Color &color)
  131. {
  132. REPA(face)
  133. {
  134. Int f=face[i];
  135. if( f&SIGN_BIT){VecI4 q=mesh.quad.ind(f^SIGN_BIT); REPA(q)mesh.vtx.color(q.c[i])=color;}
  136. else {VecI t=mesh.tri .ind(f ); REPA(t)mesh.vtx.color(t.c[i])=color;}
  137. }
  138. }
  139. static void SetRandomColor(MeshBase &mesh, Flt variation)
  140. {
  141. const Byte random=Mid(255-RoundPos(255*variation), 0, 255);
  142. mesh.setVtxDup().setAdjacencies(true, false).include(VTX_COLOR);
  143. Memc<Int> face;
  144. Mems<Bool> tri_done; tri_done.setNumZero(mesh. tris());
  145. Mems<Bool> quad_done; quad_done.setNumZero(mesh.quads());
  146. REPA(mesh.tri)if(!tri_done[i])
  147. {
  148. face.add(i); tri_done[i]=true;
  149. for(Int last=0; last!=face.elms(); )
  150. {
  151. Int new_last=face.elms();
  152. for(Int i=face.elms(); --i>=last; ) // search recently added, order is important because face will receive new elements continuously
  153. {
  154. VecI4 a;
  155. Int f=face[i];
  156. if( f&SIGN_BIT)a= mesh.quad.adjFace(f^SIGN_BIT) ; // get all adjacent faces
  157. else a.set(mesh. tri.adjFace(f ), -1); // get all adjacent faces
  158. REPA(a) // iterate through all adjacent faces
  159. {
  160. Int af =a.c[i];
  161. if( af!=-1) // if adjacent face exists, compare to -1 and not >=0 because it can have SIGN_BIT
  162. {
  163. if(af&SIGN_BIT){if(!quad_done[af^SIGN_BIT]){quad_done[af^SIGN_BIT]=true; face.add(af);}}
  164. else {if(! tri_done[af ]){ tri_done[af ]=true; face.add(af);}}
  165. }
  166. }
  167. }
  168. last=new_last;
  169. }
  170. SetRandomColor(mesh, face, Color(Random(random, 255), Random(random, 255), Random(random, 255)));
  171. face.clear();
  172. }
  173. REPA(mesh.quad)if(!quad_done[i])
  174. {
  175. face.add(i^SIGN_BIT); quad_done[i]=true;
  176. for(Int last=0; last!=face.elms(); )
  177. {
  178. Int new_last=face.elms();
  179. for(Int i=face.elms(); --i>=last; ) // search recently added, order is important because face will receive new elements continuously
  180. {
  181. VecI4 a;
  182. Int f=face[i];
  183. if( f&SIGN_BIT)a= mesh.quad.adjFace(f^SIGN_BIT) ; // get all adjacent faces
  184. else a.set(mesh. tri.adjFace(f ), -1); // get all adjacent faces
  185. REPA(a) // iterate through all adjacent faces
  186. {
  187. Int af =a.c[i];
  188. if( af!=-1) // if adjacent face exists, compare to -1 and not >=0 because it can have SIGN_BIT
  189. {
  190. if(af&SIGN_BIT){if(!quad_done[af^SIGN_BIT]){quad_done[af^SIGN_BIT]=true; face.add(af);}}
  191. else {if(! tri_done[af ]){ tri_done[af ]=true; face.add(af);}}
  192. }
  193. }
  194. }
  195. last=new_last;
  196. }
  197. SetRandomColor(mesh, face, Color(Random(random, 255), Random(random, 255), Random(random, 255)));
  198. face.clear();
  199. }
  200. }
  201. /******************************************************************************/
  202. static void SetRandomBend(MeshBase &mesh, Memc<Int> &face, Flt value)
  203. {
  204. REPA(face)
  205. {
  206. Int f=face[i];
  207. if( f&SIGN_BIT){VecI4 q=mesh.quad.ind(f^SIGN_BIT); REPA(q)mesh.vtx.size(q.c[i])=value;}
  208. else {VecI t=mesh.tri .ind(f ); REPA(t)mesh.vtx.size(t.c[i])=value;}
  209. }
  210. }
  211. static void SetRandomBend(MeshBase &mesh)
  212. {
  213. mesh.setVtxDup().setAdjacencies(true, false).include(VTX_SIZE);
  214. Memc<Int> face;
  215. Mems<Bool> tri_done; tri_done.setNumZero(mesh. tris());
  216. Mems<Bool> quad_done; quad_done.setNumZero(mesh.quads());
  217. REPA(mesh.tri)if(!tri_done[i])
  218. {
  219. face.add(i); tri_done[i]=true;
  220. for(Int last=0; last!=face.elms(); )
  221. {
  222. Int new_last=face.elms();
  223. for(Int i=face.elms(); --i>=last; ) // search recently added, order is important because face will receive new elements continuously
  224. {
  225. VecI4 a;
  226. Int f=face[i];
  227. if( f&SIGN_BIT)a= mesh.quad.adjFace(f^SIGN_BIT) ; // get all adjacent faces
  228. else a.set(mesh. tri.adjFace(f ), -1); // get all adjacent faces
  229. REPA(a) // iterate through all adjacent faces
  230. {
  231. Int af =a.c[i];
  232. if( af!=-1) // if adjacent face exists, compare to -1 and not >=0 because it can have SIGN_BIT
  233. {
  234. if(af&SIGN_BIT){if(!quad_done[af^SIGN_BIT]){quad_done[af^SIGN_BIT]=true; face.add(af);}}
  235. else {if(! tri_done[af ]){ tri_done[af ]=true; face.add(af);}}
  236. }
  237. }
  238. }
  239. last=new_last;
  240. }
  241. SetRandomBend(mesh, face, Random.f(LEAF_RANDOM_BEND_RANGE));
  242. face.clear();
  243. }
  244. REPA(mesh.quad)if(!quad_done[i])
  245. {
  246. face.add(i^SIGN_BIT); quad_done[i]=true;
  247. for(Int last=0; last!=face.elms(); )
  248. {
  249. Int new_last=face.elms();
  250. for(Int i=face.elms(); --i>=last; ) // search recently added, order is important because face will receive new elements continuously
  251. {
  252. VecI4 a;
  253. Int f=face[i];
  254. if( f&SIGN_BIT)a= mesh.quad.adjFace(f^SIGN_BIT) ; // get all adjacent faces
  255. else a.set(mesh. tri.adjFace(f ), -1); // get all adjacent faces
  256. REPA(a) // iterate through all adjacent faces
  257. {
  258. Int af =a.c[i];
  259. if( af!=-1) // if adjacent face exists, compare to -1 and not >=0 because it can have SIGN_BIT
  260. {
  261. if(af&SIGN_BIT){if(!quad_done[af^SIGN_BIT]){quad_done[af^SIGN_BIT]=true; face.add(af);}}
  262. else {if(! tri_done[af ]){ tri_done[af ]=true; face.add(af);}}
  263. }
  264. }
  265. }
  266. last=new_last;
  267. }
  268. SetRandomBend(mesh, face, Random.f(LEAF_RANDOM_BEND_RANGE));
  269. face.clear();
  270. }
  271. }
  272. /******************************************************************************/
  273. // MANAGE
  274. /******************************************************************************/
  275. MeshPart::Variation::Variation() {zero();}
  276. void MeshPart::Variation::zero () {last_solid_instance=last_shadow_instance=-1; REPAO(shader)=null; frst=null; blst=null;}
  277. void MeshPart::Variation::del () {material.clear(); zero();}
  278. Bool MeshPart::Variation::save (File &f, CChar *path)C {f.putAsset(material.id()); return f.ok();}
  279. Bool MeshPart::Variation::load (File &f, CChar *path) {material.require(f.getAssetID(), path); return f.ok();}
  280. /******************************************************************************/
  281. MeshPart::MeshPart() {zero();}
  282. void MeshPart::zero ()
  283. {
  284. name[0]=0;
  285. part_flag=0;
  286. _vtx_heightmap=0;
  287. _draw_mask=IndexToFlag(0);
  288. _draw_mask_enum_id=0;
  289. _umm=null;
  290. }
  291. MeshPart& MeshPart::del()
  292. {
  293. base .del ();
  294. render .del ();
  295. _variation .del ();
  296. _variations.del ();
  297. REPAO(_materials).clear();
  298. zero(); return T;
  299. }
  300. MeshPart& MeshPart::create(C MeshPart &src, UInt flag_and)
  301. {
  302. if(this==&src)base.keepOnly(flag_and);else
  303. {
  304. del();
  305. copyParams(src, true);
  306. base.create(src.base, flag_and);
  307. // if creating from 'render' fails (for example we're on OpenGL ES and we can't read from GPU data) then try creating from MeshBase using 'setRender'
  308. flag_and|=VTX_POS;
  309. UInt src_render_flag=(src.render.flag()&VTX_MSHR),
  310. new_render_flag=(src_render_flag &flag_and);
  311. if( src_render_flag==new_render_flag)
  312. {
  313. if(!render.create(src.render))setRender();
  314. }else
  315. {
  316. C MeshRender *src_render[]={&src.render};
  317. if(render.create(src_render, Elms(src_render), flag_and))setShader(0);else setRender();
  318. }
  319. }
  320. return T;
  321. }
  322. void MeshPart::copyParams(C MeshPart &src, Bool copy_shaders)
  323. {
  324. if(this!=&src)
  325. {
  326. Set( name ,src. name);
  327. _draw_mask =src._draw_mask;
  328. _draw_mask_enum_id=src._draw_mask_enum_id;
  329. part_flag =src. part_flag;
  330. _vtx_heightmap =src._vtx_heightmap;
  331. _umm =src._umm;
  332. REPAO(_materials)=src._materials[i];
  333. _variation =src._variation ; _variation .unlink();
  334. _variations =src._variations; REPAO(_variations).unlink();
  335. if(!copy_shaders) // clear shaders
  336. {
  337. _variation .zero();
  338. REPAO(_variations).zero();
  339. }
  340. }
  341. }
  342. void MeshPart::scaleParams(Flt scale)
  343. {
  344. if(scale)_vtx_heightmap/=scale;
  345. }
  346. MeshPart& MeshPart::include (UInt flag) { Bool base_is=base.is(); if(!base_is && render.is())base.create(render); base.include(flag); return T;} // don't create 'render' from uninitialized data
  347. MeshPart& MeshPart::exclude (UInt flag) {if((base.flag()|render.flag())&flag){Bool base_is=base.is(); if(!base_is && render.is())base.create(render); base.exclude(flag); if(render.is())setRender(); if(!base_is)delBase();} return T;}
  348. MeshPart& MeshPart::keepOnly(UInt flag) {return exclude(~flag);}
  349. /******************************************************************************/
  350. // GET
  351. /******************************************************************************/
  352. UInt MeshPart::flag ( )C {return base.is() ? base.flag () : render.flag();}
  353. UInt MeshPart::memUsage ( )C {return base.memUsage() + render.memUsage();}
  354. Int MeshPart::vtxs ( )C {return base.is() ? base.vtxs () : render.vtxs();}
  355. Int MeshPart::edges ( )C {return base.edges () ;}
  356. Int MeshPart::tris ( )C {return base.is() ? base.tris () : render.tris();}
  357. Int MeshPart::quads ( )C {return base.quads () ;}
  358. Int MeshPart::faces ( )C {return base.is() ? base.faces () : render.tris();}
  359. Int MeshPart::trisTotal( )C {return base.is() ? base.trisTotal() : render.tris();}
  360. Bool MeshPart::getBox (Box &box )C {return base.getBox(box) || render.getBox(box);}
  361. Flt MeshPart::area (Vec *center)C {return base.is() ? base.area(center) : render.area(center);}
  362. Int MeshPart::drawGroup( )C {return _draw_mask ? BitHi(_draw_mask) : -1;}
  363. C MaterialPtr& MeshPart::multiMaterial(Int i)C
  364. {
  365. if(!i)return _variation.material;
  366. if(InRange(--i, _materials))return _materials[i];
  367. return MaterialNull;
  368. }
  369. Bool MeshPart::sameMaterials(C MeshPart &part)C
  370. {
  371. if( material ( )==part. material ( )
  372. && _materials[0]==part._materials[0]
  373. && _materials[1]==part._materials[1]
  374. && _materials[2]==part._materials[2])
  375. {
  376. Int shared_vars=Min(_variations.elms(), part._variations.elms());
  377. REP(shared_vars) if( _variations[i].material!=part._variations[i].material )return false; // check variations which both parts have
  378. for(Int i=shared_vars; i< _variations.elms(); i++)if( _variations[i].material!= material())return false; // check extra variations in this part, if it's not default, then fail
  379. for(Int i=shared_vars; i<part._variations.elms(); i++)if(part._variations[i].material!= material())return false; // check extra variations in other part, if it's not default, then fail
  380. return true;
  381. }
  382. return false;
  383. }
  384. /******************************************************************************/
  385. // SET
  386. /******************************************************************************/
  387. MeshPart& MeshPart::remapMaterials(Byte new_index[4])
  388. {
  389. if(new_index[0]!=0
  390. || new_index[1]!=1
  391. || new_index[2]!=2
  392. || new_index[3]!=3)
  393. {
  394. // materials
  395. MaterialPtr temp[4]={multiMaterial(0), multiMaterial(1), multiMaterial(2), multiMaterial(3)};
  396. _variation.material =temp[new_index[0]];
  397. _materials[0]=temp[new_index[1]];
  398. _materials[1]=temp[new_index[2]];
  399. _materials[2]=temp[new_index[3]];
  400. // base
  401. if(base.vtx.material())REPA(base.vtx)
  402. {
  403. VecB4 &material=base.vtx.material(i), temp=material;
  404. material.c[0]=temp.c[new_index[0]];
  405. material.c[1]=temp.c[new_index[1]];
  406. material.c[2]=temp.c[new_index[2]];
  407. material.c[3]=temp.c[new_index[3]];
  408. }
  409. // render
  410. Int material_ofs =render.vtxOfs(VTX_MATERIAL);
  411. if( material_ofs>=0)if(Byte *vtx=render.vtxLock())
  412. {
  413. vtx+=material_ofs;
  414. REP(render.vtxs())
  415. {
  416. VecB4 &material=*(VecB4*)vtx, temp=material;
  417. material.c[0]=temp.c[new_index[0]];
  418. material.c[1]=temp.c[new_index[1]];
  419. material.c[2]=temp.c[new_index[2]];
  420. material.c[3]=temp.c[new_index[3]];
  421. vtx+=render.vtxSize();
  422. }
  423. render.vtxUnlock();
  424. }
  425. setUMM();
  426. }
  427. return T;
  428. }
  429. MeshPart& MeshPart::setUMM()
  430. {
  431. _umm=(multiMaterial(1) ? UniqueMultiMaterialMap(UniqueMultiMaterialKey(multiMaterial(0)(), multiMaterial(1)(), multiMaterial(2)(), multiMaterial(3)())) : null);
  432. return T;
  433. }
  434. MeshPart& MeshPart::setRenderEx(Bool optimize, Bool compress, Int lod_index)
  435. {
  436. render.create(base, ~0, optimize, compress);
  437. setShader(lod_index);
  438. return T;
  439. }
  440. MeshPart& MeshPart::delBase ( ) {base .del(); return T;}
  441. MeshPart& MeshPart::delRender( ) {render.del(); return T;}
  442. MeshPart& MeshPart::setBase (Bool only_if_empty ) {if(only_if_empty ? !base.is() : true)base.create(render); return T;}
  443. MeshPart& MeshPart::setRender(Bool optimize, Int lod_index) {return setRenderEx(optimize, true, lod_index);}
  444. void MeshPart::setShaderMulti(Int lod_index)
  445. {
  446. C Material *m[]=
  447. {
  448. multiMaterial(0)(),
  449. multiMaterial(1)(),
  450. multiMaterial(2)(),
  451. multiMaterial(3)(),
  452. };
  453. DefaultShaders(m, render.flag(), lod_index, heightmap()).set(_variation.shader, &_variation.frst, &_variation.blst);
  454. }
  455. void MeshPart::setShader(Int lod_index, Variation &variation)
  456. {
  457. DefaultShaders(variation.material(), render.flag(), lod_index, heightmap()).set(variation.shader, &variation.frst, &variation.blst);
  458. }
  459. MeshPart& MeshPart::setShader(Int lod_index)
  460. {
  461. if(!D._set_shader_material
  462. || multiMaterial(0)==D._set_shader_material || multiMaterial(1)==D._set_shader_material
  463. || multiMaterial(2)==D._set_shader_material || multiMaterial(3)==D._set_shader_material) // update only for changed material
  464. setShaderMulti(lod_index);
  465. REPA(_variations)
  466. {
  467. Variation &variation=_variations[i];
  468. if(!D._set_shader_material || variation.material==D._set_shader_material)setShader(lod_index, variation);
  469. }
  470. return T;
  471. }
  472. MeshPart& MeshPart::setShader(RENDER_MODE mode, Shader *shader, Int variation)
  473. {
  474. if(InRange(mode, MEMBER_ELMS(Variation, shader)))
  475. {
  476. Variation *var;
  477. if( !variation )var=&_variation ;else
  478. if(InRange(--variation, _variations))var=&_variations[variation];else goto error;
  479. var->shader[mode]=shader;
  480. }
  481. error:
  482. return T;
  483. }
  484. MeshPart& MeshPart::material(C MaterialPtr &material, Int lod_index)
  485. {
  486. if(_variation.material !=material
  487. || _materials[0]!=null
  488. || _materials[1]!=null
  489. || _materials[2]!=null)
  490. {
  491. #if SUPPORT_MATERIAL_CHANGE_IN_RENDERING
  492. _variation.unlink();
  493. #else
  494. if(_variation.drawn())Exit("Changing Mesh Material after it was already requested to be drawn with a different Material is not supported");
  495. #endif
  496. _variation.material =material;
  497. _materials[0]=null;
  498. _materials[1]=null;
  499. _materials[2]=null;
  500. setUMM();
  501. if(lod_index>=0)setShaderMulti(lod_index); // set shader only if valid lod specified (negative can be used for special case when we don't want to set shaders yet, for example because they may require locking D._lock)
  502. }
  503. return T;
  504. }
  505. MeshPart& MeshPart::multiMaterial(C MaterialPtr &m0, C MaterialPtr &m1, C MaterialPtr &m2, C MaterialPtr &m3, Int lod_index)
  506. {
  507. if(_variation.material !=m0
  508. || _materials[0]!=m1
  509. || _materials[1]!=m2
  510. || _materials[2]!=m3)
  511. {
  512. #if SUPPORT_MATERIAL_CHANGE_IN_RENDERING
  513. _variation.unlink();
  514. #else
  515. if(_variation.drawn())Exit("Changing Mesh Material after it was already requested to be drawn with a different Material is not supported");
  516. #endif
  517. _variation.material =m0;
  518. _materials[0]=m1;
  519. _materials[1]=m2;
  520. _materials[2]=m3;
  521. setUMM();
  522. if(lod_index>=0)setShaderMulti(lod_index); // set shader only if valid lod specified (negative can be used for special case when we don't want to set shaders yet, for example because they may require locking D._lock)
  523. }
  524. return T;
  525. }
  526. MeshPart& MeshPart::setAutoTanBin()
  527. {
  528. if(!heightmap()) // heightmaps generate tan/bin from normal in the shader
  529. REP(variations())
  530. if(C MaterialPtr &material=variation(i)) // if any of the variation materials need tan/bin
  531. if(material->wantTanBin())
  532. {
  533. Bool base_is=base.is();
  534. if( !base_is)base.create(render);
  535. base.setAutoTanBin();
  536. if(render.is())setRender();
  537. if( !base_is )delBase();
  538. return T;
  539. }
  540. exclude(VTX_TAN_BIN); // not wanted
  541. return T;
  542. }
  543. MeshPart& MeshPart::drawGroup(Int group, Enum *draw_group_enum)
  544. {
  545. if(InRange(group, 32))
  546. {
  547. _draw_mask =IndexToFlag(group);
  548. _draw_mask_enum_id=(draw_group_enum ? draw_group_enum->elmIDUInt(group) : 0);
  549. }else // invalid group, force zero mask, never draw
  550. {
  551. _draw_mask=0;
  552. _draw_mask_enum_id=0;
  553. }
  554. return T;
  555. }
  556. /******************************************************************************/
  557. // VARIATIONS
  558. /******************************************************************************/
  559. Int MeshPart::variations( )C {return _variations.elms()+1;}
  560. MeshPart& MeshPart::variations(Int variations)
  561. {
  562. variations=Max(0, variations-1); // 1 variation is stored in MeshPart, others are stored in '_variations'
  563. Int old=_variations.elms(); _variations.setNum(variations);
  564. for(Int i=old; i<_variations.elms(); i++)variation(i+1, material()); // set default material for newly added variations
  565. return T;
  566. }
  567. MeshPart& MeshPart::variation(Int variation, C MaterialPtr &material, Int lod_index)
  568. {
  569. if(!variation)T.material(material, lod_index);else // 0-th is stored in MeshPart
  570. if(InRange(--variation, _variations))
  571. {
  572. Variation &var=_variations[variation]; if(var.material!=material)
  573. {
  574. #if SUPPORT_MATERIAL_CHANGE_IN_RENDERING
  575. var.unlink();
  576. #else
  577. if(var.drawn())Exit("Changing Mesh Material after it was already requested to be drawn with a different Material is not supported");
  578. #endif
  579. var.material=material;
  580. if(lod_index>=0)setShader(lod_index, var); // set shader only if valid lod specified (negative can be used for special case when we don't want to set shaders yet, for example because they may require locking D._lock)
  581. }
  582. }
  583. return T;
  584. }
  585. C MaterialPtr& MeshPart::variationNull(Int variation)C
  586. {
  587. if(!variation)return material();
  588. if(InRange(--variation, _variations))return _variations[variation].material;
  589. return MaterialNull;
  590. }
  591. C MaterialPtr& MeshPart::variation(Int variation)C
  592. {
  593. if(InRange(--variation, _variations))return _variations[variation].material;
  594. return material();
  595. }
  596. C MeshPart::Variation& MeshPart::getVariation()C
  597. {
  598. Int i=Renderer._mesh_variation_1; // this is "variation-1"
  599. return InRange(i, _variations) ? _variations[i] : _variation;
  600. }
  601. C MeshPart::Variation& MeshPart::getVariation1(Int variation_1)C // this is "variation-1"
  602. {
  603. return InRange(variation_1, _variations) ? _variations[variation_1] : _variation;
  604. }
  605. void MeshPart::variationRemove(Int variation)
  606. {
  607. if(!variation)
  608. {
  609. if(_variations.elms())Swap(_variation, _variations[0]);
  610. _variations.remove(0, true); // #0 was placed in '_variation'
  611. }else
  612. {
  613. _variations.remove(variation-1, true);
  614. }
  615. }
  616. void MeshPart::variationKeep(Int variation)
  617. {
  618. if(InRange(--variation, _variations))Swap(_variation, _variations[variation]);
  619. _variations.del();
  620. }
  621. void MeshPart::variationMove(Int variation, Int new_index)
  622. {
  623. MemtN<Variation, 16> variations; variations.setNum(T.variations());
  624. Swap(variations[0], _variation); REPA(_variations)Swap(variations[i+1], _variations[i]);
  625. variations.moveElm(variation, new_index);
  626. Swap(variations[0], _variation); REPA(_variations)Swap(variations[i+1], _variations[i]);
  627. }
  628. void MeshPart::variationRemap(C Mesh &src, C Mesh &dest)
  629. {
  630. if(&src!=&dest)
  631. {
  632. MemtN<Variation, 16> variations; variations.setNum(T.variations());
  633. Swap(variations[0], _variation); REPA(_variations)Swap(variations[i+1], _variations[i]);
  634. _variations.setNum(dest.variations()-1);
  635. REP(dest.variations())
  636. {
  637. Int src_var_i=src.variationFind(dest.variationName(i));
  638. C Variation & src_var =(InRange(src_var_i, variations) ? variations[src_var_i] : variations[0]);
  639. Variation &dest_var =(i ? _variations[i-1] : _variation);
  640. dest_var=src_var;
  641. }
  642. }
  643. }
  644. /******************************************************************************/
  645. // HEIGHTMAP
  646. /******************************************************************************/
  647. MeshPart& MeshPart::heightmap(Flt tex_scale, Int lod_index)
  648. {
  649. MAX(tex_scale, 0); if(tex_scale!=_vtx_heightmap)
  650. {
  651. _vtx_heightmap=tex_scale;
  652. if(heightmap())exclude(VTX_TEX_ALL|VTX_TAN_BIN); // if enabled heightmap, then delete tex/tan/bin as they're no longer needed
  653. setShader(lod_index);
  654. }
  655. return T;
  656. }
  657. /******************************************************************************/
  658. // TEXTURIZE
  659. /******************************************************************************/
  660. MeshPart& MeshPart::texMove (C Vec2 &move , Byte tex_index) {base.texMove (move , tex_index); render.texMove (move , tex_index); return T;}
  661. MeshPart& MeshPart::texScale (C Vec2 &scale, Byte tex_index) {base.texScale (scale, tex_index); render.texScale (scale, tex_index); return T;}
  662. MeshPart& MeshPart::texRotate( Flt angle, Byte tex_index) {base.texRotate(angle, tex_index); render.texRotate(angle, tex_index); return T;}
  663. /******************************************************************************/
  664. // TRANSFORM
  665. /******************************************************************************/
  666. MeshPart& MeshPart::move ( C Vec &move) {base. move( move); render.scaleMove(VecOne, move); return T;}
  667. MeshPart& MeshPart::scale (C Vec &scale ) {base.scale (scale ); render.scaleMove(scale ); scaleParams(Abs(scale).max()); return T;}
  668. MeshPart& MeshPart::scaleMove (C Vec &scale, C Vec &move) {base.scaleMove(scale, move); render.scaleMove(scale , move); scaleParams(Abs(scale).max()); return T;}
  669. MeshPart& MeshPart::scaleMoveBase(C Vec &scale, C Vec &move) {base.scaleMove(scale, move); scaleParams(Abs(scale).max()); return T;}
  670. MeshPart& MeshPart::transform (C Matrix3 &matrix )
  671. {
  672. Bool base_is=base.is();
  673. if(! base_is)base.create(render);
  674. base.transform(matrix);
  675. scaleParams (matrix.maxScale());
  676. if(render.is())render.create(base);
  677. if(! base_is )delBase();
  678. return T;
  679. }
  680. MeshPart& MeshPart::transform(C Matrix &matrix)
  681. {
  682. Bool base_is=base.is();
  683. if(! base_is)base.create(render);
  684. base.transform(matrix);
  685. scaleParams (matrix.maxScale());
  686. if(render.is())render.create(base);
  687. if(! base_is )delBase();
  688. return T;
  689. }
  690. MeshPart& MeshPart::animate(C MemPtrN<Matrix, 256> &matrixes)
  691. {
  692. if(matrixes.elms())
  693. {
  694. Bool base_is=base.is();
  695. if(! base_is)base.create(render);
  696. base.animate(matrixes);
  697. scaleParams (matrixes[0].maxScale());
  698. if(render.is())render.create(base);
  699. if(! base_is )delBase();
  700. }
  701. return T;
  702. }
  703. MeshPart& MeshPart::animate(C AnimatedSkeleton &skel)
  704. {
  705. Bool base_is=base.is();
  706. if(! base_is)base.create(render);
  707. base.animate(skel);
  708. scaleParams (skel.matrix().maxScale());
  709. if(render.is())render.create(base);
  710. if(! base_is )delBase();
  711. return T;
  712. }
  713. /******************************************************************************/
  714. // OPERATIONS
  715. /******************************************************************************/
  716. MeshPart& MeshPart::boneRemap(C MemPtr<Byte, 256> &old_to_new)
  717. {
  718. Bool base_is=base.is();
  719. if( !base_is)base.create(render);
  720. base.boneRemap(old_to_new);
  721. if(render.is())render.create(base);
  722. if( !base_is )delBase();
  723. return T;
  724. }
  725. void MeshPart:: setUsedBones(Bool (&bones)[256])C {Zero(bones); includeUsedBones(bones);}
  726. void MeshPart::includeUsedBones(Bool (&bones)[256])C
  727. {
  728. if(base.is())base.includeUsedBones(bones);else render.includeUsedBones(bones);
  729. }
  730. MeshPart& MeshPart::freeOpenGLESData() {render.freeOpenGLESData(); return T;}
  731. /******************************************************************************/
  732. MeshPart& MeshPart::setLeafAttachment(C Vec2 &tex)
  733. {
  734. Bool base_is=base.is();
  735. if( !base_is && render.is())setBase();
  736. SetLeafAttachment(base, tex);
  737. if(render.is())setRender();
  738. if(!base_is )delBase();
  739. return T;
  740. }
  741. MeshPart& MeshPart::setLeafAttachment(C Vec &pos)
  742. {
  743. Bool base_is=base.is();
  744. if( !base_is && render.is())setBase();
  745. base.include(VTX_HLP); REPA(base.vtx)base.vtx.hlp(i)=pos;
  746. if(render.is())setRender();
  747. if(!base_is )delBase();
  748. return T;
  749. }
  750. MeshPart& MeshPart::setRandomLeafColor(Flt variation)
  751. {
  752. Bool base_is=base.is();
  753. if( !base_is && render.is())setBase();
  754. SetRandomColor(base, variation);
  755. if(render.is())setRender();
  756. if(!base_is )delBase();
  757. return T;
  758. }
  759. MeshPart& MeshPart::setRandomLeafBending()
  760. {
  761. Bool base_is=base.is();
  762. if( !base_is && render.is())setBase();
  763. SetRandomBend(base);
  764. if(render.is())setRender();
  765. if(!base_is )delBase();
  766. return T;
  767. }
  768. MeshPart& MeshPart::setRandomLeafBending(Flt random_value)
  769. {
  770. Bool base_is=base.is();
  771. if( !base_is && render.is())setBase();
  772. base.include(VTX_SIZE); REPA(base.vtx)base.vtx.size(i)=random_value;
  773. if(render.is())setRender();
  774. if(!base_is )delBase();
  775. return T;
  776. }
  777. MeshPart& MeshPart::delRandomLeafBending() {exclude(VTX_SIZE); return T;}
  778. /******************************************************************************/
  779. MeshPart& MeshPart::operator+=(C MeshPart &src)
  780. {
  781. base +=src.base ;
  782. render+=src.render;
  783. if(!(src.part_flag&MSHP_HIDDEN))FlagDisable(part_flag, MSHP_HIDDEN); // unhide part if 'src' is visible
  784. for(Int i=_variations.elms(); i<src._variations.elms(); i++)_variations.add(src._variations[i]); // add variations available in 'src' but not in 'this'
  785. return T;
  786. }
  787. /******************************************************************************/
  788. void MeshPart::unlinkSolid ()C {_variation.unlinkSolid (); REPAO(_variations).unlinkSolid ();}
  789. void MeshPart::unlinkShadow()C {_variation.unlinkShadow(); REPAO(_variations).unlinkShadow();}
  790. void MeshPart::unlink ()C {_variation.unlink (); REPAO(_variations).unlink ();}
  791. /******************************************************************************/
  792. }
  793. /******************************************************************************/