- Panda3d 1.7 Game Developer's Cookbook
- Christoph Lang
- 484字
- 2021-04-09 21:21:48
Loading data asynchronously
Panda3D really makes it very easy to load and use assets like models, actors, textures, and sounds. But there is a problem with the default behavior of the asset loader—it blocks the execution of the engine.
This is not a problem if all data is loaded before the player is allowed to see the game world, but if models and other assets are to be loaded while the game is running, we are facing a serious problem because the frame rate will drop dramatically for a moment. This will cause game execution to stop for a short moment in a sudden and unpredictable way that breaks gameplay.
To avoid getting into such problems, Panda3D offers the ability to load data through a background thread. This is a very useful feature if game assets are loaded on the fly, such as the popular use case with seamless streaming in game worlds. It is also a great way to reduce initial loading times. The main level geometry and everything visible from the starting position is loaded before the player enters the world and the rest of it is loaded afterwards, often depending on the position in the game world.
Getting ready
For this recipe you will need to set up the basic framework described in Setting up the game structure.
How to do it...
Follow these steps to create a sample application that demonstrates asynchronous data loading:
- Add the following code to
Application.py
:from direct.showbase.ShowBase import ShowBase from direct.actor.Actor import Actor from panda3d.core import Vec3 class Application(ShowBase): def __init__(self): ShowBase.__init__(self) self.cam.setPos(0, -30, 6) taskMgr.doMethodLater(3, self.load, "load", extraArgs = ["teapot", Vec3(-5, 0, 0), self.modelLoaded]) taskMgr.doMethodLater(5, self.load, "load", extraArgs = ["panda", Vec3(5, 0, 0), self.actorLoaded]) def load(self, name, pos, cb): loader.loadModel(name, callback = cb, extraArgs = [pos]) def modelLoaded(self, model, pos): model.reparentTo(render) model.setPos(pos) def actorLoaded(self, model, pos): self.panda = Actor(model, {"walk": "panda-walk"}) self.panda.reparentTo(render) self.panda.setPos(pos) self.panda.loop("walk")
- Press F6 to run the application. You will see a delay before the teapot and the panda appear in the window.
How it works...
The previous code enqueues calls to the load()
method using doMethodLater()
so you can see the objects pop up as soon as they are loaded. The list passed to the extraArgs
parameter will be used as parameters for the call to load()
.
The call to loadModel()
within the load method is very important, because instead of just passing the name of the model to load, you also set the callback parameter to one of the modelLoaded()
and actorLoaded()
methods, depending on what the cb
parameter of load()
contains.
As soon as a call to loadModel()
uses the callback parameter, the request to load the data is handed off to a background thread. When the required asset has finished loading, the callback function is called and the loaded asset is passed as the first parameter, as you can see in the modelLoaded()
and actorLoaded()
methods.
- 數據、模型與決策:基于Excel的建模和商務應用
- 從零開始:Photoshop CC中文版基礎培訓教程
- Sencha Touch Cookbook, Second Edition
- EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g
- Premiere Pro 2022從新手到高手
- Adobe 創(chuàng)意大學動漫設計師Flash CS5 + Photoshop CS5 標準實訓教材
- InDesign平面設計案例教程:從設計到印刷
- 人臉識別算法與案例分析
- Maya建模技術解析
- Flash CC動畫制作案例教程
- SolidWorks2014基礎實例教程
- Getting Started With Oracle SOA Suite 11g R1 – A Hands/On Tutorial
- WordPress Theme Design
- Joomla! 1.5 Site Blueprints: LITE
- Premiere pro CC中文版自學視頻教程