Skip to content

Commit

Permalink
feat: use render instead of createApp (#375)
Browse files Browse the repository at this point in the history
Co-authored-by: Alvaro Saburido <[email protected]>
  • Loading branch information
enpitsuLin and alvarosabu authored Aug 23, 2023
1 parent f2ae46b commit 9461a78
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
44 changes: 23 additions & 21 deletions src/components/TresCanvas.vue
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<script setup lang="ts">
import { extend } from '../core/catalogue'
import { onMounted } from 'vue'
import { createTres } from '../core/renderer'
import { useTresContextProvider, type TresContext } from '../composables'
import { App, Ref, computed, ref, shallowRef, watch, watchEffect } from 'vue'
import {
Scene,
PerspectiveCamera,
Scene,
WebGLRendererParameters,
type ColorSpace,
type ShadowMapType,
type ToneMapping,
} from 'three'
import { Ref, computed, onMounted, provide, ref, shallowRef, watch, watchEffect } from 'vue'
import { useTresContextProvider, type TresContext } from '../composables'
import { extend } from '../core/catalogue'
import { render } from '../core/renderer'
import {
useLogger,
useRenderLoop,
usePointerEventHandler,
useRenderLoop,
} from '../composables'
import type { TresCamera } from '../types/'
import { Fragment, defineComponent, h } from 'vue'
import type { RendererPresetsType } from '../composables/useRenderer/const'
import type { TresCamera, TresObject } from '../types/'
export interface TresCanvasProps extends Omit<WebGLRendererParameters, 'canvas'> {
Expand Down Expand Up @@ -65,22 +65,24 @@ const slots = defineSlots<{
default(): any
}>()
let app: App
const createInternalComponent = (context: TresContext) => {
return defineComponent({
setup() {
provide('useTres', context)
provide('extend', extend)
return () => h(Fragment, null, slots?.default ? slots.default() : [])
}
})
}
const mountCustomRenderer = (context: TresContext) => {
app = createTres(slots)
app.provide('useTres', context) // TODO obsolete?
app.provide('extend', extend)
app.mount(scene.value)
const InternalComponent = createInternalComponent(context)
render(h(InternalComponent), scene.value as unknown as TresObject)
}
const dispose = () => {
const dispose = (context: TresContext) => {
scene.value.children = []
app.unmount()
app = createTres(slots)
app.provide('extend', extend)
app.mount(scene.value)
mountCustomRenderer(context)
resume()
}
Expand Down Expand Up @@ -140,7 +142,7 @@ onMounted(() => {
}
if (import.meta.hot)
import.meta.hot.on('vite:afterUpdate', dispose)
import.meta.hot.on('vite:afterUpdate', () => dispose(context))
})
</script>
<template>
Expand All @@ -156,4 +158,4 @@ onMounted(() => {
zIndex: 1,
}">
</canvas>
</template>
</template>
14 changes: 3 additions & 11 deletions src/core/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import * as THREE from 'three'

import { createRenderer, Slots } from 'vue'
import { createRenderer } from 'vue'
import { extend } from './catalogue'
import { nodeOps } from './nodeOps'

export const { createApp } = createRenderer(nodeOps)

export const createTres = (slots: Slots) => {
const app = createApp(internalFnComponent)
function internalFnComponent() {
return slots && slots.default ? slots.default() : []
}
return app
}
export const { render } = createRenderer(nodeOps)

// Creates the catalogue of components based on THREE namespace
extend(THREE)

export default { createTres, extend }
export default { extend }

0 comments on commit 9461a78

Please sign in to comment.