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

Object3DGeneratorPlugin.ts 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import {AViewerPluginSync} from '../../viewer'
  2. import {
  3. AmbientLight2,
  4. DirectionalLight2,
  5. HemisphereLight2,
  6. IObject3D,
  7. PerspectiveCamera2,
  8. PointLight2,
  9. RectAreaLight2,
  10. SpotLight2,
  11. } from '../../core'
  12. import {uiButton, uiDropdown, uiPanelContainer} from 'uiconfig.js'
  13. import {Vector3} from 'three'
  14. /**
  15. * Adds support for generating different types of lights and camera objects in the viewer.
  16. * @category Plugin
  17. */
  18. @uiPanelContainer('Generate Scene Objects')
  19. export class Object3DGeneratorPlugin extends AViewerPluginSync<''> {
  20. public static readonly PluginType = 'Object3DGeneratorPlugin'
  21. enabled = true
  22. toJSON: any = undefined
  23. @uiDropdown('Type', undefined, (that)=>({
  24. children: [()=>Object.keys(that.generators).map(label=>({label}))],
  25. }))
  26. protected _selectedType = ''
  27. @uiButton('Generate')
  28. generate(type?: string, params?: any, addToScene = true) {
  29. if (!this._viewer) throw new Error('No viewer')
  30. const obj = this.generators[type ?? this._selectedType]?.(params)
  31. addToScene && obj && this._viewer.scene.addObject(obj)
  32. return obj
  33. }
  34. generators: Record<string, (params?: any)=>IObject3D> = {
  35. ['camera-perspective']: (params: {
  36. controlsMode?: string,
  37. autoAspect?: boolean,
  38. fov?: number,
  39. aspect?: number,
  40. position?: Vector3,
  41. target?: Vector3,
  42. autoLookAtTarget?: boolean,
  43. name?: string,
  44. } = {})=>{
  45. const camera = new PerspectiveCamera2(
  46. params.controlsMode ?? '',
  47. this._viewer?.canvas,
  48. params.autoAspect,
  49. params.fov,
  50. params.aspect,
  51. )
  52. params.position ? camera.position.copy(params.position) : camera.position.set(0, 0, 5)
  53. params.target ? camera.target.copy(params.target) : camera.target.set(0, 0, 0)
  54. camera.autoLookAtTarget = params.autoLookAtTarget ?? true
  55. camera.setDirty()
  56. camera.name = params.name ?? 'Perspective Camera'
  57. return camera
  58. },
  59. ['light-directional']: (params: {
  60. color?: number,
  61. intensity?: number,
  62. position?: Vector3,
  63. target?: Vector3,
  64. name?: string,
  65. } = {})=>{
  66. const light = new DirectionalLight2(params.color ?? 0xff0000, params.intensity ?? 3)
  67. params.position ? light.position.copy(params.position) : light.position.set(5, 5, 5)
  68. light.lookAt(params.target ?? new Vector3(0, 0, 0))
  69. light.name = 'Directional Light'
  70. return light
  71. },
  72. ['light-ambient']: (params: {
  73. color?: number,
  74. intensity?: number,
  75. name?: string,
  76. } = {})=>{
  77. const light = new AmbientLight2(params.color ?? 0xffffff, params.intensity ?? 1)
  78. light.name = 'Ambient Light'
  79. return light
  80. },
  81. ['light-point']: (params: {
  82. color?: number,
  83. intensity?: number,
  84. position?: Vector3,
  85. name?: string,
  86. } = {})=>{
  87. const light = new PointLight2(params.color ?? 0xff0000, params.intensity ?? 3)
  88. params.position ? light.position.copy(params.position) : light.position.set(5, 5, 5)
  89. light.name = 'Point Light'
  90. return light
  91. },
  92. ['light-spot']: (params: {
  93. color?: number,
  94. intensity?: number,
  95. position?: Vector3,
  96. target?: Vector3,
  97. name?: string,
  98. } = {})=>{
  99. const light = new SpotLight2(params.color ?? 0xff0000, params.intensity ?? 3)
  100. params.position ? light.position.copy(params.position) : light.position.set(5, 5, 5)
  101. light.lookAt(params.target ?? new Vector3(0, 0, 0))
  102. light.name = 'Spot Light'
  103. return light
  104. },
  105. ['light-hemisphere']: (params: {
  106. color?: number,
  107. intensity?: number,
  108. name?: string,
  109. } = {})=>{
  110. const light = new HemisphereLight2(params.color ?? 0xaaaaff, 0x555443, params.intensity ?? 1)
  111. light.name = 'Hemisphere Light'
  112. return light
  113. },
  114. ['light-rect-area']: (params: {
  115. color?: number,
  116. intensity?: number,
  117. position?: Vector3,
  118. target?: Vector3,
  119. name?: string,
  120. } = {})=>{
  121. const light = new RectAreaLight2(params.color ?? 0x000ff, params.intensity ?? 3, 2, 2)
  122. params.position ? light.position.copy(params.position) : light.position.set(5, 5, 5)
  123. light.lookAt(params.target ?? new Vector3(0, 0, 0))
  124. light.name = 'Rect Area Light'
  125. return light
  126. },
  127. }
  128. constructor() {
  129. super()
  130. this._selectedType = Object.keys(this.generators)[0]
  131. }
  132. }