library_materials.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "encoding/xml"
  7. "fmt"
  8. "io"
  9. )
  10. //
  11. // Library Materials
  12. //
  13. type LibraryMaterials struct {
  14. Id string
  15. Name string
  16. Asset *Asset
  17. Material []*Material
  18. }
  19. func (lm *LibraryMaterials) Dump(out io.Writer, indent int) {
  20. if lm == nil {
  21. return
  22. }
  23. fmt.Fprintf(out, "%sLibraryMaterials id:%s name:%s\n", sIndent(indent), lm.Id, lm.Name)
  24. for _, mat := range lm.Material {
  25. mat.Dump(out, indent+step)
  26. }
  27. }
  28. //
  29. // Material
  30. //
  31. type Material struct {
  32. Id string
  33. Name string
  34. Asset *Asset
  35. InstanceEffect InstanceEffect
  36. }
  37. func (mat *Material) Dump(out io.Writer, indent int) {
  38. fmt.Fprintf(out, "%sMaterial id:%s name:%s\n", sIndent(indent), mat.Id, mat.Name)
  39. ind := indent + step
  40. mat.InstanceEffect.Dump(out, ind)
  41. }
  42. //
  43. // InstanceEffect
  44. //
  45. type InstanceEffect struct {
  46. Sid string
  47. Name string
  48. Url string
  49. }
  50. func (ie *InstanceEffect) Dump(out io.Writer, indent int) {
  51. fmt.Fprintf(out, "%sInstanceEffect id:%s name:%s url:%s\n",
  52. sIndent(indent), ie.Sid, ie.Name, ie.Url)
  53. }
  54. func (d *Decoder) decLibraryMaterials(start xml.StartElement, dom *Collada) error {
  55. lm := new(LibraryMaterials)
  56. dom.LibraryMaterials = lm
  57. lm.Id = findAttrib(start, "id").Value
  58. lm.Name = findAttrib(start, "name").Value
  59. for {
  60. // Get next child element
  61. child, _, err := d.decNextChild(start)
  62. if err != nil || child.Name.Local == "" {
  63. return err
  64. }
  65. // Decodes <material>
  66. if child.Name.Local == "material" {
  67. err := d.decMaterial(child, lm)
  68. if err != nil {
  69. return err
  70. }
  71. continue
  72. }
  73. }
  74. return nil
  75. }
  76. func (d *Decoder) decMaterial(start xml.StartElement, lm *LibraryMaterials) error {
  77. mat := new(Material)
  78. mat.Id = findAttrib(start, "id").Value
  79. mat.Name = findAttrib(start, "name").Value
  80. lm.Material = append(lm.Material, mat)
  81. for {
  82. child, _, err := d.decNextChild(start)
  83. if err != nil || child.Name.Local == "" {
  84. return err
  85. }
  86. if child.Name.Local == "instance_effect" {
  87. err := d.decInstanceEffect(child, &mat.InstanceEffect)
  88. if err != nil {
  89. return err
  90. }
  91. continue
  92. }
  93. }
  94. return nil
  95. }
  96. func (d *Decoder) decInstanceEffect(start xml.StartElement, ie *InstanceEffect) error {
  97. ie.Sid = findAttrib(start, "sid").Value
  98. ie.Name = findAttrib(start, "name").Value
  99. ie.Url = findAttrib(start, "url").Value
  100. for {
  101. child, _, err := d.decNextChild(start)
  102. if err != nil || child.Name.Local == "" {
  103. return err
  104. }
  105. if child.Name.Local == "technique_hint setparam" {
  106. log.Warn("<technique_hint> not implemented")
  107. continue
  108. }
  109. if child.Name.Local == "setparam" {
  110. log.Warn("<setparam> not implemented")
  111. continue
  112. }
  113. }
  114. return nil
  115. }