引擎主循环
This article describes in detail the steps taken by the UNIGINE engine when the Engine::update(), Engine::render(), and Engine::swap() functions are called. For other steps and general information about execution sequence, see the Execution Sequence article.本文详细介绍了UNIGINE引擎在调用Engine::update(), Engine::render()、Engine::swap()函数时所采取的步骤。有关执行顺序的其他步骤和一般信息,请参阅执行顺序文章。
The total time the main loop has taken is displayed by the Total counter in the Performance Profiler.主循环所用的总时间由 Total 计数器显示性能分析器.
UpdateUpdate#
The update part of the execution sequence includes the following:执行序列的 update 部分包括以下内容:
The FPS value of the application starts to be calculated.开始计算应用程序的 FPS 值。
注意Calculation of FPS starts only with the second rendered frame, while the very first one is skipped.FPS 的计算仅从第二个渲染帧开始,而跳过第一个。- All pending console commands that were called during the previous frame are executed. The commands are executed in the beginning of the update() cycle, but before the scripts are updated, because otherwise they may violate the current process of rendering or physical calculations. 执行在前一帧期间调用的所有待处理控制台命令。命令在 update() 周期的开始执行,但在脚本更新之前,否则它们可能会违反当前的渲染或物理计算过程。
- If a world has been unloaded during the previous frame, another world is loaded: WorldLogic::init().如果在前一帧中卸载了一个世界,则加载另一个世界:WorldLogic::init()。
If the restart_main_window console command was executed previously (not in the current update stage), world shaders are created.如果之前执行了 restart_main_window 控制台命令(不在当前更新阶段),则会创建世界着色器。
- Properties, input, and controls are updated.更新了属性、输入和控件。
- Update of streamed textures, sound, and landscape.更新流式纹理、声音和景观。
- Game FPS and materials are updated.游戏 FPS 和材料更新。
- Ambient sound sources timers are updated.环境声源计时器已更新。
- The World Logic init() function for the newly loaded world is called.调用新加载世界的 World Logic init() 函数。
- The update() function of all plugins is called. What happens during it, solely depends on the content of this custom function.调用所有插件的 update() 函数。在此期间会发生什么,完全取决于此自定义函数的内容。
- The Editor-related Logic is updated by calling the Editor update() function.通过调用 Editor update() 函数更新与编辑器相关的逻辑。
- The System Logic is updated. The default system script update() function performs the following:
- The system script handles the mouse. It controls whether the mouse is grabbed when clicked (by default), the mouse cursor disappears when not moved for some time (set by the MOUSE_SOFT definition), or not handled by the system (set by the MOUSE_USER definition, which allows input handling by some custom module).The system script handles the mouse. It controls whether the mouse is grabbed when clicked (by default), the mouse cursor disappears when not moved for some time (set by the MOUSE_SOFT definition), or not handled by the system (set by the MOUSE_USER definition, which allows input handling by some custom module).
- The main menu logic is updated (if the MENU_USER definition is not set).The main menu logic is updated (if the MENU_USER definition is not set).
- Other system-related user input is handled. For example, the world state is saved or restored, or a screenshot of the current UNIGINE window contents is made, if required.Other system-related user input is handled. For example, the world state is saved or restored, or a screenshot of the current UNIGINE window contents is made, if required.
If the GPU Monitor plugin is initialized (the HAS_GPU_MONITOR definition is set), the plugin output is displayed in the application window and additional console commands become available.If the GPU Monitor plugin is initialized (the HAS_GPU_MONITOR definition is set), the plugin output is displayed in the application window and additional console commands become available.
- The system script handles the mouse. It controls whether the mouse is grabbed when clicked (by default), the mouse cursor disappears when not moved for some time (set by the MOUSE_SOFT definition), or not handled by the system (set by the MOUSE_USER definition, which allows input handling by some custom module).系统脚本处理鼠标。它控制鼠标点击时是否被抓取(默认),鼠标光标在一段时间不移动时消失(由 MOUSE_SOFT定义设置),或者不被系统处理(由MOUSE_USER定义设置,允许输入处理一些自定义模块)。
- The main menu logic is updated (if the MENU_USER definition is not set).更新主菜单逻辑(如果未设置 MENU_USER定义)。
- Other system-related user input is handled. For example, the world state is saved or restored, or a screenshot of the current UNIGINE window contents is made, if required.处理其他与系统相关的用户输入。例如,如果需要,保存或恢复世界状态,或者制作当前 UNIGINE 窗口内容的屏幕截图。
If the GPU Monitor plugin is initialized (the HAS_GPU_MONITOR definition is set), the plugin output is displayed in the application window and additional console commands become available.如果 GPU Monitor插件已初始化(设置了HAS_GPU_MONITOR定义),则插件输出将显示在应用程序窗口中,并且其他控制台命令可用。
If the world is loaded (it can be done from the console or via the System Logic), the World Logic gets updated. In the World Logic update() function, you can code frame-by-frame behavior of your application (see details here). 如果世界已加载(可以从安慰或通过系统逻辑),世界逻辑得到更新。在 World Logic update() 函数中,您可以对应用程序的逐帧行为进行编码(请参阅详细信息这里)。
注意Physics, if any, and continuous operations (pushing a car forward depending on current motor's RPM, simulating wind blowing constantly, performing immediate collision response, etc.), can be implemented separately in the updatePhysics() function. This function is called with a fixed frame rate (while the update() function is called each frame).物理(如果有)和连续操作(根据当前电机的 RPM 推动汽车向前,模拟不断吹来的风,执行即时碰撞响应等),可以在 updatePhysics() 函数中单独实现。该函数以固定的帧速率调用(而 update() 函数每帧调用一次)。The world and its world logic are updated in the following order:世界及其世界逻辑按以下顺序更新:
- The World Logic updateAsyncThread() function is executed. It is designed to perform logic functions that should be called every frame independently of the rendering thread. This function doesn't block the Main Thread.World Logic updateAsyncThread() 函数被执行。它旨在执行逻辑功能,这些功能应该独立于渲染线程在每一帧中调用。这个函数不会阻塞主线程。
- The World Logic updateSyncThread() function is executed: all parallel logic functions that should be executed before update(). This function blocks the Main Thread until all calls are completed.World Logic updateSyncThread()函数被执行:所有应该在update()之前执行的并行逻辑函数。该函数会阻塞主线程,直到所有调用完成。
- The World Logic update() function is executed: node parameters are updated, transformations for non-physical nodes are set and so on.执行 World Logic update() 函数:更新节点参数,设置非物理节点的转换等。
- The state of nodes existing in the world is updated (mostly for visible nodes): skinned animation is played, particle systems spawn new particles, players are repositioned, and so on. Triggered world callbacks are added to a stack (they will be executed later).世界中现有节点的状态被更新(主要是可见节点):播放蒙皮动画,粒子系统产生新粒子,重新定位玩家,等等。触发的世界回调被添加到堆栈中(它们将被执行之后)。
- World Expressions are updated. Code in World Expressions can be written via UnigineEditor.World Expressions 已更新。 World Expressions 中的代码可以通过 UnigineEditor 编写。
- GUI is updated.图形用户界面已更新。
- The World Logic postUpdate() function is called.World Logic postUpdate() 函数被调用。
- The System Logic postUpdate() function is executed, if necessary (see details here). It can access the updated data on node states and correct the behavior accordingly in the same frame.如有必要,将执行 System Logic postUpdate() 函数(请参阅详细信息这里)。它可以访问节点状态的更新数据,并在同一帧中相应地纠正行为。
- The Editor Logic postUpdate() function is executed.Editor Logic postUpdate() 函数被执行。
- The postUpdate() function of all plugins is called.这postUpdate()调用所有插件的函数。
- The world spatial tree is updated.世界空间树已更新。
- The file system is updated.文件系统已更新。
At the end of the update stage, physics and pathfinding start to be updated in their separate threads. Then they perform their tasks on all available threads in parallel to rendering.在更新阶段结束时,物理和寻路开始在各自的线程中更新。然后他们在所有可用线程上与渲染并行执行任务。
The total time of update stage is displayed by the Update counter in the Performance Profiler.update 阶段的总时间由 Update 计数器显示在性能分析器.
RenderingRendering#
As soon as the update stage is completed, UNIGINE can start rendering the world. In parallel, the pathfinding is performed in a separate thread. Depending on the physics update mode, parts of the physics calculations are performed in the main thread or in parallel to it. This approach enables to effectively balance the load between CPU and GPU, and thus allows for higher framerate in the UNIGINE-based application.一旦更新阶段完成,UNIGINE 就可以开始渲染世界。同时,寻路在单独的线程中执行。取决于物理更新模式,部分物理计算在主线程中执行或与其并行执行。这种方法能够有效地平衡 CPU 和 GPU 之间的负载,从而在基于 UNIGINE 的应用程序中实现更高的帧率。
Here is what happens during the render stage:这是渲染阶段发生的事情:
Render | Physics | Pathfinding |
---|---|---|
|
|
SwapSwap#
The swap stage is the last one in the main loop. It includes the following:交换阶段是主循环中的最后一个阶段。它包括以下内容:
- If the screenshot console command is executed previously, on this stage, the taken screenshot is saved in folder where all application data is stored.如果之前执行了 screenshot 控制台命令,在此阶段,截取的屏幕截图将保存在所有申请资料被储存了。
Synchronization of physics and pathfinding with the rendered world, i.e. waiting for all additional threads to finish their tasks. Results of the physical calculations are applied to the world. That is, on the previous step we have calculated how physical bodies with collision shapes have changed their position and orientation (due to our update-based logic or interaction). Now these transformations can be finally applied to nodes, i.e. rendered meshes.物理和寻路与渲染世界的同步,即等待所有附加线程完成它们的任务。物理计算的结果应用于世界。也就是说,在上一步中,我们已经计算了具有碰撞形状的物理物体如何改变它们的位置和方向(由于我们基于更新的逻辑或交互)。现在这些变换可以最终应用于节点,即渲染网格。
注意As the synchronization of physics follows the rendering stage (in default Async Rendering physics mode), applied physical transformations will be visible on the screen only in the next frame. To calculate physics before the rendering enable the Before Rendering physics mode.由于物理同步跟随渲染阶段(默认为 Async Rendering 物理模式),应用的物理变换将仅在下一帧中在屏幕上可见。要在渲染之前计算物理,请启用 Before Rendering 物理模式。- The World Logic swap() function is executed. It operates with the results of the updateAsyncThread() function.World Logic swap() 函数被执行。它使用 updateAsyncThread() 函数的结果进行操作。
- The plugin swap() function is called, if it exists.调用插件swap() 函数(如果存在)。
- Unloading of the world, if another world is going to be loaded in the next frame.卸载世界,如果下一帧要加载另一个世界。
- Deletion of all objects that are planned to be deleted.删除所有计划删除的对象。
- Values shown in the performance profiler are updated.性能分析器中显示的值已更新。
After the swap stage is completed, the application window initiates GPU buffers swapping as described here.交换阶段完成后,应用程序窗口将启动 GPU 缓冲区交换,如下所述这里.