babylon.spriteManager.js 7.4 KB

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