threepipe
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DirectionalLight2.ts 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {Color, ColorRepresentation, DirectionalLight, DirectionalLightShadow, Euler, Vector2, Vector3} from 'three'
  2. import {ILight} from './ILight'
  3. import {iLightCommons} from '../object/iLightCommons'
  4. import {IObject3D} from '../IObject'
  5. import {uiColor, uiFolderContainer, uiNumber, UiObjectConfig, uiSlider, uiToggle, uiVector} from 'uiconfig.js'
  6. import {onChange2, onChange3} from 'ts-browser-helpers'
  7. import {bindToValue} from '../../three'
  8. /**
  9. * Extension of three.js DirectionalLight with additional properties for serialization and UI
  10. * A directional light is a light source that has a position but no dimensions - a single point in space that emits light in a specific direction.
  11. *
  12. * Note - gltf serialization is handled by {@link GLTFLightExtrasExtension}
  13. *
  14. * @category Lights
  15. */
  16. // todo: add Light section in the readme detailing these ...2 lights
  17. @uiFolderContainer('Directional Light')
  18. export class DirectionalLight2 extends DirectionalLight implements ILight<DirectionalLightShadow> {
  19. assetType = 'light' as const
  20. setDirty = iLightCommons.setDirty
  21. refreshUi = iLightCommons.refreshUi
  22. declare uiConfig: UiObjectConfig
  23. readonly isDirectionalLight2 = true
  24. @uiToggle('Enabled')
  25. @onChange3('setDirty')
  26. declare visible: boolean
  27. @uiColor('Color', (that: DirectionalLight2)=>({onChange: ()=>that.setDirty()}))
  28. declare color: Color
  29. @uiSlider('Intensity', [0, 100], 0.01)
  30. @onChange3('setDirty')
  31. declare intensity: number
  32. @uiVector('Position', undefined, undefined, (that: DirectionalLight2)=>({onChange: ()=>that.setDirty()}))
  33. declare readonly position: Vector3
  34. @uiVector('Rotation', undefined, undefined, (that: DirectionalLight2)=>({onChange: ()=>that.setDirty()}))
  35. declare readonly rotation: Euler
  36. @uiToggle('Cast Shadow')
  37. @onChange3('setDirty')
  38. declare castShadow: boolean
  39. @uiVector('Shadow Map Size')
  40. @bindToValue({obj: 'shadow', key: 'mapSize', onChange: DirectionalLight2.prototype._mapSizeChanged, onChangeParams: false})
  41. shadowMapSize: Vector2
  42. protected _mapSizeChanged() {
  43. this.shadow.map?.dispose()
  44. this.shadow.mapPass?.dispose()
  45. this.shadow.map = null as any
  46. this.shadow.mapPass = null as any
  47. this.setDirty({change: 'shadowMapSize'})
  48. }
  49. @uiSlider('Shadow Bias', [-0.001, 0.001], 0.00001)
  50. @bindToValue({obj: 'shadow', key: 'bias', onChange: 'setDirty'})
  51. shadowBias: number
  52. @uiSlider('Shadow Normal Bias', [-0.1, 0.1], 0.005)
  53. @bindToValue({obj: 'shadow', key: 'normalBias', onChange: 'setDirty'})
  54. shadowNormalBias: number
  55. @uiSlider('Shadow Radius', [0, 5], 0.01)
  56. @bindToValue({obj: 'shadow', key: 'radius', onChange: 'setDirty'})
  57. shadowRadius: number
  58. @uiSlider('Shadow Frustum', [0.1, 50], 0.01)
  59. @onChange2(DirectionalLight2.prototype._shadowFrustumChanged)
  60. shadowFrustum: number
  61. @uiNumber('Shadow Near')
  62. @bindToValue({obj: 'shadow', key: ['camera', 'near'], onChange: DirectionalLight2.prototype._shadowCamUpdate})
  63. shadowNear: number
  64. @uiNumber('Shadow Far')
  65. @bindToValue({obj: 'shadow', key: ['camera', 'far'], onChange: DirectionalLight2.prototype._shadowCamUpdate})
  66. shadowFar: number
  67. protected _shadowFrustumChanged() {
  68. const v = this.shadowFrustum
  69. this.shadow.camera.left = -v / 2
  70. this.shadow.camera.right = v / 2
  71. this.shadow.camera.top = v / 2
  72. this.shadow.camera.bottom = -v / 2
  73. this.shadow.camera.updateProjectionMatrix()
  74. this.setDirty({change: 'shadowFrustum'})
  75. }
  76. protected _shadowCamUpdate(change?: string) {
  77. this.shadow.camera.updateProjectionMatrix()
  78. this.setDirty({change})
  79. }
  80. constructor(color?: ColorRepresentation, intensity?: number) {
  81. super(color, intensity)
  82. this.target.position.set(0, 0, -1) // because of GLTF spec: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_lights_punctual
  83. this.add(this.target) // todo: make sure the child isn't exported in gltf
  84. iLightCommons.upgradeLight.call(this)
  85. this.shadowFrustum = 10
  86. }
  87. autoScale() {
  88. console.warn('DirectionalLight2: AutoScale not supported on Lights')
  89. return this
  90. }
  91. autoCenter() {
  92. console.warn('DirectionalLight2: AutoCenter not supported on Lights')
  93. return this
  94. }
  95. /**
  96. * @deprecated use `this` instead
  97. */
  98. get lightObject(): this {
  99. return this
  100. }
  101. /**
  102. * @deprecated use `this` instead
  103. */
  104. get modelObject(): this {
  105. return this
  106. }
  107. // region inherited type fixes
  108. // re-declaring from IObject3D because: https://github.com/microsoft/TypeScript/issues/16936
  109. traverse: (callback: (object: IObject3D) => void) => void
  110. traverseVisible: (callback: (object: IObject3D) => void) => void
  111. traverseAncestors: (callback: (object: IObject3D) => void) => void
  112. getObjectById: <T extends IObject3D = IObject3D>(id: number) => T | undefined
  113. getObjectByName: <T extends IObject3D = IObject3D>(name: string) => T | undefined
  114. getObjectByProperty: <T extends IObject3D = IObject3D>(name: string, value: string) => T | undefined
  115. copy: (source: DirectionalLight|IObject3D, recursive?: boolean, ...args: any[]) => this
  116. clone: (recursive?: boolean) => this
  117. remove: (...object: IObject3D[]) => this
  118. // dispatchEvent: (event: ILightEvent) => void
  119. declare parent: IObject3D | null
  120. declare children: IObject3D[]
  121. // endregion
  122. }