threepipe
Version:
A modern 3D viewer framework built on top of three.js, written in TypeScript, designed to make creating high-quality, modular, and extensible 3D experiences on the web simple and enjoyable.
149 lines (125 loc) • 5.48 kB
text/typescript
import {Color, ColorRepresentation, DirectionalLight, DirectionalLightShadow, Euler, Vector2, Vector3} from 'three'
import {ILight} from './ILight'
import {iLightCommons} from '../object/iLightCommons'
import {IObject3D} from '../IObject'
import {generateUiConfig, uiColor, uiInput, uiNumber, UiObjectConfig, uiSlider, uiToggle, uiVector} from 'uiconfig.js'
import {onChange2, onChange3} from 'ts-browser-helpers'
import {bindToValue} from '../../three'
import {objectActionsUiConfig} from '../object/IObjectUi'
/**
* Extension of three.js DirectionalLight with additional properties for serialization and UI
* 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.
*
* Note - gltf serialization is handled by {@link GLTFLightExtrasExtension}
*
* @category Lights
*/
export class DirectionalLight2 extends DirectionalLight implements ILight<DirectionalLightShadow>, IObject3D {
assetType = 'light' as const
setDirty = iLightCommons.setDirty
refreshUi = iLightCommons.refreshUi
uiConfig: UiObjectConfig = {
type: 'folder',
label: 'Directional Light',
children: [
...generateUiConfig(this),
...objectActionsUiConfig.call(this),
],
}
readonly isDirectionalLight2 = true
('Enabled')
('setDirty')
declare visible: boolean
('Name')
('setDirty')
declare name: string
('Color', (that: DirectionalLight2)=>({onChange: ()=>that.setDirty()}))
declare color: Color
('Intensity', [0, 100], 0.01)
('setDirty')
declare intensity: number
('Position', undefined, undefined, (that: DirectionalLight2)=>({onChange: ()=>that.setDirty()}))
declare readonly position: Vector3
('Rotation', undefined, undefined, (that: DirectionalLight2)=>({onChange: ()=>that.setDirty()}))
declare readonly rotation: Euler
('Cast Shadow')
('setDirty')
declare castShadow: boolean
('Shadow Map Size')
({obj: 'shadow', key: 'mapSize', onChange: DirectionalLight2.prototype._mapSizeChanged, onChangeParams: false})
shadowMapSize: Vector2
protected _mapSizeChanged() {
this.shadow.map?.dispose()
this.shadow.mapPass?.dispose()
this.shadow.map = null as any
this.shadow.mapPass = null as any
this.setDirty({change: 'shadowMapSize'})
}
('Shadow Bias', [-0.001, 0.001], 0.00001)
({obj: 'shadow', key: 'bias', onChange: 'setDirty'})
shadowBias: number
('Shadow Normal Bias', [-0.1, 0.1], 0.005)
({obj: 'shadow', key: 'normalBias', onChange: 'setDirty'})
shadowNormalBias: number
('Shadow Radius', [0, 5], 0.01)
({obj: 'shadow', key: 'radius', onChange: 'setDirty'})
shadowRadius: number
('Shadow Frustum')
(DirectionalLight2.prototype._shadowFrustumChanged)
shadowFrustum: number
('Shadow Near')
({obj: 'shadow', key: ['camera', 'near'], onChange: DirectionalLight2.prototype._shadowCamUpdate})
shadowNear: number
('Shadow Far')
({obj: 'shadow', key: ['camera', 'far'], onChange: DirectionalLight2.prototype._shadowCamUpdate})
shadowFar: number
protected _shadowFrustumChanged() {
const v = this.shadowFrustum
this.shadow.camera.left = -v / 2
this.shadow.camera.right = v / 2
this.shadow.camera.top = v / 2
this.shadow.camera.bottom = -v / 2
this.shadow.camera.updateProjectionMatrix()
this.setDirty({change: 'shadowFrustum'})
}
protected _shadowCamUpdate(change?: string) {
this.shadow.camera.updateProjectionMatrix()
this.setDirty({change})
}
constructor(color?: ColorRepresentation, intensity?: number) {
super(color, intensity)
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
this.add(this.target) // todo: make sure the child isn't exported in gltf
iLightCommons.upgradeLight.call(this)
this.shadowFrustum = 10
}
autoScale() {
console.warn('DirectionalLight2: AutoScale not supported on Lights')
}
autoCenter() {
console.warn('DirectionalLight2: AutoCenter not supported on Lights')
}
/**
* @deprecated use `this` instead
*/
get lightObject(): this {
return this
}
/**
* @deprecated use `this` instead
*/
get modelObject(): this {
return this
}
// region inherited type fixes
// re-declaring from IObject3D because: https://github.com/microsoft/TypeScript/issues/16936
declare traverse: (callback: (object: IObject3D) => void) => void
declare traverseVisible: (callback: (object: IObject3D) => void) => void
declare traverseAncestors: (callback: (object: IObject3D) => void) => void
declare getObjectById: (id: number) => IObject3D | undefined
declare getObjectByName: (name: string) => IObject3D | undefined
declare getObjectByProperty: (name: string, value: string) => IObject3D | undefined
declare parent: IObject3D | null
declare children: IObject3D[]
// endregion
}