material_pbr.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package gltf
  2. import (
  3. "fmt"
  4. "github.com/g3n/engine/material"
  5. "github.com/g3n/engine/math32"
  6. )
  7. func (g *GLTF) loadMaterialPBR(m *Material) (material.IMaterial, error) {
  8. // Get pbr information
  9. pbr := m.PbrMetallicRoughness
  10. if pbr == nil {
  11. return nil, fmt.Errorf("PbrMetallicRoughness not supplied")
  12. }
  13. // Create new physically based material
  14. pm := material.NewPhysical()
  15. pm.SetTransparent(true) // TODO when to set this to true?
  16. // Double sided
  17. if m.DoubleSided {
  18. pm.SetSide(material.SideDouble)
  19. } else {
  20. pm.SetSide(material.SideFront)
  21. }
  22. // BaseColorFactor
  23. var baseColorFactor math32.Color4
  24. if pbr.BaseColorFactor != nil {
  25. baseColorFactor = math32.Color4{pbr.BaseColorFactor[0], pbr.BaseColorFactor[1], pbr.BaseColorFactor[2], pbr.BaseColorFactor[3]}
  26. } else {
  27. baseColorFactor = math32.Color4{1,1,1,1}
  28. }
  29. pm.SetBaseColorFactor(&baseColorFactor)
  30. // MetallicFactor
  31. var metallicFactor float32
  32. if pbr.MetallicFactor != nil {
  33. metallicFactor = *pbr.MetallicFactor
  34. } else {
  35. metallicFactor = 1
  36. }
  37. pm.SetMetallicFactor(metallicFactor)
  38. // RoughnessFactor
  39. var roughnessFactor float32
  40. if pbr.RoughnessFactor != nil {
  41. roughnessFactor = *pbr.RoughnessFactor
  42. } else {
  43. roughnessFactor = 1
  44. }
  45. pm.SetRoughnessFactor(roughnessFactor)
  46. // EmissiveFactor
  47. var emissiveFactor math32.Color
  48. if m.EmissiveFactor != nil {
  49. emissiveFactor = math32.Color{m.EmissiveFactor[0], m.EmissiveFactor[1], m.EmissiveFactor[2]}
  50. } else {
  51. if m.EmissiveTexture != nil {
  52. emissiveFactor = math32.Color{1, 1, 1}
  53. } else {
  54. emissiveFactor = math32.Color{0,0,0}
  55. }
  56. }
  57. pm.SetEmissiveFactor(&emissiveFactor)
  58. // BaseColorTexture
  59. if pbr.BaseColorTexture != nil {
  60. tex, err := g.NewTexture(pbr.BaseColorTexture.Index)
  61. if err != nil {
  62. return nil, err
  63. }
  64. pm.SetBaseColorMap(tex)
  65. }
  66. // MetallicRoughnessTexture
  67. if pbr.MetallicRoughnessTexture != nil {
  68. tex, err := g.NewTexture(pbr.MetallicRoughnessTexture.Index)
  69. if err != nil {
  70. return nil, err
  71. }
  72. pm.SetMetallicRoughnessMap(tex)
  73. }
  74. // NormalTexture
  75. if m.NormalTexture != nil {
  76. tex, err := g.NewTexture(m.NormalTexture.Index)
  77. if err != nil {
  78. return nil, err
  79. }
  80. pm.SetNormalMap(tex)
  81. }
  82. // OcclusionTexture
  83. if m.OcclusionTexture != nil {
  84. tex, err := g.NewTexture(m.OcclusionTexture.Index)
  85. if err != nil {
  86. return nil, err
  87. }
  88. pm.SetOcclusionMap(tex)
  89. }
  90. // EmissiveTexture
  91. if m.EmissiveTexture != nil {
  92. tex, err := g.NewTexture(m.EmissiveTexture.Index)
  93. if err != nil {
  94. return nil, err
  95. }
  96. pm.SetEmissiveMap(tex)
  97. }
  98. return pm, nil
  99. }