Each Vector Grid is made up of a rectangular grid of points, with each point connected by springs to its vertical and horizontal neighbors. When a point is moved from its starting position, it will drag its neightbours with it, thereby causing a cascading effect that causes the entire grid to dynamically deform.
In addition to this, certain points are also attached by springs to their starting position, causing them to naturally return to their default position provided no other forces are acting on them, which helps to maintain the overall shape of the grid.
Creating a Vector Grid
To create a Vector Grid, simply go to the main Unity menu bar and choose GameObject->Create Other->Vector Grid->Vector Grid. Alternatively, you can attach a Vector Grid component to an existing GameObject using the Component->Vector Grid menu.
Forces can be applied using the VectorGrid.AddGridForce() functions. These functions requires a force position, force strength, and force radius - the force is applied to every grid point that falls within the sphere defined by the position and radius (with the force amount reducing linearly towards the edges of the sphere).
You can choose to provide either a float force value (in which case the force is treated as an omin-directional force emanating from the force centre) or as a vector (in which case each grid point is pushed in the direction defined by the vector). Forces can also (optionally) provide colour values, which will alter the color of all points that are within the force radius.
Forces can also be applied by attaching a VectorGridForce component to any GameObject, which will cause a force to be constantly applied to the associated VectorGrid from the GameObject's position.
Forces can produce different visual effects depending on whether they are applied on a 2D plane or not (ie. the Z position of the force is the same as that of the grid points). 2D forces cause grid points to 'bunch up' around the edge of the force radius, whereas 3D forces generally cause smoother waves as the points are also able to extend along the Z axis.
Scrolling The Grid
Vector Grids can also be scrolled, allowing you to effectively create the illusion of an infinitely size Vector Grid. The code achieves this by recycling grid points from the opposite side of the grid to the direction in which it is being scrolled (ie. scrolling left will destroy grid points on the far right hand side, and vice versa). The advantage of this is that the code generates no garbage since no additional allocations are required once the initial grid has been configured. However, since there is no guarantee made that a grid point is not still on-screen when it is re-used, it is recommended to configure the grid to be larger than the visible screen space so that the user cannot see points being added or removed.
To scroll the grid, simply use the Scroll() function to scroll by a given distance. Alternatively, you can make use of the 'Tracked Camera' field in the inspector to automatically scroll the Grid as your game camera moves.
Freshly generated rows/columns will have their colors reset, so you can dynamically modify the grid's Thick Line Spawn Color/Thin Line Spawn Color variables to create a grid that appears to change color as it scrolls.
The Vector Grid code has been written with performance in mind, but ultimately the CPU cost will primarily be determined by the configuration of the grid that you are using. Bigger grids mean that the code needs to simulate more points and springs, and also that applied forces need to check over a larger array of points in order to determine which ones to affect. Reducing the grid dimensions should be your first port of call if you wish to improve performance. Also, be careful when using the Grid Smoothing Factor value, as this can drastically increase the number of triangles and vertices that the grid needs to render.
Grid Width/Grid Height
The dimensions of the grid
The horizontal/vertical spacing between grid points
Max Fog Distance/Max Fog Alpha Scale
When grid points move along the Z-axis they can optionally be faded out, which causes 'holes' to appear in the grid around areas of high force. Max Fog Distance controls the distance at which this effect is the strongest, and Max Fog Alpha Scale controls how much the alpha value is altered at this distance (1 meaning no change, 0 meaning totally transparent).
Two Way Fog
By default, fog is only applied when grid points move downwards on the Z-axis, but you can optionally also fade them out when they move forwards along the axis.
You can choose to render any Vector Grid as a plane rather than as a grid mesh, meaning that the applied material will be stretched to cover the entire mesh rather than being applied to each individual vertical/horizontal connector. This may prove useful if you wish to use a Vector Grid to simulate eg. a pool of water.
Vector Grid Point Mass
Controls how heavy each grid point is. Adjusting this will alter the behaviour of the springs.
This controls how much velocity each grid point loses every frame - ie. it controls the rate of deceleration. Lower values will cause grid points to continue moving for longer after a point is applied.
Thick Line Spawn Color/Thin Line Spawn Color/Thick Line Width /Thin Line Width/Thick Line Spacing/Thick Line Spacing
The grid can be set to spawn alternating thin and thick lines, with different colors and widths. These values control the starting color of the lines, their width, and how frequently thick lines appear vs. thin ones.
Spring Stiffness/Spring Damping
Used to control the strength and oscillation damping of the springs that connect each grid point to its neighbors.
Edge Spring Stiffness/Edge Spring Damping
Points around the edge of the grid are anchored to their starting position in order to preseve the outline of the grid. These values control the strength and oscillation damping of these springs.
Optionally, you can set the points around the edge of the grid to be completely static, and unaffected by any force.
Loose Spring Stiffness/Loose Spring Damping
Loose springs are placed on certain grid points in order to anchor them to their starting positions. This means that they will naturally return to their starting position over time, and therefore maintain the overall shape of the grid. These values control the strength and oscillation damping of these springs.
Loose Spring Interval
Controls how often loose springs are generated. eg. A value of 3 means that a loose spring will be placed at the intersection of every 3rd row and 3rd column. Altering this value can make the overall grid more or less stable.
Rest Distance Scalar
Each spring tries to the two grid points attached to it at a certain distance from one another. By default, this is just the distance that they were before any forces were applied, but the Rest Distance Scalar value allows you to scale this down, effectively creating a more taut grid.
Grid Smoothing Factor
Controls how many subdivisions are generated on the line connecting a grid point and its neighbor. The mesh rendering uses Catmull-Rom interpolation to generate curved lines connecting the points, resulting in smoother deformations at the expense of more vertices/triangles and CPU cost.
Turns on debug drawing - renders the mesh using basic debug lines.
Turn this on if you need normals to be calculated for the mesh
Assign a camera to this field and the mesh will automatically scroll to match the movement of the camera.
When using a tracked camera, this value controls whether the grid will scroll faster or slower than the camera's movement, providing a parallax effect.
Allow Horizontal Scroll/Allow Vertical Scroll
Allows you to disable horizontal or vertical scrolling if eg. you are using a tracked camera and only wish the grid to be able to scroll in a single direction.
Enable update time diagnostics - displays some debug info about CPU usage in the scene view.