index.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 { createPlugin, DefaultPluginSpec } from '../../mol-plugin';
  8. import { BuiltInTrajectoryFormat } from '../../mol-plugin-state/formats/trajectory';
  9. import { PluginCommands } from '../../mol-plugin/commands';
  10. import { PluginContext } from '../../mol-plugin/context';
  11. import './index.html';
  12. require('mol-plugin-ui/skin/light.scss')
  13. type LoadParams = { url: string, format?: BuiltInTrajectoryFormat, isBinary?: boolean, assemblyId?: string }
  14. type _Preset = Pick<Canvas3DProps, 'multiSample' | 'postprocessing' | 'renderer'>
  15. type Preset = { [K in keyof _Preset]: Partial<_Preset[K]> }
  16. const Canvas3DPresets = {
  17. illustrative: <Preset> {
  18. multiSample: {
  19. mode: 'temporal' as Canvas3DProps['multiSample']['mode']
  20. },
  21. postprocessing: {
  22. occlusion: { name: 'on', params: { bias: 0.8, kernelSize: 6, radius: 64 } },
  23. outline: { name: 'on', params: { scale: 1, threshold: 0.8 } }
  24. },
  25. renderer: {
  26. ambientIntensity: 1,
  27. lightIntensity: 0,
  28. }
  29. },
  30. occlusion: <Preset> {
  31. multiSample: {
  32. mode: 'temporal' as Canvas3DProps['multiSample']['mode']
  33. },
  34. postprocessing: {
  35. occlusion: { name: 'on', params: { bias: 0.8, kernelSize: 6, radius: 64 } },
  36. outline: { name: 'off', params: { } }
  37. },
  38. renderer: {
  39. ambientIntensity: 0.4,
  40. lightIntensity: 0.6,
  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. ambientIntensity: 0.4,
  53. lightIntensity: 0.6,
  54. }
  55. }
  56. }
  57. type Canvas3DPreset = keyof typeof Canvas3DPresets
  58. class LightingDemo {
  59. plugin: PluginContext;
  60. init(target: string | HTMLElement) {
  61. this.plugin = createPlugin(typeof target === 'string' ? document.getElementById(target)! : target, {
  62. ...DefaultPluginSpec,
  63. layout: {
  64. initial: {
  65. isExpanded: false,
  66. showControls: false
  67. },
  68. controls: { left: 'none', right: 'none', top: 'none', bottom: 'none' }
  69. }
  70. });
  71. this.setPreset('illustrative');
  72. }
  73. setPreset(preset: Canvas3DPreset) {
  74. const props = Canvas3DPresets[preset]
  75. PluginCommands.Canvas3D.SetSettings(this.plugin, { settings: {
  76. ...props,
  77. multiSample: {
  78. ...this.plugin.canvas3d!.props.multiSample,
  79. ...props.multiSample
  80. },
  81. renderer: {
  82. ...this.plugin.canvas3d!.props.renderer,
  83. ...props.renderer
  84. },
  85. postprocessing: {
  86. ...this.plugin.canvas3d!.props.postprocessing,
  87. ...props.postprocessing
  88. },
  89. }});
  90. }
  91. async load({ url, format = 'mmcif', isBinary = false, assemblyId = '' }: LoadParams) {
  92. await this.plugin.clear();
  93. const data = await this.plugin.builders.data.download({ url, isBinary }, { state: { isGhost: true } });
  94. const trajectory = await this.plugin.builders.structure.parseTrajectory(data, format);
  95. const model = await this.plugin.builders.structure.createModel(trajectory);
  96. const structure = await this.plugin.builders.structure.createStructure(model, assemblyId ? { name: 'assembly', params: { id: assemblyId } } : { name: 'deposited', params: { } });
  97. const polymer = await this.plugin.builders.structure.tryCreateComponentStatic(structure, 'polymer');
  98. if (polymer) await this.plugin.builders.structure.representation.addRepresentation(polymer, { type: 'spacefill', color: 'illustrative' });
  99. const ligand = await this.plugin.builders.structure.tryCreateComponentStatic(structure, 'ligand');
  100. if (ligand) await this.plugin.builders.structure.representation.addRepresentation(ligand, { type: 'ball-and-stick' });
  101. }
  102. }
  103. (window as any).LightingDemo = new LightingDemo();