ソースを参照

implemented glTF loader caching of skins

Daniel Salvadori 7 年 前
コミット
fa82212ce3
2 ファイル変更12 行追加1 行削除
  1. 3 0
      loader/gltf/gltf.go
  2. 9 1
      loader/gltf/loader.go

+ 3 - 0
loader/gltf/gltf.go

@@ -13,6 +13,7 @@ import (
 	"github.com/g3n/engine/material"
 	"github.com/g3n/engine/math32"
 	"image"
+	"github.com/g3n/engine/graphic"
 )
 
 // glTF Extensions.
@@ -307,6 +308,8 @@ type Skin struct {
 	Name                string                 // The user-define named of this object. Not required.
 	Extensions          map[string]interface{} // Dictionary object with extension-specific objects. Not required.
 	Extras              interface{}            // Application-specific data. Not required.
+
+	cache *graphic.Skeleton // Cached skin.
 }
 
 // Sparse storage of attributes that deviate from their initialization value.

+ 9 - 1
loader/gltf/loader.go

@@ -273,8 +273,13 @@ func (g *GLTF) LoadSkin(skinIdx int) (*graphic.Skeleton, error) {
 	if skinIdx < 0 || skinIdx >= len(g.Skins) {
 		return nil, fmt.Errorf("invalid skin index")
 	}
-	log.Debug("Loading Skin %d", skinIdx)
 	skinData := g.Skins[skinIdx]
+	// Return cached if available
+	if skinData.cache != nil {
+		log.Debug("Fetching Skin %d (cached)", skinIdx)
+		return skinData.cache, nil
+	}
+	log.Debug("Loading Skin %d", skinIdx)
 
 	// Create Skeleton and set it on Rigged mesh
 	skeleton := graphic.NewSkeleton()
@@ -296,6 +301,9 @@ func (g *GLTF) LoadSkin(skinIdx int) (*graphic.Skeleton, error) {
 		skeleton.AddBone(jointNode.GetNode(), &ibm)
 	}
 
+	// Cache skin
+	g.Skins[skinIdx].cache = skeleton
+
 	return skeleton, nil
 }