Models and assets
Your game's models, sounds and ground pictures are its own. To start with something, @v12-engine/assets has free models (CC0, from KayKit and Quaternius) you can publish a game with: characters, buildings, trees, props, weapons.
Quick start
npm install @v12-engine/assets// vite.config.ts
import { assetsPackage } from '@v12-engine/client/vite'
import { editorFiles } from '@v12-engine/editor/vite'
export default defineConfig({ plugins: [assetsPackage(), editorFiles()] })import { characters, forest, gear } from '@v12-engine/assets'
const view: GameView = {
units: {
// played with its own clips: Idle_A, Running_A, Melee_1H_Attack_Chop, Death_A…
looks: { knight: { model: { src: characters.models.knight, scale: 0.7, hold: [{ src: gear.models.sword1handed, at: 'handslot.r' }] } } }
},
ground: {
tiles: { grass: { color: [0.33, 0.52, 0.2], pattern: 'grass' } },
doodads: [forest.models.commonTree1, forest.models.rockMedium1]
}
}The files are served where shared files are (/models/characters/knight.glb), so the knight is drawn with a sword and the editor lists the trees and rocks to put down. A project from npm create @v12-engine has the package already (--no-assets leaves it out).
Common tasks
Scale a model to the game
Models are at their makers' sizes: trees about 4 tall, a character about 2.5, an rts building 2 to 6 across. Set ModelDef.scale, or a doodad's scale in the editor. @v12-engine/assets/sizes.json gives each model's size by path, a ninth of the manifest's bytes.
Change a model's materials
ModelDef.material sets its file's materials by name: a colour, a picture, what shines, metal and roughness, both sides. See Materials.
Keep a character's parts apart
A unit with its own skeleton (up close), a portrait and a cinematic's prop draw a character's parts that move with one skeleton and share a material as one mesh: the knight's nine parts are one draw. merge: false keeps each part a mesh of its own, by its name in the file, for code that hides, recolours or swaps one of them:
footman: { model: { src: characters.models.knight, scale: 0.7, merge: false } } // this model
// GameView.units.merge = false: every model that does not say. The model's own wins
client.renderer.kit.setPart(id, 'Knight_HelmetVisor', { hidden: true }) // a part by its name: hidden, or tintedThe cost is one draw a part (about nine for the knight) in the view and in every shadow pass, for each unit while it has its own skeleton. It is decided as each copy is built, not every frame: client.looks.set with another merge rebuilds the units up close a few a frame, their clip going on. The copies of a baked crowd are one mesh either way. See Large battles.
Replace a shared model
Put your own file at the same path in your public/: the project's file wins.
Give your game a folder of its own
files: {
assets: 'games/campaign'
} // under public: sounds/, models/, sounds/music/, with an assets.jsonWhat its assets.json lists comes from there, the rest from the shared folders, so two games never clash.
Make files smaller
npm run compress -- public/models packed # meshopt and WebP; --meshes draco, --textures ktx2
npm run decoders # the decoders, for a page that works offlinestartApp({ decoders: '/decoders/', warmDecoders: true }) // fetch decoders while the first scene loads
files: {
keepFiles: '1.4.0'
} // keep models and sounds in the browser between visits, until the version changesThe example's models went from 26 MB to 12. --meshes draco needs draco3dgltf; --textures ktx2 needs toktx.
Play an animation file on any model
A clip saved from the editor's Animation panel is animations/<name>.anim.glb. Any model plays it, retargeted by bone name, or by a table where names differ:
const look = {
height: 2,
model: {
src: 'units/ogre.glb',
scale: 1,
animations: { walk: 'animations/march.anim.glb' },
// only for the table; bones not in it are not used
animationFiles: { 'animations/march.anim.glb': { bones: { upperarm_l: 'Ogre_L_Arm', upperarm_r: 'Ogre_R_Arm', hips: 'Ogre_Pelvis' } } }
}
}At run time: await client.applyAnimation('units/ogre.glb', 'animations/march.anim.glb', { bones }), then client.playClip(id, 'animations/march.anim.glb', { loop: true }).
Sort assets into categories
files: {
assetCategories: [
{ id: 'my-weapons', name: 'My weapons', files: ['models/blades/*', 'sounds/swing_*'] },
{ id: 'town', name: 'Town', files: ['models/village/*', 'models/farm/barn.glb'] }
]
}* matches within a folder, ** across folders. Or decide in code: GameView.files.assetCategory(file).
Add art to the package
npm run add -w @v12-engine/assets -- forest path/to/oak.glb --author "You" --licence CC0-1.0
npm run check -w @v12-engine/assetsadd checks the file, puts it under a plain name, strips what it doesn't use, credits it and rewrites the manifest and index; it then says its name in code (forest.models.oak) and what breaks the conventions. check fails on a missing credit, a licence you can't publish with, or a model that needs files beside it.
Editor
- Drop a model on Content or Assets: it goes into your game's folder.
- Put it down and press Play to try it.
- In Assets, pick a file and set its category (written to the folder's
assets.json). - Open it in the Model view to set its scale, portrait, levels of detail and clips. See How do I bring in a model.
Reference
Groups of @v12-engine/assets
| Group | What |
|---|---|
characters | adventurers and skeletons with clips: knight, barbarian, mage, ranger, rogues, mannequins |
animations | KayKit's clips for its medium and large rigs |
gear | what adventurers hold: swords, axes, bows, crossbows, shields, a staff, a wand |
skeleton-gear | what skeletons hold |
dungeon-kit | KayKit's dungeon: floors, walls, gates, stairs, furniture, chests, loot |
weapon-bits | swords, axes, hammers, spears, a halberd, bows, staffs, shields |
weapons | axes, a cleaver, a dagger, a talwar, a trident |
rts | a fantasy RTS in ages: town centres, barracks, temples, castles, walls, farms, ports |
village | houses, an inn, a blacksmith, a mill, a stable |
farm | barns, a coop, silos, a windmill, fences |
dungeon | stone halls: walls, arches, columns, torches, treasure |
forest | trees, bushes, flowers, grass, mushrooms, rocks, paths |
trees | oaks, pines, columnar, swiggly trees, willows |
ground | ground pictures: blight (painted by groundPaint) |
items | armour, weapons, potions, books, keys, coins, crowns |
props | furniture, barrels, crates, candles, banners, tools |
resources | ore nuggets and bars, wood, planks, stone, bricks, cloth |
Models only: no sounds or music. Each group's CREDITS.md says whose its files are; manifest.json gives each file's animations, parts and size.
assetsPackage(options)
| Option | What |
|---|---|
groups | groups to take (['forest', 'rts']); all by default |
copy | what a build copies: 'used' (default), 'all', or a function of a file |
search | folders read for what the game uses (['src', 'public']) |
manifest | the package's manifest.json, if found elsewhere |
In development it serves the package behind your public/. In a build it copies what your code and maps name (a path, an index name, or a file name in quotes), named music, and the groups' sounds, and says how much it copied. A model whose path is built in code (`characters/${name}.glb`) isn't seen: use copy: 'all' or a function. Without Vite: packageFiles(root, options) and usedFiles from @v12-engine/client/vite.
Model files
- glTF: a
.glb, or a.gltfwith everything inside it. The editor makes a.gltfwith side files into one GLB on the way in. - Levels:
<model>.lod1.glbbeside it (fromnpm run lodor the Model view), listed inmodels/levels.json. See Detail and far scenery. - Shared rig clips: KayKit medium-rig characters keep clips in
characters/rig_medium_clips.glb(skeletons inrig_medium_skeleton_clips.glb), named in the character's"asset": { "extras": { "clips": [...] } }; the client loads it with the character.scripts/clips.mjsin the package does the same for your rig. - Clips that two files share key for key are kept once (
GameView.units.share.animations).
Categories, in order
A file is of the first category one of these says: your code (GameView.files.assetCategory), a declared category whose files match (in code or assets.json's categories), its @v12-engine/assets group (rts as "Fantasy RTS"), its folder (models/my-weapons/axe.glb is my-weapons). Otherwise its kind: Models, Textures, Sounds. In code: editor.assetCategories(), editor.assetCategoryOf(file), editor.putInCategory(path, id, name), assetCategoryOf, matchesFiles, withFileIn.
Animation files
Bones match by name (spelling differences too), or by the bones table; a model bone no file bone matches stays at rest. The file's marks and loop apply unless the look says its own (model.clips[<file>]). null in place of a table removes it. The retargeting code loads with the first animation file. A file with no matching bones is logged, and the model is drawn without it.
A model from Blender
- Scale: a cell is a metre. Set unit scale 1; a person is about 1.8 tall, a house 4 to 6 wide.
- Origin and facing: feet on the origin, facing −Y (towards the Front view's camera), exported with +Y Up. A Z-up file lies on its back:
ModelDef.up: 'z', or Model Setup's Turn upright (modelAnatomy(file).updetects it).ModelDef.turnturns one that faces elsewhere. - Apply all transforms.
- Animations: one action each, named as the game says (
idle,walk,attack,die), pushed to the NLA. Bake ones that use constraints or drivers. - Materials: one texture (an atlas) up to 1024 square, Principled BSDF only.
- Export: glTF Binary (.glb), +Y Up, Apply Modifiers, Animations on.
- Try it in the editor, then
npm run addit.
node packages/assets/scripts/center.mjs <files or folders> puts models made away from the origin on it (--check only reports).
Clip marks
An attack's blow lands at the clip's hit mark (ModelDef.clips[name].marks, a share of the clip; two fifths in when none), so the clip's windup matches the attack's (see Attack fields). In Set up as a unit, Set damage point from the clip and Fit the clip to the attack line them up. The Animation panel places marks. Marks play as the clip reaches them:
| Mark | Does |
|---|---|
hit | the type's blow sound (EntitySounds.hit) where the screen lands it |
foot | a footstep (EntitySounds.foot, or SoundSet.footstep), units in view only |
fx:<name> | the game's effect on the unit |
sound:<name> | a sound at the unit |
| any | told to UnitLook.onMark(entity, { name, clip }) (return true to handle it) |
Clips of another rig come in by Import clips. Blends between clips are ModelDef.blends.
A clip on a part of the body
A clip plays on a part of the body and the unit keeps doing the rest: a swing on the upper body over the walk, the legs still walking. ClipDef.mask says which part (ClipMask): 'upper' is the spine and everything that hangs from it (the chest, the arms, the head, what the hands hold), 'lower' everything else (the hips, the legs, the root), or bones by name, each with what hangs from it, spelled as the model spells them or as another tool does (Spine, mixamorig:Spine and spine.001 are one). The Animation panel's Plays on sets it and previews it over the walk.
model: { src: 'characters/knight.glb', scale: 1, clips: { Melee_1H_Attack_Chop: { mask: 'upper' } } }
// in code, for this one playing: the clip's own mask when left out, null for the whole body
client.playClip(id, 'Wave', { loop: true, mask: 'upper' })
client.playClip(id, 'Melee_1H_Attack_Chop', { mask: ['UpperArm_R'] })The clip is layered over the loop by the units' animator: its bones alone, written after the loop's, blended in over blends and melting back into the loop when it ends. An attack on the upper body is not cut short by walking (it is what the mask is for), and the unit's clips on a rig retargeted from another file (animationFiles) are masked by the model's bones all the same. A unit drawn among the copies of its crowd plays the clip whole: layering needs a skeleton of its own, which a clip of the game's (client.playClip) gives it, and an attack far away does not. A death takes the whole body whatever its clip says.