Skip to content

Lerp Material Properties

Description

Animating material properties is possible. In this example, if you hover over the polygons, then the color, plus several other material properties are lerped to new settings.

You can also click the polygon, and the scale will either increase or decrease depending on how it was left before the last click.

If you preferred to use tween, then I also have a tweening version.

<>

Example Script

./src/client/client.ts

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import Stats from 'three/examples/jsm/libs/stats.module'

function lerp(x: number, y: number, a: number): number {
    const r = (1 - a) * x + a * y
    return r < 0.001 ? 0 : r
}

class pickable extends THREE.Mesh {
    hovered = false
    clicked = false
    originalColor: THREE.Color
    colorTo = new THREE.Color(0xff2244)

    constructor(geometry: THREE.BufferGeometry, material: THREE.Material) {
        super()
        this.geometry = geometry
        this.material = material
        this.originalColor = (
            material as THREE.MeshPhysicalMaterial
        ).color.clone()
        this.castShadow = true
    }

    update(delta: number): void {
        this.rotation.x += delta
        this.rotation.y += delta
        const m = this.material as THREE.MeshPhysicalMaterial
        this.hovered
            ? (m.color.lerp(this.colorTo, 0.1),
              (m.thickness = lerp(m.thickness, 3, 0.1)),
              (m.reflectivity = lerp(m.reflectivity, 1, 0.1)),
              (m.roughness = lerp(m.roughness, 0.1, 0.1)),
              (m.clearcoat = lerp(m.clearcoat, 0.1, 0.1)),
              (m.transmission = lerp(m.transmission, 0.99, 0.1)),
              (m.ior = lerp(m.ior, 1.1, 0.1)))
            : (m.color.lerp(this.originalColor, 0.1),
              (m.thickness = lerp(m.thickness, 0, 0.1)),
              (m.reflectivity = lerp(m.reflectivity, 0, 0.1)),
              (m.roughness = lerp(m.roughness, 1.0, 0.1)),
              (m.clearcoat = lerp(m.clearcoat, 0, 0.1)),
              (m.transmission = lerp(m.transmission, 0, 0.1)),
              (m.ior = lerp(m.ior, 1.5, 0.1)))
        this.clicked
            ? this.scale.set(
                  lerp(this.scale.x, 1.5, 0.1),
                  lerp(this.scale.y, 1.5, 0.1),
                  lerp(this.scale.z, 1.5, 0.1)
              )
            : this.scale.set(
                  lerp(this.scale.x, 1.0, 0.1),
                  lerp(this.scale.y, 1.0, 0.1),
                  lerp(this.scale.z, 1.0, 0.1)
              )
    }
}

const raycaster = new THREE.Raycaster()
const pickables: pickable[] = []
let intersects: THREE.Intersection[]

const scene = new THREE.Scene()

const spotLight = new THREE.SpotLight(0xffffff, 500)
spotLight.position.set(5, 5, 5)
spotLight.angle = 0.3
spotLight.penumbra = 0.5
spotLight.castShadow = true
spotLight.shadow.mapSize.width = 512
spotLight.shadow.mapSize.height = 512
spotLight.shadow.bias = -0.001
spotLight.shadow.radius = 20
spotLight.shadow.blurSamples = 10
spotLight.shadow.camera.far = 15
scene.add(spotLight)

const camera = new THREE.PerspectiveCamera(
    75,
    window.innerWidth / window.innerHeight,
    0.1,
    100
)
camera.position.y = 2
camera.position.z = 4

const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.shadowMap.enabled = true
renderer.shadowMap.type = THREE.VSMShadowMap
renderer.domElement.addEventListener('pointerdown', onClick, false)

document.body.appendChild(renderer.domElement)
document.addEventListener('mousemove', onDocumentMouseMove, false)

const controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true

const envTexture = new THREE.CubeTextureLoader().load([
    'img/px_25.jpg',
    'img/nx_25.jpg',
    'img/py_25.jpg',
    'img/ny_25.jpg',
    'img/pz_25.jpg',
    'img/nz_25.jpg',
])
envTexture.mapping = THREE.CubeReflectionMapping
scene.environment = envTexture

const cube = new pickable(
    new THREE.BoxGeometry(),
    new THREE.MeshPhysicalMaterial({ color: 0xff8800 })
)
cube.position.set(-2, 0, 0)
scene.add(cube)
pickables.push(cube)

const cylinder = new pickable(
    new THREE.CylinderGeometry(0.66, 0.66),
    new THREE.MeshPhysicalMaterial({ color: 0x008800 })
)
scene.add(cylinder)
pickables.push(cylinder)

const pyramid = new pickable(
    new THREE.TetrahedronGeometry(),
    new THREE.MeshPhysicalMaterial({ color: 0x0088ff })
)
pyramid.position.set(2, 0, 0)
scene.add(pyramid)
pickables.push(pyramid)

const floor = new THREE.Mesh(
    new THREE.PlaneGeometry(10, 10),
    new THREE.MeshPhysicalMaterial()
)
floor.rotateX(-Math.PI / 2)
floor.position.y = -1.25
floor.receiveShadow = true
scene.add(floor)

const mouse = new THREE.Vector2()
function onDocumentMouseMove(event: MouseEvent) {
    mouse.set(
        (event.clientX / renderer.domElement.clientWidth) * 2 - 1,
        -(event.clientY / renderer.domElement.clientHeight) * 2 + 1
    )
    raycaster.setFromCamera(mouse, camera)
    pickables.forEach((p) => (p.hovered = false))
    intersects = raycaster.intersectObjects(pickables, false)
    if (intersects.length) (intersects[0].object as pickable).hovered = true
}

function onClick() {
    raycaster.setFromCamera(mouse, camera)
    intersects = raycaster.intersectObjects(pickables, false)
    intersects.forEach((i) => {
        ;(i.object as pickable).clicked = !(i.object as pickable).clicked
    })
}

const stats = new Stats()
document.body.appendChild(stats.dom)

const clock = new THREE.Clock()
let delta = 0

function render() {
    renderer.render(scene, camera)
}

function animate() {
    requestAnimationFrame(animate)

    controls.update()

    delta = clock.getDelta()
    pickables.forEach((p) => {
        p.update(delta)
    })

    render()

    stats.update()
}

function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight
    camera.updateProjectionMatrix()
    renderer.setSize(window.innerWidth, window.innerHeight)
    render()
}
window.addEventListener('resize', onWindowResize, false)

animate()

Comments