threepipe
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SwitchNodePlugin.ts 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {SwitchNodeBasePlugin} from 'threepipe'
  2. import {GridItemListPlugin} from './GridItemListPlugin'
  3. /**
  4. * Switch Node Plugin (Basic UI)
  5. * This plugin allows you to configure object variations in a file and apply them in the scene.
  6. * Each SwitchNode is a parent object with multiple direct children. Only one child is visible at a time.
  7. * This works by toggling the `visible` property of the children of a parent object.
  8. * The plugin interfaces with the picking plugin and also provides uiConfig to show and edit the variations.
  9. * It also provides a function to create snapshot previews of individual variations. This creates a limited render of the object with the selected child visible.
  10. * To get a proper render, its better to render it offline and set the image as a preview.
  11. * This functionality is inherited from `SwitchNodeBasePlugin`.
  12. *
  13. * Additionally this plugin adds a Grid UI using {@link GridItemListPlugin} in the DOM over the viewer canvas to show various object variations and allow the user to select them.
  14. * The UI can also be used in the editor to edit the variations and apply them.
  15. */
  16. export class SwitchNodePlugin extends SwitchNodeBasePlugin {
  17. public static readonly PluginType = 'SwitchNodePlugin'
  18. enableEditContextMenus = false
  19. dependencies = [GridItemListPlugin]
  20. protected _refreshUi(): boolean {
  21. if (!super._refreshUi()) return false
  22. const grid = this._viewer?.getPlugin(GridItemListPlugin)
  23. if (!grid) return false
  24. grid.removeAll(SwitchNodePlugin.PluginType)
  25. for (const variation of this.variations) {
  26. const obj = this._viewer!.scene.getObjectByName(variation.name)
  27. if (!obj) {
  28. console.warn('no object found for variation, skipping', variation)
  29. continue
  30. }
  31. if (obj.children.length < 1) {
  32. console.warn('SwitchNode does not have enough children', variation)
  33. }
  34. const container = grid.create(SwitchNodePlugin.PluginType,
  35. variation.title + (this.enableEditContextMenus ? ' (' + variation.name + ')' : ''),
  36. Math.min(5, obj.children.length),
  37. 20, 0,
  38. obj.children.map(child => {
  39. return {
  40. id: child.uuid,
  41. image: this.getPreview(variation, child),
  42. onClick: () => {
  43. this.selectNode(variation, child.name || child.uuid)
  44. },
  45. tooltip: child.name || child.uuid,
  46. }
  47. }), (d, _item)=> {
  48. // todo test in shadow dom.
  49. d.oncontextmenu = (e) => {
  50. if (!this.enableEditContextMenus) return
  51. e.preventDefault()
  52. e.stopPropagation()
  53. // todo
  54. // const menu = CustomContextMenu.Create(this.materialContextMenuItems(variation, item.id), e.clientX, e.clientY)
  55. // document.body.appendChild(menu)
  56. }
  57. }
  58. )
  59. container.oncontextmenu = (e) => {
  60. if (!this.enableEditContextMenus) return
  61. e.preventDefault()
  62. e.stopPropagation()
  63. // todo
  64. // const menu = CustomContextMenu.Create(this.variationsContextMenuItems(variation), e.clientX, e.clientY)
  65. // document.body.appendChild(menu)
  66. }
  67. }
  68. grid.rebuildUi()
  69. return true
  70. }
  71. }