SDF 2d heart sketch

Share your advanced PureBasic knowledge/code with the community.
pjay
Enthusiast
Enthusiast
Posts: 251
Joined: Thu Mar 30, 2006 11:14 am

SDF 2d heart sketch

Post by pjay »

For a while now I've been working with signed distance field 2d rendering; I wrote a fair few snippets during this time, below is one of them which is apt for todays date.

Code: Select all

; SDF Heart shape - PJ102023 - derived from IQ code @ https://www.shadertoy.com/view/dljyDc

EnableExplicit

Procedure.f sdHeart(x.f,y.f)
  Static a.f, b.f, c.f : y + 0.5 : y = 1.0 - y : x = Abs(x)
  If x + y > 1.0 : ProcedureReturn Sqr((x - 0.25) * (x - 0.25) + (y - 0.75) * (y - 0.75)) - 0.35355 : Else
    c = x + y : If c < 0 : c = 0.0 : EndIf
    a = x * x + (y - 1.0) * (y - 1.0) : b = (x - 0.5 * c) * (x - 0.5 * c) + (y - 0.5 * c) * (y - 0.5 * c)
    If a < b : ProcedureReturn Sqr(a) * Sign(x-y) : Else : ProcedureReturn Sqr(b) * Sign(x-y) : EndIf
  EndIf
EndProcedure

OpenWindow(0,0,0,640,480,"Circling the heart",#PB_Window_Maximize|#PB_Window_BorderLess) : AddWindowTimer(0,0,25)
Define.f w = WindowWidth(0), h = WindowHeight(0), hw = w / 2, hh = h / 2, x, y, p, sz, c.i, frame
CanvasGadget(0,0,0,w,h,#PB_Canvas_Keyboard) : SetActiveGadget(0)
StartVectorDrawing(CanvasVectorOutput(0)) : VectorSourceColor(RGBA(230,230,230,255)) : FillVectorOutput() : StopVectorDrawing()
Repeat
  Select WaitWindowEvent(25)
    Case #PB_Event_CloseWindow : End
    Case #PB_Event_Gadget
      Select EventType()
        Case #PB_EventType_LeftButtonDown : End
        Case #PB_EventType_KeyDown : End
      EndSelect
    Case #PB_Event_Timer
      frame + 1
      StartVectorDrawing(CanvasVectorOutput(0))
      ScaleCoordinates(DesktopResolutionX(),DesktopResolutionY())
      Select frame
        Case 0 To 250
          For c = 1 To 10
            x = Random(w) : y = Random(h) : p.f = sdHeart((x - hw) / hh, (y - hh) / hh) * hh
            If p < 0 : VectorSourceColor(RGBA(150,20,10,45)) : sz = 1.5 : Else : VectorSourceColor(RGBA(20,20,70,30)) : sz = 1.5 : EndIf
            p = Abs(p) : AddPathCircle(x,y,p) : StrokePath(sz)
          Next
        Case 250 To 350
        Case 350 To 400 : VectorSourceColor(RGBA(230,230,230,25)) : FillVectorOutput() 
        Case 401 : Frame = 1
      EndSelect
      StopVectorDrawing()
  EndSelect
ForEver
User avatar
idle
Always Here
Always Here
Posts: 5835
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: SDF 2d heart sketch

Post by idle »

nice thanks
User avatar
minimy
Enthusiast
Enthusiast
Posts: 552
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: SDF 2d heart sketch

Post by minimy »

Nice demo, is a hippy heart? :mrgreen:

Sorry for the joke... Is very nice vector demo. Thanks for share.
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
SPH
Enthusiast
Enthusiast
Posts: 561
Joined: Tue Jan 04, 2011 6:21 pm

Re: SDF 2d heart sketch

Post by SPH »

Very nice 😍

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
threedslider
Enthusiast
Enthusiast
Posts: 377
Joined: Sat Feb 12, 2022 7:15 pm

Re: SDF 2d heart sketch

Post by threedslider »

Awesome ! Love this :mrgreen:
Fred
Administrator
Administrator
Posts: 18153
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: SDF 2d heart sketch

Post by Fred »

Cool stuff !
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Re: SDF 2d heart sketch

Post by Dreamland Fantasy »

That is so cool! Nice work! :D

Kind regards,

Francis
Post Reply