khr_materials_common.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package gltf
  2. import (
  3. "github.com/g3n/engine/material"
  4. "github.com/g3n/engine/math32"
  5. "github.com/g3n/engine/texture"
  6. )
  7. // loadMaterialCommon receives an interface value describing a KHR_materials_common extension,
  8. // decodes it and returns a Material closest to the specified description.
  9. // The specification of this extension is at:
  10. // https://github.com/KhronosGroup/glTF/tree/master/extensions/1.0/Khronos/KHR_materials_common
  11. func (g *GLTF) loadMaterialCommon(ext interface{}) (material.IMaterial, error) {
  12. // The extension must be an object
  13. m := ext.(map[string]interface{})
  14. // Double sided
  15. doubleSided := false
  16. val, ok := m["doubleSided"]
  17. if ok {
  18. doubleSided = val.(bool)
  19. }
  20. // Transparent
  21. transparent := false
  22. val, ok = m["transparent"]
  23. if ok {
  24. transparent = val.(bool)
  25. }
  26. // Defaul values
  27. ambient := []float32{0, 0, 0, 1}
  28. diffuse := []float32{0, 0, 0, 1}
  29. emission := []float32{0, 0, 0, 1}
  30. specular := []float32{0, 0, 0, 1}
  31. shininess := float32(0)
  32. transparency := float32(1)
  33. var texDiffuse *texture.Texture2D
  34. // Converts a slice of interface values which should be float64
  35. // to a slice of float32
  36. convIF32 := func(v interface{}) []float32 {
  37. si := v.([]interface{})
  38. res := make([]float32, 0)
  39. for i := 0; i < len(si); i++ {
  40. res = append(res, float32(si[i].(float64)))
  41. }
  42. return res
  43. }
  44. // Values
  45. values, ok := m["values"].(map[string]interface{})
  46. if ok {
  47. // Ambient light
  48. val, ok = values["ambient"]
  49. if ok {
  50. ambient = convIF32(val)
  51. }
  52. // Diffuse light
  53. val, ok = values["diffuse"]
  54. if ok {
  55. v := convIF32(val)
  56. // Checks for texture index
  57. if len(v) == 1 {
  58. var err error
  59. texDiffuse, err = g.LoadTexture(int(v[0]))
  60. if err != nil {
  61. return nil, err
  62. }
  63. diffuse = []float32{1, 1, 1, 1}
  64. }
  65. }
  66. // Emission light
  67. val, ok = values["emission"]
  68. if ok {
  69. emission = convIF32(val)
  70. }
  71. // Specular light
  72. val, ok = values["specular"]
  73. if ok {
  74. specular = convIF32(val)
  75. }
  76. // Shininess
  77. val, ok = values["shininess"]
  78. if ok {
  79. s := convIF32(val)
  80. shininess = s[0]
  81. }
  82. // Transparency
  83. val, ok = values["transparency"]
  84. if ok {
  85. s := convIF32(val)
  86. transparency = s[0]
  87. }
  88. }
  89. //log.Error("doubleSided:%v", doubleSided)
  90. //log.Error("technique:%v", technique)
  91. //log.Error("transparent:%v", transparent)
  92. //log.Error("values:%v", values)
  93. //log.Error("ambient:%v", ambient)
  94. //log.Error("diffuse:%v", diffuse)
  95. //log.Error("emission:%v", emission)
  96. //log.Error("specular:%v", specular)
  97. //log.Error("shininess:%v", shininess)
  98. //log.Error("transparency:%v", transparency)
  99. mat := material.NewStandard(&math32.Color{diffuse[0], diffuse[1], diffuse[2]})
  100. mat.SetAmbientColor(&math32.Color{ambient[0], ambient[1], ambient[2]})
  101. mat.SetEmissiveColor(&math32.Color{emission[0], emission[1], emission[2]})
  102. mat.SetSpecularColor(&math32.Color{specular[0], specular[1], specular[2]})
  103. mat.SetShininess(shininess)
  104. mat.SetOpacity(transparency)
  105. if texDiffuse != nil {
  106. mat.AddTexture(texDiffuse)
  107. }
  108. // Double Sided
  109. if doubleSided {
  110. mat.SetSide(material.SideDouble)
  111. } else {
  112. mat.SetSide(material.SideFront)
  113. }
  114. // Transparency
  115. if transparent {
  116. mat.SetDepthMask(true)
  117. } else {
  118. mat.SetDepthMask(false)
  119. }
  120. return mat, nil
  121. }