简体   繁体   中英

Raycasting mouse position to draw on 3D mesh

I want to draw on a 3D object, and the way I do it is to raycast hits from the mouse position and paint the closest 3D vertex to that hit.

However, when I am moving the mouse really fast, the hits are not registering, and there are big gaps in my drawing:

缺少绘图

What could be the issue?

The issue is pretty clearly that you are not sampling inputs quickly enough for your given painting strategy. This may be because the time between samples is unusually slow (perhaps because your raycasts are taking too much time) or because the speed of the mouse simply exceeds any reasonable sampling rate. This is a fundamental flaw in your painting strategy, which is to paint one vertex for each input sample.

A more robust approach would involve some sort of interpolation. Here are two approaches that come to mind:

  1. Interpolate the input samples. Ie, if one mouse input is sampled at coordinates (x1, y1) and the next input is sampled at (x2, y2), you could add imaginary samples along the line segment from (x1, y1) to (x2, y2) and raycast those as well. If your raycasts are costly, this may not be a performant option. Also you would have to somehow determine a good number of extra points to introduce.
  2. Interpolate along the mesh. In this case, you raycast two consecutive input samples to find two corresponding vertices on the mesh. Then you also paint all vertices along a path between those two vertices. The shortest path seems reasonable, but perhaps there are better strategies for your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM