[Done] Include 2D Sprites Within a 3D Environment

All bugs related to the 3D engine
User avatar
JHPJHP
Addict
Addict
Posts: 2260
Joined: Sat Oct 09, 2010 3:47 am

[Done] Include 2D Sprites Within a 3D Environment

Post by JHPJHP »

Windows 11 Pro, 24H2
PureBasic 6.21 (x86 / x64)

I'm not sure if this is related to another Bug Report I posted: 2D Sprites Auto Stretch After CloseScreen.

• The following example demonstrates a 2D sprite within a 3D environment.
• Using OpenWindowedScreen the AutoStretch parameter seems to ignore the 2D sprite.

3D + 2D

Code: Select all

Enumeration
  #MainWindow
EndEnumeration

InitEngine3D() : InitSprite() : InitKeyboard()

WindowWidth = 600 : WindowHeight = 600
nFlags = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered | #PB_Window_Invisible

If OpenWindow(#MainWindow, 0, 0, WindowWidth, WindowHeight, "Include 2D sprites within a 3D environment", nFlags)
  SetWindowColor(#MainWindow, RGB(0, 0, 0))
  WindowBounds(#MainWindow, WindowWidth, WindowHeight, #PB_Default, #PB_Default)

  If OpenWindowedScreen(WindowID(#MainWindow), 0, 0, WindowWidth, WindowHeight, #True, 0, 0)
    SetFrameRate(60) : SpriteQuality(#PB_Sprite_BilinearFiltering)
    CreateSprite(0, 20, 20, #PB_Sprite_AlphaBlending)

    If StartDrawing(SpriteOutput(0))
      Box(0, 0, 20, 20, RGB(255, 0, 155))
      Box(5, 5, 10, 10, RGB(155, 0, 255))
      StopDrawing()
    EndIf
    direction = 2

    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
    Parse3DScripts()
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 5, 0) 
    CameraLookAt(0, 2, 5, 10)
    TextureSky = LoadTexture(#PB_Any, "sky.png")
    SkyDome(TextureID(TextureSky), $cc6600, $0088ff, 3, 400, -0.5, 0)
    CreateLight(0, $ff88ff, 20000, 40000, 20000)
    AmbientColor($010101)
    Fog($554488,1, 0, 1024 * 4)

    HideWindow(#MainWindow, #False)

    Repeat
      Repeat
        Event = WindowEvent()

        Select Event
          Case #PB_Event_CloseWindow
            Select EventWindow()
              Case #MainWindow : Break 2
            EndSelect
        EndSelect
      Until Not Event
      FlipBuffers() : RenderWorld() : ExamineKeyboard()

      If KeyboardReleased(#PB_Key_Escape) : Break : EndIf

      DisplayTransparentSprite(0, x, x)
      x + direction

      If x > WindowWidth - 20 : direction = -2 : EndIf
      If x < 0 : direction =  2 : EndIf

    ForEver
  EndIf
EndIf
2D Only

Code: Select all

Enumeration
  #MainWindow
EndEnumeration

InitSprite() : InitKeyboard()

WindowWidth = 600 : WindowHeight = 600
nFlags = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered | #PB_Window_Invisible

If OpenWindow(#MainWindow, 0, 0, WindowWidth, WindowHeight, "Include 2D sprites within a 3D environment", nFlags)
  SetWindowColor(#MainWindow, RGB(0, 0, 0))
  WindowBounds(#MainWindow, WindowWidth, WindowHeight, #PB_Default, #PB_Default)

  If OpenWindowedScreen(WindowID(#MainWindow), 0, 0, WindowWidth, WindowHeight, #True, 0, 0)
    SetFrameRate(60) : SpriteQuality(#PB_Sprite_BilinearFiltering)
    CreateSprite(0, 20, 20, #PB_Sprite_AlphaBlending)

    If StartDrawing(SpriteOutput(0))
      Box(0, 0, 20, 20, RGB(255, 0, 155))
      Box(5, 5, 10, 10, RGB(155, 0, 255))
      StopDrawing()
    EndIf
    direction = 2
    HideWindow(#MainWindow, #False)

    Repeat
      Repeat
        Event = WindowEvent()

        Select Event
          Case #PB_Event_CloseWindow
            Select EventWindow()
              Case #MainWindow : Break 2
            EndSelect
        EndSelect
      Until Not Event
      FlipBuffers() : ClearScreen($000000) : ExamineKeyboard()

      If KeyboardReleased(#PB_Key_Escape) : Break : EndIf

      DisplayTransparentSprite(0, x, x)
      x + direction

      If x > WindowWidth - 20 : direction = -2 : EndIf
      If x < 0 : direction =  2 : EndIf

    ForEver
  EndIf
EndIf

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Ziltch
User
User
Posts: 61
Joined: Sun Aug 03, 2003 12:05 am
Location: Australia

Re: Include 2D Sprites Within a 3D Environment

Post by Ziltch »

I came here to report the same.

My code also shows what is going wrong after resizing window.

I do think a way to resize the 3d and enlarge the 2D would be great. But fixing the 2D scaling is more important.

Love that we can now resize the 3D windows now. :D

Code: Select all

#Obj3DGridMaterial = 1
Global SizeGrid = 100
Global WinW = 512, WinH = 512
Global cx,cy

Procedure CreatePointer()
  CreateSprite(1, 32, 32, #PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(1))
  DrawingMode(#PB_2DDrawing_AllChannels) 
  Box(0, 0, 32, 32, RGBA(0, 0, 0, 0))  
  LineXY(16,0,16,31, RGBA(255, 255, 255, 220))
  LineXY(0,16,31,16, RGBA(255, 255, 255, 220))
  Circle(16, 16, 6, RGBA(255, 255, 255, 220)) 
  Circle(16, 16, 4, RGBA(0, 0, 0, 0)) 
  StopDrawing()
EndProcedure

Procedure CreateCircle()
  CreateSprite(0, 32, 32, #PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(0))
  DrawingMode(#PB_2DDrawing_AllChannels) 
  Box(0, 0, 32, 32, RGBA(0, 0, 0, 0))  
  Circle(16, 16, 14, RGBA(255, 255, 0, 255)) 
  StopDrawing()
EndProcedure

Procedure CreateGrid()
  CreateSprite(2,WinW, WinH, #PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(2))
  DrawingMode(#PB_2DDrawing_AllChannels) 
  
  Box(0, 0, WinW, WinH, RGBA(0, 0, 0, 0))  
   For i=0 To WinW-1 Step 16
     LineXY(i, 0, i,WinH, RGBA(255, 25, 0, 255)) 
     LineXY(0, i, WinW,i, RGBA(25, 255, 0, 255)) 
   Next
   DrawText(20,80, "Just change width first a small bit first. then change height")
   DrawText(20,120, "Watch mouse pointer after changed width / height")
  StopDrawing()
EndProcedure


Procedure Make3DGrid()
  Protected GridW =10,GridH=10 
  Protected GridW2 =GridW/2.0,GridH2=GridH/2.0
  Protected GridS1 = 0
  #GridS2 = 1
  #GridS3 = 1
  CreateMesh(SizeGrid,#PB_Mesh_LineList)
  For i=0 To GridH Step #GridS3
    If i%10 =0
      color = RGB(255,255,155)
    Else
      color = RGB(155,55,255)
    EndIf
      
    MeshVertexPosition(-GridW2, 0, (i-GridS1) * #GridS2 -GridH2)
    MeshVertexColor(color)
    MeshVertexPosition(GridW2, 0, (i-GridS1) * #GridS2  -GridH2)
    MeshVertexColor(color)
  Next
  For i=0 To GridW Step #GridS3
    If i%10=0
      color = RGB(255,255,155)
    Else
      color = RGB(55,55,155)
    EndIf
    MeshVertexPosition((i-GridS1) * #GridS2 - GridW2, 0, -GridH2)
    MeshVertexColor(color)
    MeshVertexPosition((i-GridS1) * #GridS2 - GridW2, 0,  GridH2)
    MeshVertexColor(color)
  Next
  FinishMesh(#True)
  CreateMaterial(#Obj3DGridMaterial,#Null)
  CreateEntity(SizeGrid,MeshID(SizeGrid),MaterialID(#Obj3DGridMaterial))  
 ; AttachNodeObject(#CameraNode,EntityID(SizeGrid))
  
  DisableMaterialLighting(#Obj3DGridMaterial,1)
EndProcedure 

If InitEngine3D(#PB_Engine3D_DebugLog)  
  Debug "Start"
Else
  Debug "Could not init 3D"
  CallDebugger
EndIf
If InitSprite() = 0
  MessageRequester("Error", "Can't open screen & sprite environment!", 0)
  End
EndIf
InitMouse()
Camera =0

Procedure UpdateScreen()
      
      RotateEntity(SizeGrid ,0.05,0.0,0.1,#PB_Relative)
      
      ;ExamineMouse() 
      RenderWorld()   
     ; ClearScreen(RGB(0, 0, 200)) ; A blue background
      cx = (cx+ 2) % WindowWidth(0)
      cy =  WindowHeight(0) -cx
      DisplayTransparentSprite(2,0,0) 
      DisplayTransparentSprite(0, cx, 10) 
      DisplayTransparentSprite(0, 260, cy)
      
     ; DisplayTransparentSprite(1,MouseX(),MouseY())
      DisplayTransparentSprite(1,WindowMouseX(0)-16,WindowMouseY(0)-16,200)
      
    ;  DskMseX = DesktopMouseX()-WindowX(0)-8
    ;  DskMseY = DesktopMouseY()-WindowY(0)-31
      
   ;   DisplayTransparentSprite(1,DskMseX-16,DskMseY-16,250,#Red)
      
      StatusBarText(0, 0, "win mouse "+Str(WindowMouseX(0))+ ","+Str(WindowMouseY(0))+" desk mouse "+Str(DskMseX)+ ","+Str(DskMseY)+ "  "+Str(cx))

      FlipBuffers()
EndProcedure


If OpenWindow(0, 0, 0, WinW, WinH, "2D resize error when using 3D.", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
  CreateStatusBar(0, WindowID(0))
  AddStatusBarField(420)
    
  StatusBarText(0, 0, "Automatically zoomed screen area when changing window size...")

  If OpenWindowedScreen(WindowID(0), 0, 0, WinW, WinH, #True, 0, 20)
    
    AddWindowTimer(0,0,20)
    
    CreateCamera(Camera, 0, 0, 100, 100)
    MoveCamera(Camera,0,0,-12.3)
    CameraLookAt(Camera,0,0,0)
    
  
    CreateLight(0,$ffffff, 10000, 10000, 0000)
    AmbientColor($777777)
    Make3DGrid()
    RotateEntity(SizeGrid ,90,0,0)
  
    CreatePointer()
    CreateCircle()
    CreateGrid()
    
    Repeat
      ; It's very important to process all the events remaining in the queue at each frame
      ;
      Repeat
        Event = WaitWindowEvent(10)
        
        If Event = #PB_Event_Timer
          UpdateScreen()
        ElseIf Event = #PB_Event_CloseWindow
          End
        EndIf
      Until Event = 0
      
      

    ForEver
    
  Else
    MessageRequester("Error", "Can't open windowed screen!", 0)
  EndIf
EndIf
Fred
Administrator
Administrator
Posts: 18342
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Include 2D Sprites Within a 3D Environment

Post by Fred »

Fixed.
User avatar
JHPJHP
Addict
Addict
Posts: 2260
Joined: Sat Oct 09, 2010 3:47 am

Re: [Done] Include 2D Sprites Within a 3D Environment

Post by JHPJHP »

Hi Fred and Team,

Thank you for the fix, including the recent one for 2D Sprites Auto Stretch After CloseScreen.

I’ve noticed that, in some cases, previous versions of PureBasic are updated (where applicable) to include certain fixes. With each major PureBasic release, I review my scripts and programs to ensure compatibility and make any necessary adjustments for the latest version.

Historically, my approach has been either to occasionally add compiler conditions based on PureBasic versions or, more often, to make no additional changes and rely on compatibility.

My question: How can we determine which fixes have been applied to earlier versions of PureBasic?

Thank you for your time and support.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Fred
Administrator
Administrator
Posts: 18342
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [Done] Include 2D Sprites Within a 3D Environment

Post by Fred »

As there is no more LTS version, fixes are no more applied to previous versions anymore.
AZJIO
Addict
Addict
Posts: 2213
Joined: Sun May 14, 2017 1:48 am

Re: [Done] Include 2D Sprites Within a 3D Environment

Post by AZJIO »

Fred wrote: Tue Sep 16, 2025 5:43 pm As there is no more LTS version, fixes are no more applied to previous versions anymore.
Can you make some improvements for version 6.04 only for Windows, as an exception to the rules? This version creates compact files. It is not necessary to update the functions for 3D games, as the size of the executable file does not matter for them. However, some new functions would be useful for small programs, such as CustomSortList() and CustomSortArray().
We can create a survey to see if this topic is interesting to users.
Fred
Administrator
Administrator
Posts: 18342
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [Done] Include 2D Sprites Within a 3D Environment

Post by Fred »

No, it's way too much work to support an old version. You can also create compact files with new version by checking the UCRT checkbox which will work on all Win 10+
Post Reply