ray.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import type { DeepImmutable, Nullable, float } from "../types";
  2. import { Matrix, Vector3 } from "../Maths/math.vector";
  3. import type { AbstractMesh } from "../Meshes/abstractMesh";
  4. import { PickingInfo } from "../Collisions/pickingInfo";
  5. import { IntersectionInfo } from "../Collisions/intersectionInfo";
  6. import type { BoundingBox } from "./boundingBox";
  7. import type { BoundingSphere } from "./boundingSphere";
  8. import type { Plane } from "../Maths/math.plane";
  9. /**
  10. * Class representing a ray with position and direction
  11. */
  12. export declare class Ray {
  13. /** origin point */
  14. origin: Vector3;
  15. /** direction */
  16. direction: Vector3;
  17. /** length of the ray */
  18. length: number;
  19. /** The epsilon value to use when calculating the ray/triangle intersection (default: Epsilon from math constants) */
  20. epsilon: number;
  21. private static readonly _TmpVector3;
  22. private static _RayDistant;
  23. private _tmpRay;
  24. /**
  25. * Creates a new ray
  26. * @param origin origin point
  27. * @param direction direction
  28. * @param length length of the ray
  29. * @param epsilon The epsilon value to use when calculating the ray/triangle intersection (default: 0)
  30. */
  31. constructor(
  32. /** origin point */
  33. origin: Vector3,
  34. /** direction */
  35. direction: Vector3,
  36. /** length of the ray */
  37. length?: number,
  38. /** The epsilon value to use when calculating the ray/triangle intersection (default: Epsilon from math constants) */
  39. epsilon?: number);
  40. /**
  41. * Clone the current ray
  42. * @returns a new ray
  43. */
  44. clone(): Ray;
  45. /**
  46. * Checks if the ray intersects a box
  47. * This does not account for the ray length by design to improve perfs.
  48. * @param minimum bound of the box
  49. * @param maximum bound of the box
  50. * @param intersectionTreshold extra extend to be added to the box in all direction
  51. * @returns if the box was hit
  52. */
  53. intersectsBoxMinMax(minimum: DeepImmutable<Vector3>, maximum: DeepImmutable<Vector3>, intersectionTreshold?: number): boolean;
  54. /**
  55. * Checks if the ray intersects a box
  56. * This does not account for the ray lenght by design to improve perfs.
  57. * @param box the bounding box to check
  58. * @param intersectionTreshold extra extend to be added to the BoundingBox in all direction
  59. * @returns if the box was hit
  60. */
  61. intersectsBox(box: DeepImmutable<BoundingBox>, intersectionTreshold?: number): boolean;
  62. /**
  63. * If the ray hits a sphere
  64. * @param sphere the bounding sphere to check
  65. * @param intersectionTreshold extra extend to be added to the BoundingSphere in all direction
  66. * @returns true if it hits the sphere
  67. */
  68. intersectsSphere(sphere: DeepImmutable<BoundingSphere>, intersectionTreshold?: number): boolean;
  69. /**
  70. * If the ray hits a triange
  71. * @param vertex0 triangle vertex
  72. * @param vertex1 triangle vertex
  73. * @param vertex2 triangle vertex
  74. * @returns intersection information if hit
  75. */
  76. intersectsTriangle(vertex0: DeepImmutable<Vector3>, vertex1: DeepImmutable<Vector3>, vertex2: DeepImmutable<Vector3>): Nullable<IntersectionInfo>;
  77. /**
  78. * Checks if ray intersects a plane
  79. * @param plane the plane to check
  80. * @returns the distance away it was hit
  81. */
  82. intersectsPlane(plane: DeepImmutable<Plane>): Nullable<number>;
  83. /**
  84. * Calculate the intercept of a ray on a given axis
  85. * @param axis to check 'x' | 'y' | 'z'
  86. * @param offset from axis interception (i.e. an offset of 1y is intercepted above ground)
  87. * @returns a vector containing the coordinates where 'axis' is equal to zero (else offset), or null if there is no intercept.
  88. */
  89. intersectsAxis(axis: string, offset?: number): Nullable<Vector3>;
  90. /**
  91. * Checks if ray intersects a mesh. The ray is defined in WORLD space. A mesh triangle can be picked both from its front and back sides,
  92. * irrespective of orientation.
  93. * @param mesh the mesh to check
  94. * @param fastCheck defines if the first intersection will be used (and not the closest)
  95. * @param trianglePredicate defines an optional predicate used to select faces when a mesh intersection is detected
  96. * @param onlyBoundingInfo defines a boolean indicating if picking should only happen using bounding info (false by default)
  97. * @param worldToUse defines the world matrix to use to get the world coordinate of the intersection point
  98. * @param skipBoundingInfo a boolean indicating if we should skip the bounding info check
  99. * @returns picking info of the intersection
  100. */
  101. intersectsMesh(mesh: DeepImmutable<AbstractMesh>, fastCheck?: boolean, trianglePredicate?: TrianglePickingPredicate, onlyBoundingInfo?: boolean, worldToUse?: Matrix, skipBoundingInfo?: boolean): PickingInfo;
  102. /**
  103. * Checks if ray intersects a mesh
  104. * @param meshes the meshes to check
  105. * @param fastCheck defines if the first intersection will be used (and not the closest)
  106. * @param results array to store result in
  107. * @returns Array of picking infos
  108. */
  109. intersectsMeshes(meshes: Array<DeepImmutable<AbstractMesh>>, fastCheck?: boolean, results?: Array<PickingInfo>): Array<PickingInfo>;
  110. private _comparePickingInfo;
  111. private static _Smallnum;
  112. private static _Rayl;
  113. /**
  114. * Intersection test between the ray and a given segment within a given tolerance (threshold)
  115. * @param sega the first point of the segment to test the intersection against
  116. * @param segb the second point of the segment to test the intersection against
  117. * @param threshold the tolerance margin, if the ray doesn't intersect the segment but is close to the given threshold, the intersection is successful
  118. * @returns the distance from the ray origin to the intersection point if there's intersection, or -1 if there's no intersection
  119. */
  120. intersectionSegment(sega: DeepImmutable<Vector3>, segb: DeepImmutable<Vector3>, threshold: number): number;
  121. /**
  122. * Update the ray from viewport position
  123. * @param x position
  124. * @param y y position
  125. * @param viewportWidth viewport width
  126. * @param viewportHeight viewport height
  127. * @param world world matrix
  128. * @param view view matrix
  129. * @param projection projection matrix
  130. * @param enableDistantPicking defines if picking should handle large values for mesh position/scaling (false by default)
  131. * @returns this ray updated
  132. */
  133. update(x: number, y: number, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>, enableDistantPicking?: boolean): Ray;
  134. /**
  135. * Creates a ray with origin and direction of 0,0,0
  136. * @returns the new ray
  137. */
  138. static Zero(): Ray;
  139. /**
  140. * Creates a new ray from screen space and viewport
  141. * @param x position
  142. * @param y y position
  143. * @param viewportWidth viewport width
  144. * @param viewportHeight viewport height
  145. * @param world world matrix
  146. * @param view view matrix
  147. * @param projection projection matrix
  148. * @returns new ray
  149. */
  150. static CreateNew(x: number, y: number, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>): Ray;
  151. /**
  152. * Function will create a new transformed ray starting from origin and ending at the end point. Ray's length will be set, and ray will be
  153. * transformed to the given world matrix.
  154. * @param origin The origin point
  155. * @param end The end point
  156. * @param world a matrix to transform the ray to. Default is the identity matrix.
  157. * @returns the new ray
  158. */
  159. static CreateNewFromTo(origin: Vector3, end: Vector3, world?: DeepImmutable<Matrix>): Ray;
  160. /**
  161. * Function will update a transformed ray starting from origin and ending at the end point. Ray's length will be set, and ray will be
  162. * transformed to the given world matrix.
  163. * @param origin The origin point
  164. * @param end The end point
  165. * @param result the object to store the result
  166. * @param world a matrix to transform the ray to. Default is the identity matrix.
  167. * @returns the ref ray
  168. */
  169. static CreateFromToToRef(origin: Vector3, end: Vector3, result: Ray, world?: DeepImmutable<Matrix>): Ray;
  170. /**
  171. * Transforms a ray by a matrix
  172. * @param ray ray to transform
  173. * @param matrix matrix to apply
  174. * @returns the resulting new ray
  175. */
  176. static Transform(ray: DeepImmutable<Ray>, matrix: DeepImmutable<Matrix>): Ray;
  177. /**
  178. * Transforms a ray by a matrix
  179. * @param ray ray to transform
  180. * @param matrix matrix to apply
  181. * @param result ray to store result in
  182. * @returns the updated result ray
  183. */
  184. static TransformToRef(ray: DeepImmutable<Ray>, matrix: DeepImmutable<Matrix>, result: Ray): Ray;
  185. /**
  186. * Unproject a ray from screen space to object space
  187. * @param sourceX defines the screen space x coordinate to use
  188. * @param sourceY defines the screen space y coordinate to use
  189. * @param viewportWidth defines the current width of the viewport
  190. * @param viewportHeight defines the current height of the viewport
  191. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  192. * @param view defines the view matrix to use
  193. * @param projection defines the projection matrix to use
  194. */
  195. unprojectRayToRef(sourceX: float, sourceY: float, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>): void;
  196. }
  197. /**
  198. * Type used to define predicate used to select faces when a mesh intersection is detected
  199. */
  200. export type TrianglePickingPredicate = (p0: Vector3, p1: Vector3, p2: Vector3, ray: Ray, i0: number, i1: number, i2: number) => boolean;
  201. declare module "../scene" {
  202. interface Scene {
  203. /** @internal */
  204. _tempPickingRay: Nullable<Ray>;
  205. /** @internal */
  206. _cachedRayForTransform: Ray;
  207. /** @internal */
  208. _pickWithRayInverseMatrix: Matrix;
  209. /** @internal */
  210. _internalPick(rayFunction: (world: Matrix, enableDistantPicking: boolean) => Ray, predicate?: (mesh: AbstractMesh) => boolean, fastCheck?: boolean, onlyBoundingInfo?: boolean, trianglePredicate?: TrianglePickingPredicate): PickingInfo;
  211. /** @internal */
  212. _internalMultiPick(rayFunction: (world: Matrix, enableDistantPicking: boolean) => Ray, predicate?: (mesh: AbstractMesh) => boolean, trianglePredicate?: TrianglePickingPredicate): Nullable<PickingInfo[]>;
  213. /** @internal */
  214. _internalPickForMesh(pickingInfo: Nullable<PickingInfo>, rayFunction: (world: Matrix, enableDistantPicking: boolean) => Ray, mesh: AbstractMesh, world: Matrix, fastCheck?: boolean, onlyBoundingInfo?: boolean, trianglePredicate?: TrianglePickingPredicate, skipBoundingInfo?: boolean): Nullable<PickingInfo>;
  215. }
  216. }