-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
ShapeGeometry.jsx
164 lines (148 loc) · 4.4 KB
/
ShapeGeometry.jsx
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
import React, { useState, useMemo } from "react";
import { lighten, darken, complement } from "polished";
import { Plane } from "three";
import { Positionner, Box } from "./Positionner.jsx";
import ClipPlane from "./ClipPlane.jsx";
import {
useWrappedFaceEvent,
useWrappedEdgeEvent,
ReplicadFacesMesh,
ReplicadEdgesMesh,
} from "./replicadMesh.jsx";
const colorVariants = (baseColor = "#5a8296") => {
return {
base: baseColor,
line: darken(0.2, baseColor),
selected: lighten(0.15, baseColor),
lineselected: lighten(0.25, baseColor),
sideColor: complement(baseColor),
};
};
const ShapeGeometry = ({
shape,
onFaceClick,
onEdgeClick,
facesHighlight,
edgesHighlight,
LineMaterial = "lineBasicMaterial",
FaceMaterial = "meshBasicMaterial",
clipDirection = null,
clipConstant = 0,
}) => {
const fOnClick = useWrappedFaceEvent(onFaceClick);
const eOnClick = useWrappedEdgeEvent(onEdgeClick);
const colors = colorVariants(shape.color);
const transparent = shape.opacity && shape.opacity !== 1;
const clippingPlane = useMemo(() => {
if (!clipDirection) return null;
const plane = new Plane();
plane.normal.set(...clipDirection);
plane.constant = clipConstant;
return plane;
}, [clipDirection, clipConstant]);
return (
<ClipPlane sideColor={colors.sideColor} clippingPlane={clippingPlane}>
{FaceMaterial && (
<ReplicadFacesMesh
faces={shape.mesh}
defaultHighlight={shape.highlight}
highlight={facesHighlight}
onClick={fOnClick}
>
<FaceMaterial
attach="material-0"
transparent={transparent}
opacity={shape.opacity}
color={colors.base}
polygonOffset
polygonOffsetFactor={2.0}
polygonOffsetUnits={1.0}
/>
<FaceMaterial
attach="material-1"
transparent={transparent}
opacity={shape.opacity}
color={colors.selected}
polygonOffset
polygonOffsetFactor={2.0}
polygonOffsetUnits={1.0}
/>
</ReplicadFacesMesh>
)}
{shape.edges && LineMaterial && (
<ReplicadEdgesMesh
edges={shape.edges}
defaultHighlight={shape.highlight}
highlight={edgesHighlight}
onClick={eOnClick}
>
<LineMaterial
attach="material-0"
transparent={transparent}
opacity={shape.opacity}
color={colors.line}
/>
<LineMaterial
attach="material-1"
transparent={transparent}
opacity={shape.opacity}
color={colors.lineselected}
/>
</ReplicadEdgesMesh>
)}
</ClipPlane>
);
};
export default ShapeGeometry;
const useSelection = (selectMode, validSelectModes) => {
const [selection, setSelection] = useState(null);
if (!validSelectModes.includes(selectMode)) return [selection, () => null];
const select = (shapeId) => (e, index) => {
if (
selection &&
selection.shapeId === shapeId &&
selection.index === index
) {
setSelection(null);
} else {
setSelection({ shapeId, index });
}
};
return [selection, select];
};
const highlight = (selection, shapeId) => {
return selection && shapeId === selection.shapeId ? selection : null;
};
export function ShapeGeometries({
shapes,
selectMode = "none",
disableAutoPosition = false,
LineMaterial = "lineBasicMaterial",
FaceMaterial = "meshBasicMaterial",
}) {
const [selectedFace, selectFace] = useSelection(selectMode, ["all", "faces"]);
const [selectedEdge, selectEdge] = useSelection(selectMode, ["all", "edges"]);
const P = disableAutoPosition ? React.Fragment : Positionner;
return (
<P>
{shapes.map((shape) => {
const faceHighlight = highlight(selectedFace, shape.name);
const edgeHighlight = highlight(selectedEdge, shape.name);
return (
<Box key={shape.name}>
<ShapeGeometry
shape={shape}
baseColor={shape.color}
edgesHighlight={edgeHighlight}
facesHighlight={faceHighlight}
LineMaterial={LineMaterial}
FaceMaterial={FaceMaterial}
onFaceClick={selectFace(shape.name)}
onEdgeClick={selectEdge(shape.name)}
/>
</Box>
);
})}
</P>
);
}