MCPcopy
hub / github.com/g3n/engine / LoadMesh

Method LoadMesh

loader/gltf/loader.go:432–543  ·  view source on GitHub ↗

LoadMesh creates and returns a Graphic Node (graphic.Mesh, graphic.Lines, graphic.Points, etc) from the specified GLTF.Meshes index.

(meshIdx int)

Source from the content-addressed store, hash-verified

430// LoadMesh creates and returns a Graphic Node (graphic.Mesh, graphic.Lines, graphic.Points, etc)
431// from the specified GLTF.Meshes index.
432func (g *GLTF) LoadMesh(meshIdx int) (core.INode, error) {
433
434 // Check if provided mesh index is valid
435 if meshIdx < 0 || meshIdx >= len(g.Meshes) {
436 return nil, fmt.Errorf("invalid mesh index")
437 }
438 meshData := g.Meshes[meshIdx]
439 // Return cached if available
440 if meshData.cache != nil {
441 // TODO CLONE/REINSTANCE INSTEAD
442 //log.Debug("Instancing Mesh %d (from cached)", meshIdx)
443 //return meshData.cache, nil
444 }
445 log.Debug("Loading Mesh %d", meshIdx)
446
447 var err error
448
449 // Create container node
450 var meshNode core.INode
451 meshNode = core.NewNode()
452
453 for i := 0; i < len(meshData.Primitives); i++ {
454
455 // Get primitive information
456 p := meshData.Primitives[i]
457
458 // Indexed Geometry
459 indices := math32.NewArrayU32(0, 0)
460 if p.Indices != nil {
461 pidx, err := g.loadIndices(*p.Indices)
462 if err != nil {
463 return nil, err
464 }
465 indices = append(indices, pidx...)
466 } else {
467 // Non-indexed primitive
468 // indices array stay empty
469 }
470
471 // Load primitive material
472 var grMat material.IMaterial
473 if p.Material != nil {
474 grMat, err = g.LoadMaterial(*p.Material)
475 if err != nil {
476 return nil, err
477 }
478 } else {
479 grMat = g.newDefaultMaterial()
480 }
481
482 // Create geometry
483 var igeom geometry.IGeometry
484 igeom = geometry.NewGeometry()
485 geom := igeom.GetGeometry()
486
487 err = g.loadAttributes(geom, p.Attributes, indices)
488 if err != nil {
489 return nil, err

Callers 1

LoadNodeMethod · 0.95

Calls 15

loadIndicesMethod · 0.95
LoadMaterialMethod · 0.95
newDefaultMaterialMethod · 0.95
GetGeometryMethod · 0.95
loadAttributesMethod · 0.95
AddMorphTargetDeltasMethod · 0.95
GetNodeMethod · 0.95
NewNodeFunction · 0.92
NewArrayU32Function · 0.92
NewGeometryFunction · 0.92
NewMorphGeometryFunction · 0.92
NewMeshFunction · 0.92

Tested by

no test coverage detected