Skip to content

Commit

Permalink
[sigma] Adds new rendering util numberToGLSLFloat
Browse files Browse the repository at this point in the history
Details:
- Deletes numberToGLSLFloat from node-border and node-piechart
- Replaces them by numberToGLSLFloat, exported from sigma/rendering
- Adds some unit tests for this helper
  • Loading branch information
jacomyal committed Apr 8, 2024
1 parent 737112e commit 2ffe08d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
4 changes: 3 additions & 1 deletion packages/node-border/src/shader-frag.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { CreateNodeBorderProgramOptions, DEFAULT_BORDER_SIZE_MODE, NodeBorderSize, numberToGLSLFloat } from "./utils";
import { numberToGLSLFloat } from "sigma/rendering";

import { CreateNodeBorderProgramOptions, DEFAULT_BORDER_SIZE_MODE, NodeBorderSize } from "./utils";

export default function getFragmentShader({ borders }: CreateNodeBorderProgramOptions) {
const fillCounts = numberToGLSLFloat(borders.filter(({ size }) => "fill" in size).length);
Expand Down
4 changes: 0 additions & 4 deletions packages/node-border/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,3 @@ export const DEFAULT_CREATE_NODE_BORDER_OPTIONS: CreateNodeBorderProgramOptions
};

export const DEFAULT_COLOR = "#000000";

export function numberToGLSLFloat(n: number): string {
return n % 1 === 0 ? n.toFixed(1) : n.toString();
}
4 changes: 3 additions & 1 deletion packages/node-piechart/src/shader-frag.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { CreateNodePiechartProgramOptions, numberToGLSLFloat } from "./utils";
import { numberToGLSLFloat } from "sigma/rendering";

import { CreateNodePiechartProgramOptions } from "./utils";

export default function getFragmentShader({ slices, offset }: CreateNodePiechartProgramOptions) {
// language=GLSL
Expand Down
4 changes: 0 additions & 4 deletions packages/node-piechart/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,3 @@ export const DEFAULT_CREATE_NODE_PIECHART_OPTIONS: Omit<CreateNodePiechartProgra
defaultColor: DEFAULT_COLOR,
offset: { value: 0 },
};

export function numberToGLSLFloat(n: number): string {
return n % 1 === 0 ? n.toFixed(1) : n.toString();
}
7 changes: 7 additions & 0 deletions packages/sigma/src/rendering/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,10 @@ export function loadProgram(gl: WebGLRenderingContext, shaders: Array<WebGLShade

return program;
}

/**
* Function use to print a float for inserting in a GLSL program.
*/
export function numberToGLSLFloat(n: number): string {
return n % 1 === 0 ? n.toFixed(1) : n.toString();
}
21 changes: 21 additions & 0 deletions packages/test/unit/rendering.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { numberToGLSLFloat } from "sigma/rendering";
import { describe, expect, test } from "vitest";

describe("rendering utils", () => {
describe("numberToGLSLFloat", () => {
test('it should properly print all kind of numbers to "GLSL float" strings', () => {
const tests: [number, string][] = [
[1, "1.0"],
[123, "123.0"],
[1.23, "1.23"],
[0.123, "0.123"],
[1230, "1230.0"],
[12300, "12300.0"],
[1e-6, "0.000001"],
[1e6, "1000000.0"],
];

tests.forEach(([input, expectedOutput]) => expect(numberToGLSLFloat(input)).toBe(expectedOutput));
});
});
});

0 comments on commit 2ffe08d

Please sign in to comment.