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