- Panda3d 1.7 Game Developer's Cookbook
- Christoph Lang
- 367字
- 2021-04-09 21:21:45
Loading and attaching sounds to objects
In this recipe we will take a look into Panda3D's positional audio capabilities. 3D sound is a wonderful tool to immerse the player and to generate great atmosphere. Positional audio also can help the player to orientate: In a shooter, for example, it is much easier to return fire if one heard that the enemy units are attacking from behind.
Getting ready
Before starting this recipe, be sure to set up a project as described in Setting up the game structure. You will also need to provide a mono sound file called loop.wav
in the sounds
folder of your project.
How to do it...
Let's load a sound file and attach it to a model:
- Open
Application.py
and add the highlighted code:from direct.showbase.ShowBase import ShowBase from direct.showbase.Audio3DManager import Audio3DManager class Application(ShowBase): def __init__(self): ShowBase.__init__(self) self.smiley = loader.loadModel("smiley") self.smiley.reparentTo(render) self.audio = Audio3DManager(self.sfxManagerList[0]) self.audio.attachListener(self.cam) self.loop = self.audio.loadSfx("loop.wav") self.loop.setLoop(True) self.audio.attachSoundToObject(self.loop, self.smiley) self.loop.play() self.cam.setPos(0, -40, 0)
- Hold the left mouse button and move the mouse to pan the camera, or hold down the right mouse button and use the mouse to zoom in and out of the scene. Note how the sound position changes.
How it works...
The key to positional audio in Panda3D is the Audio3DManager
class. After adding a smiley to the scene, we initialize the Audio3DManager
and set the camera to be the listener object. This has the effect that all sounds played using positional audio will be mixed relative to the NodePath
we pass to attachListener()
.
Then, the sound file is loaded, set to be a loop and attached to the smiley, so that the sound plays wherever the object is positioned in the scene. Finally we start to play the sound loop and position the camera.
Note
Positional audio will not work if we are using a stereo sound file. Panda3D will print a warning message in the console output if our sound is stereo, not mono.
- Moodle 2.0 E/Learning Course Development
- CAXA CAD電子圖板2020工程制圖
- Drools規(guī)則引擎技術(shù)指南
- Puppet 2.7 Cookbook
- 中文版Photoshop CS6平面設(shè)計(jì)實(shí)例教程(第2版)
- Photoshop CS6實(shí)戰(zhàn)從入門到精通(超值版)
- 從零開始:AutoCAD 2010中文版建筑制圖基礎(chǔ)培訓(xùn)教程(第2版)
- CINEMA 4D R20完全實(shí)戰(zhàn)技術(shù)手冊(cè)
- 巧學(xué)巧用Flash CS6制作動(dòng)畫
- Adobe創(chuàng)意大學(xué)InDesign CS5 產(chǎn)品專家認(rèn)證標(biāo)準(zhǔn)教材
- Microsoft Azure: Enterprise Application Development
- 中望3D從入門到精通
- SolidWorks 2020中文版從入門到精通
- AutoCAD 2016從入門到精通
- Photoshop圖像處理立體化教程:Photoshop 2021(微課版·第2版)