babylon.sprite2d.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. module BABYLON {
  2. export class Sprite2DRenderCache extends ModelRenderCache {
  3. vb: WebGLBuffer;
  4. ib: WebGLBuffer;
  5. borderVB: WebGLBuffer;
  6. borderIB: WebGLBuffer;
  7. instancingAttributes: Array<InstancingAttributeInfo[]>;
  8. texture: Texture;
  9. effect: Effect;
  10. render(instanceInfo: GroupInstanceInfo, context: Render2DContext): boolean {
  11. // Do nothing if the shader is still loading/preparing
  12. if (!this.effect.isReady() || !this.texture.isReady()) {
  13. return false;
  14. }
  15. // Compute the offset locations of the attributes in the vertexshader that will be mapped to the instance buffer data
  16. if (!this.instancingAttributes) {
  17. this.instancingAttributes = this.loadInstancingAttributes(this.effect);
  18. }
  19. var engine = instanceInfo._owner.owner.engine;
  20. engine.enableEffect(this.effect);
  21. this.effect.setTexture("diffuseSampler", this.texture);
  22. engine.bindBuffers(this.vb, this.ib, [1], 4, this.effect);
  23. engine.updateAndBindInstancesBuffer(instanceInfo._instancesPartsBuffer[0], null, this.instancingAttributes[0]);
  24. var cur = engine.getAlphaMode();
  25. engine.setAlphaMode(Engine.ALPHA_COMBINE);
  26. engine.draw(true, 0, 6, instanceInfo._instancesPartsData[0].usedElementCount);
  27. engine.setAlphaMode(cur);
  28. engine.unBindInstancesBuffer(instanceInfo._instancesPartsBuffer[0], this.instancingAttributes[0]);
  29. return true;
  30. }
  31. }
  32. export class Sprite2DInstanceData extends InstanceDataBase {
  33. @instanceData()
  34. get topLeftUV(): Vector2 {
  35. return null;
  36. }
  37. @instanceData()
  38. get sizeUV(): Vector2 {
  39. return null;
  40. }
  41. @instanceData()
  42. get textureSize(): Vector2 {
  43. return null;
  44. }
  45. @instanceData()
  46. get frame(): number {
  47. return null;
  48. }
  49. @instanceData()
  50. get invertY(): number {
  51. return null;
  52. }
  53. }
  54. @className("Sprite2D")
  55. export class Sprite2D extends RenderablePrim2D {
  56. static SPRITE2D_MAINPARTID = 1;
  57. public static textureProperty: Prim2DPropInfo;
  58. public static spriteSizeProperty: Prim2DPropInfo;
  59. public static spriteLocationProperty: Prim2DPropInfo;
  60. public static spriteFrameProperty: Prim2DPropInfo;
  61. public static invertYProperty: Prim2DPropInfo;
  62. @modelLevelProperty(RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 1, pi => Sprite2D.textureProperty = pi)
  63. public get texture(): Texture {
  64. return this._texture;
  65. }
  66. public set texture(value: Texture) {
  67. this._texture = value;
  68. }
  69. @instanceLevelProperty(RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 2, pi => Sprite2D.spriteSizeProperty = pi, false, true)
  70. public get spriteSize(): Size {
  71. return this._size;
  72. }
  73. public set spriteSize(value: Size) {
  74. this._size = value;
  75. }
  76. @instanceLevelProperty(RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 3, pi => Sprite2D.spriteLocationProperty = pi)
  77. public get spriteLocation(): Vector2 {
  78. return this._location;
  79. }
  80. public set spriteLocation(value: Vector2) {
  81. this._location = value;
  82. }
  83. @instanceLevelProperty(RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 4, pi => Sprite2D.spriteFrameProperty = pi)
  84. public get spriteFrame(): number {
  85. return this._spriteFrame;
  86. }
  87. public set spriteFrame(value: number) {
  88. this._spriteFrame = value;
  89. }
  90. @instanceLevelProperty(RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 5, pi => Sprite2D.invertYProperty = pi)
  91. public get invertY(): boolean {
  92. return this._invertY;
  93. }
  94. public set invertY(value: boolean) {
  95. this._invertY = value;
  96. }
  97. protected updateLevelBoundingInfo() {
  98. this._levelBoundingInfo.radius = Math.sqrt(this.spriteSize.width * this.spriteSize.width + this.spriteSize.height * this.spriteSize.height);
  99. this._levelBoundingInfo.extent = this.spriteSize.clone();
  100. }
  101. protected setupSprite2D(owner: Canvas2D, parent: Prim2DBase, id: string, position: Vector2, texture: Texture, spriteSize: Size, spriteLocation: Vector2, invertY: boolean) {
  102. this.setupRenderablePrim2D(owner, parent, id, position, true, null, null);
  103. this.texture = texture;
  104. this.spriteSize = spriteSize;
  105. this.spriteLocation = spriteLocation;
  106. this.spriteFrame = 0;
  107. this.invertY = invertY;
  108. this._isTransparent = true;
  109. }
  110. public static Create(parent: Prim2DBase, id: string, x: number, y: number, texture: Texture, spriteSize: Size, spriteLocation: Vector2, invertY: boolean = false): Sprite2D {
  111. Prim2DBase.CheckParent(parent);
  112. let sprite = new Sprite2D();
  113. sprite.setupSprite2D(parent.owner, parent, id, new Vector2(x, y), texture, spriteSize, spriteLocation, invertY);
  114. return sprite;
  115. }
  116. protected createModelRenderCache(): ModelRenderCache {
  117. let renderCache = new Sprite2DRenderCache();
  118. return renderCache;
  119. }
  120. protected setupModelRenderCache(modelRenderCache: ModelRenderCache) {
  121. let renderCache = <Sprite2DRenderCache>modelRenderCache;
  122. let engine = this.owner.engine;
  123. let vb = new Float32Array(4);
  124. for (let i = 0; i < 4; i++) {
  125. vb[i] = i;
  126. }
  127. renderCache.vb = engine.createVertexBuffer(vb);
  128. let ib = new Float32Array(6);
  129. ib[0] = 0;
  130. ib[1] = 1;
  131. ib[2] = 2;
  132. ib[3] = 0;
  133. ib[4] = 2;
  134. ib[5] = 3;
  135. renderCache.ib = engine.createIndexBuffer(ib);
  136. renderCache.texture = this.texture;
  137. var ei = this.getDataPartEffectInfo(Sprite2D.SPRITE2D_MAINPARTID, ["index"]);
  138. renderCache.effect = engine.createEffect({ vertex: "sprite2d", fragment: "sprite2d" }, ei.attributes, [], ["diffuseSampler"], ei.defines);
  139. return renderCache;
  140. }
  141. protected createInstanceDataParts(): InstanceDataBase[] {
  142. return [new Sprite2DInstanceData(Sprite2D.SPRITE2D_MAINPARTID)];
  143. }
  144. protected refreshInstanceDataParts(part: InstanceDataBase): boolean {
  145. if (!super.refreshInstanceDataParts(part)) {
  146. return false;
  147. }
  148. if (part.id === Sprite2D.SPRITE2D_MAINPARTID) {
  149. let d = <Sprite2DInstanceData>this._instanceDataParts[0];
  150. let ts = this.texture.getSize();
  151. let sl = this.spriteLocation;
  152. let ss = this.spriteSize;
  153. d.topLeftUV = new Vector2(sl.x / ts.width, sl.y / ts.height);
  154. let suv = new Vector2(ss.width / ts.width, ss.height / ts.height);
  155. d.sizeUV = suv;
  156. d.frame = this.spriteFrame;
  157. d.textureSize = new Vector2(ts.width, ts.height);
  158. d.invertY = this.invertY ? 1 : 0;
  159. }
  160. return true;
  161. }
  162. private _texture: Texture;
  163. private _size: Size;
  164. private _location: Vector2;
  165. private _spriteFrame: number;
  166. private _invertY: boolean;
  167. }
  168. }