Blitz3D+ Command Reference

CountMorphTargets ( entity )

Parameters

entity - handle of the entity to inspect

Description

Returns how many morph targets a model has, or 0 if it has none.

A morph target - artists usually call it a blend shape - is a second set of vertex positions stored alongside the base mesh. The same triangles, pushed into a different shape: a face with a smile, a crystal charged up, a ship hull crumpled by damage. SetMorphWeight blends between the base shape and a target, so you get smooth deformation without a single bone.

CountMorphTargets is how you find out whether a model you just loaded supports any of that. Targets are numbered from 0, so a count of 2 means targets 0 and 1 are valid and anything else will be quietly ignored. Any loop that walks a model's shapes wants this number as its upper bound.

The command starts at the entity you hand it and, if that is not a morphing mesh, walks down through its children until it finds one that is - which is exactly what you want after loading a model whose root turns out to be an empty pivot or a scene container. It reports the count for the first morphing mesh it finds, though, so a model built from several separate morphing meshes only tells you about one of them.

There is no way to get an error out of it. A cube, a light, a camera or a model with no shapes all answer 0, which makes it a safe test to run on anything before you start asking for target numbers. Note that MD2 models are animated by morphing internally but do not expose named targets, so they answer 0 as well - drive those with Animate instead.

See also: FindMorphTarget, SetMorphWeight, MorphWeight, LoadAnimMesh, CountAnimSeqs.

Example

; CountMorphTargets Example
; -------------------------

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

camera=CreateCamera()
PositionEntity camera,0,1.5,-6

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

ground=CreatePlane()
EntityTexture ground,LoadTexture("media/MossyGround.BMP")

; A power crystal exported with two morph targets baked into the file
gem=LoadAnimMesh("media/MorphGem.gltf")
If gem=0 Then RuntimeError "Could not load MorphGem.gltf: "+ModelLoadError()
PositionEntity gem,-1.6,1,0

; An ordinary primitive for comparison - no morph targets at all
crate=CreateCube()
PositionEntity crate,1.6,1,0

; Which entity we are interrogating
subject=gem
who$="gem"

While Not KeyDown(1)

    ; Space switches the entity we ask about
    If KeyHit(57)
        If subject=gem
            subject=crate
            who$="crate"
        Else
            subject=gem
            who$="gem"
        EndIf
    EndIf

    ; Pulse the first target so the gem's shapes are obviously live
    SetMorphWeight gem,0,(Sin(MilliSecs()/6.0)+1)*0.5

    TurnEntity gem,0,0.6,0
    TurnEntity crate,0,0.6,0

    ; 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,"Space: switch between the gem and the crate   Arrows: camera   Esc: exit"
    Text 0,20,"CountMorphTargets("+who$+") = "+CountMorphTargets(subject)
    Text 0,40,"Target numbers run 0 to count-1, so the gem has targets 0 and 1"
    Text 0,60,"A mesh with no morph targets answers 0 - check before you ask for one"

    Flip

Wend

End

Index