babylon.renderingGroup.ts 16 KB

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