Hi team!
@Evgeni_Popov @sebavan @Deltakosh @RaananW @carolhmj
@mawa @labris
I’d like to propose a feature which allows to create texture atlases dynamically.
My first very quick thoughts about the interface are:
interface TextureAtlasEntry {
id: string
idx: number
x: number
y: number
w: number
h: number
uvs: number[]
}
interface TextureAtlas {
getTexture(): BABYLON.Texture
getDimensions(): { w: number, h: number } // actual dimensions of the ADT texture
length(): number // number of entries in the texture atlas
putImage(id: string, image: Image): number
putTexture(id: string, texture: BABYLON.Texture): number
putCanvas(id: string, canvas: Canvas2D): number
getById(id: string): TextureAtlasEntry
getByIndex(id: number): TextureAtlasEntry
entries(): TextureAtlasEntry[]
crop(): void
pack(): void
}
const atlas = new TextureAtlas(w: number, h: number)
The put
method should add the textures to an ADT sequentially. If there is an overfloww (no room for the next entry) it will throw an error/or could resize the texture. Maybe all put
methods could have a nullable options parameter where the user could copy part of the source image/canvas/texture to the the atlas:
interface TextureAtlasEntryOptions {
sx: number
sy: number
dx: number
dy: number
etc...
}
getTexture()
will return a Texure
for the material.
crop()
will crop the ADT to optimal dimensions removing any removable blank space without rearanging the entries
pack
will rearange the entries for best fit. I would start with a simple and fast algorithm (I found this quite interesting: Simple rectangle packing / Volodymyr Agafonkin | Observable)
You can the set the texture to the materail’s texture, get the atlas entry which returns the UVs and set those UVs on the mesh.
If interested, I will gladly implement tthis feature.
Let me know your thoughts, hints, ideas… Thank you!
The idea came from this topic: