This method may not be the ultimate solution, but it serves as a good starting point.
By utilizing the THREE.GridHelper()
function, you have the ability to control the direction of its lines. By examining the source code of the helper, I discovered the sequence of points for the lines in its geometry. The key concept is to understand how each point corresponds to a specific line, enabling the manipulation of its direction. With both vertical and horizontal lines present, adjustments can be made on the x-axis for vertical lines and the z-axis for horizontal lines. The order of vertices follows this pattern:
start_point_horizontal, end_point_horizontal, start_point_vertical, end_point_vertical,...
and this pattern continues throughout.
To facilitate this process, an attribute is assigned to each vertex to determine whether it belongs to a vertical or horizontal line.
var hv = new Float32Array(plane.geometry.attributes.position.count);
for (var i = 0; i < plane.geometry.attributes.position.count; i+= 4) {
hv[i+0] = 0;
hv[i+1] = 0;
hv[i+2] = 1;
hv[i+3] = 1;
}
plane.geometry.addAttribute("horivert", new THREE.BufferAttribute(hv, 1));
Several global variables are required for this approach:
var clock = new THREE.Clock();
var direction = new THREE.Vector3();
var speed = 1;
var size = 10;
var delta = 0;
var shift = new THREE.Vector3();
var plane; // reference to our GridHelper
The animation loop brings everything together:
delta = clock.getDelta();
shift.copy(direction).normalize().multiplyScalar(delta * speed);
var arr = plane.geometry.attributes.position.array;
for(var i=0; i < plane.geometry.attributes.position.count; i++)
{
var hv = plane.geometry.attributes.horivert.array[i];
if (hv == 1) // when dealing with a point on a vertical line, we adjust it on the x-axis
{
arr[i * 3 + 0] += shift.x;
if (arr[i * 3 + 0] < -size)
arr[i * 3 + 0] = size - Math.abs(-size - arr[i * 3 + 0]);
if (arr[i * 3 + 0] > size)
arr[i * 3 + 0] = -size + Math.abs(arr[i * 3 + 0] - size);
}
else //if it's a horizontal line point, we move it along the z-axis
{
arr[i * 3 + 2] += shift.z;
if (arr[i * 3 + 2] < -size)
arr[i * 3 + 2] = size - Math.abs(-size - arr[i * 3 + 2]);
if (arr[i * 3 + 2] > size)
arr[i * 3 + 2] = -size + Math.abs(arr[i * 3 + 2] - size);
}
}
plane.geometry.attributes.position.needsUpdate = true;
If a point reaches a lower limit, it wraps around to the upper limit, and vice versa.
I created a jsfiddle example from scratch to demonstrate the implementation.