2020-09-14 22:57:11 +08:00
|
|
|
<template>
|
|
|
|
<canvas ref="canvas">
|
|
|
|
<slot></slot>
|
|
|
|
</canvas>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import useThree from './useThree';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
props: {
|
2020-09-15 04:53:30 +08:00
|
|
|
antialias: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
|
|
|
},
|
2020-09-14 22:57:11 +08:00
|
|
|
alpha: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
2020-09-15 17:48:29 +08:00
|
|
|
shadow: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
2020-09-15 04:53:30 +08:00
|
|
|
orbitCtrl: {
|
|
|
|
default: false,
|
2020-09-14 22:57:11 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
setup(props) {
|
|
|
|
return {
|
|
|
|
three: useThree(),
|
2020-09-15 04:53:30 +08:00
|
|
|
beforeRender: [],
|
|
|
|
raf: true,
|
2020-09-14 22:57:11 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
provide() {
|
|
|
|
return {
|
|
|
|
three: this.three,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
mounted() {
|
2020-09-15 04:53:30 +08:00
|
|
|
const params = {
|
2020-09-14 22:57:11 +08:00
|
|
|
canvas: this.$refs.canvas,
|
2020-09-15 04:53:30 +08:00
|
|
|
antialias: this.antialias,
|
|
|
|
alpha: this.alpha,
|
|
|
|
orbit_ctrl: this.orbitCtrl,
|
|
|
|
};
|
2020-09-14 22:57:11 +08:00
|
|
|
|
2020-09-15 04:53:30 +08:00
|
|
|
if (this.three.init(params)) {
|
2020-09-15 17:48:29 +08:00
|
|
|
this.three.renderer.shadowMap.enabled = this.shadow;
|
2020-09-15 04:53:30 +08:00
|
|
|
this.animate();
|
|
|
|
};
|
2020-09-14 22:57:11 +08:00
|
|
|
},
|
|
|
|
beforeUnmount() {
|
|
|
|
this.raf = false;
|
2020-09-15 04:53:30 +08:00
|
|
|
this.beforeRender.splice(0);
|
2020-09-14 22:57:11 +08:00
|
|
|
this.three.dispose();
|
|
|
|
},
|
|
|
|
methods: {
|
2020-09-15 04:53:30 +08:00
|
|
|
onBeforeRender(callback) {
|
|
|
|
this.beforeRender.push(callback);
|
|
|
|
},
|
|
|
|
animate() {
|
|
|
|
if (this.raf) requestAnimationFrame(this.animate);
|
|
|
|
this.beforeRender.forEach(c => c());
|
|
|
|
this.three.render();
|
2020-09-14 22:57:11 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
canvas {
|
|
|
|
display: block;
|
|
|
|
}
|
|
|
|
</style>
|