1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 04:12:02 +08:00
This commit is contained in:
Kevin Levron 2020-10-03 15:57:32 +02:00
parent 101c680161
commit 44e290f475
5 changed files with 28 additions and 9 deletions

View File

@ -38,15 +38,18 @@ export default {
width: String, width: String,
height: String, height: String,
}, },
setup(props) { setup() {
return { return {
three: useThree(), three: useThree(),
raf: true, raf: true,
onMountedCallbacks: [],
}; };
}, },
provide() { provide() {
return { return {
three: this.three, three: this.three,
// renderer: this.three.renderer,
rendererComponent: this,
}; };
}, },
mounted() { mounted() {
@ -68,12 +71,17 @@ export default {
if (this.three.composer) this.animateC(); if (this.three.composer) this.animateC();
else this.animate(); else this.animate();
}; };
this.onMountedCallbacks.forEach(c => c());
}, },
beforeUnmount() { beforeUnmount() {
this.raf = false; this.raf = false;
this.three.dispose(); this.three.dispose();
}, },
methods: { methods: {
onMounted(callback) {
this.onMountedCallbacks.push(callback);
},
onBeforeRender(callback) { onBeforeRender(callback) {
this.three.onBeforeRender(callback); this.three.onBeforeRender(callback);
}, },

View File

@ -23,6 +23,14 @@ export default {
this.three.scene = this.scene; this.three.scene = this.scene;
} }
}, },
methods: {
// add(o) {
// this.scene.add(o);
// },
// remove(o) {
// this.scene.remove(o);
// },
},
render() { render() {
if (this.$slots.default) { if (this.$slots.default) {
return this.$slots.default(); return this.$slots.default();

View File

@ -17,13 +17,15 @@ export default {
cubeRTSize: { type: Number, default: 512 }, cubeRTSize: { type: Number, default: 512 },
cubeCameraNear: { type: Number, default: 0.1 }, cubeCameraNear: { type: Number, default: 0.1 },
cubeCameraFar: { type: Number, default: 2000 }, cubeCameraFar: { type: Number, default: 2000 },
cubeRTAutoUpdate: Boolean,
}, },
mounted() { mounted() {
this.initGem(); this.initGem();
this.three.onBeforeRender(this.updateCubeRT); if (this.cubeRTAutoUpdate) this.three.onBeforeRender(this.updateCubeRT);
else this.rendererComponent.onMounted(this.updateCubeRT);
}, },
unmounted() { unmounted() {
this.three.offBeforeRender(this.upateCubeRT); this.three.offBeforeRender(this.updateCubeRT);
}, },
methods: { methods: {
initGem() { initGem() {

View File

@ -3,7 +3,7 @@ import { watch } from 'vue';
import useBindProp from '../use/useBindProp.js'; import useBindProp from '../use/useBindProp.js';
export default { export default {
inject: ['three', 'scene'], inject: ['three', 'scene', 'rendererComponent'],
emits: ['ready'], emits: ['ready'],
props: { props: {
materialId: String, materialId: String,

View File

@ -14,13 +14,15 @@ export default {
cubeRTSize: { type: Number, default: 512 }, cubeRTSize: { type: Number, default: 512 },
cubeCameraNear: { type: Number, default: 0.1 }, cubeCameraNear: { type: Number, default: 0.1 },
cubeCameraFar: { type: Number, default: 2000 }, cubeCameraFar: { type: Number, default: 2000 },
cubeRTAutoUpdate: Boolean,
}, },
mounted() { mounted() {
this.initMirrorMesh(); this.initMirrorMesh();
this.three.onBeforeRender(this.upateCubeRT); if (this.cubeRTAutoUpdate) this.three.onBeforeRender(this.updateCubeRT);
else this.rendererComponent.onMounted(this.updateCubeRT);
}, },
unmounted() { unmounted() {
this.three.offBeforeRender(this.upateCubeRT); this.three.offBeforeRender(this.updateCubeRT);
}, },
methods: { methods: {
initMirrorMesh() { initMirrorMesh() {
@ -32,11 +34,10 @@ export default {
this.material.envMap = cubeRT.texture; this.material.envMap = cubeRT.texture;
this.material.needsUpdate = true; this.material.needsUpdate = true;
}, },
upateCubeRT() { updateCubeRT() {
this.mesh.visible = false; this.mesh.visible = false;
this.cubeCamera.update(this.three.renderer, this.scene); this.cubeCamera.update(this.three.renderer, this.scene);
this.mesh.visible = true; this.mesh.visible = true;
console.log('upateCubeRT');
}, },
}, },
__hmrId: 'MirrorMesh', __hmrId: 'MirrorMesh',