index.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Copyright (c) 2019-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Canvas3DProps } from '../../mol-canvas3d/canvas3d';
  7. import { BuiltInTrajectoryFormat } from '../../mol-plugin-state/formats/trajectory';
  8. import { createPluginUI } from '../../mol-plugin-ui/react18';
  9. import { PluginUIContext } from '../../mol-plugin-ui/context';
  10. import { DefaultPluginUISpec } from '../../mol-plugin-ui/spec';
  11. import { PluginCommands } from '../../mol-plugin/commands';
  12. import { Asset } from '../../mol-util/assets';
  13. import { Color } from '../../mol-util/color';
  14. import './index.html';
  15. require('mol-plugin-ui/skin/light.scss');
  16. type LoadParams = { url: string, format?: BuiltInTrajectoryFormat, isBinary?: boolean, assemblyId?: string }
  17. type _Preset = Pick<Canvas3DProps, 'postprocessing' | 'renderer'>
  18. type Preset = { [K in keyof _Preset]: Partial<_Preset[K]> }
  19. const Canvas3DPresets = {
  20. illustrative: {
  21. canvas3d: <Preset>{
  22. postprocessing: {
  23. occlusion: { name: 'on', params: { samples: 32, radius: 6, bias: 1.4, blurKernelSize: 15, resolutionScale: 1 } },
  24. outline: { name: 'on', params: { scale: 1, threshold: 0.33, color: Color(0x000000) } },
  25. shadow: { name: 'off', params: {} },
  26. },
  27. renderer: {
  28. ambientIntensity: 1.0,
  29. light: []
  30. }
  31. }
  32. },
  33. occlusion: {
  34. canvas3d: <Preset>{
  35. postprocessing: {
  36. occlusion: { name: 'on', params: { samples: 32, radius: 6, bias: 1.4, blurKernelSize: 15, resolutionScale: 1 } },
  37. outline: { name: 'off', params: {} },
  38. shadow: { name: 'off', params: {} },
  39. },
  40. renderer: {
  41. ambientIntensity: 0.4,
  42. light: [{ inclination: 180, azimuth: 0, color: Color.fromNormalizedRgb(1.0, 1.0, 1.0),
  43. intensity: 0.6 }]
  44. }
  45. }
  46. },
  47. standard: {
  48. canvas3d: <Preset>{
  49. postprocessing: {
  50. occlusion: { name: 'off', params: {} },
  51. outline: { name: 'off', params: {} },
  52. shadow: { name: 'off', params: {} },
  53. },
  54. renderer: {
  55. ambientIntensity: 0.4,
  56. light: [{ inclination: 180, azimuth: 0, color: Color.fromNormalizedRgb(1.0, 1.0, 1.0),
  57. intensity: 0.6 }]
  58. }
  59. }
  60. }
  61. };
  62. type Canvas3DPreset = keyof typeof Canvas3DPresets
  63. class LightingDemo {
  64. plugin: PluginUIContext;
  65. private radius = 5;
  66. private bias = 1.1;
  67. private preset: Canvas3DPreset = 'illustrative';
  68. async init(target: string | HTMLElement) {
  69. this.plugin = await createPluginUI(typeof target === 'string' ? document.getElementById(target)! : target, {
  70. ...DefaultPluginUISpec(),
  71. layout: {
  72. initial: {
  73. isExpanded: false,
  74. showControls: false
  75. },
  76. },
  77. components: {
  78. controls: { left: 'none', right: 'none', top: 'none', bottom: 'none' }
  79. }
  80. });
  81. this.setPreset('illustrative');
  82. }
  83. setPreset(preset: Canvas3DPreset) {
  84. const props = Canvas3DPresets[preset];
  85. if (props.canvas3d.postprocessing.occlusion?.name === 'on') {
  86. props.canvas3d.postprocessing.occlusion.params.radius = this.radius;
  87. props.canvas3d.postprocessing.occlusion.params.bias = this.bias;
  88. }
  89. PluginCommands.Canvas3D.SetSettings(this.plugin, {
  90. settings: {
  91. ...props,
  92. renderer: {
  93. ...this.plugin.canvas3d!.props.renderer,
  94. ...props.canvas3d.renderer
  95. },
  96. postprocessing: {
  97. ...this.plugin.canvas3d!.props.postprocessing,
  98. ...props.canvas3d.postprocessing
  99. },
  100. }
  101. });
  102. }
  103. async load({ url, format = 'mmcif', isBinary = true, assemblyId = '' }: LoadParams, radius: number, bias: number) {
  104. await this.plugin.clear();
  105. const data = await this.plugin.builders.data.download({ url: Asset.Url(url), isBinary }, { state: { isGhost: true } });
  106. const trajectory = await this.plugin.builders.structure.parseTrajectory(data, format);
  107. const model = await this.plugin.builders.structure.createModel(trajectory);
  108. const structure = await this.plugin.builders.structure.createStructure(model, assemblyId ? { name: 'assembly', params: { id: assemblyId } } : { name: 'model', params: {} });
  109. const polymer = await this.plugin.builders.structure.tryCreateComponentStatic(structure, 'polymer');
  110. if (polymer) await this.plugin.builders.structure.representation.addRepresentation(polymer, { type: 'spacefill', color: 'illustrative' });
  111. const ligand = await this.plugin.builders.structure.tryCreateComponentStatic(structure, 'ligand');
  112. if (ligand) await this.plugin.builders.structure.representation.addRepresentation(ligand, { type: 'ball-and-stick', color: 'element-symbol', colorParams: { carbonColor: { name: 'element-symbol', params: {} } } });
  113. this.radius = radius;
  114. this.bias = bias;
  115. this.setPreset(this.preset);
  116. }
  117. }
  118. (window as any).LightingDemo = new LightingDemo();