view3d_utils.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # ##### BEGIN GPL LICENSE BLOCK #####
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software Foundation,
  15. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. #
  17. # ##### END GPL LICENSE BLOCK #####
  18. # <pep8-80 compliant>
  19. __all__ = (
  20. "region_2d_to_vector_3d",
  21. "region_2d_to_origin_3d",
  22. "region_2d_to_location_3d",
  23. "location_3d_to_region_2d",
  24. )
  25. def region_2d_to_vector_3d(region, rv3d, coord):
  26. """
  27. Return a direction vector from the viewport at the specific 2d region
  28. coordinate.
  29. :arg region: region of the 3D viewport, typically bpy.context.region.
  30. :type region: :class:`bpy.types.Region`
  31. :arg rv3d: 3D region data, typically bpy.context.space_data.region_3d.
  32. :type rv3d: :class:`bpy.types.RegionView3D`
  33. :arg coord: 2d coordinates relative to the region:
  34. (event.mouse_region_x, event.mouse_region_y) for example.
  35. :type coord: 2d vector
  36. :return: normalized 3d vector.
  37. :rtype: :class:`mathutils.Vector`
  38. """
  39. from mathutils import Vector
  40. viewinv = rv3d.view_matrix.inverted()
  41. if rv3d.is_perspective:
  42. persinv = rv3d.perspective_matrix.inverted()
  43. out = Vector(((2.0 * coord[0] / region.width) - 1.0,
  44. (2.0 * coord[1] / region.height) - 1.0,
  45. -0.5
  46. ))
  47. w = out.dot(persinv[3].xyz) + persinv[3][3]
  48. view_vector = ((persinv @ out) / w) - viewinv.translation
  49. else:
  50. view_vector = -viewinv.col[2].xyz
  51. view_vector.normalize()
  52. return view_vector
  53. def region_2d_to_origin_3d(region, rv3d, coord, clamp=None):
  54. """
  55. Return the 3d view origin from the region relative 2d coords.
  56. .. note::
  57. Orthographic views have a less obvious origin,
  58. the far clip is used to define the viewport near/far extents.
  59. Since far clip can be a very large value,
  60. the result may give with numeric precision issues.
  61. To avoid this problem, you can optionally clamp the far clip to a
  62. smaller value based on the data you're operating on.
  63. :arg region: region of the 3D viewport, typically bpy.context.region.
  64. :type region: :class:`bpy.types.Region`
  65. :arg rv3d: 3D region data, typically bpy.context.space_data.region_3d.
  66. :type rv3d: :class:`bpy.types.RegionView3D`
  67. :arg coord: 2d coordinates relative to the region;
  68. (event.mouse_region_x, event.mouse_region_y) for example.
  69. :type coord: 2d vector
  70. :arg clamp: Clamp the maximum far-clip value used.
  71. (negative value will move the offset away from the view_location)
  72. :type clamp: float or None
  73. :return: The origin of the viewpoint in 3d space.
  74. :rtype: :class:`mathutils.Vector`
  75. """
  76. viewinv = rv3d.view_matrix.inverted()
  77. if rv3d.is_perspective:
  78. origin_start = viewinv.translation.copy()
  79. else:
  80. persmat = rv3d.perspective_matrix.copy()
  81. dx = (2.0 * coord[0] / region.width) - 1.0
  82. dy = (2.0 * coord[1] / region.height) - 1.0
  83. persinv = persmat.inverted()
  84. origin_start = ((persinv.col[0].xyz * dx) +
  85. (persinv.col[1].xyz * dy) +
  86. persinv.translation)
  87. if clamp != 0.0:
  88. if rv3d.view_perspective != 'CAMERA':
  89. # this value is scaled to the far clip already
  90. origin_offset = persinv.col[2].xyz
  91. if clamp is not None:
  92. if clamp < 0.0:
  93. origin_offset.negate()
  94. clamp = -clamp
  95. if origin_offset.length > clamp:
  96. origin_offset.length = clamp
  97. origin_start -= origin_offset
  98. return origin_start
  99. def region_2d_to_location_3d(region, rv3d, coord, depth_location):
  100. """
  101. Return a 3d location from the region relative 2d coords, aligned with
  102. *depth_location*.
  103. :arg region: region of the 3D viewport, typically bpy.context.region.
  104. :type region: :class:`bpy.types.Region`
  105. :arg rv3d: 3D region data, typically bpy.context.space_data.region_3d.
  106. :type rv3d: :class:`bpy.types.RegionView3D`
  107. :arg coord: 2d coordinates relative to the region;
  108. (event.mouse_region_x, event.mouse_region_y) for example.
  109. :type coord: 2d vector
  110. :arg depth_location: the returned vectors depth is aligned with this since
  111. there is no defined depth with a 2d region input.
  112. :type depth_location: 3d vector
  113. :return: normalized 3d vector.
  114. :rtype: :class:`mathutils.Vector`
  115. """
  116. from mathutils import Vector
  117. coord_vec = region_2d_to_vector_3d(region, rv3d, coord)
  118. depth_location = Vector(depth_location)
  119. origin_start = region_2d_to_origin_3d(region, rv3d, coord)
  120. origin_end = origin_start + coord_vec
  121. if rv3d.is_perspective:
  122. from mathutils.geometry import intersect_line_plane
  123. viewinv = rv3d.view_matrix.inverted()
  124. view_vec = viewinv.col[2].copy()
  125. return intersect_line_plane(origin_start,
  126. origin_end,
  127. depth_location,
  128. view_vec, 1,
  129. )
  130. else:
  131. from mathutils.geometry import intersect_point_line
  132. return intersect_point_line(depth_location,
  133. origin_start,
  134. origin_end,
  135. )[0]
  136. def location_3d_to_region_2d(region, rv3d, coord, default=None):
  137. """
  138. Return the *region* relative 2d location of a 3d position.
  139. :arg region: region of the 3D viewport, typically bpy.context.region.
  140. :type region: :class:`bpy.types.Region`
  141. :arg rv3d: 3D region data, typically bpy.context.space_data.region_3d.
  142. :type rv3d: :class:`bpy.types.RegionView3D`
  143. :arg coord: 3d worldspace location.
  144. :type coord: 3d vector
  145. :arg default: Return this value if ``coord``
  146. is behind the origin of a perspective view.
  147. :return: 2d location
  148. :rtype: :class:`mathutils.Vector` or ``default`` argument.
  149. """
  150. from mathutils import Vector
  151. prj = rv3d.perspective_matrix @ Vector((coord[0], coord[1], coord[2], 1.0))
  152. if prj.w > 0.0:
  153. width_half = region.width / 2.0
  154. height_half = region.height / 2.0
  155. return Vector((width_half + width_half * (prj.x / prj.w),
  156. height_half + height_half * (prj.y / prj.w),
  157. ))
  158. else:
  159. return default