From 2fe99ca3ffab2ac720b0406c119b5cfebc33c605 Mon Sep 17 00:00:00 2001 From: Sander Moolin Date: Tue, 6 Apr 2021 10:28:04 -0400 Subject: [PATCH] new stats component --- package.json | 1 + src/components/misc/Stats.js | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/components/misc/Stats.js diff --git a/package.json b/package.json index 1dfaeb2..b23b8e9 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "gsap": "^3.5.1", "rollup-plugin-terser": "^7.0.2", "rollup-plugin-vue": "^6.0.0-beta.11", + "stats.js": "0.17.0", "three": "^0.127", "vite": "^2.1.5", "vue": "^3.0.11" diff --git a/src/components/misc/Stats.js b/src/components/misc/Stats.js new file mode 100644 index 0000000..399dc43 --- /dev/null +++ b/src/components/misc/Stats.js @@ -0,0 +1,47 @@ +import Stats from 'stats.js'; +import { inject } from 'vue'; + +export default { + props: { + noSetup: { type: Boolean, default: false } + }, + emits: ['created'], + inject: ['rendererComponent'], + setup({ noSetup }) { + const stats = new Stats(); + if (!noSetup) { + stats.showPanel(0); // 0: fps, 1: ms, 2: mb, 3+: custom + document.body.appendChild(stats.dom); + } + return { stats }; + }, + mounted() { + if (!this.noSetup) { + this.rendererComponent.onBeforeRender(this.begin); + this.rendererComponent.onAfterRender(this.end); + } + this.$emit('created', { stats: this.stats }); + }, + methods: { + begin() { + if (this.stats) { + this.stats.begin(); + } + }, + end() { + if (this.stats) { + this.stats.end(); + } + } + }, + unmounted() { + if (this.stats && this.stats.dom) { + this.stats.dom.parentElement.removeChild(this.stats.dom); + } + this.rendererComponent.offBeforeRender(this.begin); + this.rendererComponent.offAfterRender(this.end); + }, + render() { + return this.$slots.default ? this.$slots.default() : []; + }, +}; \ No newline at end of file