material.go 6.8 KB

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