Bakes surface-based mesh textures into atlas in-runtime, replacing multi-material with single texture.
Takes meshHandle (surface-based mesh entity), uvPadding (pixels between atlas regions, 0-8), atlasSize (atlas dimensions, power of 2).
Returns texture handle of created atlas, or -1 on failure.
3D Graphics
Parameters & Returns
Parameters
meshHandleInt
uvPaddingInt
atlasSizeInt
Returns
Int
Quick Summary
Bakes surface-based mesh textures into atlas in-runtime, replacing multi-material with single texture.
Takes meshHandle (surface-based mesh entity), uvPadding (pixels between atlas regions, 0-8), atlasSize (atlas dimensions, power of 2).
Returns texture handle of created atlas, or -1 on failure.
Technical Exegesis...
Bakes surface-based mesh textures into atlas in-runtime, replacing multi-material with single texture. Takes meshHandle (surface-based mesh entity), uvPadding (pixels between atlas regions, 0-8), atlasSize (atlas dimensions, power of 2). Returns texture handle of created atlas, or -1 on failure. Only works with surface-based meshes (created with b3dCreateMesh). Analyzes all surfaces to determine texture sizes and solid colors.
Bakes surface-based mesh textures into atlas in-runtime, replacing multi-material with single texture. Takes meshHandle (surface-based mesh entity), uvPadding (pixels between atlas regions, 0-8), atlasSize (atlas dimensions, power of 2). Returns texture handle of created atlas, or -1 on failure. Only works with surface-based meshes (created with b3dCreateMesh). Analyzes all surfaces to determine texture sizes and solid colors. Packs surface textures into atlas using bin-packing algorithm (shelf packing, largest first). Allocates GPU texture resource for atlas (DXGI_FORMAT_R8G8B8A8_UNORM). Renders each surface texture/color to atlas region using GPU copy or solid color fill. Remaps all vertex UVs in mesh from original coordinates to atlas coordinates: newUV = (oldUV * regionSize + regionOffset) / atlasSize. Replaces mesh material with single material referencing atlas texture. Updates mesh buffers on GPU. Returns atlas texture handle for use with b3dTextureToImage or b3dSetEntityBaseTexture. Returns -1 if: invalid handle, mesh not surface-based, atlas too small (surfaces don't fit), GPU allocation fails.
This is runtime version of b3dSaveMeshBaked - performs texture atlas baking in-memory without file export. Use cases: (1) Dynamic LOD generation (bake distant objects to single texture for performance), (2) Procedural content that changes per playthrough (randomized building textures), (3) Character customization (player selects colors/patterns, bake to atlas for rendering), (4) Real-time decal systems (graffiti/damage accumulated then baked). Performance: GPU-accelerated texture copying (fast), UV remapping (one-time CPU cost), bin-packing (negligible). After baking, mesh renders with single draw call instead of multiple (performance win for many surfaces). UVPadding prevents mipmap bleeding - essential for correct rendering at distance. Common values: padding=2, atlasSize=2048. If baking fails with "surfaces don't fit", increase atlasSize (2048->4096) or reduce individual surface texture sizes before calling. Texture handle returned can be read with b3dTextureToImage for editing (recolor atlas regions, add details), then re-upload with b3dImageToTexture and assign with b3dSetEntityBaseTexture. Baking is destructive - original multi-material data lost, can't unbake. For reusable workflow, keep original mesh and clone before baking. Alternative to baking: use texture arrays or bindless textures (more VRAM, fewer draw calls). Baked meshes ideal for static environment geometry.