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

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