lume
Version:
58 lines (51 loc) • 1.74 kB
text/typescript
import {stringAttribute} from '@lume/element'
import 'element-behaviors'
import {MeshLambertMaterial} from 'three/src/materials/MeshLambertMaterial.js'
import {behavior} from '../../Behavior.js'
import {receiver} from '../../PropReceiver.js'
import {MaterialBehavior, type MaterialBehaviorAttributes} from './MaterialBehavior.js'
export type LambertMaterialBehaviorAttributes = MaterialBehaviorAttributes | 'texture' | 'specularMap'
/**
* @behavior lambert-material
* @class LambertMaterialBehavior -
* The `lambert-material` behavior gives any mesh a [Lambertian lighting model](https://en.wikipedia.org/wiki/Lambertian_reflectance)
* for its material. It uses a
* [THREE.MeshLambertMaterial](https://threejs.org/docs/index.html?q=lambert#api/en/materials/MeshLambertMaterial) under the hood.
*
* ## Example
*
* <live-code id="liveExample"></live-code>
* <script>
* liveExample.content = meshExample({material: 'lambert', color: 'skyblue'})
* </script>
*
* @extends MaterialBehavior
*/
export
class LambertMaterialBehavior extends MaterialBehavior {
texture = ''
specularMap = ''
override _createComponent() {
return new MeshLambertMaterial({color: 0x00ff00})
}
override connectedCallback() {
super.connectedCallback()
this._handleTexture(
() => this.texture,
(mat, tex) => (mat.map = tex),
mat => !!mat.map,
() => {},
true,
)
this._handleTexture(
() => this.specularMap,
(mat, tex) => (mat.specularMap = tex),
mat => !!mat.specularMap,
() => {},
true,
)
}
}
if (globalThis.window?.document && !elementBehaviors.has('lambert-material'))
elementBehaviors.define('lambert-material', LambertMaterialBehavior)