Skip to content

Commit

Permalink
Merge pull request #474 from JupiterRider/model_animation
Browse files Browse the repository at this point in the history
ModelAnimation improvements
  • Loading branch information
gen2brain authored Dec 15, 2024
2 parents a9ad86d + c301540 commit 7a899c5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
7 changes: 2 additions & 5 deletions examples/models/animation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
package main

import (
"unsafe"

rl "github.com/gen2brain/raylib-go/raylib"
)

Expand Down Expand Up @@ -76,9 +74,8 @@ func main() {
rl.DrawModelEx(model, position, rl.NewVector3(1, 0, 0), -90, rl.NewVector3(1, 1, 1), rl.White)
// Draw translation cubes
for i := int32(0); i < model.BoneCount; i++ {
framePose := unsafe.Slice(anims[0].FramePoses, anims[0].FrameCount)
trans := unsafe.Slice(framePose[animFrameCount], model.BoneCount)
rl.DrawCube(trans[i].Translation, 0.2, 0.2, 0.2, rl.Red)
pose := anims[0].GetFramePose(animFrameCount, int(i))
rl.DrawCube(pose.Translation, 0.2, 0.2, 0.2, rl.Red)
}
rl.DrawGrid(10, 1)

Expand Down
2 changes: 1 addition & 1 deletion examples/models/gltf_loading/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func main() {

rl.EndMode3D()

rl.DrawText("current animation number: "+fmt.Sprint(animIndex), 10, 10, 10, rl.Black)
rl.DrawText(fmt.Sprintf("current animation: %s [%d]", animPlaying.GetName(), animIndex), 10, 10, 10, rl.Black)
rl.DrawText("UP/DOWN ARROW KEYS CHANGE ANIMATION", 10, 30, 10, rl.Black)

rl.EndDrawing()
Expand Down
15 changes: 7 additions & 8 deletions examples/models/m3d_loading/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
package main

import (
"unsafe"

"github.com/gen2brain/raylib-go/raylib"
rl "github.com/gen2brain/raylib-go/raylib"
)

const (
Expand Down Expand Up @@ -116,9 +114,8 @@ func main() {
if drawSkeleton {
modelBones := model.GetBones()
modelPoses := model.GetBindPose()
animBones := unsafe.Slice(anims[animID].Bones, anims[animID].BoneCount)
animPoses := unsafe.Slice(anims[animID].FramePoses, anims[animID].FrameCount)
transforms := unsafe.Slice(animPoses[animFrameCounter], anims[animID].BoneCount)
anim := anims[animID]
animBones := anim.GetBones()
for bone := 0; bone < int(model.BoneCount)-1; bone++ {
if !animPlaying || animsCount == 0 {
// Display the bind-pose skeleton
Expand All @@ -128,9 +125,11 @@ func main() {
}
} else {
// // Display the frame-pose skeleton
rl.DrawCube(transforms[bone].Translation, 0.05, 0.05, 0.05, rl.Red)
pos := anim.GetFramePose(animFrameCounter, bone).Translation
rl.DrawCube(pos, 0.05, 0.05, 0.05, rl.Red)
if animBones[bone].Parent >= 0 {
rl.DrawLine3D(transforms[bone].Translation, transforms[animBones[bone].Parent].Translation, rl.Red)
endPos := anim.GetFramePose(animFrameCounter, int(animBones[bone].Parent)).Translation
rl.DrawLine3D(pos, endPos, rl.Red)
}
}
}
Expand Down
24 changes: 23 additions & 1 deletion raylib/raylib.go
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,29 @@ type ModelAnimation struct {
FrameCount int32
Bones *BoneInfo
FramePoses **Transform
Name [32]int8
Name [32]uint8
}

// GetBones returns the bones information (skeleton) of a ModelAnimation as go slice
func (m ModelAnimation) GetBones() []BoneInfo {
return unsafe.Slice(m.Bones, m.BoneCount)
}

// GetFramePose returns the Transform for a specific bone at a specific frame
func (m ModelAnimation) GetFramePose(frame, bone int) Transform {
framePoses := unsafe.Slice(m.FramePoses, m.FrameCount)
return unsafe.Slice(framePoses[frame], m.BoneCount)[bone]
}

// GetName returns the ModelAnimation's name as go string
func (m ModelAnimation) GetName() string {
var end int
for end = range m.Name {
if m.Name[end] == 0 {
break
}
}
return string(m.Name[:end])
}

// RayCollision type - ray hit information
Expand Down

0 comments on commit 7a899c5

Please sign in to comment.