visual_server.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*************************************************************************/
  2. /* visual_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "globals.h"
  32. #include "method_bind_ext.gen.inc"
  33. VisualServer *VisualServer::singleton = NULL;
  34. VisualServer *(*VisualServer::create_func)() = NULL;
  35. VisualServer *VisualServer::get_singleton() {
  36. return singleton;
  37. }
  38. void VisualServer::set_mipmap_policy(MipMapPolicy p_policy) {
  39. mm_policy = p_policy;
  40. }
  41. VisualServer::MipMapPolicy VisualServer::get_mipmap_policy() const {
  42. return (VisualServer::MipMapPolicy)mm_policy;
  43. }
  44. DVector<String> VisualServer::_shader_get_param_list(RID p_shader) const {
  45. //remove at some point
  46. DVector<String> pl;
  47. #if 0
  48. List<StringName> params;
  49. shader_get_param_list(p_shader,&params);
  50. for(List<StringName>::Element *E=params.front();E;E=E->next()) {
  51. pl.push_back(E->get());
  52. }
  53. #endif
  54. return pl;
  55. }
  56. VisualServer *VisualServer::create() {
  57. ERR_FAIL_COND_V(singleton, NULL);
  58. if (create_func)
  59. return create_func();
  60. return NULL;
  61. }
  62. RID VisualServer::texture_create_from_image(const Image &p_image, uint32_t p_flags) {
  63. RID texture = texture_create();
  64. texture_allocate(texture, p_image.get_width(), p_image.get_height(), p_image.get_format(), p_flags); //if it has mipmaps, use, else generate
  65. ERR_FAIL_COND_V(!texture.is_valid(), texture);
  66. texture_set_data(texture, p_image);
  67. return texture;
  68. }
  69. RID VisualServer::get_test_texture() {
  70. if (test_texture) {
  71. return test_texture;
  72. };
  73. #define TEST_TEXTURE_SIZE 256
  74. Image data(TEST_TEXTURE_SIZE, TEST_TEXTURE_SIZE, 0, Image::FORMAT_RGB);
  75. for (int x = 0; x < TEST_TEXTURE_SIZE; x++) {
  76. for (int y = 0; y < TEST_TEXTURE_SIZE; y++) {
  77. Color c;
  78. int r = 255 - (x + y) / 2;
  79. if ((x % (TEST_TEXTURE_SIZE / 8)) < 2 || (y % (TEST_TEXTURE_SIZE / 8)) < 2) {
  80. c.r = y;
  81. c.g = r;
  82. c.b = x;
  83. } else {
  84. c.r = r;
  85. c.g = x;
  86. c.b = y;
  87. }
  88. data.put_pixel(x, y, c);
  89. }
  90. }
  91. test_texture = texture_create_from_image(data);
  92. return test_texture;
  93. };
  94. void VisualServer::_free_internal_rids() {
  95. if (test_texture.is_valid())
  96. free(test_texture);
  97. if (white_texture.is_valid())
  98. free(white_texture);
  99. if (test_material.is_valid())
  100. free(test_material);
  101. for (int i = 0; i < 32; i++) {
  102. if (material_2d[i].is_valid())
  103. free(material_2d[i]);
  104. }
  105. }
  106. RID VisualServer::_make_test_cube() {
  107. DVector<Vector3> vertices;
  108. DVector<Vector3> normals;
  109. DVector<float> tangents;
  110. DVector<Vector3> uvs;
  111. int vtx_idx = 0;
  112. #define ADD_VTX(m_idx) \
  113. vertices.push_back(face_points[m_idx]); \
  114. normals.push_back(normal_points[m_idx]); \
  115. tangents.push_back(normal_points[m_idx][1]); \
  116. tangents.push_back(normal_points[m_idx][2]); \
  117. tangents.push_back(normal_points[m_idx][0]); \
  118. tangents.push_back(1.0); \
  119. uvs.push_back(Vector3(uv_points[m_idx * 2 + 0], uv_points[m_idx * 2 + 1], 0)); \
  120. vtx_idx++;
  121. for (int i = 0; i < 6; i++) {
  122. Vector3 face_points[4];
  123. Vector3 normal_points[4];
  124. float uv_points[8] = { 0, 0, 0, 1, 1, 1, 1, 0 };
  125. for (int j = 0; j < 4; j++) {
  126. float v[3];
  127. v[0] = 1.0;
  128. v[1] = 1 - 2 * ((j >> 1) & 1);
  129. v[2] = v[1] * (1 - 2 * (j & 1));
  130. for (int k = 0; k < 3; k++) {
  131. if (i < 3)
  132. face_points[j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  133. else
  134. face_points[3 - j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  135. }
  136. normal_points[j] = Vector3();
  137. normal_points[j][i % 3] = (i >= 3 ? -1 : 1);
  138. }
  139. //tri 1
  140. ADD_VTX(0);
  141. ADD_VTX(1);
  142. ADD_VTX(2);
  143. //tri 2
  144. ADD_VTX(2);
  145. ADD_VTX(3);
  146. ADD_VTX(0);
  147. }
  148. RID test_cube = mesh_create();
  149. Array d;
  150. d.resize(VS::ARRAY_MAX);
  151. d[VisualServer::ARRAY_NORMAL] = normals;
  152. d[VisualServer::ARRAY_TANGENT] = tangents;
  153. d[VisualServer::ARRAY_TEX_UV] = uvs;
  154. d[VisualServer::ARRAY_VERTEX] = vertices;
  155. DVector<int> indices;
  156. indices.resize(vertices.size());
  157. for (int i = 0; i < vertices.size(); i++)
  158. indices.set(i, i);
  159. d[VisualServer::ARRAY_INDEX] = indices;
  160. mesh_add_surface(test_cube, PRIMITIVE_TRIANGLES, d);
  161. test_material = fixed_material_create();
  162. //material_set_flag(material, MATERIAL_FLAG_BILLBOARD_TOGGLE,true);
  163. fixed_material_set_texture(test_material, FIXED_MATERIAL_PARAM_DIFFUSE, get_test_texture());
  164. fixed_material_set_param(test_material, FIXED_MATERIAL_PARAM_SPECULAR_EXP, 70);
  165. fixed_material_set_param(test_material, FIXED_MATERIAL_PARAM_EMISSION, Color(0.2, 0.2, 0.2));
  166. fixed_material_set_param(test_material, FIXED_MATERIAL_PARAM_DIFFUSE, Color(1, 1, 1));
  167. fixed_material_set_param(test_material, FIXED_MATERIAL_PARAM_SPECULAR, Color(1, 1, 1));
  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. DVector<Vector3> vertices;
  173. DVector<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(mesh, PRIMITIVE_TRIANGLES, d);
  211. return mesh;
  212. }
  213. RID VisualServer::material_2d_get(bool p_shaded, bool p_transparent, bool p_double_sided, bool p_ontop, bool p_cut_alpha, bool p_opaque_prepass) {
  214. int version = 0;
  215. if (p_shaded)
  216. version = 1;
  217. if (p_transparent)
  218. version |= 2;
  219. if (p_cut_alpha)
  220. version |= 4;
  221. if (p_opaque_prepass)
  222. version |= 8;
  223. if (p_double_sided)
  224. version |= 16;
  225. if (p_ontop)
  226. version |= 32;
  227. if (material_2d[version].is_valid())
  228. return material_2d[version];
  229. //not valid, make
  230. material_2d[version] = fixed_material_create();
  231. fixed_material_set_flag(material_2d[version], FIXED_MATERIAL_FLAG_USE_ALPHA, p_transparent);
  232. fixed_material_set_flag(material_2d[version], FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY, true);
  233. fixed_material_set_flag(material_2d[version], FIXED_MATERIAL_FLAG_DISCARD_ALPHA, p_cut_alpha);
  234. material_set_flag(material_2d[version], MATERIAL_FLAG_UNSHADED, !p_shaded);
  235. material_set_flag(material_2d[version], MATERIAL_FLAG_DOUBLE_SIDED, p_double_sided);
  236. material_set_flag(material_2d[version], MATERIAL_FLAG_ONTOP, p_ontop);
  237. material_set_depth_draw_mode(material_2d[version], p_opaque_prepass ? MATERIAL_DEPTH_DRAW_OPAQUE_PRE_PASS_ALPHA : MATERIAL_DEPTH_DRAW_OPAQUE_ONLY);
  238. fixed_material_set_texture(material_2d[version], FIXED_MATERIAL_PARAM_DIFFUSE, get_white_texture());
  239. //material cut alpha?
  240. return material_2d[version];
  241. }
  242. RID VisualServer::get_white_texture() {
  243. if (white_texture.is_valid())
  244. return white_texture;
  245. DVector<uint8_t> wt;
  246. wt.resize(16 * 3);
  247. {
  248. DVector<uint8_t>::Write w = wt.write();
  249. for (int i = 0; i < 16 * 3; i++)
  250. w[i] = 255;
  251. }
  252. Image white(4, 4, 0, Image::FORMAT_RGB, wt);
  253. white_texture = texture_create();
  254. texture_allocate(white_texture, 4, 4, Image::FORMAT_RGB);
  255. texture_set_data(white_texture, white);
  256. return white_texture;
  257. }
  258. void VisualServer::_bind_methods() {
  259. ObjectTypeDB::bind_method(_MD("texture_create"), &VisualServer::texture_create);
  260. ObjectTypeDB::bind_method(_MD("texture_create_from_image"), &VisualServer::texture_create_from_image, DEFVAL(TEXTURE_FLAGS_DEFAULT));
  261. //ObjectTypeDB::bind_method(_MD("texture_allocate"),&VisualServer::texture_allocate,DEFVAL( TEXTURE_FLAGS_DEFAULT ) );
  262. //ObjectTypeDB::bind_method(_MD("texture_set_data"),&VisualServer::texture_blit_rect,DEFVAL( CUBEMAP_LEFT ) );
  263. //ObjectTypeDB::bind_method(_MD("texture_get_rect"),&VisualServer::texture_get_rect );
  264. ObjectTypeDB::bind_method(_MD("texture_set_flags"), &VisualServer::texture_set_flags);
  265. ObjectTypeDB::bind_method(_MD("texture_get_flags"), &VisualServer::texture_get_flags);
  266. ObjectTypeDB::bind_method(_MD("texture_get_width"), &VisualServer::texture_get_width);
  267. ObjectTypeDB::bind_method(_MD("texture_get_height"), &VisualServer::texture_get_height);
  268. ObjectTypeDB::bind_method(_MD("texture_set_shrink_all_x2_on_set_data", "shrink"), &VisualServer::texture_set_shrink_all_x2_on_set_data);
  269. #ifndef _3D_DISABLED
  270. ObjectTypeDB::bind_method(_MD("shader_create", "mode"), &VisualServer::shader_create, DEFVAL(SHADER_MATERIAL));
  271. ObjectTypeDB::bind_method(_MD("shader_set_mode", "shader", "mode"), &VisualServer::shader_set_mode);
  272. ObjectTypeDB::bind_method(_MD("material_create"), &VisualServer::material_create);
  273. ObjectTypeDB::bind_method(_MD("material_set_shader", "shader"), &VisualServer::material_set_shader);
  274. ObjectTypeDB::bind_method(_MD("material_get_shader"), &VisualServer::material_get_shader);
  275. ObjectTypeDB::bind_method(_MD("material_set_param"), &VisualServer::material_set_param);
  276. ObjectTypeDB::bind_method(_MD("material_get_param"), &VisualServer::material_get_param);
  277. ObjectTypeDB::bind_method(_MD("material_set_flag"), &VisualServer::material_set_flag);
  278. ObjectTypeDB::bind_method(_MD("material_get_flag"), &VisualServer::material_get_flag);
  279. ObjectTypeDB::bind_method(_MD("material_set_blend_mode"), &VisualServer::material_set_blend_mode);
  280. ObjectTypeDB::bind_method(_MD("material_get_blend_mode"), &VisualServer::material_get_blend_mode);
  281. ObjectTypeDB::bind_method(_MD("material_set_line_width"), &VisualServer::material_set_line_width);
  282. ObjectTypeDB::bind_method(_MD("material_get_line_width"), &VisualServer::material_get_line_width);
  283. ObjectTypeDB::bind_method(_MD("mesh_create"), &VisualServer::mesh_create);
  284. ObjectTypeDB::bind_method(_MD("mesh_add_surface"), &VisualServer::mesh_add_surface, DEFVAL(Array()), DEFVAL(false));
  285. ObjectTypeDB::bind_method(_MD("mesh_surface_set_material"), &VisualServer::mesh_surface_set_material, DEFVAL(false));
  286. ObjectTypeDB::bind_method(_MD("mesh_surface_get_material"), &VisualServer::mesh_surface_get_material);
  287. ObjectTypeDB::bind_method(_MD("mesh_surface_get_array_len"), &VisualServer::mesh_surface_get_array_len);
  288. ObjectTypeDB::bind_method(_MD("mesh_surface_get_array_index_len"), &VisualServer::mesh_surface_get_array_index_len);
  289. ObjectTypeDB::bind_method(_MD("mesh_surface_get_format"), &VisualServer::mesh_surface_get_format);
  290. ObjectTypeDB::bind_method(_MD("mesh_surface_get_primitive_type"), &VisualServer::mesh_surface_get_primitive_type);
  291. ObjectTypeDB::bind_method(_MD("mesh_remove_surface"), &VisualServer::mesh_remove_surface);
  292. ObjectTypeDB::bind_method(_MD("mesh_get_surface_count"), &VisualServer::mesh_get_surface_count);
  293. ObjectTypeDB::bind_method(_MD("multimesh_create"), &VisualServer::multimesh_create);
  294. ObjectTypeDB::bind_method(_MD("multimesh_set_mesh"), &VisualServer::multimesh_set_mesh);
  295. ObjectTypeDB::bind_method(_MD("multimesh_set_aabb"), &VisualServer::multimesh_set_aabb);
  296. ObjectTypeDB::bind_method(_MD("multimesh_instance_set_transform"), &VisualServer::multimesh_instance_set_transform);
  297. ObjectTypeDB::bind_method(_MD("multimesh_instance_set_color"), &VisualServer::multimesh_instance_set_color);
  298. ObjectTypeDB::bind_method(_MD("multimesh_get_mesh"), &VisualServer::multimesh_get_mesh);
  299. ObjectTypeDB::bind_method(_MD("multimesh_get_aabb"), &VisualServer::multimesh_get_aabb);
  300. ObjectTypeDB::bind_method(_MD("multimesh_instance_get_transform"), &VisualServer::multimesh_instance_get_transform);
  301. ObjectTypeDB::bind_method(_MD("multimesh_instance_get_color"), &VisualServer::multimesh_instance_get_color);
  302. ObjectTypeDB::bind_method(_MD("particles_create"), &VisualServer::particles_create);
  303. ObjectTypeDB::bind_method(_MD("particles_set_amount"), &VisualServer::particles_set_amount);
  304. ObjectTypeDB::bind_method(_MD("particles_get_amount"), &VisualServer::particles_get_amount);
  305. ObjectTypeDB::bind_method(_MD("particles_set_emitting"), &VisualServer::particles_set_emitting);
  306. ObjectTypeDB::bind_method(_MD("particles_is_emitting"), &VisualServer::particles_is_emitting);
  307. ObjectTypeDB::bind_method(_MD("particles_set_visibility_aabb"), &VisualServer::particles_set_visibility_aabb);
  308. ObjectTypeDB::bind_method(_MD("particles_get_visibility_aabb"), &VisualServer::particles_get_visibility_aabb);
  309. ObjectTypeDB::bind_method(_MD("particles_set_variable"), &VisualServer::particles_set_variable);
  310. ObjectTypeDB::bind_method(_MD("particles_get_variable"), &VisualServer::particles_get_variable);
  311. ObjectTypeDB::bind_method(_MD("particles_set_randomness"), &VisualServer::particles_set_randomness);
  312. ObjectTypeDB::bind_method(_MD("particles_get_randomness"), &VisualServer::particles_get_randomness);
  313. ObjectTypeDB::bind_method(_MD("particles_set_color_phases"), &VisualServer::particles_set_color_phases);
  314. ObjectTypeDB::bind_method(_MD("particles_get_color_phases"), &VisualServer::particles_get_color_phases);
  315. ObjectTypeDB::bind_method(_MD("particles_set_color_phase_pos"), &VisualServer::particles_set_color_phase_pos);
  316. ObjectTypeDB::bind_method(_MD("particles_get_color_phase_pos"), &VisualServer::particles_get_color_phase_pos);
  317. ObjectTypeDB::bind_method(_MD("particles_set_color_phase_color"), &VisualServer::particles_set_color_phase_color);
  318. ObjectTypeDB::bind_method(_MD("particles_get_color_phase_color"), &VisualServer::particles_get_color_phase_color);
  319. ObjectTypeDB::bind_method(_MD("particles_set_attractors"), &VisualServer::particles_set_attractors);
  320. ObjectTypeDB::bind_method(_MD("particles_get_attractors"), &VisualServer::particles_get_attractors);
  321. ObjectTypeDB::bind_method(_MD("particles_set_attractor_pos"), &VisualServer::particles_set_attractor_pos);
  322. ObjectTypeDB::bind_method(_MD("particles_get_attractor_pos"), &VisualServer::particles_get_attractor_pos);
  323. ObjectTypeDB::bind_method(_MD("particles_set_attractor_strength"), &VisualServer::particles_set_attractor_strength);
  324. ObjectTypeDB::bind_method(_MD("particles_get_attractor_strength"), &VisualServer::particles_get_attractor_strength);
  325. ObjectTypeDB::bind_method(_MD("particles_set_material"), &VisualServer::particles_set_material, DEFVAL(false));
  326. ObjectTypeDB::bind_method(_MD("particles_set_height_from_velocity"), &VisualServer::particles_set_height_from_velocity);
  327. ObjectTypeDB::bind_method(_MD("particles_has_height_from_velocity"), &VisualServer::particles_has_height_from_velocity);
  328. ObjectTypeDB::bind_method(_MD("light_create"), &VisualServer::light_create);
  329. ObjectTypeDB::bind_method(_MD("light_get_type"), &VisualServer::light_get_type);
  330. ObjectTypeDB::bind_method(_MD("light_set_color"), &VisualServer::light_set_color);
  331. ObjectTypeDB::bind_method(_MD("light_get_color"), &VisualServer::light_get_color);
  332. ObjectTypeDB::bind_method(_MD("light_set_shadow"), &VisualServer::light_set_shadow);
  333. ObjectTypeDB::bind_method(_MD("light_has_shadow"), &VisualServer::light_has_shadow);
  334. ObjectTypeDB::bind_method(_MD("light_set_volumetric"), &VisualServer::light_set_volumetric);
  335. ObjectTypeDB::bind_method(_MD("light_is_volumetric"), &VisualServer::light_is_volumetric);
  336. ObjectTypeDB::bind_method(_MD("light_set_projector"), &VisualServer::light_set_projector);
  337. ObjectTypeDB::bind_method(_MD("light_get_projector"), &VisualServer::light_get_projector);
  338. ObjectTypeDB::bind_method(_MD("light_set_var"), &VisualServer::light_set_param);
  339. ObjectTypeDB::bind_method(_MD("light_get_var"), &VisualServer::light_get_param);
  340. ObjectTypeDB::bind_method(_MD("skeleton_create"), &VisualServer::skeleton_create);
  341. ObjectTypeDB::bind_method(_MD("skeleton_resize"), &VisualServer::skeleton_resize);
  342. ObjectTypeDB::bind_method(_MD("skeleton_get_bone_count"), &VisualServer::skeleton_get_bone_count);
  343. ObjectTypeDB::bind_method(_MD("skeleton_bone_set_transform"), &VisualServer::skeleton_bone_set_transform);
  344. ObjectTypeDB::bind_method(_MD("skeleton_bone_get_transform"), &VisualServer::skeleton_bone_get_transform);
  345. ObjectTypeDB::bind_method(_MD("room_create"), &VisualServer::room_create);
  346. ObjectTypeDB::bind_method(_MD("room_set_bounds"), &VisualServer::room_set_bounds);
  347. ObjectTypeDB::bind_method(_MD("room_get_bounds"), &VisualServer::room_get_bounds);
  348. ObjectTypeDB::bind_method(_MD("portal_create"), &VisualServer::portal_create);
  349. ObjectTypeDB::bind_method(_MD("portal_set_shape"), &VisualServer::portal_set_shape);
  350. ObjectTypeDB::bind_method(_MD("portal_get_shape"), &VisualServer::portal_get_shape);
  351. ObjectTypeDB::bind_method(_MD("portal_set_enabled"), &VisualServer::portal_set_enabled);
  352. ObjectTypeDB::bind_method(_MD("portal_is_enabled"), &VisualServer::portal_is_enabled);
  353. ObjectTypeDB::bind_method(_MD("portal_set_disable_distance"), &VisualServer::portal_set_disable_distance);
  354. ObjectTypeDB::bind_method(_MD("portal_get_disable_distance"), &VisualServer::portal_get_disable_distance);
  355. ObjectTypeDB::bind_method(_MD("portal_set_disabled_color"), &VisualServer::portal_set_disabled_color);
  356. ObjectTypeDB::bind_method(_MD("portal_get_disabled_color"), &VisualServer::portal_get_disabled_color);
  357. ObjectTypeDB::bind_method(_MD("camera_create"), &VisualServer::camera_create);
  358. ObjectTypeDB::bind_method(_MD("camera_set_perspective"), &VisualServer::camera_set_perspective);
  359. ObjectTypeDB::bind_method(_MD("camera_set_orthogonal"), &VisualServer::_camera_set_orthogonal);
  360. ObjectTypeDB::bind_method(_MD("camera_set_transform"), &VisualServer::camera_set_transform);
  361. ObjectTypeDB::bind_method(_MD("viewport_create"), &VisualServer::viewport_create);
  362. ObjectTypeDB::bind_method(_MD("viewport_set_rect"), &VisualServer::_viewport_set_rect);
  363. ObjectTypeDB::bind_method(_MD("viewport_get_rect"), &VisualServer::_viewport_get_rect);
  364. ObjectTypeDB::bind_method(_MD("viewport_attach_camera"), &VisualServer::viewport_attach_camera, DEFVAL(RID()));
  365. ObjectTypeDB::bind_method(_MD("viewport_get_attached_camera"), &VisualServer::viewport_get_attached_camera);
  366. ObjectTypeDB::bind_method(_MD("viewport_get_scenario"), &VisualServer::viewport_get_scenario);
  367. ObjectTypeDB::bind_method(_MD("viewport_attach_canvas"), &VisualServer::viewport_attach_canvas);
  368. ObjectTypeDB::bind_method(_MD("viewport_remove_canvas"), &VisualServer::viewport_remove_canvas);
  369. ObjectTypeDB::bind_method(_MD("viewport_set_global_canvas_transform"), &VisualServer::viewport_set_global_canvas_transform);
  370. ObjectTypeDB::bind_method(_MD("scenario_create"), &VisualServer::scenario_create);
  371. ObjectTypeDB::bind_method(_MD("scenario_set_debug"), &VisualServer::scenario_set_debug);
  372. ObjectTypeDB::bind_method(_MD("instance_create"), &VisualServer::instance_create, DEFVAL(RID()));
  373. ObjectTypeDB::bind_method(_MD("instance_get_base"), &VisualServer::instance_get_base);
  374. ObjectTypeDB::bind_method(_MD("instance_get_base_aabb"), &VisualServer::instance_get_base);
  375. ObjectTypeDB::bind_method(_MD("instance_set_transform"), &VisualServer::instance_set_transform);
  376. ObjectTypeDB::bind_method(_MD("instance_get_transform"), &VisualServer::instance_get_transform);
  377. ObjectTypeDB::bind_method(_MD("instance_attach_object_instance_ID"), &VisualServer::instance_attach_object_instance_ID);
  378. ObjectTypeDB::bind_method(_MD("instance_get_object_instance_ID"), &VisualServer::instance_get_object_instance_ID);
  379. ObjectTypeDB::bind_method(_MD("instance_attach_skeleton"), &VisualServer::instance_attach_skeleton);
  380. ObjectTypeDB::bind_method(_MD("instance_get_skeleton"), &VisualServer::instance_get_skeleton);
  381. ObjectTypeDB::bind_method(_MD("instance_set_room"), &VisualServer::instance_set_room);
  382. ObjectTypeDB::bind_method(_MD("instance_get_room"), &VisualServer::instance_get_room);
  383. ObjectTypeDB::bind_method(_MD("instance_set_exterior"), &VisualServer::instance_set_exterior);
  384. ObjectTypeDB::bind_method(_MD("instance_is_exterior"), &VisualServer::instance_is_exterior);
  385. ObjectTypeDB::bind_method(_MD("instances_cull_aabb"), &VisualServer::instances_cull_aabb);
  386. ObjectTypeDB::bind_method(_MD("instances_cull_ray"), &VisualServer::instances_cull_ray);
  387. ObjectTypeDB::bind_method(_MD("instances_cull_convex"), &VisualServer::instances_cull_convex);
  388. ObjectTypeDB::bind_method(_MD("instance_geometry_override_material_param"), &VisualServer::instance_get_room);
  389. ObjectTypeDB::bind_method(_MD("instance_geometry_get_material_param"), &VisualServer::instance_get_room);
  390. ObjectTypeDB::bind_method(_MD("get_test_cube"), &VisualServer::get_test_cube);
  391. #endif
  392. ObjectTypeDB::bind_method(_MD("canvas_create"), &VisualServer::canvas_create);
  393. ObjectTypeDB::bind_method(_MD("canvas_item_create"), &VisualServer::canvas_item_create);
  394. ObjectTypeDB::bind_method(_MD("canvas_item_set_parent"), &VisualServer::canvas_item_set_parent);
  395. ObjectTypeDB::bind_method(_MD("canvas_item_get_parent"), &VisualServer::canvas_item_get_parent);
  396. ObjectTypeDB::bind_method(_MD("canvas_item_set_transform"), &VisualServer::canvas_item_set_transform);
  397. ObjectTypeDB::bind_method(_MD("canvas_item_set_custom_rect"), &VisualServer::canvas_item_set_custom_rect);
  398. ObjectTypeDB::bind_method(_MD("canvas_item_set_clip"), &VisualServer::canvas_item_set_clip);
  399. ObjectTypeDB::bind_method(_MD("canvas_item_set_opacity"), &VisualServer::canvas_item_set_opacity);
  400. ObjectTypeDB::bind_method(_MD("canvas_item_get_opacity"), &VisualServer::canvas_item_get_opacity);
  401. ObjectTypeDB::bind_method(_MD("canvas_item_set_self_opacity"), &VisualServer::canvas_item_set_self_opacity);
  402. ObjectTypeDB::bind_method(_MD("canvas_item_get_self_opacity"), &VisualServer::canvas_item_get_self_opacity);
  403. ObjectTypeDB::bind_method(_MD("canvas_item_set_z"), &VisualServer::canvas_item_set_z);
  404. ObjectTypeDB::bind_method(_MD("canvas_item_set_sort_children_by_y"), &VisualServer::canvas_item_set_sort_children_by_y);
  405. ObjectTypeDB::bind_method(_MD("canvas_item_add_line"), &VisualServer::canvas_item_add_line, DEFVAL(1.0));
  406. ObjectTypeDB::bind_method(_MD("canvas_item_add_rect"), &VisualServer::canvas_item_add_rect);
  407. ObjectTypeDB::bind_method(_MD("canvas_item_add_texture_rect"), &VisualServer::canvas_item_add_texture_rect, DEFVAL(Color(1, 1, 1)), DEFVAL(false));
  408. ObjectTypeDB::bind_method(_MD("canvas_item_add_texture_rect_region"), &VisualServer::canvas_item_add_texture_rect_region, DEFVAL(Color(1, 1, 1)), DEFVAL(false));
  409. ObjectTypeDB::bind_method(_MD("canvas_item_add_style_box"), &VisualServer::_canvas_item_add_style_box, DEFVAL(Color(1, 1, 1)));
  410. // ObjectTypeDB::bind_method(_MD("canvas_item_add_primitive"),&VisualServer::canvas_item_add_primitive,DEFVAL(Vector<Vector2>()),DEFVAL(RID()));
  411. ObjectTypeDB::bind_method(_MD("canvas_item_add_circle"), &VisualServer::canvas_item_add_circle);
  412. ObjectTypeDB::bind_method(_MD("viewport_set_canvas_transform"), &VisualServer::viewport_set_canvas_transform);
  413. ObjectTypeDB::bind_method(_MD("canvas_item_clear"), &VisualServer::canvas_item_clear);
  414. ObjectTypeDB::bind_method(_MD("canvas_item_raise"), &VisualServer::canvas_item_raise);
  415. ObjectTypeDB::bind_method(_MD("black_bars_set_margins", "left", "top", "right", "bottom"), &VisualServer::black_bars_set_margins);
  416. ObjectTypeDB::bind_method(_MD("black_bars_set_images", "left", "top", "right", "bottom"), &VisualServer::black_bars_set_images);
  417. ObjectTypeDB::bind_method(_MD("make_sphere_mesh"), &VisualServer::make_sphere_mesh);
  418. ObjectTypeDB::bind_method(_MD("mesh_add_surface_from_planes"), &VisualServer::mesh_add_surface_from_planes);
  419. ObjectTypeDB::bind_method(_MD("draw"), &VisualServer::draw);
  420. ObjectTypeDB::bind_method(_MD("sync"), &VisualServer::sync);
  421. ObjectTypeDB::bind_method(_MD("free_rid"), &VisualServer::free);
  422. ObjectTypeDB::bind_method(_MD("set_default_clear_color"), &VisualServer::set_default_clear_color);
  423. ObjectTypeDB::bind_method(_MD("get_default_clear_color"), &VisualServer::get_default_clear_color);
  424. ObjectTypeDB::bind_method(_MD("set_time_scale"), &VisualServer::set_time_scale);
  425. ObjectTypeDB::bind_method(_MD("get_render_info"), &VisualServer::get_render_info);
  426. BIND_CONSTANT(NO_INDEX_ARRAY);
  427. BIND_CONSTANT(CUSTOM_ARRAY_SIZE);
  428. BIND_CONSTANT(ARRAY_WEIGHTS_SIZE);
  429. BIND_CONSTANT(MAX_PARTICLE_COLOR_PHASES);
  430. BIND_CONSTANT(MAX_PARTICLE_ATTRACTORS);
  431. BIND_CONSTANT(MAX_CURSORS);
  432. BIND_CONSTANT(TEXTURE_FLAG_MIPMAPS);
  433. BIND_CONSTANT(TEXTURE_FLAG_REPEAT);
  434. BIND_CONSTANT(TEXTURE_FLAG_FILTER);
  435. BIND_CONSTANT(TEXTURE_FLAG_CUBEMAP);
  436. BIND_CONSTANT(TEXTURE_FLAGS_DEFAULT);
  437. BIND_CONSTANT(CUBEMAP_LEFT);
  438. BIND_CONSTANT(CUBEMAP_RIGHT);
  439. BIND_CONSTANT(CUBEMAP_BOTTOM);
  440. BIND_CONSTANT(CUBEMAP_TOP);
  441. BIND_CONSTANT(CUBEMAP_FRONT);
  442. BIND_CONSTANT(CUBEMAP_BACK);
  443. BIND_CONSTANT(SHADER_MATERIAL); ///< param 0: name
  444. BIND_CONSTANT(SHADER_POST_PROCESS); ///< param 0: name
  445. BIND_CONSTANT(MATERIAL_FLAG_VISIBLE);
  446. BIND_CONSTANT(MATERIAL_FLAG_DOUBLE_SIDED);
  447. BIND_CONSTANT(MATERIAL_FLAG_INVERT_FACES);
  448. BIND_CONSTANT(MATERIAL_FLAG_UNSHADED);
  449. BIND_CONSTANT(MATERIAL_FLAG_ONTOP);
  450. BIND_CONSTANT(MATERIAL_FLAG_MAX);
  451. BIND_CONSTANT(MATERIAL_BLEND_MODE_MIX);
  452. BIND_CONSTANT(MATERIAL_BLEND_MODE_ADD);
  453. BIND_CONSTANT(MATERIAL_BLEND_MODE_SUB);
  454. BIND_CONSTANT(MATERIAL_BLEND_MODE_MUL);
  455. BIND_CONSTANT(FIXED_MATERIAL_PARAM_DIFFUSE);
  456. BIND_CONSTANT(FIXED_MATERIAL_PARAM_DETAIL);
  457. BIND_CONSTANT(FIXED_MATERIAL_PARAM_SPECULAR);
  458. BIND_CONSTANT(FIXED_MATERIAL_PARAM_EMISSION);
  459. BIND_CONSTANT(FIXED_MATERIAL_PARAM_SPECULAR_EXP);
  460. BIND_CONSTANT(FIXED_MATERIAL_PARAM_GLOW);
  461. BIND_CONSTANT(FIXED_MATERIAL_PARAM_NORMAL);
  462. BIND_CONSTANT(FIXED_MATERIAL_PARAM_SHADE_PARAM);
  463. BIND_CONSTANT(FIXED_MATERIAL_PARAM_MAX);
  464. BIND_CONSTANT(FIXED_MATERIAL_TEXCOORD_SPHERE);
  465. BIND_CONSTANT(FIXED_MATERIAL_TEXCOORD_UV);
  466. BIND_CONSTANT(FIXED_MATERIAL_TEXCOORD_UV_TRANSFORM);
  467. BIND_CONSTANT(FIXED_MATERIAL_TEXCOORD_UV2);
  468. BIND_CONSTANT(ARRAY_VERTEX);
  469. BIND_CONSTANT(ARRAY_NORMAL);
  470. BIND_CONSTANT(ARRAY_TANGENT);
  471. BIND_CONSTANT(ARRAY_COLOR);
  472. BIND_CONSTANT(ARRAY_TEX_UV);
  473. BIND_CONSTANT(ARRAY_BONES);
  474. BIND_CONSTANT(ARRAY_WEIGHTS);
  475. BIND_CONSTANT(ARRAY_INDEX);
  476. BIND_CONSTANT(ARRAY_MAX);
  477. BIND_CONSTANT(ARRAY_FORMAT_VERTEX);
  478. BIND_CONSTANT(ARRAY_FORMAT_NORMAL);
  479. BIND_CONSTANT(ARRAY_FORMAT_TANGENT);
  480. BIND_CONSTANT(ARRAY_FORMAT_COLOR);
  481. BIND_CONSTANT(ARRAY_FORMAT_TEX_UV);
  482. BIND_CONSTANT(ARRAY_FORMAT_BONES);
  483. BIND_CONSTANT(ARRAY_FORMAT_WEIGHTS);
  484. BIND_CONSTANT(ARRAY_FORMAT_INDEX);
  485. BIND_CONSTANT(PRIMITIVE_POINTS);
  486. BIND_CONSTANT(PRIMITIVE_LINES);
  487. BIND_CONSTANT(PRIMITIVE_LINE_STRIP);
  488. BIND_CONSTANT(PRIMITIVE_LINE_LOOP);
  489. BIND_CONSTANT(PRIMITIVE_TRIANGLES);
  490. BIND_CONSTANT(PRIMITIVE_TRIANGLE_STRIP);
  491. BIND_CONSTANT(PRIMITIVE_TRIANGLE_FAN);
  492. BIND_CONSTANT(PRIMITIVE_MAX);
  493. BIND_CONSTANT(PARTICLE_LIFETIME);
  494. BIND_CONSTANT(PARTICLE_SPREAD);
  495. BIND_CONSTANT(PARTICLE_GRAVITY);
  496. BIND_CONSTANT(PARTICLE_LINEAR_VELOCITY);
  497. BIND_CONSTANT(PARTICLE_ANGULAR_VELOCITY);
  498. BIND_CONSTANT(PARTICLE_LINEAR_ACCELERATION);
  499. BIND_CONSTANT(PARTICLE_RADIAL_ACCELERATION);
  500. BIND_CONSTANT(PARTICLE_TANGENTIAL_ACCELERATION);
  501. BIND_CONSTANT(PARTICLE_INITIAL_SIZE);
  502. BIND_CONSTANT(PARTICLE_FINAL_SIZE);
  503. BIND_CONSTANT(PARTICLE_INITIAL_ANGLE);
  504. BIND_CONSTANT(PARTICLE_HEIGHT);
  505. BIND_CONSTANT(PARTICLE_HEIGHT_SPEED_SCALE);
  506. BIND_CONSTANT(PARTICLE_VAR_MAX);
  507. BIND_CONSTANT(LIGHT_DIRECTIONAL);
  508. BIND_CONSTANT(LIGHT_OMNI);
  509. BIND_CONSTANT(LIGHT_SPOT);
  510. BIND_CONSTANT(LIGHT_COLOR_DIFFUSE);
  511. BIND_CONSTANT(LIGHT_COLOR_SPECULAR);
  512. BIND_CONSTANT(LIGHT_PARAM_SPOT_ATTENUATION);
  513. BIND_CONSTANT(LIGHT_PARAM_SPOT_ANGLE);
  514. BIND_CONSTANT(LIGHT_PARAM_RADIUS);
  515. BIND_CONSTANT(LIGHT_PARAM_ENERGY);
  516. BIND_CONSTANT(LIGHT_PARAM_ATTENUATION);
  517. BIND_CONSTANT(LIGHT_PARAM_MAX);
  518. BIND_CONSTANT(SCENARIO_DEBUG_DISABLED);
  519. BIND_CONSTANT(SCENARIO_DEBUG_WIREFRAME);
  520. BIND_CONSTANT(SCENARIO_DEBUG_OVERDRAW);
  521. BIND_CONSTANT(INSTANCE_MESH);
  522. BIND_CONSTANT(INSTANCE_MULTIMESH);
  523. BIND_CONSTANT(INSTANCE_PARTICLES);
  524. BIND_CONSTANT(INSTANCE_LIGHT);
  525. BIND_CONSTANT(INSTANCE_ROOM);
  526. BIND_CONSTANT(INSTANCE_PORTAL);
  527. BIND_CONSTANT(INSTANCE_GEOMETRY_MASK);
  528. BIND_CONSTANT(INFO_OBJECTS_IN_FRAME);
  529. BIND_CONSTANT(INFO_VERTICES_IN_FRAME);
  530. BIND_CONSTANT(INFO_MATERIAL_CHANGES_IN_FRAME);
  531. BIND_CONSTANT(INFO_SHADER_CHANGES_IN_FRAME);
  532. BIND_CONSTANT(INFO_SURFACE_CHANGES_IN_FRAME);
  533. BIND_CONSTANT(INFO_DRAW_CALLS_IN_FRAME);
  534. BIND_CONSTANT(INFO_USAGE_VIDEO_MEM_TOTAL);
  535. BIND_CONSTANT(INFO_VIDEO_MEM_USED);
  536. BIND_CONSTANT(INFO_TEXTURE_MEM_USED);
  537. BIND_CONSTANT(INFO_VERTEX_MEM_USED);
  538. }
  539. 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) {
  540. ERR_FAIL_COND(p_margins.size() != 4);
  541. 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);
  542. }
  543. void VisualServer::_camera_set_orthogonal(RID p_camera, float p_size, float p_z_near, float p_z_far) {
  544. camera_set_orthogonal(p_camera, p_size, p_z_near, p_z_far);
  545. }
  546. void VisualServer::_viewport_set_rect(RID p_viewport, const Rect2 &p_rect) {
  547. ViewportRect r;
  548. r.x = p_rect.pos.x;
  549. r.y = p_rect.pos.y;
  550. r.width = p_rect.size.x;
  551. r.height = p_rect.size.y;
  552. viewport_set_rect(p_viewport, r);
  553. }
  554. Rect2 VisualServer::_viewport_get_rect(RID p_viewport) const {
  555. ViewportRect r = viewport_get_rect(p_viewport);
  556. return Rect2(r.x, r.y, r.width, r.height);
  557. }
  558. void VisualServer::mesh_add_surface_from_mesh_data(RID p_mesh, const Geometry::MeshData &p_mesh_data) {
  559. #if 1
  560. DVector<Vector3> vertices;
  561. DVector<Vector3> normals;
  562. for (int i = 0; i < p_mesh_data.faces.size(); i++) {
  563. const Geometry::MeshData::Face &f = p_mesh_data.faces[i];
  564. for (int j = 2; j < f.indices.size(); j++) {
  565. #define _ADD_VERTEX(m_idx) \
  566. vertices.push_back(p_mesh_data.vertices[f.indices[m_idx]]); \
  567. normals.push_back(f.plane.normal);
  568. _ADD_VERTEX(0);
  569. _ADD_VERTEX(j - 1);
  570. _ADD_VERTEX(j);
  571. }
  572. }
  573. Array d;
  574. d.resize(VS::ARRAY_MAX);
  575. d[ARRAY_VERTEX] = vertices;
  576. d[ARRAY_NORMAL] = normals;
  577. mesh_add_surface(p_mesh, PRIMITIVE_TRIANGLES, d);
  578. #else
  579. DVector<Vector3> vertices;
  580. for (int i = 0; i < p_mesh_data.edges.size(); i++) {
  581. const Geometry::MeshData::Edge &f = p_mesh_data.edges[i];
  582. vertices.push_back(p_mesh_data.vertices[f.a]);
  583. vertices.push_back(p_mesh_data.vertices[f.b]);
  584. }
  585. Array d;
  586. d.resize(VS::ARRAY_MAX);
  587. d[ARRAY_VERTEX] = vertices;
  588. mesh_add_surface(p_mesh, PRIMITIVE_LINES, d);
  589. #endif
  590. }
  591. void VisualServer::mesh_add_surface_from_planes(RID p_mesh, const DVector<Plane> &p_planes) {
  592. Geometry::MeshData mdata = Geometry::build_convex_mesh(p_planes);
  593. mesh_add_surface_from_mesh_data(p_mesh, mdata);
  594. }
  595. RID VisualServer::instance_create2(RID p_base, RID p_scenario) {
  596. RID instance = instance_create();
  597. instance_set_base(instance, p_base);
  598. instance_set_scenario(instance, p_scenario);
  599. return instance;
  600. }
  601. VisualServer::VisualServer() {
  602. // ERR_FAIL_COND(singleton);
  603. singleton = this;
  604. mm_policy = GLOBAL_DEF("render/mipmap_policy", 0);
  605. if (mm_policy < 0 || mm_policy > 2)
  606. mm_policy = 0;
  607. }
  608. VisualServer::~VisualServer() {
  609. singleton = NULL;
  610. }