💿🐜 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.

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