static_raycaster.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**************************************************************************/
  2. /* static_raycaster.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/object/ref_counted.h"
  32. class StaticRaycaster : public RefCounted {
  33. GDCLASS(StaticRaycaster, RefCounted)
  34. protected:
  35. static StaticRaycaster *(*create_function)();
  36. public:
  37. // Compatible with embree4 rays.
  38. struct alignas(16) Ray {
  39. const static unsigned int INVALID_GEOMETRY_ID = ((unsigned int)-1); // from rtcore_common.h
  40. /*! Default construction does nothing. */
  41. _FORCE_INLINE_ Ray() :
  42. geomID(INVALID_GEOMETRY_ID) {}
  43. /*! Constructs a ray from origin, direction, and ray segment. Near
  44. * has to be smaller than far. */
  45. _FORCE_INLINE_ Ray(const Vector3 &p_org,
  46. const Vector3 &p_dir,
  47. float p_tnear = 0.0f,
  48. float p_tfar = Math::INF) :
  49. org(p_org),
  50. tnear(p_tnear),
  51. dir(p_dir),
  52. time(0.0f),
  53. tfar(p_tfar),
  54. mask(-1),
  55. u(0.0),
  56. v(0.0),
  57. primID(INVALID_GEOMETRY_ID),
  58. geomID(INVALID_GEOMETRY_ID),
  59. instID(INVALID_GEOMETRY_ID) {}
  60. /*! Tests if we hit something. */
  61. _FORCE_INLINE_ explicit operator bool() const { return geomID != INVALID_GEOMETRY_ID; }
  62. public:
  63. Vector3 org; //!< Ray origin + tnear
  64. float tnear; //!< Start of ray segment
  65. Vector3 dir; //!< Ray direction + tfar
  66. float time; //!< Time of this ray for motion blur.
  67. float tfar; //!< End of ray segment
  68. unsigned int mask; //!< used to mask out objects during traversal
  69. unsigned int id; //!< ray ID
  70. unsigned int flags; //!< ray flags
  71. Vector3 normal; //!< Not normalized geometry normal
  72. float u; //!< Barycentric u coordinate of hit
  73. float v; //!< Barycentric v coordinate of hit
  74. unsigned int primID; //!< primitive ID
  75. unsigned int geomID; //!< geometry ID
  76. unsigned int instID; //!< instance ID
  77. };
  78. virtual bool intersect(Ray &p_ray) = 0;
  79. virtual void intersect(Vector<Ray> &r_rays) = 0;
  80. virtual void add_mesh(const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices, unsigned int p_id) = 0;
  81. virtual void commit() = 0;
  82. virtual void set_mesh_filter(const HashSet<int> &p_mesh_ids) = 0;
  83. virtual void clear_mesh_filter() = 0;
  84. static Ref<StaticRaycaster> create();
  85. };