rigged_mesh.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package graphic
  5. import (
  6. "github.com/g3n/engine/gls"
  7. "strconv"
  8. "github.com/g3n/engine/core"
  9. )
  10. // MaxBoneInfluencers is the maximum number of bone influencers per vertex.
  11. const MaxBoneInfluencers = 4
  12. // RiggedMesh is a Mesh associated with a skeleton.
  13. type RiggedMesh struct {
  14. *Mesh // Embedded mesh
  15. skeleton *Skeleton
  16. mBones gls.Uniform
  17. }
  18. // NewRiggedMesh returns a new rigged mesh.
  19. func NewRiggedMesh(mesh *Mesh) *RiggedMesh {
  20. rm := new(RiggedMesh)
  21. rm.Mesh = mesh
  22. rm.SetIGraphic(rm)
  23. rm.mBones.Init("mBones")
  24. rm.ShaderDefines.Set("BONE_INFLUENCERS", strconv.Itoa(MaxBoneInfluencers))
  25. rm.ShaderDefines.Set("TOTAL_BONES", "0")
  26. return rm
  27. }
  28. // SetSkeleton sets the skeleton used by the rigged mesh.
  29. func (rm *RiggedMesh) SetSkeleton(sk *Skeleton) {
  30. rm.skeleton = sk
  31. rm.ShaderDefines.Set("TOTAL_BONES", strconv.Itoa(len(rm.skeleton.Bones())))
  32. }
  33. // SetSkeleton returns the skeleton used by the rigged mesh.
  34. func (rm *RiggedMesh) Skeleton() *Skeleton {
  35. return rm.skeleton
  36. }
  37. // RenderSetup is called by the renderer before drawing the geometry.
  38. func (rm *RiggedMesh) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
  39. // Call base mesh's RenderSetup
  40. rm.Mesh.RenderSetup(gs, rinfo)
  41. // Transfer bone matrices
  42. boneMatrices := rm.skeleton.BoneMatrices()
  43. location := rm.mBones.Location(gs)
  44. gs.UniformMatrix4fv(location, int32(len(boneMatrices)), false, &boneMatrices[0][0])
  45. }