visual_server.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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. uint8_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. for (int j = 0; j < p_vertex_len; j++) {
  888. const uint8_t *v = (const uint8_t *)&r[j * total_elem_size + offsets[i]];
  889. w[j] = Vector3(float(v[0] / 255.0) * 2.0 - 1.0, float(v[1] / 255.0) * 2.0 - 1.0, float(v[2] / 255.0) * 2.0 - 1.0);
  890. }
  891. } else {
  892. PoolVector<Vector3>::Write w = arr.write();
  893. for (int j = 0; j < p_vertex_len; j++) {
  894. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  895. w[j] = Vector3(v[0], v[1], v[2]);
  896. }
  897. }
  898. ret[i] = arr;
  899. } break;
  900. case VS::ARRAY_TANGENT: {
  901. PoolVector<float> arr;
  902. arr.resize(p_vertex_len * 4);
  903. if (p_format & ARRAY_COMPRESS_TANGENT) {
  904. PoolVector<float>::Write w = arr.write();
  905. for (int j = 0; j < p_vertex_len; j++) {
  906. const uint8_t *v = (const uint8_t *)&r[j * total_elem_size + offsets[i]];
  907. for (int k = 0; k < 4; k++) {
  908. w[j * 4 + k] = float(v[k] / 255.0) * 2.0 - 1.0;
  909. }
  910. }
  911. } else {
  912. PoolVector<float>::Write w = arr.write();
  913. for (int j = 0; j < p_vertex_len; j++) {
  914. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  915. for (int k = 0; k < 4; k++) {
  916. w[j * 4 + k] = v[k];
  917. }
  918. }
  919. }
  920. ret[i] = arr;
  921. } break;
  922. case VS::ARRAY_COLOR: {
  923. PoolVector<Color> arr;
  924. arr.resize(p_vertex_len);
  925. if (p_format & ARRAY_COMPRESS_COLOR) {
  926. PoolVector<Color>::Write w = arr.write();
  927. for (int j = 0; j < p_vertex_len; j++) {
  928. const uint8_t *v = (const uint8_t *)&r[j * total_elem_size + offsets[i]];
  929. w[j] = Color(float(v[0] / 255.0) * 2.0 - 1.0, float(v[1] / 255.0) * 2.0 - 1.0, float(v[2] / 255.0) * 2.0 - 1.0, float(v[3] / 255.0) * 2.0 - 1.0);
  930. }
  931. } else {
  932. PoolVector<Color>::Write w = arr.write();
  933. for (int j = 0; j < p_vertex_len; j++) {
  934. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  935. w[j] = Color(v[0], v[1], v[2], v[3]);
  936. }
  937. }
  938. ret[i] = arr;
  939. } break;
  940. case VS::ARRAY_TEX_UV: {
  941. PoolVector<Vector2> arr;
  942. arr.resize(p_vertex_len);
  943. if (p_format & ARRAY_COMPRESS_TEX_UV) {
  944. PoolVector<Vector2>::Write w = arr.write();
  945. for (int j = 0; j < p_vertex_len; j++) {
  946. const uint16_t *v = (const uint16_t *)&r[j * total_elem_size + offsets[i]];
  947. w[j] = Vector2(Math::halfptr_to_float(&v[0]), Math::halfptr_to_float(&v[1]));
  948. }
  949. } else {
  950. PoolVector<Vector2>::Write w = arr.write();
  951. for (int j = 0; j < p_vertex_len; j++) {
  952. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  953. w[j] = Vector2(v[0], v[1]);
  954. }
  955. }
  956. ret[i] = arr;
  957. } break;
  958. case VS::ARRAY_TEX_UV2: {
  959. PoolVector<Vector2> arr;
  960. arr.resize(p_vertex_len);
  961. if (p_format & ARRAY_COMPRESS_TEX_UV2) {
  962. PoolVector<Vector2>::Write w = arr.write();
  963. for (int j = 0; j < p_vertex_len; j++) {
  964. const uint16_t *v = (const uint16_t *)&r[j * total_elem_size + offsets[i]];
  965. w[j] = Vector2(Math::halfptr_to_float(&v[0]), Math::halfptr_to_float(&v[1]));
  966. }
  967. } else {
  968. PoolVector<Vector2>::Write w = arr.write();
  969. for (int j = 0; j < p_vertex_len; j++) {
  970. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  971. w[j] = Vector2(v[0], v[1]);
  972. }
  973. }
  974. ret[i] = arr;
  975. } break;
  976. case VS::ARRAY_WEIGHTS: {
  977. PoolVector<float> arr;
  978. arr.resize(p_vertex_len * 4);
  979. if (p_format & ARRAY_COMPRESS_WEIGHTS) {
  980. PoolVector<float>::Write w = arr.write();
  981. for (int j = 0; j < p_vertex_len; j++) {
  982. const uint16_t *v = (const uint16_t *)&r[j * total_elem_size + offsets[i]];
  983. for (int k = 0; k < 4; k++) {
  984. w[j * 4 + k] = float(v[k] / 65535.0) * 2.0 - 1.0;
  985. }
  986. }
  987. } else {
  988. PoolVector<float>::Write w = arr.write();
  989. for (int j = 0; j < p_vertex_len; j++) {
  990. const float *v = (const float *)&r[j * total_elem_size + offsets[i]];
  991. for (int k = 0; k < 4; k++) {
  992. w[j * 4 + k] = v[k];
  993. }
  994. }
  995. }
  996. ret[i] = arr;
  997. } break;
  998. case VS::ARRAY_BONES: {
  999. PoolVector<int> arr;
  1000. arr.resize(p_vertex_len * 4);
  1001. if (p_format & ARRAY_FLAG_USE_16_BIT_BONES) {
  1002. PoolVector<int>::Write w = arr.write();
  1003. for (int j = 0; j < p_vertex_len; j++) {
  1004. const uint16_t *v = (const uint16_t *)&r[j * total_elem_size + offsets[i]];
  1005. for (int k = 0; k < 4; k++) {
  1006. w[j * 4 + k] = v[k];
  1007. }
  1008. }
  1009. } else {
  1010. PoolVector<int>::Write w = arr.write();
  1011. for (int j = 0; j < p_vertex_len; j++) {
  1012. const uint8_t *v = (const uint8_t *)&r[j * total_elem_size + offsets[i]];
  1013. for (int k = 0; k < 4; k++) {
  1014. w[j * 4 + k] = v[k];
  1015. }
  1016. }
  1017. }
  1018. ret[i] = arr;
  1019. } break;
  1020. case VS::ARRAY_INDEX: {
  1021. /* determine wether using 16 or 32 bits indices */
  1022. PoolVector<uint8_t>::Read ir = p_index_data.read();
  1023. PoolVector<int> arr;
  1024. arr.resize(p_index_len);
  1025. if (p_vertex_len < (1 << 16)) {
  1026. PoolVector<int>::Write w = arr.write();
  1027. for (int j = 0; j < p_index_len; j++) {
  1028. const uint16_t *v = (const uint16_t *)&ir[j * 2];
  1029. w[j] = *v;
  1030. }
  1031. } else {
  1032. PoolVector<int>::Write w = arr.write();
  1033. for (int j = 0; j < p_index_len; j++) {
  1034. const int *v = (const int *)&ir[j * 4];
  1035. w[j] = *v;
  1036. }
  1037. }
  1038. ret[i] = arr;
  1039. } break;
  1040. default: {
  1041. ERR_FAIL_V(ret);
  1042. }
  1043. }
  1044. }
  1045. return ret;
  1046. }
  1047. Array VisualServer::mesh_surface_get_arrays(RID p_mesh, int p_surface) const {
  1048. PoolVector<uint8_t> vertex_data = mesh_surface_get_array(p_mesh, p_surface);
  1049. ERR_FAIL_COND_V(vertex_data.size() == 0, Array());
  1050. int vertex_len = mesh_surface_get_array_len(p_mesh, p_surface);
  1051. PoolVector<uint8_t> index_data = mesh_surface_get_index_array(p_mesh, p_surface);
  1052. int index_len = mesh_surface_get_array_index_len(p_mesh, p_surface);
  1053. uint32_t format = mesh_surface_get_format(p_mesh, p_surface);
  1054. return _get_array_from_surface(format, vertex_data, vertex_len, index_data, index_len);
  1055. }
  1056. void VisualServer::_bind_methods() {
  1057. ClassDB::bind_method(D_METHOD("texture_create"), &VisualServer::texture_create);
  1058. ClassDB::bind_method(D_METHOD("texture_create_from_image"), &VisualServer::texture_create_from_image, DEFVAL(TEXTURE_FLAGS_DEFAULT));
  1059. //ClassDB::bind_method(D_METHOD("texture_allocate"),&VisualServer::texture_allocate,DEFVAL( TEXTURE_FLAGS_DEFAULT ) );
  1060. //ClassDB::bind_method(D_METHOD("texture_set_data"),&VisualServer::texture_blit_rect,DEFVAL( CUBEMAP_LEFT ) );
  1061. //ClassDB::bind_method(D_METHOD("texture_get_rect"),&VisualServer::texture_get_rect );
  1062. ClassDB::bind_method(D_METHOD("texture_set_flags"), &VisualServer::texture_set_flags);
  1063. ClassDB::bind_method(D_METHOD("texture_get_flags"), &VisualServer::texture_get_flags);
  1064. ClassDB::bind_method(D_METHOD("texture_get_width"), &VisualServer::texture_get_width);
  1065. ClassDB::bind_method(D_METHOD("texture_get_height"), &VisualServer::texture_get_height);
  1066. ClassDB::bind_method(D_METHOD("texture_set_shrink_all_x2_on_set_data", "shrink"), &VisualServer::texture_set_shrink_all_x2_on_set_data);
  1067. }
  1068. 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) {
  1069. ERR_FAIL_COND(p_margins.size() != 4);
  1070. //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);
  1071. }
  1072. void VisualServer::_camera_set_orthogonal(RID p_camera, float p_size, float p_z_near, float p_z_far) {
  1073. camera_set_orthogonal(p_camera, p_size, p_z_near, p_z_far);
  1074. }
  1075. void VisualServer::mesh_add_surface_from_mesh_data(RID p_mesh, const Geometry::MeshData &p_mesh_data) {
  1076. #if 1
  1077. PoolVector<Vector3> vertices;
  1078. PoolVector<Vector3> normals;
  1079. for (int i = 0; i < p_mesh_data.faces.size(); i++) {
  1080. const Geometry::MeshData::Face &f = p_mesh_data.faces[i];
  1081. for (int j = 2; j < f.indices.size(); j++) {
  1082. #define _ADD_VERTEX(m_idx) \
  1083. vertices.push_back(p_mesh_data.vertices[f.indices[m_idx]]); \
  1084. normals.push_back(f.plane.normal);
  1085. _ADD_VERTEX(0);
  1086. _ADD_VERTEX(j - 1);
  1087. _ADD_VERTEX(j);
  1088. }
  1089. }
  1090. Array d;
  1091. d.resize(VS::ARRAY_MAX);
  1092. d[ARRAY_VERTEX] = vertices;
  1093. d[ARRAY_NORMAL] = normals;
  1094. mesh_add_surface_from_arrays(p_mesh, PRIMITIVE_TRIANGLES, d);
  1095. #else
  1096. PoolVector<Vector3> vertices;
  1097. for (int i = 0; i < p_mesh_data.edges.size(); i++) {
  1098. const Geometry::MeshData::Edge &f = p_mesh_data.edges[i];
  1099. vertices.push_back(p_mesh_data.vertices[f.a]);
  1100. vertices.push_back(p_mesh_data.vertices[f.b]);
  1101. }
  1102. Array d;
  1103. d.resize(VS::ARRAY_MAX);
  1104. d[ARRAY_VERTEX] = vertices;
  1105. mesh_add_surface(p_mesh, PRIMITIVE_LINES, d);
  1106. #endif
  1107. }
  1108. void VisualServer::mesh_add_surface_from_planes(RID p_mesh, const PoolVector<Plane> &p_planes) {
  1109. Geometry::MeshData mdata = Geometry::build_convex_mesh(p_planes);
  1110. mesh_add_surface_from_mesh_data(p_mesh, mdata);
  1111. }
  1112. void VisualServer::immediate_vertex_2d(RID p_immediate, const Vector2 &p_vertex) {
  1113. immediate_vertex(p_immediate, Vector3(p_vertex.x, p_vertex.y, 0));
  1114. }
  1115. RID VisualServer::instance_create2(RID p_base, RID p_scenario) {
  1116. RID instance = instance_create();
  1117. instance_set_base(instance, p_base);
  1118. instance_set_scenario(instance, p_scenario);
  1119. return instance;
  1120. }
  1121. VisualServer::VisualServer() {
  1122. //ERR_FAIL_COND(singleton);
  1123. singleton = this;
  1124. }
  1125. VisualServer::~VisualServer() {
  1126. singleton = NULL;
  1127. }