Skip to content
Snippets Groups Projects
Commit 4287e09a authored by Alexander Rose's avatar Alexander Rose
Browse files

fix material param number formating

parent fdc006f8
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
import { NumberArray } from './type-helpers'; import { NumberArray } from './type-helpers';
import { ParamDefinition as PD } from './param-definition'; import { ParamDefinition as PD } from './param-definition';
import { toPrecision } from './number'; import { toFixed } from './number';
/** Material properties expressed as a single number */ /** Material properties expressed as a single number */
export type Material = { readonly '@type': 'material' } & number export type Material = { readonly '@type': 'material' } & number
...@@ -22,12 +22,12 @@ export namespace Material { ...@@ -22,12 +22,12 @@ export namespace Material {
return fromNormalized(v.metalness, v.roughness); 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 metalness = (material >> 16 & 255) / 255;
const roughness = (material >> 8 & 255) / 255; const roughness = (material >> 8 & 255) / 255;
return { return {
metalness: precision ? toPrecision(metalness, precision) : metalness, metalness: fractionDigits ? toFixed(metalness, fractionDigits) : metalness,
roughness: precision ? toPrecision(roughness, precision) : roughness roughness: fractionDigits ? toFixed(roughness, fractionDigits) : roughness
}; };
} }
...@@ -45,7 +45,7 @@ export namespace Material { ...@@ -45,7 +45,7 @@ export namespace Material {
export function getParam(info?: { isExpanded?: boolean, isFlat?: boolean }) { export function getParam(info?: { isExpanded?: boolean, isFlat?: boolean }) {
return PD.Converted( return PD.Converted(
(v: Material) => toObjectNormalized(v, 2), (v: Material) => toObjectNormalized(v, 1),
(v: { metalness: number, roughness: number }) => fromObjectNormalized(v), (v: { metalness: number, roughness: number }) => fromObjectNormalized(v),
PD.Group({ PD.Group({
metalness: PD.Numeric(0, { min: 0, max: 1, step: 0.1 }), metalness: PD.Numeric(0, { min: 0, max: 1, step: 0.1 }),
......
...@@ -69,3 +69,7 @@ export function getPrecision(v: number) { ...@@ -69,3 +69,7 @@ export function getPrecision(v: number) {
export function toPrecision(v: number, precision: number) { export function toPrecision(v: number, precision: number) {
return parseFloat(v.toPrecision(precision)); return parseFloat(v.toPrecision(precision));
} }
export function toFixed(v: number, fractionDigits: number) {
return parseFloat(v.toFixed(fractionDigits));
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment