babylon.sprite2d.ts 13 KB

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