primitive_meshes.cpp 43 KB

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