index.ts 5.0 KB

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