@name Bezier3
@persist T [Cp1 Cp2 Cp3 End]:entity [Q0 Q1 Q2 Q3 R0 R1 R2 S0 S1]:entity
@model models/sprops/cuboids/height06/size_1/cube_6x6x6.mdl

interval(100)

if(first())
{
    runOnLast(1)
    
    function vector bezier3([Source CurvePoint1 CurvePoint2 CurvePoint3 End]:vector,Progress) #Quartic Bezier
    {
        local Ti = 1-Progress
        local Q0 = mix(Source, CurvePoint1, Ti)
        local Q1 = mix(CurvePoint1, CurvePoint2, Ti)
        local Q2 = mix(CurvePoint2, CurvePoint3, Ti)
        local Q3 = mix(CurvePoint3, End, Ti)
        
        local R0 = mix(Q0, Q1, Ti)
        local R1 = mix(Q1, Q2, Ti)
        local R2 = mix(Q2, Q3, Ti)
        
        local S0 = mix(R0, R1, Ti)
        local S1 = mix(R1, R2, Ti)
        
        local T = mix(S0, S1, Ti)
        return T
    }
    
    entity():setColor(0,255,0)
    entity():setMaterial("models/debug/debugwhite")
    
    Cp1 = propSpawn("models/sprops/cuboids/height06/size_1/cube_6x6x6.mdl",entity():toWorld(vec(25,0,5)),0)
    Cp2 = propSpawn("models/sprops/cuboids/height06/size_1/cube_6x6x6.mdl",entity():toWorld(vec(50,0,5)),0)
    Cp3 = propSpawn("models/sprops/cuboids/height06/size_1/cube_6x6x6.mdl",entity():toWorld(vec(75,0,5)),0)
    End = propSpawn("models/sprops/cuboids/height06/size_1/cube_6x6x6.mdl",entity():toWorld(vec(100,0,5)),0)
    
    Cp1:setColor(0,0,255)
    Cp2:setColor(0,0,255)
    Cp3:setColor(0,0,255)
    End:setColor(0,255,0)
    
    Cp1:setMaterial("models/debug/debugwhite")
    Cp2:setMaterial("models/debug/debugwhite")
    Cp3:setMaterial("models/debug/debugwhite")
    End:setMaterial("models/debug/debugwhite")
    
    #Pointer Ball
    holoCreate(1)
    holoModel(1, "icosphere3")
    holoEntity(1):setTrails(5,5,30,"trails/smoke",vec(255),255)
    
    #Deg 1 Lines
    holoCreate(2)
    holoCreate(3)
    holoCreate(4)
    holoCreate(5)
    
    holoModel(2,"models/sprops/rectangles/size_1/rect_3x480x3.mdl")
    holoModel(3,"models/sprops/rectangles/size_1/rect_3x480x3.mdl")
    holoModel(4,"models/sprops/rectangles/size_1/rect_3x480x3.mdl")
    holoModel(5,"models/sprops/rectangles/size_1/rect_3x480x3.mdl")
    
    holoColor(2,vec4(255,0,0,255))
    holoColor(3,vec4(255,0,0,255))
    holoColor(4,vec4(255,0,0,255))
    holoColor(5,vec4(255,0,0,255))
    
    holoMaterial(2,"models/debug/debugwhite")
    holoMaterial(3,"models/debug/debugwhite")
    holoMaterial(4,"models/debug/debugwhite")
    holoMaterial(5,"models/debug/debugwhite")
    
    #Deg 1 Balls
    Q0 = holoCreate(6)
    Q1 = holoCreate(7)
    Q2 = holoCreate(8)
    Q3 = holoCreate(9)
    
    holoModel(6,"icosphere3")
    holoModel(7,"icosphere3")
    holoModel(8,"icosphere3")
    holoModel(9,"icosphere3")
    
    holoColor(6,vec())
    holoColor(7,vec())
    holoColor(8,vec())
    holoColor(9,vec())
    
    holoScale(6,vec(0.5))
    holoScale(7,vec(0.5))
    holoScale(8,vec(0.5))
    holoScale(9,vec(0.5))
    
    #Deg 2 Lines
    holoCreate(10)
    holoCreate(11)
    holoCreate(12)
    
    holoModel(10,"models/sprops/rectangles/size_1/rect_3x480x3.mdl")
    holoModel(11,"models/sprops/rectangles/size_1/rect_3x480x3.mdl")
    holoModel(12,"models/sprops/rectangles/size_1/rect_3x480x3.mdl")
    
    holoColor(10,vec4(0,255,255,255))
    holoColor(11,vec4(0,255,255,255))
    holoColor(12,vec4(0,255,255,255))
    
    holoMaterial(10,"models/debug/debugwhite")
    holoMaterial(11,"models/debug/debugwhite")
    holoMaterial(12,"models/debug/debugwhite")
    
    #Deg 2 Balls
    R0 = holoCreate(14)
    R1 = holoCreate(15)
    R2 = holoCreate(16)
    
    holoModel(14,"icosphere3")
    holoModel(15,"icosphere3")
    holoModel(16,"icosphere3")
    
    holoColor(14,vec())
    holoColor(15,vec())
    holoColor(16,vec())
    
    holoScale(14,vec(0.5))
    holoScale(15,vec(0.5))
    holoScale(16,vec(0.5))
    
    #Deg 3 Lines
    holoCreate(17)
    holoCreate(18)
    
    holoModel(17,"models/sprops/rectangles/size_1/rect_3x480x3.mdl")
    holoModel(18,"models/sprops/rectangles/size_1/rect_3x480x3.mdl")
    
    holoColor(17,vec4(255,0,255,255))
    holoColor(18,vec4(255,0,255,255))
    
    holoMaterial(17,"models/debug/debugwhite")
    holoMaterial(18,"models/debug/debugwhite")
    
    #Deg 3 Balls
    S0 = holoCreate(19)
    S1 = holoCreate(20)
    
    holoModel(19,"icosphere3")
    holoModel(20,"icosphere3")
    
    holoColor(19,vec())
    holoColor(20,vec())
    
    holoScale(19,vec(0.5))
    holoScale(20,vec(0.5))
    
    #Deg 4 Line
    holoCreate(21)
    
    holoModel(21,"models/sprops/rectangles/size_1/rect_3x480x3.mdl")
    
    holoColor(21,vec4(255,255,0,255))
    
    holoMaterial(21,"models/debug/debugwhite")
}

T += 0.0125
T = T <= 1 ? T : -0.025

holoPos(1, bezier3(entity():pos(), Cp1:pos(), Cp2:pos(), Cp3:pos(), End:pos(), clamp(T,0,1)))

#Deg 1 Lines
holoPos(2, mix(entity():pos(), Cp1:pos(),0.5))
holoAng(2, (Cp1:pos() - entity():pos()):toAngle())
holoScale(2, vec(entity():pos():distance(Cp1:pos())/480, 1, 1))

holoPos(3, mix(Cp1:pos(), Cp2:pos(),0.5))
holoAng(3, (Cp2:pos() -  Cp1:pos()):toAngle())
holoScale(3, vec(Cp1:pos():distance(Cp2:pos())/480, 1, 1))

holoPos(4, mix(Cp2:pos(), Cp3:pos(),0.5))
holoAng(4, (Cp3:pos() - Cp2:pos()):toAngle())
holoScale(4, vec(Cp2:pos():distance(Cp3:pos())/480, 1, 1))

holoPos(5, mix(Cp3:pos(), End:pos(),0.5))
holoAng(5, (End:pos() - Cp3:pos()):toAngle())
holoScale(5, vec(Cp3:pos():distance(End:pos())/480, 1, 1))

#Deg 1 Balls
holoPos(6,mix(entity():pos(), Cp1:pos(),clamp(1-T,0,1)))
holoPos(7,mix(Cp1:pos(), Cp2:pos(),clamp(1-T,0,1)))
holoPos(8,mix(Cp2:pos(), Cp3:pos(),clamp(1-T,0,1)))
holoPos(9,mix(Cp3:pos(), End:pos(),clamp(1-T,0,1)))

#Deg 2 lines
holoPos(10, mix(Q0:pos(), Q1:pos(),0.5))
holoAng(10, (Q1:pos() - Q0:pos()):toAngle())
holoScale(10, vec(Q0:pos():distance(Q1:pos())/480, 1, 1))

holoPos(11, mix(Q1:pos(), Q2:pos(),0.5))
holoAng(11, (Q2:pos() - Q1:pos()):toAngle())
holoScale(11, vec(Q1:pos():distance(Q2:pos())/480, 1, 1))

holoPos(12, mix(Q2:pos(), Q3:pos(),0.5))
holoAng(12, (Q3:pos() - Q2:pos()):toAngle())
holoScale(12, vec(Q2:pos():distance(Q3:pos())/480, 1, 1))

#Deg 2 Balls
holoPos(14, mix(Q0:pos(), Q1:pos(), clamp(1-T,0,1)))
holoPos(15, mix(Q1:pos(), Q2:pos(), clamp(1-T,0,1)))
holoPos(16, mix(Q2:pos(), Q3:pos(), clamp(1-T,0,1)))

#Deg 3 lines
holoPos(17,mix(R0:pos(), R1:pos(), 0.5))
holoAng(17, (R1:pos() - R0:pos()):toAngle())
holoScale(17, vec(R0:pos():distance(R1:pos())/480, 1, 1))

holoPos(18,mix(R1:pos(), R2:pos(), 0.5))
holoAng(18, (R2:pos() - R1:pos()):toAngle())
holoScale(18, vec(R1:pos():distance(R2:pos())/480, 1, 1))

#Deg 3 Balls
holoPos(19, mix(R0:pos(), R1:pos(), clamp(1-T,0,1)))
holoPos(20, mix(R1:pos(), R2:pos(), clamp(1-T,0,1)))

#Deg 4 Line
holoPos(21,mix(S0:pos(), S1:pos(), 0.5))
holoAng(21, (S1:pos() - S0:pos()):toAngle())
holoScale(21, vec(S0:pos():distance(S1:pos())/480, 1, 1))

if(last())
{
    array(Cp1,Cp2,Cp3,End):propDelete()
}

