Swinming pool, summer is here!

Everything related to 3D programming
User avatar
minimy
Enthusiast
Enthusiast
Posts: 619
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Swinming pool, summer is here!

Post by minimy »

Hi! Two procedures to create your own waters with position, speed, and size. Is a simple effect of wave but you can improve if you want.

Summer is here! or at least the pool :lol:

Code: Select all


; By minimy 2024
;{ GLOBALES
  Global.b sal
  Global.l contador
  Global.b pantallaCompleta
;}

  ;{ FUENTES
  Global  fontLit= LoadFont(#PB_Any,"Arial",9)
  Global  fontMed= LoadFont(#PB_Any,"Arial",16)
  Global  fontBig= LoadFont(#PB_Any,"Arial",24)
  ;}

  ;{ SISTEMA 3D
  Procedure   creaSuelo(x,y,z, size=50, col1.l=$0,col2.l=$aaaaaa, res=256, h=0, colision=#False)
    Protected.i suelo_tx3D, suelo_mate, suelo_mesh, suelo
    Protected.i a,m, e1,e2,e3,e4
    Protected.f u,v
    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)
                  If size % 2= 0:ScrollMaterial(suelo_mate,0.5,0.5,#PB_Material_Fixed):EndIf
    suelo=        CreateEntity(#PB_Any, MeshID(suelo_mesh), MaterialID(suelo_mate),x,y,z)
    pivot=        CreateNode(#PB_Any)
    
    If h>0
      a=  CreateCube(#PB_Any,1)
      m=  CreateMaterial(#PB_Any,TextureID(suelo_tx3D))
          ScaleMaterial(m,1/size,1/h)
          If size % 2= 0:u=0.5:EndIf : If h % 2= 0:v=0.5:EndIf : ScrollMaterial(m,u,v,#PB_Material_Fixed)
      e1= CreateEntity(#PB_Any, MeshID(a),MaterialID(m), x, y+(h/2), z+((size/2)+0.5))
          ScaleEntity(e1,size,h,1,#PB_Absolute)
      
      e2=  CopyEntity(e1,#PB_Any): MoveEntity(e2, x, y+(h/2), z+((-size/2)-0.5),#PB_Absolute)
      e3=  CopyEntity(e1,#PB_Any): RotateEntity(e3,0,90,0,#PB_Absolute): MoveEntity(e3, x+((-size/2)-0.5), y+(h/2), z,#PB_Absolute)
      e4=  CopyEntity(e1,#PB_Any): RotateEntity(e4,0,90,0,#PB_Absolute): MoveEntity(e4, x+((size/2)+0.5), y+(h/2), z,#PB_Absolute)
      If colision
        CreateEntityBody(suelo,#PB_Entity_StaticBody)
        CreateEntityBody(e1,#PB_Entity_StaticBody)
        CreateEntityBody(e2,#PB_Entity_StaticBody)
        CreateEntityBody(e3,#PB_Entity_StaticBody)
        CreateEntityBody(e4,#PB_Entity_StaticBody)
      EndIf
      
    EndIf
    
  ProcedureReturn suelo
EndProcedure
  Procedure   spr2DText(spr, txt.s, ink.l=$ff00ffff)
    Protected n,y=2,tl= CountString(txt,#CR$)
    Protected tx.s
    StartDrawing(SpriteOutput(spr))
    DrawingMode(#PB_2DDrawing_AlphaChannel)
      Box(0,0,OutputWidth(),OutputHeight(),$00000000)
    DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Transparent)
      Box(0,0,OutputWidth(),OutputHeight(),$aa000000)
      DrawingFont(FontID(fontLit))
      For n=1 To tl+1
        tx= StringField(txt,n,#CR$)
        DrawText(5,y,tx,ink)
        y+16
      Next n
      DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Outlined)
      Box(0,0,OutputWidth(),OutputHeight(),ink)
    StopDrawing()
  EndProcedure
  Procedure   eventosWindows()
    Repeat
      event= WindowEvent()
      Select event
        Case #PB_Event_Gadget
          EventGadget=  EventGadget()
          EventType=    EventType()
          
        Case #PB_Event_CloseWindow
          sal= 1
      EndSelect
    Until event= 0
  EndProcedure
  Procedure   eventos3D(camara, speed.f=1, mouseSpd.f=0.05)
    Protected.f KeyX,KeyY, MouseX,MouseY
    If KeyboardPushed(#PB_Key_Escape)
      sal=1
    EndIf
    
    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   iniDX(title.s="Agua")
    Protected w,h
    InitEngine3D()
      InitSprite()
      InitKeyboard()
      InitMouse()
      OpenWindow(0,0,0,1280,720,title,#PB_Window_ScreenCentered|#PB_Window_BorderLess|#PB_Window_Maximize)
      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)
      
      Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
      Add3DArchive("Data/Model", #PB_3DArchive_FileSystem)
      Parse3DScripts()
      WorldShadows(#PB_Shadow_Additive, -1, RGB(s,s,s), 2048)
  EndProcedure
  
  iniDX("Base 3D")
  sprInfo=  CreateSprite(#PB_Any,100,200,#PB_Sprite_AlphaBlending)
  ;}
  
  ;{ AGUA
  #aguaTilesX=  20
  #aguaTilesZ=  20
  #aguaVertic=  (#aguaTilesX+1) * (#aguaTilesZ+1)
  Structure agua_stru
    mate.i
    mesh.i
    enti.i
    vX.MeshVertex[#aguaVertic]
    wave.f
    alto.f
    spd.f
    mode.a
  EndStructure
  Global NewList agua.agua_stru()
  Global Dim aguaVec.MeshVertex(0)
  Procedure   aguaNew(x.f=0,y.f=0,z.f=0, mater=0, sx=20,sz=20, alto.f=0.25, speed.f=0.025, mode.a=0)
    Protected p
    AddElement(agua())
    agua()\mesh=  CreatePlane(#PB_Any, sx,sz, #aguaTilesX,#aguaTilesZ, 1,1)
    agua()\enti=  CreateEntity(#PB_Any, MeshID(agua()\mesh), MaterialID(mater), x,y,z)
    agua()\alto=  alto
    agua()\spd=   speed
    agua()\mode=  mode
    GetMeshData(agua()\mesh,0, aguaVec(), #PB_Mesh_Vertex | #PB_Mesh_Normal  , 0, MeshVertexCount(agua()\mesh)-1)
    For p= 0 To #aguaVertic-1 : agua()\vX[p]= aguaVec(p) : Next p
    EntityRenderMode(agua()\enti,#PB_Shadow_None)
  EndProcedure
  Procedure   aguaAnim(mode.a=0)
    Protected.f w
    Protected   x,z,v,c
    ForEach agua()
      For p= 0 To #aguaVertic-1 : aguaVec(p)= agua()\vX[p] : Next p
        w= agua()\wave
      For p= 0 To #aguaVertic-1
        aguaVec(p)\y= Sin(w) * agua()\alto
        w+agua()\spd:If w>=360:w=0:EndIf
      Next p
      
      w= agua()\wave
      For p= 0 To #aguaTilesX
        For n= 0 To #aguaTilesZ
          v= n+(p*(#aguaTilesZ+1))
          If v<#aguaVertic-1
            aguaVec(v)\y+ (Sin(w) * agua()\alto)
          EndIf
        Next n
        w+(agua()\spd*5):If w>=360:w=0:EndIf
      Next p
      
      agua()\wave +agua()\spd : If agua()\wave>=360 : agua()\wave=0 : EndIf
      SetMeshData(agua()\mesh,0, aguaVec(), #PB_Mesh_Vertex | #PB_Mesh_Normal, 0, MeshVertexCount(agua()\mesh)-1)     
      NormalizeMesh(agua()\mesh)
      UpdateMeshBoundingBox(agua()\mesh)
    Next
  EndProcedure

  ;}
  
  ;{ DECORADO 3D
  res= 256
  suelo_tx3D=   CreateTexture(#PB_Any,res,res)
  StartDrawing(TextureOutput(suelo_tx3D))
    DrawingMode(#PB_2DDrawing_AllChannels)
    Box(0,0,OutputWidth(),OutputHeight(),$aa000000)
    Box(2,2,OutputWidth()-4,OutputHeight()-4,$aaff4400)
  StopDrawing()
  
  size= 20
  agua_mate=   CreateMaterial(#PB_Any,TextureID(suelo_tx3D))
                ScaleMaterial(agua_mate,1/size,1/size,0)
                ScrollMaterial(agua_mate,0.01,0.01,#PB_Material_Animated,0)
                MaterialCullingMode(agua_mate, #PB_Material_NoCulling)
                SetMaterialColor(agua_mate,#PB_Material_AmbientColor,$ff440000)
                MaterialShininess(agua_mate,50,$ffffff)
                MaterialBlendingMode(agua_mate,#PB_Material_AlphaBlend)
  
  cubo_mesh=    CreateCube(#PB_Any,1)
  cubo_mate=    CreateMaterial(#PB_Any, #Null,$0000ff)
  cubo_enti=    CreateEntity(#PB_Any, MeshID(cubo_mesh), MaterialID(cubo_mate),-4,1,7)
  ;}
  
  pool0= creaSuelo(10,-5,-15, 40,$0,$aaaaaa,256,5)
  pool1= creaSuelo(0,-5,0, 20,$0,$aaaaaa,256,10)
  pool2= creaSuelo(-10,0,-10, 10,$0,$aaaaaa,256,10)

  aguaNew(10,-1,-15,agua_mate, 40,40, 0.25,0.1)
  aguaNew(0,4.4,0,agua_mate, 20,20, 0.25,0.05)
  aguaNew(-10,9.4,-10,agua_mate, 10,10, 0.25,0.025)

  
  ;{ CÁMARA Y LUZ
  luz=          CreateLight(#PB_Any,$ffffff,-50,50,-50,#PB_Light_Point)
  
  
  camara=       CreateCamera(#PB_Any,0,0,100,100)
    MoveCamera(camara,18.59,30.12,34.02)
    CameraLookAt(camara,10,0,0)
    CameraRange(camara, 0.1,1000)
  ;}
                
  SetFrameRate(50)
  ;{ BUCLE PRINCIPAL
  Repeat
    ;{ PROCESO EVENTOS
    If pantallaCompleta= 0
      eventosWindows()
    EndIf
    ;}
    
    ExamineKeyboard()
    ExamineMouse()
    eventos3D(camara, 0.1)
    
    aguaAnim()
    
    ElapsedTime = RenderWorld()
    
    ;{ HUD
    If contador % 25 = 0
      txt.s= "DEBUGGER"+ #CR$+
             "FPS: "+Str(Engine3DStatus(#PB_Engine3D_CurrentFPS)) +#CR$+
             Str(ScreenWidth())+" x "+Str(ScreenHeight()) +#CR$+
;              "X: "+StrF(CameraX(camara),2)+#CR$+"Y: "+StrF(CameraY(camara),2)+#CR$+"Z: "+StrF(CameraZ(camara),2); +#CR$+
;              "X: "+StrF(land\playX) +#CR$+
;              "Z: "+StrF(land\playZ) +#CR$+
;              "CAY: "+StrF(cya,2); +#CR$+
      spr2DText(sprInfo,txt, $ff00ffff)
    EndIf
    DisplayTransparentSprite(sprInfo,ScreenWidth()-110,10)
    ;}
    
    FlipBuffers()
    contador +1
    Delay(1)
  Until sal=1
  ;}
  

If translation=Error: reply="Sorry, Im Spanish": Endif
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Swinming pool, summer is here!

Post by BarryG »

Nice! I knew it'd be something like this before I even hit F5 to compile/run. :)
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 386
Joined: Thu Jul 09, 2015 9:07 am

Re: Swinming pool, summer is here!

Post by pf shadoko »

there are several shaders for water management
look at the 3d examples:
- ShaderSkyWaterBump.pb
- ShaderOcean.pb
User avatar
minimy
Enthusiast
Enthusiast
Posts: 619
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Swinming pool, summer is here!

Post by minimy »

BarryG wrote: Sun May 12, 2024 3:09 am Nice! I knew it'd be something like this before I even hit F5 to compile/run. :)
Hello BarryG! What i thinking now?... nah, is joke! :lol: Thanks! i hope you enjoy it this summer!
pf shadoko wrote: Tue May 14, 2024 2:37 pm there are several shaders for water management
look at the 3d examples:
- ShaderSkyWaterBump.pb
- ShaderOcean.pb
Hello pf shadoko! Thanks for your help master!!
I've seen all your demos and they're really amazing. You're definitely on another level!
My level with shadders is -87 :lol:
I'll look at the examples and see if I catch something. I made several attempts to make an outlinear edge shader, but it didn't end well hehe, the shader told me: It's not you, it's me :mrgreen:
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 386
Joined: Thu Jul 09, 2015 9:07 am

Re: Swinming pool, summer is here!

Post by pf shadoko »

hi,
i don't see how to make this kind of shader (maybe in 2 passes)
I think you're not starting with the simplest of things

if it works for you, you can start with my lib screen
it's 2d, but shader creation is very easy and in real time

https://www.purebasic.fr/english/viewtopic.php?t=84206
Post Reply