--- title: useTres description: useTres provides a convenient access to a simplified TresJS context. --- The `useTres` composable provides convenient access to a simplified TresJS context with direct access to core properties like the scene, renderer, camera, and utility functions. It's designed to be more straightforward than `useTresContext` while still providing access to essential TresJS functionality. ## Usage ::warning `useTres` can only be used in child components of a [`TresCanvas`](/api/components/tres-canvas) component, as its data is provided by [`TresCanvas`](/api/components/tres-canvas). :: ```ts import { useTres } from '@tresjs/core' const { scene, renderer, camera, sizes, invalidate, advance } = useTres() // Access the active camera console.log('Current camera:', camera.value) // Get canvas dimensions console.log('Canvas size:', sizes.width.value, sizes.height.value) // Trigger a re-render in on-demand mode invalidate() ``` ### Basic Example ::code-group ```vue [MyComponent.vue] ``` ```vue [App.vue] ``` :: ### Extending the Catalogue Use the `extend` function to add custom Three.js objects to the TresJS catalogue: ```vue ``` ::read-more{to="/api/components/tres-objects#extending-the-catalogue"} :: ### Manual Rendering Control For precise control over when frames are rendered: ```vue ``` ## Properties The `useTres` composable returns an object with the following properties: :::field-group ::::field{name="scene" type="ShallowRef"} The Three.js scene object containing all 3D objects in your scene. :::: ::::field{name="renderer" type="TresRenderer"} The Three.js WebGL renderer instance. Direct access to renderer methods and properties. :::: ::::field{name="camera" type="ComputedRef"} The currently active camera in the scene. Reactive reference that updates when the active camera changes. :::: ::::field{name="sizes" type="SizesType"} Reactive size information including canvas width, height, aspect ratio, and pixel ratio. :::: ::::field{name="controls" type="Ref"} Reference to the current camera controls instance (e.g., OrbitControls, FlyControls). :::: ::::field{name="extend" type="(objects: any) => void"} Function to extend the TresJS component catalogue with custom Three.js objects. :::: ::::field{name="events" type="EventManager"} The event manager instance for handling pointer interactions with 3D objects. :::: ::::field{name="invalidate" type="() => void"} Function to mark the scene as needing an update in the next frame. Essential for on-demand rendering mode. :::: ::::field{name="advance" type="() => void"} Function to manually advance the render loop by one frame. Required for manual rendering mode. :::: ::: ## Practical Examples ### Responsive Camera Setup ```vue ``` ### Custom Render Pipeline ```vue ``` ### Dynamic Scene Management ```vue ``` ## Difference from useTresContext `useTres` provides a simplified interface compared to `useTresContext`: | Feature | useTres | useTresContext | |---------|---------|----------------| | **Renderer** | Direct `TresRenderer` instance | Renderer manager with additional methods | | **Camera** | Active camera reference | Full camera management system | | **Complexity** | Simplified, focused API | Complete context with all internal details | | **Use Case** | Most common scenarios | Advanced use cases, internal operations | ::tip **Developer Tip:** Choose `useTres` for typical 3D scene interactions, and `useTresContext` when you need access to the complete internal context. :: ## Type ```ts [Signature] function useTres(): TresPartialContext interface TresPartialContext { /** The Three.js scene object containing all 3D objects */ scene: ShallowRef /** The Three.js WebGL renderer instance */ renderer: TresRenderer /** The currently active camera */ camera: ComputedRef /** Reactive size information for the canvas */ sizes: SizesType /** Reference to current camera controls */ controls: Ref /** TresJS extension function for adding custom objects */ extend: (objects: any) => void /** Event manager for pointer interactions */ events: EventManager /** Mark scene for re-render in on-demand mode */ invalidate: () => void /** Manually advance one frame in manual mode */ advance: () => void } interface SizesType { /** Canvas width in pixels */ width: Ref /** Canvas height in pixels */ height: Ref /** Canvas aspect ratio (width / height) */ aspectRatio: Ref /** Device pixel ratio */ pixelRatio: Ref } type TresRenderer = WebGLRenderer | Renderer type TresControl = any // Camera controls instance type EventManager = any // Event manager instance ```