spriteManager.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import type { IDisposable, Scene } from "../scene";
  2. import type { Nullable } from "../types";
  3. import { Observable } from "../Misc/observable";
  4. import { Sprite } from "./sprite";
  5. import { PickingInfo } from "../Collisions/pickingInfo";
  6. import type { Camera } from "../Cameras/camera";
  7. import { Texture } from "../Materials/Textures/texture";
  8. import type { Ray } from "../Culling/ray";
  9. /**
  10. * Defines the minimum interface to fulfill in order to be a sprite manager.
  11. */
  12. export interface ISpriteManager extends IDisposable {
  13. /**
  14. * Gets manager's name
  15. */
  16. name: string;
  17. /**
  18. * Restricts the camera to viewing objects with the same layerMask.
  19. * A camera with a layerMask of 1 will render spriteManager.layerMask & camera.layerMask!== 0
  20. */
  21. layerMask: number;
  22. /**
  23. * Gets or sets a boolean indicating if the mesh can be picked (by scene.pick for instance or through actions). Default is true
  24. */
  25. isPickable: boolean;
  26. /**
  27. * Gets the hosting scene
  28. */
  29. scene: Scene;
  30. /**
  31. * Specifies the rendering group id for this mesh (0 by default)
  32. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/advanced/transparent_rendering#rendering-groups
  33. */
  34. renderingGroupId: number;
  35. /**
  36. * Defines the list of sprites managed by the manager.
  37. */
  38. sprites: Array<Sprite>;
  39. /**
  40. * Gets or sets the spritesheet texture
  41. */
  42. texture: Texture;
  43. /** Defines the default width of a cell in the spritesheet */
  44. cellWidth: number;
  45. /** Defines the default height of a cell in the spritesheet */
  46. cellHeight: number;
  47. /** @internal */
  48. _wasDispatched: boolean;
  49. /**
  50. * Tests the intersection of a sprite with a specific ray.
  51. * @param ray The ray we are sending to test the collision
  52. * @param camera The camera space we are sending rays in
  53. * @param predicate A predicate allowing excluding sprites from the list of object to test
  54. * @param fastCheck defines if the first intersection will be used (and not the closest)
  55. * @returns picking info or null.
  56. */
  57. intersects(ray: Ray, camera: Camera, predicate?: (sprite: Sprite) => boolean, fastCheck?: boolean): Nullable<PickingInfo>;
  58. /**
  59. * Intersects the sprites with a ray
  60. * @param ray defines the ray to intersect with
  61. * @param camera defines the current active camera
  62. * @param predicate defines a predicate used to select candidate sprites
  63. * @returns null if no hit or a PickingInfo array
  64. */
  65. multiIntersects(ray: Ray, camera: Camera, predicate?: (sprite: Sprite) => boolean): Nullable<PickingInfo[]>;
  66. /**
  67. * Renders the list of sprites on screen.
  68. */
  69. render(): void;
  70. /**
  71. * Rebuilds the manager (after a context lost, for eg)
  72. */
  73. rebuild(): void;
  74. /**
  75. * Serializes the sprite manager to a JSON object
  76. */
  77. serialize(serializeTexture?: boolean): any;
  78. }
  79. /**
  80. * Class used to manage multiple sprites on the same spritesheet
  81. * @see https://doc.babylonjs.com/features/featuresDeepDive/sprites
  82. */
  83. export declare class SpriteManager implements ISpriteManager {
  84. /** defines the manager's name */
  85. name: string;
  86. /** Define the Url to load snippets */
  87. static SnippetUrl: string;
  88. /** Snippet ID if the manager was created from the snippet server */
  89. snippetId: string;
  90. /** Gets the list of sprites */
  91. sprites: Sprite[];
  92. /** Gets or sets the rendering group id (0 by default) */
  93. renderingGroupId: number;
  94. /** Gets or sets camera layer mask */
  95. layerMask: number;
  96. /** Gets or sets a boolean indicating if the sprites are pickable */
  97. isPickable: boolean;
  98. /**
  99. * Gets or sets an object used to store user defined information for the sprite manager
  100. */
  101. metadata: any;
  102. /** @internal */
  103. _wasDispatched: boolean;
  104. /**
  105. * An event triggered when the manager is disposed.
  106. */
  107. onDisposeObservable: Observable<SpriteManager>;
  108. /**
  109. * Callback called when the manager is disposed
  110. */
  111. set onDispose(callback: () => void);
  112. /**
  113. * Gets or sets the unique id of the sprite
  114. */
  115. uniqueId: number;
  116. /**
  117. * Gets the array of sprites
  118. */
  119. get children(): Sprite[];
  120. /**
  121. * Gets the hosting scene
  122. */
  123. get scene(): Scene;
  124. /**
  125. * Gets the capacity of the manager
  126. */
  127. get capacity(): number;
  128. /**
  129. * Gets or sets the spritesheet texture
  130. */
  131. get texture(): Texture;
  132. set texture(value: Texture);
  133. /** Defines the default width of a cell in the spritesheet */
  134. get cellWidth(): number;
  135. set cellWidth(value: number);
  136. /** Defines the default height of a cell in the spritesheet */
  137. get cellHeight(): number;
  138. set cellHeight(value: number);
  139. /** Gets or sets a boolean indicating if the manager must consider scene fog when rendering */
  140. get fogEnabled(): boolean;
  141. set fogEnabled(value: boolean);
  142. /**
  143. * Blend mode use to render the particle, it can be any of
  144. * the static Constants.ALPHA_x properties provided in this class.
  145. * Default value is Constants.ALPHA_COMBINE
  146. */
  147. get blendMode(): number;
  148. set blendMode(blendMode: number);
  149. private _disableDepthWrite;
  150. /** Disables writing to the depth buffer when rendering the sprites.
  151. * It can be handy to disable depth writing when using textures without alpha channel
  152. * and setting some specific blend modes.
  153. */
  154. get disableDepthWrite(): boolean;
  155. set disableDepthWrite(value: boolean);
  156. /**
  157. * Gets or sets a boolean indicating if the renderer must render sprites with pixel perfect rendering
  158. * In this mode, sprites are rendered as "pixel art", which means that they appear as pixelated but remain stable when moving or when rotated or scaled.
  159. * Note that for this mode to work as expected, the sprite texture must use the BILINEAR sampling mode, not NEAREST!
  160. */
  161. get pixelPerfect(): boolean;
  162. set pixelPerfect(value: boolean);
  163. private _spriteRenderer;
  164. /** Associative array from JSON sprite data file */
  165. private _cellData;
  166. /** Array of sprite names from JSON sprite data file */
  167. private _spriteMap;
  168. /** True when packed cell data from JSON file is ready*/
  169. private _packedAndReady;
  170. private _textureContent;
  171. private _onDisposeObserver;
  172. private _fromPacked;
  173. private _scene;
  174. /**
  175. * Creates a new sprite manager
  176. * @param name defines the manager's name
  177. * @param imgUrl defines the sprite sheet url
  178. * @param capacity defines the maximum allowed number of sprites
  179. * @param cellSize defines the size of a sprite cell
  180. * @param scene defines the hosting scene
  181. * @param epsilon defines the epsilon value to align texture (0.01 by default)
  182. * @param samplingMode defines the sampling mode to use with spritesheet
  183. * @param fromPacked set to false; do not alter
  184. * @param spriteJSON null otherwise a JSON object defining sprite sheet data; do not alter
  185. */
  186. constructor(
  187. /** defines the manager's name */
  188. name: string, imgUrl: string, capacity: number, cellSize: any, scene: Scene, epsilon?: number, samplingMode?: number, fromPacked?: boolean, spriteJSON?: any | null);
  189. /**
  190. * Returns the string "SpriteManager"
  191. * @returns "SpriteManager"
  192. */
  193. getClassName(): string;
  194. private _makePacked;
  195. private _checkTextureAlpha;
  196. /**
  197. * Intersects the sprites with a ray
  198. * @param ray defines the ray to intersect with
  199. * @param camera defines the current active camera
  200. * @param predicate defines a predicate used to select candidate sprites
  201. * @param fastCheck defines if a fast check only must be done (the first potential sprite is will be used and not the closer)
  202. * @returns null if no hit or a PickingInfo
  203. */
  204. intersects(ray: Ray, camera: Camera, predicate?: (sprite: Sprite) => boolean, fastCheck?: boolean): Nullable<PickingInfo>;
  205. /**
  206. * Intersects the sprites with a ray
  207. * @param ray defines the ray to intersect with
  208. * @param camera defines the current active camera
  209. * @param predicate defines a predicate used to select candidate sprites
  210. * @returns null if no hit or a PickingInfo array
  211. */
  212. multiIntersects(ray: Ray, camera: Camera, predicate?: (sprite: Sprite) => boolean): Nullable<PickingInfo[]>;
  213. /**
  214. * Render all child sprites
  215. */
  216. render(): void;
  217. private _customUpdate;
  218. /**
  219. * Rebuilds the manager (after a context lost, for eg)
  220. */
  221. rebuild(): void;
  222. /**
  223. * Release associated resources
  224. */
  225. dispose(): void;
  226. /**
  227. * Serializes the sprite manager to a JSON object
  228. * @param serializeTexture defines if the texture must be serialized as well
  229. * @returns the JSON object
  230. */
  231. serialize(serializeTexture?: boolean): any;
  232. /**
  233. * Parses a JSON object to create a new sprite manager.
  234. * @param parsedManager The JSON object to parse
  235. * @param scene The scene to create the sprite manager
  236. * @param rootUrl The root url to use to load external dependencies like texture
  237. * @returns the new sprite manager
  238. */
  239. static Parse(parsedManager: any, scene: Scene, rootUrl: string): SpriteManager;
  240. /**
  241. * Creates a sprite manager from a snippet saved in a remote file
  242. * @param name defines the name of the sprite manager to create (can be null or empty to use the one from the json data)
  243. * @param url defines the url to load from
  244. * @param scene defines the hosting scene
  245. * @param rootUrl defines the root URL to use to load textures and relative dependencies
  246. * @returns a promise that will resolve to the new sprite manager
  247. */
  248. static ParseFromFileAsync(name: Nullable<string>, url: string, scene: Scene, rootUrl?: string): Promise<SpriteManager>;
  249. /**
  250. * Creates a sprite manager from a snippet saved by the sprite editor
  251. * @param snippetId defines the snippet to load (can be set to _BLANK to create a default one)
  252. * @param scene defines the hosting scene
  253. * @param rootUrl defines the root URL to use to load textures and relative dependencies
  254. * @returns a promise that will resolve to the new sprite manager
  255. */
  256. static ParseFromSnippetAsync(snippetId: string, scene: Scene, rootUrl?: string): Promise<SpriteManager>;
  257. /**
  258. * Creates a sprite manager from a snippet saved by the sprite editor
  259. * @deprecated Please use ParseFromSnippetAsync instead
  260. * @param snippetId defines the snippet to load (can be set to _BLANK to create a default one)
  261. * @param scene defines the hosting scene
  262. * @param rootUrl defines the root URL to use to load textures and relative dependencies
  263. * @returns a promise that will resolve to the new sprite manager
  264. */
  265. static CreateFromSnippetAsync: typeof SpriteManager.ParseFromSnippetAsync;
  266. }