@khronosgroup/gltf-interactivity-engine
v1.2.1
Published
Engine of the Khronos Interactivity Graph Authoring Tool
Readme
KHR_interactivity Typescript Engine
This README provides an overview and instructions for connecting the BasicBehaveEngine to a frontend (e.g. DCC tool, renderer).
Table of Contents
- KHR_interactivity Typescript Engine
Introduction
This engine implements the glTF KHR_interactivity extension. The KHR_interactivity extension allows you to add interactive features and behaviors to your 3D models, enhancing the user experience in 3D applications.
NPM package
Add this project as a npm package to your project via
npm i @khronosgroup/gltf-interactivity-engine
Integration
Registering Pointer mappings
The KHR_interactivity spec allows for setting and getting properties of an object model, this logic of mapping an object path to a setter and getter is handled by the respective decorator (so a logging engine can set a path in a different way than a Babylon one to help with implementation specific mapping). To add your own pointer, simply add the logic for your getter and setter to the registerKnownPointers function in the respective Decorators.
Loading a graph
A graph can be loaded into the engine by calling:
loadBehaveGraph(behaveGraph: any, run = true)behaveGraph is the JSON representation of the graph. run can be set to false, if the graph should not be executed automatically after loading.
The event queue can be pause and played by calling:
pauseEventQueue()
playEventQueue()Be aware that you need to pause/play e.g. animations and hover in your renderer.
Resetting a graph
Resetting the engine needs to be done manually. An empty graph can be loaded and there are several clear functions for different purposes.
Additionally, you need to manually reset the animation state and the whole glTF state which was modified by pointer/set nodes.
Events
To sent custom events to the engine call:
dispatchCustomEvent(name: string, vals: any)The name needs to be prefixed with KHR_INTERACTIVITY:.
Vals is a JSON object containing all values needed for the event.
Animations
To implement animations the following nodes need to be registered like this:
registerBehaveEngineNode("animation/stop", interactivity.AnimationStop);
registerBehaveEngineNode("animation/start", interactivity.AnimationStart);
registerBehaveEngineNode("animation/stopAt", interactivity.AnimationStopAt);Additionally, the following functions need to be added to the BasicBehaveEngine:
this.behaveEngine.stopAnimation = (animationIndex: number): void => {
//TODO
};
this.behaveEngine.stopAnimationAt = (animationIndex: number, stopTime: number , callback: () => void): void => {
//TODO
};
this.behaveEngine.startAnimation = (animationIndex: number, startTime: number, endTime: number, speed: number, callback: () => void): void => {
//TODO
};For implementation details, check out the KHR_interactivity specification.
KHR_selectability and KHR_hoverability
To implement KHR_selectability and KHR_hoverability the following nodes need to be registered like this:
registerBehaveEngineNode("event/onSelect", interactivity.OnSelect);
registerBehaveEngineNode("event/onHoverIn", interactivity.OnHoverIn);
registerBehaveEngineNode("event/onHoverOut", interactivity.OnHoverOut);Additionally, the following function need to be added to the BasicBehaveEngine:
this.behaveEngine.getParentNodeIndex = (nodeIndex: number) => number | undefined => {
// Return index of parent node. If no parent exists return undefined.
}You can pass a selection event via:
select(selectedNodeIndex: number, controllerIndex: number, selectionPoint: [number, number, number] | undefined, selectionRayOrigin: [number, number, number] | undefined);selectionPoint and selectionRayOrigin are optional.
You can pass a hover event via:
hoverOn(hoveredNodeIndex: number | undefined, controllerIndex: number);hoveredNodeIndex can be undefined if nothing is hover over.
KHR_physics_rigid_bodies
To implement KHR_physics_rigid_bodies register the nodes using:
this.registerRigidBodyNodes();You can pass a trigger enter and exit events via:
rigidBodyTriggerEntered(nodeIndex: number, colliderNodeIndex: number, motionNodeIndex: number | undefined);
rigidBodyTriggerExited(nodeIndex: number, colliderNodeIndex: number, motionNodeIndex: number | undefined);nodeIndex describes the node index of the trigger, colliderNodeIndex describes the node index of the collider and motionNodeIndex describes the index of the motion which might be attached to the collider or one of it parents. This should be called with undefined, if no motion is found (in case of a static collider).
The following functions need to be implemented and need to overwrite their counterparts in the behave engine:
applyImpulseToRigidBody(nodeIndex: number, linearImpulse: [number, number, number], angularImpulse: [number, number, number])
applyPointImpulseToRigidBody(nodeIndex: number, impulse: [number, number, number], position: [number, number, number])
rayCastRigidBodies(rayStart: [number, number, number], rayEnd: [number, number, number], collisionFilterIndex: number): {hitNodeIndex: number, hitFraction: number | undefined, hitNormal: [number, number, number] | undefined}
this.behaveEngine.applyImpulseToRigidBody = this.applyImpulseToRigidBody;
this.behaveEngine.applyPointImpulseToRigidBody = this.applyPointImpulseToRigidBody;
this.behaveEngine.rayCastRigidBodies = this.rayCastRigidBodies;If a ray cast result in a miss, {hitNodeIndex: -1} should be returned.
The full interactivity node definitions and explanations can be read in the spec.
Debugging
The following functions of the BasicBehaveEngine can be used to perform debugging/logging etc.
this.behaveEngine.processNodeStarted = (behaveEngineNode: BehaveEngineNode) => void {
};
this.behaveEngine.processAddingNodeToQueue = (flow: IInteractivityFlow) => void {
};
this.behaveEngine.processExecutingNextNode = (flow: IInteractivityFlow) => void {
};Important Notes About Tool Execution
- The tool passes around JSON for the execution graph. Be aware that the engine does not necessarily create deep copies of arrays or objects. Modifying these from outside the engine might create undefined/unexpected behavior. Make sure to not modify passed values or create deep copies.
- Matrix values are flat arrays in column-major order. Object-model pointer getters and setters must use the same representation.
Development
Prerequisites
- Install Node.js. Please use an LTS version of Node.js.
Installation
Install the required dependencies:
npm install
Building
Run
npm run build
The build emits one ESM entry point, source map and declarations into build/. npm pack rebuilds first. Runtime validation is exported as validateInteractivityExtension and validateInteractivityGraph; the glTF loader integration invokes it before loading model dependencies. Non-default graphs are deliberately not validated until selected.
Release verification
From src/BasicBehaveEngine, run npm ci and npm run test:package. The check builds and packs the package, installs the exact tarball in an isolated consumer, imports the public API in Node, executes a graph, checks declarations with NodeNext resolution, and runs npm publish --dry-run. It prints the retained artifact path and checksum.
The package contains the renderer-independent engine. Three.js and Needle adapters use three-gltf-interactivity (including its /needle import) and depend on this package. Publish the core package before publishing an adapter version that requires it.
Evaluation
Graph loading pre-resolves standard scalar/vector/matrix value edges and common synchronous control nodes into cached closures. It uses no generated source or eval. Custom operation registrations retain their normal processors. Constant branch trees can use direct decision tables; attaching execution/debug hooks restores the per-node control path. Value caches invalidate between flow activations, and clearValueEvaluationCache() also invalidates the compiled cells after external state changes.
Adding Nodes to BasicBehaveEngine
The provided Basic BehaveEngine uses a decorator approach, so you behave nodes will be defined only once and the Logging and Babylon decorators will decorate certain touch points in the execution logic with their specific functionality. To create a new node...
- add a new class in the nodes directory which extends BehaveEngineNode.
- add your REQUIRED_VALUES and/or REQUIRED_CONFIGURATIONS arrays which will be used to validate the node.
- In your constructor, run super() then set the instance name and finally run the needed validations (flows, values and configurations) for your node
- override processNode, if your node has custom named out flows (i.e. in branch we have true and false instead of out) use this.processFlow, if not you can either do this.processFlow on the default out flow or run super.processNode
- Finally, either in BasicBehaveEngine or in the decorator of your choice, call registerBehaveEngineNode with the namespace of your node and the node class name
- If you want to do some complex behaviors, like async listeners look at CustomEvent/Receive it has a good example of how async nodes post their subsequent flows to the execution queue instead of invoking synchronously and shows how to set up an event listener so it is not triggered by another node but an event.
TODO
- customEvent default values for event values
- configuration default values and validation
- expand semantic validation beyond the current valid/invalid test corpus
- handle graphs with different types in authoring system => do not override types array or assume it is always the standard one
- unit test to ensure graph values are not overwritten when loaded into authoring view for all nodes (especially ones with configurations)
Non normative implementations
- writing mesh material with pointer path (will be replaced in the future when KHR_materials_variants is specified) > "/meshes/${nodeIndex}/primitives/${primitiveIndex}/material""
