|
entity - the mesh, or the parent of the meshes, to save. It stands for the file: its children are saved with their places, its own position, rotation and scale are not file$ - the .glb file to write; an existing file is replaced flags (optional) - what to leave out or join; add them together: 0: everything, as it is (default) 1: join every mesh into one, in the entity's space, as LoadMesh loads a file 2: leave out animation clips |
|
Saves an entity and its children, with their materials and textures, as a .glb model file. Anything you build in a program - a house made of cubes, a shape from the CSG commands, a mesh your own generator makes, a character joined with FuseMesh - lives only in memory until you save it. SaveMesh writes it as a glTF binary (.glb) file, which LoadMesh and LoadAnimMesh load straight back and which Blender, other engines and web viewers open too. It returns True when the file was written; on failure it returns False, leaves any file already there as it was, and ModelSaveError says why. The file holds the model as it is now. Positions, normals, both texture-coordinate sets, vertex colours, tangents and triangles are saved exactly, and loading the file gives back the same model: saving and loading again does not add a level to the hierarchy. Copies that share a mesh share it in the file too. Materials keep their glTF settings, and a classic brush (colour, shininess, blend, effects and every texture layer) is kept in the file's Blitz3D+ extras, so it comes back exactly as it was; other programs draw its colour and first texture. A texture loaded from a PNG or JPEG file, or embedded in a model, is written as it was stored, so nothing is lost; one you drew on, or made with CreateTexture, is written as PNG. DDS and cube-map textures are left out. Skinned characters keep their bones and skin, morphing meshes their targets, and LoadAnimMesh models their animation clips, by name, so the saved file animates as the original does. Levels of detail are saved too, so a model whose simpler versions you made once - with AutoEntityLODs, SimplifyEntityLOD or SetEntityLODMesh - loads with them, ready to draw, and the game makes nothing while it runs. They are written with glTF's MSFT_lod extension, which other viewers read as well. Gotcha: sprites, planes, particles, terrains, MD2 and BSP models are not meshes, so they save as empty nodes that keep their children in place. Lightmaps from BakeLighting are not saved. The file name must end in .glb. See also: ModelSaveError, LoadMesh, LoadAnimMesh, PrepareModel, FuseMesh. |
; SaveMesh Example
; ----------------
Graphics3D 640,480,0,2
SetBuffer BackBuffer()
camera=CreateCamera()
PositionEntity camera,0,1,-6
light=CreateLight()
RotateEntity light,45,45,0
; A street lamp built from primitives: a post with a lamp on top
post=CreateCylinder(16)
ScaleMesh post,0.12,1.5,0.12
EntityColor post,90,90,110
lamp=CreateSphere(16,post)
ScaleMesh lamp,0.35,0.35,0.35
PositionEntity lamp,0,1.7,0
EntityColor lamp,255,220,120
PositionEntity post,-1.5,0,0
; Save the post and its lamp as one .glb file
file$=SystemProperty("tempdir")+"street_lamp.glb"
saved=SaveMesh(post,file)
; Load the file back, beside the original
copy=LoadAnimMesh(file)
PositionEntity copy,1.5,0,0
While Not KeyDown(1)
TurnEntity post,0,1,0
If copy Then TurnEntity copy,0,1,0
; Arrow keys move the camera
If KeyDown(200) Then MoveEntity camera,0,0,0.1
If KeyDown(208) Then MoveEntity camera,0,0,-0.1
If KeyDown(203) Then TurnEntity camera,0,1,0
If KeyDown(205) Then TurnEntity camera,0,-1,0
RenderWorld
Text 0,0,"Arrow keys: move camera Esc: exit"
If saved Then
Text 0,20,"SaveMesh(post,file$) wrote "+FileSize(file)+" bytes: the original left, the file loaded back right"
Else
Text 0,20,"SaveMesh failed: "+ModelSaveError()
EndIf
Flip
Wend
; Tidy up the file the example wrote
DeleteFile file
End