|
| 1 | +# Scaling performance 🚀 |
| 2 | + |
| 3 | +> Quick guide with tips to improve performance of your Tres.js application. |
| 4 | +
|
| 5 | +We are running WebGL on the browser, which can be quite expensive and it will depend on how powerful the user's device is. To make 3D accessible to everyone, we need to make sure our applications are optimized to run also on low-end devices. This guide will provide some tips to improve the performance of your Tres.js application. |
| 6 | + |
| 7 | +## On-demand rendering <Badge type="tip" text="^4.0.0" /> |
| 8 | + |
| 9 | +By default, Tres.js will render your scene on every frame. This is great for most applications, but if you are building a game or a complex application, you might want to control when the scene is rendered. |
| 10 | + |
| 11 | +Otherwise it might drain your device battery 🔋 🔜 🪫 and make your computer sound like an airplane 🛫. |
| 12 | + |
| 13 | +Ideally, you only want to **render the scene when necessary**, for example when the user interacts with the scene and the camera moves, or when objects in the scene are animated. |
| 14 | + |
| 15 | +You can do that by setting the `renderMode` prop to `on-demand` or `manual`: |
| 16 | + |
| 17 | + |
| 18 | +### Mode `on-demand` |
| 19 | + |
| 20 | +<ClientOnly> |
| 21 | + <div style="position: relative; aspect-ratio: 16/9; height: auto; margin: 2rem 0; border-radius: 8px; overflow:hidden;"> |
| 22 | + <onDemandRendering /> |
| 23 | + </div> |
| 24 | +</ClientOnly> |
| 25 | + |
| 26 | + |
| 27 | +```vue |
| 28 | +<TresCanvas render-mode="on-demand"> |
| 29 | + <!-- Your scene goes here --> |
| 30 | +</TresCanvas> |
| 31 | +``` |
| 32 | + |
| 33 | +#### Automatic Invalidation |
| 34 | + |
| 35 | +When using `render-mode="on-demand"`, Tres.js will automatically invalidate the current frame by observing component props and lifecycle hooks like `onMounted` and `onUnmounted`. It will also invalidate the frame when resizing the window or changing any prop from the `<TresCanvas>` component like `clearColor` or `antialias`. |
| 36 | + |
| 37 | +The code below updates TresMesh's position-x prop every second, triggering a new render. |
| 38 | + |
| 39 | +```vue |
| 40 | +<script setup> |
| 41 | +import { ref } from 'vue' |
| 42 | +
|
| 43 | +const positionX = ref(0) |
| 44 | +
|
| 45 | +setTimeout(() => { |
| 46 | + positionX.value = 1 |
| 47 | +}, 1000) |
| 48 | +</script> |
| 49 | +
|
| 50 | +<template> |
| 51 | + <TresCanvas render-mode="on-demand"> |
| 52 | + <TresMesh :position-x="positionX"> |
| 53 | + <TresBoxGeometry /> |
| 54 | + <TresMeshBasicMaterial color="teal" /> |
| 55 | + </TresMesh> |
| 56 | + </TresCanvas> |
| 57 | +</template> |
| 58 | +``` |
| 59 | + |
| 60 | +#### Manual Invalidation |
| 61 | + |
| 62 | +Since it is not really possible to observe all the possible changes in your application, you can also manually invalidate the frame by calling the `invalidate()` method from the [`useTresContext` composable](../api/composables.md#usetrescontext): |
| 63 | + |
| 64 | + |
| 65 | +::: code-group |
| 66 | + |
| 67 | +```vue [App.vue] |
| 68 | +<script setup> |
| 69 | +import { TresCanvas } from '@tresjs/core' |
| 70 | +import Scene from './Scene.vue' |
| 71 | +</script> |
| 72 | +
|
| 73 | +<template> |
| 74 | + <TresCanvas |
| 75 | + render-mode="manual" |
| 76 | + > |
| 77 | + <Scene /> |
| 78 | + </TresCanvas> |
| 79 | +</template> |
| 80 | +``` |
| 81 | + |
| 82 | +```vue [Scene.vue] |
| 83 | +<script setup> |
| 84 | +import { useTres } from '@tresjs/core' |
| 85 | +
|
| 86 | +const boxRef = ref() |
| 87 | +const { invalidate } = useTres() |
| 88 | +
|
| 89 | +watch(boxRef.value, () => { |
| 90 | + boxRef.value.position.x = 1 |
| 91 | + invalidate() |
| 92 | +}) |
| 93 | +</script> |
| 94 | +
|
| 95 | +<template> |
| 96 | + <TresMesh ref="boxRef"> |
| 97 | + <TresBoxGeometry /> |
| 98 | + <TresMeshBasicMaterial color="teal" /> |
| 99 | + </TresMesh> |
| 100 | +</template> |
| 101 | +``` |
| 102 | + |
| 103 | +::: |
| 104 | + |
| 105 | +### Mode `always` |
| 106 | + |
| 107 | +In this rendering mode, Tres will continously render the scene on every frame. This is the default mode and the easiest to use, but it's also the most resource expensive one. |
| 108 | + |
| 109 | + |
| 110 | +### Mode `manual` |
| 111 | + |
| 112 | +If you want to have full control of when the scene is rendered, you can set the `render-mode` prop to `manual`: |
| 113 | + |
| 114 | +```vue |
| 115 | +<TresCanvas render-mode="manual"> |
| 116 | + <!-- Your scene goes here --> |
| 117 | +</TresCanvas> |
| 118 | +``` |
| 119 | + |
| 120 | +In this mode, Tres will not render the scene automatically. You will need to call the `advance()` method from the [`useTresContext` composable](../api/composables.md#usetrescontext) to render the scene: |
| 121 | + |
| 122 | +```vue |
| 123 | +<script setup> |
| 124 | +import { useTres } from '@tresjs/core' |
| 125 | +
|
| 126 | +const { advance } = useTres() |
| 127 | +
|
| 128 | +advance() |
| 129 | +</script> |
| 130 | +``` |
| 131 | + |
0 commit comments