💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

335 lines
9.7 KiB

3 years ago
3 years ago
  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "tool-system.hpp"
  20. #include "ecs/components/collision-component.hpp"
  21. #include "ecs/components/tool-component.hpp"
  22. #include "ecs/components/transform-component.hpp"
  23. #include "event/event-dispatcher.hpp"
  24. #include "game/events/tool-events.hpp"
  25. #include "animation/orbit-cam.hpp"
  26. #include "animation/ease.hpp"
  27. #include "geom/mesh.hpp"
  28. #include "geom/intersection.hpp"
  29. #include "math/math.hpp"
  30. #include "ecs/commands.hpp"
  31. namespace ecs {
  32. tool_system::tool_system(ecs::registry& registry, ::event_dispatcher* event_dispatcher):
  33. entity_system(registry),
  34. event_dispatcher(event_dispatcher),
  35. camera(nullptr),
  36. orbit_cam(orbit_cam),
  37. viewport{0, 0, 0, 0},
  38. mouse_position{0, 0},
  39. pick_enabled(true),
  40. was_pick_enabled(pick_enabled),
  41. active_tool(entt::null)
  42. {
  43. hand_angle_spring.z = 1.0f;
  44. hand_angle_spring.w = hz_to_rads(8.0f);
  45. hand_angle_spring.x1 = math::pi<float>;
  46. hand_angle_spring.x0 = hand_angle_spring.x1;
  47. hand_angle_spring.v = 0.0f;
  48. pick_spring.z = 1.0f;
  49. pick_spring.w = hz_to_rads(30.0f);
  50. pick_spring.x1 = {0.0f, 0.0f, 0.0f};
  51. pick_spring.x0 = pick_spring.x1;
  52. pick_spring.v = {0.0f, 0.0f, 0.0f};
  53. // Create descend animation
  54. animation_channel<float>* channel = descend_animation.add_channel(0);
  55. descend_animation.set_interpolator(ease<float, double>::out_cubic);
  56. descend_animation.set_frame_callback
  57. (
  58. [this](int channel, const float& t)
  59. {
  60. this->active_tool_distance = t;
  61. }
  62. );
  63. // Create descend animation
  64. channel = ascend_animation.add_channel(0);
  65. ascend_animation.set_interpolator(ease<float, double>::out_cubic);
  66. ascend_animation.set_frame_callback
  67. (
  68. [this](int channel, const float& t)
  69. {
  70. this->active_tool_distance = t;
  71. }
  72. );
  73. active_tool = entt::null;
  74. active_tool_distance = 0.0f;
  75. warp = true;
  76. tool_active = false;
  77. event_dispatcher->subscribe<mouse_moved_event>(this);
  78. event_dispatcher->subscribe<window_resized_event>(this);
  79. }
  80. tool_system::~tool_system()
  81. {
  82. event_dispatcher->unsubscribe<mouse_moved_event>(this);
  83. event_dispatcher->unsubscribe<window_resized_event>(this);
  84. }
  85. void tool_system::update(double t, double dt)
  86. {
  87. if (active_tool == entt::null)
  88. return;
  89. // Advance animations
  90. ascend_animation.advance(dt);
  91. descend_animation.advance(dt);
  92. if (!camera)
  93. return;
  94. float3 pick_near = camera->unproject({mouse_position[0], viewport[3] - mouse_position[1], 0.0f}, viewport);
  95. float3 pick_far = camera->unproject({mouse_position[0], viewport[3] - mouse_position[1], 1.0f}, viewport);
  96. float3 pick_origin = pick_near;
  97. float3 pick_direction = math::normalize(pick_far - pick_near);
  98. geom::ray<float> picking_ray = {pick_near, pick_direction};
  99. float a = std::numeric_limits<float>::infinity();
  100. bool intersection = false;
  101. float3 pick;
  102. // Cast ray from cursor to collision components to find closest intersection
  103. registry.view<transform_component, collision_component>().each(
  104. [&](ecs::entity entity, auto& transform, auto& collision)
  105. {
  106. math::transform<float> inverse_transform = math::inverse(transform.local);
  107. float3 origin = inverse_transform * pick_origin;
  108. float3 direction = math::normalize(math::conjugate(transform.local.rotation) * pick_direction);
  109. geom::ray<float> transformed_ray = {origin, direction};
  110. // Broad phase AABB test
  111. auto aabb_result = geom::ray_aabb_intersection(transformed_ray, collision.bounds);
  112. if (!std::get<0>(aabb_result))
  113. {
  114. return;
  115. }
  116. // Narrow phase mesh test
  117. auto mesh_result = collision.mesh_accelerator.query_nearest(transformed_ray);
  118. if (mesh_result)
  119. {
  120. intersection = true;
  121. if (mesh_result->t < a)
  122. {
  123. a = mesh_result->t;
  124. pick = picking_ray.extrapolate(a);
  125. pick_spring.x1 = pick;
  126. }
  127. }
  128. });
  129. const float3& camera_position = camera->get_translation();
  130. float3 pick_planar_position = float3{pick.x, 0, pick.z};
  131. float3 camera_planar_position = float3{camera_position.x, 0, camera_position.z};
  132. float pick_angle = 0.0f;
  133. float3 pick_planar_direction = math::normalize(pick_planar_position - camera_planar_position);
  134. float3 camera_planar_focal_point = float3{orbit_cam->get_focal_point().x, 0, orbit_cam->get_focal_point().z};
  135. float3 camera_planar_direction = math::normalize(camera_planar_focal_point - camera_planar_position);
  136. if (std::fabs(math::length_squared(camera_planar_direction - pick_planar_direction) > 0.0001f))
  137. {
  138. pick_angle = std::acos(math::dot(camera_planar_direction, pick_planar_direction));
  139. if (math::dot(math::cross(camera_planar_direction, pick_planar_direction), float3{0, 1, 0}) < 0.0f)
  140. pick_angle = -pick_angle;
  141. }
  142. // Determine target hand angle
  143. hand_angle_spring.x1 = -std::min<float>(0.5f, std::max<float>(-0.5f, ((mouse_position[0] / viewport[2]) - 0.5f) * 1.0f)) * (math::pi<float>);
  144. // Solve springs
  145. solve_numeric_spring<float, float>(hand_angle_spring, dt);
  146. solve_numeric_spring<float3, float>(pick_spring, dt);
  147. // Don't use spring for picking
  148. pick_spring.x0 = pick_spring.x1;
  149. // Move active tools to intersection location
  150. registry.view<tool_component, transform_component>().each(
  151. [&](ecs::entity entity, auto& tool, auto& transform)
  152. {
  153. /*
  154. if (registry.has<model_component>(entity))
  155. {
  156. }*/
  157. if (!tool.active)
  158. return;
  159. active_tool = entity;
  160. float tool_distance = active_tool_distance;//(tool_active) ? tool.active_distance : tool.idle_distance;
  161. // Interpolate between left and right hand
  162. math::quaternion<float> hand_rotation = math::angle_axis(orbit_cam->get_azimuth() + hand_angle_spring.x0, float3{0, 1, 0});
  163. math::quaternion<float> tilt_rotation = math::angle_axis(orbit_cam->get_elevation(), float3{-1.0f, 0.0f, 0.0f});
  164. if (tool.heliotropic)
  165. {
  166. math::quaternion<float> solar_rotation = math::rotation(float3{0, -1, 0}, sun_direction);
  167. transform.local.translation = pick_spring.x0 + solar_rotation * float3{0, tool_distance, 0};
  168. transform.local.rotation = solar_rotation * hand_rotation;
  169. }
  170. else
  171. {
  172. math::quaternion<float> rotation = hand_rotation * tilt_rotation;
  173. transform.local.translation = pick_spring.x0 + rotation * float3{0, tool_distance, 0};
  174. transform.local.rotation = rotation;
  175. }
  176. if (warp)
  177. {
  178. transform.warp = true;
  179. command::assign_render_layers(registry, active_tool, 1);
  180. warp = false;
  181. }
  182. // Update tool's cursor position
  183. tool.cursor = pick_spring.x0;
  184. //math::quaternion<float> rotation = math::angle_axis(orbit_cam->get_azimuth() + pick_angle, float3{0, 1, 0});
  185. //transform.transform.rotation = rotation;
  186. });
  187. was_pick_enabled = pick_enabled;
  188. }
  189. void tool_system::set_camera(const scene::camera* camera)
  190. {
  191. this->camera = camera;
  192. }
  193. void tool_system::set_orbit_cam(const ::orbit_cam* orbit_cam)
  194. {
  195. this->orbit_cam = orbit_cam;
  196. }
  197. void tool_system::set_viewport(const float4& viewport)
  198. {
  199. this->viewport = viewport;
  200. mouse_position.x = viewport[2] * 0.5f;
  201. mouse_position.y = viewport[3] * 0.5f;
  202. }
  203. void tool_system::set_pick(bool enabled)
  204. {
  205. pick_enabled = enabled;
  206. }
  207. void tool_system::set_sun_direction(const float3& direction)
  208. {
  209. sun_direction = direction;
  210. }
  211. void tool_system::set_active_tool(ecs::entity entity)
  212. {
  213. if (active_tool == entity)
  214. return;
  215. const float descent_time = 0.1f;
  216. const float ascent_time = 0.1f;
  217. if (active_tool != entt::null)
  218. {
  219. auto& tool = registry.get<tool_component>(active_tool);
  220. tool.active = false;
  221. command::assign_render_layers(registry, active_tool, 0);
  222. }
  223. active_tool = entity;
  224. if (active_tool != entt::null)
  225. {
  226. auto& tool = registry.get<tool_component>(active_tool);
  227. tool.active = true;
  228. active_tool_distance = tool.idle_distance;
  229. command::warp_to(registry, active_tool, pick_spring.x0 + float3{0.0f, tool.idle_distance, 0.0f});
  230. // Adjust descend and ascend animations
  231. animation_channel<float>* channel = descend_animation.get_channel(0);
  232. channel->remove_keyframes();
  233. channel->insert_keyframe({0.0, tool.idle_distance});
  234. channel->insert_keyframe({descent_time, tool.active_distance});
  235. channel = ascend_animation.get_channel(0);
  236. channel->remove_keyframes();
  237. channel->insert_keyframe({0.0, tool.active_distance});
  238. channel->insert_keyframe({ascent_time, tool.idle_distance});
  239. }
  240. warp = true;
  241. }
  242. void tool_system::set_tool_active(bool active)
  243. {
  244. tool_active = active;
  245. if (active)
  246. {
  247. descend_animation.rewind();
  248. descend_animation.play();
  249. // Queue tool pressed event
  250. tool_pressed_event event;
  251. event.entity = active_tool;
  252. event.position = pick_spring.x0;
  253. event_dispatcher->queue(event);
  254. }
  255. else
  256. {
  257. ascend_animation.rewind();
  258. ascend_animation.play();
  259. // Queue tool pressed event
  260. tool_released_event event;
  261. event.entity = active_tool;
  262. event.position = pick_spring.x0;
  263. event_dispatcher->queue(event);
  264. }
  265. }
  266. void tool_system::handle_event(const mouse_moved_event& event)
  267. {
  268. if (pick_enabled && was_pick_enabled)
  269. {
  270. mouse_position[0] = event.x;
  271. mouse_position[1] = event.y;
  272. }
  273. }
  274. void tool_system::handle_event(const window_resized_event& event)
  275. {
  276. set_viewport({0.0f, 0.0f, static_cast<float>(event.w), static_cast<float>(event.h)});
  277. }
  278. } // namespace ecs