Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModelAnimation improvements #474

Merged
merged 6 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading