Skip to content

Commit 852db52

Browse files
authored
feat: allow some customization of highligter component (#68)
1 parent ee73930 commit 852db52

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/components/Handlers/Highlighter/Highlighter.tsx

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
import { useMemo } from 'react'
2+
import { AdditiveBlending, Blending, Color, DoubleSide, InstancedMesh, Line, Matrix4, Mesh, MeshBasicMaterial } from 'three'
23
import { useHighlightState } from './highlight-state'
3-
import { AdditiveBlending, DoubleSide, InstancedMesh, Line, Matrix4, Mesh, MeshBasicMaterial } from 'three'
4-
5-
const mat = new MeshBasicMaterial({
6-
color: 0x404040,
7-
depthTest: true,
8-
depthWrite: false,
9-
transparent: true,
10-
opacity: 1,
11-
blending: AdditiveBlending,
12-
side: DoubleSide,
13-
})
144

155
const instanceMatrix = new Matrix4()
166

7+
export type HighlighterProps = {
8+
color?: string | number | Color,
9+
blending?: Blending,
10+
}
11+
1712
/**
1813
* Adds highlighting to rendered objects by rendering a "ghost" object on top it.
1914
*
@@ -30,9 +25,24 @@ const instanceMatrix = new Matrix4()
3025
*
3126
* @group Components
3227
*/
33-
export const Highlighter = () => {
28+
export const Highlighter = ({
29+
color,
30+
blending,
31+
}: HighlighterProps) => {
3432
const { highlighted } = useHighlightState()
3533

34+
const material = useMemo(() => {
35+
return new MeshBasicMaterial({
36+
color: color || 0x404040,
37+
depthTest: true,
38+
depthWrite: false,
39+
transparent: true,
40+
opacity: 1,
41+
blending: blending || AdditiveBlending,
42+
side: DoubleSide,
43+
})
44+
}, [color, blending])
45+
3646
const highlightObjects = useMemo(() => {
3747
return highlighted.map(item => {
3848
let primitiveObject: Mesh | Line | InstancedMesh
@@ -41,31 +51,30 @@ export const Highlighter = () => {
4151
if (item.object instanceof InstancedMesh) {
4252
const instanced = item.object as InstancedMesh
4353
if (item.instanceIndex) {
44-
primitiveObject = new Mesh(geometry, mat)
54+
primitiveObject = new Mesh(geometry, material)
4555
item.object.getWorldPosition(primitiveObject.position)
4656
instanced.getMatrixAt(item.instanceIndex, instanceMatrix)
4757
primitiveObject.applyMatrix4(instanceMatrix)
4858
} else {
49-
const imesh = new InstancedMesh(geometry, mat, instanced.count)
59+
const imesh = new InstancedMesh(geometry, material, instanced.count)
5060
item.object.getWorldPosition(imesh.position)
5161
imesh.instanceMatrix = instanced.instanceMatrix
5262
primitiveObject = imesh
5363
}
54-
}
64+
}
5565
else if (item.object instanceof Mesh) {
56-
primitiveObject = new Mesh(geometry, mat)
66+
primitiveObject = new Mesh(geometry, material)
5767
item.object.getWorldPosition(primitiveObject.position)
5868
} else {
59-
primitiveObject = new Line(geometry, mat)
69+
primitiveObject = new Line(geometry, material)
6070
item.object.getWorldPosition(primitiveObject.position)
6171
}
6272
primitiveObject.visible = item.object.visible
63-
6473

65-
6674
return primitiveObject
6775
})
68-
}, [highlighted])
76+
}, [highlighted, material])
77+
6978

7079
return <group renderOrder={999}>
7180
{highlightObjects.map(po => (

0 commit comments

Comments
 (0)