visual_server.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. /*************************************************************************/
  2. /* visual_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "visual_server.h"
  31. #include "global_config.h"
  32. #include "method_bind_ext.inc"
  33. VisualServer *VisualServer::singleton = NULL;
  34. VisualServer *(*VisualServer::create_func)() = NULL;
  35. VisualServer *VisualServer::get_singleton() {
  36. return singleton;
  37. }
  38. PoolVector<String> VisualServer::_shader_get_param_list(RID p_shader) const {
  39. //remove at some point
  40. PoolVector<String> pl;
  41. #if 0
  42. List<StringName> params;
  43. shader_get_param_list(p_shader,&params);
  44. for(List<StringName>::Element *E=params.front();E;E=E->next()) {
  45. pl.push_back(E->get());
  46. }
  47. #endif
  48. return pl;
  49. }
  50. VisualServer *VisualServer::create() {
  51. ERR_FAIL_COND_V(singleton, NULL);
  52. if (create_func)
  53. return create_func();
  54. return NULL;
  55. }
  56. RID VisualServer::texture_create_from_image(const Ref<Image> &p_image, uint32_t p_flags) {
  57. ERR_FAIL_COND_V(!p_image.is_valid(), RID());
  58. RID texture = texture_create();
  59. texture_allocate(texture, p_image->get_width(), p_image->get_height(), p_image->get_format(), p_flags); //if it has mipmaps, use, else generate
  60. ERR_FAIL_COND_V(!texture.is_valid(), texture);
  61. texture_set_data(texture, p_image);
  62. return texture;
  63. }
  64. RID VisualServer::get_test_texture() {
  65. if (test_texture.is_valid()) {
  66. return test_texture;
  67. };
  68. #define TEST_TEXTURE_SIZE 256
  69. PoolVector<uint8_t> test_data;
  70. test_data.resize(TEST_TEXTURE_SIZE * TEST_TEXTURE_SIZE * 3);
  71. {
  72. PoolVector<uint8_t>::Write w = test_data.write();
  73. for (int x = 0; x < TEST_TEXTURE_SIZE; x++) {
  74. for (int y = 0; y < TEST_TEXTURE_SIZE; y++) {
  75. Color c;
  76. int r = 255 - (x + y) / 2;
  77. if ((x % (TEST_TEXTURE_SIZE / 8)) < 2 || (y % (TEST_TEXTURE_SIZE / 8)) < 2) {
  78. c.r = y;
  79. c.g = r;
  80. c.b = x;
  81. } else {
  82. c.r = r;
  83. c.g = x;
  84. c.b = y;
  85. }
  86. w[(y * TEST_TEXTURE_SIZE + x) * 3 + 0] = uint8_t(CLAMP(c.r * 255, 0, 255));
  87. w[(y * TEST_TEXTURE_SIZE + x) * 3 + 1] = uint8_t(CLAMP(c.g * 255, 0, 255));
  88. w[(y * TEST_TEXTURE_SIZE + x) * 3 + 2] = uint8_t(CLAMP(c.b * 255, 0, 255));
  89. }
  90. }
  91. }
  92. Ref<Image> data = memnew(Image(TEST_TEXTURE_SIZE, TEST_TEXTURE_SIZE, false, Image::FORMAT_RGB8, test_data));
  93. test_texture = texture_create_from_image(data);
  94. return test_texture;
  95. }
  96. void VisualServer::_free_internal_rids() {
  97. if (test_texture.is_valid())
  98. free(test_texture);
  99. if (white_texture.is_valid())
  100. free(white_texture);
  101. if (test_material.is_valid())
  102. free(test_material);
  103. for (int i = 0; i < 16; i++) {
  104. if (material_2d[i].is_valid())
  105. free(material_2d[i]);
  106. }
  107. }
  108. RID VisualServer::_make_test_cube() {
  109. PoolVector<Vector3> vertices;
  110. PoolVector<Vector3> normals;
  111. PoolVector<float> tangents;
  112. PoolVector<Vector3> uvs;
  113. int vtx_idx = 0;
  114. #define ADD_VTX(m_idx) \
  115. vertices.push_back(face_points[m_idx]); \
  116. normals.push_back(normal_points[m_idx]); \
  117. tangents.push_back(normal_points[m_idx][1]); \
  118. tangents.push_back(normal_points[m_idx][2]); \
  119. tangents.push_back(normal_points[m_idx][0]); \
  120. tangents.push_back(1.0); \
  121. uvs.push_back(Vector3(uv_points[m_idx * 2 + 0], uv_points[m_idx * 2 + 1], 0)); \
  122. vtx_idx++;
  123. for (int i = 0; i < 6; i++) {
  124. Vector3 face_points[4];
  125. Vector3 normal_points[4];
  126. float uv_points[8] = { 0, 0, 0, 1, 1, 1, 1, 0 };
  127. for (int j = 0; j < 4; j++) {
  128. float v[3];
  129. v[0] = 1.0;
  130. v[1] = 1 - 2 * ((j >> 1) & 1);
  131. v[2] = v[1] * (1 - 2 * (j & 1));
  132. for (int k = 0; k < 3; k++) {
  133. if (i < 3)
  134. face_points[j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  135. else
  136. face_points[3 - j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  137. }
  138. normal_points[j] = Vector3();
  139. normal_points[j][i % 3] = (i >= 3 ? -1 : 1);
  140. }
  141. //tri 1
  142. ADD_VTX(0);
  143. ADD_VTX(1);
  144. ADD_VTX(2);
  145. //tri 2
  146. ADD_VTX(2);
  147. ADD_VTX(3);
  148. ADD_VTX(0);
  149. }
  150. RID test_cube = mesh_create();
  151. Array d;
  152. d.resize(VS::ARRAY_MAX);
  153. d[VisualServer::ARRAY_NORMAL] = normals;
  154. d[VisualServer::ARRAY_TANGENT] = tangents;
  155. d[VisualServer::ARRAY_TEX_UV] = uvs;
  156. d[VisualServer::ARRAY_VERTEX] = vertices;
  157. PoolVector<int> indices;
  158. indices.resize(vertices.size());
  159. for (int i = 0; i < vertices.size(); i++)
  160. indices.set(i, i);
  161. d[VisualServer::ARRAY_INDEX] = indices;
  162. mesh_add_surface_from_arrays(test_cube, PRIMITIVE_TRIANGLES, d);
  163. /*
  164. test_material = fixed_material_create();
  165. //material_set_flag(material, MATERIAL_FLAG_BILLBOARD_TOGGLE,true);
  166. fixed_material_set_texture( test_material, FIXED_MATERIAL_PARAM_DIFFUSE, get_test_texture() );
  167. fixed_material_set_param( test_material, FIXED_MATERIAL_PARAM_SPECULAR_EXP, 70 );
  168. fixed_material_set_param( test_material, FIXED_MATERIAL_PARAM_EMISSION, Color(0.2,0.2,0.2) );
  169. fixed_material_set_param( test_material, FIXED_MATERIAL_PARAM_DIFFUSE, Color(1, 1, 1) );
  170. fixed_material_set_param( test_material, FIXED_MATERIAL_PARAM_SPECULAR, Color(1,1,1) );
  171. */
  172. mesh_surface_set_material(test_cube, 0, test_material);
  173. return test_cube;
  174. }
  175. RID VisualServer::make_sphere_mesh(int p_lats, int p_lons, float p_radius) {
  176. PoolVector<Vector3> vertices;
  177. PoolVector<Vector3> normals;
  178. for (int i = 1; i <= p_lats; i++) {
  179. double lat0 = Math_PI * (-0.5 + (double)(i - 1) / p_lats);
  180. double z0 = Math::sin(lat0);
  181. double zr0 = Math::cos(lat0);
  182. double lat1 = Math_PI * (-0.5 + (double)i / p_lats);
  183. double z1 = Math::sin(lat1);
  184. double zr1 = Math::cos(lat1);
  185. for (int j = p_lons; j >= 1; j--) {
  186. double lng0 = 2 * Math_PI * (double)(j - 1) / p_lons;
  187. double x0 = Math::cos(lng0);
  188. double y0 = Math::sin(lng0);
  189. double lng1 = 2 * Math_PI * (double)(j) / p_lons;
  190. double x1 = Math::cos(lng1);
  191. double y1 = Math::sin(lng1);
  192. Vector3 v[4] = {
  193. Vector3(x1 * zr0, z0, y1 * zr0),
  194. Vector3(x1 * zr1, z1, y1 * zr1),
  195. Vector3(x0 * zr1, z1, y0 * zr1),
  196. Vector3(x0 * zr0, z0, y0 * zr0)
  197. };
  198. #define ADD_POINT(m_idx) \
  199. normals.push_back(v[m_idx]); \
  200. vertices.push_back(v[m_idx] * p_radius);
  201. ADD_POINT(0);
  202. ADD_POINT(1);
  203. ADD_POINT(2);
  204. ADD_POINT(2);
  205. ADD_POINT(3);
  206. ADD_POINT(0);
  207. }
  208. }
  209. RID mesh = mesh_create();
  210. Array d;
  211. d.resize(VS::ARRAY_MAX);
  212. d[ARRAY_VERTEX] = vertices;
  213. d[ARRAY_NORMAL] = normals;
  214. mesh_add_surface_from_arrays(mesh, PRIMITIVE_TRIANGLES, d);
  215. return mesh;
  216. }
  217. RID VisualServer::material_2d_get(bool p_shaded, bool p_transparent, bool p_cut_alpha, bool p_opaque_prepass) {
  218. int version = 0;
  219. if (p_shaded)
  220. version = 1;
  221. if (p_transparent)
  222. version |= 2;
  223. if (p_cut_alpha)
  224. version |= 4;
  225. if (p_opaque_prepass)
  226. version |= 8;
  227. if (material_2d[version].is_valid())
  228. return material_2d[version];
  229. //not valid, make
  230. /* material_2d[version]=fixed_material_create();
  231. fixed_material_set_flag(material_2d[version],FIXED_MATERIAL_FLAG_USE_ALPHA,p_transparent);
  232. fixed_material_set_flag(material_2d[version],FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY,true);
  233. fixed_material_set_flag(material_2d[version],FIXED_MATERIAL_FLAG_DISCARD_ALPHA,p_cut_alpha);
  234. material_set_flag(material_2d[version],MATERIAL_FLAG_UNSHADED,!p_shaded);
  235. material_set_flag(material_2d[version],MATERIAL_FLAG_DOUBLE_SIDED,true);
  236. material_set_depth_draw_mode(material_2d[version],p_opaque_prepass?MATERIAL_DEPTH_DRAW_OPAQUE_PRE_PASS_ALPHA:MATERIAL_DEPTH_DRAW_OPAQUE_ONLY);
  237. fixed_material_set_texture(material_2d[version],FIXED_MATERIAL_PARAM_DIFFUSE,get_white_texture());
  238. //material cut alpha?*/
  239. return material_2d[version];
  240. }
  241. RID VisualServer::get_white_texture() {
  242. if (white_texture.is_valid())
  243. return white_texture;
  244. PoolVector<uint8_t> wt;
  245. wt.resize(16 * 3);
  246. {
  247. PoolVector<uint8_t>::Write w = wt.write();
  248. for (int i = 0; i < 16 * 3; i++)
  249. w[i] = 255;
  250. }
  251. Ref<Image> white = memnew(Image(4, 4, 0, Image::FORMAT_RGB8, wt));
  252. white_texture = texture_create();
  253. texture_allocate(white_texture, 4, 4, Image::FORMAT_RGB8);
  254. texture_set_data(white_texture, white);
  255. return white_texture;
  256. }
  257. Error VisualServer::_surface_set_data(Array p_arrays, uint32_t p_format, uint32_t *p_offsets, uint32_t p_stride, PoolVector<uint8_t> &r_vertex_array, int p_vertex_array_len, PoolVector<uint8_t> &r_index_array, int p_index_array_len, Rect3 &r_aabb, Vector<Rect3> r_bone_aabb) {
  258. PoolVector<uint8_t>::Write vw = r_vertex_array.write();
  259. PoolVector<uint8_t>::Write iw;
  260. if (r_index_array.size()) {
  261. print_line("elements: " + itos(r_index_array.size()));
  262. iw = r_index_array.write();
  263. }
  264. int max_bone = 0;
  265. for (int ai = 0; ai < VS::ARRAY_MAX; ai++) {
  266. if (!(p_format & (1 << ai))) // no array
  267. continue;
  268. switch (ai) {
  269. case VS::ARRAY_VERTEX: {
  270. if (p_format & VS::ARRAY_FLAG_USE_2D_VERTICES) {
  271. PoolVector<Vector2> array = p_arrays[ai];
  272. ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
  273. PoolVector<Vector2>::Read read = array.read();
  274. const Vector2 *src = read.ptr();
  275. // setting vertices means regenerating the AABB
  276. Rect2 aabb;
  277. if (p_format & ARRAY_COMPRESS_VERTEX) {
  278. for (int i = 0; i < p_vertex_array_len; i++) {
  279. uint16_t vector[2] = { Math::make_half_float(src[i].x), Math::make_half_float(src[i].y) };
  280. copymem(&vw[p_offsets[ai] + i * p_stride], vector, sizeof(uint16_t) * 2);
  281. if (i == 0) {
  282. aabb = Rect2(src[i], Vector2());
  283. } else {
  284. aabb.expand_to(src[i]);
  285. }
  286. }
  287. } else {
  288. for (int i = 0; i < p_vertex_array_len; i++) {
  289. float vector[2] = { src[i].x, src[i].y };
  290. copymem(&vw[p_offsets[ai] + i * p_stride], vector, sizeof(float) * 2);
  291. if (i == 0) {
  292. aabb = Rect2(src[i], Vector2());
  293. } else {
  294. aabb.expand_to(src[i]);
  295. }
  296. }
  297. }
  298. r_aabb = Rect3(Vector3(aabb.position.x, aabb.position.y, 0), Vector3(aabb.size.x, aabb.size.y, 0));
  299. } else {
  300. PoolVector<Vector3> array = p_arrays[ai];
  301. ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
  302. PoolVector<Vector3>::Read read = array.read();
  303. const Vector3 *src = read.ptr();
  304. // setting vertices means regenerating the AABB
  305. Rect3 aabb;
  306. if (p_format & ARRAY_COMPRESS_VERTEX) {
  307. for (int i = 0; i < p_vertex_array_len; i++) {
  308. uint16_t vector[4] = { Math::make_half_float(src[i].x), Math::make_half_float(src[i].y), Math::make_half_float(src[i].z), Math::make_half_float(1.0) };
  309. copymem(&vw[p_offsets[ai] + i * p_stride], vector, sizeof(uint16_t) * 4);
  310. if (i == 0) {
  311. aabb = Rect3(src[i], Vector3());
  312. } else {
  313. aabb.expand_to(src[i]);
  314. }
  315. }
  316. } else {
  317. for (int i = 0; i < p_vertex_array_len; i++) {
  318. float vector[3] = { src[i].x, src[i].y, src[i].z };
  319. copymem(&vw[p_offsets[ai] + i * p_stride], vector, sizeof(float) * 3);
  320. if (i == 0) {
  321. aabb = Rect3(src[i], Vector3());
  322. } else {
  323. aabb.expand_to(src[i]);
  324. }
  325. }
  326. }
  327. r_aabb = aabb;
  328. }
  329. } break;
  330. case VS::ARRAY_NORMAL: {
  331. ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::POOL_VECTOR3_ARRAY, ERR_INVALID_PARAMETER);
  332. PoolVector<Vector3> array = p_arrays[ai];
  333. ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
  334. PoolVector<Vector3>::Read read = array.read();
  335. const Vector3 *src = read.ptr();
  336. // setting vertices means regenerating the AABB
  337. if (p_format & ARRAY_COMPRESS_NORMAL) {
  338. for (int i = 0; i < p_vertex_array_len; i++) {
  339. int8_t vector[4] = {
  340. CLAMP(src[i].x * 127, -128, 127),
  341. CLAMP(src[i].y * 127, -128, 127),
  342. CLAMP(src[i].z * 127, -128, 127),
  343. 0,
  344. };
  345. copymem(&vw[p_offsets[ai] + i * p_stride], vector, 4);
  346. }
  347. } else {
  348. for (int i = 0; i < p_vertex_array_len; i++) {
  349. float vector[3] = { src[i].x, src[i].y, src[i].z };
  350. copymem(&vw[p_offsets[ai] + i * p_stride], vector, 3 * 4);
  351. }
  352. }
  353. } break;
  354. case VS::ARRAY_TANGENT: {
  355. ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::POOL_REAL_ARRAY, ERR_INVALID_PARAMETER);
  356. PoolVector<real_t> array = p_arrays[ai];
  357. ERR_FAIL_COND_V(array.size() != p_vertex_array_len * 4, ERR_INVALID_PARAMETER);
  358. PoolVector<real_t>::Read read = array.read();
  359. const real_t *src = read.ptr();
  360. if (p_format & ARRAY_COMPRESS_TANGENT) {
  361. for (int i = 0; i < p_vertex_array_len; i++) {
  362. uint8_t xyzw[4] = {
  363. CLAMP(src[i * 4 + 0] * 127, -128, 127),
  364. CLAMP(src[i * 4 + 1] * 127, -128, 127),
  365. CLAMP(src[i * 4 + 2] * 127, -128, 127),
  366. CLAMP(src[i * 4 + 3] * 127, -128, 127)
  367. };
  368. copymem(&vw[p_offsets[ai] + i * p_stride], xyzw, 4);
  369. }
  370. } else {
  371. for (int i = 0; i < p_vertex_array_len; i++) {
  372. float xyzw[4] = {
  373. src[i * 4 + 0],
  374. src[i * 4 + 1],
  375. src[i * 4 + 2],
  376. src[i * 4 + 3]
  377. };
  378. copymem(&vw[p_offsets[ai] + i * p_stride], xyzw, 4 * 4);
  379. }
  380. }
  381. } break;
  382. case VS::ARRAY_COLOR: {
  383. ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::POOL_COLOR_ARRAY, ERR_INVALID_PARAMETER);
  384. PoolVector<Color> array = p_arrays[ai];
  385. ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
  386. PoolVector<Color>::Read read = array.read();
  387. const Color *src = read.ptr();
  388. if (p_format & ARRAY_COMPRESS_COLOR) {
  389. for (int i = 0; i < p_vertex_array_len; i++) {
  390. uint8_t colors[4];
  391. for (int j = 0; j < 4; j++) {
  392. colors[j] = CLAMP(int((src[i][j]) * 255.0), 0, 255);
  393. }
  394. copymem(&vw[p_offsets[ai] + i * p_stride], colors, 4);
  395. }
  396. } else {
  397. for (int i = 0; i < p_vertex_array_len; i++) {
  398. copymem(&vw[p_offsets[ai] + i * p_stride], &src[i], 4 * 4);
  399. }
  400. }
  401. } break;
  402. case VS::ARRAY_TEX_UV: {
  403. ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::POOL_VECTOR3_ARRAY && p_arrays[ai].get_type() != Variant::POOL_VECTOR2_ARRAY, ERR_INVALID_PARAMETER);
  404. PoolVector<Vector2> array = p_arrays[ai];
  405. ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
  406. PoolVector<Vector2>::Read read = array.read();
  407. const Vector2 *src = read.ptr();
  408. if (p_format & ARRAY_COMPRESS_TEX_UV) {
  409. for (int i = 0; i < p_vertex_array_len; i++) {
  410. uint16_t uv[2] = { Math::make_half_float(src[i].x), Math::make_half_float(src[i].y) };
  411. copymem(&vw[p_offsets[ai] + i * p_stride], uv, 2 * 2);
  412. }
  413. } else {
  414. for (int i = 0; i < p_vertex_array_len; i++) {
  415. float uv[2] = { src[i].x, src[i].y };
  416. copymem(&vw[p_offsets[ai] + i * p_stride], uv, 2 * 4);
  417. }
  418. }
  419. } break;
  420. case VS::ARRAY_TEX_UV2: {
  421. ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::POOL_VECTOR3_ARRAY && p_arrays[ai].get_type() != Variant::POOL_VECTOR2_ARRAY, ERR_INVALID_PARAMETER);
  422. PoolVector<Vector2> array = p_arrays[ai];
  423. ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
  424. PoolVector<Vector2>::Read read = array.read();
  425. const Vector2 *src = read.ptr();
  426. if (p_format & ARRAY_COMPRESS_TEX_UV2) {
  427. for (int i = 0; i < p_vertex_array_len; i++) {
  428. uint16_t uv[2] = { Math::make_half_float(src[i].x), Math::make_half_float(src[i].y) };
  429. copymem(&vw[p_offsets[ai] + i * p_stride], uv, 2 * 2);
  430. }
  431. } else {
  432. for (int i = 0; i < p_vertex_array_len; i++) {
  433. float uv[2] = { src[i].x, src[i].y };
  434. copymem(&vw[p_offsets[ai] + i * p_stride], uv, 2 * 4);
  435. }
  436. }
  437. } break;
  438. case VS::ARRAY_WEIGHTS: {
  439. ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::POOL_REAL_ARRAY, ERR_INVALID_PARAMETER);
  440. PoolVector<real_t> array = p_arrays[ai];
  441. ERR_FAIL_COND_V(array.size() != p_vertex_array_len * VS::ARRAY_WEIGHTS_SIZE, ERR_INVALID_PARAMETER);
  442. PoolVector<real_t>::Read read = array.read();
  443. const real_t *src = read.ptr();
  444. if (p_format & ARRAY_COMPRESS_WEIGHTS) {
  445. for (int i = 0; i < p_vertex_array_len; i++) {
  446. uint16_t data[VS::ARRAY_WEIGHTS_SIZE];
  447. for (int j = 0; j < VS::ARRAY_WEIGHTS_SIZE; j++) {
  448. data[j] = CLAMP(src[i * VS::ARRAY_WEIGHTS_SIZE + j] * 65535, 0, 65535);
  449. }
  450. copymem(&vw[p_offsets[ai] + i * p_stride], data, 2 * 4);
  451. }
  452. } else {
  453. for (int i = 0; i < p_vertex_array_len; i++) {
  454. float data[VS::ARRAY_WEIGHTS_SIZE];
  455. for (int j = 0; j < VS::ARRAY_WEIGHTS_SIZE; j++) {
  456. data[j] = src[i * VS::ARRAY_WEIGHTS_SIZE + j];
  457. }
  458. copymem(&vw[p_offsets[ai] + i * p_stride], data, 4 * 4);
  459. }
  460. }
  461. } break;
  462. case VS::ARRAY_BONES: {
  463. ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::POOL_INT_ARRAY && p_arrays[ai].get_type() != Variant::POOL_REAL_ARRAY, ERR_INVALID_PARAMETER);
  464. PoolVector<int> array = p_arrays[ai];
  465. ERR_FAIL_COND_V(array.size() != p_vertex_array_len * VS::ARRAY_WEIGHTS_SIZE, ERR_INVALID_PARAMETER);
  466. PoolVector<int>::Read read = array.read();
  467. const int *src = read.ptr();
  468. if (!(p_format & ARRAY_FLAG_USE_16_BIT_BONES)) {
  469. for (int i = 0; i < p_vertex_array_len; i++) {
  470. uint8_t data[VS::ARRAY_WEIGHTS_SIZE];
  471. for (int j = 0; j < VS::ARRAY_WEIGHTS_SIZE; j++) {
  472. data[j] = CLAMP(src[i * VS::ARRAY_WEIGHTS_SIZE + j], 0, 255);
  473. max_bone = MAX(data[j], max_bone);
  474. }
  475. copymem(&vw[p_offsets[ai] + i * p_stride], data, 4);
  476. }
  477. } else {
  478. for (int i = 0; i < p_vertex_array_len; i++) {
  479. uint16_t data[VS::ARRAY_WEIGHTS_SIZE];
  480. for (int j = 0; j < VS::ARRAY_WEIGHTS_SIZE; j++) {
  481. data[j] = src[i * VS::ARRAY_WEIGHTS_SIZE + j];
  482. max_bone = MAX(data[j], max_bone);
  483. }
  484. copymem(&vw[p_offsets[ai] + i * p_stride], data, 2 * 4);
  485. }
  486. }
  487. } break;
  488. case VS::ARRAY_INDEX: {
  489. ERR_FAIL_COND_V(p_index_array_len <= 0, ERR_INVALID_DATA);
  490. ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::POOL_INT_ARRAY, ERR_INVALID_PARAMETER);
  491. PoolVector<int> indices = p_arrays[ai];
  492. ERR_FAIL_COND_V(indices.size() == 0, ERR_INVALID_PARAMETER);
  493. ERR_FAIL_COND_V(indices.size() != p_index_array_len, ERR_INVALID_PARAMETER);
  494. /* determine wether using 16 or 32 bits indices */
  495. PoolVector<int>::Read read = indices.read();
  496. const int *src = read.ptr();
  497. for (int i = 0; i < p_index_array_len; i++) {
  498. if (p_vertex_array_len < (1 << 16)) {
  499. uint16_t v = src[i];
  500. copymem(&iw[i * 2], &v, 2);
  501. } else {
  502. uint32_t v = src[i];
  503. copymem(&iw[i * 4], &v, 4);
  504. }
  505. }
  506. } break;
  507. default: {
  508. ERR_FAIL_V(ERR_INVALID_DATA);
  509. }
  510. }
  511. }
  512. if (p_format & VS::ARRAY_FORMAT_BONES) {
  513. //create AABBs for each detected bone
  514. int total_bones = max_bone + 1;
  515. bool first = r_bone_aabb.size() == 0;
  516. r_bone_aabb.resize(total_bones);
  517. if (first) {
  518. for (int i = 0; i < total_bones; i++) {
  519. r_bone_aabb[i].size == Vector3(-1, -1, -1); //negative means unused
  520. }
  521. }
  522. PoolVector<Vector3> vertices = p_arrays[VS::ARRAY_VERTEX];
  523. PoolVector<int> bones = p_arrays[VS::ARRAY_BONES];
  524. PoolVector<float> weights = p_arrays[VS::ARRAY_WEIGHTS];
  525. bool any_valid = false;
  526. if (vertices.size() && bones.size() == vertices.size() * 4 && weights.size() == bones.size()) {
  527. int vs = vertices.size();
  528. PoolVector<Vector3>::Read rv = vertices.read();
  529. PoolVector<int>::Read rb = bones.read();
  530. PoolVector<float>::Read rw = weights.read();
  531. Rect3 *bptr = r_bone_aabb.ptr();
  532. for (int i = 0; i < vs; i++) {
  533. Vector3 v = rv[i];
  534. for (int j = 0; j < 4; j++) {
  535. int idx = rb[i * 4 + j];
  536. float w = rw[i * 4 + j];
  537. if (w == 0)
  538. continue; //break;
  539. ERR_FAIL_INDEX_V(idx, total_bones, ERR_INVALID_DATA);
  540. if (bptr->size.x < 0) {
  541. //first
  542. bptr[idx] = Rect3();
  543. bptr[idx].position = v;
  544. any_valid = true;
  545. } else {
  546. bptr[idx].expand_to(v);
  547. }
  548. }
  549. }
  550. }
  551. if (!any_valid && first) {
  552. r_bone_aabb.clear();
  553. }
  554. }
  555. return OK;
  556. }
  557. void VisualServer::mesh_add_surface_from_arrays(RID p_mesh, PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes, uint32_t p_compress_format) {
  558. ERR_FAIL_INDEX(p_primitive, VS::PRIMITIVE_MAX);
  559. ERR_FAIL_COND(p_arrays.size() != VS::ARRAY_MAX);
  560. uint32_t format = 0;
  561. // validation
  562. int index_array_len = 0;
  563. int array_len = 0;
  564. for (int i = 0; i < p_arrays.size(); i++) {
  565. if (p_arrays[i].get_type() == Variant::NIL)
  566. continue;
  567. format |= (1 << i);
  568. if (i == VS::ARRAY_VERTEX) {
  569. Variant var = p_arrays[i];
  570. switch (var.get_type()) {
  571. case Variant::POOL_VECTOR2_ARRAY: {
  572. PoolVector<Vector2> v2 = var;
  573. array_len = v2.size();
  574. } break;
  575. case Variant::POOL_VECTOR3_ARRAY: {
  576. PoolVector<Vector3> v3 = var;
  577. array_len = v3.size();
  578. } break;
  579. default: {
  580. Array v = var;
  581. array_len = v.size();
  582. } break;
  583. }
  584. array_len = PoolVector3Array(p_arrays[i]).size();
  585. ERR_FAIL_COND(array_len == 0);
  586. } else if (i == VS::ARRAY_INDEX) {
  587. index_array_len = PoolIntArray(p_arrays[i]).size();
  588. }
  589. }
  590. ERR_FAIL_COND((format & VS::ARRAY_FORMAT_VERTEX) == 0); // mandatory
  591. if (p_blend_shapes.size()) {
  592. //validate format for morphs
  593. for (int i = 0; i < p_blend_shapes.size(); i++) {
  594. uint32_t bsformat = 0;
  595. Array arr = p_blend_shapes[i];
  596. for (int j = 0; j < arr.size(); j++) {
  597. if (arr[j].get_type() != Variant::NIL)
  598. bsformat |= (1 << j);
  599. }
  600. ERR_FAIL_COND((bsformat) != (format & (VS::ARRAY_FORMAT_INDEX - 1)));
  601. }
  602. }
  603. uint32_t offsets[VS::ARRAY_MAX];
  604. int total_elem_size = 0;
  605. for (int i = 0; i < VS::ARRAY_MAX; i++) {
  606. offsets[i] = 0; //reset
  607. if (!(format & (1 << i))) // no array
  608. continue;
  609. int elem_size = 0;
  610. switch (i) {
  611. case VS::ARRAY_VERTEX: {
  612. Variant arr = p_arrays[0];
  613. if (arr.get_type() == Variant::POOL_VECTOR2_ARRAY) {
  614. elem_size = 2;
  615. p_compress_format |= ARRAY_FLAG_USE_2D_VERTICES;
  616. } else if (arr.get_type() == Variant::POOL_VECTOR3_ARRAY) {
  617. p_compress_format &= ~ARRAY_FLAG_USE_2D_VERTICES;
  618. elem_size = 3;
  619. } else {
  620. elem_size = (p_compress_format & ARRAY_FLAG_USE_2D_VERTICES) ? 2 : 3;
  621. }
  622. if (p_compress_format & ARRAY_COMPRESS_VERTEX) {
  623. elem_size *= sizeof(int16_t);
  624. } else {
  625. elem_size *= sizeof(float);
  626. }
  627. if (elem_size == 6) {
  628. //had to pad
  629. elem_size = 8;
  630. }
  631. } break;
  632. case VS::ARRAY_NORMAL: {
  633. if (p_compress_format & ARRAY_COMPRESS_NORMAL) {
  634. elem_size = sizeof(uint32_t);
  635. } else {
  636. elem_size = sizeof(float) * 3;
  637. }
  638. } break;
  639. case VS::ARRAY_TANGENT: {
  640. if (p_compress_format & ARRAY_COMPRESS_TANGENT) {
  641. elem_size = sizeof(uint32_t);
  642. } else {
  643. elem_size = sizeof(float) * 4;
  644. }
  645. } break;
  646. case VS::ARRAY_COLOR: {
  647. if (p_compress_format & ARRAY_COMPRESS_COLOR) {
  648. elem_size = sizeof(uint32_t);
  649. } else {
  650. elem_size = sizeof(float) * 4;
  651. }
  652. } break;
  653. case VS::ARRAY_TEX_UV: {
  654. if (p_compress_format & ARRAY_COMPRESS_TEX_UV) {
  655. elem_size = sizeof(uint32_t);
  656. } else {
  657. elem_size = sizeof(float) * 2;
  658. }
  659. } break;
  660. case VS::ARRAY_TEX_UV2: {
  661. if (p_compress_format & ARRAY_COMPRESS_TEX_UV2) {
  662. elem_size = sizeof(uint32_t);
  663. } else {
  664. elem_size = sizeof(float) * 2;
  665. }
  666. } break;
  667. case VS::ARRAY_WEIGHTS: {
  668. if (p_compress_format & ARRAY_COMPRESS_WEIGHTS) {
  669. elem_size = sizeof(uint16_t) * 4;
  670. } else {
  671. elem_size = sizeof(float) * 4;
  672. }
  673. } break;
  674. case VS::ARRAY_BONES: {
  675. PoolVector<int> bones = p_arrays[VS::ARRAY_BONES];
  676. int max_bone = 0;
  677. {
  678. int bc = bones.size();
  679. PoolVector<int>::Read r = bones.read();
  680. for (int j = 0; j < bc; j++) {
  681. max_bone = MAX(r[j], max_bone);
  682. }
  683. }
  684. if (max_bone > 255) {
  685. p_compress_format |= ARRAY_FLAG_USE_16_BIT_BONES;
  686. elem_size = sizeof(uint16_t) * 4;
  687. } else {
  688. p_compress_format &= ~ARRAY_FLAG_USE_16_BIT_BONES;
  689. elem_size = sizeof(uint32_t);
  690. }
  691. } break;
  692. case VS::ARRAY_INDEX: {
  693. if (index_array_len <= 0) {
  694. ERR_PRINT("index_array_len==NO_INDEX_ARRAY");
  695. break;
  696. }
  697. /* determine wether using 16 or 32 bits indices */
  698. if (array_len >= (1 << 16)) {
  699. elem_size = 4;
  700. } else {
  701. elem_size = 2;
  702. }
  703. offsets[i] = elem_size;
  704. continue;
  705. } break;
  706. default: {
  707. ERR_FAIL();
  708. }
  709. }
  710. offsets[i] = total_elem_size;
  711. total_elem_size += elem_size;
  712. }
  713. uint32_t mask = (1 << ARRAY_MAX) - 1;
  714. format |= (~mask) & p_compress_format; //make the full format
  715. int array_size = total_elem_size * array_len;
  716. PoolVector<uint8_t> vertex_array;
  717. vertex_array.resize(array_size);
  718. int index_array_size = offsets[VS::ARRAY_INDEX] * index_array_len;
  719. PoolVector<uint8_t> index_array;
  720. index_array.resize(index_array_size);
  721. Rect3 aabb;
  722. Vector<Rect3> bone_aabb;
  723. Error err = _surface_set_data(p_arrays, format, offsets, total_elem_size, vertex_array, array_len, index_array, index_array_len, aabb, bone_aabb);
  724. if (err) {
  725. ERR_EXPLAIN("Invalid array format for surface");
  726. ERR_FAIL_COND(err != OK);
  727. }
  728. Vector<PoolVector<uint8_t> > blend_shape_data;
  729. for (int i = 0; i < p_blend_shapes.size(); i++) {
  730. PoolVector<uint8_t> vertex_array_shape;
  731. vertex_array_shape.resize(array_size);
  732. PoolVector<uint8_t> noindex;
  733. Rect3 laabb;
  734. Error err = _surface_set_data(p_blend_shapes[i], format & ~ARRAY_FORMAT_INDEX, offsets, total_elem_size, vertex_array_shape, array_len, noindex, 0, laabb, bone_aabb);
  735. aabb.merge_with(laabb);
  736. if (err) {
  737. ERR_EXPLAIN("Invalid blend shape array format for surface");
  738. ERR_FAIL_COND(err != OK);
  739. }
  740. blend_shape_data.push_back(vertex_array_shape);
  741. }
  742. mesh_add_surface(p_mesh, format, p_primitive, vertex_array, array_len, index_array, index_array_len, aabb, blend_shape_data, bone_aabb);
  743. }
  744. Array VisualServer::_get_array_from_surface(uint32_t p_format, PoolVector<uint8_t> p_vertex_data, int p_vertex_len, PoolVector<uint8_t> p_index_data, int p_index_len) const {
  745. uint32_t offsets[ARRAY_MAX];
  746. int total_elem_size = 0;
  747. for (int i = 0; i < VS::ARRAY_MAX; i++) {
  748. offsets[i] = 0; //reset
  749. if (!(p_format & (1 << i))) // no array
  750. continue;
  751. int elem_size = 0;
  752. switch (i) {
  753. case VS::ARRAY_VERTEX: {
  754. if (p_format & ARRAY_FLAG_USE_2D_VERTICES) {
  755. elem_size = 2;
  756. } else {
  757. elem_size = 3;
  758. }
  759. if (p_format & ARRAY_COMPRESS_VERTEX) {
  760. elem_size *= sizeof(int16_t);
  761. } else {
  762. elem_size *= sizeof(float);
  763. }
  764. if (elem_size == 6) {
  765. elem_size = 8;
  766. }
  767. } break;
  768. case VS::ARRAY_NORMAL: {
  769. if (p_format & ARRAY_COMPRESS_NORMAL) {
  770. elem_size = sizeof(uint32_t);
  771. } else {
  772. elem_size = sizeof(float) * 3;
  773. }
  774. } break;
  775. case VS::ARRAY_TANGENT: {
  776. if (p_format & ARRAY_COMPRESS_TANGENT) {
  777. elem_size = sizeof(uint32_t);
  778. } else {
  779. elem_size = sizeof(float) * 4;
  780. }
  781. } break;
  782. case VS::ARRAY_COLOR: {
  783. if (p_format & ARRAY_COMPRESS_COLOR) {
  784. elem_size = sizeof(uint32_t);
  785. } else {
  786. elem_size = sizeof(float) * 4;
  787. }
  788. } break;
  789. case VS::ARRAY_TEX_UV: {
  790. if (p_format & ARRAY_COMPRESS_TEX_UV) {
  791. elem_size = sizeof(uint32_t);
  792. } else {
  793. elem_size = sizeof(float) * 2;
  794. }
  795. } break;
  796. case VS::ARRAY_TEX_UV2: {
  797. if (p_format & ARRAY_COMPRESS_TEX_UV2) {
  798. elem_size = sizeof(uint32_t);
  799. } else {
  800. elem_size = sizeof(float) * 2;
  801. }
  802. } break;
  803. case VS::ARRAY_WEIGHTS: {
  804. if (p_format & ARRAY_COMPRESS_WEIGHTS) {
  805. elem_size = sizeof(uint16_t) * 4;
  806. } else {
  807. elem_size = sizeof(float) * 4;
  808. }
  809. } break;
  810. case VS::ARRAY_BONES: {
  811. if (p_format & ARRAY_FLAG_USE_16_BIT_BONES) {
  812. elem_size = sizeof(uint16_t) * 4;
  813. } else {
  814. elem_size = sizeof(uint32_t);
  815. }
  816. } break;
  817. case VS::ARRAY_INDEX: {
  818. if (p_index_len <= 0) {
  819. ERR_PRINT("index_array_len==NO_INDEX_ARRAY");
  820. break;
  821. }
  822. /* determine wether using 16 or 32 bits indices */
  823. if (p_vertex_len >= (1 << 16)) {
  824. elem_size = 4;
  825. } else {
  826. elem_size = 2;
  827. }
  828. offsets[i] = elem_size;
  829. continue;
  830. } break;
  831. default: {
  832. ERR_FAIL_V(Array());
  833. }
  834. }
  835. offsets[i] = total_elem_size;
  836. total_elem_size += elem_size;
  837. }
  838. Array ret;
  839. ret.resize(VS::ARRAY_MAX);
  840. PoolVector<uint8_t>::Read r = p_vertex_data.read();
  841. for (int i = 0; i < VS::ARRAY_MAX; i++) {
  842. if (!(p_format & (1 << i)))
  843. continue;
  844. switch (i) {
  845. case VS::ARRAY_VERTEX: {
  846. if (p_format & ARRAY_FLAG_USE_2D_VERTICES) {
  847. PoolVector<Vector2> arr_2d;
  848. arr_2d.resize(p_vertex_len);
  849. if (p_format & ARRAY_COMPRESS_VERTEX) {
  850. PoolVector<Vector2>::Write w = arr_2d.write();
  851. for (int j = 0; j < p_vertex_len; j++) {
  852. const uint16_t *v = (const uint16_t *)&r[j * total_elem_size + offsets[i]];
  853. w[j] = Vector2(Math::halfptr_to_float(&v[0]), Math::halfptr_to_float(&v[1]));
  854. }
  855. } else {
  856. PoolVector<Vector2>::Write w = arr_2d.write();
  857. for (int j = 0; j < p_vertex_len; j++) {
  858. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  859. w[j] = Vector2(v[0], v[1]);
  860. }
  861. }
  862. ret[i] = arr_2d;
  863. } else {
  864. PoolVector<Vector3> arr_3d;
  865. arr_3d.resize(p_vertex_len);
  866. if (p_format & ARRAY_COMPRESS_VERTEX) {
  867. PoolVector<Vector3>::Write w = arr_3d.write();
  868. for (int j = 0; j < p_vertex_len; j++) {
  869. const uint16_t *v = (const uint16_t *)&r[j * total_elem_size + offsets[i]];
  870. w[j] = Vector3(Math::halfptr_to_float(&v[0]), Math::halfptr_to_float(&v[1]), Math::halfptr_to_float(&v[2]));
  871. }
  872. } else {
  873. PoolVector<Vector3>::Write w = arr_3d.write();
  874. for (int j = 0; j < p_vertex_len; j++) {
  875. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  876. w[j] = Vector3(v[0], v[1], v[2]);
  877. }
  878. }
  879. ret[i] = arr_3d;
  880. }
  881. } break;
  882. case VS::ARRAY_NORMAL: {
  883. PoolVector<Vector3> arr;
  884. arr.resize(p_vertex_len);
  885. if (p_format & ARRAY_COMPRESS_NORMAL) {
  886. PoolVector<Vector3>::Write w = arr.write();
  887. const float multiplier = 1.f / 127.f;
  888. for (int j = 0; j < p_vertex_len; j++) {
  889. const int8_t *v = (const int8_t *)&r[j * total_elem_size + offsets[i]];
  890. w[j] = Vector3(float(v[0]) * multiplier, float(v[1]) * multiplier, float(v[2]) * multiplier);
  891. }
  892. } else {
  893. PoolVector<Vector3>::Write w = arr.write();
  894. for (int j = 0; j < p_vertex_len; j++) {
  895. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  896. w[j] = Vector3(v[0], v[1], v[2]);
  897. }
  898. }
  899. ret[i] = arr;
  900. } break;
  901. case VS::ARRAY_TANGENT: {
  902. PoolVector<float> arr;
  903. arr.resize(p_vertex_len * 4);
  904. if (p_format & ARRAY_COMPRESS_TANGENT) {
  905. PoolVector<float>::Write w = arr.write();
  906. for (int j = 0; j < p_vertex_len; j++) {
  907. const uint8_t *v = (const uint8_t *)&r[j * total_elem_size + offsets[i]];
  908. for (int k = 0; k < 4; k++) {
  909. w[j * 4 + k] = float(v[k] / 255.0) * 2.0 - 1.0;
  910. }
  911. }
  912. } else {
  913. PoolVector<float>::Write w = arr.write();
  914. for (int j = 0; j < p_vertex_len; j++) {
  915. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  916. for (int k = 0; k < 4; k++) {
  917. w[j * 4 + k] = v[k];
  918. }
  919. }
  920. }
  921. ret[i] = arr;
  922. } break;
  923. case VS::ARRAY_COLOR: {
  924. PoolVector<Color> arr;
  925. arr.resize(p_vertex_len);
  926. if (p_format & ARRAY_COMPRESS_COLOR) {
  927. PoolVector<Color>::Write w = arr.write();
  928. for (int j = 0; j < p_vertex_len; j++) {
  929. const uint8_t *v = (const uint8_t *)&r[j * total_elem_size + offsets[i]];
  930. w[j] = Color(float(v[0] / 255.0), float(v[1] / 255.0), float(v[2] / 255.0), float(v[3] / 255.0));
  931. }
  932. } else {
  933. PoolVector<Color>::Write w = arr.write();
  934. for (int j = 0; j < p_vertex_len; j++) {
  935. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  936. w[j] = Color(v[0], v[1], v[2], v[3]);
  937. }
  938. }
  939. ret[i] = arr;
  940. } break;
  941. case VS::ARRAY_TEX_UV: {
  942. PoolVector<Vector2> arr;
  943. arr.resize(p_vertex_len);
  944. if (p_format & ARRAY_COMPRESS_TEX_UV) {
  945. PoolVector<Vector2>::Write w = arr.write();
  946. for (int j = 0; j < p_vertex_len; j++) {
  947. const uint16_t *v = (const uint16_t *)&r[j * total_elem_size + offsets[i]];
  948. w[j] = Vector2(Math::halfptr_to_float(&v[0]), Math::halfptr_to_float(&v[1]));
  949. }
  950. } else {
  951. PoolVector<Vector2>::Write w = arr.write();
  952. for (int j = 0; j < p_vertex_len; j++) {
  953. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  954. w[j] = Vector2(v[0], v[1]);
  955. }
  956. }
  957. ret[i] = arr;
  958. } break;
  959. case VS::ARRAY_TEX_UV2: {
  960. PoolVector<Vector2> arr;
  961. arr.resize(p_vertex_len);
  962. if (p_format & ARRAY_COMPRESS_TEX_UV2) {
  963. PoolVector<Vector2>::Write w = arr.write();
  964. for (int j = 0; j < p_vertex_len; j++) {
  965. const uint16_t *v = (const uint16_t *)&r[j * total_elem_size + offsets[i]];
  966. w[j] = Vector2(Math::halfptr_to_float(&v[0]), Math::halfptr_to_float(&v[1]));
  967. }
  968. } else {
  969. PoolVector<Vector2>::Write w = arr.write();
  970. for (int j = 0; j < p_vertex_len; j++) {
  971. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  972. w[j] = Vector2(v[0], v[1]);
  973. }
  974. }
  975. ret[i] = arr;
  976. } break;
  977. case VS::ARRAY_WEIGHTS: {
  978. PoolVector<float> arr;
  979. arr.resize(p_vertex_len * 4);
  980. if (p_format & ARRAY_COMPRESS_WEIGHTS) {
  981. PoolVector<float>::Write w = arr.write();
  982. for (int j = 0; j < p_vertex_len; j++) {
  983. const uint16_t *v = (const uint16_t *)&r[j * total_elem_size + offsets[i]];
  984. for (int k = 0; k < 4; k++) {
  985. w[j * 4 + k] = float(v[k] / 65535.0) * 2.0 - 1.0;
  986. }
  987. }
  988. } else {
  989. PoolVector<float>::Write w = arr.write();
  990. for (int j = 0; j < p_vertex_len; j++) {
  991. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  992. for (int k = 0; k < 4; k++) {
  993. w[j * 4 + k] = v[k];
  994. }
  995. }
  996. }
  997. ret[i] = arr;
  998. } break;
  999. case VS::ARRAY_BONES: {
  1000. PoolVector<int> arr;
  1001. arr.resize(p_vertex_len * 4);
  1002. if (p_format & ARRAY_FLAG_USE_16_BIT_BONES) {
  1003. PoolVector<int>::Write w = arr.write();
  1004. for (int j = 0; j < p_vertex_len; j++) {
  1005. const uint16_t *v = (const uint16_t *)&r[j * total_elem_size + offsets[i]];
  1006. for (int k = 0; k < 4; k++) {
  1007. w[j * 4 + k] = v[k];
  1008. }
  1009. }
  1010. } else {
  1011. PoolVector<int>::Write w = arr.write();
  1012. for (int j = 0; j < p_vertex_len; j++) {
  1013. const uint8_t *v = (const uint8_t *)&r[j * total_elem_size + offsets[i]];
  1014. for (int k = 0; k < 4; k++) {
  1015. w[j * 4 + k] = v[k];
  1016. }
  1017. }
  1018. }
  1019. ret[i] = arr;
  1020. } break;
  1021. case VS::ARRAY_INDEX: {
  1022. /* determine wether using 16 or 32 bits indices */
  1023. PoolVector<uint8_t>::Read ir = p_index_data.read();
  1024. PoolVector<int> arr;
  1025. arr.resize(p_index_len);
  1026. if (p_vertex_len < (1 << 16)) {
  1027. PoolVector<int>::Write w = arr.write();
  1028. for (int j = 0; j < p_index_len; j++) {
  1029. const uint16_t *v = (const uint16_t *)&ir[j * 2];
  1030. w[j] = *v;
  1031. }
  1032. } else {
  1033. PoolVector<int>::Write w = arr.write();
  1034. for (int j = 0; j < p_index_len; j++) {
  1035. const int *v = (const int *)&ir[j * 4];
  1036. w[j] = *v;
  1037. }
  1038. }
  1039. ret[i] = arr;
  1040. } break;
  1041. default: {
  1042. ERR_FAIL_V(ret);
  1043. }
  1044. }
  1045. }
  1046. return ret;
  1047. }
  1048. Array VisualServer::mesh_surface_get_arrays(RID p_mesh, int p_surface) const {
  1049. PoolVector<uint8_t> vertex_data = mesh_surface_get_array(p_mesh, p_surface);
  1050. ERR_FAIL_COND_V(vertex_data.size() == 0, Array());
  1051. int vertex_len = mesh_surface_get_array_len(p_mesh, p_surface);
  1052. PoolVector<uint8_t> index_data = mesh_surface_get_index_array(p_mesh, p_surface);
  1053. int index_len = mesh_surface_get_array_index_len(p_mesh, p_surface);
  1054. uint32_t format = mesh_surface_get_format(p_mesh, p_surface);
  1055. return _get_array_from_surface(format, vertex_data, vertex_len, index_data, index_len);
  1056. }
  1057. void VisualServer::_bind_methods() {
  1058. ClassDB::bind_method(D_METHOD("texture_create"), &VisualServer::texture_create);
  1059. ClassDB::bind_method(D_METHOD("texture_create_from_image"), &VisualServer::texture_create_from_image, DEFVAL(TEXTURE_FLAGS_DEFAULT));
  1060. //ClassDB::bind_method(D_METHOD("texture_allocate"),&VisualServer::texture_allocate,DEFVAL( TEXTURE_FLAGS_DEFAULT ) );
  1061. //ClassDB::bind_method(D_METHOD("texture_set_data"),&VisualServer::texture_blit_rect,DEFVAL( CUBEMAP_LEFT ) );
  1062. //ClassDB::bind_method(D_METHOD("texture_get_rect"),&VisualServer::texture_get_rect );
  1063. ClassDB::bind_method(D_METHOD("texture_set_flags"), &VisualServer::texture_set_flags);
  1064. ClassDB::bind_method(D_METHOD("texture_get_flags"), &VisualServer::texture_get_flags);
  1065. ClassDB::bind_method(D_METHOD("texture_get_width"), &VisualServer::texture_get_width);
  1066. ClassDB::bind_method(D_METHOD("texture_get_height"), &VisualServer::texture_get_height);
  1067. ClassDB::bind_method(D_METHOD("texture_set_shrink_all_x2_on_set_data", "shrink"), &VisualServer::texture_set_shrink_all_x2_on_set_data);
  1068. }
  1069. void VisualServer::_canvas_item_add_style_box(RID p_item, const Rect2 &p_rect, const Rect2 &p_source, RID p_texture, const Vector<float> &p_margins, const Color &p_modulate) {
  1070. ERR_FAIL_COND(p_margins.size() != 4);
  1071. //canvas_item_add_style_box(p_item,p_rect,p_source,p_texture,Vector2(p_margins[0],p_margins[1]),Vector2(p_margins[2],p_margins[3]),true,p_modulate);
  1072. }
  1073. void VisualServer::_camera_set_orthogonal(RID p_camera, float p_size, float p_z_near, float p_z_far) {
  1074. camera_set_orthogonal(p_camera, p_size, p_z_near, p_z_far);
  1075. }
  1076. void VisualServer::mesh_add_surface_from_mesh_data(RID p_mesh, const Geometry::MeshData &p_mesh_data) {
  1077. #if 1
  1078. PoolVector<Vector3> vertices;
  1079. PoolVector<Vector3> normals;
  1080. for (int i = 0; i < p_mesh_data.faces.size(); i++) {
  1081. const Geometry::MeshData::Face &f = p_mesh_data.faces[i];
  1082. for (int j = 2; j < f.indices.size(); j++) {
  1083. #define _ADD_VERTEX(m_idx) \
  1084. vertices.push_back(p_mesh_data.vertices[f.indices[m_idx]]); \
  1085. normals.push_back(f.plane.normal);
  1086. _ADD_VERTEX(0);
  1087. _ADD_VERTEX(j - 1);
  1088. _ADD_VERTEX(j);
  1089. }
  1090. }
  1091. Array d;
  1092. d.resize(VS::ARRAY_MAX);
  1093. d[ARRAY_VERTEX] = vertices;
  1094. d[ARRAY_NORMAL] = normals;
  1095. mesh_add_surface_from_arrays(p_mesh, PRIMITIVE_TRIANGLES, d);
  1096. #else
  1097. PoolVector<Vector3> vertices;
  1098. for (int i = 0; i < p_mesh_data.edges.size(); i++) {
  1099. const Geometry::MeshData::Edge &f = p_mesh_data.edges[i];
  1100. vertices.push_back(p_mesh_data.vertices[f.a]);
  1101. vertices.push_back(p_mesh_data.vertices[f.b]);
  1102. }
  1103. Array d;
  1104. d.resize(VS::ARRAY_MAX);
  1105. d[ARRAY_VERTEX] = vertices;
  1106. mesh_add_surface(p_mesh, PRIMITIVE_LINES, d);
  1107. #endif
  1108. }
  1109. void VisualServer::mesh_add_surface_from_planes(RID p_mesh, const PoolVector<Plane> &p_planes) {
  1110. Geometry::MeshData mdata = Geometry::build_convex_mesh(p_planes);
  1111. mesh_add_surface_from_mesh_data(p_mesh, mdata);
  1112. }
  1113. void VisualServer::immediate_vertex_2d(RID p_immediate, const Vector2 &p_vertex) {
  1114. immediate_vertex(p_immediate, Vector3(p_vertex.x, p_vertex.y, 0));
  1115. }
  1116. RID VisualServer::instance_create2(RID p_base, RID p_scenario) {
  1117. RID instance = instance_create();
  1118. instance_set_base(instance, p_base);
  1119. instance_set_scenario(instance, p_scenario);
  1120. return instance;
  1121. }
  1122. VisualServer::VisualServer() {
  1123. //ERR_FAIL_COND(singleton);
  1124. singleton = this;
  1125. }
  1126. VisualServer::~VisualServer() {
  1127. singleton = NULL;
  1128. }