Page 1 sur 1

Les ombres dans un environnement 3D

Publié : mar. 13/janv./2026 20:29
par falsam
La fonction WorldShadows() permet de changer le type d'ombre qui sera appliqué dans le monde 3D.
https://www.purebasic.com/french/docume ... adows.html
■ Exemple.

Code : Tout sélectionner

; Ombrage
WorldShadows(#PB_Shadow_Modulative, 0, RGB(180,180,180), 2048)
■ Depuis la version 6.20 ce code qui affiche un cube avec une projection d'ombre sur le sol ne fonctionne plus et génère l'erreur
L'éxécutable de débogage se ferme de façon inattendue.

Code : Tout sélectionner

EnableExplicit

; Evenement window
Global Event, Quit.b

; Sprite Help
Global Bottom, FontInfo = LoadFont(#PB_Any, "Arial", 12)

; Initialisation de l'environnement 3D
If InitEngine3D(#PB_Engine3D_DebugLog) = 0 Or InitSprite() = 0  Or InitKeyboard() = 0
  MessageRequester("Information", "Souci d'initialisation environnement 3D")
EndIf

OpenWindow(0, 0, 0, 0, 0, "Test ombrage", #PB_Window_Maximize | #PB_Window_BorderLess)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0), 0, 0, 0, #PB_Screen_NoSynchronization) 

; Clavier international
KeyboardMode(#PB_Keyboard_International | #PB_Keyboard_AllowSystemKeys)

; Ombrage
WorldShadows(#PB_Shadow_Modulative, 0, RGB(180,180,180), 2048)

; Camera
CreateCamera(0, 0, 0, 100, 100)
CameraBackColor(0, RGB(119, 136, 153))
MoveCamera(0, 2, 2, 5, #PB_Absolute)
CameraLookAt(0, 0, 0, 0)

; Light
CreateLight(0, RGB(255, 255, 255), -10, 20, 5)

; Création d'un sol
CreatePlane(0, 10, 10, 1, 1, 1, 1)
CreateMaterial(0, 0, RGB(154, 205, 50))
CreateEntity(0, MeshID(0), MaterialID(0))

; Et d'un cube
CreateCube(1, 1)
CreateMaterial(1, 0, RGB(184, 134, 11))
CreateEntity(#PB_Any, MeshID(1), MaterialID(1), 0, 0.5, 0)

;- Création du sprite Help
Bottom = CreateSprite(#PB_Any, ScreenWidth(), 150, #PB_Sprite_AlphaBlending)
If StartDrawing(SpriteOutput(Bottom))
  DrawingFont(FontID(FontInfo))
  DrawingMode(#PB_2DDrawing_AlphaChannel)
  Box(0, 0, ScreenWidth(), SpriteHeight(Bottom), RGBA(0, 0, 0, 140))
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawText(10, 10, ". : : Help : : .")
  DrawText(10, 40, "Touche Escape pour quitter.")
  StopDrawing()
EndIf 

; Boucle evenementielle
Repeat
  ; Traiter les évenement de la file d'attente
  Repeat
    Event = WindowEvent()
    
    Select Event         
      Case #PB_Event_CloseWindow
        Quit = #True
    EndSelect
  Until Event = 0
  
  ; Evenements 2D/3D
  ExamineKeyboard()
    
  RenderWorld()
  
  ; Sprite help
  DisplayTransparentSprite(Bottom, 0, ScreenHeight()-SpriteHeight(Bottom))
  
  FlipBuffers() 
Until KeyboardPushed(#PB_Key_Escape) Or Quit = #True
■ La solution.
Ajouter un path relatif au dossier "examples/3d/Data/Main" du dossier d'installation de PureBasic.
Ce dossier contient les fichiers nécessaires à la génération des ombres.

Code : Tout sélectionner

; Si PureBasic >= 6.20
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Main", #PB_3DArchive_FileSystem)
Parse3DScripts()

■ Voici le code corrigé.

Code : Tout sélectionner

EnableExplicit

; Evenement window
Global Event, Quit.b

; Sprite Help
Global Bottom, FontInfo = LoadFont(#PB_Any, "Arial", 12)

; Initialisation de l'environnement 3D
If InitEngine3D(#PB_Engine3D_DebugLog) = 0 Or InitSprite() = 0  Or InitKeyboard() = 0
  MessageRequester("Information", "Souci d'initialisation environnement 3D")
EndIf

OpenWindow(0, 0, 0, 0, 0, "Test ombrage", #PB_Window_Maximize | #PB_Window_BorderLess)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0), 0, 0, 0, #PB_Screen_NoSynchronization) 

; Si PureBasic >= 6.20
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Main", #PB_3DArchive_FileSystem)
Parse3DScripts()

; Clavier international
KeyboardMode(#PB_Keyboard_International | #PB_Keyboard_AllowSystemKeys)

; Ombrage
WorldShadows(#PB_Shadow_Modulative, 0, RGB(180,180,180), 2048)

; Camera
CreateCamera(0, 0, 0, 100, 100)
CameraBackColor(0, RGB(119, 136, 153))
MoveCamera(0, 2, 2, 5, #PB_Absolute)
CameraLookAt(0, 0, 0, 0)

; Light
CreateLight(0, RGB(255, 255, 255), -10, 20, 5)

; Création d'un sol
CreatePlane(0, 10, 10, 1, 1, 1, 1)
CreateMaterial(0, 0, RGB(154, 205, 50))
CreateEntity(0, MeshID(0), MaterialID(0))

; Et d'un cube
CreateCube(1, 1)
CreateMaterial(1, 0, RGB(184, 134, 11))
CreateEntity(#PB_Any, MeshID(1), MaterialID(1), 0, 0.5, 0)

;- Création du sprite Help
Bottom = CreateSprite(#PB_Any, ScreenWidth(), 150, #PB_Sprite_AlphaBlending)
If StartDrawing(SpriteOutput(Bottom))
  DrawingFont(FontID(FontInfo))
  DrawingMode(#PB_2DDrawing_AlphaChannel)
  Box(0, 0, ScreenWidth(), SpriteHeight(Bottom), RGBA(0, 0, 0, 140))
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawText(10, 10, ". : : Help : : .")
  DrawText(10, 40, "Touche Escape pour quitter.")
  StopDrawing()
EndIf 

; Boucle evenementielle
Repeat
  ; Traiter les évenement de la file d'attente
  Repeat
    Event = WindowEvent()
    
    Select Event         
      Case #PB_Event_CloseWindow
        Quit = #True
    EndSelect
  Until Event = 0
  
  ; Evenements 2D/3D
  ExamineKeyboard()
    
  RenderWorld()
  
  ; Sprite help
  DisplayTransparentSprite(Bottom, 0, ScreenHeight()-SpriteHeight(Bottom))
  
  FlipBuffers() 
Until KeyboardPushed(#PB_Key_Escape) Or Quit = #True
le cube s'affiche avec son ombre sur le sol.