Skip to content

Commit

Permalink
Store textures in asset to avoid memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsmalm committed Apr 26, 2023
1 parent fde244f commit 9ae86f4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Fixed an issue which caused memory leaks when creating several models from the same glTF asset.

## [2.3.1] - 2023-03-29
### Fixed
- Fixed an issue which caused rendering to crash when using iOS 16.4.
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Jens Malmborg
Copyright (c) 2023 Jens Malmborg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 5 additions & 0 deletions src/gltf/gltf-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { Compatibility } from "../compatibility/compatibility"
* glTF assets are JSON files plus supporting external data.
*/
export class glTFAsset {
/**
* The textures used by this asset.
*/
readonly textures: Texture[] = []

/**
* Creates a new glTF asset using the specified JSON descriptor.
* @param descriptor The JSON descriptor to create the asset from.
Expand Down
17 changes: 9 additions & 8 deletions src/gltf/gltf-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export class glTFParser {
private _asset: glTFAsset
private _materialFactory: MaterialFactory
private _descriptor: any
private _textures: Texture[] = []

/**
* Creates a new parser using the specified asset.
Expand All @@ -36,8 +35,10 @@ export class glTFParser {
this._asset = asset
this._materialFactory = materialFactory || new StandardMaterialFactory()
this._descriptor = this._asset.descriptor
for (let i = 0; i < this._descriptor.textures?.length; i++) {
this._textures.push(this.parseTexture(i))
if (asset.textures.length === 0) {
for (let i = 0; i < this._descriptor.textures?.length; i++) {
asset.textures.push(this.parseTexture(i))
}
}
}

Expand Down Expand Up @@ -126,7 +127,7 @@ export class glTFParser {
return this._materialFactory.create(result)
}
if (material.occlusionTexture !== undefined) {
result.occlusionTexture = this._textures[material.occlusionTexture.index].clone()
result.occlusionTexture = this._asset.textures[material.occlusionTexture.index].clone()
result.occlusionTexture.strength = material.occlusionTexture.strength
result.occlusionTexture.texCoord = material.occlusionTexture.texCoord
if (material.occlusionTexture.extensions && material.occlusionTexture.extensions.KHR_texture_transform) {
Expand All @@ -137,7 +138,7 @@ export class glTFParser {
}
}
if (material.normalTexture !== undefined) {
result.normalTexture = this._textures[material.normalTexture.index].clone()
result.normalTexture = this._asset.textures[material.normalTexture.index].clone()
result.normalTexture.scale = material.normalTexture.scale || 1
result.normalTexture.texCoord = material.normalTexture.texCoord
if (material.normalTexture.extensions && material.normalTexture.extensions.KHR_texture_transform) {
Expand All @@ -148,7 +149,7 @@ export class glTFParser {
}
}
if (material.emissiveTexture !== undefined) {
result.emissiveTexture = this._textures[material.emissiveTexture.index].clone()
result.emissiveTexture = this._asset.textures[material.emissiveTexture.index].clone()
result.emissiveTexture.texCoord = material.emissiveTexture.texCoord
if (material.emissiveTexture.extensions && material.emissiveTexture.extensions.KHR_texture_transform) {
result.emissiveTexture.transform = material.emissiveTexture.extensions.KHR_texture_transform
Expand All @@ -171,7 +172,7 @@ export class glTFParser {
}
let pbr = material.pbrMetallicRoughness
if (pbr?.metallicRoughnessTexture !== undefined) {
result.metallicRoughnessTexture = this._textures[pbr.metallicRoughnessTexture.index].clone()
result.metallicRoughnessTexture = this._asset.textures[pbr.metallicRoughnessTexture.index].clone()
result.metallicRoughnessTexture.texCoord = pbr.metallicRoughnessTexture.texCoord
if (pbr.metallicRoughnessTexture.extensions && pbr.metallicRoughnessTexture.extensions.KHR_texture_transform) {
result.metallicRoughnessTexture.transform = pbr.metallicRoughnessTexture.extensions.KHR_texture_transform
Expand All @@ -184,7 +185,7 @@ export class glTFParser {
result.baseColor = pbr.baseColorFactor
}
if (pbr?.baseColorTexture !== undefined) {
result.baseColorTexture = this._textures[pbr.baseColorTexture.index].clone()
result.baseColorTexture = this._asset.textures[pbr.baseColorTexture.index].clone()
result.baseColorTexture.texCoord = pbr.baseColorTexture.texCoord
if (pbr.baseColorTexture.extensions && pbr.baseColorTexture.extensions.KHR_texture_transform) {
result.baseColorTexture.transform = pbr.baseColorTexture.extensions.KHR_texture_transform
Expand Down

0 comments on commit 9ae86f4

Please sign in to comment.