babylon.spriteManager.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // Color
  25. vertices.push(sprite.color.r);
  26. vertices.push(sprite.color.g);
  27. vertices.push(sprite.color.b);
  28. vertices.push(sprite.color.a);
  29. };
  30. BABYLON.SpriteManager = function (name, imgUrl, capacity, cellSize, scene, epsilon) {
  31. this.name = name;
  32. this._capacity = capacity;
  33. this.cellSize = cellSize;
  34. this._spriteTexture = new BABYLON.Texture(imgUrl, scene, true, false);
  35. this._spriteTexture.wrapU = false;
  36. this._spriteTexture.wrapV = false;
  37. this._epsilon = epsilon === undefined ? 0.01 : epsilon;
  38. this._scene = scene;
  39. this._scene.spriteManagers.push(this);
  40. // VBO
  41. this._vertexDeclaration = [3, 4, 4, 4];
  42. this._vertexStrideSize = 15 * 4; // 15 floats per sprite (x, y, z, angle, size, offsetX, offsetY, invertU, invertV, cellIndexX, cellIndexY, color)
  43. this._vertexBuffer = scene.getEngine().createDynamicVertexBuffer(capacity * this._vertexStrideSize * 4);
  44. var indices = [];
  45. var index = 0;
  46. for (var count = 0; count < capacity; count++) {
  47. indices.push(index);
  48. indices.push(index + 1);
  49. indices.push(index + 2);
  50. indices.push(index);
  51. indices.push(index + 2);
  52. indices.push(index + 3);
  53. index += 4;
  54. }
  55. this._indexBuffer = scene.getEngine().createIndexBuffer(indices);
  56. // Sprites
  57. this.sprites = [];
  58. // Effects
  59. this._effectBase = this._scene.getEngine().createEffect("sprites",
  60. ["position", "options", "cellInfo", "color"],
  61. ["view", "projection", "textureInfos", "alphaTest"],
  62. ["diffuseSampler"], "");
  63. this._effectFog = this._scene.getEngine().createEffect("sprites",
  64. ["position", "options", "cellInfo", "color"],
  65. ["view", "projection", "textureInfos", "alphaTest", "vFogInfos", "vFogColor"],
  66. ["diffuseSampler"], "#define FOG");
  67. };
  68. // Members
  69. BABYLON.SpriteManager.prototype.onDispose = null;
  70. // Methods
  71. BABYLON.SpriteManager.prototype.render = function() {
  72. // Check
  73. if (!this._effectBase.isReady() || !this._effectFog.isReady() || !this._spriteTexture || !this._spriteTexture.isReady())
  74. return 0;
  75. var engine = this._scene.getEngine();
  76. var baseSize = this._spriteTexture.getBaseSize();
  77. // Sprites
  78. var deltaTime = BABYLON.Tools.GetDeltaTime();
  79. var vertices = [];
  80. var max = Math.min(this._capacity, this.sprites.length);
  81. var rowSize = baseSize.width / this.cellSize;
  82. for (var index = 0; index < max; index++) {
  83. var sprite = this.sprites[index];
  84. sprite._animate(deltaTime);
  85. appendSpriteVertex(sprite, vertices, 0, 0, rowSize, this._epsilon);
  86. appendSpriteVertex(sprite, vertices, 1, 0, rowSize, this._epsilon);
  87. appendSpriteVertex(sprite, vertices, 1, 1, rowSize, this._epsilon);
  88. appendSpriteVertex(sprite, vertices, 0, 1, rowSize, this._epsilon);
  89. }
  90. engine.updateDynamicVertexBuffer(this._vertexBuffer, vertices);
  91. // Render
  92. var effect = this._effectBase;
  93. if (this._scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  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.setVector2("textureInfos", this.cellSize / baseSize.width, this.cellSize / baseSize.height);
  102. // Fog
  103. if (this._scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  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. BABYLON.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. })();