Blitz3D+ Command Reference

CrossFadeAnim entity,sequence[,blend#][,mode][,speed#][,restart]

Parameters

entity - handle of an animated entity
sequence - animation sequence number to play
blend# (optional) - crossfade time, in UpdateWorld units; 0 (default)
mode (optional) - playback mode; 1 (default):
1: loop
2: ping-pong
3: one-shot

speed# (optional) - animation frames advanced per UpdateWorld; 1 (default)
restart (optional) - True to restart even if this sequence is already playing; False (default)

Description

Switches an entity to another animation sequence, blending smoothly out of the old one.

Where Animate cuts straight to the first frame of the new sequence, CrossFadeAnim keeps both animations running for blend# worth of time and mixes the poses together. That is the difference between a character that snaps into a run and one that eases into it. Transform animation and morph animation are both blended.

blend# is measured in the same units as the elapsed value you pass to UpdateWorld, so with the default UpdateWorld a blend of 0.25 takes a quarter of an update. A blend of 0 changes clips immediately but still goes through the same path, so speeds and modes behave consistently.

With restart False - the default - asking for the sequence and mode that are already running is ignored, which makes the command safe to call every frame from a state machine. Pass True to force a fresh start.

This is the numeric version; PlayAnim does the same job by clip name, which is usually easier to read. Note that the classic Animate command is unchanged - its transition# parameter still tweens the current pose into the first frame rather than blending whole clips.

An invalid sequence, mode, blend or speed raises an error, so check your sequence numbers against AnimSeq and the value LoadAnimSeq gave you.

See also: PlayAnim, Animate, AnimSeq, AnimSpeed, LoadAnimSeq.

Example

; CrossFadeAnim Example
; ---------------------

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

camera=CreateCamera()
PositionEntity camera,0,2,-6
RotateEntity camera,12,0,0

light=CreateLight()
RotateEntity light,45,45,0

; A grassy paddock for the fox to roam
ground=CreatePlane()
EntityTexture ground,LoadTexture("media/MossyGround.BMP")

; Load the rigged fox; it has the named clips "Survey", "Walk" and "Run"
fox=LoadAnimMesh("../../../samples/glTF/assets/Fox/Fox.glb")
If fox=0 Then RuntimeError "Could not load Fox.glb: "+ModelLoadError()

; Shrink the fox to game scale (the file is modelled about 100 units tall)
FitMesh fox,-0.8,0,-0.8,1.6,1.6,1.6,True

; Look up the sequence numbers of the named clips
survey=FindAnimSeq(fox,"Survey")
walk=FindAnimSeq(fox,"Walk")
run=FindAnimSeq(fox,"Run")

; Blend time, in UpdateWorld elapsed units
blend#=15

; Start with the survey clip
CrossFadeAnim fox,survey,0,1

While Not KeyDown(1)

    ; Adjust how long the crossfade takes
    If KeyDown(26) And blend>0 Then blend=blend-0.25
    If KeyDown(27) And blend<60 Then blend=blend+0.25

    ; CrossFadeAnim blends smoothly from the playing clip to the new one;
    ; the incoming clip keeps advancing during the blend
    If KeyHit(2) Then CrossFadeAnim fox,survey,blend,1
    If KeyHit(3) Then CrossFadeAnim fox,walk,blend,1
    If KeyHit(4) Then CrossFadeAnim fox,run,blend,1

    UpdateWorld

    ; Arrow keys move the camera
    If KeyDown(200) Then MoveEntity camera,0,0,0.1
    If KeyDown(208) Then MoveEntity camera,0,0,-0.1
    If KeyDown(203) Then TurnEntity camera,0,1,0
    If KeyDown(205) Then TurnEntity camera,0,-1,0

    RenderWorld

    Text 0,0,"1/2/3: crossfade to Survey/Walk/Run   [ / ]: blend time   Arrows: camera   Esc: exit"
    Text 0,20,"CrossFadeAnim fox,sequence,"+blend+",1"
    Text 0,40,"Current sequence: "+AnimSeq(fox)

    Flip

Wend

End

Index