material.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 collada
  5. import (
  6. "fmt"
  7. "github.com/g3n/engine/material"
  8. "github.com/g3n/engine/math32"
  9. "github.com/g3n/engine/texture"
  10. "path/filepath"
  11. "strings"
  12. )
  13. // GetMaterial returns a pointer to an instance of the material
  14. // with the specified id in the Collada document and an error.
  15. // If no previous instance of the material was found it is created.
  16. func (d *Decoder) GetMaterial(id string) (material.IMaterial, error) {
  17. // If material already created, returns it
  18. mat := d.materials[id]
  19. if mat != nil {
  20. return mat, nil
  21. }
  22. // Creates material and saves it associated with its id
  23. mat, err := d.NewMaterial(id)
  24. if err != nil {
  25. return nil, err
  26. }
  27. d.materials[id] = mat
  28. return mat, nil
  29. }
  30. // NewMaterial creates and returns a pointer to a new material
  31. // from the specified material id/url in the dom
  32. func (d *Decoder) NewMaterial(id string) (material.IMaterial, error) {
  33. id = strings.TrimPrefix(id, "#")
  34. // Looks for material with specified id
  35. mat := findMaterial(&d.dom, id)
  36. if mat == nil {
  37. return nil, fmt.Errorf("Material id:%s not found", id)
  38. }
  39. // Looks for associated effect
  40. effect := findEffect(&d.dom, mat.InstanceEffect.Url)
  41. if effect == nil {
  42. return nil, fmt.Errorf("Effect id:%s not found", mat.InstanceEffect.Url)
  43. }
  44. // Looks for ProfileCOMMON
  45. pc := findProfileCOMMON(effect)
  46. if pc == nil {
  47. return nil, fmt.Errorf("ProfileCOMMON not found")
  48. }
  49. switch se := pc.Technique.ShaderElement.(type) {
  50. case *Blinn:
  51. return d.newBlinnMaterial(se)
  52. case *Constant:
  53. return d.newConstantMaterial(se)
  54. case *Lambert:
  55. return d.newLambertMaterial(se)
  56. case *Phong:
  57. return d.newPhongMaterial(se)
  58. default:
  59. return nil, fmt.Errorf("Invalid shader element")
  60. }
  61. return nil, nil
  62. }
  63. // GetTexture2D returns a pointer to an instance of the Texture2D
  64. // with the specified id in the Collada document and an error.
  65. // If no previous instance of the texture was found it is created.
  66. func (d *Decoder) GetTexture2D(id string) (*texture.Texture2D, error) {
  67. // If texture already created, returns it
  68. tex := d.tex2D[id]
  69. if tex != nil {
  70. return tex, nil
  71. }
  72. // Creates texture and saves it associated with its id
  73. tex, err := d.NewTexture2D(id)
  74. if err != nil {
  75. return nil, err
  76. }
  77. d.tex2D[id] = tex
  78. return tex, nil
  79. }
  80. // NewTexture2D creates and returns a pointer to a new Texture2D
  81. // from the specified sampler2D id/url in the dom
  82. func (d *Decoder) NewTexture2D(id string) (*texture.Texture2D, error) {
  83. // Find newparam in all effects profiles with the specified id
  84. np := findNewparam(&d.dom, id)
  85. if np == nil {
  86. return nil, fmt.Errorf("Texture id:%s not found", id)
  87. }
  88. // Checks if parameter is a Sampler2D
  89. sampler2D, ok := np.ParameterType.(*Sampler2D)
  90. if !ok {
  91. return nil, fmt.Errorf("Texture id:%s is not a sampler2D", id)
  92. }
  93. // Get the parameter for the Sampler2D source
  94. np = findNewparam(&d.dom, sampler2D.Source)
  95. if np == nil {
  96. return nil, fmt.Errorf("Sampler2D source:%s not found", id)
  97. }
  98. // Checks if parameter is a surface
  99. surface, ok := np.ParameterType.(*Surface)
  100. if !ok {
  101. return nil, fmt.Errorf("Sampler2D source:%s is not a Surface", id)
  102. }
  103. // Checks if surface Init is InitFrom
  104. initFrom, ok := surface.Init.(InitFrom)
  105. if !ok {
  106. return nil, fmt.Errorf("Surface:%s init is not InitFrom", sampler2D.Source)
  107. }
  108. // Find image
  109. img := findImage(&d.dom, initFrom.Uri)
  110. if img == nil {
  111. return nil, fmt.Errorf("Image:%s not found", initFrom.Uri)
  112. }
  113. // Get image init from
  114. imgInitFrom, ok := img.ImageSource.(InitFrom)
  115. if !ok {
  116. return nil, fmt.Errorf("Image:%s source is not InitFrom", initFrom.Uri)
  117. }
  118. // Builds image file path and try to create texture
  119. filepath := filepath.Join(d.dirImages, filepath.Base(imgInitFrom.Uri))
  120. tex, err := texture.NewTexture2DFromImage(filepath)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return tex, nil
  125. }
  126. func (d *Decoder) newBlinnMaterial(se *Blinn) (material.IMaterial, error) {
  127. return nil, fmt.Errorf("Not implemented")
  128. }
  129. func (d *Decoder) newConstantMaterial(se *Constant) (material.IMaterial, error) {
  130. return nil, fmt.Errorf("Not implemented")
  131. }
  132. func (d *Decoder) newLambertMaterial(se *Lambert) (material.IMaterial, error) {
  133. return nil, fmt.Errorf("Not implemented")
  134. }
  135. func (d *Decoder) newPhongMaterial(se *Phong) (material.IMaterial, error) {
  136. // Creates material with default color
  137. m := material.NewPhong(&math32.Color{0.5, 0.5, 0.5})
  138. // If "diffuse" is Color set its value in the material
  139. _, ok := se.Diffuse.(*Color)
  140. if ok {
  141. color := getColor(se.Diffuse)
  142. m.SetColor(&color)
  143. } else {
  144. // Diffuse must be a Texture
  145. tex, ok := se.Diffuse.(*Texture)
  146. if !ok {
  147. return nil, fmt.Errorf("diffuse is not Color nor Texture")
  148. }
  149. // Get texture 2D
  150. tex2D, err := d.GetTexture2D(tex.Texture)
  151. if err != nil {
  152. return nil, err
  153. }
  154. // Add texture to this material
  155. m.AddTexture(tex2D)
  156. }
  157. emission := getColor(se.Emission)
  158. m.SetEmissiveColor(&emission)
  159. //ambient := getColor(se.Ambient)
  160. //m.SetAmbientColor(&ambient)
  161. specular := getColor(se.Specular)
  162. m.SetSpecularColor(&specular)
  163. shininess := getFloatOrParam(se.Shininess)
  164. m.SetShininess(shininess)
  165. //m.SetOpacity(opacity float32) {
  166. //m.SetWireframe(true)
  167. m.SetSide(material.SideDouble)
  168. return m, nil
  169. }
  170. func getColor(ci interface{}) math32.Color {
  171. switch c := ci.(type) {
  172. case *Color:
  173. return math32.Color{c.Data[0], c.Data[1], c.Data[2]}
  174. break
  175. default:
  176. return math32.Color{}
  177. }
  178. return math32.Color{}
  179. }
  180. func getColor4(ci interface{}) math32.Color4 {
  181. switch c := ci.(type) {
  182. case Color:
  183. return math32.Color4{c.Data[0], c.Data[1], c.Data[2], c.Data[3]}
  184. break
  185. default:
  186. return math32.Color4{}
  187. }
  188. return math32.Color4{}
  189. }
  190. func getFloatOrParam(vi interface{}) float32 {
  191. switch v := vi.(type) {
  192. case *Float:
  193. return v.Data
  194. break
  195. default:
  196. return 0
  197. }
  198. return 0
  199. }
  200. func findMaterial(dom *Collada, id string) *Material {
  201. for _, m := range dom.LibraryMaterials.Material {
  202. if m.Id == id {
  203. return m
  204. }
  205. }
  206. return nil
  207. }
  208. func findEffect(dom *Collada, uri string) *Effect {
  209. id := strings.TrimPrefix(uri, "#")
  210. for _, effect := range dom.LibraryEffects.Effect {
  211. if effect.Id == id {
  212. return effect
  213. }
  214. }
  215. return nil
  216. }
  217. func findProfileCOMMON(ef *Effect) *ProfileCOMMON {
  218. for _, pi := range ef.Profile {
  219. pc, ok := pi.(*ProfileCOMMON)
  220. if ok {
  221. return pc
  222. }
  223. }
  224. return nil
  225. }
  226. func findNewparam(dom *Collada, uri string) *Newparam {
  227. id := strings.TrimPrefix(uri, "#")
  228. for _, effect := range dom.LibraryEffects.Effect {
  229. for _, prof := range effect.Profile {
  230. pc, ok := prof.(*ProfileCOMMON)
  231. if !ok {
  232. continue
  233. }
  234. for _, np := range pc.Newparam {
  235. if np.Sid == id {
  236. return np
  237. }
  238. }
  239. }
  240. }
  241. return nil
  242. }
  243. func findImage(dom *Collada, uri string) *Image {
  244. id := strings.TrimPrefix(uri, "#")
  245. for _, img := range dom.LibraryImages.Image {
  246. if img.Id == id {
  247. return img
  248. }
  249. }
  250. return nil
  251. }