Skip to content

⬅️ Back to Table of Contents

📄 OculusHandModel.js

📊 Analysis Summary

Metric Count
🔧 Functions 4
🧱 Classes 1
📦 Imports 4
📊 Variables & Constants 5

📚 Table of Contents

🛠️ File Location:

📂 examples/jsm/webxr/OculusHandModel.js

📦 Imports

Name Source
Object3D three
Sphere three
Box3 three
XRHandMeshModel ./XRHandMeshModel.js

Variables & Constants

Name Type Kind Value Exported
TOUCH_RADIUS 0.01 let/var 0.01
POINTING_JOINT "index-finger-tip" let/var 'index-finger-tip'
xrInputSource any let/var event.data
indexFingerTip any let/var this.controller.joints[ POINTING_JOINT ]
indexSphere any let/var new Sphere( pointerPosition, TOUCH_RADIUS )

Functions

OculusHandModel.updateMatrixWorld(force: boolean): void

JSDoc:

/**
     * Overwritten with a custom implementation. Makes sure the motion controller updates the mesh.
     *
     * @param {boolean} [force=false] - When set to `true`, a recomputation of world matrices is forced even
     * when {@link Object3D#matrixWorldAutoUpdate} is set to `false`.
     */

Parameters:

  • force boolean

Returns: void

Calls:

  • super.updateMatrixWorld
  • this.motionController.updateMesh
Code
updateMatrixWorld( force ) {

        super.updateMatrixWorld( force );

        if ( this.motionController ) {

            this.motionController.updateMesh();

        }

    }

OculusHandModel.getPointerPosition(): any

JSDoc:

/**
     * Returns the pointer position which is the position of the index finger tip.
     *
     * @return {Vector3|null} The pointer position. Returns `null` if not index finger tip joint was found.
     */

Returns: any

Code
getPointerPosition() {

        const indexFingerTip = this.controller.joints[ POINTING_JOINT ];
        if ( indexFingerTip ) {

            return indexFingerTip.position;

        } else {

            return null;

        }

    }

OculusHandModel.intersectBoxObject(boxObject: Mesh): boolean

JSDoc:

/**
     * Returns `true` if the current pointer position (the index finger tip) intersections
     * with the given box object.
     *
     *  @param {Mesh} boxObject - The box object.
     * @return {boolean} Whether an intersection was found or not.
     */

Parameters:

  • boxObject Mesh

Returns: boolean

Calls:

  • this.getPointerPosition
  • new Box3().setFromObject
  • indexSphere.intersectsBox
Code
intersectBoxObject( boxObject ) {

        const pointerPosition = this.getPointerPosition();
        if ( pointerPosition ) {

            const indexSphere = new Sphere( pointerPosition, TOUCH_RADIUS );
            const box = new Box3().setFromObject( boxObject );
            return indexSphere.intersectsBox( box );

        } else {

            return false;

        }

    }

OculusHandModel.checkButton(button: any): void

JSDoc:

/**
     * Executed actions depending on the interaction state with
     * the given button.
     *
     *  @param {Object} button - The button.
     */

Parameters:

  • button any

Returns: void

Calls:

  • this.intersectBoxObject
  • button.onPress
  • button.onClear
  • button.isPressed
  • button.whilePressed
Code
checkButton( button ) {

        if ( this.intersectBoxObject( button ) ) {

            button.onPress();

        } else {

            button.onClear();

        }

        if ( button.isPressed() ) {

            button.whilePressed();

        }

    }

Classes

OculusHandModel

Class Code
class OculusHandModel extends Object3D {

    /**
     * Constructs a new Oculus hand model.
     *
     * @param {Group} controller - The hand controller.
     * @param {?Loader} [loader=null] - A loader that is used to load hand models.
     * @param {?Function} [onLoad=null] - A callback that is executed when a hand model has been loaded.
     */
    constructor( controller, loader = null, onLoad = null ) {

        super();

        /**
         * The hand controller.
         *
         * @type {Group}
         */
        this.controller = controller;

        /**
         * The motion controller.
         *
         * @type {?MotionController}
         * @default null
         */
        this.motionController = null;

        /**
         * The model's environment map.
         *
         * @type {?Texture}
         * @default null
         */
        this.envMap = null;

        /**
         * A loader that is used to load hand models.
         *
         * @type {?Loader}
         * @default null
         */
        this.loader = loader;

        /**
         * A callback that is executed when a hand model has been loaded.
         *
         * @type {?Function}
         * @default null
         */
        this.onLoad = onLoad;

        /**
         * The path to the model repository.
         *
         * @type {?string}
         * @default null
         */
        this.path = null;

        /**
         * The model mesh.
         *
         * @type {Mesh}
         * @default null
         */
        this.mesh = null;

        controller.addEventListener( 'connected', ( event ) => {

            const xrInputSource = event.data;

            if ( xrInputSource.hand && ! this.motionController ) {

                this.xrInputSource = xrInputSource;

                this.motionController = new XRHandMeshModel( this, controller, this.path, xrInputSource.handedness, this.loader, this.onLoad );

            }

        } );

        controller.addEventListener( 'disconnected', () => {

            this.clear();
            this.motionController = null;

        } );

    }

    /**
     * Overwritten with a custom implementation. Makes sure the motion controller updates the mesh.
     *
     * @param {boolean} [force=false] - When set to `true`, a recomputation of world matrices is forced even
     * when {@link Object3D#matrixWorldAutoUpdate} is set to `false`.
     */
    updateMatrixWorld( force ) {

        super.updateMatrixWorld( force );

        if ( this.motionController ) {

            this.motionController.updateMesh();

        }

    }

    /**
     * Returns the pointer position which is the position of the index finger tip.
     *
     * @return {Vector3|null} The pointer position. Returns `null` if not index finger tip joint was found.
     */
    getPointerPosition() {

        const indexFingerTip = this.controller.joints[ POINTING_JOINT ];
        if ( indexFingerTip ) {

            return indexFingerTip.position;

        } else {

            return null;

        }

    }

    /**
     * Returns `true` if the current pointer position (the index finger tip) intersections
     * with the given box object.
     *
     *  @param {Mesh} boxObject - The box object.
     * @return {boolean} Whether an intersection was found or not.
     */
    intersectBoxObject( boxObject ) {

        const pointerPosition = this.getPointerPosition();
        if ( pointerPosition ) {

            const indexSphere = new Sphere( pointerPosition, TOUCH_RADIUS );
            const box = new Box3().setFromObject( boxObject );
            return indexSphere.intersectsBox( box );

        } else {

            return false;

        }

    }

    /**
     * Executed actions depending on the interaction state with
     * the given button.
     *
     *  @param {Object} button - The button.
     */
    checkButton( button ) {

        if ( this.intersectBoxObject( button ) ) {

            button.onPress();

        } else {

            button.onClear();

        }

        if ( button.isPressed() ) {

            button.whilePressed();

        }

    }

}

Methods

updateMatrixWorld(force: boolean): void
Code
updateMatrixWorld( force ) {

        super.updateMatrixWorld( force );

        if ( this.motionController ) {

            this.motionController.updateMesh();

        }

    }
getPointerPosition(): any
Code
getPointerPosition() {

        const indexFingerTip = this.controller.joints[ POINTING_JOINT ];
        if ( indexFingerTip ) {

            return indexFingerTip.position;

        } else {

            return null;

        }

    }
intersectBoxObject(boxObject: Mesh): boolean
Code
intersectBoxObject( boxObject ) {

        const pointerPosition = this.getPointerPosition();
        if ( pointerPosition ) {

            const indexSphere = new Sphere( pointerPosition, TOUCH_RADIUS );
            const box = new Box3().setFromObject( boxObject );
            return indexSphere.intersectsBox( box );

        } else {

            return false;

        }

    }
checkButton(button: any): void
Code
checkButton( button ) {

        if ( this.intersectBoxObject( button ) ) {

            button.onPress();

        } else {

            button.onClear();

        }

        if ( button.isPressed() ) {

            button.whilePressed();

        }

    }