babylon.spriteManager.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.layerMask = 0x0FFFFFFF;
  11. this.fogEnabled = true;
  12. this.isPickable = false;
  13. this._vertexDeclaration = [4, 4, 4, 4];
  14. this._vertexStrideSize = 16 * 4; // 15 floats per sprite (x, y, z, angle, sizeX, sizeY, offsetX, offsetY, invertU, invertV, cellIndexX, cellIndexY, color)
  15. this._capacity = capacity;
  16. this._spriteTexture = new BABYLON.Texture(imgUrl, scene, true, false, samplingMode);
  17. this._spriteTexture.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  18. this._spriteTexture.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  19. this._epsilon = epsilon === undefined ? 0.01 : epsilon;
  20. this._scene = scene;
  21. this._scene.spriteManagers.push(this);
  22. // VBO
  23. this._vertexBuffer = scene.getEngine().createDynamicVertexBuffer(capacity * this._vertexStrideSize * 4);
  24. var indices = [];
  25. var index = 0;
  26. for (var count = 0; count < capacity; count++) {
  27. indices.push(index);
  28. indices.push(index + 1);
  29. indices.push(index + 2);
  30. indices.push(index);
  31. indices.push(index + 2);
  32. indices.push(index + 3);
  33. index += 4;
  34. }
  35. this._indexBuffer = scene.getEngine().createIndexBuffer(indices);
  36. this._vertices = new Float32Array(capacity * this._vertexStrideSize);
  37. // Effects
  38. this._effectBase = this._scene.getEngine().createEffect("sprites", ["position", "options", "cellInfo", "color"], ["view", "projection", "textureInfos", "alphaTest"], ["diffuseSampler"], "");
  39. this._effectFog = this._scene.getEngine().createEffect("sprites", ["position", "options", "cellInfo", "color"], ["view", "projection", "textureInfos", "alphaTest", "vFogInfos", "vFogColor"], ["diffuseSampler"], "#define FOG");
  40. }
  41. SpriteManager.prototype._appendSpriteVertex = function (index, sprite, offsetX, offsetY, rowSize) {
  42. var arrayOffset = index * 16;
  43. if (offsetX === 0)
  44. offsetX = this._epsilon;
  45. else if (offsetX === 1)
  46. offsetX = 1 - this._epsilon;
  47. if (offsetY === 0)
  48. offsetY = this._epsilon;
  49. else if (offsetY === 1)
  50. offsetY = 1 - this._epsilon;
  51. this._vertices[arrayOffset] = sprite.position.x;
  52. this._vertices[arrayOffset + 1] = sprite.position.y;
  53. this._vertices[arrayOffset + 2] = sprite.position.z;
  54. this._vertices[arrayOffset + 3] = sprite.angle;
  55. this._vertices[arrayOffset + 4] = sprite.width;
  56. this._vertices[arrayOffset + 5] = sprite.height;
  57. this._vertices[arrayOffset + 6] = offsetX;
  58. this._vertices[arrayOffset + 7] = offsetY;
  59. this._vertices[arrayOffset + 8] = sprite.invertU ? 1 : 0;
  60. this._vertices[arrayOffset + 9] = sprite.invertV ? 1 : 0;
  61. var offset = (sprite.cellIndex / rowSize) >> 0;
  62. this._vertices[arrayOffset + 10] = sprite.cellIndex - offset * rowSize;
  63. this._vertices[arrayOffset + 11] = offset;
  64. // Color
  65. this._vertices[arrayOffset + 12] = sprite.color.r;
  66. this._vertices[arrayOffset + 13] = sprite.color.g;
  67. this._vertices[arrayOffset + 14] = sprite.color.b;
  68. this._vertices[arrayOffset + 15] = sprite.color.a;
  69. };
  70. SpriteManager.prototype.intersects = function (ray, camera, predicate, fastCheck) {
  71. var count = Math.min(this._capacity, this.sprites.length);
  72. var min = BABYLON.Vector3.Zero();
  73. var max = BABYLON.Vector3.Zero();
  74. var distance = Number.MAX_VALUE;
  75. var currentSprite;
  76. var cameraSpacePosition = BABYLON.Vector3.Zero();
  77. var cameraView = camera.getViewMatrix();
  78. for (var index = 0; index < count; index++) {
  79. var sprite = this.sprites[index];
  80. if (!sprite) {
  81. continue;
  82. }
  83. if (predicate) {
  84. if (!predicate(sprite)) {
  85. continue;
  86. }
  87. }
  88. else if (!sprite.isPickable) {
  89. continue;
  90. }
  91. BABYLON.Vector3.TransformCoordinatesToRef(sprite.position, cameraView, cameraSpacePosition);
  92. min.copyFromFloats(cameraSpacePosition.x - sprite.width / 2, cameraSpacePosition.y - sprite.height / 2, cameraSpacePosition.z);
  93. max.copyFromFloats(cameraSpacePosition.x + sprite.width / 2, cameraSpacePosition.y + sprite.height / 2, cameraSpacePosition.z);
  94. if (ray.intersectsBoxMinMax(min, max)) {
  95. var currentDistance = BABYLON.Vector3.Distance(cameraSpacePosition, ray.origin);
  96. if (distance > currentDistance) {
  97. distance = currentDistance;
  98. currentSprite = sprite;
  99. if (fastCheck) {
  100. break;
  101. }
  102. }
  103. }
  104. }
  105. if (currentSprite) {
  106. var result = new BABYLON.PickingInfo();
  107. result.hit = true;
  108. result.pickedSprite = currentSprite;
  109. result.distance = distance;
  110. return result;
  111. }
  112. return null;
  113. };
  114. SpriteManager.prototype.render = function () {
  115. // Check
  116. if (!this._effectBase.isReady() || !this._effectFog.isReady() || !this._spriteTexture || !this._spriteTexture.isReady())
  117. return;
  118. var engine = this._scene.getEngine();
  119. var baseSize = this._spriteTexture.getBaseSize();
  120. // Sprites
  121. var deltaTime = engine.getDeltaTime();
  122. var max = Math.min(this._capacity, this.sprites.length);
  123. var rowSize = baseSize.width / this.cellSize;
  124. var offset = 0;
  125. for (var index = 0; index < max; index++) {
  126. var sprite = this.sprites[index];
  127. if (!sprite) {
  128. continue;
  129. }
  130. sprite._animate(deltaTime);
  131. this._appendSpriteVertex(offset++, sprite, 0, 0, rowSize);
  132. this._appendSpriteVertex(offset++, sprite, 1, 0, rowSize);
  133. this._appendSpriteVertex(offset++, sprite, 1, 1, rowSize);
  134. this._appendSpriteVertex(offset++, sprite, 0, 1, rowSize);
  135. }
  136. engine.updateDynamicVertexBuffer(this._vertexBuffer, this._vertices);
  137. // Render
  138. var effect = this._effectBase;
  139. if (this._scene.fogEnabled && this._scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  140. effect = this._effectFog;
  141. }
  142. engine.enableEffect(effect);
  143. var viewMatrix = this._scene.getViewMatrix();
  144. effect.setTexture("diffuseSampler", this._spriteTexture);
  145. effect.setMatrix("view", viewMatrix);
  146. effect.setMatrix("projection", this._scene.getProjectionMatrix());
  147. effect.setFloat2("textureInfos", this.cellSize / baseSize.width, this.cellSize / baseSize.height);
  148. // Fog
  149. if (this._scene.fogEnabled && this._scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  150. effect.setFloat4("vFogInfos", this._scene.fogMode, this._scene.fogStart, this._scene.fogEnd, this._scene.fogDensity);
  151. effect.setColor3("vFogColor", this._scene.fogColor);
  152. }
  153. // VBOs
  154. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, effect);
  155. // Draw order
  156. engine.setDepthFunctionToLessOrEqual();
  157. effect.setBool("alphaTest", true);
  158. engine.setColorWrite(false);
  159. engine.draw(true, 0, max * 6);
  160. engine.setColorWrite(true);
  161. effect.setBool("alphaTest", false);
  162. engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
  163. engine.draw(true, 0, max * 6);
  164. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  165. };
  166. SpriteManager.prototype.dispose = function () {
  167. if (this._vertexBuffer) {
  168. this._scene.getEngine()._releaseBuffer(this._vertexBuffer);
  169. this._vertexBuffer = null;
  170. }
  171. if (this._indexBuffer) {
  172. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  173. this._indexBuffer = null;
  174. }
  175. if (this._spriteTexture) {
  176. this._spriteTexture.dispose();
  177. this._spriteTexture = null;
  178. }
  179. // Remove from scene
  180. var index = this._scene.spriteManagers.indexOf(this);
  181. this._scene.spriteManagers.splice(index, 1);
  182. // Callback
  183. if (this.onDispose) {
  184. this.onDispose();
  185. }
  186. };
  187. return SpriteManager;
  188. })();
  189. BABYLON.SpriteManager = SpriteManager;
  190. })(BABYLON || (BABYLON = {}));