Blend loader: Add texture cache.

This commit is contained in:
Alberto Torres 2024-12-16 21:17:43 +01:00
parent 67307032da
commit 8155c53314
3 changed files with 10 additions and 2 deletions

View file

@ -94,6 +94,7 @@ method close*(self: BlendLoader) =
self.resource = nil
self.blend_file = nil
self.cached_materials.clear()
self.textures.clear()
method openAssetFile*(self: BlendLoader, path: string) =
if self.resource != nil:
@ -204,17 +205,21 @@ method loadTextureImpl*(self: BlendLoader, name: string, img: FNode): Texture =
return self.engine.newTexture(name,1,1,1,RGBA_f16,pixels=c16.to float32)
# of 5: # UDIM sequence
else:
assert false, "Image source not supported yet: " & $source
assert false, &"Image source not supported yet: {source}, image '{name}'"
method loadTexture*(self: BlendLoader, name: string): Texture =
# sources:
# 1 image
# 4 generated
# 5 render
if name in self.textures:
return self.textures[name]
for img in self.blend_file.named_blocks["IM"]:
let nm = img.id.name.str.strip2
if name == nm:
return self.loadTextureImpl(name, img)
let tex = self.loadTextureImpl(name, img)
self.textures[name] = tex
return tex
raise newException(KeyError, "Can't find image " & name)
proc makeMaterialAndTextures(self: BlendLoader;

View file

@ -77,6 +77,8 @@ proc pack_normal(v: Vec3): float32 =
(z)
let f = cast[float32](i)
return f - (if f >= 0.0: 1.0 else: -1.0)
proc equals[T](a: ArrRef[T], s:seq[T], offset: int): bool =
for i, v in a:
if a[i] != s[offset+i]: return false

View file

@ -914,6 +914,7 @@ type BlendLoader* = ref object of Loader
cached_materials*: Table[(FNode, string, bool),
(string, seq[Varying], OrderedTable[string, string], OrderedTable[string, TexturePixels])] ## private
resource*: LoadableResource
textures*: Table[string, Texture]
template enqueue*(renderer: RenderManager, fun: untyped) =
## Run a proc after the renderer has been initialized.