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

325 lines
9.4 KiB

3 years ago
3 years ago
  1. /*
  2. * Copyright (C) 2020 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. {
  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. // Advance animations
  88. ascend_animation.advance(dt);
  89. descend_animation.advance(dt);
  90. if (!camera)
  91. return;
  92. float3 pick_near = camera->unproject({mouse_position[0], viewport[3] - mouse_position[1], 0.0f}, viewport);
  93. float3 pick_far = camera->unproject({mouse_position[0], viewport[3] - mouse_position[1], 1.0f}, viewport);
  94. float3 pick_origin = pick_near;
  95. float3 pick_direction = math::normalize(pick_far - pick_near);
  96. ray<float> picking_ray = {pick_near, pick_direction};
  97. float a = std::numeric_limits<float>::infinity();
  98. bool intersection = false;
  99. float3 pick;
  100. // Cast ray from cursor to collision components to find closest intersection
  101. registry.view<transform_component, collision_component>().each(
  102. [&](auto entity, auto& transform, auto& collision)
  103. {
  104. math::transform<float> inverse_transform = math::inverse(transform.local);
  105. float3 origin = inverse_transform * pick_origin;
  106. float3 direction = math::normalize(math::conjugate(transform.local.rotation) * pick_direction);
  107. ray<float> transformed_ray = {origin, direction};
  108. // Broad phase AABB test
  109. auto aabb_result = ray_aabb_intersection(transformed_ray, collision.bounds);
  110. if (!std::get<0>(aabb_result))
  111. {
  112. return;
  113. }
  114. // Narrow phase mesh test
  115. auto mesh_result = collision.mesh_accelerator.query_nearest(transformed_ray);
  116. if (mesh_result)
  117. {
  118. intersection = true;
  119. if (mesh_result->t < a)
  120. {
  121. a = mesh_result->t;
  122. pick = picking_ray.extrapolate(a);
  123. pick_spring.x1 = pick;
  124. }
  125. }
  126. });
  127. const float3& camera_position = camera->get_translation();
  128. float3 pick_planar_position = float3{pick.x, 0, pick.z};
  129. float3 camera_planar_position = float3{camera_position.x, 0, camera_position.z};
  130. float pick_angle = 0.0f;
  131. float3 pick_planar_direction = math::normalize(pick_planar_position - camera_planar_position);
  132. float3 camera_planar_focal_point = float3{orbit_cam->get_focal_point().x, 0, orbit_cam->get_focal_point().z};
  133. float3 camera_planar_direction = math::normalize(camera_planar_focal_point - camera_planar_position);
  134. if (std::fabs(math::length_squared(camera_planar_direction - pick_planar_direction) > 0.0001f))
  135. {
  136. pick_angle = std::acos(math::dot(camera_planar_direction, pick_planar_direction));
  137. if (math::dot(math::cross(camera_planar_direction, pick_planar_direction), float3{0, 1, 0}) < 0.0f)
  138. pick_angle = -pick_angle;
  139. }
  140. // Determine target hand angle
  141. 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>);
  142. // Solve springs
  143. solve_numeric_spring<float, float>(hand_angle_spring, dt);
  144. solve_numeric_spring<float3, float>(pick_spring, dt);
  145. // Don't use spring for picking
  146. pick_spring.x0 = pick_spring.x1;
  147. // Move active tools to intersection location
  148. registry.view<tool_component, transform_component>().each(
  149. [&](auto entity, auto& tool, auto& transform)
  150. {
  151. /*
  152. if (registry.has<model_component>(entity))
  153. {
  154. }*/
  155. if (!tool.active)
  156. return;
  157. active_tool = entity;
  158. float tool_distance = active_tool_distance;//(tool_active) ? tool.active_distance : tool.idle_distance;
  159. // Interpolate between left and right hand
  160. math::quaternion<float> hand_rotation = math::angle_axis(orbit_cam->get_azimuth() + hand_angle_spring.x0, float3{0, 1, 0});
  161. math::quaternion<float> tilt_rotation = math::angle_axis(orbit_cam->get_elevation(), float3{-1.0f, 0.0f, 0.0f});
  162. if (tool.heliotropic)
  163. {
  164. math::quaternion<float> solar_rotation = math::rotation(float3{0, -1, 0}, sun_direction);
  165. transform.local.translation = pick_spring.x0 + solar_rotation * float3{0, tool_distance, 0};
  166. transform.local.rotation = solar_rotation * hand_rotation;
  167. }
  168. else
  169. {
  170. math::quaternion<float> rotation = hand_rotation * tilt_rotation;
  171. transform.local.translation = pick_spring.x0 + rotation * float3{0, tool_distance, 0};
  172. transform.local.rotation = rotation;
  173. }
  174. if (warp)
  175. {
  176. transform.warp = true;
  177. ec::assign_render_layers(registry, active_tool, 1);
  178. warp = false;
  179. }
  180. //math::quaternion<float> rotation = math::angle_axis(orbit_cam->get_azimuth() + pick_angle, float3{0, 1, 0});
  181. //transform.transform.rotation = rotation;
  182. });
  183. was_pick_enabled = pick_enabled;
  184. }
  185. void tool_system::set_camera(const ::camera* camera)
  186. {
  187. this->camera = camera;
  188. }
  189. void tool_system::set_orbit_cam(const ::orbit_cam* orbit_cam)
  190. {
  191. this->orbit_cam = orbit_cam;
  192. }
  193. void tool_system::set_viewport(const float4& viewport)
  194. {
  195. this->viewport = viewport;
  196. mouse_position.x = viewport[2] * 0.5f;
  197. mouse_position.y = viewport[3] * 0.5f;
  198. }
  199. void tool_system::set_pick(bool enabled)
  200. {
  201. pick_enabled = enabled;
  202. }
  203. void tool_system::set_sun_direction(const float3& direction)
  204. {
  205. sun_direction = direction;
  206. }
  207. void tool_system::set_active_tool(entt::entity entity)
  208. {
  209. if (active_tool == entity)
  210. return;
  211. const float descent_time = 0.1f;
  212. const float ascent_time = 0.1f;
  213. if (active_tool != entt::null)
  214. {
  215. auto& tool = registry.get<tool_component>(active_tool);
  216. tool.active = false;
  217. ec::assign_render_layers(registry, active_tool, 0);
  218. }
  219. active_tool = entity;
  220. if (active_tool != entt::null)
  221. {
  222. auto& tool = registry.get<tool_component>(active_tool);
  223. tool.active = true;
  224. active_tool_distance = tool.idle_distance;
  225. ec::warp_to(registry, active_tool, pick_spring.x0 + float3{0.0f, tool.idle_distance, 0.0f});
  226. // Adjust descend and ascend animations
  227. animation_channel<float>* channel = descend_animation.get_channel(0);
  228. channel->remove_keyframes();
  229. channel->insert_keyframe({0.0, tool.idle_distance});
  230. channel->insert_keyframe({descent_time, tool.active_distance});
  231. channel = ascend_animation.get_channel(0);
  232. channel->remove_keyframes();
  233. channel->insert_keyframe({0.0, tool.active_distance});
  234. channel->insert_keyframe({ascent_time, tool.idle_distance});
  235. }
  236. warp = true;
  237. }
  238. void tool_system::set_tool_active(bool active)
  239. {
  240. tool_active = active;
  241. if (active)
  242. {
  243. descend_animation.rewind();
  244. descend_animation.play();
  245. // Queue tool pressed event
  246. tool_pressed_event event;
  247. event.entity = active_tool;
  248. event_dispatcher->queue(event);
  249. }
  250. else
  251. {
  252. ascend_animation.rewind();
  253. ascend_animation.play();
  254. // Queue tool pressed event
  255. tool_released_event event;
  256. event.entity = active_tool;
  257. event_dispatcher->queue(event);
  258. }
  259. }
  260. void tool_system::handle_event(const mouse_moved_event& event)
  261. {
  262. if (pick_enabled && was_pick_enabled)
  263. {
  264. mouse_position[0] = event.x;
  265. mouse_position[1] = event.y;
  266. }
  267. }
  268. void tool_system::handle_event(const window_resized_event& event)
  269. {
  270. set_viewport({0.0f, 0.0f, static_cast<float>(event.w), static_cast<float>(event.h)});
  271. }