babylon.spriteManager.js 7.0 KB

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