Skip to content

Commit

Permalink
Add function to export animation mesh data
Browse files Browse the repository at this point in the history
  • Loading branch information
JiepengTan committed Aug 13, 2024
1 parent 554822c commit 755a2bd
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
18 changes: 18 additions & 0 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"time"
"unsafe"

"github.com/goplus/spx/internal/anim"
"github.com/goplus/spx/internal/audiorecord"
"github.com/goplus/spx/internal/coroutine"
"github.com/goplus/spx/internal/engine"
Expand Down Expand Up @@ -1351,3 +1352,20 @@ func (p *Game) GetWidget(name string) Widget {
}
return nil
}

func Gopt_ParseSkeletonAnimData(resource interface{}, sprite string, animName string) (*anim.AnimExportData, error) {
fs, err := resourceDir(resource)
if err != nil {
panic(err)
}
var baseDir = "sprites/" + sprite + "/"
var conf spriteConfig
err = loadJson(&conf, fs, baseDir+"index.json")
if err != nil {
return nil, err
}
animator := anim.NewAnimator(fs, baseDir, conf.Animator, conf.Avatar)
data, err := anim.GetExportData(animator, animName)
fmt.Println("Animation Frame Count ", len(data.Frames))
return data, err
}
19 changes: 19 additions & 0 deletions internal/anim/anim.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ type IAnimator interface {
Draw(screen *ebiten.Image)
Play(clipName string) *common.AnimClipState
GetClipState(clipName string) *common.AnimClipState
GetFrameData() *common.AnimExportFrame
}

type AnimExportData struct {
Frames []common.AnimExportFrame
}

const (
Expand Down Expand Up @@ -115,6 +120,20 @@ func NewAnimator(fs spxfs.Dir, spriteDir string, animatorPath string, avatarPath
return animator
}

func GetExportData(pself IAnimator, animName string) (*AnimExportData, error) {
data := &AnimExportData{}
if pself.GetClipState(animName) == nil {
return data, errors.New("can not find animation clip " + animName)
}
state := pself.Play(animName)
data.Frames = make([]common.AnimExportFrame, state.FrameCount)
for i := 0; i < state.FrameCount; i++ {
pself.Update()
data.Frames[i] = *pself.GetFrameData()
}
return data, nil
}

func NewAnim(name string, fps float64, totalframe int, isLoop bool) *Anim {
this := &Anim{}

Expand Down
8 changes: 6 additions & 2 deletions internal/anim/common/animator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import (
"github.com/hajimehoshi/ebiten/v2"
)

type AnimFrameExportData struct {
type AnimExportFrame struct {
Meshes []AnimExportMesh
}

type AnimExportMesh struct {
Indices []uint16 `json:"Indices"`
Vertices []math32.Vector3 `json:"Vertices"`
Uvs []math32.Vector2 `json:"Uvs"`
Vertices []math32.Vector3 `json:"Vertices"`
}

type Avatar struct {
Expand Down
14 changes: 14 additions & 0 deletions internal/anim/skeleton/animator.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ func (pself *Animator) Update() {
}
}

func (pself *Animator) GetFrameData() *common.AnimExportFrame {
retVal := &common.AnimExportFrame{}
retVal.Meshes = make([]common.AnimExportMesh, len(pself.Mesh.SkinMesh))
for k := 0; k < len(pself.RederOrder); k++ {
i := pself.RederOrder[k]
item := common.AnimExportMesh{}
item.Indices = pself.Mesh.SkinMesh[i].Indices
item.Uvs = pself.Mesh.SkinMesh[i].Uvs
item.Vertices = pself.LogicVertices[i]
retVal.Meshes[i] = item
}
return retVal
}

func (pself *Animator) drawBone(screen *ebiten.Image) {
for i := 0; i < len(pself.RenderBones); i++ {
pos := pself.RenderBones[i]
Expand Down
5 changes: 5 additions & 0 deletions internal/anim/vertex/animator.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,8 @@ func (pself *Animator) Update() {
RenderVerteies[j].DstY = float32(pos.Y)
}
}

func (pself *Animator) GetFrameData() *common.AnimExportFrame {
// TODO (jiepengtan): implement this
return nil
}

0 comments on commit 755a2bd

Please sign in to comment.