playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
177 lines (176 loc) • 6.55 kB
JavaScript
import { path } from "../../core/path.js";
import { GlbContainerParser } from "../parsers/glb-container-parser.js";
import { ResourceHandler } from "./handler.js";
class ContainerResource {
/**
* Instantiates an entity with a model component.
*
* @param {object} [options] - The initialization data for the model component type
* {@link ModelComponent}.
* @returns {Entity} A single entity with a model component. Model component internally
* contains a hierarchy based on {@link GraphNode}.
* @example
* // load a glb file and instantiate an entity with a model component based on it
* app.assets.loadFromUrl("statue.glb", "container", (err, asset) => {
* const entity = asset.resource.instantiateModelEntity({
* castShadows: true
* });
* app.root.addChild(entity);
* });
*/
instantiateModelEntity(options) {
return null;
}
/**
* Instantiates an entity with a render component.
*
* @param {object} [options] - The initialization data for the render component type
* {@link RenderComponent}.
* @returns {Entity} A hierarchy of entities with render components on entities containing
* renderable geometry.
* @example
* // load a glb file and instantiate an entity with a render component based on it
* app.assets.loadFromUrl("statue.glb", "container", (err, asset) => {
* const entity = asset.resource.instantiateRenderEntity({
* castShadows: true
* });
* app.root.addChild(entity);
*
* // find all render components containing mesh instances, and change blend mode on their materials
* const renders = entity.findComponents("render");
* renders.forEach((render) => {
* render.meshInstances.forEach((meshInstance) => {
* meshInstance.material.blendType = pc.BLEND_MULTIPLICATIVE;
* meshInstance.material.update();
* });
* });
* });
*/
instantiateRenderEntity(options) {
return null;
}
/**
* Queries the list of available material variants.
*
* @returns {string[]} An array of variant names.
*/
getMaterialVariants() {
return null;
}
/**
* Applies a material variant to an entity hierarchy.
*
* @param {Entity} entity - The entity root to which material variants will be applied.
* @param {string} [name] - The name of the variant, as queried from getMaterialVariants, if
* null the variant will be reset to the default.
* @example
* // load a glb file and instantiate an entity with a render component based on it
* app.assets.loadFromUrl("statue.glb", "container", (err, asset) => {
* const entity = asset.resource.instantiateRenderEntity({
* castShadows: true
* });
* app.root.addChild(entity);
* const materialVariants = asset.resource.getMaterialVariants();
* asset.resource.applyMaterialVariant(entity, materialVariants[0]);
* });
*/
applyMaterialVariant(entity, name) {
}
/**
* Applies a material variant to a set of mesh instances. Compared to the applyMaterialVariant,
* this method allows for setting the variant on a specific set of mesh instances instead of the
* whole entity.
*
* @param {MeshInstance[]} instances - An array of mesh instances.
* @param {string} [name] - The name of the variant, as queried by getMaterialVariants. If null,
* the variant will be reset to the default.
* @example
* // load a glb file and instantiate an entity with a render component based on it
* app.assets.loadFromUrl("statue.glb", "container", (err, asset) => {
* const entity = asset.resource.instantiateRenderEntity({
* castShadows: true
* });
* app.root.addChild(entity);
* const materialVariants = asset.resource.getMaterialVariants();
* const renders = entity.findComponents("render");
* for (let i = 0; i < renders.length; i++) {
* const renderComponent = renders[i];
* asset.resource.applyMaterialVariantInstances(renderComponent.meshInstances, materialVariants[0]);
* }
* });
*/
applyMaterialVariantInstances(instances, name) {
}
}
class ContainerHandler extends ResourceHandler {
/**
* Create a new ContainerResource instance.
*
* @param {AppBase} app - The running {@link AppBase}.
* @ignore
*/
constructor(app) {
super(app, "container");
this.glbContainerParser = new GlbContainerParser(app.graphicsDevice, app.assets, 0);
this.parsers = {};
}
set maxRetries(value) {
this.glbContainerParser.maxRetries = value;
for (const parser in this.parsers) {
if (this.parsers.hasOwnProperty(parser)) {
this.parsers[parser].maxRetries = value;
}
}
}
get maxRetries() {
return this.glbContainerParser.maxRetries;
}
/**
* @param {string} url - The resource URL.
* @returns {string} The URL with query parameters removed.
* @private
*/
_getUrlWithoutParams(url) {
return url.indexOf("?") >= 0 ? url.split("?")[0] : url;
}
/**
* @param {string} url - The resource URL.
* @returns {*} A suitable parser to parse the resource.
* @private
*/
_getParser(url) {
const ext = url ? path.getExtension(this._getUrlWithoutParams(url)).toLowerCase().replace(".", "") : null;
return this.parsers[ext] || this.glbContainerParser;
}
/**
* @param {string | {load: string, original: string}} url - Either the URL of the resource to
* load or a structure containing the load URL (used for loading the resource) and the original
* URL (used for identifying the resource format; necessary when loading, for example, from
* a blob URL).
* @param {ResourceHandlerCallback} callback - The callback used when the resource is loaded or
* an error occurs.
* @param {Asset} [asset] - Optional asset that is passed by ResourceLoader.
*/
load(url, callback, asset) {
if (typeof url === "string") {
url = {
load: url,
original: url
};
}
this._getParser(url.original).load(url, callback, asset);
}
/**
* @param {string} url - The URL of the resource to open.
* @param {*} data - The raw resource data passed by callback from {@link ResourceHandler#load}.
* @param {Asset} [asset] - Optional asset that is passed by ResourceLoader.
* @returns {*} The parsed resource data.
*/
open(url, data, asset) {
return this._getParser(url).open(url, data, asset);
}
}
export {
ContainerHandler,
ContainerResource
};