babylon.sprite2d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. module BABYLON {
  2. export class Sprite2DRenderCache extends ModelRenderCache {
  3. vb: WebGLBuffer;
  4. ib: WebGLBuffer;
  5. instancingAttributes: InstancingAttributeInfo[];
  6. texture: Texture;
  7. effect: Effect;
  8. render(instanceInfo: GroupInstanceInfo, context: Render2DContext): boolean {
  9. // Do nothing if the shader is still loading/preparing
  10. if (!this.effect.isReady() || !this.texture.isReady()) {
  11. return false;
  12. }
  13. // Compute the offset locations of the attributes in the vertex shader that will be mapped to the instance buffer data
  14. var engine = instanceInfo._owner.owner.engine;
  15. engine.enableEffect(this.effect);
  16. this.effect.setTexture("diffuseSampler", this.texture);
  17. engine.bindBuffersDirectly(this.vb, this.ib, [1], 4, this.effect);
  18. var cur = engine.getAlphaMode();
  19. engine.setAlphaMode(Engine.ALPHA_COMBINE);
  20. let count = instanceInfo._instancesPartsData[0].usedElementCount;
  21. if (instanceInfo._owner.owner.supportInstancedArray) {
  22. if (!this.instancingAttributes) {
  23. this.instancingAttributes = this.loadInstancingAttributes(Sprite2D.SPRITE2D_MAINPARTID, this.effect);
  24. }
  25. engine.updateAndBindInstancesBuffer(instanceInfo._instancesPartsBuffer[0], null, this.instancingAttributes);
  26. engine.draw(true, 0, 6, count);
  27. engine.unbindInstanceAttributes();
  28. } else {
  29. for (let i = 0; i < count; i++) {
  30. this.setupUniforms(this.effect, 0, instanceInfo._instancesPartsData[0], i);
  31. engine.draw(true, 0, 6);
  32. }
  33. }
  34. engine.setAlphaMode(cur);
  35. return true;
  36. }
  37. public dispose(): boolean {
  38. if (!super.dispose()) {
  39. return false;
  40. }
  41. if (this.vb) {
  42. this._engine._releaseBuffer(this.vb);
  43. this.vb = null;
  44. }
  45. if (this.ib) {
  46. this._engine._releaseBuffer(this.ib);
  47. this.ib = null;
  48. }
  49. if (this.texture) {
  50. this.texture.dispose();
  51. this.texture = null;
  52. }
  53. if (this.effect) {
  54. this._engine._releaseEffect(this.effect);
  55. this.effect = null;
  56. }
  57. return true;
  58. }
  59. }
  60. export class Sprite2DInstanceData extends InstanceDataBase {
  61. constructor(partId: number) {
  62. super(partId, 1);
  63. }
  64. @instanceData()
  65. get topLeftUV(): Vector2 {
  66. return null;
  67. }
  68. @instanceData()
  69. get sizeUV(): Vector2 {
  70. return null;
  71. }
  72. @instanceData()
  73. get textureSize(): Vector2 {
  74. return null;
  75. }
  76. @instanceData()
  77. get frame(): number {
  78. return null;
  79. }
  80. @instanceData()
  81. get invertY(): number {
  82. return null;
  83. }
  84. }
  85. @className("Sprite2D")
  86. export class Sprite2D extends RenderablePrim2D {
  87. static SPRITE2D_MAINPARTID = 1;
  88. public static textureProperty: Prim2DPropInfo;
  89. public static spriteSizeProperty: Prim2DPropInfo;
  90. public static spriteLocationProperty: Prim2DPropInfo;
  91. public static spriteFrameProperty: Prim2DPropInfo;
  92. public static invertYProperty: Prim2DPropInfo;
  93. @modelLevelProperty(RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 1, pi => Sprite2D.textureProperty = pi)
  94. public get texture(): Texture {
  95. return this._texture;
  96. }
  97. public set texture(value: Texture) {
  98. this._texture = value;
  99. }
  100. public get actualSize(): Size {
  101. return this.spriteSize;
  102. }
  103. @instanceLevelProperty(RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 2, pi => Sprite2D.spriteSizeProperty = pi, false, true)
  104. public get spriteSize(): Size {
  105. return this._size;
  106. }
  107. public set spriteSize(value: Size) {
  108. this._size = value;
  109. }
  110. @instanceLevelProperty(RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 3, pi => Sprite2D.spriteLocationProperty = pi)
  111. public get spriteLocation(): Vector2 {
  112. return this._location;
  113. }
  114. public set spriteLocation(value: Vector2) {
  115. this._location = value;
  116. }
  117. @instanceLevelProperty(RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 4, pi => Sprite2D.spriteFrameProperty = pi)
  118. public get spriteFrame(): number {
  119. return this._spriteFrame;
  120. }
  121. public set spriteFrame(value: number) {
  122. this._spriteFrame = value;
  123. }
  124. @instanceLevelProperty(RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 5, pi => Sprite2D.invertYProperty = pi)
  125. public get invertY(): boolean {
  126. return this._invertY;
  127. }
  128. public set invertY(value: boolean) {
  129. this._invertY = value;
  130. }
  131. protected updateLevelBoundingInfo() {
  132. BoundingInfo2D.CreateFromSizeToRef(this.spriteSize, this._levelBoundingInfo, this.origin);
  133. }
  134. public getAnimatables(): IAnimatable[] {
  135. let res = new Array<IAnimatable>();
  136. if (this.texture && this.texture.animations && this.texture.animations.length > 0) {
  137. res.push(this.texture);
  138. }
  139. return res;
  140. }
  141. protected levelIntersect(intersectInfo: IntersectInfo2D): boolean {
  142. // 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
  143. return true;
  144. }
  145. protected setupSprite2D(owner: Canvas2D, parent: Prim2DBase, id: string, position: Vector2, origin: Vector2, texture: Texture, spriteSize: Size, spriteLocation: Vector2, invertY: boolean) {
  146. this.setupRenderablePrim2D(owner, parent, id, position, origin, true);
  147. this.texture = texture;
  148. this.texture.wrapU = Texture.CLAMP_ADDRESSMODE;
  149. this.texture.wrapV = Texture.CLAMP_ADDRESSMODE;
  150. this.spriteSize = spriteSize || null;
  151. this.spriteLocation = spriteLocation || new Vector2(0,0);
  152. this.spriteFrame = 0;
  153. this.invertY = invertY;
  154. this._isTransparent = true;
  155. if (!this.spriteSize) {
  156. var s = texture.getSize();
  157. this.spriteSize = new Size(s.width, s.height);
  158. }
  159. }
  160. /**
  161. * Create an 2D Sprite primitive
  162. * @param parent the parent primitive, must be a valid primitive (or the Canvas)
  163. * @param texture the texture that stores the sprite to render
  164. * options:
  165. * - id a text identifier, for information purpose
  166. * - x: the X position relative to its parent, default is 0
  167. * - y: the Y position relative to its parent, default is 0
  168. * - origin: define the normalized origin point location, default [0.5;0.5]
  169. * - spriteSize: the size of the sprite, if null the size of the given texture will be used, default is null.
  170. * - spriteLocation: the location in the texture of the top/left corner of the Sprite to display, default is null (0,0)
  171. * - invertY: if true the texture Y will be inverted, default is false.
  172. */
  173. public static Create(parent: Prim2DBase, texture: Texture, options: { id?: string, x?: number, y?: number, origin?: Vector2, spriteSize?: Size, spriteLocation?: Vector2, invertY?: boolean}): Sprite2D {
  174. Prim2DBase.CheckParent(parent);
  175. let sprite = new Sprite2D();
  176. sprite.setupSprite2D(parent.owner, parent, options && options.id || null, new Vector2(options && options.x || 0, options && options.y || 0), options && options.origin || null, texture, options && options.spriteSize || null, options && options.spriteLocation || null, options && options.invertY || false);
  177. return sprite;
  178. }
  179. static _createCachedCanvasSprite(owner: Canvas2D, texture: MapTexture, size: Size, pos: Vector2): Sprite2D {
  180. let sprite = new Sprite2D();
  181. sprite.setupSprite2D(owner, null, "__cachedCanvasSprite__", new Vector2(0, 0), null, texture, size, pos, false);
  182. return sprite;
  183. }
  184. protected createModelRenderCache(modelKey: string, isTransparent: boolean): ModelRenderCache {
  185. let renderCache = new Sprite2DRenderCache(this.owner.engine, modelKey, isTransparent);
  186. return renderCache;
  187. }
  188. protected setupModelRenderCache(modelRenderCache: ModelRenderCache) {
  189. let renderCache = <Sprite2DRenderCache>modelRenderCache;
  190. let engine = this.owner.engine;
  191. let vb = new Float32Array(4);
  192. for (let i = 0; i < 4; i++) {
  193. vb[i] = i;
  194. }
  195. renderCache.vb = engine.createVertexBuffer(vb);
  196. let ib = new Float32Array(6);
  197. ib[0] = 0;
  198. ib[1] = 2;
  199. ib[2] = 1;
  200. ib[3] = 0;
  201. ib[4] = 3;
  202. ib[5] = 2;
  203. renderCache.ib = engine.createIndexBuffer(ib);
  204. renderCache.texture = this.texture;
  205. var ei = this.getDataPartEffectInfo(Sprite2D.SPRITE2D_MAINPARTID, ["index"]);
  206. renderCache.effect = engine.createEffect({ vertex: "sprite2d", fragment: "sprite2d" }, ei.attributes, ei.uniforms, ["diffuseSampler"], ei.defines, null, e => {
  207. // renderCache.setupUniformsLocation(e, ei.uniforms, Sprite2D.SPRITE2D_MAINPARTID);
  208. });
  209. return renderCache;
  210. }
  211. protected createInstanceDataParts(): InstanceDataBase[] {
  212. return [new Sprite2DInstanceData(Sprite2D.SPRITE2D_MAINPARTID)];
  213. }
  214. protected refreshInstanceDataPart(part: InstanceDataBase): boolean {
  215. if (!super.refreshInstanceDataPart(part)) {
  216. return false;
  217. }
  218. if (part.id === Sprite2D.SPRITE2D_MAINPARTID) {
  219. let d = <Sprite2DInstanceData>this._instanceDataParts[0];
  220. let ts = this.texture.getBaseSize();
  221. let sl = this.spriteLocation;
  222. let ss = this.spriteSize;
  223. d.topLeftUV = new Vector2(sl.x / ts.width, sl.y / ts.height);
  224. let suv = new Vector2(ss.width / ts.width, ss.height / ts.height);
  225. d.sizeUV = suv;
  226. d.frame = this.spriteFrame;
  227. d.textureSize = new Vector2(ts.width, ts.height);
  228. d.invertY = this.invertY ? 1 : 0;
  229. }
  230. return true;
  231. }
  232. private _texture: Texture;
  233. private _size: Size;
  234. private _location: Vector2;
  235. private _spriteFrame: number;
  236. private _invertY: boolean;
  237. }
  238. }