14 Replies Latest reply on Dec 20, 2006 3:09 AM by Newt99

    Attatching sounds to 3D objects

    r1chard..w3lch Level 1
      Im sure there was an old topic about this but i cant find it anywhere. There is a way of attatching a sound to a 3d object within the .w3d file, so that if the camera gets closer the sound volume increases. If anybody knows what im talking about and can help it would be much appriciated!

      Thanks, Rich
        • 1. Re: Attatching sounds to 3D objects
          milochong Level 1
          theres is a behaviour in the chromelib pack that does this,
          http://www.chromelib.com

          I find these so handy, does anyone know when there may be an update of these coming out, or even what new goodies are on the way.

          hope this helps
          • 2. Re: Attatching sounds to 3D objects
            r1chard..w3lch Level 1
            Thanks for the quick reply. would i have to purchase this pack? also what is the behaviour called? Thanks
            Rich
            • 3. Re: Attatching sounds to 3D objects
              duckets Level 1
              Or you could just measure the distance between the camera and the object, and use that as a factor for setting your volume, like this:

              objDist = (obj.transform.position - cam.transform.position).length
              soundVolume = min(255,integer((10.0/objDist)*255))

              The above will give you a maximum volume when the object is within 10 units (hence the 10.0), and will tail off as the object gets further away, becoming almost inaudible at a distance of 1000 units.

              The equation makes sure the value is an integer (suitable for using as a sound channel volume), and it caps the value at 255.

              You might want to adjust it so the sound remains louder at further distances by increasing the value from 10.0 to 100.0 or something, depending on the intended loudness of your object, and the relative scale of your scene.

              hope this helps!

              - Ben
              • 4. Re: Attatching sounds to 3D objects
                r1chard..w3lch Level 1
                Hi ben, thanks for the reply. Your solution sounds pretty much what I'm trying to work out, the only problem is im a bit confused at where I would quote the model within my .w3d world and where I would quote the sound in my cast list?
                My .w3d world is fairly basic but there are a few different models that make it up, e.g: landscape, cube1, cube2, sphere1.

                Thanks Rich
                • 5. Re: Attatching sounds to 3D objects
                  duckets Level 1
                  if your 3d cast member is called "myscene" then add these lines before the code I posted originally.

                  scene = member("myscene")
                  cam = scene.camera[1]
                  obj = scene.model("cube1")

                  or whatever object you want to pick out as the sound emitter...

                  - ben
                  • 6. Re: Attatching sounds to 3D objects
                    r1chard..w3lch Level 1
                    Hi, thanks for the response Ben,
                    I understand the script but im not sure how i'd tell director which sound it needs to emit from the 3d object as there is currently no mention of sound in the script.

                    Thanks
                    Rich
                    • 7. Re: Attatching sounds to 3D objects
                      duckets Level 1
                      Ok, so the full set of code would be something like the attached lines below.

                      When and where you actually use the lines which play the sound are up to you (you obviously don't want to be re-triggering a sound every single frame). Probably you'll want to either play a looped sound in a channel, then just adjust the volume - or you'll want to re-trigger a sound at certain moments. Either way, the code below should be a good starting point to work from.

                      hope this helps!

                      - Ben
                      • 8. Re: Attatching sounds to 3D objects
                        r1chard..w3lch Level 1
                        Hi Ben, I've tried the code but its not working as yet. I've tried a few variations, like writing the code in amongst my main movement behavior, creating a movie script and trying it as a new behavior onto the 3d world.

                        Here’s what I've attempted to use with no success yet (but also no errors)
                        Do you have any ideas of what I’m doing wrong?


                        • 9. Re: Attatching sounds to 3D objects
                          duckets Level 1
                          Yep, because you have the volume equations in your 'beginsprite' handler, you're not re-calculating the volume each frame.

                          If "CRLOOP2" is a looped sound, you won't need to re-trigger it, so the code that you'll need to perform each frame (or each time your 3d object moves) is this:

                          objDist = (obj.transform.position - cam.transform.position).length
                          soundVolume = min(255,integer((100.0/objDist)*255))
                          sound(4).volume = soundVolume

                          You've also somehow accidentally got 2055 in there, instead of 255.

                          - Ben
                          • 10. Re: Attatching sounds to 3D objects
                            r1chard..w3lch Level 1
                            Hi, thanks for your reply,
                            It says 'object expected'. The camera is called "2egocam"
                            The camera is created in the 'movement' script so im unsure if direcor does not realise this or i need to put something else in the lingo...

                            • 11. Re: Attatching sounds to 3D objects
                              duckets Level 1

                              Well without seeing how your movie is set up in general, it's difficult to advise.
                              Is this sound code a behaviour on your 3d scene?
                              Is the 'movement' script also a behaviour on your 3d scene?

                              If so, make sure the 'movement' behaviour is higher up in the behaviour list than the volume script, because they are executed in that order. If the movement script creates the cam, then of course this will have to happen before you try to use references to the cam.

                              Which line generates the 'object expected' error?

                              Also - from the code you posted - it looks like you still haven't moved the actual sound volume calculations out of the beginsprite handler - because the volume will need to be recalculated each frame (or each time the object moves), so they should be in an enterFrame handler or someplace similar.

                              - Ben


                              • 12. Re: Attatching sounds to 3D objects
                                r1chard..w3lch Level 1
                                Hi
                                I can now hear sound but it makes no difference how far / near the camara is from the sound emmiting cube.
                                My character is a 3d shape which is right next to the camara. Would it work if i could use the characters 3d model as the 'ears' that hear the sound from the emmiting cube?

                                In other words would I be able to replace where it says ' cam' with something relevent to my characters 3d model?


                                scene = member("level RN")
                                cam = scene.model("character")

                                obj = scene.model("hke_move_soundcube")

                                ----------------------

                                objDist = (obj.transform.position - cam.transform.position).length
                                soundVolume = min(255,integer((100.0/objDist)*255))
                                sound(4).volume = soundVolume
                                sound(4).play(member("CRLOOP2"))





                                Rich
                                • 13. Re: Attatching sounds to 3D objects
                                  duckets Level 1
                                  quote:

                                  Originally posted by: duckets
                                  Also - from the code you posted - it looks like you still haven't moved the actual sound volume calculations out of the beginsprite handler - because the volume will need to be recalculated each frame (or each time the object moves), so they should be in an enterFrame handler or someplace similar.



                                  Have you done the above yet?


                                  quote:

                                  Originally posted by: r1chard..w3lch
                                  Would it work if i could use the characters 3d model as the 'ears' that hear the sound from the emmiting cube? <snip> would I be able to replace where it says ' cam' with something relevent to my characters 3d model?


                                  It should work, but it should also work with your camera too. But feel free to try it out for yourself - you don't have to ask first! :-)

                                  If you have moved the calculations into an enterframe/exitframe handler as advised above, then perhaps you have more than one camera in your scene, and you're referring to the wrong one.

                                  All you're trying to do is ascertain the distance between two objects in your scene. It shouldn't matter what kind of objects these are (cameras, models, lights, group nodes etc).

                                  Remember you can drag your 3d cast member into the object inspector, to inspect what exactly is contained within your 3d scene.

                                  - Ben
                                  • 14. Re: Attatching sounds to 3D objects
                                    Newt99 Level 1

                                    Hello milochong,

                                    I wait for news about Director 11 before updating the Chrome Lib.

                                    r1chard..w3lch, the 3D Sound behavior controls the sound volume based on the distance between the emitter model and the current camera. It is in the free version. You don't have to purchase the pay version.

                                    Best regards,
                                    Karl Sigiscar.

                                    http://www.chromelib.com/