Implementing smooth drawing in Unity for VR app with lots of miscalculations

June 24, 2022
Implementing smooth drawing in Unity for VR app with lots of miscalculations_main

While working on a virtual reality application for painting graffiti on walls our team had difficulties with the implementation of smooth painting with spray paint.

The drawing process itself is calculated mathematically using linear interpolation. We launch a beam from a can of paint and aim at the texture of the wall on which we will paint, after which we calculate the pixels, and the paint is transferred to the wall.

The main difficulty occurred when moving quickly with a can. The paint did not fill all the intermediate points, but only separate ones, as a result, voids appeared in the filling with color. Naturally, at the same time, the drawing process lost its realism and smoothness and became uncomfortable. The reason for this was the use of Unity’s built-in math, which could not cope with a large number of miscalculations.

In order to solve this problem, we decided to use third-party libraries:

  • Hybrid renderer — provides systems and components for rendering ECS ​​entities. Hybrid Renderer is not a render pipeline: it is a system that collects the data necessary for rendering ECS ​​entities and sends this data to Unity’s existing rendering architecture.
  • BurstCompiler — it translates from IL/.NET byte code to highly optimized native code using LLVM. It is released as a unity package and integrated into Unity using the Unity Package Manager.
  • Unity C# Job System — lets you write simple and safe multithreaded code that interacts with the Unity Engine for enhanced game performance.

In the video below, you can see the difference in performance before and after using third party libraries.

After the implementation of the above libraries, the efficiency of the application was improved by 90-100%.