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';
exportclassTextureDropInstantiatorextendsAssetInstantiator { staticdescriptor(): Descriptor { constd = newDescriptor(); 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'); returnd; }
asyncprepareDependencies( _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. constabsPath = newEditor.Path(import.meta.resolve('plane.gltf')); constdest = newEditor.Model.SourcePath( newEditor.Path('Meshes'), Editor.Model.SourceRootDirectory.Assets, ); letmeta = manager.findImportedCopy(absPath); if (!meta) { constresult = awaitmanager.importExternalFileAsync( absPath, dest, Editor.Model.ResultType.Packed, ); meta = result.files[0]; } constimported = (meta?.assets ?? []) asEditor.Assets.Asset[]; returnimported.filter(a=>a.isOfType('RenderMesh') || a.isOfType('Material')); }
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) { constpassInfo = mat.passInfos[0] asunknownas { baseTex: Editor.Assets.TextureParameter }; passInfo.baseTex = newEditor.Assets.TextureParameter((assetasEditor.Model.Entity).id); } } return [objasunknownasEditor.Model.Prefabable]; } }
Module providing asset instantiation capabilities for Lens Studio plugins.
Example