SoundRange3D() : La caméra à des oreilles

Généralités sur la programmation 3D
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

SoundRange3D() : La caméra à des oreilles

Message par falsam »

Le 10 Janvier j'ai signalé un bug avec la fonction SoundRange3D() sur le forum anglais. En fait c'est de ma faute.

Quand on utilise la fonction SoundRange3D(), on doit aussi utiliser la fonction SoundListenerLocate(x, y, z).

x = CameraX(#camera)
y = CameraY(#camera)
z = CameraZ(#camera)

Un code (Testé sous Windows 7 et Windows 8.1) qui vous montre comment tout cela fonctionne. Dirigez vous avec les flèches en direction de la sphère rouge ou bleu.

Code : Tout sélectionner

Enumeration Window
  #Mainform
EndEnumeration

Enumeration D3
  #camera
  
  #player
  
  #material
  
  #ground
  
  #nodeRed
  #nodeBlue
  
  #soundRed
  #soundBlue
  
  #LandMarkRed
  #LandMarkBlue
  
  #Panel
EndEnumeration

Global PlayerSpeed.f = 0.2

InitEngine3D()
InitKeyboard()
InitSprite()
InitMouse()

ExamineDesktops()

OpenWindow(#Mainform,0, 0, 800, 600, "", #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(#Mainform), 0, 0, 800, 600, 0, 0, 0)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

KeyboardMode(#PB_Keyboard_International)
 
;Light & Shadow
CreateLight(#PB_Any,RGB(255, 255, 255), -4, 100, -50)
WorldShadows(#PB_Shadow_Additive, 30, RGB(250, 235, 215)) ;Comment if Windows 8 

;Camera 
CreateCamera(#camera ,0, 0, 100, 100)
CameraBackColor(#camera, RGB(145, 182, 201))
MoveCamera(#camera, 2, 5, 15, #PB_Absolute)  
CameraLookAt(#camera, 0,0,0)   

;Ground 
GetScriptMaterial(#material, "Color/Yellow")
CreateEntity(#ground, MeshID(CreatePlane(#PB_Any, 100, 100, 1, 1, 1, 1)), MaterialID(#material))


;Red sound (Node, Sound, Red LandMark)
CreateNode(#nodeRed, 20, 1, 30)
LoadSound3D(#soundRed, "Roar.ogg")
GetScriptMaterial(#material,"Color/Red")
CreateEntity(#LandMarkRed, MeshID(CreateSphere(#PB_Any, 1)), MaterialID(#material))
AttachNodeObject(#nodeRed, SoundID3D(#soundRed))
AttachNodeObject(#nodeRed, EntityID(#LandMarkRed))
SoundRange3D(#soundRed, 0, 30)
PlaySound3D(#soundRed, #PB_Sound3D_Loop)

;Blue sound (Node, Sound, Red LandMark)
CreateNode(#nodeBlue, -20, 1, 30)
LoadSound3D(#soundBlue, "Siren.ogg")
GetScriptMaterial(#material,"Color/Blue")
CreateEntity(#LandMarkBlue, MeshID(CreateSphere(#PB_Any, 1)), MaterialID(#material))
AttachNodeObject(#nodeBlue, SoundID3D(#soundBlue))
AttachNodeObject(#nodeBlue, EntityID(#LandMarkBlue))
SoundRange3D(#soundBlue, 0, 30)
PlaySound3D(#soundBlue, #PB_Sound3D_Loop)

;Player
CreateEntity(#player, MeshID(CreateCube(#PB_Any, 1)), #PB_Material_None, 0, 1, -10)
HideEntity(#player, #True)

Repeat ;Event 3D Loop
  Repeat ;Event Window Loop
    Event  = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        End
        
    EndSelect
  Until Event = 0
  
  If ExamineKeyboard()
 
    If KeyboardPushed (#PB_Key_Escape)
      End
    EndIf
    
    ;Move Up or Down
    If KeyboardPushed (#PB_Key_Up)
      MoveEntity(#player, 0, 0, PlayerSpeed, #PB_Absolute|#PB_Local)
    ElseIf KeyboardPushed (#PB_Key_Down)
      MoveEntity(#player, 0, 0, -PlayerSpeed, #PB_Absolute|#PB_Local)
    EndIf
    
    ;Rotate Left or Right
    If KeyboardPushed (#PB_Key_Left)
      RotateEntity(#player, 0, 1.5, 0, #PB_Relative)
    ElseIf KeyboardPushed (#PB_Key_Right)
      RotateEntity(#player, 0, -1.5, 0, #PB_Relative)
    EndIf    
  EndIf
  
  CameraFollow(#camera, EntityID(#player), 180, 1, 1, 0.2, 0.2)
  
  ;IMPORTANT The ear follows the camera (Les oreilles suivent la caméra)
  SoundListenerLocate(CameraX(#camera), CameraY(#camera), CameraZ(#camera))
  
  RenderWorld(30)
  
  ;-Control Panel
  CreateSprite(#Panel, 800, 40)
  StartDrawing(SpriteOutput(#Panel))
  Box(0,0, 800, 40, RGBA(178, 34, 34, 20))
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawText(5,2,"FPS: "+ Str(Engine3DStatus(#PB_Engine3D_CurrentFPS)) +
               "  Min: " + Str(Engine3DStatus(#PB_Engine3D_MinimumFPS)) + 
               "  Max: " + Str(Engine3DStatus(#PB_Engine3D_MaximumFPS)))
  
  DrawText(200, 2, "(" + Str(EntityX(#player)) + ", " + Str(EntityY(#Player)) + ", " + Str(EntityZ(#Player)) + ")")
  StopDrawing()  
  DisplaySprite(#Panel, 0, 0)
  FlipBuffers()  
  
ForEver
■ Si ça ne fonctionne pas sous Window 8 ou Windows 8.1
- Commenter la ligne 46

Code : Tout sélectionner

WorldShadows(#PB_Shadow_Additive, 30, RGB(250, 235, 215))
- Ou configurer les options de compilation avec le sous system OpenGL.
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
Fig
Messages : 1176
Inscription : jeu. 14/oct./2004 19:48

Re: SoundRange3D() : La caméra à des oreilles

Message par Fig »

Je ne suis pas sûr ... mais si on se retourne, ça n'inverse pas les sons on dirait.. (??)
Il y a deux méthodes pour écrire des programmes sans erreurs. Mais il n’y a que la troisième qui marche.
Version de PB : 6.00LTS - 64 bits
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: SoundRange3D() : La caméra à des oreilles

Message par falsam »

Fig a écrit :Je ne suis pas sûr ... mais si on se retourne, ça n'inverse pas les sons on dirait.. (??)
Tu as raison !!!

Pensant que l'inversion de son fonctionnait auparavant, j'ai testé aprés quelques modifications de compatibilité avec la version 5.11 et 5.23 et le résultat est le même : Pas d'inversion de son.
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
Fig
Messages : 1176
Inscription : jeu. 14/oct./2004 19:48

Re: SoundRange3D() : La caméra à des oreilles

Message par Fig »

Donc bugué finalement ? :?
Ou est ce que c'est simplement parce que nous sommes un "point" qui n'a pas d'orientation ?
Il y a deux méthodes pour écrire des programmes sans erreurs. Mais il n’y a que la troisième qui marche.
Version de PB : 6.00LTS - 64 bits
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: SoundRange3D() : La caméra à des oreilles

Message par falsam »

Fig a écrit :Donc bugué finalement ? :?
Ou est ce que c'est simplement parce que nous sommes un "point" qui n'a pas d'orientation ?
Bugué non puisque les coordonnées de la camera restes les même durant la rotation.

Par contre si tu te déplaces autour d'un des points, le son passe bien de gauche à droite ou inversement.
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Répondre