primitive_meshes.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  1. /*************************************************************************/
  2. /* primitive_meshes.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "primitive_meshes.h"
  31. #include "servers/visual_server.h"
  32. /**
  33. PrimitiveMesh
  34. */
  35. void PrimitiveMesh::_update() const {
  36. Array arr;
  37. arr.resize(VS::ARRAY_MAX);
  38. _create_mesh_array(arr);
  39. PoolVector<Vector3> points = arr[VS::ARRAY_VERTEX];
  40. aabb = AABB();
  41. int pc = points.size();
  42. ERR_FAIL_COND(pc == 0);
  43. {
  44. PoolVector<Vector3>::Read r = points.read();
  45. for (int i = 0; i < pc; i++) {
  46. if (i == 0)
  47. aabb.position = r[i];
  48. else
  49. aabb.expand_to(r[i]);
  50. }
  51. }
  52. if (flip_faces) {
  53. PoolVector<Vector3> normals = arr[VS::ARRAY_NORMAL];
  54. PoolVector<int> indices = arr[VS::ARRAY_INDEX];
  55. if (normals.size() && indices.size()) {
  56. {
  57. int nc = normals.size();
  58. PoolVector<Vector3>::Write w = normals.write();
  59. for (int i = 0; i < nc; i++) {
  60. w[i] = -w[i];
  61. }
  62. }
  63. {
  64. int ic = indices.size();
  65. PoolVector<int>::Write w = indices.write();
  66. for (int i = 0; i < ic; i += 3) {
  67. SWAP(w[i + 0], w[i + 1]);
  68. }
  69. }
  70. arr[VS::ARRAY_NORMAL] = normals;
  71. arr[VS::ARRAY_INDEX] = indices;
  72. }
  73. }
  74. // in with the new
  75. VisualServer::get_singleton()->mesh_clear(mesh);
  76. VisualServer::get_singleton()->mesh_add_surface_from_arrays(mesh, (VisualServer::PrimitiveType)primitive_type, arr);
  77. VisualServer::get_singleton()->mesh_surface_set_material(mesh, 0, material.is_null() ? RID() : material->get_rid());
  78. pending_request = false;
  79. clear_cache();
  80. const_cast<PrimitiveMesh *>(this)->emit_changed();
  81. }
  82. void PrimitiveMesh::_request_update() {
  83. if (pending_request)
  84. return;
  85. _update();
  86. }
  87. int PrimitiveMesh::get_surface_count() const {
  88. if (pending_request) {
  89. _update();
  90. }
  91. return 1;
  92. }
  93. int PrimitiveMesh::surface_get_array_len(int p_idx) const {
  94. ERR_FAIL_INDEX_V(p_idx, 1, -1);
  95. if (pending_request) {
  96. _update();
  97. }
  98. return VisualServer::get_singleton()->mesh_surface_get_array_len(mesh, 0);
  99. }
  100. int PrimitiveMesh::surface_get_array_index_len(int p_idx) const {
  101. ERR_FAIL_INDEX_V(p_idx, 1, -1);
  102. if (pending_request) {
  103. _update();
  104. }
  105. return VisualServer::get_singleton()->mesh_surface_get_array_index_len(mesh, 0);
  106. }
  107. Array PrimitiveMesh::surface_get_arrays(int p_surface) const {
  108. ERR_FAIL_INDEX_V(p_surface, 1, Array());
  109. if (pending_request) {
  110. _update();
  111. }
  112. return VisualServer::get_singleton()->mesh_surface_get_arrays(mesh, 0);
  113. }
  114. Array PrimitiveMesh::surface_get_blend_shape_arrays(int p_surface) const {
  115. ERR_FAIL_INDEX_V(p_surface, 1, Array());
  116. if (pending_request) {
  117. _update();
  118. }
  119. return Array();
  120. }
  121. uint32_t PrimitiveMesh::surface_get_format(int p_idx) const {
  122. ERR_FAIL_INDEX_V(p_idx, 1, 0);
  123. if (pending_request) {
  124. _update();
  125. }
  126. return VisualServer::get_singleton()->mesh_surface_get_format(mesh, 0);
  127. }
  128. Mesh::PrimitiveType PrimitiveMesh::surface_get_primitive_type(int p_idx) const {
  129. return primitive_type;
  130. }
  131. Ref<Material> PrimitiveMesh::surface_get_material(int p_idx) const {
  132. ERR_FAIL_INDEX_V(p_idx, 1, NULL);
  133. return material;
  134. }
  135. int PrimitiveMesh::get_blend_shape_count() const {
  136. return 0;
  137. }
  138. StringName PrimitiveMesh::get_blend_shape_name(int p_index) const {
  139. return StringName();
  140. }
  141. AABB PrimitiveMesh::get_aabb() const {
  142. if (pending_request) {
  143. _update();
  144. }
  145. return aabb;
  146. }
  147. RID PrimitiveMesh::get_rid() const {
  148. if (pending_request) {
  149. _update();
  150. }
  151. return mesh;
  152. }
  153. void PrimitiveMesh::_bind_methods() {
  154. ClassDB::bind_method(D_METHOD("_update"), &PrimitiveMesh::_update);
  155. ClassDB::bind_method(D_METHOD("set_material", "material"), &PrimitiveMesh::set_material);
  156. ClassDB::bind_method(D_METHOD("get_material"), &PrimitiveMesh::get_material);
  157. ClassDB::bind_method(D_METHOD("get_mesh_arrays"), &PrimitiveMesh::get_mesh_arrays);
  158. ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &PrimitiveMesh::set_custom_aabb);
  159. ClassDB::bind_method(D_METHOD("get_custom_aabb"), &PrimitiveMesh::get_custom_aabb);
  160. ClassDB::bind_method(D_METHOD("set_flip_faces", "flip_faces"), &PrimitiveMesh::set_flip_faces);
  161. ClassDB::bind_method(D_METHOD("get_flip_faces"), &PrimitiveMesh::get_flip_faces);
  162. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "SpatialMaterial,ShaderMaterial"), "set_material", "get_material");
  163. ADD_PROPERTY(PropertyInfo(Variant::AABB, "custom_aabb", PROPERTY_HINT_NONE, ""), "set_custom_aabb", "get_custom_aabb");
  164. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_faces"), "set_flip_faces", "get_flip_faces");
  165. }
  166. void PrimitiveMesh::set_material(const Ref<Material> &p_material) {
  167. material = p_material;
  168. if (!pending_request) {
  169. // just apply it, else it'll happen when _update is called.
  170. VisualServer::get_singleton()->mesh_surface_set_material(mesh, 0, material.is_null() ? RID() : material->get_rid());
  171. _change_notify();
  172. emit_changed();
  173. };
  174. }
  175. Ref<Material> PrimitiveMesh::get_material() const {
  176. return material;
  177. }
  178. Array PrimitiveMesh::get_mesh_arrays() const {
  179. return surface_get_arrays(0);
  180. }
  181. void PrimitiveMesh::set_custom_aabb(const AABB &p_custom) {
  182. custom_aabb = p_custom;
  183. VS::get_singleton()->mesh_set_custom_aabb(mesh, custom_aabb);
  184. emit_changed();
  185. }
  186. AABB PrimitiveMesh::get_custom_aabb() const {
  187. return custom_aabb;
  188. }
  189. void PrimitiveMesh::set_flip_faces(bool p_enable) {
  190. flip_faces = p_enable;
  191. _request_update();
  192. }
  193. bool PrimitiveMesh::get_flip_faces() const {
  194. return flip_faces;
  195. }
  196. PrimitiveMesh::PrimitiveMesh() {
  197. flip_faces = false;
  198. // defaults
  199. mesh = VisualServer::get_singleton()->mesh_create();
  200. // assume primitive triangles as the type, correct for all but one and it will change this :)
  201. primitive_type = Mesh::PRIMITIVE_TRIANGLES;
  202. // make sure we do an update after we've finished constructing our object
  203. pending_request = true;
  204. }
  205. PrimitiveMesh::~PrimitiveMesh() {
  206. VisualServer::get_singleton()->free(mesh);
  207. }
  208. /**
  209. CapsuleMesh
  210. */
  211. void CapsuleMesh::_create_mesh_array(Array &p_arr) const {
  212. int i, j, prevrow, thisrow, point;
  213. float x, y, z, u, v, w;
  214. float onethird = 1.0 / 3.0;
  215. float twothirds = 2.0 / 3.0;
  216. // note, this has been aligned with our collision shape but I've left the descriptions as top/middle/bottom
  217. PoolVector<Vector3> points;
  218. PoolVector<Vector3> normals;
  219. PoolVector<float> tangents;
  220. PoolVector<Vector2> uvs;
  221. PoolVector<int> indices;
  222. point = 0;
  223. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  224. tangents.push_back(m_x); \
  225. tangents.push_back(m_y); \
  226. tangents.push_back(m_z); \
  227. tangents.push_back(m_d);
  228. /* top hemisphere */
  229. thisrow = 0;
  230. prevrow = 0;
  231. for (j = 0; j <= (rings + 1); j++) {
  232. v = j;
  233. v /= (rings + 1);
  234. w = sin(0.5 * Math_PI * v);
  235. z = radius * cos(0.5 * Math_PI * v);
  236. for (i = 0; i <= radial_segments; i++) {
  237. u = i;
  238. u /= radial_segments;
  239. x = sin(u * (Math_PI * 2.0));
  240. y = -cos(u * (Math_PI * 2.0));
  241. Vector3 p = Vector3(x * radius * w, y * radius * w, z);
  242. points.push_back(p + Vector3(0.0, 0.0, 0.5 * mid_height));
  243. normals.push_back(p.normalized());
  244. ADD_TANGENT(-y, x, 0.0, 1.0)
  245. uvs.push_back(Vector2(u, v * onethird));
  246. point++;
  247. if (i > 0 && j > 0) {
  248. indices.push_back(prevrow + i - 1);
  249. indices.push_back(prevrow + i);
  250. indices.push_back(thisrow + i - 1);
  251. indices.push_back(prevrow + i);
  252. indices.push_back(thisrow + i);
  253. indices.push_back(thisrow + i - 1);
  254. };
  255. };
  256. prevrow = thisrow;
  257. thisrow = point;
  258. };
  259. /* cylinder */
  260. thisrow = point;
  261. prevrow = 0;
  262. for (j = 0; j <= (rings + 1); j++) {
  263. v = j;
  264. v /= (rings + 1);
  265. z = mid_height * v;
  266. z = (mid_height * 0.5) - z;
  267. for (i = 0; i <= radial_segments; i++) {
  268. u = i;
  269. u /= radial_segments;
  270. x = sin(u * (Math_PI * 2.0));
  271. y = -cos(u * (Math_PI * 2.0));
  272. Vector3 p = Vector3(x * radius, y * radius, z);
  273. points.push_back(p);
  274. normals.push_back(Vector3(x, y, 0.0));
  275. ADD_TANGENT(-y, x, 0.0, 1.0)
  276. uvs.push_back(Vector2(u, onethird + (v * onethird)));
  277. point++;
  278. if (i > 0 && j > 0) {
  279. indices.push_back(prevrow + i - 1);
  280. indices.push_back(prevrow + i);
  281. indices.push_back(thisrow + i - 1);
  282. indices.push_back(prevrow + i);
  283. indices.push_back(thisrow + i);
  284. indices.push_back(thisrow + i - 1);
  285. };
  286. };
  287. prevrow = thisrow;
  288. thisrow = point;
  289. };
  290. /* bottom hemisphere */
  291. thisrow = point;
  292. prevrow = 0;
  293. for (j = 0; j <= (rings + 1); j++) {
  294. v = j;
  295. v /= (rings + 1);
  296. v += 1.0;
  297. w = sin(0.5 * Math_PI * v);
  298. z = radius * cos(0.5 * Math_PI * v);
  299. for (i = 0; i <= radial_segments; i++) {
  300. float u = i;
  301. u /= radial_segments;
  302. x = sin(u * (Math_PI * 2.0));
  303. y = -cos(u * (Math_PI * 2.0));
  304. Vector3 p = Vector3(x * radius * w, y * radius * w, z);
  305. points.push_back(p + Vector3(0.0, 0.0, -0.5 * mid_height));
  306. normals.push_back(p.normalized());
  307. ADD_TANGENT(-y, x, 0.0, 1.0)
  308. uvs.push_back(Vector2(u, twothirds + ((v - 1.0) * onethird)));
  309. point++;
  310. if (i > 0 && j > 0) {
  311. indices.push_back(prevrow + i - 1);
  312. indices.push_back(prevrow + i);
  313. indices.push_back(thisrow + i - 1);
  314. indices.push_back(prevrow + i);
  315. indices.push_back(thisrow + i);
  316. indices.push_back(thisrow + i - 1);
  317. };
  318. };
  319. prevrow = thisrow;
  320. thisrow = point;
  321. };
  322. p_arr[VS::ARRAY_VERTEX] = points;
  323. p_arr[VS::ARRAY_NORMAL] = normals;
  324. p_arr[VS::ARRAY_TANGENT] = tangents;
  325. p_arr[VS::ARRAY_TEX_UV] = uvs;
  326. p_arr[VS::ARRAY_INDEX] = indices;
  327. }
  328. void CapsuleMesh::_bind_methods() {
  329. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CapsuleMesh::set_radius);
  330. ClassDB::bind_method(D_METHOD("get_radius"), &CapsuleMesh::get_radius);
  331. ClassDB::bind_method(D_METHOD("set_mid_height", "mid_height"), &CapsuleMesh::set_mid_height);
  332. ClassDB::bind_method(D_METHOD("get_mid_height"), &CapsuleMesh::get_mid_height);
  333. ClassDB::bind_method(D_METHOD("set_radial_segments", "segments"), &CapsuleMesh::set_radial_segments);
  334. ClassDB::bind_method(D_METHOD("get_radial_segments"), &CapsuleMesh::get_radial_segments);
  335. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &CapsuleMesh::set_rings);
  336. ClassDB::bind_method(D_METHOD("get_rings"), &CapsuleMesh::get_rings);
  337. ADD_PROPERTY(PropertyInfo(Variant::REAL, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater"), "set_radius", "get_radius");
  338. ADD_PROPERTY(PropertyInfo(Variant::REAL, "mid_height", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater"), "set_mid_height", "get_mid_height");
  339. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_radial_segments", "get_radial_segments");
  340. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_rings", "get_rings");
  341. }
  342. void CapsuleMesh::set_radius(const float p_radius) {
  343. radius = p_radius;
  344. _request_update();
  345. }
  346. float CapsuleMesh::get_radius() const {
  347. return radius;
  348. }
  349. void CapsuleMesh::set_mid_height(const float p_mid_height) {
  350. mid_height = p_mid_height;
  351. _request_update();
  352. }
  353. float CapsuleMesh::get_mid_height() const {
  354. return mid_height;
  355. }
  356. void CapsuleMesh::set_radial_segments(const int p_segments) {
  357. radial_segments = p_segments > 4 ? p_segments : 4;
  358. _request_update();
  359. }
  360. int CapsuleMesh::get_radial_segments() const {
  361. return radial_segments;
  362. }
  363. void CapsuleMesh::set_rings(const int p_rings) {
  364. rings = p_rings > 1 ? p_rings : 1;
  365. _request_update();
  366. }
  367. int CapsuleMesh::get_rings() const {
  368. return rings;
  369. }
  370. CapsuleMesh::CapsuleMesh() {
  371. // defaults
  372. radius = 1.0;
  373. mid_height = 1.0;
  374. radial_segments = 64;
  375. rings = 8;
  376. }
  377. /**
  378. CubeMesh
  379. */
  380. void CubeMesh::_create_mesh_array(Array &p_arr) const {
  381. int i, j, prevrow, thisrow, point;
  382. float x, y, z;
  383. float onethird = 1.0 / 3.0;
  384. float twothirds = 2.0 / 3.0;
  385. Vector3 start_pos = size * -0.5;
  386. // set our bounding box
  387. PoolVector<Vector3> points;
  388. PoolVector<Vector3> normals;
  389. PoolVector<float> tangents;
  390. PoolVector<Vector2> uvs;
  391. PoolVector<int> indices;
  392. point = 0;
  393. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  394. tangents.push_back(m_x); \
  395. tangents.push_back(m_y); \
  396. tangents.push_back(m_z); \
  397. tangents.push_back(m_d);
  398. // front + back
  399. y = start_pos.y;
  400. thisrow = point;
  401. prevrow = 0;
  402. for (j = 0; j <= subdivide_h + 1; j++) {
  403. x = start_pos.x;
  404. for (i = 0; i <= subdivide_w + 1; i++) {
  405. float u = i;
  406. float v = j;
  407. u /= (3.0 * (subdivide_w + 1.0));
  408. v /= (2.0 * (subdivide_h + 1.0));
  409. // front
  410. points.push_back(Vector3(x, -y, -start_pos.z)); // double negative on the Z!
  411. normals.push_back(Vector3(0.0, 0.0, 1.0));
  412. ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
  413. uvs.push_back(Vector2(u, v));
  414. point++;
  415. // back
  416. points.push_back(Vector3(-x, -y, start_pos.z));
  417. normals.push_back(Vector3(0.0, 0.0, -1.0));
  418. ADD_TANGENT(-1.0, 0.0, 0.0, 1.0);
  419. uvs.push_back(Vector2(twothirds + u, v));
  420. point++;
  421. if (i > 0 && j > 0) {
  422. int i2 = i * 2;
  423. // front
  424. indices.push_back(prevrow + i2 - 2);
  425. indices.push_back(prevrow + i2);
  426. indices.push_back(thisrow + i2 - 2);
  427. indices.push_back(prevrow + i2);
  428. indices.push_back(thisrow + i2);
  429. indices.push_back(thisrow + i2 - 2);
  430. // back
  431. indices.push_back(prevrow + i2 - 1);
  432. indices.push_back(prevrow + i2 + 1);
  433. indices.push_back(thisrow + i2 - 1);
  434. indices.push_back(prevrow + i2 + 1);
  435. indices.push_back(thisrow + i2 + 1);
  436. indices.push_back(thisrow + i2 - 1);
  437. };
  438. x += size.x / (subdivide_w + 1.0);
  439. };
  440. y += size.y / (subdivide_h + 1.0);
  441. prevrow = thisrow;
  442. thisrow = point;
  443. };
  444. // left + right
  445. y = start_pos.y;
  446. thisrow = point;
  447. prevrow = 0;
  448. for (j = 0; j <= (subdivide_h + 1); j++) {
  449. z = start_pos.z;
  450. for (i = 0; i <= (subdivide_d + 1); i++) {
  451. float u = i;
  452. float v = j;
  453. u /= (3.0 * (subdivide_d + 1.0));
  454. v /= (2.0 * (subdivide_h + 1.0));
  455. // right
  456. points.push_back(Vector3(-start_pos.x, -y, -z));
  457. normals.push_back(Vector3(1.0, 0.0, 0.0));
  458. ADD_TANGENT(0.0, 0.0, -1.0, 1.0);
  459. uvs.push_back(Vector2(onethird + u, v));
  460. point++;
  461. // left
  462. points.push_back(Vector3(start_pos.x, -y, z));
  463. normals.push_back(Vector3(-1.0, 0.0, 0.0));
  464. ADD_TANGENT(0.0, 0.0, 1.0, 1.0);
  465. uvs.push_back(Vector2(u, 0.5 + v));
  466. point++;
  467. if (i > 0 && j > 0) {
  468. int i2 = i * 2;
  469. // right
  470. indices.push_back(prevrow + i2 - 2);
  471. indices.push_back(prevrow + i2);
  472. indices.push_back(thisrow + i2 - 2);
  473. indices.push_back(prevrow + i2);
  474. indices.push_back(thisrow + i2);
  475. indices.push_back(thisrow + i2 - 2);
  476. // left
  477. indices.push_back(prevrow + i2 - 1);
  478. indices.push_back(prevrow + i2 + 1);
  479. indices.push_back(thisrow + i2 - 1);
  480. indices.push_back(prevrow + i2 + 1);
  481. indices.push_back(thisrow + i2 + 1);
  482. indices.push_back(thisrow + i2 - 1);
  483. };
  484. z += size.z / (subdivide_d + 1.0);
  485. };
  486. y += size.y / (subdivide_h + 1.0);
  487. prevrow = thisrow;
  488. thisrow = point;
  489. };
  490. // top + bottom
  491. z = start_pos.z;
  492. thisrow = point;
  493. prevrow = 0;
  494. for (j = 0; j <= (subdivide_d + 1); j++) {
  495. x = start_pos.x;
  496. for (i = 0; i <= (subdivide_w + 1); i++) {
  497. float u = i;
  498. float v = j;
  499. u /= (3.0 * (subdivide_w + 1.0));
  500. v /= (2.0 * (subdivide_d + 1.0));
  501. // top
  502. points.push_back(Vector3(-x, -start_pos.y, -z));
  503. normals.push_back(Vector3(0.0, 1.0, 0.0));
  504. ADD_TANGENT(-1.0, 0.0, 0.0, 1.0);
  505. uvs.push_back(Vector2(onethird + u, 0.5 + v));
  506. point++;
  507. // bottom
  508. points.push_back(Vector3(x, start_pos.y, -z));
  509. normals.push_back(Vector3(0.0, -1.0, 0.0));
  510. ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
  511. uvs.push_back(Vector2(twothirds + u, 0.5 + v));
  512. point++;
  513. if (i > 0 && j > 0) {
  514. int i2 = i * 2;
  515. // top
  516. indices.push_back(prevrow + i2 - 2);
  517. indices.push_back(prevrow + i2);
  518. indices.push_back(thisrow + i2 - 2);
  519. indices.push_back(prevrow + i2);
  520. indices.push_back(thisrow + i2);
  521. indices.push_back(thisrow + i2 - 2);
  522. // bottom
  523. indices.push_back(prevrow + i2 - 1);
  524. indices.push_back(prevrow + i2 + 1);
  525. indices.push_back(thisrow + i2 - 1);
  526. indices.push_back(prevrow + i2 + 1);
  527. indices.push_back(thisrow + i2 + 1);
  528. indices.push_back(thisrow + i2 - 1);
  529. };
  530. x += size.x / (subdivide_w + 1.0);
  531. };
  532. z += size.z / (subdivide_d + 1.0);
  533. prevrow = thisrow;
  534. thisrow = point;
  535. };
  536. p_arr[VS::ARRAY_VERTEX] = points;
  537. p_arr[VS::ARRAY_NORMAL] = normals;
  538. p_arr[VS::ARRAY_TANGENT] = tangents;
  539. p_arr[VS::ARRAY_TEX_UV] = uvs;
  540. p_arr[VS::ARRAY_INDEX] = indices;
  541. }
  542. void CubeMesh::_bind_methods() {
  543. ClassDB::bind_method(D_METHOD("set_size", "size"), &CubeMesh::set_size);
  544. ClassDB::bind_method(D_METHOD("get_size"), &CubeMesh::get_size);
  545. ClassDB::bind_method(D_METHOD("set_subdivide_width", "subdivide"), &CubeMesh::set_subdivide_width);
  546. ClassDB::bind_method(D_METHOD("get_subdivide_width"), &CubeMesh::get_subdivide_width);
  547. ClassDB::bind_method(D_METHOD("set_subdivide_height", "divisions"), &CubeMesh::set_subdivide_height);
  548. ClassDB::bind_method(D_METHOD("get_subdivide_height"), &CubeMesh::get_subdivide_height);
  549. ClassDB::bind_method(D_METHOD("set_subdivide_depth", "divisions"), &CubeMesh::set_subdivide_depth);
  550. ClassDB::bind_method(D_METHOD("get_subdivide_depth"), &CubeMesh::get_subdivide_depth);
  551. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size"), "set_size", "get_size");
  552. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_width", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_width", "get_subdivide_width");
  553. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_height", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_height", "get_subdivide_height");
  554. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_depth", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_depth", "get_subdivide_depth");
  555. }
  556. void CubeMesh::set_size(const Vector3 &p_size) {
  557. size = p_size;
  558. _request_update();
  559. }
  560. Vector3 CubeMesh::get_size() const {
  561. return size;
  562. }
  563. void CubeMesh::set_subdivide_width(const int p_divisions) {
  564. subdivide_w = p_divisions > 0 ? p_divisions : 0;
  565. _request_update();
  566. }
  567. int CubeMesh::get_subdivide_width() const {
  568. return subdivide_w;
  569. }
  570. void CubeMesh::set_subdivide_height(const int p_divisions) {
  571. subdivide_h = p_divisions > 0 ? p_divisions : 0;
  572. _request_update();
  573. }
  574. int CubeMesh::get_subdivide_height() const {
  575. return subdivide_h;
  576. }
  577. void CubeMesh::set_subdivide_depth(const int p_divisions) {
  578. subdivide_d = p_divisions > 0 ? p_divisions : 0;
  579. _request_update();
  580. }
  581. int CubeMesh::get_subdivide_depth() const {
  582. return subdivide_d;
  583. }
  584. CubeMesh::CubeMesh() {
  585. // defaults
  586. size = Vector3(2.0, 2.0, 2.0);
  587. subdivide_w = 0;
  588. subdivide_h = 0;
  589. subdivide_d = 0;
  590. }
  591. /**
  592. CylinderMesh
  593. */
  594. void CylinderMesh::_create_mesh_array(Array &p_arr) const {
  595. int i, j, prevrow, thisrow, point;
  596. float x, y, z, u, v, radius;
  597. radius = bottom_radius > top_radius ? bottom_radius : top_radius;
  598. PoolVector<Vector3> points;
  599. PoolVector<Vector3> normals;
  600. PoolVector<float> tangents;
  601. PoolVector<Vector2> uvs;
  602. PoolVector<int> indices;
  603. point = 0;
  604. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  605. tangents.push_back(m_x); \
  606. tangents.push_back(m_y); \
  607. tangents.push_back(m_z); \
  608. tangents.push_back(m_d);
  609. thisrow = 0;
  610. prevrow = 0;
  611. for (j = 0; j <= (rings + 1); j++) {
  612. v = j;
  613. v /= (rings + 1);
  614. radius = top_radius + ((bottom_radius - top_radius) * v);
  615. y = height * v;
  616. y = (height * 0.5) - y;
  617. for (i = 0; i <= radial_segments; i++) {
  618. u = i;
  619. u /= radial_segments;
  620. x = sin(u * (Math_PI * 2.0));
  621. z = cos(u * (Math_PI * 2.0));
  622. Vector3 p = Vector3(x * radius, y, z * radius);
  623. points.push_back(p);
  624. normals.push_back(Vector3(x, 0.0, z));
  625. ADD_TANGENT(z, 0.0, -x, 1.0)
  626. uvs.push_back(Vector2(u, v * 0.5));
  627. point++;
  628. if (i > 0 && j > 0) {
  629. indices.push_back(prevrow + i - 1);
  630. indices.push_back(prevrow + i);
  631. indices.push_back(thisrow + i - 1);
  632. indices.push_back(prevrow + i);
  633. indices.push_back(thisrow + i);
  634. indices.push_back(thisrow + i - 1);
  635. };
  636. };
  637. prevrow = thisrow;
  638. thisrow = point;
  639. };
  640. // add top
  641. if (top_radius > 0.0) {
  642. y = height * 0.5;
  643. thisrow = point;
  644. points.push_back(Vector3(0.0, y, 0.0));
  645. normals.push_back(Vector3(0.0, 1.0, 0.0));
  646. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  647. uvs.push_back(Vector2(0.25, 0.75));
  648. point++;
  649. for (i = 0; i <= radial_segments; i++) {
  650. float r = i;
  651. r /= radial_segments;
  652. x = sin(r * (Math_PI * 2.0));
  653. z = cos(r * (Math_PI * 2.0));
  654. u = ((x + 1.0) * 0.25);
  655. v = 0.5 + ((z + 1.0) * 0.25);
  656. Vector3 p = Vector3(x * top_radius, y, z * top_radius);
  657. points.push_back(p);
  658. normals.push_back(Vector3(0.0, 1.0, 0.0));
  659. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  660. uvs.push_back(Vector2(u, v));
  661. point++;
  662. if (i > 0) {
  663. indices.push_back(thisrow);
  664. indices.push_back(point - 1);
  665. indices.push_back(point - 2);
  666. };
  667. };
  668. };
  669. // add bottom
  670. if (bottom_radius > 0.0) {
  671. y = height * -0.5;
  672. thisrow = point;
  673. points.push_back(Vector3(0.0, y, 0.0));
  674. normals.push_back(Vector3(0.0, -1.0, 0.0));
  675. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  676. uvs.push_back(Vector2(0.75, 0.75));
  677. point++;
  678. for (i = 0; i <= radial_segments; i++) {
  679. float r = i;
  680. r /= radial_segments;
  681. x = sin(r * (Math_PI * 2.0));
  682. z = cos(r * (Math_PI * 2.0));
  683. u = 0.5 + ((x + 1.0) * 0.25);
  684. v = 1.0 - ((z + 1.0) * 0.25);
  685. Vector3 p = Vector3(x * bottom_radius, y, z * bottom_radius);
  686. points.push_back(p);
  687. normals.push_back(Vector3(0.0, -1.0, 0.0));
  688. ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
  689. uvs.push_back(Vector2(u, v));
  690. point++;
  691. if (i > 0) {
  692. indices.push_back(thisrow);
  693. indices.push_back(point - 2);
  694. indices.push_back(point - 1);
  695. };
  696. };
  697. };
  698. p_arr[VS::ARRAY_VERTEX] = points;
  699. p_arr[VS::ARRAY_NORMAL] = normals;
  700. p_arr[VS::ARRAY_TANGENT] = tangents;
  701. p_arr[VS::ARRAY_TEX_UV] = uvs;
  702. p_arr[VS::ARRAY_INDEX] = indices;
  703. }
  704. void CylinderMesh::_bind_methods() {
  705. ClassDB::bind_method(D_METHOD("set_top_radius", "radius"), &CylinderMesh::set_top_radius);
  706. ClassDB::bind_method(D_METHOD("get_top_radius"), &CylinderMesh::get_top_radius);
  707. ClassDB::bind_method(D_METHOD("set_bottom_radius", "radius"), &CylinderMesh::set_bottom_radius);
  708. ClassDB::bind_method(D_METHOD("get_bottom_radius"), &CylinderMesh::get_bottom_radius);
  709. ClassDB::bind_method(D_METHOD("set_height", "height"), &CylinderMesh::set_height);
  710. ClassDB::bind_method(D_METHOD("get_height"), &CylinderMesh::get_height);
  711. ClassDB::bind_method(D_METHOD("set_radial_segments", "segments"), &CylinderMesh::set_radial_segments);
  712. ClassDB::bind_method(D_METHOD("get_radial_segments"), &CylinderMesh::get_radial_segments);
  713. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &CylinderMesh::set_rings);
  714. ClassDB::bind_method(D_METHOD("get_rings"), &CylinderMesh::get_rings);
  715. ADD_PROPERTY(PropertyInfo(Variant::REAL, "top_radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater"), "set_top_radius", "get_top_radius");
  716. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bottom_radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater"), "set_bottom_radius", "get_bottom_radius");
  717. ADD_PROPERTY(PropertyInfo(Variant::REAL, "height", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater"), "set_height", "get_height");
  718. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_radial_segments", "get_radial_segments");
  719. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_rings", "get_rings");
  720. }
  721. void CylinderMesh::set_top_radius(const float p_radius) {
  722. top_radius = p_radius;
  723. _request_update();
  724. }
  725. float CylinderMesh::get_top_radius() const {
  726. return top_radius;
  727. }
  728. void CylinderMesh::set_bottom_radius(const float p_radius) {
  729. bottom_radius = p_radius;
  730. _request_update();
  731. }
  732. float CylinderMesh::get_bottom_radius() const {
  733. return bottom_radius;
  734. }
  735. void CylinderMesh::set_height(const float p_height) {
  736. height = p_height;
  737. _request_update();
  738. }
  739. float CylinderMesh::get_height() const {
  740. return height;
  741. }
  742. void CylinderMesh::set_radial_segments(const int p_segments) {
  743. radial_segments = p_segments > 4 ? p_segments : 4;
  744. _request_update();
  745. }
  746. int CylinderMesh::get_radial_segments() const {
  747. return radial_segments;
  748. }
  749. void CylinderMesh::set_rings(const int p_rings) {
  750. rings = p_rings > 0 ? p_rings : 0;
  751. _request_update();
  752. }
  753. int CylinderMesh::get_rings() const {
  754. return rings;
  755. }
  756. CylinderMesh::CylinderMesh() {
  757. // defaults
  758. top_radius = 1.0;
  759. bottom_radius = 1.0;
  760. height = 2.0;
  761. radial_segments = 64;
  762. rings = 4;
  763. }
  764. /**
  765. PlaneMesh
  766. */
  767. void PlaneMesh::_create_mesh_array(Array &p_arr) const {
  768. int i, j, prevrow, thisrow, point;
  769. float x, z;
  770. Size2 start_pos = size * -0.5;
  771. PoolVector<Vector3> points;
  772. PoolVector<Vector3> normals;
  773. PoolVector<float> tangents;
  774. PoolVector<Vector2> uvs;
  775. PoolVector<int> indices;
  776. point = 0;
  777. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  778. tangents.push_back(m_x); \
  779. tangents.push_back(m_y); \
  780. tangents.push_back(m_z); \
  781. tangents.push_back(m_d);
  782. /* top + bottom */
  783. z = start_pos.y;
  784. thisrow = point;
  785. prevrow = 0;
  786. for (j = 0; j <= (subdivide_d + 1); j++) {
  787. x = start_pos.x;
  788. for (i = 0; i <= (subdivide_w + 1); i++) {
  789. float u = i;
  790. float v = j;
  791. u /= (subdivide_w + 1.0);
  792. v /= (subdivide_d + 1.0);
  793. points.push_back(Vector3(-x, 0.0, -z));
  794. normals.push_back(Vector3(0.0, 1.0, 0.0));
  795. ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
  796. uvs.push_back(Vector2(1.0 - u, 1.0 - v)); /* 1.0 - uv to match orientation with Quad */
  797. point++;
  798. if (i > 0 && j > 0) {
  799. indices.push_back(prevrow + i - 1);
  800. indices.push_back(prevrow + i);
  801. indices.push_back(thisrow + i - 1);
  802. indices.push_back(prevrow + i);
  803. indices.push_back(thisrow + i);
  804. indices.push_back(thisrow + i - 1);
  805. };
  806. x += size.x / (subdivide_w + 1.0);
  807. };
  808. z += size.y / (subdivide_d + 1.0);
  809. prevrow = thisrow;
  810. thisrow = point;
  811. };
  812. p_arr[VS::ARRAY_VERTEX] = points;
  813. p_arr[VS::ARRAY_NORMAL] = normals;
  814. p_arr[VS::ARRAY_TANGENT] = tangents;
  815. p_arr[VS::ARRAY_TEX_UV] = uvs;
  816. p_arr[VS::ARRAY_INDEX] = indices;
  817. }
  818. void PlaneMesh::_bind_methods() {
  819. ClassDB::bind_method(D_METHOD("set_size", "size"), &PlaneMesh::set_size);
  820. ClassDB::bind_method(D_METHOD("get_size"), &PlaneMesh::get_size);
  821. ClassDB::bind_method(D_METHOD("set_subdivide_width", "subdivide"), &PlaneMesh::set_subdivide_width);
  822. ClassDB::bind_method(D_METHOD("get_subdivide_width"), &PlaneMesh::get_subdivide_width);
  823. ClassDB::bind_method(D_METHOD("set_subdivide_depth", "subdivide"), &PlaneMesh::set_subdivide_depth);
  824. ClassDB::bind_method(D_METHOD("get_subdivide_depth"), &PlaneMesh::get_subdivide_depth);
  825. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size");
  826. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_width", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_width", "get_subdivide_width");
  827. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_depth", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_depth", "get_subdivide_depth");
  828. }
  829. void PlaneMesh::set_size(const Size2 &p_size) {
  830. size = p_size;
  831. _request_update();
  832. }
  833. Size2 PlaneMesh::get_size() const {
  834. return size;
  835. }
  836. void PlaneMesh::set_subdivide_width(const int p_divisions) {
  837. subdivide_w = p_divisions > 0 ? p_divisions : 0;
  838. _request_update();
  839. }
  840. int PlaneMesh::get_subdivide_width() const {
  841. return subdivide_w;
  842. }
  843. void PlaneMesh::set_subdivide_depth(const int p_divisions) {
  844. subdivide_d = p_divisions > 0 ? p_divisions : 0;
  845. _request_update();
  846. }
  847. int PlaneMesh::get_subdivide_depth() const {
  848. return subdivide_d;
  849. }
  850. PlaneMesh::PlaneMesh() {
  851. // defaults
  852. size = Size2(2.0, 2.0);
  853. subdivide_w = 0;
  854. subdivide_d = 0;
  855. }
  856. /**
  857. PrismMesh
  858. */
  859. void PrismMesh::_create_mesh_array(Array &p_arr) const {
  860. int i, j, prevrow, thisrow, point;
  861. float x, y, z;
  862. float onethird = 1.0 / 3.0;
  863. float twothirds = 2.0 / 3.0;
  864. Vector3 start_pos = size * -0.5;
  865. // set our bounding box
  866. PoolVector<Vector3> points;
  867. PoolVector<Vector3> normals;
  868. PoolVector<float> tangents;
  869. PoolVector<Vector2> uvs;
  870. PoolVector<int> indices;
  871. point = 0;
  872. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  873. tangents.push_back(m_x); \
  874. tangents.push_back(m_y); \
  875. tangents.push_back(m_z); \
  876. tangents.push_back(m_d);
  877. /* front + back */
  878. y = start_pos.y;
  879. thisrow = point;
  880. prevrow = 0;
  881. for (j = 0; j <= (subdivide_h + 1); j++) {
  882. float scale = (y - start_pos.y) / size.y;
  883. float scaled_size_x = size.x * scale;
  884. float start_x = start_pos.x + (1.0 - scale) * size.x * left_to_right;
  885. float offset_front = (1.0 - scale) * onethird * left_to_right;
  886. float offset_back = (1.0 - scale) * onethird * (1.0 - left_to_right);
  887. x = 0.0;
  888. for (i = 0; i <= (subdivide_w + 1); i++) {
  889. float u = i;
  890. float v = j;
  891. u /= (3.0 * (subdivide_w + 1.0));
  892. v /= (2.0 * (subdivide_h + 1.0));
  893. u *= scale;
  894. /* front */
  895. points.push_back(Vector3(start_x + x, -y, -start_pos.z)); // double negative on the Z!
  896. normals.push_back(Vector3(0.0, 0.0, 1.0));
  897. ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
  898. uvs.push_back(Vector2(offset_front + u, v));
  899. point++;
  900. /* back */
  901. points.push_back(Vector3(start_x + scaled_size_x - x, -y, start_pos.z));
  902. normals.push_back(Vector3(0.0, 0.0, -1.0));
  903. ADD_TANGENT(-1.0, 0.0, 0.0, 1.0);
  904. uvs.push_back(Vector2(twothirds + offset_back + u, v));
  905. point++;
  906. if (i > 0 && j == 1) {
  907. int i2 = i * 2;
  908. /* front */
  909. indices.push_back(prevrow + i2);
  910. indices.push_back(thisrow + i2);
  911. indices.push_back(thisrow + i2 - 2);
  912. /* back */
  913. indices.push_back(prevrow + i2 + 1);
  914. indices.push_back(thisrow + i2 + 1);
  915. indices.push_back(thisrow + i2 - 1);
  916. } else if (i > 0 && j > 0) {
  917. int i2 = i * 2;
  918. /* front */
  919. indices.push_back(prevrow + i2 - 2);
  920. indices.push_back(prevrow + i2);
  921. indices.push_back(thisrow + i2 - 2);
  922. indices.push_back(prevrow + i2);
  923. indices.push_back(thisrow + i2);
  924. indices.push_back(thisrow + i2 - 2);
  925. /* back */
  926. indices.push_back(prevrow + i2 - 1);
  927. indices.push_back(prevrow + i2 + 1);
  928. indices.push_back(thisrow + i2 - 1);
  929. indices.push_back(prevrow + i2 + 1);
  930. indices.push_back(thisrow + i2 + 1);
  931. indices.push_back(thisrow + i2 - 1);
  932. };
  933. x += scale * size.x / (subdivide_w + 1.0);
  934. };
  935. y += size.y / (subdivide_h + 1.0);
  936. prevrow = thisrow;
  937. thisrow = point;
  938. };
  939. /* left + right */
  940. Vector3 normal_left, normal_right;
  941. normal_left = Vector3(-size.y, size.x * left_to_right, 0.0);
  942. normal_right = Vector3(size.y, size.x * left_to_right, 0.0);
  943. normal_left.normalize();
  944. normal_right.normalize();
  945. y = start_pos.y;
  946. thisrow = point;
  947. prevrow = 0;
  948. for (j = 0; j <= (subdivide_h + 1); j++) {
  949. float left, right;
  950. float scale = (y - start_pos.y) / size.y;
  951. left = start_pos.x + (size.x * (1.0 - scale) * left_to_right);
  952. right = left + (size.x * scale);
  953. z = start_pos.z;
  954. for (i = 0; i <= (subdivide_d + 1); i++) {
  955. float u = i;
  956. float v = j;
  957. u /= (3.0 * (subdivide_d + 1.0));
  958. v /= (2.0 * (subdivide_h + 1.0));
  959. /* right */
  960. points.push_back(Vector3(right, -y, -z));
  961. normals.push_back(normal_right);
  962. ADD_TANGENT(0.0, 0.0, -1.0, 1.0);
  963. uvs.push_back(Vector2(onethird + u, v));
  964. point++;
  965. /* left */
  966. points.push_back(Vector3(left, -y, z));
  967. normals.push_back(normal_left);
  968. ADD_TANGENT(0.0, 0.0, 1.0, 1.0);
  969. uvs.push_back(Vector2(u, 0.5 + v));
  970. point++;
  971. if (i > 0 && j > 0) {
  972. int i2 = i * 2;
  973. /* right */
  974. indices.push_back(prevrow + i2 - 2);
  975. indices.push_back(prevrow + i2);
  976. indices.push_back(thisrow + i2 - 2);
  977. indices.push_back(prevrow + i2);
  978. indices.push_back(thisrow + i2);
  979. indices.push_back(thisrow + i2 - 2);
  980. /* left */
  981. indices.push_back(prevrow + i2 - 1);
  982. indices.push_back(prevrow + i2 + 1);
  983. indices.push_back(thisrow + i2 - 1);
  984. indices.push_back(prevrow + i2 + 1);
  985. indices.push_back(thisrow + i2 + 1);
  986. indices.push_back(thisrow + i2 - 1);
  987. };
  988. z += size.z / (subdivide_d + 1.0);
  989. };
  990. y += size.y / (subdivide_h + 1.0);
  991. prevrow = thisrow;
  992. thisrow = point;
  993. };
  994. /* bottom */
  995. z = start_pos.z;
  996. thisrow = point;
  997. prevrow = 0;
  998. for (j = 0; j <= (subdivide_d + 1); j++) {
  999. x = start_pos.x;
  1000. for (i = 0; i <= (subdivide_w + 1); i++) {
  1001. float u = i;
  1002. float v = j;
  1003. u /= (3.0 * (subdivide_w + 1.0));
  1004. v /= (2.0 * (subdivide_d + 1.0));
  1005. /* bottom */
  1006. points.push_back(Vector3(x, start_pos.y, -z));
  1007. normals.push_back(Vector3(0.0, -1.0, 0.0));
  1008. ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
  1009. uvs.push_back(Vector2(twothirds + u, 0.5 + v));
  1010. point++;
  1011. if (i > 0 && j > 0) {
  1012. /* bottom */
  1013. indices.push_back(prevrow + i - 1);
  1014. indices.push_back(prevrow + i);
  1015. indices.push_back(thisrow + i - 1);
  1016. indices.push_back(prevrow + i);
  1017. indices.push_back(thisrow + i);
  1018. indices.push_back(thisrow + i - 1);
  1019. };
  1020. x += size.x / (subdivide_w + 1.0);
  1021. };
  1022. z += size.z / (subdivide_d + 1.0);
  1023. prevrow = thisrow;
  1024. thisrow = point;
  1025. };
  1026. p_arr[VS::ARRAY_VERTEX] = points;
  1027. p_arr[VS::ARRAY_NORMAL] = normals;
  1028. p_arr[VS::ARRAY_TANGENT] = tangents;
  1029. p_arr[VS::ARRAY_TEX_UV] = uvs;
  1030. p_arr[VS::ARRAY_INDEX] = indices;
  1031. }
  1032. void PrismMesh::_bind_methods() {
  1033. ClassDB::bind_method(D_METHOD("set_left_to_right", "left_to_right"), &PrismMesh::set_left_to_right);
  1034. ClassDB::bind_method(D_METHOD("get_left_to_right"), &PrismMesh::get_left_to_right);
  1035. ClassDB::bind_method(D_METHOD("set_size", "size"), &PrismMesh::set_size);
  1036. ClassDB::bind_method(D_METHOD("get_size"), &PrismMesh::get_size);
  1037. ClassDB::bind_method(D_METHOD("set_subdivide_width", "segments"), &PrismMesh::set_subdivide_width);
  1038. ClassDB::bind_method(D_METHOD("get_subdivide_width"), &PrismMesh::get_subdivide_width);
  1039. ClassDB::bind_method(D_METHOD("set_subdivide_height", "segments"), &PrismMesh::set_subdivide_height);
  1040. ClassDB::bind_method(D_METHOD("get_subdivide_height"), &PrismMesh::get_subdivide_height);
  1041. ClassDB::bind_method(D_METHOD("set_subdivide_depth", "segments"), &PrismMesh::set_subdivide_depth);
  1042. ClassDB::bind_method(D_METHOD("get_subdivide_depth"), &PrismMesh::get_subdivide_depth);
  1043. ADD_PROPERTY(PropertyInfo(Variant::REAL, "left_to_right", PROPERTY_HINT_RANGE, "-2.0,2.0,0.1"), "set_left_to_right", "get_left_to_right");
  1044. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size"), "set_size", "get_size");
  1045. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_width", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_width", "get_subdivide_width");
  1046. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_height", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_height", "get_subdivide_height");
  1047. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_depth", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_depth", "get_subdivide_depth");
  1048. }
  1049. void PrismMesh::set_left_to_right(const float p_left_to_right) {
  1050. left_to_right = p_left_to_right;
  1051. _request_update();
  1052. }
  1053. float PrismMesh::get_left_to_right() const {
  1054. return left_to_right;
  1055. }
  1056. void PrismMesh::set_size(const Vector3 &p_size) {
  1057. size = p_size;
  1058. _request_update();
  1059. }
  1060. Vector3 PrismMesh::get_size() const {
  1061. return size;
  1062. }
  1063. void PrismMesh::set_subdivide_width(const int p_divisions) {
  1064. subdivide_w = p_divisions > 0 ? p_divisions : 0;
  1065. _request_update();
  1066. }
  1067. int PrismMesh::get_subdivide_width() const {
  1068. return subdivide_w;
  1069. }
  1070. void PrismMesh::set_subdivide_height(const int p_divisions) {
  1071. subdivide_h = p_divisions > 0 ? p_divisions : 0;
  1072. _request_update();
  1073. }
  1074. int PrismMesh::get_subdivide_height() const {
  1075. return subdivide_h;
  1076. }
  1077. void PrismMesh::set_subdivide_depth(const int p_divisions) {
  1078. subdivide_d = p_divisions > 0 ? p_divisions : 0;
  1079. _request_update();
  1080. }
  1081. int PrismMesh::get_subdivide_depth() const {
  1082. return subdivide_d;
  1083. }
  1084. PrismMesh::PrismMesh() {
  1085. // defaults
  1086. left_to_right = 0.5;
  1087. size = Vector3(2.0, 2.0, 2.0);
  1088. subdivide_w = 0;
  1089. subdivide_h = 0;
  1090. subdivide_d = 0;
  1091. }
  1092. /**
  1093. QuadMesh
  1094. */
  1095. void QuadMesh::_create_mesh_array(Array &p_arr) const {
  1096. PoolVector<Vector3> faces;
  1097. PoolVector<Vector3> normals;
  1098. PoolVector<float> tangents;
  1099. PoolVector<Vector2> uvs;
  1100. faces.resize(6);
  1101. normals.resize(6);
  1102. tangents.resize(6 * 4);
  1103. uvs.resize(6);
  1104. Vector2 _size = Vector2(size.x / 2.0f, size.y / 2.0f);
  1105. Vector3 quad_faces[4] = {
  1106. Vector3(-_size.x, -_size.y, 0),
  1107. Vector3(-_size.x, _size.y, 0),
  1108. Vector3(_size.x, _size.y, 0),
  1109. Vector3(_size.x, -_size.y, 0),
  1110. };
  1111. static const int indices[6] = {
  1112. 0, 1, 2,
  1113. 0, 2, 3
  1114. };
  1115. for (int i = 0; i < 6; i++) {
  1116. int j = indices[i];
  1117. faces.set(i, quad_faces[j]);
  1118. normals.set(i, Vector3(0, 0, 1));
  1119. tangents.set(i * 4 + 0, 1.0);
  1120. tangents.set(i * 4 + 1, 0.0);
  1121. tangents.set(i * 4 + 2, 0.0);
  1122. tangents.set(i * 4 + 3, 1.0);
  1123. static const Vector2 quad_uv[4] = {
  1124. Vector2(0, 1),
  1125. Vector2(0, 0),
  1126. Vector2(1, 0),
  1127. Vector2(1, 1),
  1128. };
  1129. uvs.set(i, quad_uv[j]);
  1130. }
  1131. p_arr[VS::ARRAY_VERTEX] = faces;
  1132. p_arr[VS::ARRAY_NORMAL] = normals;
  1133. p_arr[VS::ARRAY_TANGENT] = tangents;
  1134. p_arr[VS::ARRAY_TEX_UV] = uvs;
  1135. }
  1136. void QuadMesh::_bind_methods() {
  1137. ClassDB::bind_method(D_METHOD("set_size", "size"), &QuadMesh::set_size);
  1138. ClassDB::bind_method(D_METHOD("get_size"), &QuadMesh::get_size);
  1139. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size");
  1140. }
  1141. QuadMesh::QuadMesh() {
  1142. primitive_type = PRIMITIVE_TRIANGLES;
  1143. size = Size2(1.0, 1.0);
  1144. }
  1145. void QuadMesh::set_size(const Size2 &p_size) {
  1146. size = p_size;
  1147. _request_update();
  1148. }
  1149. Size2 QuadMesh::get_size() const {
  1150. return size;
  1151. }
  1152. /**
  1153. SphereMesh
  1154. */
  1155. void SphereMesh::_create_mesh_array(Array &p_arr) const {
  1156. int i, j, prevrow, thisrow, point;
  1157. float x, y, z;
  1158. // set our bounding box
  1159. PoolVector<Vector3> points;
  1160. PoolVector<Vector3> normals;
  1161. PoolVector<float> tangents;
  1162. PoolVector<Vector2> uvs;
  1163. PoolVector<int> indices;
  1164. point = 0;
  1165. #define ADD_TANGENT(m_x, m_y, m_z, m_d) \
  1166. tangents.push_back(m_x); \
  1167. tangents.push_back(m_y); \
  1168. tangents.push_back(m_z); \
  1169. tangents.push_back(m_d);
  1170. thisrow = 0;
  1171. prevrow = 0;
  1172. for (j = 0; j <= (rings + 1); j++) {
  1173. float v = j;
  1174. float w;
  1175. v /= (rings + 1);
  1176. w = sin(Math_PI * v);
  1177. y = height * (is_hemisphere ? 1.0 : 0.5) * cos(Math_PI * v);
  1178. for (i = 0; i <= radial_segments; i++) {
  1179. float u = i;
  1180. u /= radial_segments;
  1181. x = sin(u * (Math_PI * 2.0));
  1182. z = cos(u * (Math_PI * 2.0));
  1183. if (is_hemisphere && y < 0.0) {
  1184. points.push_back(Vector3(x * radius * w, 0.0, z * radius * w));
  1185. normals.push_back(Vector3(0.0, -1.0, 0.0));
  1186. } else {
  1187. Vector3 p = Vector3(x * radius * w, y, z * radius * w);
  1188. points.push_back(p);
  1189. normals.push_back(p.normalized());
  1190. };
  1191. ADD_TANGENT(z, 0.0, -x, 1.0)
  1192. uvs.push_back(Vector2(u, v));
  1193. point++;
  1194. if (i > 0 && j > 0) {
  1195. indices.push_back(prevrow + i - 1);
  1196. indices.push_back(prevrow + i);
  1197. indices.push_back(thisrow + i - 1);
  1198. indices.push_back(prevrow + i);
  1199. indices.push_back(thisrow + i);
  1200. indices.push_back(thisrow + i - 1);
  1201. };
  1202. };
  1203. prevrow = thisrow;
  1204. thisrow = point;
  1205. };
  1206. p_arr[VS::ARRAY_VERTEX] = points;
  1207. p_arr[VS::ARRAY_NORMAL] = normals;
  1208. p_arr[VS::ARRAY_TANGENT] = tangents;
  1209. p_arr[VS::ARRAY_TEX_UV] = uvs;
  1210. p_arr[VS::ARRAY_INDEX] = indices;
  1211. }
  1212. void SphereMesh::_bind_methods() {
  1213. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &SphereMesh::set_radius);
  1214. ClassDB::bind_method(D_METHOD("get_radius"), &SphereMesh::get_radius);
  1215. ClassDB::bind_method(D_METHOD("set_height", "height"), &SphereMesh::set_height);
  1216. ClassDB::bind_method(D_METHOD("get_height"), &SphereMesh::get_height);
  1217. ClassDB::bind_method(D_METHOD("set_radial_segments", "radial_segments"), &SphereMesh::set_radial_segments);
  1218. ClassDB::bind_method(D_METHOD("get_radial_segments"), &SphereMesh::get_radial_segments);
  1219. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &SphereMesh::set_rings);
  1220. ClassDB::bind_method(D_METHOD("get_rings"), &SphereMesh::get_rings);
  1221. ClassDB::bind_method(D_METHOD("set_is_hemisphere", "is_hemisphere"), &SphereMesh::set_is_hemisphere);
  1222. ClassDB::bind_method(D_METHOD("get_is_hemisphere"), &SphereMesh::get_is_hemisphere);
  1223. ADD_PROPERTY(PropertyInfo(Variant::REAL, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater"), "set_radius", "get_radius");
  1224. ADD_PROPERTY(PropertyInfo(Variant::REAL, "height", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater"), "set_height", "get_height");
  1225. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_radial_segments", "get_radial_segments");
  1226. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_rings", "get_rings");
  1227. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "is_hemisphere"), "set_is_hemisphere", "get_is_hemisphere");
  1228. }
  1229. void SphereMesh::set_radius(const float p_radius) {
  1230. radius = p_radius;
  1231. _request_update();
  1232. }
  1233. float SphereMesh::get_radius() const {
  1234. return radius;
  1235. }
  1236. void SphereMesh::set_height(const float p_height) {
  1237. height = p_height;
  1238. _request_update();
  1239. }
  1240. float SphereMesh::get_height() const {
  1241. return height;
  1242. }
  1243. void SphereMesh::set_radial_segments(const int p_radial_segments) {
  1244. radial_segments = p_radial_segments > 4 ? p_radial_segments : 4;
  1245. _request_update();
  1246. }
  1247. int SphereMesh::get_radial_segments() const {
  1248. return radial_segments;
  1249. }
  1250. void SphereMesh::set_rings(const int p_rings) {
  1251. rings = p_rings > 1 ? p_rings : 1;
  1252. _request_update();
  1253. }
  1254. int SphereMesh::get_rings() const {
  1255. return rings;
  1256. }
  1257. void SphereMesh::set_is_hemisphere(const bool p_is_hemisphere) {
  1258. is_hemisphere = p_is_hemisphere;
  1259. _request_update();
  1260. }
  1261. bool SphereMesh::get_is_hemisphere() const {
  1262. return is_hemisphere;
  1263. }
  1264. SphereMesh::SphereMesh() {
  1265. // defaults
  1266. radius = 1.0;
  1267. height = 2.0;
  1268. radial_segments = 64;
  1269. rings = 32;
  1270. is_hemisphere = false;
  1271. }