material.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * Copyright (c) 2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { NumberArray } from './type-helpers';
  7. import { ParamDefinition as PD } from './param-definition';
  8. export interface Material {
  9. /** Normalized to [0, 1] range */
  10. metalness: number,
  11. /** Normalized to [0, 1] range */
  12. roughness: number
  13. /** Normalized to [0, 1] range */
  14. bumpiness: number
  15. }
  16. export function Material(values?: Partial<Material>) {
  17. return { ...Material.Zero, ...values };
  18. }
  19. export namespace Material {
  20. export const Zero: Material = { metalness: 0, roughness: 0, bumpiness: 0 };
  21. export function toArray(material: Material, array: NumberArray, offset: number) {
  22. array[offset] = material.metalness * 255;
  23. array[offset + 1] = material.roughness * 255;
  24. array[offset + 2] = material.bumpiness * 255;
  25. return array;
  26. }
  27. export function toString({ metalness, roughness, bumpiness }: Material) {
  28. return `M ${metalness.toFixed(2)} | R ${roughness.toFixed(2)} | B ${bumpiness.toFixed(2)}`;
  29. }
  30. export function getParam(info?: { isExpanded?: boolean, isFlat?: boolean }) {
  31. return PD.Group({
  32. metalness: PD.Numeric(0, { min: 0, max: 1, step: 0.01 }),
  33. roughness: PD.Numeric(1, { min: 0, max: 1, step: 0.01 }),
  34. bumpiness: PD.Numeric(0, { min: 0, max: 1, step: 0.01 }),
  35. }, {
  36. ...info,
  37. presets: [
  38. [{ metalness: 0, roughness: 1, bumpiness: 0 }, 'Matte'],
  39. [{ metalness: 0, roughness: 0.2, bumpiness: 0 }, 'Plastic'],
  40. [{ metalness: 0, roughness: 0.6, bumpiness: 0 }, 'Glossy'],
  41. [{ metalness: 1.0, roughness: 0.6, bumpiness: 0 }, 'Metallic'],
  42. ]
  43. });
  44. }
  45. }