# graphics3d/skinnedmodelasset (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/graphics3d/skinnedmodelasset/

Skinned model asset. (decoded data and GPU resources shared by the model instances built from the same file)

## SkinnedModelAsset extends Object

Skinned model asset. (decoded data and GPU resources shared by the model instances built from the same file)
- Holds one copy of the mesh buffers and vertex arrays, the material prototypes and their textures, the morph textures, the node and skin prototypes and the animation clips.
  (the same as clones sharing Unity's mesh and sharedMaterial, or three.js BufferGeometry and Material)
- Instances (SkinnedModel) share it by reference count. Each instance keeps its own pose, animation playback state, morph weights,
  forked materials (getMaterial), wireframe buffers and morph textures forked by addMorphTarget.
- When the last instance calls destroy (reference count 0), the GPU resources are deleted and the asset leaves the cache.
- The cache is looked up by shader program and asset key (a path or asset name normalized with normalizeKey). Loads such as SkinnedModel.loadFromUrl use it.

Example: Loading the same GLB twice shares one asset

```js
import { SkinnedModel, SkinnedModelAsset, SkinnedModelRenderer } from "./build/vanilla.js";

const canvas = document.createElement("canvas");
const webGL2RenderingContext = canvas.getContext("webgl2");
const skinnedModelRenderer = new SkinnedModelRenderer(webGL2RenderingContext);
const firstModel = await SkinnedModel.loadFromUrl(webGL2RenderingContext, "examples/ancientmountain/assets/fox.glb", skinnedModelRenderer);
const secondModel = await SkinnedModel.loadFromUrl(webGL2RenderingContext, "./examples/ancientmountain/assets/fox.glb", skinnedModelRenderer);
const skinnedModelAsset = firstModel.getAsset();
console.log(skinnedModelAsset === secondModel.getAsset(), skinnedModelAsset.getReferenceCount(), skinnedModelAsset.getKey());
firstModel.destroy();
secondModel.destroy();
console.log(skinnedModelAsset.getReferenceCount(), skinnedModelAsset.isDestroyed(), SkinnedModelAsset.getCachedCount());
```

- `constructor(webGL2RenderingContext, shaderProgram, content)` (constructor, since 0.4.0): Constructor. (built by SkinnedModel.uploadMeshesAndMaterials after uploading, reference count 1)
  - `webGL2RenderingContext` WebGL2RenderingContext: Rendering context the resources were created with.
  - `shaderProgram` ShaderProgram: Shader program used by the vertex arrays and materials.
  - `content` object: Data to share. ({ drawableList, materialList, nodeList, rootNodeIndices, skinList, animationList, bufferByteCount }, drawables stay as prototypes that instances copy)
- `static normalizeKey(path)` (static, since 0.4.0): Normalize an asset key. (static: backslashes become /, "." and ".." segments are resolved and empty segments dropped; a leading "scheme://" and a trailing ? or # part stay as they are)
  - The same file written differently, such as "./a/b.glb" and "a/c/../b.glb", gets the same key. It does not resolve against a base URL into an absolute one.
  - `path` string: Asset path or name.
  - returns string: Normalized key.

  Example: The same path written differently

  ```js
  import { SkinnedModelAsset } from "./build/vanilla.js";
  
  console.log(SkinnedModelAsset.normalizeKey("./assets/models/../fox.glb"));
  console.log(SkinnedModelAsset.normalizeKey("assets\\fox.glb"));
  console.log(SkinnedModelAsset.normalizeKey("https://example.com/a/./b/../fox.glb?v=2"));
  ```

- `static find(assetKey, shaderProgram)` (static, since 0.4.0): Find in the cache. (static, a live asset with the same shader program and key)
  - `assetKey` string: Asset key. (normalized with normalizeKey)
  - `shaderProgram` ShaderProgram: Shader program the model is drawn with. (the renderer's getShaderProgram)
  - returns SkinnedModelAsset | null: Asset found. (null if none)
- `static register(assetKey, asset)` (static, since 0.4.0): Add to the cache. (static, gives the asset the key. An asset that held the same slot only leaves the cache, and instances using it keep working)
  - `assetKey` string: Asset key. (normalized with normalizeKey)
  - `asset` SkinnedModelAsset: Asset to add. (throws if the asset was destroyed)

  Example: Share a model built from bundled bytes by name

  ```js
  import { SkinnedModel, SkinnedModelAsset, SkinnedModelRenderer } from "./build/vanilla.js";
  
  const canvas = document.createElement("canvas");
  const webGL2RenderingContext = canvas.getContext("webgl2");
  const skinnedModelRenderer = new SkinnedModelRenderer(webGL2RenderingContext);
  const response = await fetch("examples/ancientmountain/assets/fox.glb");
  const arrayBuffer = await response.arrayBuffer();
  const skinnedModel = await SkinnedModel.loadFromBytes(webGL2RenderingContext, arrayBuffer, skinnedModelRenderer);
  SkinnedModelAsset.register("fox", skinnedModel.getAsset());
  const shaderProgram = skinnedModelRenderer.getShaderProgram();
  const foundAsset = SkinnedModelAsset.find("fox", shaderProgram);
  const otherModel = SkinnedModel.createFromAsset(foundAsset);
  console.log(foundAsset.getReferenceCount(), otherModel.getDrawableList().length);
  otherModel.destroy();
  skinnedModel.destroy();
  ```

- `static clearCache()` (static, since 0.4.0): Clear the cache. (static, removes every asset from the cache. Instances in use keep drawing, and the GPU resources are deleted when the last instance calls destroy)
  - The next load parses the file again into a new asset.

  Example: Clear the cache when switching stages

  ```js
  import { SkinnedModelAsset } from "./build/vanilla.js";
  
  SkinnedModelAsset.clearCache();
  console.log(SkinnedModelAsset.getCachedCount());
  ```

- `static getCachedCount()` (static, since 0.4.0): Number of assets in the cache. (static, sum over all shader programs)
  - returns number: Number of assets in the cache.
- `retain()` (method, since 0.4.0): Add a reference. (when an instance starts sharing the asset, called by SkinnedModel.createFromAsset)
- `release()` (method, since 0.4.0): Remove a reference. (when an instance calls destroy; at 0 the GPU resources are deleted and the asset leaves the cache)
  - returns boolean: True if this call deleted the resources.
- `getKey()` (method, since 0.4.0): Return the asset key.
  - returns string | null: Key added to the cache. (null if never added or removed from the cache)
- `getReferenceCount()` (method, since 0.4.0): Return the reference count. (number of live instances sharing this asset)
  - returns number: Reference count. (0 once destroyed)
- `isDestroyed()` (method, since 0.4.0): Whether destroyed.
  - returns boolean: True if the last instance called destroy and the GPU resources were deleted.
- `getGpuByteCount()` (method, since 0.4.0): GPU byte count. (vertex and index buffers, morph textures and material prototype textures including mipmaps, one copy regardless of the instance count)
  - returns number: Bytes this asset uploaded to the GPU. (0 once destroyed)
- `getWebGL2RenderingContext()` (method, since 0.4.0): Returns the rendering context.
  - returns WebGL2RenderingContext: Rendering context the resources were created with.
- `getShaderProgram()` (method, since 0.4.0): Returns the shader program.
  - returns ShaderProgram: Shader program used by the vertex arrays and materials.
- `getDrawableList()` (method, since 0.4.0): Return the prototype drawable list. (instances copy it to share the buffers and material prototypes; do not change it)
  - returns object[]: Prototype drawable list.
- `getMaterialList()` (method, since 0.4.0): Return the material prototype list. (the default material is last; this asset created them and deletes them when the last instance calls destroy)
  - returns Material[]: Material prototype list. (sharedMaterial; changing a value changes every instance using this asset)
- `getNodeList()` (method, since 0.4.0): Return the node prototype list. (the nodes at upload; instances copy them and keep their own pose)
  - returns object[]: Node prototype list.
- `getRootNodeIndices()` (method, since 0.4.0): Return the root node list.
  - returns number[]: Root node index list.
- `getSkinList()` (method, since 0.4.0): Return the skin prototype list. (joints and inverse bind matrices are shared, the joint matrix array is per instance)
  - returns object[]: Skin prototype list.
- `getAnimationList()` (method, since 0.4.0): Return the animation clip list. (all instances share the same list; clips added with addAnimationsFromFbx* are visible to all)
  - returns object[]: Animation clip list.
