2021-04-20 01:37:11 +08:00
|
|
|
import { defineComponent, inject, watch } from 'vue'
|
2021-04-16 09:19:50 +08:00
|
|
|
import { Scene, Color, Object3D } from 'three'
|
2021-04-22 03:05:02 +08:00
|
|
|
import { RendererInterface } from './Renderer'
|
2020-09-14 22:57:11 +08:00
|
|
|
|
2021-04-04 04:19:57 +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-20 01:37:11 +08:00
|
|
|
// id: String,
|
2020-09-16 01:50:51 +08:00
|
|
|
background: [String, Number],
|
2020-09-15 04:53:30 +08:00
|
|
|
},
|
2020-10-03 21:57:32 +08:00
|
|
|
setup(props) {
|
2021-04-22 03:05:02 +08:00
|
|
|
const renderer = inject('renderer') as RendererInterface
|
2021-04-16 09:19:50 +08:00
|
|
|
const scene = new Scene()
|
|
|
|
if (props.background) {
|
|
|
|
scene.background = new Color(props.background)
|
|
|
|
}
|
|
|
|
watch(() => props.background, (value) => { if (scene.background instanceof Color && value) scene.background.set(value) })
|
2021-04-22 03:05:02 +08:00
|
|
|
return { renderer, scene }
|
2020-09-14 22:57:11 +08:00
|
|
|
},
|
|
|
|
provide() {
|
|
|
|
return {
|
|
|
|
scene: this.scene,
|
2021-04-16 09:19:50 +08:00
|
|
|
}
|
2020-09-14 22:57:11 +08:00
|
|
|
},
|
2021-04-19 04:27:53 +08:00
|
|
|
created() {
|
2021-04-22 03:35:43 +08:00
|
|
|
this.renderer.scene = this.scene
|
2020-09-14 22:57:11 +08:00
|
|
|
},
|
2020-10-03 21:57:32 +08:00
|
|
|
methods: {
|
2021-04-16 09:19:50 +08:00
|
|
|
add(o: Object3D) { this.scene.add(o) },
|
|
|
|
remove(o: Object3D) { this.scene.remove(o) },
|
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
|
|
|
})
|