babylon.spriteManager.js 9.8 KB

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