visual_server.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  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.gen.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 < 32; 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_double_sided, 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 (p_double_sided)
  228. version |= 16;
  229. if (material_2d[version].is_valid())
  230. return material_2d[version];
  231. //not valid, make
  232. /* material_2d[version]=fixed_material_create();
  233. fixed_material_set_flag(material_2d[version],FIXED_MATERIAL_FLAG_USE_ALPHA,p_transparent);
  234. fixed_material_set_flag(material_2d[version],FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY,true);
  235. fixed_material_set_flag(material_2d[version],FIXED_MATERIAL_FLAG_DISCARD_ALPHA,p_cut_alpha);
  236. material_set_flag(material_2d[version],MATERIAL_FLAG_UNSHADED,!p_shaded);
  237. material_set_flag(material_2d[version], MATERIAL_FLAG_DOUBLE_SIDED, p_double_sided);
  238. material_set_depth_draw_mode(material_2d[version],p_opaque_prepass?MATERIAL_DEPTH_DRAW_OPAQUE_PRE_PASS_ALPHA:MATERIAL_DEPTH_DRAW_OPAQUE_ONLY);
  239. fixed_material_set_texture(material_2d[version],FIXED_MATERIAL_PARAM_DIFFUSE,get_white_texture());
  240. //material cut alpha?*/
  241. return material_2d[version];
  242. }
  243. RID VisualServer::get_white_texture() {
  244. if (white_texture.is_valid())
  245. return white_texture;
  246. PoolVector<uint8_t> wt;
  247. wt.resize(16 * 3);
  248. {
  249. PoolVector<uint8_t>::Write w = wt.write();
  250. for (int i = 0; i < 16 * 3; i++)
  251. w[i] = 255;
  252. }
  253. Ref<Image> white = memnew(Image(4, 4, 0, Image::FORMAT_RGB8, wt));
  254. white_texture = texture_create();
  255. texture_allocate(white_texture, 4, 4, Image::FORMAT_RGB8);
  256. texture_set_data(white_texture, white);
  257. return white_texture;
  258. }
  259. 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) {
  260. PoolVector<uint8_t>::Write vw = r_vertex_array.write();
  261. PoolVector<uint8_t>::Write iw;
  262. if (r_index_array.size()) {
  263. iw = r_index_array.write();
  264. }
  265. int max_bone = 0;
  266. for (int ai = 0; ai < VS::ARRAY_MAX; ai++) {
  267. if (!(p_format & (1 << ai))) // no array
  268. continue;
  269. switch (ai) {
  270. case VS::ARRAY_VERTEX: {
  271. if (p_format & VS::ARRAY_FLAG_USE_2D_VERTICES) {
  272. PoolVector<Vector2> array = p_arrays[ai];
  273. ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
  274. PoolVector<Vector2>::Read read = array.read();
  275. const Vector2 *src = read.ptr();
  276. // setting vertices means regenerating the AABB
  277. Rect2 aabb;
  278. if (p_format & ARRAY_COMPRESS_VERTEX) {
  279. for (int i = 0; i < p_vertex_array_len; i++) {
  280. uint16_t vector[2] = { Math::make_half_float(src[i].x), Math::make_half_float(src[i].y) };
  281. copymem(&vw[p_offsets[ai] + i * p_stride], vector, sizeof(uint16_t) * 2);
  282. if (i == 0) {
  283. aabb = Rect2(src[i], Vector2());
  284. } else {
  285. aabb.expand_to(src[i]);
  286. }
  287. }
  288. } else {
  289. for (int i = 0; i < p_vertex_array_len; i++) {
  290. float vector[2] = { src[i].x, src[i].y };
  291. copymem(&vw[p_offsets[ai] + i * p_stride], vector, sizeof(float) * 2);
  292. if (i == 0) {
  293. aabb = Rect2(src[i], Vector2());
  294. } else {
  295. aabb.expand_to(src[i]);
  296. }
  297. }
  298. }
  299. r_aabb = Rect3(Vector3(aabb.position.x, aabb.position.y, 0), Vector3(aabb.size.x, aabb.size.y, 0));
  300. } else {
  301. PoolVector<Vector3> array = p_arrays[ai];
  302. ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
  303. PoolVector<Vector3>::Read read = array.read();
  304. const Vector3 *src = read.ptr();
  305. // setting vertices means regenerating the AABB
  306. Rect3 aabb;
  307. if (p_format & ARRAY_COMPRESS_VERTEX) {
  308. for (int i = 0; i < p_vertex_array_len; i++) {
  309. 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) };
  310. copymem(&vw[p_offsets[ai] + i * p_stride], vector, sizeof(uint16_t) * 4);
  311. if (i == 0) {
  312. aabb = Rect3(src[i], Vector3());
  313. } else {
  314. aabb.expand_to(src[i]);
  315. }
  316. }
  317. } else {
  318. for (int i = 0; i < p_vertex_array_len; i++) {
  319. float vector[3] = { src[i].x, src[i].y, src[i].z };
  320. copymem(&vw[p_offsets[ai] + i * p_stride], vector, sizeof(float) * 3);
  321. if (i == 0) {
  322. aabb = Rect3(src[i], Vector3());
  323. } else {
  324. aabb.expand_to(src[i]);
  325. }
  326. }
  327. }
  328. r_aabb = aabb;
  329. }
  330. } break;
  331. case VS::ARRAY_NORMAL: {
  332. ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::POOL_VECTOR3_ARRAY, ERR_INVALID_PARAMETER);
  333. PoolVector<Vector3> array = p_arrays[ai];
  334. ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
  335. PoolVector<Vector3>::Read read = array.read();
  336. const Vector3 *src = read.ptr();
  337. // setting vertices means regenerating the AABB
  338. if (p_format & ARRAY_COMPRESS_NORMAL) {
  339. for (int i = 0; i < p_vertex_array_len; i++) {
  340. int8_t vector[4] = {
  341. CLAMP(src[i].x * 127, -128, 127),
  342. CLAMP(src[i].y * 127, -128, 127),
  343. CLAMP(src[i].z * 127, -128, 127),
  344. 0,
  345. };
  346. copymem(&vw[p_offsets[ai] + i * p_stride], vector, 4);
  347. }
  348. } else {
  349. for (int i = 0; i < p_vertex_array_len; i++) {
  350. float vector[3] = { src[i].x, src[i].y, src[i].z };
  351. copymem(&vw[p_offsets[ai] + i * p_stride], vector, 3 * 4);
  352. }
  353. }
  354. } break;
  355. case VS::ARRAY_TANGENT: {
  356. ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::POOL_REAL_ARRAY, ERR_INVALID_PARAMETER);
  357. PoolVector<real_t> array = p_arrays[ai];
  358. ERR_FAIL_COND_V(array.size() != p_vertex_array_len * 4, ERR_INVALID_PARAMETER);
  359. PoolVector<real_t>::Read read = array.read();
  360. const real_t *src = read.ptr();
  361. if (p_format & ARRAY_COMPRESS_TANGENT) {
  362. for (int i = 0; i < p_vertex_array_len; i++) {
  363. uint8_t xyzw[4] = {
  364. CLAMP(src[i * 4 + 0] * 127, -128, 127),
  365. CLAMP(src[i * 4 + 1] * 127, -128, 127),
  366. CLAMP(src[i * 4 + 2] * 127, -128, 127),
  367. CLAMP(src[i * 4 + 3] * 127, -128, 127)
  368. };
  369. copymem(&vw[p_offsets[ai] + i * p_stride], xyzw, 4);
  370. }
  371. } else {
  372. for (int i = 0; i < p_vertex_array_len; i++) {
  373. float xyzw[4] = {
  374. src[i * 4 + 0],
  375. src[i * 4 + 1],
  376. src[i * 4 + 2],
  377. src[i * 4 + 3]
  378. };
  379. copymem(&vw[p_offsets[ai] + i * p_stride], xyzw, 4 * 4);
  380. }
  381. }
  382. } break;
  383. case VS::ARRAY_COLOR: {
  384. ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::POOL_COLOR_ARRAY, ERR_INVALID_PARAMETER);
  385. PoolVector<Color> array = p_arrays[ai];
  386. ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
  387. PoolVector<Color>::Read read = array.read();
  388. const Color *src = read.ptr();
  389. if (p_format & ARRAY_COMPRESS_COLOR) {
  390. for (int i = 0; i < p_vertex_array_len; i++) {
  391. uint8_t colors[4];
  392. for (int j = 0; j < 4; j++) {
  393. colors[j] = CLAMP(int((src[i][j]) * 255.0), 0, 255);
  394. }
  395. copymem(&vw[p_offsets[ai] + i * p_stride], colors, 4);
  396. }
  397. } else {
  398. for (int i = 0; i < p_vertex_array_len; i++) {
  399. copymem(&vw[p_offsets[ai] + i * p_stride], &src[i], 4 * 4);
  400. }
  401. }
  402. } break;
  403. case VS::ARRAY_TEX_UV: {
  404. 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);
  405. PoolVector<Vector2> array = p_arrays[ai];
  406. ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
  407. PoolVector<Vector2>::Read read = array.read();
  408. const Vector2 *src = read.ptr();
  409. if (p_format & ARRAY_COMPRESS_TEX_UV) {
  410. for (int i = 0; i < p_vertex_array_len; i++) {
  411. uint16_t uv[2] = { Math::make_half_float(src[i].x), Math::make_half_float(src[i].y) };
  412. copymem(&vw[p_offsets[ai] + i * p_stride], uv, 2 * 2);
  413. }
  414. } else {
  415. for (int i = 0; i < p_vertex_array_len; i++) {
  416. float uv[2] = { src[i].x, src[i].y };
  417. copymem(&vw[p_offsets[ai] + i * p_stride], uv, 2 * 4);
  418. }
  419. }
  420. } break;
  421. case VS::ARRAY_TEX_UV2: {
  422. 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);
  423. PoolVector<Vector2> array = p_arrays[ai];
  424. ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
  425. PoolVector<Vector2>::Read read = array.read();
  426. const Vector2 *src = read.ptr();
  427. if (p_format & ARRAY_COMPRESS_TEX_UV2) {
  428. for (int i = 0; i < p_vertex_array_len; i++) {
  429. uint16_t uv[2] = { Math::make_half_float(src[i].x), Math::make_half_float(src[i].y) };
  430. copymem(&vw[p_offsets[ai] + i * p_stride], uv, 2 * 2);
  431. }
  432. } else {
  433. for (int i = 0; i < p_vertex_array_len; i++) {
  434. float uv[2] = { src[i].x, src[i].y };
  435. copymem(&vw[p_offsets[ai] + i * p_stride], uv, 2 * 4);
  436. }
  437. }
  438. } break;
  439. case VS::ARRAY_WEIGHTS: {
  440. ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::POOL_REAL_ARRAY, ERR_INVALID_PARAMETER);
  441. PoolVector<real_t> array = p_arrays[ai];
  442. ERR_FAIL_COND_V(array.size() != p_vertex_array_len * VS::ARRAY_WEIGHTS_SIZE, ERR_INVALID_PARAMETER);
  443. PoolVector<real_t>::Read read = array.read();
  444. const real_t *src = read.ptr();
  445. if (p_format & ARRAY_COMPRESS_WEIGHTS) {
  446. for (int i = 0; i < p_vertex_array_len; i++) {
  447. uint16_t data[VS::ARRAY_WEIGHTS_SIZE];
  448. for (int j = 0; j < VS::ARRAY_WEIGHTS_SIZE; j++) {
  449. data[j] = CLAMP(src[i * VS::ARRAY_WEIGHTS_SIZE + j] * 65535, 0, 65535);
  450. }
  451. copymem(&vw[p_offsets[ai] + i * p_stride], data, 2 * 4);
  452. }
  453. } else {
  454. for (int i = 0; i < p_vertex_array_len; i++) {
  455. float data[VS::ARRAY_WEIGHTS_SIZE];
  456. for (int j = 0; j < VS::ARRAY_WEIGHTS_SIZE; j++) {
  457. data[j] = src[i * VS::ARRAY_WEIGHTS_SIZE + j];
  458. }
  459. copymem(&vw[p_offsets[ai] + i * p_stride], data, 4 * 4);
  460. }
  461. }
  462. } break;
  463. case VS::ARRAY_BONES: {
  464. 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);
  465. PoolVector<int> array = p_arrays[ai];
  466. ERR_FAIL_COND_V(array.size() != p_vertex_array_len * VS::ARRAY_WEIGHTS_SIZE, ERR_INVALID_PARAMETER);
  467. PoolVector<int>::Read read = array.read();
  468. const int *src = read.ptr();
  469. if (!(p_format & ARRAY_FLAG_USE_16_BIT_BONES)) {
  470. for (int i = 0; i < p_vertex_array_len; i++) {
  471. uint8_t data[VS::ARRAY_WEIGHTS_SIZE];
  472. for (int j = 0; j < VS::ARRAY_WEIGHTS_SIZE; j++) {
  473. data[j] = CLAMP(src[i * VS::ARRAY_WEIGHTS_SIZE + j], 0, 255);
  474. max_bone = MAX(data[j], max_bone);
  475. }
  476. copymem(&vw[p_offsets[ai] + i * p_stride], data, 4);
  477. }
  478. } else {
  479. for (int i = 0; i < p_vertex_array_len; i++) {
  480. uint16_t data[VS::ARRAY_WEIGHTS_SIZE];
  481. for (int j = 0; j < VS::ARRAY_WEIGHTS_SIZE; j++) {
  482. data[j] = src[i * VS::ARRAY_WEIGHTS_SIZE + j];
  483. max_bone = MAX(data[j], max_bone);
  484. }
  485. copymem(&vw[p_offsets[ai] + i * p_stride], data, 2 * 4);
  486. }
  487. }
  488. } break;
  489. case VS::ARRAY_INDEX: {
  490. ERR_FAIL_COND_V(p_index_array_len <= 0, ERR_INVALID_DATA);
  491. ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::POOL_INT_ARRAY, ERR_INVALID_PARAMETER);
  492. PoolVector<int> indices = p_arrays[ai];
  493. ERR_FAIL_COND_V(indices.size() == 0, ERR_INVALID_PARAMETER);
  494. ERR_FAIL_COND_V(indices.size() != p_index_array_len, ERR_INVALID_PARAMETER);
  495. /* determine wether using 16 or 32 bits indices */
  496. PoolVector<int>::Read read = indices.read();
  497. const int *src = read.ptr();
  498. for (int i = 0; i < p_index_array_len; i++) {
  499. if (p_vertex_array_len < (1 << 16)) {
  500. uint16_t v = src[i];
  501. copymem(&iw[i * 2], &v, 2);
  502. } else {
  503. uint32_t v = src[i];
  504. copymem(&iw[i * 4], &v, 4);
  505. }
  506. }
  507. } break;
  508. default: {
  509. ERR_FAIL_V(ERR_INVALID_DATA);
  510. }
  511. }
  512. }
  513. if (p_format & VS::ARRAY_FORMAT_BONES) {
  514. //create AABBs for each detected bone
  515. int total_bones = max_bone + 1;
  516. bool first = r_bone_aabb.size() == 0;
  517. r_bone_aabb.resize(total_bones);
  518. if (first) {
  519. for (int i = 0; i < total_bones; i++) {
  520. r_bone_aabb[i].size == Vector3(-1, -1, -1); //negative means unused
  521. }
  522. }
  523. PoolVector<Vector3> vertices = p_arrays[VS::ARRAY_VERTEX];
  524. PoolVector<int> bones = p_arrays[VS::ARRAY_BONES];
  525. PoolVector<float> weights = p_arrays[VS::ARRAY_WEIGHTS];
  526. bool any_valid = false;
  527. if (vertices.size() && bones.size() == vertices.size() * 4 && weights.size() == bones.size()) {
  528. int vs = vertices.size();
  529. PoolVector<Vector3>::Read rv = vertices.read();
  530. PoolVector<int>::Read rb = bones.read();
  531. PoolVector<float>::Read rw = weights.read();
  532. Rect3 *bptr = r_bone_aabb.ptr();
  533. for (int i = 0; i < vs; i++) {
  534. Vector3 v = rv[i];
  535. for (int j = 0; j < 4; j++) {
  536. int idx = rb[i * 4 + j];
  537. float w = rw[i * 4 + j];
  538. if (w == 0)
  539. continue; //break;
  540. ERR_FAIL_INDEX_V(idx, total_bones, ERR_INVALID_DATA);
  541. if (bptr->size.x < 0) {
  542. //first
  543. bptr[idx] = Rect3();
  544. bptr[idx].position = v;
  545. any_valid = true;
  546. } else {
  547. bptr[idx].expand_to(v);
  548. }
  549. }
  550. }
  551. }
  552. if (!any_valid && first) {
  553. r_bone_aabb.clear();
  554. }
  555. }
  556. return OK;
  557. }
  558. 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) {
  559. ERR_FAIL_INDEX(p_primitive, VS::PRIMITIVE_MAX);
  560. ERR_FAIL_COND(p_arrays.size() != VS::ARRAY_MAX);
  561. uint32_t format = 0;
  562. // validation
  563. int index_array_len = 0;
  564. int array_len = 0;
  565. for (int i = 0; i < p_arrays.size(); i++) {
  566. if (p_arrays[i].get_type() == Variant::NIL)
  567. continue;
  568. format |= (1 << i);
  569. if (i == VS::ARRAY_VERTEX) {
  570. Variant var = p_arrays[i];
  571. switch (var.get_type()) {
  572. case Variant::POOL_VECTOR2_ARRAY: {
  573. PoolVector<Vector2> v2 = var;
  574. array_len = v2.size();
  575. } break;
  576. case Variant::POOL_VECTOR3_ARRAY: {
  577. PoolVector<Vector3> v3 = var;
  578. array_len = v3.size();
  579. } break;
  580. default: {
  581. Array v = var;
  582. array_len = v.size();
  583. } break;
  584. }
  585. array_len = PoolVector3Array(p_arrays[i]).size();
  586. ERR_FAIL_COND(array_len == 0);
  587. } else if (i == VS::ARRAY_INDEX) {
  588. index_array_len = PoolIntArray(p_arrays[i]).size();
  589. }
  590. }
  591. ERR_FAIL_COND((format & VS::ARRAY_FORMAT_VERTEX) == 0); // mandatory
  592. if (p_blend_shapes.size()) {
  593. //validate format for morphs
  594. for (int i = 0; i < p_blend_shapes.size(); i++) {
  595. uint32_t bsformat = 0;
  596. Array arr = p_blend_shapes[i];
  597. for (int j = 0; j < arr.size(); j++) {
  598. if (arr[j].get_type() != Variant::NIL)
  599. bsformat |= (1 << j);
  600. }
  601. ERR_FAIL_COND((bsformat) != (format & (VS::ARRAY_FORMAT_INDEX - 1)));
  602. }
  603. }
  604. uint32_t offsets[VS::ARRAY_MAX];
  605. int total_elem_size = 0;
  606. for (int i = 0; i < VS::ARRAY_MAX; i++) {
  607. offsets[i] = 0; //reset
  608. if (!(format & (1 << i))) // no array
  609. continue;
  610. int elem_size = 0;
  611. switch (i) {
  612. case VS::ARRAY_VERTEX: {
  613. Variant arr = p_arrays[0];
  614. if (arr.get_type() == Variant::POOL_VECTOR2_ARRAY) {
  615. elem_size = 2;
  616. p_compress_format |= ARRAY_FLAG_USE_2D_VERTICES;
  617. } else if (arr.get_type() == Variant::POOL_VECTOR3_ARRAY) {
  618. p_compress_format &= ~ARRAY_FLAG_USE_2D_VERTICES;
  619. elem_size = 3;
  620. } else {
  621. elem_size = (p_compress_format & ARRAY_FLAG_USE_2D_VERTICES) ? 2 : 3;
  622. }
  623. if (p_compress_format & ARRAY_COMPRESS_VERTEX) {
  624. elem_size *= sizeof(int16_t);
  625. } else {
  626. elem_size *= sizeof(float);
  627. }
  628. if (elem_size == 6) {
  629. //had to pad
  630. elem_size = 8;
  631. }
  632. } break;
  633. case VS::ARRAY_NORMAL: {
  634. if (p_compress_format & ARRAY_COMPRESS_NORMAL) {
  635. elem_size = sizeof(uint32_t);
  636. } else {
  637. elem_size = sizeof(float) * 3;
  638. }
  639. } break;
  640. case VS::ARRAY_TANGENT: {
  641. if (p_compress_format & ARRAY_COMPRESS_TANGENT) {
  642. elem_size = sizeof(uint32_t);
  643. } else {
  644. elem_size = sizeof(float) * 4;
  645. }
  646. } break;
  647. case VS::ARRAY_COLOR: {
  648. if (p_compress_format & ARRAY_COMPRESS_COLOR) {
  649. elem_size = sizeof(uint32_t);
  650. } else {
  651. elem_size = sizeof(float) * 4;
  652. }
  653. } break;
  654. case VS::ARRAY_TEX_UV: {
  655. if (p_compress_format & ARRAY_COMPRESS_TEX_UV) {
  656. elem_size = sizeof(uint32_t);
  657. } else {
  658. elem_size = sizeof(float) * 2;
  659. }
  660. } break;
  661. case VS::ARRAY_TEX_UV2: {
  662. if (p_compress_format & ARRAY_COMPRESS_TEX_UV2) {
  663. elem_size = sizeof(uint32_t);
  664. } else {
  665. elem_size = sizeof(float) * 2;
  666. }
  667. } break;
  668. case VS::ARRAY_WEIGHTS: {
  669. if (p_compress_format & ARRAY_COMPRESS_WEIGHTS) {
  670. elem_size = sizeof(uint16_t) * 4;
  671. } else {
  672. elem_size = sizeof(float) * 4;
  673. }
  674. } break;
  675. case VS::ARRAY_BONES: {
  676. PoolVector<int> bones = p_arrays[VS::ARRAY_BONES];
  677. int max_bone = 0;
  678. {
  679. int bc = bones.size();
  680. PoolVector<int>::Read r = bones.read();
  681. for (int j = 0; j < bc; j++) {
  682. max_bone = MAX(r[j], max_bone);
  683. }
  684. }
  685. if (max_bone > 255) {
  686. p_compress_format |= ARRAY_FLAG_USE_16_BIT_BONES;
  687. elem_size = sizeof(uint16_t) * 4;
  688. } else {
  689. p_compress_format &= ~ARRAY_FLAG_USE_16_BIT_BONES;
  690. elem_size = sizeof(uint32_t);
  691. }
  692. } break;
  693. case VS::ARRAY_INDEX: {
  694. if (index_array_len <= 0) {
  695. ERR_PRINT("index_array_len==NO_INDEX_ARRAY");
  696. break;
  697. }
  698. /* determine wether using 16 or 32 bits indices */
  699. if (array_len >= (1 << 16)) {
  700. elem_size = 4;
  701. } else {
  702. elem_size = 2;
  703. }
  704. offsets[i] = elem_size;
  705. continue;
  706. } break;
  707. default: {
  708. ERR_FAIL();
  709. }
  710. }
  711. offsets[i] = total_elem_size;
  712. total_elem_size += elem_size;
  713. }
  714. uint32_t mask = (1 << ARRAY_MAX) - 1;
  715. format |= (~mask) & p_compress_format; //make the full format
  716. int array_size = total_elem_size * array_len;
  717. PoolVector<uint8_t> vertex_array;
  718. vertex_array.resize(array_size);
  719. int index_array_size = offsets[VS::ARRAY_INDEX] * index_array_len;
  720. PoolVector<uint8_t> index_array;
  721. index_array.resize(index_array_size);
  722. Rect3 aabb;
  723. Vector<Rect3> bone_aabb;
  724. Error err = _surface_set_data(p_arrays, format, offsets, total_elem_size, vertex_array, array_len, index_array, index_array_len, aabb, bone_aabb);
  725. if (err) {
  726. ERR_EXPLAIN("Invalid array format for surface");
  727. ERR_FAIL_COND(err != OK);
  728. }
  729. Vector<PoolVector<uint8_t> > blend_shape_data;
  730. for (int i = 0; i < p_blend_shapes.size(); i++) {
  731. PoolVector<uint8_t> vertex_array_shape;
  732. vertex_array_shape.resize(array_size);
  733. PoolVector<uint8_t> noindex;
  734. Rect3 laabb;
  735. 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);
  736. aabb.merge_with(laabb);
  737. if (err) {
  738. ERR_EXPLAIN("Invalid blend shape array format for surface");
  739. ERR_FAIL_COND(err != OK);
  740. }
  741. blend_shape_data.push_back(vertex_array_shape);
  742. }
  743. mesh_add_surface(p_mesh, format, p_primitive, vertex_array, array_len, index_array, index_array_len, aabb, blend_shape_data, bone_aabb);
  744. }
  745. 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 {
  746. uint32_t offsets[ARRAY_MAX];
  747. int total_elem_size = 0;
  748. for (int i = 0; i < VS::ARRAY_MAX; i++) {
  749. offsets[i] = 0; //reset
  750. if (!(p_format & (1 << i))) // no array
  751. continue;
  752. int elem_size = 0;
  753. switch (i) {
  754. case VS::ARRAY_VERTEX: {
  755. if (p_format & ARRAY_FLAG_USE_2D_VERTICES) {
  756. elem_size = 2;
  757. } else {
  758. elem_size = 3;
  759. }
  760. if (p_format & ARRAY_COMPRESS_VERTEX) {
  761. elem_size *= sizeof(int16_t);
  762. } else {
  763. elem_size *= sizeof(float);
  764. }
  765. if (elem_size == 6) {
  766. elem_size = 8;
  767. }
  768. } break;
  769. case VS::ARRAY_NORMAL: {
  770. if (p_format & ARRAY_COMPRESS_NORMAL) {
  771. elem_size = sizeof(uint32_t);
  772. } else {
  773. elem_size = sizeof(float) * 3;
  774. }
  775. } break;
  776. case VS::ARRAY_TANGENT: {
  777. if (p_format & ARRAY_COMPRESS_TANGENT) {
  778. elem_size = sizeof(uint32_t);
  779. } else {
  780. elem_size = sizeof(float) * 4;
  781. }
  782. } break;
  783. case VS::ARRAY_COLOR: {
  784. if (p_format & ARRAY_COMPRESS_COLOR) {
  785. elem_size = sizeof(uint32_t);
  786. } else {
  787. elem_size = sizeof(float) * 4;
  788. }
  789. } break;
  790. case VS::ARRAY_TEX_UV: {
  791. if (p_format & ARRAY_COMPRESS_TEX_UV) {
  792. elem_size = sizeof(uint32_t);
  793. } else {
  794. elem_size = sizeof(float) * 2;
  795. }
  796. } break;
  797. case VS::ARRAY_TEX_UV2: {
  798. if (p_format & ARRAY_COMPRESS_TEX_UV2) {
  799. elem_size = sizeof(uint32_t);
  800. } else {
  801. elem_size = sizeof(float) * 2;
  802. }
  803. } break;
  804. case VS::ARRAY_WEIGHTS: {
  805. if (p_format & ARRAY_COMPRESS_WEIGHTS) {
  806. elem_size = sizeof(uint16_t) * 4;
  807. } else {
  808. elem_size = sizeof(float) * 4;
  809. }
  810. } break;
  811. case VS::ARRAY_BONES: {
  812. if (p_format & ARRAY_FLAG_USE_16_BIT_BONES) {
  813. elem_size = sizeof(uint16_t) * 4;
  814. } else {
  815. elem_size = sizeof(uint32_t);
  816. }
  817. } break;
  818. case VS::ARRAY_INDEX: {
  819. if (p_index_len <= 0) {
  820. ERR_PRINT("index_array_len==NO_INDEX_ARRAY");
  821. break;
  822. }
  823. /* determine wether using 16 or 32 bits indices */
  824. if (p_vertex_len >= (1 << 16)) {
  825. elem_size = 4;
  826. } else {
  827. elem_size = 2;
  828. }
  829. offsets[i] = elem_size;
  830. continue;
  831. } break;
  832. default: {
  833. ERR_FAIL_V(Array());
  834. }
  835. }
  836. offsets[i] = total_elem_size;
  837. total_elem_size += elem_size;
  838. }
  839. Array ret;
  840. ret.resize(VS::ARRAY_MAX);
  841. PoolVector<uint8_t>::Read r = p_vertex_data.read();
  842. for (int i = 0; i < VS::ARRAY_MAX; i++) {
  843. if (!(p_format & (1 << i)))
  844. continue;
  845. switch (i) {
  846. case VS::ARRAY_VERTEX: {
  847. if (p_format & ARRAY_FLAG_USE_2D_VERTICES) {
  848. PoolVector<Vector2> arr_2d;
  849. arr_2d.resize(p_vertex_len);
  850. if (p_format & ARRAY_COMPRESS_VERTEX) {
  851. PoolVector<Vector2>::Write w = arr_2d.write();
  852. for (int j = 0; j < p_vertex_len; j++) {
  853. const uint16_t *v = (const uint16_t *)&r[j * total_elem_size + offsets[i]];
  854. w[j] = Vector2(Math::halfptr_to_float(&v[0]), Math::halfptr_to_float(&v[1]));
  855. }
  856. } else {
  857. PoolVector<Vector2>::Write w = arr_2d.write();
  858. for (int j = 0; j < p_vertex_len; j++) {
  859. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  860. w[j] = Vector2(v[0], v[1]);
  861. }
  862. }
  863. ret[i] = arr_2d;
  864. } else {
  865. PoolVector<Vector3> arr_3d;
  866. arr_3d.resize(p_vertex_len);
  867. if (p_format & ARRAY_COMPRESS_VERTEX) {
  868. PoolVector<Vector3>::Write w = arr_3d.write();
  869. for (int j = 0; j < p_vertex_len; j++) {
  870. const uint16_t *v = (const uint16_t *)&r[j * total_elem_size + offsets[i]];
  871. w[j] = Vector3(Math::halfptr_to_float(&v[0]), Math::halfptr_to_float(&v[1]), Math::halfptr_to_float(&v[2]));
  872. }
  873. } else {
  874. PoolVector<Vector3>::Write w = arr_3d.write();
  875. for (int j = 0; j < p_vertex_len; j++) {
  876. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  877. w[j] = Vector3(v[0], v[1], v[2]);
  878. }
  879. }
  880. ret[i] = arr_3d;
  881. }
  882. } break;
  883. case VS::ARRAY_NORMAL: {
  884. PoolVector<Vector3> arr;
  885. arr.resize(p_vertex_len);
  886. if (p_format & ARRAY_COMPRESS_NORMAL) {
  887. PoolVector<Vector3>::Write w = arr.write();
  888. const float multiplier = 1.f / 127.f;
  889. for (int j = 0; j < p_vertex_len; j++) {
  890. const int8_t *v = (const int8_t *)&r[j * total_elem_size + offsets[i]];
  891. w[j] = Vector3(float(v[0]) * multiplier, float(v[1]) * multiplier, float(v[2]) * multiplier);
  892. }
  893. } else {
  894. PoolVector<Vector3>::Write w = arr.write();
  895. for (int j = 0; j < p_vertex_len; j++) {
  896. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  897. w[j] = Vector3(v[0], v[1], v[2]);
  898. }
  899. }
  900. ret[i] = arr;
  901. } break;
  902. case VS::ARRAY_TANGENT: {
  903. PoolVector<float> arr;
  904. arr.resize(p_vertex_len * 4);
  905. if (p_format & ARRAY_COMPRESS_TANGENT) {
  906. PoolVector<float>::Write w = arr.write();
  907. for (int j = 0; j < p_vertex_len; j++) {
  908. const int8_t *v = (const int8_t *)&r[j * total_elem_size + offsets[i]];
  909. for (int k = 0; k < 4; k++) {
  910. w[j * 4 + k] = float(v[k] / 127.0);
  911. }
  912. }
  913. } else {
  914. PoolVector<float>::Write w = arr.write();
  915. for (int j = 0; j < p_vertex_len; j++) {
  916. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  917. for (int k = 0; k < 4; k++) {
  918. w[j * 4 + k] = v[k];
  919. }
  920. }
  921. }
  922. ret[i] = arr;
  923. } break;
  924. case VS::ARRAY_COLOR: {
  925. PoolVector<Color> arr;
  926. arr.resize(p_vertex_len);
  927. if (p_format & ARRAY_COMPRESS_COLOR) {
  928. PoolVector<Color>::Write w = arr.write();
  929. for (int j = 0; j < p_vertex_len; j++) {
  930. const uint8_t *v = (const uint8_t *)&r[j * total_elem_size + offsets[i]];
  931. w[j] = Color(float(v[0] / 255.0), float(v[1] / 255.0), float(v[2] / 255.0), float(v[3] / 255.0));
  932. }
  933. } else {
  934. PoolVector<Color>::Write w = arr.write();
  935. for (int j = 0; j < p_vertex_len; j++) {
  936. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  937. w[j] = Color(v[0], v[1], v[2], v[3]);
  938. }
  939. }
  940. ret[i] = arr;
  941. } break;
  942. case VS::ARRAY_TEX_UV: {
  943. PoolVector<Vector2> arr;
  944. arr.resize(p_vertex_len);
  945. if (p_format & ARRAY_COMPRESS_TEX_UV) {
  946. PoolVector<Vector2>::Write w = arr.write();
  947. for (int j = 0; j < p_vertex_len; j++) {
  948. const uint16_t *v = (const uint16_t *)&r[j * total_elem_size + offsets[i]];
  949. w[j] = Vector2(Math::halfptr_to_float(&v[0]), Math::halfptr_to_float(&v[1]));
  950. }
  951. } else {
  952. PoolVector<Vector2>::Write w = arr.write();
  953. for (int j = 0; j < p_vertex_len; j++) {
  954. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  955. w[j] = Vector2(v[0], v[1]);
  956. }
  957. }
  958. ret[i] = arr;
  959. } break;
  960. case VS::ARRAY_TEX_UV2: {
  961. PoolVector<Vector2> arr;
  962. arr.resize(p_vertex_len);
  963. if (p_format & ARRAY_COMPRESS_TEX_UV2) {
  964. PoolVector<Vector2>::Write w = arr.write();
  965. for (int j = 0; j < p_vertex_len; j++) {
  966. const uint16_t *v = (const uint16_t *)&r[j * total_elem_size + offsets[i]];
  967. w[j] = Vector2(Math::halfptr_to_float(&v[0]), Math::halfptr_to_float(&v[1]));
  968. }
  969. } else {
  970. PoolVector<Vector2>::Write w = arr.write();
  971. for (int j = 0; j < p_vertex_len; j++) {
  972. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  973. w[j] = Vector2(v[0], v[1]);
  974. }
  975. }
  976. ret[i] = arr;
  977. } break;
  978. case VS::ARRAY_WEIGHTS: {
  979. PoolVector<float> arr;
  980. arr.resize(p_vertex_len * 4);
  981. if (p_format & ARRAY_COMPRESS_WEIGHTS) {
  982. PoolVector<float>::Write w = arr.write();
  983. for (int j = 0; j < p_vertex_len; j++) {
  984. const uint16_t *v = (const uint16_t *)&r[j * total_elem_size + offsets[i]];
  985. for (int k = 0; k < 4; k++) {
  986. w[j * 4 + k] = float(v[k] / 65535.0) * 2.0 - 1.0;
  987. }
  988. }
  989. } else {
  990. PoolVector<float>::Write w = arr.write();
  991. for (int j = 0; j < p_vertex_len; j++) {
  992. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  993. for (int k = 0; k < 4; k++) {
  994. w[j * 4 + k] = v[k];
  995. }
  996. }
  997. }
  998. ret[i] = arr;
  999. } break;
  1000. case VS::ARRAY_BONES: {
  1001. PoolVector<int> arr;
  1002. arr.resize(p_vertex_len * 4);
  1003. if (p_format & ARRAY_FLAG_USE_16_BIT_BONES) {
  1004. PoolVector<int>::Write w = arr.write();
  1005. for (int j = 0; j < p_vertex_len; j++) {
  1006. const uint16_t *v = (const uint16_t *)&r[j * total_elem_size + offsets[i]];
  1007. for (int k = 0; k < 4; k++) {
  1008. w[j * 4 + k] = v[k];
  1009. }
  1010. }
  1011. } else {
  1012. PoolVector<int>::Write w = arr.write();
  1013. for (int j = 0; j < p_vertex_len; j++) {
  1014. const uint8_t *v = (const uint8_t *)&r[j * total_elem_size + offsets[i]];
  1015. for (int k = 0; k < 4; k++) {
  1016. w[j * 4 + k] = v[k];
  1017. }
  1018. }
  1019. }
  1020. ret[i] = arr;
  1021. } break;
  1022. case VS::ARRAY_INDEX: {
  1023. /* determine wether using 16 or 32 bits indices */
  1024. PoolVector<uint8_t>::Read ir = p_index_data.read();
  1025. PoolVector<int> arr;
  1026. arr.resize(p_index_len);
  1027. if (p_vertex_len < (1 << 16)) {
  1028. PoolVector<int>::Write w = arr.write();
  1029. for (int j = 0; j < p_index_len; j++) {
  1030. const uint16_t *v = (const uint16_t *)&ir[j * 2];
  1031. w[j] = *v;
  1032. }
  1033. } else {
  1034. PoolVector<int>::Write w = arr.write();
  1035. for (int j = 0; j < p_index_len; j++) {
  1036. const int *v = (const int *)&ir[j * 4];
  1037. w[j] = *v;
  1038. }
  1039. }
  1040. ret[i] = arr;
  1041. } break;
  1042. default: {
  1043. ERR_FAIL_V(ret);
  1044. }
  1045. }
  1046. }
  1047. return ret;
  1048. }
  1049. Array VisualServer::mesh_surface_get_arrays(RID p_mesh, int p_surface) const {
  1050. PoolVector<uint8_t> vertex_data = mesh_surface_get_array(p_mesh, p_surface);
  1051. ERR_FAIL_COND_V(vertex_data.size() == 0, Array());
  1052. int vertex_len = mesh_surface_get_array_len(p_mesh, p_surface);
  1053. PoolVector<uint8_t> index_data = mesh_surface_get_index_array(p_mesh, p_surface);
  1054. int index_len = mesh_surface_get_array_index_len(p_mesh, p_surface);
  1055. uint32_t format = mesh_surface_get_format(p_mesh, p_surface);
  1056. return _get_array_from_surface(format, vertex_data, vertex_len, index_data, index_len);
  1057. }
  1058. void VisualServer::_bind_methods() {
  1059. ClassDB::bind_method(D_METHOD("texture_create"), &VisualServer::texture_create);
  1060. ClassDB::bind_method(D_METHOD("texture_create_from_image"), &VisualServer::texture_create_from_image, DEFVAL(TEXTURE_FLAGS_DEFAULT));
  1061. //ClassDB::bind_method(D_METHOD("texture_allocate"),&VisualServer::texture_allocate,DEFVAL( TEXTURE_FLAGS_DEFAULT ) );
  1062. //ClassDB::bind_method(D_METHOD("texture_set_data"),&VisualServer::texture_blit_rect,DEFVAL( CUBEMAP_LEFT ) );
  1063. //ClassDB::bind_method(D_METHOD("texture_get_rect"),&VisualServer::texture_get_rect );
  1064. ClassDB::bind_method(D_METHOD("texture_set_flags"), &VisualServer::texture_set_flags);
  1065. ClassDB::bind_method(D_METHOD("texture_get_flags"), &VisualServer::texture_get_flags);
  1066. ClassDB::bind_method(D_METHOD("texture_get_width"), &VisualServer::texture_get_width);
  1067. ClassDB::bind_method(D_METHOD("texture_get_height"), &VisualServer::texture_get_height);
  1068. ClassDB::bind_method(D_METHOD("texture_set_shrink_all_x2_on_set_data", "shrink"), &VisualServer::texture_set_shrink_all_x2_on_set_data);
  1069. }
  1070. 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) {
  1071. ERR_FAIL_COND(p_margins.size() != 4);
  1072. //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);
  1073. }
  1074. void VisualServer::_camera_set_orthogonal(RID p_camera, float p_size, float p_z_near, float p_z_far) {
  1075. camera_set_orthogonal(p_camera, p_size, p_z_near, p_z_far);
  1076. }
  1077. void VisualServer::mesh_add_surface_from_mesh_data(RID p_mesh, const Geometry::MeshData &p_mesh_data) {
  1078. #if 1
  1079. PoolVector<Vector3> vertices;
  1080. PoolVector<Vector3> normals;
  1081. for (int i = 0; i < p_mesh_data.faces.size(); i++) {
  1082. const Geometry::MeshData::Face &f = p_mesh_data.faces[i];
  1083. for (int j = 2; j < f.indices.size(); j++) {
  1084. #define _ADD_VERTEX(m_idx) \
  1085. vertices.push_back(p_mesh_data.vertices[f.indices[m_idx]]); \
  1086. normals.push_back(f.plane.normal);
  1087. _ADD_VERTEX(0);
  1088. _ADD_VERTEX(j - 1);
  1089. _ADD_VERTEX(j);
  1090. }
  1091. }
  1092. Array d;
  1093. d.resize(VS::ARRAY_MAX);
  1094. d[ARRAY_VERTEX] = vertices;
  1095. d[ARRAY_NORMAL] = normals;
  1096. mesh_add_surface_from_arrays(p_mesh, PRIMITIVE_TRIANGLES, d);
  1097. #else
  1098. PoolVector<Vector3> vertices;
  1099. for (int i = 0; i < p_mesh_data.edges.size(); i++) {
  1100. const Geometry::MeshData::Edge &f = p_mesh_data.edges[i];
  1101. vertices.push_back(p_mesh_data.vertices[f.a]);
  1102. vertices.push_back(p_mesh_data.vertices[f.b]);
  1103. }
  1104. Array d;
  1105. d.resize(VS::ARRAY_MAX);
  1106. d[ARRAY_VERTEX] = vertices;
  1107. mesh_add_surface(p_mesh, PRIMITIVE_LINES, d);
  1108. #endif
  1109. }
  1110. void VisualServer::mesh_add_surface_from_planes(RID p_mesh, const PoolVector<Plane> &p_planes) {
  1111. Geometry::MeshData mdata = Geometry::build_convex_mesh(p_planes);
  1112. mesh_add_surface_from_mesh_data(p_mesh, mdata);
  1113. }
  1114. void VisualServer::immediate_vertex_2d(RID p_immediate, const Vector2 &p_vertex) {
  1115. immediate_vertex(p_immediate, Vector3(p_vertex.x, p_vertex.y, 0));
  1116. }
  1117. RID VisualServer::instance_create2(RID p_base, RID p_scenario) {
  1118. RID instance = instance_create();
  1119. instance_set_base(instance, p_base);
  1120. instance_set_scenario(instance, p_scenario);
  1121. return instance;
  1122. }
  1123. VisualServer::VisualServer() {
  1124. //ERR_FAIL_COND(singleton);
  1125. singleton = this;
  1126. GLOBAL_DEF("rendering/vram_compression/import_s3tc", true);
  1127. GLOBAL_DEF("rendering/vram_compression/import_etc", false);
  1128. GLOBAL_DEF("rendering/vram_compression/import_etc2", true);
  1129. GLOBAL_DEF("rendering/vram_compression/import_pvrtc", false);
  1130. GLOBAL_DEF("rendering/quality/directional_shadow/size", 4096);
  1131. GLOBAL_DEF("rendering/quality/shadow_atlas/size", 4096);
  1132. GlobalConfig::get_singleton()->set_custom_property_info("rendering/shadow_atlas/size", PropertyInfo(Variant::INT, "rendering/shadow_atlas/size", PROPERTY_HINT_RANGE, "256,16384"));
  1133. GLOBAL_DEF("rendering/quality/shadow_atlas/quadrant_0_subdiv", 1);
  1134. GLOBAL_DEF("rendering/quality/shadow_atlas/quadrant_1_subdiv", 2);
  1135. GLOBAL_DEF("rendering/quality/shadow_atlas/quadrant_2_subdiv", 3);
  1136. GLOBAL_DEF("rendering/quality/shadow_atlas/quadrant_3_subdiv", 4);
  1137. GlobalConfig::get_singleton()->set_custom_property_info("rendering/quality/shadow_atlas/quadrant_0_subdiv", PropertyInfo(Variant::INT, "rendering/quality/shadow_atlas/quadrant_0_subdiv", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"));
  1138. GlobalConfig::get_singleton()->set_custom_property_info("rendering/quality/shadow_atlas/quadrant_1_subdiv", PropertyInfo(Variant::INT, "rendering/quality/shadow_atlas/quadrant_1_subdiv", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"));
  1139. GlobalConfig::get_singleton()->set_custom_property_info("rendering/quality/shadow_atlas/quadrant_2_subdiv", PropertyInfo(Variant::INT, "rendering/quality/shadow_atlas/quadrant_2_subdiv", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"));
  1140. GlobalConfig::get_singleton()->set_custom_property_info("rendering/quality/shadow_atlas/quadrant_3_subdiv", PropertyInfo(Variant::INT, "rendering/quality/shadow_atlas/quadrant_3_subdiv", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"));
  1141. }
  1142. VisualServer::~VisualServer() {
  1143. singleton = NULL;
  1144. }