From 4287e09a9aa5bdf07c180db12ea2ea665ba31979 Mon Sep 17 00:00:00 2001 From: Alexander Rose <alexander.rose@weirdbyte.de> Date: Sat, 27 Nov 2021 17:25:51 -0800 Subject: [PATCH] fix material param number formating --- src/mol-util/material.ts | 10 +++++----- src/mol-util/number.ts | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/mol-util/material.ts b/src/mol-util/material.ts index 10e1914b1..4dee3882b 100644 --- a/src/mol-util/material.ts +++ b/src/mol-util/material.ts @@ -6,7 +6,7 @@ import { NumberArray } from './type-helpers'; import { ParamDefinition as PD } from './param-definition'; -import { toPrecision } from './number'; +import { toFixed } from './number'; /** Material properties expressed as a single number */ export type Material = { readonly '@type': 'material' } & number @@ -22,12 +22,12 @@ export namespace Material { return fromNormalized(v.metalness, v.roughness); } - export function toObjectNormalized(material: Material, precision?: number) { + export function toObjectNormalized(material: Material, fractionDigits?: number) { const metalness = (material >> 16 & 255) / 255; const roughness = (material >> 8 & 255) / 255; return { - metalness: precision ? toPrecision(metalness, precision) : metalness, - roughness: precision ? toPrecision(roughness, precision) : roughness + metalness: fractionDigits ? toFixed(metalness, fractionDigits) : metalness, + roughness: fractionDigits ? toFixed(roughness, fractionDigits) : roughness }; } @@ -45,7 +45,7 @@ export namespace Material { export function getParam(info?: { isExpanded?: boolean, isFlat?: boolean }) { return PD.Converted( - (v: Material) => toObjectNormalized(v, 2), + (v: Material) => toObjectNormalized(v, 1), (v: { metalness: number, roughness: number }) => fromObjectNormalized(v), PD.Group({ metalness: PD.Numeric(0, { min: 0, max: 1, step: 0.1 }), diff --git a/src/mol-util/number.ts b/src/mol-util/number.ts index 542ae2c2d..ecf5d5cdc 100644 --- a/src/mol-util/number.ts +++ b/src/mol-util/number.ts @@ -68,4 +68,8 @@ export function getPrecision(v: number) { export function toPrecision(v: number, precision: number) { return parseFloat(v.toPrecision(precision)); +} + +export function toFixed(v: number, fractionDigits: number) { + return parseFloat(v.toFixed(fractionDigits)); } \ No newline at end of file -- GitLab