Currently, I am in the process of developing a Minecraft-inspired game using Three.js.
At the moment, my setup involves adding cubes to a scene along with a Raycaster that identifies if the pointer is hovering over any of the cubes. It then creates an indicator cube for the purpose of adding or removing a cube.
In the removal mode, the cube the pointer is on top of will be highlighted in red. Clicking on it will remove that cube. Subsequently, the Raycaster is triggered again to check if the pointer is now hovering over any cubes that were previously hidden behind the removed one. This process works smoothly.
Recently, I incorporated the feature of adding cubes with a click. When the pointer hovers over a cube, the indicator is placed next to the face where the new cube would be inserted. This functionality also works correctly.
However, an issue arises when adding a new cube. The Raycaster fails to recognize the new intersection with the newly added cube. Consequently, the indicator position is not updated immediately. Only after moving the mouse slightly does the Raycaster detect the new cube and update the indicator position.
A minimal Three.js file has been created, where pressing 1
activates "Add Mode" and pressing 2
activates "Removal Mode".
[Three.js code example]
I will provide videos demonstrating the cube removal and addition processes. It is evident that the red indicator is promptly updated when clicking in "Removal Mode", but experiences a delay in updating in "Add Mode".
I attempted a workaround by calling the checkIntersection
function on the pointerup
event, which did solve the issue temporarily. However, this is not the intended solution.
Logging the intersections
array confirmed my suspicion that the new object is not being detected by the Raycaster. I also tried manually adding the new cube to the intersections object, but it did not resolve the problem.
It is perplexing why everything functions as expected in "Removal Mode" but encounters difficulties in "Add Mode". Could this be due to the immediate removal of the object from the scene?
There seems to be a missing piece in the puzzle. Despite the challenges, I believe there must be a solution, and I am determined to find it.