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

223 lines
6.7 KiB

  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 "scene/camera.hpp"
  24. #include "animation/orbit-cam.hpp"
  25. #include "geometry/mesh.hpp"
  26. #include "geometry/intersection.hpp"
  27. #include "math/math.hpp"
  28. #include "game/entity-commands.hpp"
  29. using namespace ecs;
  30. tool_system::tool_system(entt::registry& registry):
  31. entity_system(registry),
  32. camera(nullptr),
  33. orbit_cam(orbit_cam),
  34. viewport{0, 0, 0, 0},
  35. mouse_position{0, 0},
  36. pick_enabled(true),
  37. was_pick_enabled(pick_enabled)
  38. {
  39. hand_angle_spring.z = 1.0f;
  40. hand_angle_spring.w = hz_to_rads(8.0f);
  41. hand_angle_spring.x1 = math::pi<float>;
  42. hand_angle_spring.x0 = hand_angle_spring.x1;
  43. hand_angle_spring.v = 0.0f;
  44. pick_spring.z = 1.0f;
  45. pick_spring.w = hz_to_rads(30.0f);
  46. pick_spring.x1 = {0.0f, 0.0f, 0.0f};
  47. pick_spring.x0 = pick_spring.x1;
  48. pick_spring.v = {0.0f, 0.0f, 0.0f};
  49. active_tool = entt::null;
  50. }
  51. void tool_system::update(double t, double dt)
  52. {
  53. if (!camera)
  54. return;
  55. float3 pick_near = camera->unproject({mouse_position[0], viewport[3] - mouse_position[1], 0.0f}, viewport);
  56. float3 pick_far = camera->unproject({mouse_position[0], viewport[3] - mouse_position[1], 1.0f}, viewport);
  57. float3 pick_origin = pick_near;
  58. float3 pick_direction = math::normalize(pick_far - pick_near);
  59. ray<float> picking_ray = {pick_near, pick_direction};
  60. float a = std::numeric_limits<float>::infinity();
  61. bool intersection = false;
  62. float3 pick;
  63. // Cast ray from cursor to collision components to find closest intersection
  64. registry.view<transform_component, collision_component>().each(
  65. [&](auto entity, auto& transform, auto& collision)
  66. {
  67. math::transform<float> inverse_transform = math::inverse(transform.transform);
  68. float3 origin = inverse_transform * pick_origin;
  69. float3 direction = math::normalize(math::conjugate(transform.transform.rotation) * pick_direction);
  70. ray<float> transformed_ray = {origin, direction};
  71. // Broad phase AABB test
  72. auto aabb_result = ray_aabb_intersection(transformed_ray, collision.bounds);
  73. if (!std::get<0>(aabb_result))
  74. {
  75. return;
  76. }
  77. // Narrow phase mesh test
  78. auto mesh_result = collision.mesh_accelerator.query_nearest(transformed_ray);
  79. if (mesh_result)
  80. {
  81. intersection = true;
  82. if (mesh_result->t < a)
  83. {
  84. a = mesh_result->t;
  85. pick = picking_ray.extrapolate(a);
  86. pick_spring.x1 = pick;
  87. }
  88. }
  89. });
  90. const float3& camera_position = camera->get_translation();
  91. float3 pick_planar_position = float3{pick.x, 0, pick.z};
  92. float3 camera_planar_position = float3{camera_position.x, 0, camera_position.z};
  93. float pick_angle = 0.0f;
  94. float3 pick_planar_direction = math::normalize(pick_planar_position - camera_planar_position);
  95. float3 camera_planar_focal_point = float3{orbit_cam->get_focal_point().x, 0, orbit_cam->get_focal_point().z};
  96. float3 camera_planar_direction = math::normalize(camera_planar_focal_point - camera_planar_position);
  97. if (std::fabs(math::length_squared(camera_planar_direction - pick_planar_direction) > 0.0001f))
  98. {
  99. pick_angle = std::acos(math::dot(camera_planar_direction, pick_planar_direction));
  100. if (math::dot(math::cross(camera_planar_direction, pick_planar_direction), float3{0, 1, 0}) < 0.0f)
  101. pick_angle = -pick_angle;
  102. }
  103. // Determine target hand angle
  104. hand_angle_spring.x1 = math::pi<float> - std::min<float>(0.5f, std::max<float>(-0.5f, ((mouse_position[0] / viewport[2]) - 0.5f) * 1.0f)) * (math::pi<float> + math::half_pi<float>);
  105. // Solve springs
  106. solve_numeric_spring<float, float>(hand_angle_spring, dt);
  107. solve_numeric_spring<float3, float>(pick_spring, dt);
  108. // Move active tools to intersection location
  109. registry.view<tool_component, transform_component>().each(
  110. [&](auto entity, auto& tool, auto& transform)
  111. {
  112. /*
  113. if (registry.has<model_component>(entity))
  114. {
  115. }*/
  116. if (!tool.active)
  117. return;
  118. active_tool = entity;
  119. if (intersection)
  120. {
  121. transform.transform.translation = pick_spring.x0 + float3{0, tool.hover_distance, 0};
  122. }
  123. // Interpolate between left and right hand
  124. math::quaternion<float> hand_rotation = math::angle_axis(orbit_cam->get_azimuth() + hand_angle_spring.x0, float3{0, 1, 0});
  125. if (tool.heliotropic)
  126. {
  127. math::quaternion<float> solar_rotation = math::rotation(float3{0, -1, 0}, sun_direction);
  128. transform.transform.translation = pick_spring.x0 + solar_rotation * float3{0, tool.hover_distance, 0};
  129. transform.transform.rotation = solar_rotation * hand_rotation;
  130. }
  131. else
  132. {
  133. transform.transform.rotation = hand_rotation;
  134. }
  135. //math::quaternion<float> rotation = math::angle_axis(orbit_cam->get_azimuth() + pick_angle, float3{0, 1, 0});
  136. //transform.transform.rotation = rotation;
  137. });
  138. was_pick_enabled = pick_enabled;
  139. }
  140. void tool_system::set_camera(const ::camera* camera)
  141. {
  142. this->camera = camera;
  143. }
  144. void tool_system::set_orbit_cam(const ::orbit_cam* orbit_cam)
  145. {
  146. this->orbit_cam = orbit_cam;
  147. }
  148. void tool_system::set_viewport(const float4& viewport)
  149. {
  150. this->viewport = viewport;
  151. }
  152. void tool_system::set_pick(bool enabled)
  153. {
  154. pick_enabled = enabled;
  155. }
  156. void tool_system::set_sun_direction(const float3& direction)
  157. {
  158. sun_direction = direction;
  159. }
  160. void tool_system::set_active_tool(entt::entity entity)
  161. {
  162. if (active_tool != entt::null)
  163. {
  164. auto& tool = registry.get<tool_component>(active_tool);
  165. tool.active = false;
  166. ec::assign_render_layers(registry, active_tool, 0);
  167. }
  168. active_tool = entity;
  169. if (active_tool != entt::null)
  170. {
  171. auto& tool = registry.get<tool_component>(active_tool);
  172. tool.active = true;
  173. ec::assign_render_layers(registry, active_tool, 1);
  174. }
  175. }
  176. void tool_system::handle_event(const mouse_moved_event& event)
  177. {
  178. if (pick_enabled && was_pick_enabled)
  179. {
  180. mouse_position[0] = event.x;
  181. mouse_position[1] = event.y;
  182. }
  183. }
  184. void tool_system::handle_event(const window_resized_event& event)
  185. {
  186. set_viewport({0.0f, 0.0f, static_cast<float>(event.w), static_cast<float>(event.h)});
  187. }