Skip to content

Commit

Permalink
Add support for metallic-roughness textures
Browse files Browse the repository at this point in the history
  • Loading branch information
mokiat committed Sep 29, 2024
1 parent b9895a0 commit b080ced
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
23 changes: 17 additions & 6 deletions game/asset/dsl/provider_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion game/asset/mdl/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions game/asset/mdl/texture.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Texture struct {
height int
format TextureFormat
generateMipmaps bool
isLinear bool
layers []TextureLayer
}

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit b080ced

Please sign in to comment.