- Home /
How to draw a mesh for a single frame with vsync disabled?
I have a mesh for drawing bullet trails as a line. The bullets travel instantly, so I just want to draw it for a single frame. If the line is visible for too long, and the player is moving while firing, the gun will move away from the line and it will be left floating in space, which looks bad. I don't want to move the line with the gun, I need the line to only be visible while it is in the same location as the gun barrel.
This is what I have:
int renderCount = 0;
void OnRenderObject()
{
if (renderCount < 1)
{
Graphics.DrawMesh(mesh, matrix, material, 0);
}
renderCount++;
}
This works with vsync enabled. With vsync off, the line is only occasionally visible. I assume this is because the scene is being rendered multiple times before being displayed on the screen. Even using if (renderCount < 5) does not show the line consistently.
Am I correct? Is there a solution? Is there a way to know when something has actually been rendered?
I guess it's because VSync reduces framerate, so you have a chance to notice your bullet trail, while with VSync switched off you don't have a chance, it's too fast. :)
Why don't you draw it for a fixed amount of time ins$$anonymous$$d of number of frames? It's awful to make framerate-dependent code!
I don't know how long I need to draw it for unless I make it depend on the frame rate. I need to draw it for the $$anonymous$$imum time possible. How would I do that?
The only way something would last .25 seconds at a $$anonymous$$imum is if your framerate was 4fps, or your logic was incorrect. All you have to do is wait for the desired amount of time, say .05 seconds (1/20th). Coroutines or Invoke are generally easier for this sort of thing than Update.
You need to turn on vsync. Also Invoke is not necessarily synced to the framerate...just attach this to an object, and hit space:
function Update () {
if (renderer.enabled) {
renderer.enabled = false;
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
renderer.enabled = true;
}
}
This is easily visible and anyone can see it 100% of the time; 1/60th of a second is a relatively long time when it comes to vision. Again, vsync is required, or else the effect can disappear "between" frames.
Answer by hexagonius · Feb 12, 2015 at 07:23 AM
I think what you need is the exact moment, a frame was renderer to the screen. This could help render as long as this Unity message is called, then stop
Just read the documentation here Graphics.DrawMesh and it seems that it should definitely draw it for one frame. You might want to file a bug report (under Unity -> Help -> Report a bug) on the docs.