gizmo.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import type { Observer } from "../Misc/observable";
  2. import type { Nullable } from "../types";
  3. import type { Scene, IDisposable } from "../scene";
  4. import { Quaternion, Matrix } from "../Maths/math.vector";
  5. import type { AbstractMesh } from "../Meshes/abstractMesh";
  6. import { Mesh } from "../Meshes/mesh";
  7. import type { Node } from "../node";
  8. import { UtilityLayerRenderer } from "../Rendering/utilityLayerRenderer";
  9. import type { TransformNode } from "../Meshes/transformNode";
  10. import type { StandardMaterial } from "../Materials/standardMaterial";
  11. import type { PointerInfo } from "../Events/pointerEvents";
  12. import type { PointerDragBehavior } from "../Behaviors/Meshes/pointerDragBehavior";
  13. /**
  14. * Cache built by each axis. Used for managing state between all elements of gizmo for enhanced UI
  15. */
  16. export interface GizmoAxisCache {
  17. /** Mesh used to render the Gizmo */
  18. gizmoMeshes: Mesh[];
  19. /** Mesh used to detect user interaction with Gizmo */
  20. colliderMeshes: Mesh[];
  21. /** Material used to indicate color of gizmo mesh */
  22. material: StandardMaterial;
  23. /** Material used to indicate hover state of the Gizmo */
  24. hoverMaterial: StandardMaterial;
  25. /** Material used to indicate disabled state of the Gizmo */
  26. disableMaterial: StandardMaterial;
  27. /** Used to indicate Active state of the Gizmo */
  28. active: boolean;
  29. /** DragBehavior */
  30. dragBehavior: PointerDragBehavior;
  31. }
  32. /**
  33. * Anchor options where the Gizmo can be positioned in relation to its anchored node
  34. */
  35. export declare enum GizmoAnchorPoint {
  36. /** The origin of the attached node */
  37. Origin = 0,
  38. /** The pivot point of the attached node*/
  39. Pivot = 1
  40. }
  41. /**
  42. * Coordinates mode: Local or World. Defines how axis is aligned: either on world axis or transform local axis
  43. */
  44. export declare enum GizmoCoordinatesMode {
  45. World = 0,
  46. Local = 1
  47. }
  48. /**
  49. * Interface for basic gizmo
  50. */
  51. export interface IGizmo extends IDisposable {
  52. /** True when the mouse pointer is hovered a gizmo mesh */
  53. readonly isHovered: boolean;
  54. /** The root mesh of the gizmo */
  55. _rootMesh: Mesh;
  56. /** Ratio for the scale of the gizmo */
  57. scaleRatio: number;
  58. /**
  59. * Mesh that the gizmo will be attached to. (eg. on a drag gizmo the mesh that will be dragged)
  60. * * When set, interactions will be enabled
  61. */
  62. attachedMesh: Nullable<AbstractMesh>;
  63. /**
  64. * Node that the gizmo will be attached to. (eg. on a drag gizmo the mesh, bone or NodeTransform that will be dragged)
  65. * * When set, interactions will be enabled
  66. */
  67. attachedNode: Nullable<Node>;
  68. /**
  69. * If set the gizmo's rotation will be updated to match the attached mesh each frame (Default: true)
  70. */
  71. updateGizmoRotationToMatchAttachedMesh: boolean;
  72. /** The utility layer the gizmo will be added to */
  73. gizmoLayer: UtilityLayerRenderer;
  74. /**
  75. * If set the gizmo's position will be updated to match the attached mesh each frame (Default: true)
  76. */
  77. updateGizmoPositionToMatchAttachedMesh: boolean;
  78. /**
  79. * Defines where the gizmo will be positioned if `updateGizmoPositionToMatchAttachedMesh` is enabled.
  80. * (Default: GizmoAnchorPoint.Origin)
  81. */
  82. anchorPoint: GizmoAnchorPoint;
  83. /**
  84. * Set the coordinate mode to use. By default it's local.
  85. */
  86. coordinatesMode: GizmoCoordinatesMode;
  87. /**
  88. * When set, the gizmo will always appear the same size no matter where the camera is (default: true)
  89. */
  90. updateScale: boolean;
  91. /**
  92. * posture that the gizmo will be display
  93. * When set null, default value will be used (Quaternion(0, 0, 0, 1))
  94. */
  95. customRotationQuaternion: Nullable<Quaternion>;
  96. /**
  97. * Disposes and replaces the current meshes in the gizmo with the specified mesh
  98. * @param mesh The mesh to replace the default mesh of the gizmo
  99. */
  100. setCustomMesh(mesh: Mesh): void;
  101. /**
  102. * Additional transform applied to the gizmo.
  103. * It's useful when the gizmo is attached to a bone: if the bone is part of a skeleton attached to a mesh, you should define the mesh as additionalTransformNode if you want the gizmo to be displayed at the bone's correct location.
  104. * Otherwise, as the gizmo is relative to the skeleton root, the mesh transformation will not be taken into account.
  105. */
  106. additionalTransformNode?: TransformNode | undefined;
  107. }
  108. /**
  109. * Renders gizmos on top of an existing scene which provide controls for position, rotation, etc.
  110. */
  111. export declare class Gizmo implements IGizmo {
  112. /** The utility layer the gizmo will be added to */
  113. gizmoLayer: UtilityLayerRenderer;
  114. /**
  115. * The root mesh of the gizmo
  116. */
  117. _rootMesh: Mesh;
  118. protected _attachedMesh: Nullable<AbstractMesh>;
  119. protected _attachedNode: Nullable<Node>;
  120. protected _customRotationQuaternion: Nullable<Quaternion>;
  121. protected _additionalTransformNode?: TransformNode;
  122. /**
  123. * Ratio for the scale of the gizmo (Default: 1)
  124. */
  125. protected _scaleRatio: number;
  126. /**
  127. * boolean updated by pointermove when a gizmo mesh is hovered
  128. */
  129. protected _isHovered: boolean;
  130. /**
  131. * When enabled, any gizmo operation will perserve scaling sign. Default is off.
  132. * Only valid for TransformNode derived classes (Mesh, AbstractMesh, ...)
  133. */
  134. static PreserveScaling: boolean;
  135. /**
  136. * There are 2 ways to preserve scaling: using mesh scaling or absolute scaling. Depending of hierarchy, non uniform scaling and LH or RH coordinates. One is preferable than the other.
  137. * If the scaling to be preserved is the local scaling, then set this value to false.
  138. * Default is true which means scaling to be preserved is absolute one (with hierarchy applied)
  139. */
  140. static UseAbsoluteScaling: boolean;
  141. /**
  142. * Ratio for the scale of the gizmo (Default: 1)
  143. */
  144. set scaleRatio(value: number);
  145. get scaleRatio(): number;
  146. /**
  147. * True when the mouse pointer is hovered a gizmo mesh
  148. */
  149. get isHovered(): boolean;
  150. /**
  151. * If a custom mesh has been set (Default: false)
  152. */
  153. protected _customMeshSet: boolean;
  154. /**
  155. * Mesh that the gizmo will be attached to. (eg. on a drag gizmo the mesh that will be dragged)
  156. * * When set, interactions will be enabled
  157. */
  158. get attachedMesh(): Nullable<AbstractMesh>;
  159. set attachedMesh(value: Nullable<AbstractMesh>);
  160. /**
  161. * Node that the gizmo will be attached to. (eg. on a drag gizmo the mesh, bone or NodeTransform that will be dragged)
  162. * * When set, interactions will be enabled
  163. */
  164. get attachedNode(): Nullable<Node>;
  165. set attachedNode(value: Nullable<Node>);
  166. /**
  167. * Disposes and replaces the current meshes in the gizmo with the specified mesh
  168. * @param mesh The mesh to replace the default mesh of the gizmo
  169. */
  170. setCustomMesh(mesh: Mesh): void;
  171. /**
  172. * Additional transform applied to the gizmo.
  173. * It's useful when the gizmo is attached to a bone: if the bone is part of a skeleton attached to a mesh, you should define the mesh as additionalTransformNode if you want the gizmo to be displayed at the bone's correct location.
  174. * Otherwise, as the gizmo is relative to the skeleton root, the mesh transformation will not be taken into account.
  175. */
  176. get additionalTransformNode(): TransformNode | undefined;
  177. set additionalTransformNode(value: TransformNode | undefined);
  178. protected _updateGizmoRotationToMatchAttachedMesh: boolean;
  179. protected _updateGizmoPositionToMatchAttachedMesh: boolean;
  180. protected _anchorPoint: GizmoAnchorPoint;
  181. protected _updateScale: boolean;
  182. protected _coordinatesMode: GizmoCoordinatesMode;
  183. /**
  184. * If set the gizmo's rotation will be updated to match the attached mesh each frame (Default: true)
  185. * NOTE: This is only possible for meshes with uniform scaling, as otherwise it's not possible to decompose the rotation
  186. */
  187. set updateGizmoRotationToMatchAttachedMesh(value: boolean);
  188. get updateGizmoRotationToMatchAttachedMesh(): boolean;
  189. /**
  190. * If set the gizmo's position will be updated to match the attached mesh each frame (Default: true)
  191. */
  192. set updateGizmoPositionToMatchAttachedMesh(value: boolean);
  193. get updateGizmoPositionToMatchAttachedMesh(): boolean;
  194. /**
  195. * Defines where the gizmo will be positioned if `updateGizmoPositionToMatchAttachedMesh` is enabled.
  196. * (Default: GizmoAnchorPoint.Origin)
  197. */
  198. set anchorPoint(value: GizmoAnchorPoint);
  199. get anchorPoint(): GizmoAnchorPoint;
  200. /**
  201. * Set the coordinate system to use. By default it's local.
  202. * But it's possible for a user to tweak so its local for translation and world for rotation.
  203. * In that case, setting the coordinate system will change `updateGizmoRotationToMatchAttachedMesh` and `updateGizmoPositionToMatchAttachedMesh`
  204. */
  205. set coordinatesMode(coordinatesMode: GizmoCoordinatesMode);
  206. get coordinatesMode(): GizmoCoordinatesMode;
  207. /**
  208. * When set, the gizmo will always appear the same size no matter where the camera is (default: true)
  209. */
  210. set updateScale(value: boolean);
  211. get updateScale(): boolean;
  212. protected _interactionsEnabled: boolean;
  213. protected _attachedNodeChanged(value: Nullable<Node>): void;
  214. protected _beforeRenderObserver: Nullable<Observer<Scene>>;
  215. private _rightHandtoLeftHandMatrix;
  216. /**
  217. * Creates a gizmo
  218. * @param gizmoLayer The utility layer the gizmo will be added to
  219. */
  220. constructor(
  221. /** The utility layer the gizmo will be added to */
  222. gizmoLayer?: UtilityLayerRenderer);
  223. /**
  224. * posture that the gizmo will be display
  225. * When set null, default value will be used (Quaternion(0, 0, 0, 1))
  226. */
  227. get customRotationQuaternion(): Nullable<Quaternion>;
  228. set customRotationQuaternion(customRotationQuaternion: Nullable<Quaternion>);
  229. /**
  230. * Updates the gizmo to match the attached mesh's position/rotation
  231. */
  232. protected _update(): void;
  233. /**
  234. * if transform has a pivot and is not using PostMultiplyPivotMatrix, then the worldMatrix contains the pivot matrix (it's not cancelled at the end)
  235. * so, when extracting the world matrix component, the translation (and other components) is containing the pivot translation.
  236. * And the pivot is applied each frame. Removing it anyway here makes it applied only in computeWorldMatrix.
  237. * @param transform local transform that needs to be transform by the pivot inverse matrix
  238. * @param localMatrix local matrix that needs to be transform by the pivot inverse matrix
  239. * @param result resulting matrix transformed by pivot inverse if the transform node is using pivot without using post Multiply Pivot Matrix
  240. */
  241. protected _handlePivotMatrixInverse(transform: TransformNode, localMatrix: Matrix, result: Matrix): void;
  242. /**
  243. * computes the rotation/scaling/position of the transform once the Node world matrix has changed.
  244. */
  245. protected _matrixChanged(): void;
  246. /**
  247. * refresh gizmo mesh material
  248. * @param gizmoMeshes
  249. * @param material material to apply
  250. */
  251. protected _setGizmoMeshMaterial(gizmoMeshes: Mesh[], material: StandardMaterial): void;
  252. /**
  253. * Subscribes to pointer up, down, and hover events. Used for responsive gizmos.
  254. * @param gizmoLayer The utility layer the gizmo will be added to
  255. * @param gizmoAxisCache Gizmo axis definition used for reactive gizmo UI
  256. * @returns {Observer<PointerInfo>} pointerObserver
  257. */
  258. static GizmoAxisPointerObserver(gizmoLayer: UtilityLayerRenderer, gizmoAxisCache: Map<Mesh, GizmoAxisCache>): Observer<PointerInfo>;
  259. /**
  260. * Disposes of the gizmo
  261. */
  262. dispose(): void;
  263. }