material_common.go 3.4 KB

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