visual_server_canvas.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*************************************************************************/
  2. /* visual_server_canvas.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. #ifndef VISUALSERVERCANVAS_H
  31. #define VISUALSERVERCANVAS_H
  32. #include "rasterizer.h"
  33. #include "visual_server_viewport.h"
  34. class VisualServerCanvas {
  35. public:
  36. struct Item : public RasterizerCanvas::Item {
  37. RID parent; // canvas it belongs to
  38. List<Item *>::Element *E;
  39. int z_index;
  40. bool z_relative;
  41. bool sort_y;
  42. Color modulate;
  43. Color self_modulate;
  44. bool use_parent_material;
  45. int index;
  46. bool children_order_dirty;
  47. int ysort_children_count;
  48. Transform2D ysort_xform;
  49. Vector2 ysort_pos;
  50. Vector<Item *> child_items;
  51. Item() {
  52. children_order_dirty = true;
  53. E = NULL;
  54. z_index = 0;
  55. modulate = Color(1, 1, 1, 1);
  56. self_modulate = Color(1, 1, 1, 1);
  57. sort_y = false;
  58. use_parent_material = false;
  59. z_relative = true;
  60. index = 0;
  61. ysort_children_count = -1;
  62. ysort_xform = Transform2D();
  63. ysort_pos = Vector2();
  64. }
  65. };
  66. struct ItemIndexSort {
  67. _FORCE_INLINE_ bool operator()(const Item *p_left, const Item *p_right) const {
  68. return p_left->index < p_right->index;
  69. }
  70. };
  71. struct ItemPtrSort {
  72. _FORCE_INLINE_ bool operator()(const Item *p_left, const Item *p_right) const {
  73. if (Math::abs(p_left->ysort_pos.y - p_right->ysort_pos.y) < CMP_EPSILON)
  74. return p_left->ysort_pos.x < p_right->ysort_pos.x;
  75. else
  76. return p_left->ysort_pos.y < p_right->ysort_pos.y;
  77. }
  78. };
  79. struct LightOccluderPolygon : RID_Data {
  80. bool active;
  81. Rect2 aabb;
  82. VS::CanvasOccluderPolygonCullMode cull_mode;
  83. RID occluder;
  84. Set<RasterizerCanvas::LightOccluderInstance *> owners;
  85. LightOccluderPolygon() {
  86. active = false;
  87. cull_mode = VS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  88. }
  89. };
  90. RID_Owner<LightOccluderPolygon> canvas_light_occluder_polygon_owner;
  91. RID_Owner<RasterizerCanvas::LightOccluderInstance> canvas_light_occluder_owner;
  92. struct Canvas : public VisualServerViewport::CanvasBase {
  93. Set<RID> viewports;
  94. struct ChildItem {
  95. Point2 mirror;
  96. Item *item;
  97. bool operator<(const ChildItem &p_item) const {
  98. return item->index < p_item.item->index;
  99. }
  100. };
  101. Set<RasterizerCanvas::Light *> lights;
  102. Set<RasterizerCanvas::LightOccluderInstance *> occluders;
  103. bool children_order_dirty;
  104. Vector<ChildItem> child_items;
  105. Color modulate;
  106. int find_item(Item *p_item) {
  107. for (int i = 0; i < child_items.size(); i++) {
  108. if (child_items[i].item == p_item)
  109. return i;
  110. }
  111. return -1;
  112. }
  113. void erase_item(Item *p_item) {
  114. int idx = find_item(p_item);
  115. if (idx >= 0)
  116. child_items.remove(idx);
  117. }
  118. Canvas() {
  119. modulate = Color(1, 1, 1, 1);
  120. children_order_dirty = true;
  121. }
  122. };
  123. RID_Owner<Canvas> canvas_owner;
  124. RID_Owner<Item> canvas_item_owner;
  125. RID_Owner<RasterizerCanvas::Light> canvas_light_owner;
  126. private:
  127. void _render_canvas_item_tree(Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, RasterizerCanvas::Light *p_lights);
  128. void _render_canvas_item(Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, int p_z, RasterizerCanvas::Item **z_list, RasterizerCanvas::Item **z_last_list, Item *p_canvas_clip, Item *p_material_owner);
  129. void _light_mask_canvas_items(int p_z, RasterizerCanvas::Item *p_canvas_item, RasterizerCanvas::Light *p_masked_lights);
  130. public:
  131. void render_canvas(Canvas *p_canvas, const Transform2D &p_transform, RasterizerCanvas::Light *p_lights, RasterizerCanvas::Light *p_masked_lights, const Rect2 &p_clip_rect);
  132. RID canvas_create();
  133. void canvas_set_item_mirroring(RID p_canvas, RID p_item, const Point2 &p_mirroring);
  134. void canvas_set_modulate(RID p_canvas, const Color &p_color);
  135. RID canvas_item_create();
  136. void canvas_item_set_parent(RID p_item, RID p_parent);
  137. void canvas_item_set_visible(RID p_item, bool p_visible);
  138. void canvas_item_set_light_mask(RID p_item, int p_mask);
  139. void canvas_item_set_transform(RID p_item, const Transform2D &p_transform);
  140. void canvas_item_set_clip(RID p_item, bool p_clip);
  141. void canvas_item_set_distance_field_mode(RID p_item, bool p_enable);
  142. void canvas_item_set_custom_rect(RID p_item, bool p_custom_rect, const Rect2 &p_rect = Rect2());
  143. void canvas_item_set_modulate(RID p_item, const Color &p_color);
  144. void canvas_item_set_self_modulate(RID p_item, const Color &p_color);
  145. void canvas_item_set_draw_behind_parent(RID p_item, bool p_enable);
  146. void canvas_item_set_update_when_visible(RID p_item, bool p_update);
  147. void canvas_item_add_line(RID p_item, const Point2 &p_from, const Point2 &p_to, const Color &p_color, float p_width = 1.0, bool p_antialiased = false);
  148. void canvas_item_add_polyline(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width = 1.0, bool p_antialiased = false);
  149. void canvas_item_add_multiline(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width = 1.0, bool p_antialiased = false);
  150. void canvas_item_add_rect(RID p_item, const Rect2 &p_rect, const Color &p_color);
  151. void canvas_item_add_circle(RID p_item, const Point2 &p_pos, float p_radius, const Color &p_color);
  152. void canvas_item_add_texture_rect(RID p_item, const Rect2 &p_rect, RID p_texture, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, RID p_normal_map = RID());
  153. void canvas_item_add_texture_rect_region(RID p_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, RID p_normal_map = RID(), bool p_clip_uv = false);
  154. void canvas_item_add_nine_patch(RID p_item, const Rect2 &p_rect, const Rect2 &p_source, RID p_texture, const Vector2 &p_topleft, const Vector2 &p_bottomright, VS::NinePatchAxisMode p_x_axis_mode = VS::NINE_PATCH_STRETCH, VS::NinePatchAxisMode p_y_axis_mode = VS::NINE_PATCH_STRETCH, bool p_draw_center = true, const Color &p_modulate = Color(1, 1, 1), RID p_normal_map = RID());
  155. void canvas_item_add_primitive(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, RID p_texture, float p_width = 1.0, RID p_normal_map = RID());
  156. void canvas_item_add_polygon(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), RID p_texture = RID(), RID p_normal_map = RID(), bool p_antialiased = false);
  157. void canvas_item_add_triangle_array(RID p_item, const Vector<int> &p_indices, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), const Vector<int> &p_bones = Vector<int>(), const Vector<float> &p_weights = Vector<float>(), RID p_texture = RID(), int p_count = -1, RID p_normal_map = RID());
  158. void canvas_item_add_mesh(RID p_item, const RID &p_mesh, RID p_texture = RID(), RID p_normal_map = RID());
  159. void canvas_item_add_multimesh(RID p_item, RID p_mesh, RID p_texture = RID(), RID p_normal_map = RID());
  160. void canvas_item_add_particles(RID p_item, RID p_particles, RID p_texture, RID p_normal);
  161. void canvas_item_add_set_transform(RID p_item, const Transform2D &p_transform);
  162. void canvas_item_add_clip_ignore(RID p_item, bool p_ignore);
  163. void canvas_item_set_sort_children_by_y(RID p_item, bool p_enable);
  164. void canvas_item_set_z_index(RID p_item, int p_z);
  165. void canvas_item_set_z_as_relative_to_parent(RID p_item, bool p_enable);
  166. void canvas_item_set_copy_to_backbuffer(RID p_item, bool p_enable, const Rect2 &p_rect);
  167. void canvas_item_attach_skeleton(RID p_item, RID p_skeleton);
  168. void canvas_item_clear(RID p_item);
  169. void canvas_item_set_draw_index(RID p_item, int p_index);
  170. void canvas_item_set_material(RID p_item, RID p_material);
  171. void canvas_item_set_use_parent_material(RID p_item, bool p_enable);
  172. RID canvas_light_create();
  173. void canvas_light_attach_to_canvas(RID p_light, RID p_canvas);
  174. void canvas_light_set_enabled(RID p_light, bool p_enabled);
  175. void canvas_light_set_scale(RID p_light, float p_scale);
  176. void canvas_light_set_transform(RID p_light, const Transform2D &p_transform);
  177. void canvas_light_set_texture(RID p_light, RID p_texture);
  178. void canvas_light_set_texture_offset(RID p_light, const Vector2 &p_offset);
  179. void canvas_light_set_color(RID p_light, const Color &p_color);
  180. void canvas_light_set_height(RID p_light, float p_height);
  181. void canvas_light_set_energy(RID p_light, float p_energy);
  182. void canvas_light_set_z_range(RID p_light, int p_min_z, int p_max_z);
  183. void canvas_light_set_layer_range(RID p_light, int p_min_layer, int p_max_layer);
  184. void canvas_light_set_item_cull_mask(RID p_light, int p_mask);
  185. void canvas_light_set_item_shadow_cull_mask(RID p_light, int p_mask);
  186. void canvas_light_set_mode(RID p_light, VS::CanvasLightMode p_mode);
  187. void canvas_light_set_shadow_enabled(RID p_light, bool p_enabled);
  188. void canvas_light_set_shadow_buffer_size(RID p_light, int p_size);
  189. void canvas_light_set_shadow_gradient_length(RID p_light, float p_length);
  190. void canvas_light_set_shadow_filter(RID p_light, VS::CanvasLightShadowFilter p_filter);
  191. void canvas_light_set_shadow_color(RID p_light, const Color &p_color);
  192. void canvas_light_set_shadow_smooth(RID p_light, float p_smooth);
  193. RID canvas_light_occluder_create();
  194. void canvas_light_occluder_attach_to_canvas(RID p_occluder, RID p_canvas);
  195. void canvas_light_occluder_set_enabled(RID p_occluder, bool p_enabled);
  196. void canvas_light_occluder_set_polygon(RID p_occluder, RID p_polygon);
  197. void canvas_light_occluder_set_transform(RID p_occluder, const Transform2D &p_xform);
  198. void canvas_light_occluder_set_light_mask(RID p_occluder, int p_mask);
  199. RID canvas_occluder_polygon_create();
  200. void canvas_occluder_polygon_set_shape(RID p_occluder_polygon, const PoolVector<Vector2> &p_shape, bool p_closed);
  201. void canvas_occluder_polygon_set_shape_as_lines(RID p_occluder_polygon, const PoolVector<Vector2> &p_shape);
  202. void canvas_occluder_polygon_set_cull_mode(RID p_occluder_polygon, VS::CanvasOccluderPolygonCullMode p_mode);
  203. bool free(RID p_rid);
  204. VisualServerCanvas();
  205. };
  206. #endif // VISUALSERVERCANVAS_H