babylon.ellipse2d.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. module BABYLON {
  2. export class Ellipse2DRenderCache extends ModelRenderCache {
  3. effectsReady: boolean = false;
  4. fillVB: WebGLBuffer = null;
  5. fillIB: WebGLBuffer = null;
  6. fillIndicesCount: number = 0;
  7. instancingFillAttributes: InstancingAttributeInfo[] = null;
  8. effectFillInstanced: Effect = null;
  9. effectFill: Effect = null;
  10. borderVB: WebGLBuffer = null;
  11. borderIB: WebGLBuffer = null;
  12. borderIndicesCount: number = 0;
  13. instancingBorderAttributes: InstancingAttributeInfo[] = null;
  14. effectBorderInstanced: Effect = null;
  15. effectBorder: Effect = null;
  16. constructor(engine: Engine, modelKey: string) {
  17. super(engine, modelKey);
  18. }
  19. render(instanceInfo: GroupInstanceInfo, context: Render2DContext): boolean {
  20. // Do nothing if the shader is still loading/preparing
  21. if (!this.effectsReady) {
  22. if ((this.effectFill && (!this.effectFill.isReady() || (this.effectFillInstanced && !this.effectFillInstanced.isReady()))) ||
  23. (this.effectBorder && (!this.effectBorder.isReady() || (this.effectBorderInstanced && !this.effectBorderInstanced.isReady())))) {
  24. return false;
  25. }
  26. this.effectsReady = true;
  27. }
  28. let canvas = instanceInfo.owner.owner;
  29. var engine = canvas.engine;
  30. let depthFunction = 0;
  31. if (this.effectFill && this.effectBorder) {
  32. depthFunction = engine.getDepthFunction();
  33. engine.setDepthFunctionToLessOrEqual();
  34. }
  35. let curAlphaMode = engine.getAlphaMode();
  36. if (this.effectFill) {
  37. let partIndex = instanceInfo.partIndexFromId.get(Shape2D.SHAPE2D_FILLPARTID.toString());
  38. let pid = context.groupInfoPartData[partIndex];
  39. if (context.renderMode !== Render2DContext.RenderModeOpaque) {
  40. engine.setAlphaMode(Engine.ALPHA_COMBINE, true);
  41. }
  42. let effect = context.useInstancing ? this.effectFillInstanced : this.effectFill;
  43. engine.enableEffect(effect);
  44. engine.bindBuffersDirectly(this.fillVB, this.fillIB, [1], 4, effect);
  45. if (context.useInstancing) {
  46. if (!this.instancingFillAttributes) {
  47. this.instancingFillAttributes = this.loadInstancingAttributes(Shape2D.SHAPE2D_FILLPARTID, effect);
  48. }
  49. let glBuffer = context.instancedBuffers ? context.instancedBuffers[partIndex] : pid._partBuffer;
  50. let count = context.instancedBuffers ? context.instancesCount : pid._partData.usedElementCount;
  51. canvas._addDrawCallCount(1, context.renderMode);
  52. engine.updateAndBindInstancesBuffer(glBuffer, null, this.instancingFillAttributes);
  53. engine.draw(true, 0, this.fillIndicesCount, count);
  54. engine.unbindInstanceAttributes();
  55. } else {
  56. canvas._addDrawCallCount(context.partDataEndIndex - context.partDataStartIndex, context.renderMode);
  57. for (let i = context.partDataStartIndex; i < context.partDataEndIndex; i++) {
  58. this.setupUniforms(effect, partIndex, pid._partData, i);
  59. engine.draw(true, 0, this.fillIndicesCount);
  60. }
  61. }
  62. }
  63. if (this.effectBorder) {
  64. let partIndex = instanceInfo.partIndexFromId.get(Shape2D.SHAPE2D_BORDERPARTID.toString());
  65. let pid = context.groupInfoPartData[partIndex];
  66. if (context.renderMode !== Render2DContext.RenderModeOpaque) {
  67. engine.setAlphaMode(Engine.ALPHA_COMBINE, true);
  68. }
  69. let effect = context.useInstancing ? this.effectBorderInstanced : this.effectBorder;
  70. engine.enableEffect(effect);
  71. engine.bindBuffersDirectly(this.borderVB, this.borderIB, [1], 4, effect);
  72. if (context.useInstancing) {
  73. if (!this.instancingBorderAttributes) {
  74. this.instancingBorderAttributes = this.loadInstancingAttributes(Shape2D.SHAPE2D_BORDERPARTID, effect);
  75. }
  76. let glBuffer = context.instancedBuffers ? context.instancedBuffers[partIndex] : pid._partBuffer;
  77. let count = context.instancedBuffers ? context.instancesCount : pid._partData.usedElementCount;
  78. canvas._addDrawCallCount(1, context.renderMode);
  79. engine.updateAndBindInstancesBuffer(glBuffer, null, this.instancingBorderAttributes);
  80. engine.draw(true, 0, this.borderIndicesCount, count);
  81. engine.unbindInstanceAttributes();
  82. } else {
  83. canvas._addDrawCallCount(context.partDataEndIndex - context.partDataStartIndex, context.renderMode);
  84. for (let i = context.partDataStartIndex; i < context.partDataEndIndex; i++) {
  85. this.setupUniforms(effect, partIndex, pid._partData, i);
  86. engine.draw(true, 0, this.borderIndicesCount);
  87. }
  88. }
  89. }
  90. engine.setAlphaMode(curAlphaMode, true);
  91. if (this.effectFill && this.effectBorder) {
  92. engine.setDepthFunction(depthFunction);
  93. }
  94. return true;
  95. }
  96. public dispose(): boolean {
  97. if (!super.dispose()) {
  98. return false;
  99. }
  100. if (this.fillVB) {
  101. this._engine._releaseBuffer(this.fillVB);
  102. this.fillVB = null;
  103. }
  104. if (this.fillIB) {
  105. this._engine._releaseBuffer(this.fillIB);
  106. this.fillIB = null;
  107. }
  108. this.effectFill = null;
  109. this.effectFillInstanced = null;
  110. this.effectBorder = null;
  111. this.effectBorderInstanced = null;
  112. if (this.borderVB) {
  113. this._engine._releaseBuffer(this.borderVB);
  114. this.borderVB = null;
  115. }
  116. if (this.borderIB) {
  117. this._engine._releaseBuffer(this.borderIB);
  118. this.borderIB = null;
  119. }
  120. return true;
  121. }
  122. }
  123. export class Ellipse2DInstanceData extends Shape2DInstanceData {
  124. constructor(partId: number) {
  125. super(partId, 1);
  126. }
  127. @instanceData()
  128. get properties(): Vector3 {
  129. return null;
  130. }
  131. }
  132. @className("Ellipse2D")
  133. /**
  134. * Ellipse Primitive class
  135. */
  136. export class Ellipse2D extends Shape2D {
  137. public static acutalSizeProperty: Prim2DPropInfo;
  138. public static subdivisionsProperty: Prim2DPropInfo;
  139. @instanceLevelProperty(Shape2D.SHAPE2D_PROPCOUNT + 1, pi => Ellipse2D.acutalSizeProperty = pi, false, true)
  140. /**
  141. * Get/Set the size of the ellipse
  142. */
  143. public get actualSize(): Size {
  144. if (this._actualSize) {
  145. return this._actualSize;
  146. }
  147. return this.size;
  148. }
  149. public set actualSize(value: Size) {
  150. this._actualSize = value;
  151. }
  152. @modelLevelProperty(Shape2D.SHAPE2D_PROPCOUNT + 2, pi => Ellipse2D.subdivisionsProperty = pi)
  153. /**
  154. * Get/set the number of subdivisions used to draw the ellipsis. Default is 64.
  155. */
  156. public get subdivisions(): number {
  157. return this._subdivisions;
  158. }
  159. public set subdivisions(value: number) {
  160. this._subdivisions = value;
  161. }
  162. protected levelIntersect(intersectInfo: IntersectInfo2D): boolean {
  163. let w = this.size.width / 2;
  164. let h = this.size.height / 2;
  165. let x = intersectInfo._localPickPosition.x-w;
  166. let y = intersectInfo._localPickPosition.y-h;
  167. return ((x * x) / (w * w) + (y * y) / (h * h)) <= 1;
  168. }
  169. protected updateLevelBoundingInfo() {
  170. BoundingInfo2D.CreateFromSizeToRef(this.actualSize, this._levelBoundingInfo);
  171. }
  172. /**
  173. * Create an Ellipse 2D Shape primitive
  174. * @param settings a combination of settings, possible ones are
  175. * - parent: the parent primitive/canvas, must be specified if the primitive is not constructed as a child of another one (i.e. as part of the children array setting)
  176. * - children: an array of direct children
  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. * - rotation: the initial rotation (in radian) of the primitive. default is 0
  180. * - scale: the initial scale of the primitive. default is 1. You can alternatively use scaleX &| scaleY to apply non uniform scale
  181. * - opacity: set the overall opacity of the primitive, 1 to be opaque (default), less than 1 to be transparent.
  182. * - origin: define the normalized origin point location, default [0.5;0.5]
  183. * - size: the size of the group. Alternatively the width and height properties can be set. Default will be [10;10].
  184. * - subdivision: the number of subdivision to create the ellipse perimeter, default is 64.
  185. * - fill: the brush used to draw the fill content of the ellipse, you can set null to draw nothing (but you will have to set a border brush), default is a SolidColorBrush of plain white. can also be a string value (see Canvas2D.GetBrushFromString)
  186. * - border: the brush used to draw the border of the ellipse, you can set null to draw nothing (but you will have to set a fill brush), default is null. can be a string value (see Canvas2D.GetBrushFromString)
  187. * - borderThickness: the thickness of the drawn border, default is 1.
  188. * - isVisible: true if the group must be visible, false for hidden. Default is true.
  189. * - childrenFlatZOrder: if true all the children (direct and indirect) will share the same Z-Order. Use this when there's a lot of children which don't overlap. The drawing order IS NOT GUARANTED!
  190. * - marginTop: top margin, can be a number (will be pixels) or a string (see PrimitiveThickness.fromString)
  191. * - marginLeft: left margin, can be a number (will be pixels) or a string (see PrimitiveThickness.fromString)
  192. * - marginRight: right margin, can be a number (will be pixels) or a string (see PrimitiveThickness.fromString)
  193. * - marginBottom: bottom margin, can be a number (will be pixels) or a string (see PrimitiveThickness.fromString)
  194. * - margin: top, left, right and bottom margin formatted as a single string (see PrimitiveThickness.fromString)
  195. * - marginHAlignment: one value of the PrimitiveAlignment type's static properties
  196. * - marginVAlignment: one value of the PrimitiveAlignment type's static properties
  197. * - marginAlignment: a string defining the alignment, see PrimitiveAlignment.fromString
  198. * - paddingTop: top padding, can be a number (will be pixels) or a string (see PrimitiveThickness.fromString)
  199. * - paddingLeft: left padding, can be a number (will be pixels) or a string (see PrimitiveThickness.fromString)
  200. * - paddingRight: right padding, can be a number (will be pixels) or a string (see PrimitiveThickness.fromString)
  201. * - paddingBottom: bottom padding, can be a number (will be pixels) or a string (see PrimitiveThickness.fromString)
  202. * - padding: top, left, right and bottom padding formatted as a single string (see PrimitiveThickness.fromString)
  203. */
  204. constructor(settings?: {
  205. parent ?: Prim2DBase,
  206. children ?: Array<Prim2DBase>,
  207. id ?: string,
  208. position ?: Vector2,
  209. x ?: number,
  210. y ?: number,
  211. rotation ?: number,
  212. scale ?: number,
  213. scaleX ?: number,
  214. scaleY ?: number,
  215. opacity ?: number,
  216. origin ?: Vector2,
  217. size ?: Size,
  218. width ?: number,
  219. height ?: number,
  220. subdivisions ?: number,
  221. fill ?: IBrush2D | string,
  222. border ?: IBrush2D | string,
  223. borderThickness ?: number,
  224. isVisible ?: boolean,
  225. childrenFlatZOrder?: boolean,
  226. marginTop ?: number | string,
  227. marginLeft ?: number | string,
  228. marginRight ?: number | string,
  229. marginBottom ?: number | string,
  230. margin ?: number | string,
  231. marginHAlignment ?: number,
  232. marginVAlignment ?: number,
  233. marginAlignment ?: string,
  234. paddingTop ?: number | string,
  235. paddingLeft ?: number | string,
  236. paddingRight ?: number | string,
  237. paddingBottom ?: number | string,
  238. padding ?: string,
  239. }) {
  240. // Avoid checking every time if the object exists
  241. if (settings == null) {
  242. settings = {};
  243. }
  244. super(settings);
  245. if (settings.size != null) {
  246. this.size = settings.size;
  247. }
  248. else if (settings.width || settings.height) {
  249. let size = new Size(settings.width, settings.height);
  250. this.size = size;
  251. }
  252. let sub = (settings.subdivisions == null) ? 64 : settings.subdivisions;
  253. this.subdivisions = sub;
  254. }
  255. protected createModelRenderCache(modelKey: string): ModelRenderCache {
  256. let renderCache = new Ellipse2DRenderCache(this.owner.engine, modelKey);
  257. return renderCache;
  258. }
  259. protected setupModelRenderCache(modelRenderCache: ModelRenderCache) {
  260. let renderCache = <Ellipse2DRenderCache>modelRenderCache;
  261. let engine = this.owner.engine;
  262. // Need to create WebGL resources for fill part?
  263. if (this.fill) {
  264. let vbSize = this.subdivisions + 1;
  265. let vb = new Float32Array(vbSize);
  266. for (let i = 0; i < vbSize; i++) {
  267. vb[i] = i;
  268. }
  269. renderCache.fillVB = engine.createVertexBuffer(vb);
  270. let triCount = vbSize - 1;
  271. let ib = new Float32Array(triCount * 3);
  272. for (let i = 0; i < triCount; i++) {
  273. ib[i * 3 + 0] = 0;
  274. ib[i * 3 + 2] = i + 1;
  275. ib[i * 3 + 1] = i + 2;
  276. }
  277. ib[triCount * 3 - 2] = 1;
  278. renderCache.fillIB = engine.createIndexBuffer(ib);
  279. renderCache.fillIndicesCount = triCount * 3;
  280. // 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
  281. let ei = this.getDataPartEffectInfo(Shape2D.SHAPE2D_FILLPARTID, ["index"], true);
  282. if (ei) {
  283. renderCache.effectFillInstanced = engine.createEffect({ vertex: "ellipse2d", fragment: "ellipse2d" }, ei.attributes, ei.uniforms, [], ei.defines, null);
  284. }
  285. // Get the non instanced version
  286. ei = this.getDataPartEffectInfo(Shape2D.SHAPE2D_FILLPARTID, ["index"], false);
  287. renderCache.effectFill = engine.createEffect({ vertex: "ellipse2d", fragment: "ellipse2d" }, ei.attributes, ei.uniforms, [], ei.defines, null);
  288. }
  289. // Need to create WebGL resource for border part?
  290. if (this.border) {
  291. let vbSize = this.subdivisions * 2;
  292. let vb = new Float32Array(vbSize);
  293. for (let i = 0; i < vbSize; i++) {
  294. vb[i] = i;
  295. }
  296. renderCache.borderVB = engine.createVertexBuffer(vb);
  297. let triCount = vbSize;
  298. let rs = triCount / 2;
  299. let ib = new Float32Array(triCount * 3);
  300. for (let i = 0; i < rs; i++) {
  301. let r0 = i;
  302. let r1 = (i + 1) % rs;
  303. ib[i * 6 + 0] = rs + r1;
  304. ib[i * 6 + 1] = rs + r0;
  305. ib[i * 6 + 2] = r0;
  306. ib[i * 6 + 3] = r1;
  307. ib[i * 6 + 4] = rs + r1;
  308. ib[i * 6 + 5] = r0;
  309. }
  310. renderCache.borderIB = engine.createIndexBuffer(ib);
  311. renderCache.borderIndicesCount = (triCount* 3);
  312. // 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
  313. let ei = this.getDataPartEffectInfo(Shape2D.SHAPE2D_BORDERPARTID, ["index"], true);
  314. if (ei) {
  315. renderCache.effectBorderInstanced = engine.createEffect("ellipse2d", ei.attributes, ei.uniforms, [], ei.defines, null);
  316. }
  317. // Get the non instanced version
  318. ei = this.getDataPartEffectInfo(Shape2D.SHAPE2D_BORDERPARTID, ["index"], false);
  319. renderCache.effectBorder = engine.createEffect("ellipse2d", ei.attributes, ei.uniforms, [], ei.defines, null);
  320. }
  321. return renderCache;
  322. }
  323. protected createInstanceDataParts(): InstanceDataBase[] {
  324. var res = new Array<InstanceDataBase>();
  325. if (this.border) {
  326. res.push(new Ellipse2DInstanceData(Shape2D.SHAPE2D_BORDERPARTID));
  327. }
  328. if (this.fill) {
  329. res.push(new Ellipse2DInstanceData(Shape2D.SHAPE2D_FILLPARTID));
  330. }
  331. return res;
  332. }
  333. protected refreshInstanceDataPart(part: InstanceDataBase): boolean {
  334. if (!super.refreshInstanceDataPart(part)) {
  335. return false;
  336. }
  337. if (part.id === Shape2D.SHAPE2D_BORDERPARTID) {
  338. let d = <Ellipse2DInstanceData>part;
  339. let size = this.actualSize;
  340. d.properties = new Vector3(size.width, size.height, this.subdivisions);
  341. }
  342. else if (part.id === Shape2D.SHAPE2D_FILLPARTID) {
  343. let d = <Ellipse2DInstanceData>part;
  344. let size = this.actualSize;
  345. d.properties = new Vector3(size.width, size.height, this.subdivisions);
  346. }
  347. return true;
  348. }
  349. private _subdivisions: number;
  350. }
  351. }