Repository navigation
Expand file tree
/
Copy pathCells.js
More file actions
227 lines (177 loc) · 7.09 KB
/
Copy pathCells.js
File metadata and controls
227 lines (177 loc) · 7.09 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import * as React from 'react';
import {useEffect, useRef, useMemo, useState, createContext} from 'react';
import {a} from '@react-spring/three';
import {useAnimationHook, useGenerateMazeHook, useSolver, GenerateMazeHook} from './LayoutsFunctions';
import {directions} from '../Algorithms/Maze/Directions';
import {_FindCell} from '../Algorithms/Maze/Tools';
import board from './Board';
import {BinaryTreeCreation} from '../Algorithms/Maze/Generation/BinaryTreeCreation';
import {
FLOOR_TYPE, FLOOR_COLOR, WALL_TYPE, WALL_COLOR,
getColor, GOAL_TYPE, START_TYPE, SELECTED_COLOR,
BURN_COLOR, PATH_TYPE, PATH_COLOR,
} from '../../Utility/Colors';
// import {BufferPoints} from './Point';
import * as THREE from 'three';
// import {PointEvent} from './Point';
const tempOBJ = new THREE.Object3D();
const tempCOLOR = new THREE.Color();
function updateInstancedMeshMatrices({mesh, board, colorAttrib, colorArray}) {
if (!mesh) {
return;
}
// const numPoints = board.length;
// transform mesh to world space
for (let i = 0; i < board.length; ++i) {
const {x, y, z} = board[i];
tempOBJ.position.set(x, y, z);
tempOBJ.rotation.set(0.5 * Math.PI, 0, 0); // Look at the origin
tempCOLOR.set(getColor(board[i].type));
tempCOLOR.toArray(colorArray, i * 3);
tempOBJ.updateMatrix();
mesh.setMatrixAt(i, tempOBJ.matrix);
}
colorAttrib.current.needsUpdate = true;
mesh.instanceMatrix.needsUpdate = true;
}
const usePointColorsHook = ({board}, selectedPoint) => {
const numPoints = board.length;
const colorAttrib = React.useRef();
const colorArray = React.useMemo(() => new Float32Array(numPoints * 3), [
numPoints,
]);
useEffect(() => {
for (let i = 0; i < board.length; ++i) {
tempCOLOR.set(
board[i] === selectedPoint ? SELECTED_COLOR : getColor(board[i].type),
);
tempCOLOR.toArray(colorArray, i * 3);
}
colorAttrib.current.needsUpdate = true;
}, [board, selectedPoint, colorArray]);
return {colorAttrib, colorArray};
};
const useMouseClickHook = ({board, selectedPoint, onSelectPoint}) => {
const onMouseDownHandler = useRef([0, 0]); // Record and maintain the handler for the mouse down event
const onMouseUpHandler = useRef([0, 0]); // Record and maintain the handler for the mouse up event
const onMouseMoveHandler = useRef([0, 0]); // Record and maintain the handler for the mouse move event
/*
* Pointer Click Down Position
*/
const setDownPointerCoord = event => {
onMouseDownHandler.current[0] = event.clientX; // Mouseclick x coordinate
onMouseDownHandler.current[1] = event.clientY; // Mouseclick y coordinate
};
/*
* If the mouse has clicked on a point, if that point is occupied by an
* interactable object, gather its reference.
*/
const getClickTarget = event => {
const {instanceId, clientX, clientY} = event;
const travelDistance = Math.sqrt(
Math.pow(onMouseDownHandler.current[0] - clientX, 2)
+
Math.pow(onMouseDownHandler.current[1] - clientY, 2),
);
// Prevents repeat calls to click handler unless drag selection is initiated.
if (travelDistance > 50) {
/* if (!useDrag) { */
event.stopPropagation();
return;
// }
}
const point = board[instanceId];
console.log(board[instanceId]);
console.log('Point clicked: ', point);
// toggle the point
if (point === selectedPoint) {
onSelectPoint(null);
}
else {
onSelectPoint(point);
}
};
return {setDownPointerCoord, getClickTarget};
};
const Cells = ({board, layoutType, solving, algorithm, mazeType, selectedPoint, onSelectPoint /*, useDrag */}) => {
const meshRef = useRef();
const [activeRef, setActive] = useState(false);
const numPoints = board.length;
// @Test
board[100].type = GOAL_TYPE;
board[1].type = START_TYPE;
useGenerateMazeHook({board, mazeType});
useAnimationHook({board, layoutType});
const {solvingProgress} = useSolver({
board,
algorithm,
solving,
onFrame: () => {
updateInstancedMeshMatrices({mesh: meshRef.current, board});
},
});
useEffect(() => {
console.log('Board updated');
updateInstancedMeshMatrices({mesh: meshRef.current, board, colorAttrib, colorArray});
}, [board, layoutType, mazeType]);
const {getClickTarget, setDownPointerCoord} = useMouseClickHook({
board,
selectedPoint,
onSelectPoint,
});
const {colorAttrib, colorArray} = usePointColorsHook({board}, selectedPoint);
return (
<group>
<instancedMesh
ref = {meshRef}
args = {[null, null, numPoints]}
frustumCulled = {false}
onClick = {getClickTarget}
onPointerDown = {setDownPointerCoord}
>
<boxBufferGeometry attach = "geometry" args = {[0.6, 0.3, 0.6, 18]}>
<instancedBufferAttribute
ref = {colorAttrib}
attachObject = {['attributes', 'color']}
array = {colorArray}
itemSize = {3}
/>
</boxBufferGeometry>
<meshStandardMaterial
attach = "material"
vertexColors = {THREE.VertexColors}
/>
{/* {generationInProgress && <a.group */}
{/* position = {generationInProgress.interpolate((x, y, z) => [x, y, z])} */}
{/* />} */}
{/* <BufferPoints numPoints={numPoints} /> */}
{/* <BufferPoints numPoints = {numPoints}/> */}
{selectedPoint && (
<a.group
position = {[
selectedPoint.x,
selectedPoint.y,
selectedPoint.z,
]}
>
<pointLight
distance = {6}
position = {[0, 0, 0.6]}
decay = {30} // Fast defusion of light. Drop light intensity by half each second. (30 = 1/2^30). No directly select_color leaves the box.
intensity = {155}
color = {SELECTED_COLOR}
/>
<pointLight
distance = {12} // With decay: 1, distance = 5 boxes in each direction
position = {[0, 0.11, 0.051]}
decay = {10} // Slow defusion of light
intensity = {20}
color = {BURN_COLOR}
/>
</a.group>
)}
</instancedMesh>
</group>
);
};
export default Cells;