babylon.shape2d.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. module BABYLON {
  2. @className("Shape2D")
  3. /**
  4. * The abstract class for parametric shape based Primitives types.
  5. * Shape2D based primitives are composed of two parts: fill and border, both are optional but at least one must be specified.
  6. * The fill part is the primitive 'body', the border is a border around this body. The border has a thickness that can be changed.
  7. */
  8. export abstract class Shape2D extends RenderablePrim2D {
  9. static SHAPE2D_BORDERPARTID = 1;
  10. static SHAPE2D_FILLPARTID = 2;
  11. static SHAPE2D_CATEGORY_BORDER = "Border";
  12. static SHAPE2D_CATEGORY_BORDERSOLID = "BorderSolid";
  13. static SHAPE2D_CATEGORY_BORDERGRADIENT = "BorderGradient";
  14. static SHAPE2D_CATEGORY_FILLSOLID = "FillSolid";
  15. static SHAPE2D_CATEGORY_FILLGRADIENT = "FillGradient";
  16. static SHAPE2D_PROPCOUNT: number = RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 5;
  17. public static borderProperty: Prim2DPropInfo;
  18. public static fillProperty: Prim2DPropInfo;
  19. public static borderThicknessProperty: Prim2DPropInfo;
  20. @modelLevelProperty(RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 1, pi => Shape2D.borderProperty = pi, true)
  21. /**
  22. * Get/set the brush to render the Border part of the Primitive
  23. */
  24. public get border(): IBrush2D {
  25. return this._border;
  26. }
  27. public set border(value: IBrush2D) {
  28. this._border = value;
  29. this._updateTransparencyStatus();
  30. }
  31. /**
  32. * Get/set the brush to render the Fill part of the Primitive
  33. */
  34. @modelLevelProperty(RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 2, pi => Shape2D.fillProperty = pi, true)
  35. public get fill(): IBrush2D {
  36. return this._fill;
  37. }
  38. public set fill(value: IBrush2D) {
  39. this._fill = value;
  40. this._updateTransparencyStatus();
  41. }
  42. @instanceLevelProperty(RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 3, pi => Shape2D.borderThicknessProperty = pi)
  43. /**
  44. * Get/set the thickness of the border part.
  45. */
  46. public get borderThickness(): number {
  47. return this._borderThickness;
  48. }
  49. public set borderThickness(value: number) {
  50. this._borderThickness = value;
  51. }
  52. constructor(settings?: {
  53. fill ?: IBrush2D | string,
  54. border ?: IBrush2D | string,
  55. borderThickness?: number,
  56. }) {
  57. super(settings);
  58. if (!settings) {
  59. settings = {};
  60. }
  61. let borderBrush: IBrush2D = null;
  62. if (settings.border) {
  63. if (typeof (settings.border) === "string") {
  64. borderBrush = Canvas2D.GetBrushFromString(<string>settings.border);
  65. } else {
  66. borderBrush = <IBrush2D>settings.border;
  67. }
  68. }
  69. let fillBrush: IBrush2D = null;
  70. if (settings.fill) {
  71. if (typeof (settings.fill) === "string") {
  72. fillBrush = Canvas2D.GetBrushFromString(<string>settings.fill);
  73. } else {
  74. fillBrush = <IBrush2D>settings.fill;
  75. }
  76. }
  77. this._isTransparent = false;
  78. this._oldTransparent = false;
  79. this.border = borderBrush;
  80. this.fill = fillBrush;
  81. this._updateTransparencyStatus();
  82. this.borderThickness = settings.borderThickness;
  83. }
  84. protected getUsedShaderCategories(dataPart: InstanceDataBase): string[] {
  85. var cat = super.getUsedShaderCategories(dataPart);
  86. // Fill Part
  87. if (dataPart.id === Shape2D.SHAPE2D_FILLPARTID) {
  88. let fill = this.fill;
  89. if (fill instanceof SolidColorBrush2D) {
  90. cat.push(Shape2D.SHAPE2D_CATEGORY_FILLSOLID);
  91. }
  92. if (fill instanceof GradientColorBrush2D) {
  93. cat.push(Shape2D.SHAPE2D_CATEGORY_FILLGRADIENT);
  94. }
  95. }
  96. // Border Part
  97. if (dataPart.id === Shape2D.SHAPE2D_BORDERPARTID) {
  98. cat.push(Shape2D.SHAPE2D_CATEGORY_BORDER);
  99. let border = this.border;
  100. if (border instanceof SolidColorBrush2D) {
  101. cat.push(Shape2D.SHAPE2D_CATEGORY_BORDERSOLID);
  102. }
  103. if (border instanceof GradientColorBrush2D) {
  104. cat.push(Shape2D.SHAPE2D_CATEGORY_BORDERGRADIENT);
  105. }
  106. }
  107. return cat;
  108. }
  109. protected applyActualScaleOnTransform(): boolean {
  110. return false;
  111. }
  112. protected refreshInstanceDataPart(part: InstanceDataBase): boolean {
  113. if (!super.refreshInstanceDataPart(part)) {
  114. return false;
  115. }
  116. // Fill Part
  117. if (part.id === Shape2D.SHAPE2D_FILLPARTID) {
  118. let d = <Shape2DInstanceData>part;
  119. if (this.fill) {
  120. let fill = this.fill;
  121. if (fill instanceof SolidColorBrush2D) {
  122. d.fillSolidColor = fill.color;
  123. } else if (fill instanceof GradientColorBrush2D) {
  124. d.fillGradientColor1 = fill.color1;
  125. d.fillGradientColor2 = fill.color2;
  126. var t = Matrix.Compose(new Vector3(fill.scale, fill.scale, fill.scale), Quaternion.RotationAxis(new Vector3(0, 0, 1), fill.rotation), new Vector3(fill.translation.x, fill.translation.y, 0));
  127. let ty = new Vector4(t.m[1], t.m[5], t.m[9], t.m[13]);
  128. d.fillGradientTY = ty;
  129. }
  130. }
  131. }
  132. else if (part.id === Shape2D.SHAPE2D_BORDERPARTID) {
  133. let d = <Shape2DInstanceData>part;
  134. if (this.border) {
  135. d.borderThickness = this.borderThickness;
  136. let border = this.border;
  137. if (border instanceof SolidColorBrush2D) {
  138. d.borderSolidColor = border.color;
  139. } else if (border instanceof GradientColorBrush2D) {
  140. d.borderGradientColor1 = border.color1;
  141. d.borderGradientColor2 = border.color2;
  142. var t = Matrix.Compose(new Vector3(border.scale, border.scale, border.scale), Quaternion.RotationAxis(new Vector3(0, 0, 1), border.rotation), new Vector3(border.translation.x, border.translation.y, 0));
  143. let ty = new Vector4(t.m[1], t.m[5], t.m[9], t.m[13]);
  144. d.borderGradientTY = ty;
  145. }
  146. }
  147. }
  148. return true;
  149. }
  150. private _updateTransparencyStatus() {
  151. this._isTransparent = (this._border && this._border.isTransparent()) || (this._fill && this._fill.isTransparent()) || (this.actualOpacity < 1);
  152. if (this._isTransparent !== this._oldTransparent) {
  153. this._oldTransparent = this._isTransparent;
  154. this._updateRenderMode();
  155. }
  156. }
  157. protected _mustUpdateInstance(): boolean {
  158. let res = this._oldTransparent !== this._isTransparent;
  159. if (res) {
  160. this._updateRenderMode();
  161. this._oldTransparent = this._isTransparent;
  162. }
  163. return res;
  164. }
  165. protected _isPrimTransparent(): boolean {
  166. return this._isTransparent;
  167. }
  168. private _oldTransparent: boolean;
  169. private _isTransparent: boolean;
  170. private _border: IBrush2D;
  171. private _borderThickness: number;
  172. private _fill: IBrush2D;
  173. }
  174. export class Shape2DInstanceData extends InstanceDataBase {
  175. // FILL ATTRIBUTES
  176. @instanceData(Shape2D.SHAPE2D_CATEGORY_FILLSOLID)
  177. get fillSolidColor(): Color4 {
  178. return null;
  179. }
  180. set fillSolidColor(value: Color4) {
  181. }
  182. @instanceData(Shape2D.SHAPE2D_CATEGORY_FILLGRADIENT)
  183. get fillGradientColor1(): Color4 {
  184. return null;
  185. }
  186. set fillGradientColor1(value: Color4) {
  187. }
  188. @instanceData(Shape2D.SHAPE2D_CATEGORY_FILLGRADIENT)
  189. get fillGradientColor2(): Color4 {
  190. return null;
  191. }
  192. set fillGradientColor2(value: Color4) {
  193. }
  194. @instanceData(Shape2D.SHAPE2D_CATEGORY_FILLGRADIENT)
  195. get fillGradientTY(): Vector4 {
  196. return null;
  197. }
  198. set fillGradientTY(value: Vector4) {
  199. }
  200. // BORDER ATTRIBUTES
  201. @instanceData(Shape2D.SHAPE2D_CATEGORY_BORDER)
  202. get borderThickness(): number {
  203. return null;
  204. }
  205. set borderThickness(value: number) {
  206. }
  207. @instanceData(Shape2D.SHAPE2D_CATEGORY_BORDERSOLID)
  208. get borderSolidColor(): Color4 {
  209. return null;
  210. }
  211. set borderSolidColor(value: Color4) {
  212. }
  213. @instanceData(Shape2D.SHAPE2D_CATEGORY_BORDERGRADIENT)
  214. get borderGradientColor1(): Color4 {
  215. return null;
  216. }
  217. set borderGradientColor1(value: Color4) {
  218. }
  219. @instanceData(Shape2D.SHAPE2D_CATEGORY_BORDERGRADIENT)
  220. get borderGradientColor2(): Color4 {
  221. return null;
  222. }
  223. set borderGradientColor2(value: Color4) {
  224. }
  225. @instanceData(Shape2D.SHAPE2D_CATEGORY_BORDERGRADIENT)
  226. get borderGradientTY(): Vector4 {
  227. return null;
  228. }
  229. set borderGradientTY(value: Vector4) {
  230. }
  231. }
  232. }