renderingGroup.js 15 KB

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