Page 1 of 1

Is it possible to render 3d objects on the 2d layer?

Posted: Tue Feb 04, 2025 4:14 am
by skinkairewalker
hello everyone, how u doing ?
Is it possible to render 3d objects on the 2d layer?
draw objects or camera with transparent background so that 3d objects are in front of the user interface.
similar to those mmorpg games, which display your own character's face next to the life bar, or in the user's inventory which shows the character's appearance before applying

like this :
screenshot 1 :
Image

screenshot 2 :
Image

screenshot 3 :
Image

Re: Is it possible to render 3d objects on the 2d layer?

Posted: Tue Feb 04, 2025 4:18 am
by miso
Yes. Rendertextures + maybe ortho camera.

Re: Is it possible to render 3d objects on the 2d layer?

Posted: Tue Feb 04, 2025 4:20 am
by skinkairewalker
miso wrote: Tue Feb 04, 2025 4:18 am Yes. Rendertextures + maybe ortho camera.
Do you have any examples?

Re: Is it possible to render 3d objects on the 2d layer?

Posted: Tue Feb 04, 2025 4:53 am
by miso
Not in the way you descibed, but the idea is the same.
I have a prototyping example code I made. I'm not sure how helpful it is... ( full of magic numbers, quad does not placed using frustum computations, but has rendering to a texture, that is textured on a quad as an overlay. My goal was to render world in a lower resolution than the screen resolution, that can be high, and not really 3d user interface overlay, but thats possible too with that way too.)

Camera masking is the key, it shows only objects on render that are associated with camera.

Weathever, heres the code. Please don't be too harsh, it was not really meant to be shown here:

Code: Select all

#MASK_GENERALPICKMASK=1<<1

#MASK_MAINCAMERA=1<<1
#MASK_RENDERCAMERA=1<<31


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

ExamineDesktops()
OpenWindow(0, 0,0, DesktopWidth(0)*0.8,DesktopHeight(0)*0.8, "renderquad",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0), 0, 0, 0)
maincamera=CreateCamera(-1,0,0,100,100,#MASK_MAINCAMERA)
CameraBackColor(maincamera,(RGB(0,0,0)))
rendercamera=CreateCamera(-1,0,0,100,100,#MASK_RENDERCAMERA)
CameraBackColor(rendercamera,(RGB(255,0,255)))
CameraProjectionMode(rendercamera,#PB_Camera_Orthographic)

spheremesh=CreateSphere(-1,100)

planemesh=CreatePlane(-1,WindowWidth(0)*1.2,WindowHeight(0)*1.2,1,1,1,1)
mylight= CreateLight(-1,RGB(255,255,255),200,200,200)
rendertexture = CreateRenderTexture(-1,CameraID(maincamera),320,200,#PB_Texture_AutomaticUpdate)
rendermaterial =CreateMaterial(-1,TextureID(rendertexture))
DisableMaterialLighting(rendermaterial,#True)
MaterialFilteringMode(rendermaterial,#PB_Material_None)

AmbientColor(RGB(150,150,150))

myobj = CreateEntity(-1,MeshID(spheremesh),#PB_Material_None,0,0,0,#MASK_GENERALPICKMASK,#MASK_MAINCAMERA)
myplane=CreateEntity(-1,MeshID(planemesh),#PB_Material_None,0,-100,0,#MASK_GENERALPICKMASK,#MASK_MAINCAMERA)

mainquadobj=CreateEntity(-1,MeshID(planemesh),MaterialID(rendermaterial),0,0,-100000,#MASK_GENERALPICKMASK,#MASK_RENDERCAMERA)
RotateEntity(mainquadobj,90,180,0,#PB_Absolute)

MoveCamera(rendercamera,0,0,-500,#PB_Absolute|#PB_World)

MoveCamera(maincamera,0,0,-500,#PB_Absolute|#PB_World)
MoveEntity(myobj,0,0,0,#PB_Absolute|#PB_World)
CameraLookAt(maincamera,EntityX(myobj),EntityY(myobj),EntityZ(myobj))
Repeat
	While WindowEvent():Wend
	ExamineKeyboard()
	ExamineMouse()
	If KeyboardPushed(#PB_Key_Left) : RotateCamera(maincamera,0,1,0,#PB_Relative): EndIf
	If KeyboardPushed(#PB_Key_Right) : RotateCamera(maincamera,0,-1.0,0,#PB_Relative): EndIf
	If KeyboardPushed(#PB_Key_Up) : MoveCamera(maincamera,0,0,-10,#PB_Local|#PB_Relative): EndIf
	If KeyboardPushed(#PB_Key_Down) : MoveCamera(maincamera,0,0,10,#PB_Local|#PB_Relative): EndIf
	RotateEntity(myobj,0,0.1,0,#PB_Relative)
	RenderWorld()
	FlipBuffers()    
Until KeyboardReleased(#PB_Key_Escape) Or MouseButton(3)

Re: Is it possible to render 3d objects on the 2d layer?

Posted: Tue Feb 04, 2025 5:41 am
by miso
Uhm, so maybe it does not show how this is related to your question. I mean I could create a GUI camera, that renders only gui elements, and that can be shown as an overlay on a quad. Camerabackcolor can be dropped using a simple shader for example.

Re: Is it possible to render 3d objects on the 2d layer?

Posted: Tue Feb 04, 2025 2:35 pm
by skinkairewalker
Excellent, I am immensely grateful for the example.
It gave me an idea of ​​what needs to be done

Re: Is it possible to render 3d objects on the 2d layer?

Posted: Wed Apr 02, 2025 2:24 pm
by Nituvious
With OpenGL to achieve this effect all you need to do is change your projection before the draw call of the mesh you want to draw as "2D" :mrgreen:

Re: Is it possible to render 3d objects on the 2d layer?

Posted: Thu Apr 03, 2025 3:17 pm
by miso
Nituvious wrote: Wed Apr 02, 2025 2:24 pm With OpenGL to achieve this effect all you need to do is change your projection before the draw call of the mesh you want to draw as "2D" :mrgreen:
But we, common people has to work with what is given ;) With the fixes came with the new 6.21 betas, I belive it's not a problem anymore.

Fixed the code to be DPI aware, and quad always fitting to the opened screen. (no enableexplicit was tested, just modified the old code)

Code: Select all

#MASK_GENERALPICKMASK=1<<1

#MASK_MAINCAMERA=1<<1
#MASK_RENDERCAMERA=1<<31


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

ExamineDesktops()
OpenWindow(0, 0,0, DesktopUnscaledX(DesktopWidth(0))*0.8,DesktopUnscaledY(DesktopHeight(0))*0.8, "renderquad",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0), 0, 0, 0)
maincamera=CreateCamera(-1,0,0,100,100,#MASK_MAINCAMERA)
CameraBackColor(maincamera,(RGB(0,0,0)))
rendercamera=CreateCamera(-1,0,0,100,100,#MASK_RENDERCAMERA)
CameraBackColor(rendercamera,(RGB(255,0,255)))
CameraProjectionMode(rendercamera,#PB_Camera_Orthographic)

spheremesh=CreateSphere(-1,100)

planemesh=CreatePlane(-1,ScreenWidth()/ScreenHeight()*1000,1000,1,1,1,1)
mylight= CreateLight(-1,RGB(255,255,255),200,200,200)
rendertexture = CreateRenderTexture(-1,CameraID(maincamera),320,200,#PB_Texture_AutomaticUpdate)
rendermaterial =CreateMaterial(-1,TextureID(rendertexture))
DisableMaterialLighting(rendermaterial,#True)
MaterialFilteringMode(rendermaterial,#PB_Material_None)

AmbientColor(RGB(150,150,150))

myobj = CreateEntity(-1,MeshID(spheremesh),#PB_Material_None,0,0,0,#MASK_GENERALPICKMASK,#MASK_MAINCAMERA)
myplane=CreateEntity(-1,MeshID(planemesh),#PB_Material_None,0,-100,0,#MASK_GENERALPICKMASK,#MASK_MAINCAMERA)

mainquadobj=CreateEntity(-1,MeshID(planemesh),MaterialID(rendermaterial),0,0,-100000,#MASK_GENERALPICKMASK,#MASK_RENDERCAMERA)
RotateEntity(mainquadobj,90,180,0,#PB_Absolute)

MoveCamera(rendercamera,0,0,-500,#PB_Absolute|#PB_World)

MoveCamera(maincamera,0,0,-500,#PB_Absolute|#PB_World)
MoveEntity(myobj,0,0,0,#PB_Absolute|#PB_World)
CameraLookAt(maincamera,EntityX(myobj),EntityY(myobj),EntityZ(myobj))
Repeat
	While WindowEvent():Wend
	ExamineKeyboard()
	ExamineMouse()
	If KeyboardPushed(#PB_Key_Left) : RotateCamera(maincamera,0,1,0,#PB_Relative): EndIf
	If KeyboardPushed(#PB_Key_Right) : RotateCamera(maincamera,0,-1.0,0,#PB_Relative): EndIf
	If KeyboardPushed(#PB_Key_Up) : MoveCamera(maincamera,0,0,-10,#PB_Local|#PB_Relative): EndIf
	If KeyboardPushed(#PB_Key_Down) : MoveCamera(maincamera,0,0,10,#PB_Local|#PB_Relative): EndIf
	RotateEntity(myobj,0,0.1,0,#PB_Relative)
	RenderWorld()
	FlipBuffers()    
Until KeyboardReleased(#PB_Key_Escape) Or MouseButton(3)

Re: Is it possible to render 3d objects on the 2d layer?

Posted: Sat Apr 05, 2025 12:15 am
by minimy
Hello skinwalker and miso! Im making an inventary system for my project, had same problem to make any thing like yours images (screenshot 1).
My solution was using second camera combined with sprite to make the effect and put text over 3D image (second camera).
The trick is put second camera and target object outside of your scene.

This is the example canwork as start point. I hope this can help you in your project.

Code: Select all


fnt1= LoadFont(#PB_Any,"Arial",12)
fnt2= LoadFont(#PB_Any,"Arial",20)
fnt3= LoadFont(#PB_Any,"Impact",40)


Global  camara
Procedure   iniDX(title.s="")
  Protected w,h
  InitEngine3D()
    InitSprite()
    InitKeyboard()
    InitMouse()
    OpenWindow(0,0,0,800,600,title,#PB_Window_ScreenCentered|#PB_Window_BorderLess|#PB_Window_Maximize);|#PB_Window_Invisible)
    SetWindowColor(0,$444444)
    w= WindowWidth(0):h=WindowHeight(0)
    AntialiasingMode(#PB_AntialiasingMode_None)
    OpenWindowedScreen(WindowID(0), 0, 0, w,h, 0,0,0,#PB_Screen_NoSynchronization)
    KeyboardMode(#PB_Keyboard_International)
    s=128
    WorldShadows(#PB_Shadow_Modulative, -1, RGB(s,s,s), 2048)
    EnableWorldPhysics(1)
    EnableWorldCollisions(1)
EndProcedure
Procedure   eventos3D()
  Protected.f speed=0.1, mouseSpd=0.05
  Protected.f KeyX,KeyY, MouseX,MouseY
  If KeyboardPushed(#PB_Key_A)
    KeyX= -speed
  ElseIf KeyboardPushed(#PB_Key_D)
    KeyX= speed
  Else
    KeyX= 0
  EndIf
  
  If KeyboardPushed(#PB_Key_W)
    KeyY= -speed
  ElseIf KeyboardPushed(#PB_Key_S)
    KeyY= speed
  Else
    KeyY= 0
  EndIf
  
  If KeyboardPushed(#PB_Key_LeftShift)
    keyY * 10
    keyX * 10
  EndIf

  MouseX = -MouseDeltaX() * mouseSpd
  MouseY = -MouseDeltaY() * mouseSpd
  
  RotateCamera(camara, MouseY, MouseX, 0, #PB_Relative)
  MoveCamera (camara, KeyX, 0, KeyY)
EndProcedure
Procedure   creaSuelo(size=50, col1.l=$0,col2.l=$aaaaaa, res=256)
  Protected.i suelo_tx3D, suelo_mate, suelo_mesh, suelo
  suelo_tx3D=   CreateTexture(#PB_Any,res,res)
  StartDrawing(TextureOutput(suelo_tx3D))
    Box(0,0,OutputWidth(),OutputHeight(),col1)
    Box(2,2,OutputWidth()-4,OutputHeight()-4,col2)
  StopDrawing()
  
  suelo_mesh=   CreatePlane(#PB_Any,size,size,1,1,1,1)
  suelo_mate=   CreateMaterial(#PB_Any,TextureID(suelo_tx3D))
                ScaleMaterial(suelo_mate,1/size,1/size)
  suelo=        CreateEntity(#PB_Any, MeshID(suelo_mesh), MaterialID(suelo_mate))
ProcedureReturn suelo
EndProcedure

iniDX("Test Overlay")
creaSuelo()

Macro tabla(x,y,w,h)
  Box(x,y,w-4,h-4,$8899aa)
  Box(x+4,y+4,w-4,h-4,$112233)
  Box(x+2,y+2,w-4,h-4,$556677)
EndMacro
tx3d= CreateTexture(#PB_Any,200,200)
im3d= CreateImage(#PB_Any,200,200)
StartDrawing(ImageOutput(im3d))
w= OutputWidth()/5
For p=0 To 4
  tabla(w*p,0,w,OutputHeight())
Next p
tabla(0,w/2,OutputWidth(),w)
tabla(0,w*3.5,OutputWidth(),w)
DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
DrawingFont(FontID(fnt3))
txt.s="ARMY"
DrawText((OutputWidth()-TextWidth(txt))/2,(OutputHeight()-TextHeight(txt))/2,txt,$55ffffff)
StopDrawing()
StartDrawing(TextureOutput(tx3d))
  DrawAlphaImage(ImageID(im3d),0,0)
StopDrawing()

cubo_mesh=    CreateCube(#PB_Any,2)
cubo_verd=    CreateMaterial(#PB_Any, TextureID(tx3d))
; cubo_verd=    CreateMaterial(#PB_Any, #Null,$00ff00)
cubo_rojo=    CreateMaterial(#PB_Any, #Null,$0000ff)
cubo_entR=    CreateEntity(#PB_Any, MeshID(cubo_mesh), MaterialID(cubo_rojo),0,1,0)
cubo_entV=    CreateEntity(#PB_Any, MeshID(cubo_mesh), MaterialID(cubo_verd),50,1,0)
luz=          CreateLight(#PB_Any,$ffffff,10,30,30,#PB_Light_Point)

camara=       CreateCamera(#PB_Any,0,0,100,100,1 << 1)
              MoveCamera(camara,10,10,10)
              CameraLookAt(camara,0,0,0)
              CameraRange(camara, 0.1, 999999)
              CameraBackColor(camara,$555555)
              
camMenu=      CreateCamera(#PB_Any,40,30,20,50,1 << 1)
              MoveCamera(camMenu,40,1,0)
              CameraLookAt(camMenu,50,1,0)
              CameraBackColor(camMenu,$007700)
              
imgDeco=      CreateImage(#PB_Any,16+WindowWidth(0)*0.2,16+WindowHeight(0)*0.5,32,#PB_Image_Transparent)
sprDeco=      CreateSprite(#PB_Any,ImageWidth(imgDeco),ImageHeight(imgDeco),#PB_Sprite_AlphaBlending)
              StartDrawing(ImageOutput(imgDeco))
                DrawingMode(#PB_2DDrawing_AlphaBlend)
                RoundBox(0,0,OutputWidth(),OutputHeight(),25,25,$ff000000)
                DrawingMode(#PB_2DDrawing_AllChannels)
                RoundBox(16,16,OutputWidth()-32,OutputHeight()-32,15,15,$00000000)
              StopDrawing()
              StartDrawing(SpriteOutput(sprDeco))
                DrawAlphaImage(ImageID(imgDeco),0,0)
                DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
                DrawingFont(FontID(fnt2))
                DrawText(20,20,"Box",$ff00ffff)
                DrawingFont(FontID(fnt1))
                DrawText(20,OutputHeight()-80,"This is a box",$ff00ffff)
                DrawText(20,OutputHeight()-60,"Simple box of wood",$ff00ffff)
              StopDrawing()
sprDecoX= -8+WindowWidth(0)*0.4
sprDecoY= -8+WindowHeight(0)*0.3


              
Repeat
  contador+ 1
  ;{ WINDOWS Y GADGETS
  Repeat : event= WindowEvent()
  Until event= 0
  ;}
  ExamineKeyboard() : ExamineMouse()
  eventos3D()
  
  RotateEntity(cubo_entV,0.25,1,0.5,#PB_Relative)
  
  If KeyboardPushed(#PB_Key_Escape) : sal= 1: EndIf
  RenderTime = RenderWorld()
  DisplayTransparentSprite(sprDeco, sprDecoX,sprDecoY)
  FlipBuffers()
  Delay(1)
Until sal=1


Re: Is it possible to render 3d objects on the 2d layer?

Posted: Sat Apr 05, 2025 1:43 am
by miso
Thanks Minimy for sharing.
The trick is put second camera and target object outside of your scene.
You don't have to. Camera and object masks select what is rendered and whats not on a specified camera. (choose different masks for the two camera, and the two scene entities.) But careful placement can also work.

Re: Is it possible to render 3d objects on the 2d layer?

Posted: Sat Apr 05, 2025 2:06 am
by minimy
Hey miso! Yes, i forget masks, thanks for remember

Re: Is it possible to render 3d objects on the 2d layer?

Posted: Sat Apr 05, 2025 2:12 am
by miso
This way the crate won't be seen in your main camera ( or your world scene in your item camera )
Edit: Something is off with 6.21 beta 4 with this setup, it seems to work with older compilers.
Edit 2: It's the behavior of the 2d drawing commands, that differs, I put a fix some posts down, so one can examine the differences.

Code: Select all

fnt1= LoadFont(#PB_Any,"Arial",12)
fnt2= LoadFont(#PB_Any,"Arial",20)
fnt3= LoadFont(#PB_Any,"Impact",40)


Global  camara
Procedure   iniDX(title.s="")
  Protected w,h
  InitEngine3D()
    InitSprite()
    InitKeyboard()
    InitMouse()
    OpenWindow(0,0,0,800,600,title,#PB_Window_ScreenCentered|#PB_Window_BorderLess|#PB_Window_Maximize);|#PB_Window_Invisible)
    SetWindowColor(0,$444444)
    w= WindowWidth(0):h=WindowHeight(0)
    AntialiasingMode(#PB_AntialiasingMode_None)
    OpenWindowedScreen(WindowID(0), 0, 0, w,h, 0,0,0,#PB_Screen_NoSynchronization)
    KeyboardMode(#PB_Keyboard_International)
    s=128
   ; WorldShadows(#PB_Shadow_Modulative, -1, RGB(s,s,s), 2048)
    EnableWorldPhysics(1)
    EnableWorldCollisions(1)
EndProcedure
Procedure   eventos3D()
  Protected.f speed=0.1, mouseSpd=0.05
  Protected.f KeyX,KeyY, MouseX,MouseY
  If KeyboardPushed(#PB_Key_A)
    KeyX= -speed
  ElseIf KeyboardPushed(#PB_Key_D)
    KeyX= speed
  Else
    KeyX= 0
  EndIf
  
  If KeyboardPushed(#PB_Key_W)
    KeyY= -speed
  ElseIf KeyboardPushed(#PB_Key_S)
    KeyY= speed
  Else
    KeyY= 0
  EndIf
  
  If KeyboardPushed(#PB_Key_LeftShift)
    keyY * 10
    keyX * 10
  EndIf

  MouseX = -MouseDeltaX() * mouseSpd
  MouseY = -MouseDeltaY() * mouseSpd
  
  RotateCamera(camara, MouseY, MouseX, 0, #PB_Relative)
  MoveCamera (camara, KeyX, 0, KeyY)
EndProcedure
Procedure   creaSuelo(size=50, col1.l=$0,col2.l=$aaaaaa, res=256)
  Protected.i suelo_tx3D, suelo_mate, suelo_mesh, suelo
  suelo_tx3D=   CreateTexture(#PB_Any,res,res)
  StartDrawing(TextureOutput(suelo_tx3D))
    Box(0,0,OutputWidth(),OutputHeight(),col1)
    Box(2,2,OutputWidth()-4,OutputHeight()-4,col2)
  StopDrawing()
  
  suelo_mesh=   CreatePlane(#PB_Any,size,size,1,1,1,1)
  suelo_mate=   CreateMaterial(#PB_Any,TextureID(suelo_tx3D))
                ScaleMaterial(suelo_mate,1/size,1/size)
  suelo=        CreateEntity(#PB_Any, MeshID(suelo_mesh), MaterialID(suelo_mate),0,0,0,0,1<<1)
ProcedureReturn suelo
EndProcedure

iniDX("Test Overlay")
creaSuelo()

Macro tabla(x,y,w,h)
  Box(x,y,w-4,h-4,$8899aa)
  Box(x+4,y+4,w-4,h-4,$112233)
  Box(x+2,y+2,w-4,h-4,$556677)
EndMacro
tx3d= CreateTexture(#PB_Any,200,200)
im3d= CreateImage(#PB_Any,200,200)
StartDrawing(ImageOutput(im3d))
w= OutputWidth()/5
For p=0 To 4
  tabla(w*p,0,w,OutputHeight())
Next p
tabla(0,w/2,OutputWidth(),w)
tabla(0,w*3.5,OutputWidth(),w)
DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
DrawingFont(FontID(fnt3))
txt.s="ARMY"
DrawText((OutputWidth()-TextWidth(txt))/2,(OutputHeight()-TextHeight(txt))/2,txt,$55ffffff)
StopDrawing()
StartDrawing(TextureOutput(tx3d))
  DrawAlphaImage(ImageID(im3d),0,0)
StopDrawing()

cubo_mesh=    CreateCube(#PB_Any,2)
cubo_verd=    CreateMaterial(#PB_Any, TextureID(tx3d))
; cubo_verd=    CreateMaterial(#PB_Any, #Null,$00ff00)
cubo_rojo=    CreateMaterial(#PB_Any, #Null,$0000ff)
cubo_entR=    CreateEntity(#PB_Any, MeshID(cubo_mesh), MaterialID(cubo_rojo),0,1,0,0,1<<1)
cubo_entV=    CreateEntity(#PB_Any, MeshID(cubo_mesh), MaterialID(cubo_verd),50,1,0,0,1<<2)
luz=          CreateLight(#PB_Any,$ffffff,10,30,30,#PB_Light_Point)

camara=       CreateCamera(#PB_Any,0,0,100,100,1 << 1)
              MoveCamera(camara,10,10,10)
              CameraLookAt(camara,0,0,0)
              CameraRange(camara, 0.1, 999999)
              CameraBackColor(camara,$555555)
              
camMenu=      CreateCamera(#PB_Any,40,30,20,50,1 << 2)
              MoveCamera(camMenu,40,1,0)
              CameraLookAt(camMenu,50,1,0)
              CameraBackColor(camMenu,$007700)
              
imgDeco=      CreateImage(#PB_Any,16+WindowWidth(0)*0.2,16+WindowHeight(0)*0.5,32,#PB_Image_Transparent)
sprDeco=      CreateSprite(#PB_Any,ImageWidth(imgDeco),ImageHeight(imgDeco),#PB_Sprite_AlphaBlending)
              StartDrawing(ImageOutput(imgDeco))
                DrawingMode(#PB_2DDrawing_AlphaBlend)
                RoundBox(0,0,OutputWidth(),OutputHeight(),25,25,$ff000000)
                DrawingMode(#PB_2DDrawing_AllChannels)
                RoundBox(16,16,OutputWidth()-32,OutputHeight()-32,15,15,$00000000)
              StopDrawing()
              StartDrawing(SpriteOutput(sprDeco))
                DrawAlphaImage(ImageID(imgDeco),0,0)
                DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
                DrawingFont(FontID(fnt2))
                DrawText(20,20,"Box",$ff00ffff)
                DrawingFont(FontID(fnt1))
                DrawText(20,OutputHeight()-80,"This is a box",$ff00ffff)
                DrawText(20,OutputHeight()-60,"Simple box of wood",$ff00ffff)
              StopDrawing()
sprDecoX= -8+WindowWidth(0)*0.4
sprDecoY= -8+WindowHeight(0)*0.3


              
Repeat
  contador+ 1
  ;{ WINDOWS Y GADGETS
  Repeat : event= WindowEvent()
  Until event= 0
  ;}
  ExamineKeyboard() : ExamineMouse()
  eventos3D()
  
  RotateEntity(cubo_entV,0.25,1,0.5,#PB_Relative)
  
  If KeyboardPushed(#PB_Key_Escape) : sal= 1: EndIf
  RenderTime = RenderWorld()
  DisplayTransparentSprite(sprDeco, sprDecoX,sprDecoY)
  FlipBuffers()
  Delay(1)
Until sal=1


Re: Is it possible to render 3d objects on the 2d layer?

Posted: Sat Apr 05, 2025 2:51 pm
by minimy
Hi miso, very nice! Now can put the box in coord 0,1,0, camera in coord -10,1,0 and work fine. Here with PB 6.02 work too.
I change this line:

Code: Select all

  RotateEntity(cubo_entV,0.25,1,0.5,#PB_Relative)
with this, to keep constant rotation speed of the box. (independent of PC speed)

Code: Select all

  RotateEntity(cubo_entV,0.025*RenderTime,0.1*RenderTime,0.05*RenderTime,#PB_Relative)

Re: Is it possible to render 3d objects on the 2d layer?

Posted: Sat Apr 05, 2025 8:01 pm
by miso
Hello Minimy. I figured out, that the 2d drawing commands are the ones, that slightly work differently. That's why the sprite was black in your example with the newest beta.

This code works with either the old versions or the newest 6.21 beta4. (sprite draw modes are changed)

Code: Select all

fnt1= LoadFont(#PB_Any,"Arial",12)
fnt2= LoadFont(#PB_Any,"Arial",20)
fnt3= LoadFont(#PB_Any,"Impact",40)


Global  camara
Procedure   iniDX(title.s="")
  Protected w,h
  InitEngine3D()
    InitSprite()
    InitKeyboard()
    InitMouse()
    OpenWindow(0,0,0,800,600,title,#PB_Window_ScreenCentered|#PB_Window_BorderLess|#PB_Window_Maximize);|#PB_Window_Invisible)
    SetWindowColor(0,$444444)
    w= WindowWidth(0):h=WindowHeight(0)
    AntialiasingMode(#PB_AntialiasingMode_None)
    OpenWindowedScreen(WindowID(0), 0, 0, w,h, 0,0,0,#PB_Screen_NoSynchronization)
    KeyboardMode(#PB_Keyboard_International)
    s=128
   ; WorldShadows(#PB_Shadow_Modulative, -1, RGB(s,s,s), 2048)
    EnableWorldPhysics(1)
    EnableWorldCollisions(1)
EndProcedure
Procedure   eventos3D()
  Protected.f speed=0.1, mouseSpd=0.05
  Protected.f KeyX,KeyY, MouseX,MouseY
  If KeyboardPushed(#PB_Key_A)
    KeyX= -speed
  ElseIf KeyboardPushed(#PB_Key_D)
    KeyX= speed
  Else
    KeyX= 0
  EndIf
  
  If KeyboardPushed(#PB_Key_W)
    KeyY= -speed
  ElseIf KeyboardPushed(#PB_Key_S)
    KeyY= speed
  Else
    KeyY= 0
  EndIf
  
  If KeyboardPushed(#PB_Key_LeftShift)
    keyY * 10
    keyX * 10
  EndIf

  MouseX = -MouseDeltaX() * mouseSpd
  MouseY = -MouseDeltaY() * mouseSpd
  
  RotateCamera(camara, MouseY, MouseX, 0, #PB_Relative)
  MoveCamera (camara, KeyX, 0, KeyY)
EndProcedure
Procedure   creaSuelo(size=50, col1.l=$0,col2.l=$aaaaaa, res=256)
  Protected.i suelo_tx3D, suelo_mate, suelo_mesh, suelo
  suelo_tx3D=   CreateTexture(#PB_Any,res,res)
  StartDrawing(TextureOutput(suelo_tx3D))
    Box(0,0,OutputWidth(),OutputHeight(),col1)
    Box(2,2,OutputWidth()-4,OutputHeight()-4,col2)
  StopDrawing()
  
  suelo_mesh=   CreatePlane(#PB_Any,size,size,1,1,1,1)
  suelo_mate=   CreateMaterial(#PB_Any,TextureID(suelo_tx3D))
                ScaleMaterial(suelo_mate,1/size,1/size)
  suelo=        CreateEntity(#PB_Any, MeshID(suelo_mesh), MaterialID(suelo_mate),0,0,0,0,1<<1)
ProcedureReturn suelo
EndProcedure

iniDX("Test Overlay")
creaSuelo()

Macro tabla(x,y,w,h)
  Box(x,y,w-4,h-4,$8899aa)
  Box(x+4,y+4,w-4,h-4,$112233)
  Box(x+2,y+2,w-4,h-4,$556677)
EndMacro
tx3d= CreateTexture(#PB_Any,200,200)
im3d= CreateImage(#PB_Any,200,200)
StartDrawing(ImageOutput(im3d))
w= OutputWidth()/5
For p=0 To 4
  tabla(w*p,0,w,OutputHeight())
Next p
tabla(0,w/2,OutputWidth(),w)
tabla(0,w*3.5,OutputWidth(),w)
DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
DrawingFont(FontID(fnt3))
txt.s="ARMY"
DrawText((OutputWidth()-TextWidth(txt))/2,(OutputHeight()-TextHeight(txt))/2,txt,$55ffffff)
StopDrawing()
StartDrawing(TextureOutput(tx3d))
  DrawAlphaImage(ImageID(im3d),0,0)
StopDrawing()

cubo_mesh=    CreateCube(#PB_Any,2)
cubo_verd=    CreateMaterial(#PB_Any, TextureID(tx3d))
; cubo_verd=    CreateMaterial(#PB_Any, #Null,$00ff00)
cubo_rojo=    CreateMaterial(#PB_Any, #Null,$0000ff)
cubo_entR=    CreateEntity(#PB_Any, MeshID(cubo_mesh), MaterialID(cubo_rojo),0,1,0,0,1<<1)
cubo_entV=    CreateEntity(#PB_Any, MeshID(cubo_mesh), MaterialID(cubo_verd),50,1,0,0,1<<2)
luz=          CreateLight(#PB_Any,$ffffff,10,30,30,#PB_Light_Point)

camara=       CreateCamera(#PB_Any,0,0,100,100,1 << 1)
              MoveCamera(camara,10,10,10)
              CameraLookAt(camara,0,0,0)
              CameraRange(camara, 0.1, 999999)
              CameraBackColor(camara,$555555)
              
camMenu=      CreateCamera(#PB_Any,40,30,20,50,1 << 2)
              MoveCamera(camMenu,40,1,0)
              CameraLookAt(camMenu,50,1,0)
              CameraBackColor(camMenu,$007700)
              
imgDeco=      CreateImage(#PB_Any,16+WindowWidth(0)*0.2,16+WindowHeight(0)*0.5,32,#PB_Image_Transparent)
sprDeco=      CreateSprite(#PB_Any,ImageWidth(imgDeco),ImageHeight(imgDeco),#PB_Sprite_AlphaBlending)
              StartDrawing(ImageOutput(imgDeco))
              DrawingMode(#PB_2DDrawing_AllChannels)
              DrawingMode(#PB_2DDrawing_AlphaBlend)
                RoundBox(0,0,OutputWidth(),OutputHeight(),25,25,$ff000000)
                DrawingMode(#PB_2DDrawing_AllChannels)
                RoundBox(16,16,OutputWidth()-32,OutputHeight()-32,15,15,$00000000)
              StopDrawing()
              StartDrawing(SpriteOutput(sprDeco))
                DrawingMode(#PB_2DDrawing_AllChannels)
                Box(0,0,OutputWidth(),OutputHeight(),RGBA(0,0,0,0))
                
                DrawingMode(#PB_2DDrawing_AlphaBlend)
                DrawAlphaImage(ImageID(imgDeco),0,0)
                DrawingFont(FontID(fnt2))
                DrawText(20,20,"Box",$ff00ffff,$00000000)
                DrawingFont(FontID(fnt1))
                DrawText(20,OutputHeight()-80,"This is a box",$ff00ffff,$00000000)
                DrawText(20,OutputHeight()-60,"Simple box of wood",$ff00ffff,$00000000)
              StopDrawing()
sprDecoX= -8+WindowWidth(0)*0.4
sprDecoY= -8+WindowHeight(0)*0.3


              
Repeat
  contador+ 1
  ;{ WINDOWS Y GADGETS
  Repeat : event= WindowEvent()
  Until event= 0
  ;}
  ExamineKeyboard() : ExamineMouse()
  eventos3D()
  
  RotateEntity(cubo_entV,0.25,1,0.5,#PB_Relative)
  
  If KeyboardPushed(#PB_Key_Escape) : sal= 1: EndIf
  RenderTime = RenderWorld()
  DisplayTransparentSprite(sprDeco, sprDecoX,sprDecoY)
  FlipBuffers()
  Delay(1)
Until sal=1