playcanvas
Version:
PlayCanvas WebGL game engine
77 lines (74 loc) • 2.4 kB
JavaScript
import { EventHandler } from '../core/event-handler.js';
/**
* @import { Mesh } from './mesh.js'
*/ /**
* A `Render` contains an array of meshes that are referenced by a single hierarchy node in a GLB
* scene, and are accessible using the {@link ContainerResource#renders} property. A `Render` is
* the resource of a Render Asset. They are usually created by the GLB loader and not created by
* hand.
*/ class Render extends EventHandler {
/**
* Sets the meshes that the render contains.
*
* @type {Array<Mesh|null>|null}
*/ set meshes(value) {
// decrement references on the existing meshes
this.decRefMeshes();
// assign new meshes
this._meshes = value;
this.incRefMeshes();
this.fire('set:meshes', value);
}
/**
* Gets the meshes that the render contains.
*
* @type {Array<Mesh|null>|null}
*/ get meshes() {
return this._meshes;
}
destroy() {
this.meshes = null;
}
/**
* Decrement references to meshes. Destroy the ones with zero references.
*/ decRefMeshes() {
var _this__meshes;
(_this__meshes = this._meshes) == null ? undefined : _this__meshes.forEach((mesh, index)=>{
if (mesh) {
mesh.decRefCount();
if (mesh.refCount < 1) {
mesh.destroy();
this._meshes[index] = null;
}
}
});
}
/**
* Increments ref count on all meshes.
*/ incRefMeshes() {
var _this__meshes;
(_this__meshes = this._meshes) == null ? undefined : _this__meshes.forEach((mesh)=>{
mesh == null ? undefined : mesh.incRefCount();
});
}
constructor(...args){
super(...args), /**
* Meshes are reference counted, and this class owns the references and is responsible for
* releasing the meshes when they are no longer referenced.
*
* @type {Array<Mesh|null>|null}
* @private
*/ this._meshes = null;
}
}
/**
* Fired when the meshes are set on the render. The handler is passed the an array of
* {@link Mesh} objects.
*
* @event
* @example
* render.on('set:meshes', (meshes) => {
* console.log(`Render has ${meshes.length} meshes`);
* });
*/ Render.EVENT_SETMESHES = 'set:meshes';
export { Render };