babylon.sprite2d.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.bindBuffers(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.unBindInstancesBuffer(instanceInfo._instancesPartsBuffer[0], this.instancingAttributes);
  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 setupSprite2D(owner: Canvas2D, parent: Prim2DBase, id: string, position: Vector2, texture: Texture, spriteSize: Size, spriteLocation: Vector2, invertY: boolean) {
  142. this.setupRenderablePrim2D(owner, parent, id, position, true);
  143. this.texture = texture;
  144. this.texture.wrapU = Texture.CLAMP_ADDRESSMODE;
  145. this.texture.wrapV = Texture.CLAMP_ADDRESSMODE;
  146. this.spriteSize = spriteSize;
  147. this.spriteLocation = spriteLocation;
  148. this.spriteFrame = 0;
  149. this.invertY = invertY;
  150. this._isTransparent = true;
  151. }
  152. public static Create(parent: Prim2DBase, id: string, x: number, y: number, texture: Texture, spriteSize: Size, spriteLocation: Vector2, invertY: boolean = false): Sprite2D {
  153. Prim2DBase.CheckParent(parent);
  154. let sprite = new Sprite2D();
  155. sprite.setupSprite2D(parent.owner, parent, id, new Vector2(x, y), texture, spriteSize, spriteLocation, invertY);
  156. return sprite;
  157. }
  158. static _createCachedCanvasSprite(owner: Canvas2D, texture: MapTexture, size: Size, pos: Vector2): Sprite2D {
  159. let sprite = new Sprite2D();
  160. sprite.setupSprite2D(owner, null, "__cachedCanvasSprite__", new Vector2(0, 0), texture, size, pos, false);
  161. return sprite;
  162. }
  163. protected createModelRenderCache(modelKey: string, isTransparent: boolean): ModelRenderCache {
  164. let renderCache = new Sprite2DRenderCache(this.owner.engine, modelKey, isTransparent);
  165. return renderCache;
  166. }
  167. protected setupModelRenderCache(modelRenderCache: ModelRenderCache) {
  168. let renderCache = <Sprite2DRenderCache>modelRenderCache;
  169. let engine = this.owner.engine;
  170. let vb = new Float32Array(4);
  171. for (let i = 0; i < 4; i++) {
  172. vb[i] = i;
  173. }
  174. renderCache.vb = engine.createVertexBuffer(vb);
  175. let ib = new Float32Array(6);
  176. ib[0] = 0;
  177. ib[1] = 2;
  178. ib[2] = 1;
  179. ib[3] = 0;
  180. ib[4] = 3;
  181. ib[5] = 2;
  182. renderCache.ib = engine.createIndexBuffer(ib);
  183. renderCache.texture = this.texture;
  184. var ei = this.getDataPartEffectInfo(Sprite2D.SPRITE2D_MAINPARTID, ["index"]);
  185. renderCache.effect = engine.createEffect({ vertex: "sprite2d", fragment: "sprite2d" }, ei.attributes, ei.uniforms, ["diffuseSampler"], ei.defines, null, e => {
  186. // renderCache.setupUniformsLocation(e, ei.uniforms, Sprite2D.SPRITE2D_MAINPARTID);
  187. });
  188. return renderCache;
  189. }
  190. protected createInstanceDataParts(): InstanceDataBase[] {
  191. return [new Sprite2DInstanceData(Sprite2D.SPRITE2D_MAINPARTID)];
  192. }
  193. protected refreshInstanceDataPart(part: InstanceDataBase): boolean {
  194. if (!super.refreshInstanceDataPart(part)) {
  195. return false;
  196. }
  197. if (part.id === Sprite2D.SPRITE2D_MAINPARTID) {
  198. let d = <Sprite2DInstanceData>this._instanceDataParts[0];
  199. let ts = this.texture.getSize();
  200. let sl = this.spriteLocation;
  201. let ss = this.spriteSize;
  202. d.topLeftUV = new Vector2(sl.x / ts.width, sl.y / ts.height);
  203. let suv = new Vector2(ss.width / ts.width, ss.height / ts.height);
  204. d.sizeUV = suv;
  205. d.frame = this.spriteFrame;
  206. d.textureSize = new Vector2(ts.width, ts.height);
  207. d.invertY = this.invertY ? 1 : 0;
  208. }
  209. return true;
  210. }
  211. private _texture: Texture;
  212. private _size: Size;
  213. private _location: Vector2;
  214. private _spriteFrame: number;
  215. private _invertY: boolean;
  216. }
  217. }