Skip to content

Commit

Permalink
Deduplicate images, samplers, textures (#48)
Browse files Browse the repository at this point in the history
* Deduplicate images, samplers and textures when adding a tex

* Fix tests

* Address comments

* Change test to check for no-empty-sampler state

* Rollback unnecessary changes
  • Loading branch information
alexykot authored Dec 16, 2024
1 parent 92790b3 commit 4ab31c8
Show file tree
Hide file tree
Showing 6 changed files with 821 additions and 53 deletions.
26 changes: 13 additions & 13 deletions formats/gltf/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (sg PolyformPbrSpecularGlossiness) ToMaterialExtensionData(w *Writer) map[s
}

if sg.DiffuseTexture != nil {
metadata["diffuseTexture"] = w.AddTexture(*sg.DiffuseTexture)
metadata["diffuseTexture"] = w.AddTexture(sg.DiffuseTexture)
}

if sg.SpecularFactor != nil {
Expand All @@ -74,7 +74,7 @@ func (sg PolyformPbrSpecularGlossiness) ToMaterialExtensionData(w *Writer) map[s
}

if sg.SpecularGlossinessTexture != nil {
metadata["specularGlossinessTexture"] = w.AddTexture(*sg.SpecularGlossinessTexture)
metadata["specularGlossinessTexture"] = w.AddTexture(sg.SpecularGlossinessTexture)
}
return metadata
}
Expand All @@ -101,7 +101,7 @@ func (tr PolyformTransmission) ToMaterialExtensionData(w *Writer) map[string]any
metadata["transmissionFactor"] = tr.Factor

if tr.Texture != nil {
metadata["transmissionTexture"] = w.AddTexture(*tr.Texture)
metadata["transmissionTexture"] = w.AddTexture(tr.Texture)
}

return metadata
Expand Down Expand Up @@ -142,7 +142,7 @@ func (v PolyformVolume) ToMaterialExtensionData(w *Writer) map[string]any {
metadata["thicknessFactor"] = v.ThicknessFactor

if v.ThicknessTexture != nil {
metadata["thicknessTexture"] = w.AddTexture(*v.ThicknessTexture)
metadata["thicknessTexture"] = w.AddTexture(v.ThicknessTexture)
}

if v.AttenuationDistance != nil {
Expand Down Expand Up @@ -216,15 +216,15 @@ func (ps PolyformSpecular) ToMaterialExtensionData(w *Writer) map[string]any {
}

if ps.Texture != nil {
metadata["specularTexture"] = w.AddTexture(*ps.Texture)
metadata["specularTexture"] = w.AddTexture(ps.Texture)
}

if ps.ColorFactor != nil {
metadata["specularColorFactor"] = rgbToFloatArr(ps.ColorFactor)
}

if ps.ColorTexture != nil {
metadata["specularColorTexture"] = w.AddTexture(*ps.ColorTexture)
metadata["specularColorTexture"] = w.AddTexture(ps.ColorTexture)
}

return metadata
Expand Down Expand Up @@ -260,12 +260,12 @@ func (pmc PolyformClearcoat) ToMaterialExtensionData(w *Writer) map[string]any {

metadata["clearcoatFactor"] = pmc.ClearcoatFactor
if pmc.ClearcoatTexture != nil {
metadata["clearcoatTexture"] = w.AddTexture(*pmc.ClearcoatTexture)
metadata["clearcoatTexture"] = w.AddTexture(pmc.ClearcoatTexture)
}

metadata["clearcoatRoughnessFactor"] = pmc.ClearcoatRoughnessFactor
if pmc.ClearcoatRoughnessTexture != nil {
metadata["clearcoatRoughnessTexture"] = w.AddTexture(*pmc.ClearcoatRoughnessTexture)
metadata["clearcoatRoughnessTexture"] = w.AddTexture(pmc.ClearcoatRoughnessTexture)
}

// if pmc.ClearcoatNormalTexture != nil {
Expand Down Expand Up @@ -344,7 +344,7 @@ func (pmi PolyformIridescence) ToMaterialExtensionData(w *Writer) map[string]any
metadata["iridescenceFactor"] = pmi.IridescenceFactor

if pmi.IridescenceTexture != nil {
metadata["iridescenceTexture"] = w.AddTexture(*pmi.IridescenceTexture)
metadata["iridescenceTexture"] = w.AddTexture(pmi.IridescenceTexture)
}

if pmi.IridescenceIor != nil {
Expand All @@ -360,7 +360,7 @@ func (pmi PolyformIridescence) ToMaterialExtensionData(w *Writer) map[string]any
}

if pmi.IridescenceThicknessTexture != nil {
metadata["iridescenceThicknessTexture"] = w.AddTexture(*pmi.IridescenceThicknessTexture)
metadata["iridescenceThicknessTexture"] = w.AddTexture(pmi.IridescenceThicknessTexture)
}

return metadata
Expand Down Expand Up @@ -397,13 +397,13 @@ func (ps PolyformSheen) ToMaterialExtensionData(w *Writer) map[string]any {
}

if ps.SheenColorTexture != nil {
metadata["sheenColorTexture"] = w.AddTexture(*ps.SheenColorTexture)
metadata["sheenColorTexture"] = w.AddTexture(ps.SheenColorTexture)
}

metadata["sheenRoughnessFactor"] = ps.SheenRoughnessFactor

if ps.SheenRoughnessTexture != nil {
metadata["sheenRoughnessTexture"] = w.AddTexture(*ps.SheenRoughnessTexture)
metadata["sheenRoughnessTexture"] = w.AddTexture(ps.SheenRoughnessTexture)
}

return metadata
Expand Down Expand Up @@ -441,7 +441,7 @@ func (pa PolyformAnisotropy) ToMaterialExtensionData(w *Writer) map[string]any {
metadata["anisotropyRotation"] = pa.AnisotropyRotation

if pa.AnisotropyTexture != nil {
metadata["anisotropyTexture"] = w.AddTexture(*pa.AnisotropyTexture)
metadata["anisotropyTexture"] = w.AddTexture(pa.AnisotropyTexture)
}

return metadata
Expand Down
2 changes: 1 addition & 1 deletion formats/gltf/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func TestMaterialExtension_ToExtensionData(t *testing.T) {
}

for k, v := range tc.want {
assert.Equal(t, v, data[k])
assert.Equal(t, v, data[k], "key %s not matching", k)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions formats/gltf/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type PolyformPbrMetallicRoughness struct {
}

type PolyformNormal struct {
PolyformTexture
*PolyformTexture
Scale *float64
}

Expand Down Expand Up @@ -125,7 +125,7 @@ func (pt *PolyformTexture) equal(other *PolyformTexture) bool {
}

func (pt *PolyformNormal) equal(other *PolyformNormal) bool {
if !pt.PolyformTexture.equal(&other.PolyformTexture) {
if !pt.PolyformTexture.equal(other.PolyformTexture) {
return false
}
return float64PtrsEqual(pt.Scale, other.Scale)
Expand Down
6 changes: 6 additions & 0 deletions formats/gltf/model_trackers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ type meshEntry struct {

// materialIndices handle deduplication of GLTF materials
type meshIndices map[meshEntry]int

// samplerIndices handle deduplication of texture samplers
type samplerIndices map[*Sampler]int

// textureIndices handle deduplication of textures
type textureIndices map[*PolyformTexture]int
Loading

0 comments on commit 4ab31c8

Please sign in to comment.