Grass Deformation

I developed a solution for the real-time long-lasting grass deformation according to the vehicles' and characters' movements.

In real-world battlefields, vehicle tracks are dangerous as they can reveal position to enemies. Thus, a feature to simulate this behavior is widely imperative for military training. However, in large-scale terrain, keeping the grass deformed for hours or even days of simulation can be incredibly memory-intensive. For this reason, this functionality was denied many times to the client and also drove my determination to seek a solution.

  • Long-lasting grass deformation, supporting hours or even days of simulation;
  • Control restoration times based on the type of collider, such as vehicles or humans;
  • Scalable for numerous colliders;
  • Deformation computed on GPU side using compute-shaders;
  • Advanced data structures and compression techniques for efficient memory management, resulting in memory savings of over 90%;
  • Seamless terrain integration, enhancing the visuals for fly-cameras;
  • GPU-instancing to optimize the grass rendering;

Demonstration of multiple colliders interacting with plants (on the left) and different restoration times for grass interacting with the tank’s base or tracks (on the right).




Solution

The approach involves dividing the scenarios into smaller sections using a quadtree and implementing a vector field to determine the direction of plants for each node. The vector fields are updated accordingly to objects move through the scenario. For performance purposes, complex 3D objects are approximated by planes and spheres (image below). When the colliders interact with the grass, vector fields are updated and the cost is assigned to restore each vector, allowing the simulation of different behaviors. For example, if a human disrupts the grass, it can quickly regain its shape, whereas if a vehicle damages the vector field, it may take longer to restore it (you can see both of these behaviors in the accompanying video).

Nature


In the vertex shader, grasses and small shrubs sample the vector fields to identify their orientation based on the interpolation of the closest vectors. Below we have a color debug showing the direction of the vectors in the vector fields after moving a vehicle. Also, we have the result after rendering, showing the vehicle’s path.

Italy
Italy
Italy
Italy


In the fragment shader stage to texture the terrain the vector fields are sampled for a slight color attenuation. Here, the sample in the vector field is used to make a cross product with the up_vector, resulting in a scalar value used to attenuate the terrain fragment’s color. It is interesting to highlight deformation in far regions without 3D grams. The two images on the left show the before and after vehicles' movement, highlighting the path by terrain’s texturing. The two images on the right show the influence of the trails projected on terrain to improve the blend of deformed grams and the terrain.

Italy
Italy
Italy
Italy


Compute-shaders were used to deform and restore the vector fields. LOD and GPU-instancing techniques were used to optimize plants' rendering. Vector fields are kept in a TextureAtlas to reuse memory, preventing allocations and memory fragmentation. Vector fields far way from the camera without dynamic objects close are compressed (using the LZ4 algorithm) to save memory. When needed, the vector fields are decompressed. Then, it is possible to maintain deformations for long periods with a low storage cost. Due to the minimal variation in vector field data, compression rates can be extremely high, resulting in memory savings of 90-95%.


More details can be seen here.




Nifty tech tag lists fromĀ Wouter Beeftink