fluidRenderingObjectCustomParticles.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { VertexBuffer } from "../../Buffers/buffer.js";
  2. import { EffectWrapper } from "../../Materials/effectRenderer.js";
  3. import { FluidRenderingObject } from "./fluidRenderingObject.js";
  4. /**
  5. * Defines a rendering object based on a list of custom buffers
  6. * The list must contain at least a "position" buffer!
  7. */
  8. export class FluidRenderingObjectCustomParticles extends FluidRenderingObject {
  9. /**
  10. * @returns the name of the class
  11. */
  12. getClassName() {
  13. return "FluidRenderingObjectCustomParticles";
  14. }
  15. /**
  16. * Gets the vertex buffers
  17. */
  18. get vertexBuffers() {
  19. return this._vertexBuffers;
  20. }
  21. /**
  22. * Creates a new instance of the class
  23. * @param scene The scene the particles should be rendered into
  24. * @param buffers The list of buffers (must contain at least one "position" buffer!). Note that you don't have to pass all (or any!) buffers at once in the constructor, you can use the addBuffers method to add more later.
  25. * @param numParticles Number of vertices to take into account from the buffers
  26. */
  27. constructor(scene, buffers, numParticles) {
  28. super(scene);
  29. this._numParticles = numParticles;
  30. this._diffuseEffectWrapper = null;
  31. this._vertexBuffers = {};
  32. this.addBuffers(buffers);
  33. }
  34. /**
  35. * Add some new buffers
  36. * @param buffers List of buffers
  37. */
  38. addBuffers(buffers) {
  39. for (const name in buffers) {
  40. let stride;
  41. let instanced = true;
  42. switch (name) {
  43. case "velocity":
  44. stride = 3;
  45. break;
  46. case "offset":
  47. instanced = false;
  48. break;
  49. }
  50. this._vertexBuffers[name] = new VertexBuffer(this._engine, buffers[name], name, true, false, stride, instanced);
  51. }
  52. }
  53. _createEffects() {
  54. super._createEffects();
  55. const uniformNames = ["view", "projection", "size"];
  56. const attributeNames = ["position", "offset", "color"];
  57. this._diffuseEffectWrapper = new EffectWrapper({
  58. engine: this._engine,
  59. useShaderStore: true,
  60. vertexShader: "fluidRenderingParticleDiffuse",
  61. fragmentShader: "fluidRenderingParticleDiffuse",
  62. attributeNames,
  63. uniformNames,
  64. samplerNames: [],
  65. });
  66. }
  67. /**
  68. * Indicates if the object is ready to be rendered
  69. * @returns True if everything is ready for the object to be rendered, otherwise false
  70. */
  71. isReady() {
  72. if (!this._vertexBuffers["offset"]) {
  73. this._vertexBuffers["offset"] = new VertexBuffer(this._engine, [0, 0, 1, 0, 0, 1, 1, 1], "offset", false, false, 2);
  74. }
  75. return super.isReady() && (this._diffuseEffectWrapper?.effect.isReady() ?? false);
  76. }
  77. /**
  78. * Gets the number of particles in this object
  79. * @returns The number of particles
  80. */
  81. get numParticles() {
  82. return this._numParticles;
  83. }
  84. /**
  85. * Sets the number of particles in this object
  86. * @param num The number of particles to take into account
  87. */
  88. setNumParticles(num) {
  89. this._numParticles = num;
  90. }
  91. /**
  92. * Render the diffuse texture for this object
  93. */
  94. renderDiffuseTexture() {
  95. const numParticles = this.numParticles;
  96. if (!this._diffuseEffectWrapper || numParticles === 0) {
  97. return;
  98. }
  99. const diffuseDrawWrapper = this._diffuseEffectWrapper._drawWrapper;
  100. const diffuseEffect = diffuseDrawWrapper.effect;
  101. this._engine.enableEffect(diffuseDrawWrapper);
  102. this._engine.bindBuffers(this.vertexBuffers, this.indexBuffer, diffuseEffect);
  103. diffuseEffect.setMatrix("view", this._scene.getViewMatrix());
  104. diffuseEffect.setMatrix("projection", this._scene.getProjectionMatrix());
  105. if (this._particleSize !== null) {
  106. diffuseEffect.setFloat2("size", this._particleSize, this._particleSize);
  107. }
  108. if (this.useInstancing) {
  109. this._engine.drawArraysType(7, 0, 4, numParticles);
  110. }
  111. else {
  112. this._engine.drawElementsType(0, 0, numParticles);
  113. }
  114. }
  115. /**
  116. * Releases the ressources used by the class
  117. */
  118. dispose() {
  119. super.dispose();
  120. this._diffuseEffectWrapper?.dispose();
  121. for (const name in this._vertexBuffers) {
  122. this._vertexBuffers[name].dispose();
  123. }
  124. this._vertexBuffers = {};
  125. }
  126. }
  127. //# sourceMappingURL=fluidRenderingObjectCustomParticles.js.map