| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- module BABYLON {
- export class Rectangle2DRenderCache extends ModelRenderCache {
- effectsReady: boolean = false;
- fillVB: WebGLBuffer = null;
- fillIB: WebGLBuffer = null;
- fillIndicesCount: number = 0;
- instancingFillAttributes: InstancingAttributeInfo[] = null;
- effectFill: Effect = null;
- effectFillInstanced: Effect = null;
- borderVB: WebGLBuffer = null;
- borderIB: WebGLBuffer = null;
- borderIndicesCount: number = 0;
- instancingBorderAttributes: InstancingAttributeInfo[] = null;
- effectBorder: Effect = null;
- effectBorderInstanced: Effect = null;
- constructor(engine: Engine, modelKey: string) {
- super(engine, modelKey);
- }
- render(instanceInfo: GroupInstanceInfo, context: Render2DContext): boolean {
- // Do nothing if the shader is still loading/preparing
- if (!this.effectsReady) {
- if ((this.effectFill && (!this.effectFill.isReady() || (this.effectFillInstanced && !this.effectFillInstanced.isReady()))) ||
- (this.effectBorder && (!this.effectBorder.isReady() || (this.effectBorderInstanced && !this.effectBorderInstanced.isReady())))) {
- return false;
- }
- this.effectsReady = true;
- }
- var engine = instanceInfo.owner.owner.engine;
- let depthFunction = 0;
- if (this.effectFill && this.effectBorder) {
- depthFunction = engine.getDepthFunction();
- engine.setDepthFunctionToLessOrEqual();
- }
- var curAlphaMode = engine.getAlphaMode();
- if (this.effectFill) {
- let partIndex = instanceInfo.partIndexFromId.get(Shape2D.SHAPE2D_FILLPARTID.toString());
- let pid = context.groupInfoPartData[partIndex];
- if (context.renderMode !== Render2DContext.RenderModeOpaque) {
- engine.setAlphaMode(Engine.ALPHA_COMBINE);
- }
- let effect = context.useInstancing ? this.effectFillInstanced : this.effectFill;
- engine.enableEffect(effect);
- engine.bindBuffersDirectly(this.fillVB, this.fillIB, [1], 4, effect);
- if (context.useInstancing) {
- if (!this.instancingFillAttributes) {
- this.instancingFillAttributes = this.loadInstancingAttributes(Shape2D.SHAPE2D_FILLPARTID, effect);
- }
- engine.updateAndBindInstancesBuffer(pid._partBuffer, null, this.instancingFillAttributes);
- engine.draw(true, 0, this.fillIndicesCount, pid._partData.usedElementCount);
- engine.unbindInstanceAttributes();
- } else {
- for (let i = context.partDataStartIndex; i < context.partDataEndIndex; i++) {
- this.setupUniforms(effect, partIndex, pid._partData, i);
- engine.draw(true, 0, this.fillIndicesCount);
- }
- }
- }
- if (this.effectBorder) {
- let partIndex = instanceInfo.partIndexFromId.get(Shape2D.SHAPE2D_BORDERPARTID.toString());
- let pid = context.groupInfoPartData[partIndex];
- if (context.renderMode !== Render2DContext.RenderModeOpaque) {
- engine.setAlphaMode(Engine.ALPHA_COMBINE);
- }
- let effect = context.useInstancing ? this.effectBorderInstanced : this.effectBorder;
- engine.enableEffect(effect);
- engine.bindBuffersDirectly(this.borderVB, this.borderIB, [1], 4, effect);
- if (context.useInstancing) {
- if (!this.instancingBorderAttributes) {
- this.instancingBorderAttributes = this.loadInstancingAttributes(Shape2D.SHAPE2D_BORDERPARTID, effect);
- }
- engine.updateAndBindInstancesBuffer(pid._partBuffer, null, this.instancingBorderAttributes);
- engine.draw(true, 0, this.borderIndicesCount, pid._partData.usedElementCount);
- engine.unbindInstanceAttributes();
- } else {
- for (let i = context.partDataStartIndex; i < context.partDataEndIndex; i++) {
- this.setupUniforms(effect, partIndex, pid._partData, i);
- engine.draw(true, 0, this.borderIndicesCount);
- }
- }
- }
- engine.setAlphaMode(curAlphaMode);
- if (this.effectFill && this.effectBorder) {
- engine.setDepthFunction(depthFunction);
- }
- return true;
- }
- public dispose(): boolean {
- if (!super.dispose()) {
- return false;
- }
- if (this.fillVB) {
- this._engine._releaseBuffer(this.fillVB);
- this.fillVB = null;
- }
- if (this.fillIB) {
- this._engine._releaseBuffer(this.fillIB);
- this.fillIB = null;
- }
- if (this.effectFill) {
- this._engine._releaseEffect(this.effectFill);
- this.effectFill = null;
- }
- if (this.effectFillInstanced) {
- this._engine._releaseEffect(this.effectFillInstanced);
- this.effectFillInstanced = null;
- }
- if (this.borderVB) {
- this._engine._releaseBuffer(this.borderVB);
- this.borderVB = null;
- }
- if (this.borderIB) {
- this._engine._releaseBuffer(this.borderIB);
- this.borderIB = null;
- }
- if (this.effectBorder) {
- this._engine._releaseEffect(this.effectBorder);
- this.effectBorder = null;
- }
- if (this.effectBorderInstanced) {
- this._engine._releaseEffect(this.effectBorderInstanced);
- this.effectBorderInstanced = null;
- }
- return true;
- }
- }
- export class Rectangle2DInstanceData extends Shape2DInstanceData {
- constructor(partId: number) {
- super(partId, 1);
- }
- @instanceData()
- get properties(): Vector3 {
- return null;
- }
- }
- @className("Rectangle2D")
- export class Rectangle2D extends Shape2D {
- public static actualSizeProperty: Prim2DPropInfo;
- public static notRoundedProperty: Prim2DPropInfo;
- public static roundRadiusProperty: Prim2DPropInfo;
- @instanceLevelProperty(Shape2D.SHAPE2D_PROPCOUNT + 1, pi => Rectangle2D.actualSizeProperty = pi, false, true)
- public get actualSize(): Size {
- if (this._actualSize) {
- return this._actualSize;
- }
- return this.size;
- }
- public set actualSize(value: Size) {
- this._actualSize = value;
- }
- @modelLevelProperty(Shape2D.SHAPE2D_PROPCOUNT + 2, pi => Rectangle2D.notRoundedProperty = pi)
- public get notRounded(): boolean {
- return this._notRounded;
- }
- public set notRounded(value: boolean) {
- this._notRounded = value;
- }
- @instanceLevelProperty(Shape2D.SHAPE2D_PROPCOUNT + 3, pi => Rectangle2D.roundRadiusProperty = pi)
- public get roundRadius(): number {
- return this._roundRadius;
- }
- public set roundRadius(value: number) {
- this._roundRadius = value;
- this.notRounded = value === 0;
- this._positioningDirty();
- }
- private static _i0 = Vector2.Zero();
- private static _i1 = Vector2.Zero();
- private static _i2 = Vector2.Zero();
- protected levelIntersect(intersectInfo: IntersectInfo2D): boolean {
- // If we got there it mean the boundingInfo intersection succeed, if the rectangle has not roundRadius, it means it succeed!
- if (this.notRounded) {
- return true;
- }
- // If we got so far it means the bounding box at least passed, so we know it's inside the bounding rectangle, but it can be outside the roundedRectangle.
- // The easiest way is to check if the point is inside on of the four corners area (a little square of roundRadius size at the four corners)
- // If it's the case for one, check if the mouse is located in the quarter that we care about (the one who is visible) then finally make a distance check with the roundRadius radius to see if it's inside the circle quarter or outside.
- // First let remove the origin out the equation, to have the rectangle with an origin at bottom/left
- let size = this.size;
- Rectangle2D._i0.x = intersectInfo._localPickPosition.x;
- Rectangle2D._i0.y = intersectInfo._localPickPosition.y;
- let rr = this.roundRadius;
- let rrs = rr * rr;
- // Check if the point is in the bottom/left quarter area
- Rectangle2D._i1.x = rr;
- Rectangle2D._i1.y = rr;
- if (Rectangle2D._i0.x <= Rectangle2D._i1.x && Rectangle2D._i0.y <= Rectangle2D._i1.y) {
- // Compute the intersection point in the quarter local space
- Rectangle2D._i2.x = Rectangle2D._i0.x - Rectangle2D._i1.x;
- Rectangle2D._i2.y = Rectangle2D._i0.y - Rectangle2D._i1.y;
- // It's a hit if the squared distance is less/equal to the squared radius of the round circle
- return Rectangle2D._i2.lengthSquared() <= rrs;
- }
- // Check if the point is in the top/left quarter area
- Rectangle2D._i1.x = rr;
- Rectangle2D._i1.y = size.height - rr;
- if (Rectangle2D._i0.x <= Rectangle2D._i1.x && Rectangle2D._i0.y >= Rectangle2D._i1.y) {
- // Compute the intersection point in the quarter local space
- Rectangle2D._i2.x = Rectangle2D._i0.x - Rectangle2D._i1.x;
- Rectangle2D._i2.y = Rectangle2D._i0.y - Rectangle2D._i1.y;
- // It's a hit if the squared distance is less/equal to the squared radius of the round circle
- return Rectangle2D._i2.lengthSquared() <= rrs;
- }
- // Check if the point is in the top/right quarter area
- Rectangle2D._i1.x = size.width - rr;
- Rectangle2D._i1.y = size.height - rr;
- if (Rectangle2D._i0.x >= Rectangle2D._i1.x && Rectangle2D._i0.y >= Rectangle2D._i1.y) {
- // Compute the intersection point in the quarter local space
- Rectangle2D._i2.x = Rectangle2D._i0.x - Rectangle2D._i1.x;
- Rectangle2D._i2.y = Rectangle2D._i0.y - Rectangle2D._i1.y;
- // It's a hit if the squared distance is less/equal to the squared radius of the round circle
- return Rectangle2D._i2.lengthSquared() <= rrs;
- }
- // Check if the point is in the bottom/right quarter area
- Rectangle2D._i1.x = size.width - rr;
- Rectangle2D._i1.y = rr;
- if (Rectangle2D._i0.x >= Rectangle2D._i1.x && Rectangle2D._i0.y <= Rectangle2D._i1.y) {
- // Compute the intersection point in the quarter local space
- Rectangle2D._i2.x = Rectangle2D._i0.x - Rectangle2D._i1.x;
- Rectangle2D._i2.y = Rectangle2D._i0.y - Rectangle2D._i1.y;
- // It's a hit if the squared distance is less/equal to the squared radius of the round circle
- return Rectangle2D._i2.lengthSquared() <= rrs;
- }
- // At any other locations the point is guarantied to be inside
- return true;
- }
- protected updateLevelBoundingInfo() {
- BoundingInfo2D.CreateFromSizeToRef(this.actualSize, this._levelBoundingInfo);
- }
- /**
- * Create an Rectangle 2D Shape primitive. May be a sharp rectangle (with sharp corners), or a rounded one.
- * @param parent the parent primitive, must be a valid primitive (or the Canvas)
- * options:
- * - id a text identifier, for information purpose
- * - position: the X & Y positions relative to its parent. Alternatively the x and y properties can be set. Default is [0;0]
- * - origin: define the normalized origin point location, default [0.5;0.5]
- * - size: the size of the group. Alternatively the width and height properties can be set. Default will be [10;10].
- * - roundRadius: if the rectangle has rounded corner, set their radius, default is 0 (to get a sharp rectangle).
- * - 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.
- * - 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.
- * - borderThickness: the thickness of the drawn border, default is 1.
- * - isVisible: true if the primitive must be visible, false for hidden. Default is true.
- * - 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.
- * - hAlighment: define horizontal alignment of the Canvas, alignment is optional, default value null: no alignment.
- * - vAlighment: define horizontal alignment of the Canvas, alignment is optional, default value null: no alignment.
- */
- constructor(settings ?: {
- parent ?: Prim2DBase,
- children ?: Array<Prim2DBase>,
- id ?: string,
- position ?: Vector2,
- x ?: number,
- y ?: number,
- rotation ?: number,
- scale ?: number,
- origin ?: Vector2,
- size ?: Size,
- width ?: number,
- height ?: number,
- roundRadius ?: number,
- fill ?: IBrush2D | string,
- border ?: IBrush2D | string,
- borderThickness ?: number,
- isVisible ?: boolean,
- marginTop ?: number | string,
- marginLeft ?: number | string,
- marginRight ?: number | string,
- marginBottom ?: number | string,
- margin ?: number | string,
- marginHAlignment ?: number,
- marginVAlignment ?: number,
- marginAlignment ?: string,
- paddingTop ?: number | string,
- paddingLeft ?: number | string,
- paddingRight ?: number | string,
- paddingBottom ?: number | string,
- padding ?: string,
- paddingHAlignment?: number,
- paddingVAlignment?: number,
- }) {
- // Avoid checking every time if the object exists
- if (settings == null) {
- settings = {};
- }
- super(settings);
- if (settings.size != null) {
- this.size = settings.size;
- }
- else if (settings.width || settings.height) {
- let size = new Size(settings.width, settings.height);
- this.size = size;
- }
- //let size = settings.size || (new Size((settings.width === null) ? null : (settings.width || 10), (settings.height === null) ? null : (settings.height || 10)));
- let roundRadius = (settings.roundRadius == null) ? 0 : settings.roundRadius;
- let borderThickness = (settings.borderThickness == null) ? 1 : settings.borderThickness;
- //this.size = size;
- this.roundRadius = roundRadius;
- this.borderThickness = borderThickness;
- }
- public static roundSubdivisions = 16;
- protected createModelRenderCache(modelKey: string): ModelRenderCache {
- let renderCache = new Rectangle2DRenderCache(this.owner.engine, modelKey);
- return renderCache;
- }
- protected setupModelRenderCache(modelRenderCache: ModelRenderCache) {
- let renderCache = <Rectangle2DRenderCache>modelRenderCache;
- let engine = this.owner.engine;
- // Need to create WebGL resources for fill part?
- if (this.fill) {
- let vbSize = ((this.notRounded ? 1 : Rectangle2D.roundSubdivisions) * 4) + 1;
- let vb = new Float32Array(vbSize);
- for (let i = 0; i < vbSize; i++) {
- vb[i] = i;
- }
- renderCache.fillVB = engine.createVertexBuffer(vb);
- let triCount = vbSize - 1;
- let ib = new Float32Array(triCount * 3);
- for (let i = 0; i < triCount; i++) {
- ib[i * 3 + 0] = 0;
- ib[i * 3 + 2] = i + 1;
- ib[i * 3 + 1] = i + 2;
- }
- ib[triCount * 3 - 2] = 1;
- renderCache.fillIB = engine.createIndexBuffer(ib);
- renderCache.fillIndicesCount = triCount * 3;
- // 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
- let ei = this.getDataPartEffectInfo(Shape2D.SHAPE2D_FILLPARTID, ["index"], true);
- if (ei) {
- renderCache.effectFillInstanced = engine.createEffect("rect2d", ei.attributes, ei.uniforms, [], ei.defines, null);
- }
- // Get the non instanced version
- ei = this.getDataPartEffectInfo(Shape2D.SHAPE2D_FILLPARTID, ["index"], false);
- renderCache.effectFill = engine.createEffect("rect2d", ei.attributes, ei.uniforms, [], ei.defines, null);
- }
- // Need to create WebGL resource for border part?
- if (this.border) {
- let vbSize = (this.notRounded ? 1 : Rectangle2D.roundSubdivisions) * 4 * 2;
- let vb = new Float32Array(vbSize);
- for (let i = 0; i < vbSize; i++) {
- vb[i] = i;
- }
- renderCache.borderVB = engine.createVertexBuffer(vb);
- let triCount = vbSize;
- let rs = triCount / 2;
- let ib = new Float32Array(triCount * 3);
- for (let i = 0; i < rs; i++) {
- let r0 = i;
- let r1 = (i + 1) % rs;
- ib[i * 6 + 0] = rs + r1;
- ib[i * 6 + 1] = rs + r0;
- ib[i * 6 + 2] = r0;
- ib[i * 6 + 3] = r1;
- ib[i * 6 + 4] = rs + r1;
- ib[i * 6 + 5] = r0;
- }
- renderCache.borderIB = engine.createIndexBuffer(ib);
- renderCache.borderIndicesCount = triCount * 3;
- // 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
- let ei = this.getDataPartEffectInfo(Shape2D.SHAPE2D_BORDERPARTID, ["index"], true);
- if (ei) {
- renderCache.effectBorderInstanced = engine.createEffect("rect2d", ei.attributes, ei.uniforms, [], ei.defines, null);
- }
- // Get the non instanced version
- ei = this.getDataPartEffectInfo(Shape2D.SHAPE2D_BORDERPARTID, ["index"], false);
- renderCache.effectBorder = engine.createEffect("rect2d", ei.attributes, ei.uniforms, [], ei.defines, null);
- }
- return renderCache;
- }
- // We override this method because if there's a roundRadius set, we will reduce the initial Content Area to make sure the computed area won't intersect with the shape contour. The formula is simple: we shrink the incoming size by the amount of the roundRadius
- protected _getInitialContentAreaToRef(primSize: Size, initialContentPosition: Vector2, initialContentArea: Size) {
- // Fall back to default implementation if there's no round Radius
- if (this._notRounded) {
- super._getInitialContentAreaToRef(primSize, initialContentPosition, initialContentArea);
- } else {
- let rr = Math.round((this.roundRadius - (this.roundRadius/Math.sqrt(2))) * 1.3);
- initialContentPosition.x = initialContentPosition.y = rr;
- initialContentArea.width = Math.max(0, primSize.width - (rr * 2));
- initialContentArea.height = Math.max(0, primSize.height - (rr * 2));
- }
- }
- protected createInstanceDataParts(): InstanceDataBase[] {
- var res = new Array<InstanceDataBase>();
- if (this.border) {
- res.push(new Rectangle2DInstanceData(Shape2D.SHAPE2D_BORDERPARTID));
- }
- if (this.fill) {
- res.push(new Rectangle2DInstanceData(Shape2D.SHAPE2D_FILLPARTID));
- }
- return res;
- }
- protected refreshInstanceDataPart(part: InstanceDataBase): boolean {
- if (!super.refreshInstanceDataPart(part)) {
- return false;
- }
- if (part.id === Shape2D.SHAPE2D_BORDERPARTID) {
- let d = <Rectangle2DInstanceData>part;
- let size = this.actualSize;
- d.properties = new Vector3(size.width, size.height, this.roundRadius || 0);
- }
- else if (part.id === Shape2D.SHAPE2D_FILLPARTID) {
- let d = <Rectangle2DInstanceData>part;
- let size = this.actualSize;
- d.properties = new Vector3(size.width, size.height, this.roundRadius || 0);
- }
- return true;
- }
- private _notRounded: boolean;
- private _roundRadius: number;
- }
- }
|