index.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * Copyright (c) 2019 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 { createPlugin } from '../../mol-plugin-ui';
  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 './index.html';
  14. require('mol-plugin-ui/skin/light.scss');
  15. type LoadParams = { url: string, format?: BuiltInTrajectoryFormat, isBinary?: boolean, assemblyId?: string }
  16. type _Preset = Pick<Canvas3DProps, 'multiSample' | 'postprocessing' | 'renderer'>
  17. type Preset = { [K in keyof _Preset]: Partial<_Preset[K]> }
  18. const Canvas3DPresets = {
  19. illustrative: <Preset> {
  20. multiSample: {
  21. mode: 'temporal' as Canvas3DProps['multiSample']['mode']
  22. },
  23. postprocessing: {
  24. occlusion: { name: 'on', params: { samples: 32, radius: 6, bias: 1.4, blurKernelSize: 15 } },
  25. outline: { name: 'on', params: { scale: 1, threshold: 0.1 } }
  26. },
  27. renderer: {
  28. style: { name: 'flat', params: {} }
  29. }
  30. },
  31. occlusion: <Preset> {
  32. multiSample: {
  33. mode: 'temporal' as Canvas3DProps['multiSample']['mode']
  34. },
  35. postprocessing: {
  36. occlusion: { name: 'on', params: { samples: 32, radius: 6, bias: 1.4, blurKernelSize: 15 } },
  37. outline: { name: 'off', params: { } }
  38. },
  39. renderer: {
  40. style: { name: 'matte', params: {} }
  41. }
  42. },
  43. standard: <Preset> {
  44. multiSample: {
  45. mode: 'off' as Canvas3DProps['multiSample']['mode']
  46. },
  47. postprocessing: {
  48. occlusion: { name: 'off', params: { } },
  49. outline: { name: 'off', params: { } }
  50. },
  51. renderer: {
  52. style: { name: 'matte', params: {} }
  53. }
  54. }
  55. };
  56. type Canvas3DPreset = keyof typeof Canvas3DPresets
  57. class LightingDemo {
  58. plugin: PluginUIContext;
  59. private radius = 5;
  60. private bias = 1.1;
  61. private preset: Canvas3DPreset = 'illustrative';
  62. init(target: string | HTMLElement) {
  63. this.plugin = createPlugin(typeof target === 'string' ? document.getElementById(target)! : target, {
  64. ...DefaultPluginUISpec(),
  65. layout: {
  66. initial: {
  67. isExpanded: false,
  68. showControls: false
  69. },
  70. },
  71. components: {
  72. controls: { left: 'none', right: 'none', top: 'none', bottom: 'none' }
  73. }
  74. });
  75. this.setPreset('illustrative');
  76. }
  77. setPreset(preset: Canvas3DPreset) {
  78. const props = Canvas3DPresets[preset];
  79. if (props.postprocessing.occlusion?.name === 'on') {
  80. props.postprocessing.occlusion.params.radius = this.radius;
  81. props.postprocessing.occlusion.params.bias = this.bias;
  82. }
  83. PluginCommands.Canvas3D.SetSettings(this.plugin, { settings: {
  84. ...props,
  85. multiSample: {
  86. ...this.plugin.canvas3d!.props.multiSample,
  87. ...props.multiSample
  88. },
  89. renderer: {
  90. ...this.plugin.canvas3d!.props.renderer,
  91. ...props.renderer
  92. },
  93. postprocessing: {
  94. ...this.plugin.canvas3d!.props.postprocessing,
  95. ...props.postprocessing
  96. },
  97. }});
  98. }
  99. async load({ url, format = 'mmcif', isBinary = true, assemblyId = '' }: LoadParams, radius: number, bias: number) {
  100. await this.plugin.clear();
  101. const data = await this.plugin.builders.data.download({ url: Asset.Url(url), isBinary }, { state: { isGhost: true } });
  102. const trajectory = await this.plugin.builders.structure.parseTrajectory(data, format);
  103. const model = await this.plugin.builders.structure.createModel(trajectory);
  104. const structure = await this.plugin.builders.structure.createStructure(model, assemblyId ? { name: 'assembly', params: { id: assemblyId } } : { name: 'model', params: { } });
  105. const polymer = await this.plugin.builders.structure.tryCreateComponentStatic(structure, 'polymer');
  106. if (polymer) await this.plugin.builders.structure.representation.addRepresentation(polymer, { type: 'spacefill', color: 'illustrative' });
  107. const ligand = await this.plugin.builders.structure.tryCreateComponentStatic(structure, 'ligand');
  108. if (ligand) await this.plugin.builders.structure.representation.addRepresentation(ligand, { type: 'ball-and-stick', color: 'element-symbol', colorParams: { carbonColor: { name: 'element-symbol', params: {} } } });
  109. this.radius = radius;
  110. this.bias = bias;
  111. this.setPreset(this.preset);
  112. }
  113. }
  114. (window as any).LightingDemo = new LightingDemo();