The context is the main object that holds all the data and state of the Needle Engine.
It can be used to access the scene, renderer, camera, input, physics, networking, and more.

import { Behaviour } from "@needle-tools/engine";
import { Mesh, BoxGeometry, MeshBasicMaterial } from "three";
export class MyScript extends Behaviour {
start() {
console.log("Hello from MyScript");
this.context.scene.add(new Mesh(new BoxGeometry(), new MeshBasicMaterial()));
}
}

Implements

  • IContext

Constructors

Properties

alias: undefined | null | string

An alias for the context

animations: AnimationsRegistry

access animation mixer used by components in the scene

application: Application

access application state (e.g. if all audio should be muted)

AssetDataBase is deprecated

composer: null | EffectComposer | EffectComposer = null

The effect composer can be used to render postprocessing effects. If assigned then it will automatically render the scene every frame.

connection: NetworkConnection

access networking methods (use it to send or listen to messages or join a networking backend)

domElement: HTMLElement

The <needle-engine> web component

hash?: string

used to append to loaded assets

input: Input

access input data (e.g. click or touch events)

isManagedExternally: boolean = false

When the renderer or camera are managed by an external process (e.g. when running in r3f context). When this is false you are responsible to call update(timestamp, xframe.
It is also currently assumed that rendering is handled performed by an external process

isPaused: boolean = false

set to true to pause the update loop. You can receive an event for it in your components. Note that script updates will not be called when paused

mainCameraComponent: undefined | Camera = undefined

The main camera component of the scene - this camera is used for rendering.
Use setCurrentCamera for updating the main camera.

mainLight: null | ILight = null

The main light in the scene

maxRenderResolution?: Vec2

Clamps the renderer max resolution. If undefined the max resolution is not clamped. Default is undefined

menu: NeedleMenu

Access the needle menu to add or remove buttons to the menu element

name: string

The name of the context

physics: Physics

access physics related methods (e.g. raycasting). To access the phyiscs engine use context.physics.engine

post_render_callbacks: Function[] = []

called every frame after rendering (after all component events)

post_setup_callbacks: Function[] = []

callbacks called once after the context has been created

pre_render_callbacks: (frame: null | XRFrame) => void[] = []

called every frame before rendering (after all component events)

pre_update_callbacks: Function[] = []

called every frame at the beginning of the frame (after component start events and before earlyUpdate)

pre_update_oneshot_callbacks: Function[] = []

called every frame befroe update (this list is emptied every frame)

renderer: WebGLRenderer

The renderer is used to render the scene. It is automatically created when the context is created.

runInBackground: boolean = false

When enabled the application will run while not visible on the page

scene: Scene

The scene contains all objects in the hierarchy and is automatically rendered by the context every frane.

targetFrameRate?: number | { value?: number }

Set to the target framerate you want your application to run in (you can use ?stats to check the fps) Set to undefined if you want to run at the maximum framerate

time: Time

access timings (current frame number, deltaTime, timeScale, ...)

xr: null | NeedleXRSession = null

shorthand for NeedleXRSession.active
Automatically set by NeedleXRSession when a XR session is active

the active XR session or null if no session is active

Accessors

  • get arOverlayElement(): HTMLElement

    The AR overlay element is used to display 2D HTML elements while a AR session is active.

    Returns HTMLElement

  • get currentFrameEvent(): FrameEvent

    Current event of the update cycle (e.g. FrameEvent.EarlyUpdate or FrameEvent.OnBeforeRender)

    Returns FrameEvent

  • get depthTexture(): null | DepthTexture

    Returns null | DepthTexture

  • get devicePixelRatio(): number | "auto" | "manual"

    Control the renderer devicePixelRatio.
    Options

    • auto - Needle Engine automatically sets the pixel ratio to the current window.devicePixelRatio.
    • manual - Needle Engine will not change the renderer pixel ratio. You can set it manually.
    • number - Needle Engine will set the pixel ratio to the given number. The change will be applied to the renderer and the composer (if used) at the end of the current frame.

    Returns number | "auto" | "manual"

  • set devicePixelRatio(val: number | "auto" | "manual"): void

    Control the renderer devicePixelRatio.
    Options

    • auto - Needle Engine automatically sets the pixel ratio to the current window.devicePixelRatio.
    • manual - Needle Engine will not change the renderer pixel ratio. You can set it manually.
    • number - Needle Engine will set the pixel ratio to the given number. The change will be applied to the renderer and the composer (if used) at the end of the current frame.

    Parameters

    • val: number | "auto" | "manual"

    Returns void

  • get domHeight(): number

    The height of the <needle-engine> element on the website

    Returns number

  • get domWidth(): number

    The width of the <needle-engine> element on the website

    Returns number

  • get domX(): number

    the X position of the <needle-engine> element on the website

    Returns number

  • get domY(): number

    the Y position of the <needle-engine> element on the website

    Returns number

  • get isCreated(): boolean

    Returns boolean

    true if the context is fully created and ready

  • get isInAR(): boolean

    Shorthand for this.xrSessionMode === "immersive-ar"

    Returns boolean

    true if a webxr AR session is currently active.

  • get isInPassThrough(): boolean

    If a XR session is active and in pass through mode (immersive-ar on e.g. Quest)

    Returns boolean

    true if the XR session is in pass through mode

  • get isInVR(): boolean

    Shorthand for this.xrSessionMode === "immersive-vr"

    Returns boolean

    true if a webxr VR session is currently active.

  • get isInXR(): boolean

    Is a XR session currently active and presenting?

    Returns boolean

    true if the xr renderer is currently presenting

  • get isRendering(): boolean

    Returns boolean

    true while the WebGL renderer is rendering (between onBeforeRender and onAfterRender events)

  • get isVisibleToUser(): boolean

    Returns boolean

    true if the <needle-engine> DOM element is visible on screen (context.domElement)

  • get mainCamera(): Camera

    The main camera of the scene - this camera is used for rendering
    Use setCurrentCamera for updating the main camera.

    Returns Camera

  • set mainCamera(cam: null | Camera): void

    Set the main camera of the scene. If set to null the camera of the mainCameraComponent will be used - this camera is used for rendering

    Parameters

    Returns void

  • get opaqueColorTexture(): null | Texture

    Returns null | Texture

  • get resolutionScaleFactor(): number

    use to scale the resolution up or down of the renderer. default is 1

    Returns number

  • set resolutionScaleFactor(val: number): void

    use to scale the resolution up or down of the renderer. default is 1

    Parameters

    • val: number

    Returns void

  • get version(): string

    The needle engine version

    Returns string

  • get xrCamera(): undefined | WebXRArrayCamera

    Returns undefined | WebXRArrayCamera

    the current WebXR camera while the WebXRManager is active (shorthand for context.renderer.xr.getCamera())

  • get xrSession(): null | XRSession

    access the raw XRSession object (shorthand for context.renderer.xr.getSession()). For more control use NeedleXRSession.active

    Returns null | XRSession

  • get xrSessionMode(): undefined | XRSessionMode

    Shorthand for this.xr?.mode. AR or VR

    Returns undefined | XRSessionMode

    the current XR session mode (immersive-vr or immersive-ar)

  • get Current(): Context

    The currently active context. Only set during the update loops

    Returns Context

  • get DefaultTargetFrameRate(): undefined | number

    When a new context is created this is the framerate that will be used by default

    Returns undefined | number

  • set DefaultTargetFrameRate(val: undefined | number): void

    When a new context is created this is the framerate that will be used by default

    Parameters

    • val: undefined | number

    Returns void

  • get DefaultWebGLRendererParameters(): WebGLRendererParameters

    The default parameters that will be used when creating a new WebGLRenderer.
    Modify in global context to change the default parameters for all new contexts.

    Returns WebGLRendererParameters

    import { Context } from "@needle-tools/engine";
    Context.DefaultWebGLRendererParameters.antialias = false;

Methods

  • Clears the context and destroys all scenes and objects in the scene.
    The ContextCleared event is called at the end.
    This is automatically called when e.g. the src attribute changes on <needle-engine>
    or when the web component is removed from the DOM

    Returns void

  • Calling this function will dispose the current renderer and create a new one which will then be assigned to the context. It can be used to create a new renderer with custom WebGLRendererParameters.
    Note: Instead you can also modify the static Context.DefaultWebGlRendererParameters before the context is created.
    Note: This method is recommended because it re-uses an potentially already existing canvas element. This is necessary to keep input event handlers from working (e.g. components like OrbitControls subscribe to input events on the canvas)

    Parameters

    • Optionalparams: WebGLRendererParameters

    Returns WebGLRenderer

    the newly created renderer

  • Dispose all allocated resources and clears the scene. This is automatically called e.g. when the <needle-engine> component is removed from the DOM.

    Returns void

  • Returns void

    use dispose()

  • This will recreate the whole needle engine context and dispose the whole scene content
    All content will be reloaded (loading times might be faster due to browser caches)
    All scripts will be recreated

    Returns void

  • Remove the camera from the mainCamera stack (if it has been set before with setCurrentCamera)

    Parameters

    Returns void

  • Parameters

    Returns boolean

  • will request a renderer size update the next render call (will call updateSize the next update)

    Returns void

  • Sets the animation loop.
    Can not be done while creating the context or when disposed

    Returns boolean

  • Change the main camera

    Parameters

    Returns void

  • Parameters

    • val: boolean

    Returns void

  • Parameters

    • val: boolean

    Returns void

  • Performs a full update step including script callbacks, rendering (unless isManagedExternally is set to false) and post render callbacks

    Parameters

    • timestamp: number
    • Optionalframe: null | XRFrame

    Returns void

  • Update the camera aspect ratio or orthorgraphic camera size. This is automatically called when a DOM size change is detected.

    Parameters

    • camera: PerspectiveCamera | OrthographicCamera
    • Optionalwidth: number
    • Optionalheight: number

    Returns void

  • Call to manually perform physics steps.
    By default the context uses the physicsSteps property to perform steps during the update loop
    If you just want to increase the accuracy of physics you can instead set the physicsSteps property to a higher value

    Parameters

    • steps: number

    Returns void

  • Update the renderer and canvas size. This is also automatically called when a DOM size change is detected.

    Parameters

    • force: boolean = false

    Returns void