3D snippets
Posted: Sun Apr 13, 2025 11:51 pm
I think it would be interesting to have a post about help tools for the 3D environment. What exactly do I mean? Keyboards, maps, controls, mazes, warnings, radars, FX, or anything that can be easily added to a game or app.
That's why I've created this post.
In it, we could include code snippets to help with specific tasks.
I want to start it with this keyboard for entering text.
Input keyboard for 2D/3D environment
It works with both the physical and virtual keyboard.
It's sprite-based, so it's quite user-friendly.
The result phrase is stored in kb\txt.
Right-clicking the mouse shows or hides the keyboard.
Include sprite mouse cursor.
I followed miso's advice, and it should also work in new versions.
I read that skinkairewalker is looking for something like this. I hope it helps.
Keyboard is Spanish, but you can change. In procedure spriteKeyboard(x,y,w,h,txt.s="Keyboard") change 'Ñ' in kb\lett with your own country keyboard.
Solved black sprite. (i think...
)
TESTED: 6.02 and 6.20
That's why I've created this post.
In it, we could include code snippets to help with specific tasks.
I want to start it with this keyboard for entering text.
Input keyboard for 2D/3D environment
It works with both the physical and virtual keyboard.
It's sprite-based, so it's quite user-friendly.
The result phrase is stored in kb\txt.
Right-clicking the mouse shows or hides the keyboard.
Include sprite mouse cursor.
I followed miso's advice, and it should also work in new versions.
I read that skinkairewalker is looking for something like this. I hope it helps.
Keyboard is Spanish, but you can change. In procedure spriteKeyboard(x,y,w,h,txt.s="Keyboard") change 'Ñ' in kb\lett with your own country keyboard.
Solved black sprite. (i think...

TESTED: 6.02 and 6.20
Code: Select all
Global.b sal
Global.l renderTime
Global camara
Global fontLit= LoadFont(#PB_Any,"Arial",9)
Global fontMed= LoadFont(#PB_Any,"Arial",16)
Global fontBig= LoadFont(#PB_Any,"Arial",24)
Procedure control3D()
Protected.f speed=0.005, mouseSpd=0.005
Protected.f KeyX,KeyY, MouseX,MouseY
If KeyboardPushed(#PB_Key_A)
KeyX= -speed * renderTime
ElseIf KeyboardPushed(#PB_Key_D)
KeyX= speed * renderTime
Else
KeyX= 0
EndIf
If KeyboardPushed(#PB_Key_W)
KeyY= -speed * renderTime
ElseIf KeyboardPushed(#PB_Key_S)
KeyY= speed * renderTime
Else
KeyY= 0
EndIf
If KeyboardPushed(#PB_Key_LeftShift)
keyY * 10
keyX * 10
EndIf
MouseX = -MouseDeltaX() * mouseSpd * renderTime
MouseY = -MouseDeltaY() * mouseSpd * renderTime
RotateCamera(camara, MouseY, MouseX, 0, #PB_Relative)
MoveCamera (camara, KeyX, 0, KeyY)
EndProcedure
Procedure iniDX(title.s="")
Protected w,h
Protected size=50, col1.l=$0,col2.l=$aaaaaa, res=256
Protected.i suelo_tx3D, suelo_mate, suelo_mesh, suelo
UsePNGImageDecoder()
UseJPEGImageDecoder()
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)
CompilerIf #PB_Compiler_Version >= 620 ;this is for new PB. Thanks miso :)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Main", #PB_3DArchive_FileSystem)
Parse3DScripts()
CompilerEndIf
s=128
WorldShadows(#PB_Shadow_Modulative, -1, RGB(s,s,s), 2048)
EnableWorldPhysics(1)
EnableWorldCollisions(1)
cubo_mesh= CreateCube(#PB_Any,2)
cubo_mate= CreateMaterial(#PB_Any, #Null,$0000ff)
cubo_enti= CreateEntity(#PB_Any, MeshID(cubo_mesh), MaterialID(cubo_mate),0,1,0)
luz= CreateLight(#PB_Any,$ffffff,30,50,0,#PB_Light_Point)
camara= CreateCamera(#PB_Any,0,0,100,100)
MoveCamera(camara,0,5,30)
CameraBackColor(camara,$886644)
CameraRange(camara, 0.1,1000)
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
;------- SPRITE KEYBOARD
Structure sprKeyboard_stru
x.w
y.w
w.w
h.w
xc.d
yc.d
txt.s
key.a
caps.b
lett.s
show.b
spr.i
sprMouse.i
EndStructure
Global kb.sprKeyboard_stru
Procedure spriteKeyboardDisplay()
If kb\show
DisplayTransparentSprite(kb\spr,kb\x,kb\y)
DisplayTransparentSprite(kb\sprMouse,MouseX(),MouseY())
EndIf
EndProcedure
Procedure spriteKeyboardDraw()
Protected.l back= $ff888888
Protected.l screen= $ff000000
Protected.l screenInk= $ff00ff00
Protected.l ink= $ff000000
Protected.l br,sh
Protected.w tx,ty
Protected.a m
Protected.s t
Protected n,p
StartDrawing(SpriteOutput(kb\spr))
DrawingMode(#PB_2DDrawing_AlphaBlend);|#PB_2DDrawing_Transparent)
DrawingFont(FontID(fontMed))
RoundBox(0,0,OutputWidth(),OutputHeight(),5,5,back)
x=0:y=0:m=1
For n=2 To 5
For p=0 To 9
t= Mid(kb\lett,m+kb\caps,1)
tx= (kb\xc-TextWidth(t))/2: ty= ((kb\yc-4)-TextHeight(t))/2
If kb\key=m
br= $77000000
sh= $77ffffff
Else
br= $77ffffff
sh= $77000000
EndIf
Box(x+p*kb\xc,y+n*kb\yc,kb\xc-4,kb\yc-4,br)
Box(4+x+p*kb\xc,4+y+n*kb\yc,kb\xc-4,kb\yc-4,sh)
Box(4+x+p*kb\xc,4+y+n*kb\yc,kb\xc-8,kb\yc-8,back)
DrawText(x+tx+p*kb\xc,y+ty+n*kb\yc,t,ink,$00000000)
m+1
Next p
Next n
Box(4,4,kb\w-8,-8+kb\yc*2,$77000000)
Box(8,8,kb\w-12,-12+kb\yc*2,$77ffffff)
Box(8,8,kb\w-16,-16+kb\yc*2,screen)
ClipOutput(8,8,kb\w-16,-16+kb\yc*2)
DrawingFont(FontID(fontBig))
DrawText(10,10,kb\txt,screenInk,$00000000)
UnclipOutput()
StopDrawing()
EndProcedure
Procedure spriteKeyboard(x,y,w,h,txt.s="Keyboard")
Protected.l back= $ff888888
Protected.l ink= $ff000000
Protected i
kb\xc= w/10
kb\yc= h/6
kb\x= x
kb\y= y
kb\w= w
kb\h= h
kb\txt= txt
kb\lett= "1234567890"+
"qwertyuiop"+
"asdfghjklñ"+
"zxcvbnm ◄▲"+
"?@!$%&/.-_"+
"QWERTYUIOP"+
"ASDFGHJKLÑ"+
"ZXCVBNM ◄▼"
kb\spr= CreateSprite(#PB_Any,w,h,#PB_Sprite_AlphaBlending)
kb\sprMouse= CreateSprite(#PB_Any,32,32,#PB_Sprite_AlphaBlending)
i= CreateImage(#PB_Any,32,32,32,#PB_Image_Transparent)
StartVectorDrawing(ImageVectorOutput(i))
VectorSourceColor($cc55ff88)
AddPathSegments("M 0 0 L 31 15 L 31 31 L 15 31 C")
FillPath()
VectorSourceColor($ff000000)
AddPathSegments("M 0 0 L 31 15 L 31 31 L 15 31 L 0 0 C")
StrokePath(2)
StopVectorDrawing()
StartDrawing(SpriteOutput(kb\sprMouse))
DrawingMode(#PB_2DDrawing_AllChannels)
Box(0,0,OutputWidth(),OutputHeight(),RGBA(0,0,0,0))
DrawAlphaImage(ImageID(i),0,0)
StopDrawing()
FreeImage(i)
spriteKeyboardDraw()
EndProcedure
Procedure spriteKeyboardEvento()
Protected x=MouseX(), y=MouseY(), px,py
Static leftButton, rightButton,timer
;show/hide
If MouseButton(#PB_MouseButton_Right)
If rightButton=#False
If kb\show
kb\show= #False
Else
kb\show= #True
EndIf
rightButton=#True
EndIf
Else
If rightButton=#True
rightButton=#False
EndIf
EndIf
;keyboard control
If kb\show
If MouseButton(#PB_MouseButton_Left)
If leftButton=#False
If x>kb\x And x<kb\x+kb\w
If y>kb\y And y<kb\y+kb\h
px= Round((MouseX()-kb\x)/kb\xc,#PB_Round_Up)
py= Round((MouseY()-((kb\y+(kb\yc*3))))/kb\yc,#PB_Round_Up)
kb\key= px + (py * 10)
If kb\key= 40
If kb\caps: kb\caps= 0: Else: kb\caps= 40: EndIf
ElseIf kb\key= 39
If Len(kb\txt)=1
kb\txt= ""
ElseIf Len(kb\txt)>1
kb\txt= Left(kb\txt,Len(kb\txt)-1)
EndIf
Else
kb\txt+ Mid(kb\lett,kb\key+kb\caps,1)
EndIf
spriteKeyboardDraw()
EndIf
EndIf
leftButton= #True
EndIf
Else ;UP
If leftButton=#True
kb\key= 0
spriteKeyboardDraw()
leftButton=#False
EndIf
EndIf
;keyboard phisyc
If KeyboardReleased(#PB_Key_Back)
If Len(kb\txt)=1
kb\txt= ""
ElseIf Len(kb\txt)>1
kb\txt= Left(kb\txt,Len(kb\txt)-1)
EndIf
spriteKeyboardDraw()
EndIf
tec.s= KeyboardInkey()
If tec
For p=1 To 80
If tec=Mid(kb\lett,p,1)
kb\txt+ tec;Mid(kb\lett,kb\key+kb\caps,1)
If p>40:kb\caps=40:Else:kb\caps=0:EndIf
kb\key=p-kb\caps
timer= ElapsedMilliseconds()+250
spriteKeyboardDraw()
Break
EndIf
Next p
Else
If timer
If ElapsedMilliseconds()>timer
kb\key=0: timer= 0
spriteKeyboardDraw()
EndIf
EndIf
EndIf
Else
control3D()
EndIf
EndProcedure
iniDX("spriteKeyboardInput")
spriteKeyboard((ScreenWidth()-400)/2,(ScreenHeight()-200)/2,400,200)
kb\show= #True
Repeat
Repeat : event= WindowEvent(): Until event= 0
ExamineKeyboard()
ExamineMouse()
If KeyboardPushed(#PB_Key_Escape)
sal=1
EndIf
spriteKeyboardEvento()
renderTime = RenderWorld()
spriteKeyboardDisplay()
FlipBuffers()
Delay(1)
Until sal=1