library_materials.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. return nil
  78. }
  79. func (d *Decoder) decMaterial(start xml.StartElement, lm *LibraryMaterials) error {
  80. mat := new(Material)
  81. mat.Id = findAttrib(start, "id").Value
  82. mat.Name = findAttrib(start, "name").Value
  83. lm.Material = append(lm.Material, mat)
  84. for {
  85. child, _, err := d.decNextChild(start)
  86. if err != nil || child.Name.Local == "" {
  87. return err
  88. }
  89. if child.Name.Local == "instance_effect" {
  90. err := d.decInstanceEffect(child, &mat.InstanceEffect)
  91. if err != nil {
  92. return err
  93. }
  94. continue
  95. }
  96. }
  97. return nil
  98. }
  99. func (d *Decoder) decInstanceEffect(start xml.StartElement, ie *InstanceEffect) error {
  100. ie.Sid = findAttrib(start, "sid").Value
  101. ie.Name = findAttrib(start, "name").Value
  102. ie.Url = findAttrib(start, "url").Value
  103. for {
  104. child, _, err := d.decNextChild(start)
  105. if err != nil || child.Name.Local == "" {
  106. return err
  107. }
  108. if child.Name.Local == "technique_hint setparam" {
  109. log.Warn("<technique_hint> not implemented")
  110. continue
  111. }
  112. if child.Name.Local == "setparam" {
  113. log.Warn("<setparam> not implemented")
  114. continue
  115. }
  116. }
  117. return nil
  118. }