babylon.spriteManager.js 7.6 KB

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