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

97 lines
1.7 KiB
Vue
Raw Normal View History

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: {
2020-09-16 01:50:51 +08:00
type: [Boolean, Object],
2020-09-15 04:53:30 +08:00
default: false,
2020-09-14 22:57:11 +08:00
},
2020-09-16 01:50:51 +08:00
mouseMove: {
type: Boolean,
default: false,
},
mouseRaycast: {
type: Boolean,
default: false,
},
resize: {
type: [Boolean, String, Element],
default: 'window',
},
width: String,
height: String,
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-16 01:50:51 +08:00
mouse_move: this.mouseMove,
mouse_raycast: this.mouseRaycast,
resize: this.resize,
width: this.width,
height: this.height,
2020-09-15 04:53:30 +08:00
};
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);
},
2020-09-16 01:50:51 +08:00
onAfterResize() {},
2020-09-15 04:53:30 +08:00
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>