position.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ColorTheme } from '../color';
  7. import { Color } from '../../mol-util/color';
  8. import { Location } from '../../mol-model/location';
  9. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  10. import { ThemeDataContext } from '../theme';
  11. import { isPositionLocation } from '../../mol-geo/util/location-iterator';
  12. import { Vec3 } from '../../mol-math/linear-algebra';
  13. const DefaultColor = Color(0xCCCCCC);
  14. const Description = 'Gives geometry vertex colors based on positions.';
  15. export const PositionColorThemeParams = {
  16. };
  17. export type PositionColorThemeParams = typeof PositionColorThemeParams
  18. export function getPositionColorThemeParams(ctx: ThemeDataContext) {
  19. return PositionColorThemeParams; // TODO return copy
  20. }
  21. export function PositionColorTheme(ctx: ThemeDataContext, props: PD.Values<PositionColorThemeParams>): ColorTheme<PositionColorThemeParams> {
  22. const p = Vec3();
  23. return {
  24. factory: PositionColorTheme,
  25. granularity: 'vertexInstance',
  26. color: (location: Location): Color => {
  27. if (isPositionLocation(location)) {
  28. Vec3.scale(p, location.position, 1 / 50);
  29. return ColorCosine.palette6(p);
  30. }
  31. return DefaultColor;
  32. },
  33. props: props,
  34. description: Description,
  35. };
  36. }
  37. export const PositionColorThemeProvider: ColorTheme.Provider<PositionColorThemeParams, 'position'> = {
  38. name: 'position',
  39. label: 'Position',
  40. category: ColorTheme.Category.Misc,
  41. factory: PositionColorTheme,
  42. getParams: getPositionColorThemeParams,
  43. defaultValues: PD.getDefaultValues(PositionColorThemeParams),
  44. isApplicable: (ctx: ThemeDataContext) => true
  45. };
  46. //
  47. // ported from https://iquilezles.org/www/articles/palettes/palettes.htm
  48. export namespace ColorCosine {
  49. const tmp = Vec3();
  50. export function palette(t: Vec3, a: Vec3, b: Vec3, c: Vec3, d: Vec3): Color {
  51. // a + b * cos(6.28318 * (c * t + d))
  52. Vec3.mul(tmp, c, t);
  53. Vec3.add(tmp, tmp, d);
  54. Vec3.scale(tmp, tmp, 6.28318);
  55. tmp[0] = Math.cos(tmp[0]);
  56. tmp[1] = Math.cos(tmp[1]);
  57. tmp[2] = Math.cos(tmp[2]);
  58. Vec3.mul(tmp, b, tmp);
  59. Vec3.add(tmp, a, tmp);
  60. return Color.fromNormalizedArray(tmp, 0);
  61. }
  62. const a6 = Vec3.create(0.5, 0.5, 0.5);
  63. const b6 = Vec3.create(0.5, 0.5, 0.5);
  64. const c6 = Vec3.create(2.0, 1.0, 0.0);
  65. const d6 = Vec3.create(0.50, 0.20, 0.25);
  66. export function palette6(t: Vec3) {
  67. return palette(t, a6, b6, c6, d6);
  68. }
  69. }