babylon.spriteManager.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. var appendSpriteVertex = function (sprite, vertices, offsetX, offsetY, rowSize, epsilon) {
  4. if (offsetX == 0)
  5. offsetX = epsilon;
  6. else if (offsetX == 1)
  7. offsetX = 1 - epsilon;
  8. if (offsetY == 0)
  9. offsetY = epsilon;
  10. else if (offsetY == 1)
  11. offsetY = 1 - epsilon;
  12. vertices.push(sprite.position.x);
  13. vertices.push(sprite.position.y);
  14. vertices.push(sprite.position.z);
  15. vertices.push(sprite.angle);
  16. vertices.push(sprite.size);
  17. vertices.push(offsetX);
  18. vertices.push(offsetY);
  19. vertices.push(sprite.invertU ? 1 : 0);
  20. vertices.push(sprite.invertV ? 1 : 0);
  21. var offset = (sprite.cellIndex / rowSize) >> 0;
  22. vertices.push(sprite.cellIndex - offset * rowSize);
  23. vertices.push(offset);
  24. };
  25. BABYLON.SpriteManager = function (name, imgUrl, capacity, cellSize, scene, epsilon) {
  26. this.name = name;
  27. this._capacity = capacity;
  28. this.cellSize = cellSize;
  29. this._spriteTexture = new BABYLON.Texture(imgUrl, scene, true, false);
  30. this._spriteTexture.wrapU = false;
  31. this._spriteTexture.wrapV = false;
  32. this._epsilon = epsilon === undefined ? 0.01 : epsilon;
  33. this._scene = scene;
  34. this._scene.spriteManagers.push(this);
  35. // VBO
  36. this._vertexDeclaration = [3, 4, 4];
  37. this._vertexStrideSize = 11 * 4; // 11 floats per sprite (x, y, z, angle, size, offsetX, offsetY, invertU, invertV, cellIndexX, cellIndexY)
  38. this._vertexBuffer = scene.getEngine().createDynamicVertexBuffer(capacity * this._vertexStrideSize * 4);
  39. var indices = [];
  40. var index = 0;
  41. for (var count = 0; count < capacity; count++) {
  42. indices.push(index);
  43. indices.push(index + 1);
  44. indices.push(index + 2);
  45. indices.push(index);
  46. indices.push(index + 2);
  47. indices.push(index + 3);
  48. index += 4;
  49. }
  50. this._indexBuffer = scene.getEngine().createIndexBuffer(indices);
  51. // Sprites
  52. this.sprites = [];
  53. // Effects
  54. this._effectBase = this._scene.getEngine().createEffect("sprites",
  55. ["position", "options", "cellInfo"],
  56. ["view", "projection", "textureInfos", "alphaTest"],
  57. ["diffuseSampler"], "");
  58. this._effectFog = this._scene.getEngine().createEffect("sprites",
  59. ["position", "options", "cellInfo"],
  60. ["view", "projection", "textureInfos", "alphaTest", "vFogInfos", "vFogColor"],
  61. ["diffuseSampler"], "#define FOG");
  62. };
  63. // Members
  64. BABYLON.SpriteManager.prototype.onDispose = null;
  65. // Methods
  66. BABYLON.SpriteManager.prototype.render = function() {
  67. // Check
  68. if (!this._effectBase.isReady() || !this._effectFog.isReady() || !this._spriteTexture || !this._spriteTexture.isReady())
  69. return 0;
  70. var engine = this._scene.getEngine();
  71. var baseSize = this._spriteTexture.getBaseSize();
  72. // Sprites
  73. var deltaTime = BABYLON.Tools.GetDeltaTime();
  74. var vertices = [];
  75. var max = Math.min(this._capacity, this.sprites.length);
  76. var rowSize = baseSize.width / this.cellSize;
  77. for (var index = 0; index < max; index++) {
  78. var sprite = this.sprites[index];
  79. sprite._animate(deltaTime);
  80. appendSpriteVertex(sprite, vertices, 0, 0, rowSize, this._epsilon);
  81. appendSpriteVertex(sprite, vertices, 1, 0, rowSize, this._epsilon);
  82. appendSpriteVertex(sprite, vertices, 1, 1, rowSize, this._epsilon);
  83. appendSpriteVertex(sprite, vertices, 0, 1, rowSize, this._epsilon);
  84. }
  85. engine.updateDynamicVertexBuffer(this._vertexBuffer, vertices);
  86. // Render
  87. var effect = this._effectBase;
  88. if (this._scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  89. effect = this._effectFog;
  90. }
  91. engine.enableEffect(effect);
  92. var viewMatrix = this._scene.getViewMatrix();
  93. effect.setTexture("diffuseSampler", this._spriteTexture);
  94. effect.setMatrix("view", viewMatrix);
  95. effect.setMatrix("projection", this._scene.getProjectionMatrix());
  96. effect.setVector2("textureInfos", this.cellSize / baseSize.width, this.cellSize / baseSize.height);
  97. // Fog
  98. if (this._scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  99. effect.setFloat4("vFogInfos", this._scene.fogMode, this._scene.fogStart, this._scene.fogEnd, this._scene.fogDensity);
  100. effect.setColor3("vFogColor", this._scene.fogColor);
  101. }
  102. // VBOs
  103. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, effect);
  104. // Draw order
  105. effect.setBool("alphaTest", true);
  106. engine.setColorWrite(false);
  107. engine.draw(true, 0, max * 6);
  108. engine.setColorWrite(true);
  109. effect.setBool("alphaTest", false);
  110. engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
  111. engine.draw(true, 0, max * 6);
  112. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  113. };
  114. BABYLON.SpriteManager.prototype.dispose = function () {
  115. if (this._vertexBuffer) {
  116. //this._scene.getEngine()._releaseBuffer(this._vertexBuffer);
  117. this._vertexBuffer = null;
  118. }
  119. if (this._indexBuffer) {
  120. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  121. this._indexBuffer = null;
  122. }
  123. if (this._spriteTexture) {
  124. this._spriteTexture.dispose();
  125. this._spriteTexture = null;
  126. }
  127. // Remove from scene
  128. var index = this._scene.spriteManagers.indexOf(this);
  129. this._scene.spriteManagers.splice(index, 1);
  130. // Callback
  131. if (this.onDispose) {
  132. this.onDispose();
  133. }
  134. };
  135. })();