threepipe
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PointLightHelper2.ts 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {ColorRepresentation, Object3D, PointLight, SphereGeometry} from 'three'
  2. import {WireframeGeometry2} from 'three/examples/jsm/lines/WireframeGeometry2.js'
  3. import {Wireframe} from 'three/examples/jsm/lines/Wireframe.js'
  4. import {onChange} from 'ts-browser-helpers'
  5. import {ALightHelperWidget} from './ALightHelperWidget'
  6. import {IUiConfigContainer, uiSlider} from 'uiconfig.js'
  7. import {LineMaterial2} from '../../core'
  8. export class PointLightHelper2 extends ALightHelperWidget {
  9. color: ColorRepresentation | undefined
  10. lightSphere: Wireframe
  11. declare light: (PointLight & IUiConfigContainer) | undefined
  12. @onChange(PointLightHelper2.prototype.update)
  13. material: LineMaterial2
  14. @onChange(PointLightHelper2.prototype.update)
  15. @uiSlider(undefined, [0.1, 20], 0.01)
  16. lineWidth = 5
  17. @onChange(PointLightHelper2.prototype.update)
  18. @uiSlider(undefined, [0.01, 10], 0.01)
  19. size = 0.5
  20. constructor(light: PointLight, size?: number, color?: ColorRepresentation) {
  21. super(light)
  22. this.color = color
  23. if (size !== undefined) this.size = size
  24. const geometry = new WireframeGeometry2(new SphereGeometry(0.5, 4, 2))
  25. this.material = new LineMaterial2({
  26. color: 0xff0000,
  27. linewidth: 0.005, // in world units with size attenuation, pixels otherwise
  28. vertexColors: false,
  29. dashed: false,
  30. alphaToCoverage: true,
  31. toneMapped: false,
  32. transparent: true,
  33. depthTest: false,
  34. depthWrite: false,
  35. })
  36. this.material.userData.renderToGBuffer = false
  37. this.material.userData.renderToDepth = false
  38. this.lightSphere = new Wireframe(geometry, this.material)
  39. this.lightSphere.computeLineDistances()
  40. this.add(this.lightSphere)
  41. this.update()
  42. this.traverse(o => {
  43. o.userData.__keepShadowDef = true
  44. o.castShadow = false
  45. o.receiveShadow = false
  46. })
  47. }
  48. dispose() {
  49. this.lightSphere.geometry.dispose()
  50. ;(this.lightSphere.material as any).dispose()
  51. super.dispose()
  52. }
  53. update() {
  54. if (!this.light || !this.lightSphere) return
  55. this.material.color.set(this.color ?? this.light.color)
  56. this.material.linewidth = this.lineWidth * 0.001
  57. this.lightSphere.scale.setScalar(this.size)
  58. super.update()
  59. }
  60. static Check(light: Object3D) {
  61. return (light as PointLight).isPointLight
  62. }
  63. static Create(light: Object3D) {
  64. return new PointLightHelper2(light as PointLight)
  65. }
  66. }