babylon.spriteManager.js 7.0 KB

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