- Home /
Allow Unity to render as fast as possible?
Hey there,
I hope I can make my issue clear, it's not that easy to get into :)
What I am trying to do is rendering a live video from the running application, as fast as possible (and then do other stuff with that video, but that is of no concern here). The video capturing itself is already working (I have written a dll using ffmpeg).
Now the problem is the "as fast as possible".
Normally, when you load in - for example - an animated character, you want its 1 second animation to play in 1 second, no matter how many frames that is on the clients platform.
I want this "scaling" to be gone. If the animation has 30 frames with a setup of 30 frames per second (not sure if Maya saves the desired fps), I want those 30 frames finished in 30 unity frames, and if that only takes 0.2 seconds for the engine and looks awkward as hell, then this is exactly what I want. Think of the old DOS games that had no time scaling and ran ridiculously fast as PCs became faster. The DLL itself then scales 30 frames it gets from the engine to be one second in the output video. What counts is that the video is rendered as fast as possible, and that is not possible if the engine slows itself down to appeal to the eye.
I tried setting the Application.targetFrameRate to -1, but that did not remove the animation scaling. My guess (and hope) is that unity uses some deltaTime variable in everything it does internal. And I want that deltaTime to be 1, or 0, or whatever disables it. Unfortunately, I couldn't find anything.
I am aware that this is completely irrelevant for 99% of all Unity development cases, so it may be that it simply is not possible that easy. Would this change maybe require source code access to Unity?
PS: I already have this running in another engine, and all it took there was to simply not apply any deltaTime, but the company I work for wants to switch to a better engine. And me too :D
Answer by Bunny83 · Dec 05, 2011 at 11:28 PM
Ok, as you said it's against everything people would expect from an engine :D. You can't simply "turn off" deltaTime. Animations don't have frames (well in a lot animation tools the timeline is split into frames but that's irrelevant), they have keyframes! Keyframes usually aren't at fix timesteps. They are placed at important spots in a movement.
However if you want to play an animation at a custom pseudo frame-rate you have to do it manually.
The easiest way would be to set the AnimationState.normalizedTime manually from 0 to 1 in the desired speed. A small example: If you have an animation that takes 1.5 sec and have 30 frames (in your animation software) you just need to increase the normalized time by 1/30 each visual frame. That way the animation gets sampled at fix positions along it's timeline.
You just need to enable the animationState and set it's weight to 1.0. Don't use Play, CrossFade or any similar functions in this case.
I'm still not sure why you need this. ;)
Answer by TheSHEEEP · Dec 06, 2011 at 01:22 PM
Thanks for the answer, that was what I needed. Unfortunately, I would have to do this for everything... movement, videos in background, etc.. Speed all of that up. But at least that seems doable. Just not sure if we want to go with that much effort.
Another problem I have is that our animations are supposed to be in 30 fps. And I cannot get Unity to play faster than 70fps. This is at least more than double speed, but capturing the video reduces this by over 30 fps, so in the end we are capturing at around 30-40 fps, which is almost no gain.
As a comparison, the engine we currently use renders (and captures with the same plugin) at over 200 fps, in debug mode.
There is nothing but an animated character, 2 or 3 (unanimated) props and possibly a video playing in the background. This is nothing, which is why the engine can run that fast.
In Unity, only the animated character is there, and still there is 70fps at max. Quality settings have no impact here. I still have the feeling the negine is "holding back". Or is Unity really that slow? That would be bad.
Should I probably open a new question for this?
EDIT:
FPS problem is solved. Editor never goes fast than 70fps. Outside of the editor, I get around 1100 frames (160-170 while capturing), which is fine for the moment. Consider this one solved.
Good to hear ;) I don't know for what platform you develop (i guess standalone) but you might have a look at Application.targetFrameRate. Note the hint about vsync and webplayer ;)
Your answer
Follow this Question
Related Questions
Speed up video play 2 Answers
How to select an animation clip by index number? 7 Answers
Animation speed 4 Answers
Small video files or sprite sheet animations? 0 Answers
How to calculate the speed at which a rigidbody2d is actually moving ? 1 Answer