Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fbo): add autoRender flag as an option to useFBO #458

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/guide/abstractions/fbo.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Cientos provides an `<Fbo />` component make it easy to use FBOs in your applica
| **`height`** | `number` - the height of the FBO | Height of the canvas |
| **`depth`** | `boolean` - Whether or not the FBO should render the depth to a [`depthTexture`](https://threejs.org/docs/?q=webglre#api/en/renderers/WebGLRenderTarget.depthTexture). | `false` |
| **`settings`** | `WebGLRenderTargetOptions` - Every other configuration property for the [`WebGLRenderTarget` class](https://threejs.org/docs/#api/en/renderers/WebGLRenderTarget) | `{}` |
| **`autoRender`** | `boolean` - Whether to automatically render the FBO on the default scene. | `true` |
1 change: 1 addition & 0 deletions playground/src/pages/abstractions/fbo/FBODemo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const capsuleRef = shallowRef(null)
const { onBeforeRender, resume } = useLoop()

const state = shallowReactive({
autoRender: true,
depth: false,
settings: {
samples: 1,
Expand Down
1 change: 1 addition & 0 deletions src/core/abstractions/useFBO/component.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useFBO } from '.'
const props = withDefaults(defineProps<FboOptions>(), {
depth: false,
settings: undefined,
autoRender: true,
})

const target = useFBO(props)
Expand Down
21 changes: 16 additions & 5 deletions src/core/abstractions/useFBO/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,21 @@ export interface FboOptions {
* @memberof FboProps
*/
settings?: RenderTargetOptions

/**
* Whether to automatically render the FBO on the default scene.
*
* @default true
* @type {boolean}
* @memberof FboProps
*/
autoRender?: boolean
}

export function useFBO(options: FboOptions) {
const target: Ref<WebGLRenderTarget | null> = ref(null)

const { height, width, settings, depth } = isReactive(options) ? toRefs(options) : toRefs(reactive(options))
const { height, width, settings, depth, autoRender = ref(true) } = isReactive(options) ? toRefs(options) : toRefs(reactive(options))

const { onBeforeRender } = useLoop()
const { camera, renderer, scene, sizes, invalidate } = useTresContext()
Expand All @@ -71,11 +80,13 @@ export function useFBO(options: FboOptions) {
}, { immediate: true })

onBeforeRender(() => {
renderer.value.setRenderTarget(target.value)
renderer.value.clear()
renderer.value.render(scene.value, camera.value as Camera)
if (autoRender.value) {
renderer.value.setRenderTarget(target.value)
renderer.value.clear()
renderer.value.render(scene.value, camera.value as Camera)

renderer.value.setRenderTarget(null)
renderer.value.setRenderTarget(null)
}
}, Number.POSITIVE_INFINITY)

onBeforeUnmount(() => {
Expand Down
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import banner from 'vite-plugin-banner'
import dts from 'vite-plugin-dts'
/* import analyze from 'rollup-plugin-analyzer' */


/* import { visualizer } from 'rollup-plugin-visualizer' */
import { templateCompilerOptions } from '@tresjs/core'

Expand Down