From b080ced82df543faa0d7b780a1a198c5edbc43d4 Mon Sep 17 00:00:00 2001 From: Momchil Atanasov Date: Sun, 29 Sep 2024 03:25:22 +0300 Subject: [PATCH] Add support for metallic-roughness textures --- game/asset/dsl/provider_model.go | 23 +++++++++++++++++------ game/asset/mdl/converter.go | 2 +- game/asset/mdl/texture.go | 9 +++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/game/asset/dsl/provider_model.go b/game/asset/dsl/provider_model.go index d5db0b32..7416ae68 100644 --- a/game/asset/dsl/provider_model.go +++ b/game/asset/dsl/provider_model.go @@ -213,6 +213,8 @@ func BuildModelResource(gltfDoc *gltf.Document, forceCollision bool) (*mdl.Model } if texIndex := gltfutil.MetallicRoughnessTextureIndex(gltfDoc, gltfPBR); texIndex != nil { metallicRoughnessTextureIndex = texIndex + sampler := samplersFromIndex[*texIndex] + sampler.Texture().SetLinear(true) } } else { color = sprec.NewVec4(1.0, 1.0, 1.0, 1.0) @@ -771,17 +773,26 @@ func createPBRShader(cfg pbrShaderConfig) string { if cfg.hasAlphaTesting { sourceCode += ` - if #color.a < alphaThreshold { - discard - } + if #color.a < alphaThreshold { + discard + } ` } - sourceCode += ` + if cfg.hasMetallicRoughnessTexture { + sourceCode += ` + var metallicRoughness vec4 = sample(metallicRoughnessSampler, #vertexUV) + #metallic = metallicRoughness.b + #roughness = metallicRoughness.g + ` + } else { + sourceCode += ` #metallic = metallic #roughness = roughness - } - ` + ` + } + + sourceCode += `}` return sourceCode } diff --git a/game/asset/mdl/converter.go b/game/asset/mdl/converter.go index 59f95b85..e2700e43 100644 --- a/game/asset/mdl/converter.go +++ b/game/asset/mdl/converter.go @@ -837,7 +837,7 @@ func (c *Converter) convertTexture(texture *Texture) (uint32, error) { default: return 0, fmt.Errorf("unsupported texture kind %d", texture.Kind()) } - if isLikelyLinearSpace(texture.format) { + if isLikelyLinearSpace(texture.format) || texture.isLinear { flags |= asset.TextureFlagLinearSpace } if texture.generateMipmaps { diff --git a/game/asset/mdl/texture.go b/game/asset/mdl/texture.go index 74657d38..55b80a96 100644 --- a/game/asset/mdl/texture.go +++ b/game/asset/mdl/texture.go @@ -45,6 +45,7 @@ type Texture struct { height int format TextureFormat generateMipmaps bool + isLinear bool layers []TextureLayer } @@ -83,6 +84,14 @@ func (t *Texture) SetFormat(format TextureFormat) { } } +func (t *Texture) Linear() bool { + return t.isLinear +} + +func (t *Texture) SetLinear(isLinear bool) { + t.isLinear = isLinear +} + func (t *Texture) GenerateMipmaps() bool { return t.generateMipmaps }