Page 1 of 1

SDF 2d heart sketch

Posted: Wed Feb 14, 2024 9:48 pm
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

Re: SDF 2d heart sketch

Posted: Wed Feb 14, 2024 9:56 pm
by idle
nice thanks

Re: SDF 2d heart sketch

Posted: Wed Feb 14, 2024 9:58 pm
by minimy
Nice demo, is a hippy heart? :mrgreen:

Sorry for the joke... Is very nice vector demo. Thanks for share.

Re: SDF 2d heart sketch

Posted: Wed Feb 14, 2024 10:58 pm
by SPH
Very nice 😍

Re: SDF 2d heart sketch

Posted: Thu Feb 15, 2024 1:13 pm
by threedslider
Awesome ! Love this :mrgreen:

Re: SDF 2d heart sketch

Posted: Thu Feb 15, 2024 10:18 pm
by Fred
Cool stuff !

Re: SDF 2d heart sketch

Posted: Mon Feb 19, 2024 1:58 am
by Dreamland Fantasy
That is so cool! Nice work! :D

Kind regards,

Francis