1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 12:22:03 +08:00
trois/src/core/Scene.ts

47 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-04-25 03:45:57 +08:00
import { defineComponent, inject, InjectionKey, provide, watch } from 'vue'
import { Scene, Color, Object3D, Texture } from 'three'
import { RendererInjectionKey } from './Renderer'
export const SceneInjectionKey: InjectionKey<Scene> = Symbol('Scene')
2020-09-14 22:57:11 +08:00
export default defineComponent({
2021-03-07 20:22:54 +08:00
name: 'Scene',
2020-09-15 04:53:30 +08:00
props: {
2021-04-25 03:45:57 +08:00
background: [String, Number, Object],
2020-09-15 04:53:30 +08:00
},
2020-10-03 21:57:32 +08:00
setup(props) {
2021-04-25 03:45:57 +08:00
const renderer = inject(RendererInjectionKey)
2021-04-16 09:19:50 +08:00
const scene = new Scene()
2021-04-25 03:45:57 +08:00
if (!renderer) {
console.error('Renderer not found')
return
2021-04-16 09:19:50 +08:00
}
2021-04-25 03:45:57 +08:00
renderer.scene = scene
provide(SceneInjectionKey, scene)
const setBackground = (value: any): void => {
if (!value) return
if (typeof value === 'string' || typeof value === 'number') {
if (scene.background instanceof Color) scene.background.set(value)
else scene.background = new Color(value)
} else if (value instanceof Texture) {
scene.background = value
}
2021-04-16 09:19:50 +08:00
}
2021-04-25 03:45:57 +08:00
setBackground(props.background)
watch(() => props.background, setBackground)
const add = (o: Object3D): void => { scene.add(o) }
const remove = (o: Object3D): void => { scene.remove(o) }
2021-04-25 04:49:00 +08:00
return { scene, add, remove }
2020-10-03 21:57:32 +08:00
},
2020-09-14 22:57:11 +08:00
render() {
2021-04-16 09:19:50 +08:00
return this.$slots.default ? this.$slots.default() : []
2020-09-14 22:57:11 +08:00
},
2021-03-07 22:14:34 +08:00
__hmrId: 'Scene',
2021-04-16 09:19:50 +08:00
})