babylon.sprite2d.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  7. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  8. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  9. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  10. return c > 3 && r && Object.defineProperty(target, key, r), r;
  11. };
  12. var BABYLON;
  13. (function (BABYLON) {
  14. var Sprite2DRenderCache = (function (_super) {
  15. __extends(Sprite2DRenderCache, _super);
  16. function Sprite2DRenderCache() {
  17. _super.apply(this, arguments);
  18. this.effectsReady = false;
  19. this.vb = null;
  20. this.ib = null;
  21. this.instancingAttributes = null;
  22. this.texture = null;
  23. this.effect = null;
  24. this.effectInstanced = null;
  25. }
  26. Sprite2DRenderCache.prototype.render = function (instanceInfo, context) {
  27. // Do nothing if the shader is still loading/preparing
  28. if (!this.effectsReady) {
  29. if ((this.effect && (!this.effect.isReady() || (this.effectInstanced && !this.effectInstanced.isReady())))) {
  30. return false;
  31. }
  32. this.effectsReady = true;
  33. }
  34. // Compute the offset locations of the attributes in the vertex shader that will be mapped to the instance buffer data
  35. var engine = instanceInfo.owner.owner.engine;
  36. var effect = context.useInstancing ? this.effectInstanced : this.effect;
  37. engine.enableEffect(effect);
  38. effect.setTexture("diffuseSampler", this.texture);
  39. engine.bindBuffersDirectly(this.vb, this.ib, [1], 4, effect);
  40. var cur = engine.getAlphaMode();
  41. if (context.renderMode !== BABYLON.Render2DContext.RenderModeOpaque) {
  42. engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
  43. }
  44. var pid = context.groupInfoPartData[0];
  45. if (context.useInstancing) {
  46. if (!this.instancingAttributes) {
  47. this.instancingAttributes = this.loadInstancingAttributes(Sprite2D.SPRITE2D_MAINPARTID, effect);
  48. }
  49. engine.updateAndBindInstancesBuffer(pid._partBuffer, null, this.instancingAttributes);
  50. engine.draw(true, 0, 6, pid._partData.usedElementCount);
  51. engine.unbindInstanceAttributes();
  52. }
  53. else {
  54. for (var i = context.partDataStartIndex; i < context.partDataEndIndex; i++) {
  55. this.setupUniforms(effect, 0, pid._partData, i);
  56. engine.draw(true, 0, 6);
  57. }
  58. }
  59. engine.setAlphaMode(cur);
  60. return true;
  61. };
  62. Sprite2DRenderCache.prototype.dispose = function () {
  63. if (!_super.prototype.dispose.call(this)) {
  64. return false;
  65. }
  66. if (this.vb) {
  67. this._engine._releaseBuffer(this.vb);
  68. this.vb = null;
  69. }
  70. if (this.ib) {
  71. this._engine._releaseBuffer(this.ib);
  72. this.ib = null;
  73. }
  74. if (this.texture) {
  75. this.texture.dispose();
  76. this.texture = null;
  77. }
  78. if (this.effect) {
  79. this._engine._releaseEffect(this.effect);
  80. this.effect = null;
  81. }
  82. if (this.effectInstanced) {
  83. this._engine._releaseEffect(this.effectInstanced);
  84. this.effectInstanced = null;
  85. }
  86. return true;
  87. };
  88. return Sprite2DRenderCache;
  89. }(BABYLON.ModelRenderCache));
  90. BABYLON.Sprite2DRenderCache = Sprite2DRenderCache;
  91. var Sprite2DInstanceData = (function (_super) {
  92. __extends(Sprite2DInstanceData, _super);
  93. function Sprite2DInstanceData(partId) {
  94. _super.call(this, partId, 1);
  95. }
  96. Object.defineProperty(Sprite2DInstanceData.prototype, "topLeftUV", {
  97. get: function () {
  98. return null;
  99. },
  100. enumerable: true,
  101. configurable: true
  102. });
  103. Object.defineProperty(Sprite2DInstanceData.prototype, "sizeUV", {
  104. get: function () {
  105. return null;
  106. },
  107. enumerable: true,
  108. configurable: true
  109. });
  110. Object.defineProperty(Sprite2DInstanceData.prototype, "textureSize", {
  111. get: function () {
  112. return null;
  113. },
  114. enumerable: true,
  115. configurable: true
  116. });
  117. Object.defineProperty(Sprite2DInstanceData.prototype, "properties", {
  118. // 3 floats being:
  119. // - x: frame number to display
  120. // - y: invertY setting
  121. // - z: alignToPixel setting
  122. get: function () {
  123. return null;
  124. },
  125. enumerable: true,
  126. configurable: true
  127. });
  128. __decorate([
  129. BABYLON.instanceData()
  130. ], Sprite2DInstanceData.prototype, "topLeftUV", null);
  131. __decorate([
  132. BABYLON.instanceData()
  133. ], Sprite2DInstanceData.prototype, "sizeUV", null);
  134. __decorate([
  135. BABYLON.instanceData()
  136. ], Sprite2DInstanceData.prototype, "textureSize", null);
  137. __decorate([
  138. BABYLON.instanceData()
  139. ], Sprite2DInstanceData.prototype, "properties", null);
  140. return Sprite2DInstanceData;
  141. }(BABYLON.InstanceDataBase));
  142. BABYLON.Sprite2DInstanceData = Sprite2DInstanceData;
  143. var Sprite2D = (function (_super) {
  144. __extends(Sprite2D, _super);
  145. /**
  146. * Create an 2D Sprite primitive
  147. * @param texture the texture that stores the sprite to render
  148. * @param settings a combination of settings, possible ones are
  149. * - parent: the parent primitive/canvas, must be specified if the primitive is not constructed as a child of another one (i.e. as part of the children array setting)
  150. * - children: an array of direct children
  151. * - id a text identifier, for information purpose
  152. * - position: the X & Y positions relative to its parent. Alternatively the x and y properties can be set. Default is [0;0]
  153. * - rotation: the initial rotation (in radian) of the primitive. default is 0
  154. * - scale: the initial scale of the primitive. default is 1
  155. * - origin: define the normalized origin point location, default [0.5;0.5]
  156. * - spriteSize: the size of the sprite (in pixels), if null the size of the given texture will be used, default is null.
  157. * - spriteLocation: the location (in pixels) in the texture of the top/left corner of the Sprite to display, default is null (0,0)
  158. * - invertY: if true the texture Y will be inverted, default is false.
  159. * - alignToPixel: if true the sprite's texels will be aligned to the rendering viewport pixels, ensuring the best rendering quality but slow animations won't be done as smooth as if you set false. If false a texel could lies between two pixels, being blended by the texture sampling mode you choose, the rendering result won't be as good, but very slow animation will be overall better looking. Default is true: content will be aligned.
  160. * - isVisible: true if the sprite must be visible, false for hidden. Default is true.
  161. * - marginTop: top margin, can be a number (will be pixels) or a string (see PrimitiveThickness.fromString)
  162. * - marginLeft: left margin, can be a number (will be pixels) or a string (see PrimitiveThickness.fromString)
  163. * - marginRight: right margin, can be a number (will be pixels) or a string (see PrimitiveThickness.fromString)
  164. * - marginBottom: bottom margin, can be a number (will be pixels) or a string (see PrimitiveThickness.fromString)
  165. * - margin: top, left, right and bottom margin formatted as a single string (see PrimitiveThickness.fromString)
  166. * - marginHAlignment: one value of the PrimitiveAlignment type's static properties
  167. * - marginVAlignment: one value of the PrimitiveAlignment type's static properties
  168. * - marginAlignment: a string defining the alignment, see PrimitiveAlignment.fromString
  169. * - paddingTop: top padding, can be a number (will be pixels) or a string (see PrimitiveThickness.fromString)
  170. * - paddingLeft: left padding, can be a number (will be pixels) or a string (see PrimitiveThickness.fromString)
  171. * - paddingRight: right padding, can be a number (will be pixels) or a string (see PrimitiveThickness.fromString)
  172. * - paddingBottom: bottom padding, can be a number (will be pixels) or a string (see PrimitiveThickness.fromString)
  173. * - padding: top, left, right and bottom padding formatted as a single string (see PrimitiveThickness.fromString)
  174. */
  175. function Sprite2D(texture, settings) {
  176. if (!settings) {
  177. settings = {};
  178. }
  179. _super.call(this, settings);
  180. this.texture = texture;
  181. this.texture.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  182. this.texture.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  183. this.size = settings.spriteSize;
  184. this.spriteLocation = settings.spriteLocation || new BABYLON.Vector2(0, 0);
  185. this.spriteFrame = 0;
  186. this.invertY = (settings.invertY == null) ? false : settings.invertY;
  187. this.alignToPixel = (settings.alignToPixel == null) ? true : settings.alignToPixel;
  188. this._isTransparent = true;
  189. if (settings.spriteSize == null) {
  190. var s = texture.getSize();
  191. this.size = new BABYLON.Size(s.width, s.height);
  192. }
  193. }
  194. Object.defineProperty(Sprite2D.prototype, "texture", {
  195. get: function () {
  196. return this._texture;
  197. },
  198. set: function (value) {
  199. this._texture = value;
  200. },
  201. enumerable: true,
  202. configurable: true
  203. });
  204. Object.defineProperty(Sprite2D.prototype, "actualSize", {
  205. get: function () {
  206. if (this._actualSize) {
  207. return this._actualSize;
  208. }
  209. return this.size;
  210. },
  211. set: function (value) {
  212. this._actualSize = value;
  213. },
  214. enumerable: true,
  215. configurable: true
  216. });
  217. Object.defineProperty(Sprite2D.prototype, "spriteLocation", {
  218. get: function () {
  219. return this._location;
  220. },
  221. set: function (value) {
  222. this._location = value;
  223. },
  224. enumerable: true,
  225. configurable: true
  226. });
  227. Object.defineProperty(Sprite2D.prototype, "spriteFrame", {
  228. get: function () {
  229. return this._spriteFrame;
  230. },
  231. set: function (value) {
  232. this._spriteFrame = value;
  233. },
  234. enumerable: true,
  235. configurable: true
  236. });
  237. Object.defineProperty(Sprite2D.prototype, "invertY", {
  238. get: function () {
  239. return this._invertY;
  240. },
  241. set: function (value) {
  242. this._invertY = value;
  243. },
  244. enumerable: true,
  245. configurable: true
  246. });
  247. Object.defineProperty(Sprite2D.prototype, "alignToPixel", {
  248. /**
  249. * Get/set if the sprite rendering should be aligned to the target rendering device pixel or not
  250. */
  251. get: function () {
  252. return this._alignToPixel;
  253. },
  254. set: function (value) {
  255. this._alignToPixel = value;
  256. },
  257. enumerable: true,
  258. configurable: true
  259. });
  260. Sprite2D.prototype.updateLevelBoundingInfo = function () {
  261. BABYLON.BoundingInfo2D.CreateFromSizeToRef(this.size, this._levelBoundingInfo);
  262. };
  263. /**
  264. * Get the animatable array (see http://doc.babylonjs.com/tutorials/Animations)
  265. */
  266. Sprite2D.prototype.getAnimatables = function () {
  267. var res = new Array();
  268. if (this.texture && this.texture.animations && this.texture.animations.length > 0) {
  269. res.push(this.texture);
  270. }
  271. return res;
  272. };
  273. Sprite2D.prototype.levelIntersect = function (intersectInfo) {
  274. // If we've made it so far it means the boundingInfo intersection test succeed, the Sprite2D is shaped the same, so we always return true
  275. return true;
  276. };
  277. Sprite2D._createCachedCanvasSprite = function (owner, texture, size, pos) {
  278. var sprite = new Sprite2D(texture, { parent: owner, id: "__cachedCanvasSprite__", position: BABYLON.Vector2.Zero(), origin: BABYLON.Vector2.Zero(), spriteSize: size, spriteLocation: pos, alignToPixel: true });
  279. return sprite;
  280. };
  281. Sprite2D.prototype.createModelRenderCache = function (modelKey) {
  282. var renderCache = new Sprite2DRenderCache(this.owner.engine, modelKey);
  283. return renderCache;
  284. };
  285. Sprite2D.prototype.setupModelRenderCache = function (modelRenderCache) {
  286. var renderCache = modelRenderCache;
  287. var engine = this.owner.engine;
  288. var vb = new Float32Array(4);
  289. for (var i = 0; i < 4; i++) {
  290. vb[i] = i;
  291. }
  292. renderCache.vb = engine.createVertexBuffer(vb);
  293. var ib = new Float32Array(6);
  294. ib[0] = 0;
  295. ib[1] = 2;
  296. ib[2] = 1;
  297. ib[3] = 0;
  298. ib[4] = 3;
  299. ib[5] = 2;
  300. renderCache.ib = engine.createIndexBuffer(ib);
  301. renderCache.texture = this.texture;
  302. // Get the instanced version of the effect, if the engine does not support it, null is return and we'll only draw on by one
  303. var ei = this.getDataPartEffectInfo(Sprite2D.SPRITE2D_MAINPARTID, ["index"], true);
  304. if (ei) {
  305. renderCache.effectInstanced = engine.createEffect("sprite2d", ei.attributes, ei.uniforms, ["diffuseSampler"], ei.defines, null);
  306. }
  307. ei = this.getDataPartEffectInfo(Sprite2D.SPRITE2D_MAINPARTID, ["index"], false);
  308. renderCache.effect = engine.createEffect("sprite2d", ei.attributes, ei.uniforms, ["diffuseSampler"], ei.defines, null);
  309. return renderCache;
  310. };
  311. Sprite2D.prototype.createInstanceDataParts = function () {
  312. return [new Sprite2DInstanceData(Sprite2D.SPRITE2D_MAINPARTID)];
  313. };
  314. Sprite2D.prototype.refreshInstanceDataPart = function (part) {
  315. if (!_super.prototype.refreshInstanceDataPart.call(this, part)) {
  316. return false;
  317. }
  318. if (part.id === Sprite2D.SPRITE2D_MAINPARTID) {
  319. var d = this._instanceDataParts[0];
  320. var ts = this.texture.getBaseSize();
  321. var sl = this.spriteLocation;
  322. var ss = this.actualSize;
  323. d.topLeftUV = new BABYLON.Vector2(sl.x / ts.width, sl.y / ts.height);
  324. var suv = new BABYLON.Vector2(ss.width / ts.width, ss.height / ts.height);
  325. d.sizeUV = suv;
  326. Sprite2D._prop.x = this.spriteFrame;
  327. Sprite2D._prop.y = this.invertY ? 1 : 0;
  328. Sprite2D._prop.z = this.alignToPixel ? 1 : 0;
  329. d.properties = Sprite2D._prop;
  330. d.textureSize = new BABYLON.Vector2(ts.width, ts.height);
  331. }
  332. return true;
  333. };
  334. Sprite2D.SPRITE2D_MAINPARTID = 1;
  335. Sprite2D._prop = BABYLON.Vector3.Zero();
  336. __decorate([
  337. BABYLON.modelLevelProperty(BABYLON.RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 1, function (pi) { return Sprite2D.textureProperty = pi; })
  338. ], Sprite2D.prototype, "texture", null);
  339. __decorate([
  340. BABYLON.instanceLevelProperty(BABYLON.RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 2, function (pi) { return Sprite2D.actualSizeProperty = pi; }, false, true)
  341. ], Sprite2D.prototype, "actualSize", null);
  342. __decorate([
  343. BABYLON.instanceLevelProperty(BABYLON.RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 3, function (pi) { return Sprite2D.spriteLocationProperty = pi; })
  344. ], Sprite2D.prototype, "spriteLocation", null);
  345. __decorate([
  346. BABYLON.instanceLevelProperty(BABYLON.RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 4, function (pi) { return Sprite2D.spriteFrameProperty = pi; })
  347. ], Sprite2D.prototype, "spriteFrame", null);
  348. __decorate([
  349. BABYLON.instanceLevelProperty(BABYLON.RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 5, function (pi) { return Sprite2D.invertYProperty = pi; })
  350. ], Sprite2D.prototype, "invertY", null);
  351. Sprite2D = __decorate([
  352. BABYLON.className("Sprite2D")
  353. ], Sprite2D);
  354. return Sprite2D;
  355. }(BABYLON.RenderablePrim2D));
  356. BABYLON.Sprite2D = Sprite2D;
  357. })(BABYLON || (BABYLON = {}));