babylon.spriteManager.js 7.3 KB

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