material_common.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/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. // Technique
  21. technique := ""
  22. val, ok = m["technique"]
  23. if ok {
  24. technique = val.(string)
  25. }
  26. // Transparent
  27. transparent := false
  28. val, ok = m["transparent"]
  29. if ok {
  30. transparent = val.(bool)
  31. }
  32. // Defaul values
  33. ambient := []float32{0, 0, 0, 1}
  34. diffuse := []float32{0, 0, 0, 1}
  35. emission := []float32{0, 0, 0, 1}
  36. specular := []float32{0, 0, 0, 1}
  37. shininess := float32(0)
  38. transparency := float32(1)
  39. var texDiffuse *texture.Texture2D
  40. // Converts a slice of interface values which should be float64
  41. // to a slice of float32
  42. convIF32 := func(v interface{}) []float32 {
  43. si := v.([]interface{})
  44. res := make([]float32, 0)
  45. for i := 0; i < len(si); i++ {
  46. res = append(res, float32(si[i].(float64)))
  47. }
  48. return res
  49. }
  50. // Values
  51. values, ok := m["values"].(map[string]interface{})
  52. if ok {
  53. // Ambient light
  54. val, ok = values["ambient"]
  55. if ok {
  56. ambient = convIF32(val)
  57. }
  58. // Diffuse light
  59. val, ok = values["diffuse"]
  60. if ok {
  61. v := convIF32(val)
  62. // Checks for texture index
  63. if len(v) == 1 {
  64. var err error
  65. texDiffuse, err = g.loadTexture(int(v[0]))
  66. if err != nil {
  67. return nil, err
  68. }
  69. diffuse = []float32{1, 1, 1, 1}
  70. }
  71. }
  72. // Emission light
  73. val, ok = values["emission"]
  74. if ok {
  75. emission = convIF32(val)
  76. }
  77. // Specular light
  78. val, ok = values["specular"]
  79. if ok {
  80. specular = convIF32(val)
  81. }
  82. // Shininess
  83. val, ok = values["shininess"]
  84. if ok {
  85. s := convIF32(val)
  86. shininess = s[0]
  87. }
  88. // Transparency
  89. val, ok = values["transparency"]
  90. if ok {
  91. s := convIF32(val)
  92. transparency = s[0]
  93. }
  94. }
  95. //log.Error("doubleSided:%v", doubleSided)
  96. //log.Error("technique:%v", technique)
  97. //log.Error("transparent:%v", transparent)
  98. //log.Error("values:%v", values)
  99. //log.Error("ambient:%v", ambient)
  100. //log.Error("diffuse:%v", diffuse)
  101. //log.Error("emission:%v", emission)
  102. //log.Error("specular:%v", specular)
  103. //log.Error("shininess:%v", shininess)
  104. //log.Error("transparency:%v", transparency)
  105. var imat material.IMaterial
  106. if technique == "PHONG" {
  107. pm := material.NewPhong(&math32.Color{diffuse[0], diffuse[1], diffuse[2]})
  108. pm.SetAmbientColor(&math32.Color{ambient[0], ambient[1], ambient[2]})
  109. pm.SetEmissiveColor(&math32.Color{emission[0], emission[1], emission[2]})
  110. pm.SetSpecularColor(&math32.Color{specular[0], specular[1], specular[2]})
  111. pm.SetShininess(shininess)
  112. pm.SetOpacity(transparency)
  113. if texDiffuse != nil {
  114. pm.AddTexture(texDiffuse)
  115. }
  116. imat = pm
  117. } else {
  118. sm := material.NewStandard(&math32.Color{diffuse[0], diffuse[1], diffuse[2]})
  119. sm.SetAmbientColor(&math32.Color{ambient[0], ambient[1], ambient[2]})
  120. sm.SetEmissiveColor(&math32.Color{emission[0], emission[1], emission[2]})
  121. sm.SetSpecularColor(&math32.Color{specular[0], specular[1], specular[2]})
  122. sm.SetShininess(shininess)
  123. sm.SetOpacity(transparency)
  124. if texDiffuse != nil {
  125. sm.AddTexture(texDiffuse)
  126. }
  127. imat = sm
  128. }
  129. // Double Sided
  130. mat := imat.GetMaterial()
  131. if doubleSided {
  132. mat.SetSide(material.SideDouble)
  133. } else {
  134. mat.SetSide(material.SideFront)
  135. }
  136. // Transparency
  137. if transparent {
  138. mat.SetDepthMask(true)
  139. } else {
  140. mat.SetDepthMask(false)
  141. }
  142. return imat, nil
  143. }