Skip to main content

LensStudio:AssetInstantiator

Module

Module providing asset instantiation capabilities for Lens Studio plugins.

Example

// LensStudio:AssetInstantiator wires an asset into the scene as a SceneObject.
// When the user drops a Texture into the scene/hierarchy, Lens Studio finds
// an instantiator whose canInstantiate returns true, calls prepareDependencies
// to import any peer assets the result will need, then calls instantiate to
// produce the scene objects under target.
//
// This example handles Texture drops. prepareDependencies imports a plane
// mesh + PBR material bundled with the plugin (so the material's pass is
// populated with a real shader), and instantiate creates a SceneObject with
// a RenderMeshVisual that uses them — with the dropped texture wired into
// the material's first pass.
import { AssetInstantiator, Descriptor } from 'LensStudio:AssetInstantiator';

export class TextureDropInstantiator extends AssetInstantiator {
static descriptor(): Descriptor {
const d = new Descriptor();
d.id = 'com.docs.AssetInstantiatorExample';
d.name = 'Texture Drop Instantiator';
d.description = 'Drops a Texture into the scene as a textured plane';
d.dependencies = [Editor.Model.IModel];
d.canInstantiate = (asset: Editor.Assets.Asset) => asset.isOfType('Texture');
return d;
}

async prepareDependencies(
_asset: Editor.Assets.Asset,
manager: Editor.Model.AssetManager,
): Promise<Editor.Assets.Asset[]> {
// Resolve the bundled plane.gltf relative to this plugin's module —
// the same pattern preset-style plugins use to ship resources.
const absPath = new Editor.Path(import.meta.resolve('plane.gltf'));
const dest = new Editor.Model.SourcePath(
new Editor.Path('Meshes'),
Editor.Model.SourceRootDirectory.Assets,
);
let meta = manager.findImportedCopy(absPath);
if (!meta) {
const result = await manager.importExternalFileAsync(
absPath, dest, Editor.Model.ResultType.Packed,
);
meta = result.files[0];
}
const imported = (meta?.assets ?? []) as Editor.Assets.Asset[];
return imported.filter(a => a.isOfType('RenderMesh') || a.isOfType('Material'));
}

instantiate(
asset: Editor.Assets.Asset,
scene: Editor.Assets.ObjectOwner,
target: Editor.Model.SceneObject,
): Editor.Model.Prefabable[] {
const obj = scene.addSceneObject(target);
obj.name = `${asset.name}_visual`;
const visual = obj.addComponent('RenderMeshVisual');

const model = this.pluginSystem.findInterface(Editor.Model.IModel) as Editor.Model.IModel;
const absPath = new Editor.Path(import.meta.resolve('plane.gltf'));
const meta = model.project.assetManager.findImportedCopy(absPath);
const imported = (meta?.assets ?? []) as Editor.Assets.Asset[];
const mesh = imported.find(a => a.isOfType('RenderMesh')) as Editor.Assets.RenderMesh | undefined;
const mat = imported.find(a => a.isOfType('Material')) as Editor.Assets.Material | undefined;

if (mesh) visual.mesh = mesh;
if (mat) {
visual.addMaterialAt(mat);
// Bind the dropped texture into the material's first pass. The
// binding slot is shader-specific — 'baseTex' is the common slot for
// Lens Studio's flat / PBR passes.
if (mat.passInfos.length > 0) {
const passInfo = mat.passInfos[0] as unknown as { baseTex: Editor.Assets.TextureParameter };
passInfo.baseTex = new Editor.Assets.TextureParameter((asset as Editor.Model.Entity).id);
}
}
return [obj as unknown as Editor.Model.Prefabable];
}
}

Classes

ClassDescription
AssetInstantiatorPlugin class that manages asset instantiation, initialized with a PluginSystem and optional Descriptor.
DescriptorConfiguration descriptor for the AssetInstantiator plugin, extending BaseDescriptor.