visual_server.cpp 43 KB

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