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