renderingGroup.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. import { SmartArray } from "Tools/smartArray";
  2. import { SubMesh } from "Mesh/subMesh";
  3. import { AbstractMesh } from "Mesh/abstractMesh";
  4. import { Nullable } from "types";
  5. import { Vector3 } from "Math/math";
  6. import { IParticleSystem } from "Particles/IParticleSystem";
  7. import { IEdgesRenderer } from "./edgesRenderer";
  8. import { ISpriteManager } from "Sprites/spriteManager";
  9. import { Engine } from "Engine/engine";
  10. import { Material } from "Materials/material";
  11. import { Scene } from "scene";
  12. import { Camera } from "Cameras/camera";
  13. /**
  14. * This represents the object necessary to create a rendering group.
  15. * This is exclusively used and created by the rendering manager.
  16. * To modify the behavior, you use the available helpers in your scene or meshes.
  17. * @hidden
  18. */
  19. export class RenderingGroup {
  20. private _scene: Scene;
  21. private _opaqueSubMeshes = new SmartArray<SubMesh>(256);
  22. private _transparentSubMeshes = new SmartArray<SubMesh>(256);
  23. private _alphaTestSubMeshes = new SmartArray<SubMesh>(256);
  24. private _depthOnlySubMeshes = new SmartArray<SubMesh>(256);
  25. private _particleSystems = new SmartArray<IParticleSystem>(256);
  26. private _spriteManagers = new SmartArray<ISpriteManager>(256);
  27. private _opaqueSortCompareFn: Nullable<(a: SubMesh, b: SubMesh) => number>;
  28. private _alphaTestSortCompareFn: Nullable<(a: SubMesh, b: SubMesh) => number>;
  29. private _transparentSortCompareFn: (a: SubMesh, b: SubMesh) => number;
  30. private _renderOpaque: (subMeshes: SmartArray<SubMesh>) => void;
  31. private _renderAlphaTest: (subMeshes: SmartArray<SubMesh>) => void;
  32. private _renderTransparent: (subMeshes: SmartArray<SubMesh>) => void;
  33. private _edgesRenderers = new SmartArray<IEdgesRenderer>(16);
  34. public onBeforeTransparentRendering: () => void;
  35. /**
  36. * Set the opaque sort comparison function.
  37. * If null the sub meshes will be render in the order they were created
  38. */
  39. public set opaqueSortCompareFn(value: Nullable<(a: SubMesh, b: SubMesh) => number>) {
  40. this._opaqueSortCompareFn = value;
  41. if (value) {
  42. this._renderOpaque = this.renderOpaqueSorted;
  43. }
  44. else {
  45. this._renderOpaque = RenderingGroup.renderUnsorted;
  46. }
  47. }
  48. /**
  49. * Set the alpha test sort comparison function.
  50. * If null the sub meshes will be render in the order they were created
  51. */
  52. public set alphaTestSortCompareFn(value: Nullable<(a: SubMesh, b: SubMesh) => number>) {
  53. this._alphaTestSortCompareFn = value;
  54. if (value) {
  55. this._renderAlphaTest = this.renderAlphaTestSorted;
  56. }
  57. else {
  58. this._renderAlphaTest = RenderingGroup.renderUnsorted;
  59. }
  60. }
  61. /**
  62. * Set the transparent sort comparison function.
  63. * If null the sub meshes will be render in the order they were created
  64. */
  65. public set transparentSortCompareFn(value: Nullable<(a: SubMesh, b: SubMesh) => number>) {
  66. if (value) {
  67. this._transparentSortCompareFn = value;
  68. }
  69. else {
  70. this._transparentSortCompareFn = RenderingGroup.defaultTransparentSortCompare;
  71. }
  72. this._renderTransparent = this.renderTransparentSorted;
  73. }
  74. /**
  75. * Creates a new rendering group.
  76. * @param index The rendering group index
  77. * @param opaqueSortCompareFn The opaque sort comparison function. If null no order is applied
  78. * @param alphaTestSortCompareFn The alpha test sort comparison function. If null no order is applied
  79. * @param transparentSortCompareFn The transparent sort comparison function. If null back to front + alpha index sort is applied
  80. */
  81. constructor(public index: number, scene: Scene,
  82. opaqueSortCompareFn: Nullable<(a: SubMesh, b: SubMesh) => number> = null,
  83. alphaTestSortCompareFn: Nullable<(a: SubMesh, b: SubMesh) => number> = null,
  84. transparentSortCompareFn: Nullable<(a: SubMesh, b: SubMesh) => number> = null) {
  85. this._scene = scene;
  86. this.opaqueSortCompareFn = opaqueSortCompareFn;
  87. this.alphaTestSortCompareFn = alphaTestSortCompareFn;
  88. this.transparentSortCompareFn = transparentSortCompareFn;
  89. }
  90. /**
  91. * Render all the sub meshes contained in the group.
  92. * @param customRenderFunction Used to override the default render behaviour of the group.
  93. * @returns true if rendered some submeshes.
  94. */
  95. public render(customRenderFunction: Nullable<(opaqueSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, depthOnlySubMeshes: SmartArray<SubMesh>) => void>, renderSprites: boolean, renderParticles: boolean, activeMeshes: Nullable<AbstractMesh[]>): void {
  96. if (customRenderFunction) {
  97. customRenderFunction(this._opaqueSubMeshes, this._alphaTestSubMeshes, this._transparentSubMeshes, this._depthOnlySubMeshes);
  98. return;
  99. }
  100. var engine = this._scene.getEngine();
  101. // Depth only
  102. if (this._depthOnlySubMeshes.length !== 0) {
  103. engine.setColorWrite(false);
  104. this._renderAlphaTest(this._depthOnlySubMeshes);
  105. engine.setColorWrite(true);
  106. }
  107. // Opaque
  108. if (this._opaqueSubMeshes.length !== 0) {
  109. this._renderOpaque(this._opaqueSubMeshes);
  110. }
  111. // Alpha test
  112. if (this._alphaTestSubMeshes.length !== 0) {
  113. this._renderAlphaTest(this._alphaTestSubMeshes);
  114. }
  115. var stencilState = engine.getStencilBuffer();
  116. engine.setStencilBuffer(false);
  117. // Sprites
  118. if (renderSprites) {
  119. this._renderSprites();
  120. }
  121. // Particles
  122. if (renderParticles) {
  123. this._renderParticles(activeMeshes);
  124. }
  125. if (this.onBeforeTransparentRendering) {
  126. this.onBeforeTransparentRendering();
  127. }
  128. // Transparent
  129. if (this._transparentSubMeshes.length !== 0) {
  130. this._renderTransparent(this._transparentSubMeshes);
  131. engine.setAlphaMode(Engine.ALPHA_DISABLE);
  132. }
  133. // Set back stencil to false in case it changes before the edge renderer.
  134. engine.setStencilBuffer(false);
  135. // Edges
  136. if (this._edgesRenderers.length) {
  137. for (var edgesRendererIndex = 0; edgesRendererIndex < this._edgesRenderers.length; edgesRendererIndex++) {
  138. this._edgesRenderers.data[edgesRendererIndex].render();
  139. }
  140. engine.setAlphaMode(Engine.ALPHA_DISABLE);
  141. }
  142. // Restore Stencil state.
  143. engine.setStencilBuffer(stencilState);
  144. }
  145. /**
  146. * Renders the opaque submeshes in the order from the opaqueSortCompareFn.
  147. * @param subMeshes The submeshes to render
  148. */
  149. private renderOpaqueSorted(subMeshes: SmartArray<SubMesh>): void {
  150. return RenderingGroup.renderSorted(subMeshes, this._opaqueSortCompareFn, this._scene.activeCamera, false);
  151. }
  152. /**
  153. * Renders the opaque submeshes in the order from the alphatestSortCompareFn.
  154. * @param subMeshes The submeshes to render
  155. */
  156. private renderAlphaTestSorted(subMeshes: SmartArray<SubMesh>): void {
  157. return RenderingGroup.renderSorted(subMeshes, this._alphaTestSortCompareFn, this._scene.activeCamera, false);
  158. }
  159. /**
  160. * Renders the opaque submeshes in the order from the transparentSortCompareFn.
  161. * @param subMeshes The submeshes to render
  162. */
  163. private renderTransparentSorted(subMeshes: SmartArray<SubMesh>): void {
  164. return RenderingGroup.renderSorted(subMeshes, this._transparentSortCompareFn, this._scene.activeCamera, true);
  165. }
  166. /**
  167. * Renders the submeshes in a specified order.
  168. * @param subMeshes The submeshes to sort before render
  169. * @param sortCompareFn The comparison function use to sort
  170. * @param cameraPosition The camera position use to preprocess the submeshes to help sorting
  171. * @param transparent Specifies to activate blending if true
  172. */
  173. private static renderSorted(subMeshes: SmartArray<SubMesh>, sortCompareFn: Nullable<(a: SubMesh, b: SubMesh) => number>, camera: Nullable<Camera>, transparent: boolean): void {
  174. let subIndex = 0;
  175. let subMesh: SubMesh;
  176. let cameraPosition = camera ? camera.globalPosition : Vector3.Zero();
  177. for (; subIndex < subMeshes.length; subIndex++) {
  178. subMesh = subMeshes.data[subIndex];
  179. subMesh._alphaIndex = subMesh.getMesh().alphaIndex;
  180. subMesh._distanceToCamera = subMesh.getBoundingInfo().boundingSphere.centerWorld.subtract(cameraPosition).length();
  181. }
  182. let sortedArray = subMeshes.data.slice(0, subMeshes.length);
  183. if (sortCompareFn) {
  184. sortedArray.sort(sortCompareFn);
  185. }
  186. for (subIndex = 0; subIndex < sortedArray.length; subIndex++) {
  187. subMesh = sortedArray[subIndex];
  188. if (transparent) {
  189. let material = subMesh.getMaterial();
  190. if (material && material.needDepthPrePass) {
  191. let engine = material.getScene().getEngine();
  192. engine.setColorWrite(false);
  193. engine.setAlphaMode(Engine.ALPHA_DISABLE);
  194. subMesh.render(false);
  195. engine.setColorWrite(true);
  196. }
  197. }
  198. subMesh.render(transparent);
  199. }
  200. }
  201. /**
  202. * Renders the submeshes in the order they were dispatched (no sort applied).
  203. * @param subMeshes The submeshes to render
  204. */
  205. private static renderUnsorted(subMeshes: SmartArray<SubMesh>): void {
  206. for (var subIndex = 0; subIndex < subMeshes.length; subIndex++) {
  207. let submesh = subMeshes.data[subIndex];
  208. submesh.render(false);
  209. }
  210. }
  211. /**
  212. * Build in function which can be applied to ensure meshes of a special queue (opaque, alpha test, transparent)
  213. * are rendered back to front if in the same alpha index.
  214. *
  215. * @param a The first submesh
  216. * @param b The second submesh
  217. * @returns The result of the comparison
  218. */
  219. public static defaultTransparentSortCompare(a: SubMesh, b: SubMesh): number {
  220. // Alpha index first
  221. if (a._alphaIndex > b._alphaIndex) {
  222. return 1;
  223. }
  224. if (a._alphaIndex < b._alphaIndex) {
  225. return -1;
  226. }
  227. // Then distance to camera
  228. return RenderingGroup.backToFrontSortCompare(a, b);
  229. }
  230. /**
  231. * Build in function which can be applied to ensure meshes of a special queue (opaque, alpha test, transparent)
  232. * are rendered back to front.
  233. *
  234. * @param a The first submesh
  235. * @param b The second submesh
  236. * @returns The result of the comparison
  237. */
  238. public static backToFrontSortCompare(a: SubMesh, b: SubMesh): number {
  239. // Then distance to camera
  240. if (a._distanceToCamera < b._distanceToCamera) {
  241. return 1;
  242. }
  243. if (a._distanceToCamera > b._distanceToCamera) {
  244. return -1;
  245. }
  246. return 0;
  247. }
  248. /**
  249. * Build in function which can be applied to ensure meshes of a special queue (opaque, alpha test, transparent)
  250. * are rendered front to back (prevent overdraw).
  251. *
  252. * @param a The first submesh
  253. * @param b The second submesh
  254. * @returns The result of the comparison
  255. */
  256. public static frontToBackSortCompare(a: SubMesh, b: SubMesh): number {
  257. // Then distance to camera
  258. if (a._distanceToCamera < b._distanceToCamera) {
  259. return -1;
  260. }
  261. if (a._distanceToCamera > b._distanceToCamera) {
  262. return 1;
  263. }
  264. return 0;
  265. }
  266. /**
  267. * Resets the different lists of submeshes to prepare a new frame.
  268. */
  269. public prepare(): void {
  270. this._opaqueSubMeshes.reset();
  271. this._transparentSubMeshes.reset();
  272. this._alphaTestSubMeshes.reset();
  273. this._depthOnlySubMeshes.reset();
  274. this._particleSystems.reset();
  275. this._spriteManagers.reset();
  276. this._edgesRenderers.reset();
  277. }
  278. public dispose(): void {
  279. this._opaqueSubMeshes.dispose();
  280. this._transparentSubMeshes.dispose();
  281. this._alphaTestSubMeshes.dispose();
  282. this._depthOnlySubMeshes.dispose();
  283. this._particleSystems.dispose();
  284. this._spriteManagers.dispose();
  285. this._edgesRenderers.dispose();
  286. }
  287. /**
  288. * Inserts the submesh in its correct queue depending on its material.
  289. * @param subMesh The submesh to dispatch
  290. * @param [mesh] Optional reference to the submeshes's mesh. Provide if you have an exiting reference to improve performance.
  291. * @param [material] Optional reference to the submeshes's material. Provide if you have an exiting reference to improve performance.
  292. */
  293. public dispatch(subMesh: SubMesh, mesh?: AbstractMesh, material?: Nullable<Material>): void {
  294. // Get mesh and materials if not provided
  295. if (mesh === undefined) {
  296. mesh = subMesh.getMesh();
  297. }
  298. if (material === undefined) {
  299. material = subMesh.getMaterial();
  300. }
  301. if (material === null || material === undefined) {
  302. return;
  303. }
  304. if (material.needAlphaBlendingForMesh(mesh)) { // Transparent
  305. this._transparentSubMeshes.push(subMesh);
  306. } else if (material.needAlphaTesting()) { // Alpha test
  307. if (material.needDepthPrePass) {
  308. this._depthOnlySubMeshes.push(subMesh);
  309. }
  310. this._alphaTestSubMeshes.push(subMesh);
  311. } else {
  312. if (material.needDepthPrePass) {
  313. this._depthOnlySubMeshes.push(subMesh);
  314. }
  315. this._opaqueSubMeshes.push(subMesh); // Opaque
  316. }
  317. if (mesh._edgesRenderer && mesh._edgesRenderer.isEnabled) {
  318. this._edgesRenderers.push(mesh._edgesRenderer);
  319. }
  320. }
  321. public dispatchSprites(spriteManager: ISpriteManager) {
  322. this._spriteManagers.push(spriteManager);
  323. }
  324. public dispatchParticles(particleSystem: IParticleSystem) {
  325. this._particleSystems.push(particleSystem);
  326. }
  327. private _renderParticles(activeMeshes: Nullable<AbstractMesh[]>): void {
  328. if (this._particleSystems.length === 0) {
  329. return;
  330. }
  331. // Particles
  332. var activeCamera = this._scene.activeCamera;
  333. this._scene.onBeforeParticlesRenderingObservable.notifyObservers(this._scene);
  334. for (var particleIndex = 0; particleIndex < this._particleSystems.length; particleIndex++) {
  335. var particleSystem = this._particleSystems.data[particleIndex];
  336. if ((activeCamera && activeCamera.layerMask & particleSystem.layerMask) === 0) {
  337. continue;
  338. }
  339. let emitter: any = particleSystem.emitter;
  340. if (!emitter.position || !activeMeshes || activeMeshes.indexOf(emitter) !== -1) {
  341. this._scene._activeParticles.addCount(particleSystem.render(), false);
  342. }
  343. }
  344. this._scene.onAfterParticlesRenderingObservable.notifyObservers(this._scene);
  345. }
  346. private _renderSprites(): void {
  347. if (!this._scene.spritesEnabled || this._spriteManagers.length === 0) {
  348. return;
  349. }
  350. // Sprites
  351. var activeCamera = this._scene.activeCamera;
  352. this._scene.onBeforeSpritesRenderingObservable.notifyObservers(this._scene);
  353. for (var id = 0; id < this._spriteManagers.length; id++) {
  354. var spriteManager = this._spriteManagers.data[id];
  355. if (((activeCamera && activeCamera.layerMask & spriteManager.layerMask) !== 0)) {
  356. spriteManager.render();
  357. }
  358. }
  359. this._scene.onAfterSpritesRenderingObservable.notifyObservers(this._scene);
  360. }
  361. }