Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 158 stats #166

Merged
merged 7 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ export default defineConfig({
},
{
text: 'Misc',
items: [{ text: 'useTweakpane', link: '/guide/misc/use-tweakpane' }],
items: [
{ text: 'useTweakpane', link: '/guide/misc/use-tweakpane' },
{ text: 'Stats', link: '/guide/misc/stats' },
],
},
],

Expand Down
24 changes: 24 additions & 0 deletions docs/guide/misc/stats.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Stats

[Stats.js](https://github.com/mrdoob/stats.js/) is a JavaScript performance monitor, made by [mrdoop](https://github.com/mrdoob). Stats.js provides a simple info box that will help you monitor your code performance.

- FPS Frames rendered in the last second. The higher the number the better.
- MS Milliseconds needed to render a frame. The lower the number the better.
- MB MBytes of allocated memory. (Run Chrome with --enable-precise-memory-info)
- CUSTOM User-defined panel support.

**TresJS** provides a function called `stats` that creates a panel without effort or configuration in the top left corner where you'll be able to monitor your app.

## Basic usage

```ts
import { stats } from '@tresjs/cientos'

stats()
```

## Options

| Name | Type | Default | Description |
| :------------ | -------- | ------- | -------------------- |
| **showPanel** | `number` | `0` | The initial monitor. |
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"vitepress": "1.0.0-rc.4"
},
"dependencies": {
"stats.js": "^0.17.0",
"@tresjs/core": "3.1.1",
"@vueuse/core": "^10.3.0",
"camera-controls": "^2.7.1",
Expand Down
5 changes: 4 additions & 1 deletion playground/src/pages/FirstPersonControlsDemo.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { TresCanvas } from '@tresjs/core'
import { BasicShadowMap, NoToneMapping } from 'three'
import { PointerLockControls, KeyboardControls } from '@tresjs/cientos'
import { PointerLockControls, KeyboardControls, stats } from '@tresjs/cientos'

const gl = {
clearColor: '#82DBC5',
Expand All @@ -11,6 +11,8 @@ const gl = {
toneMapping: NoToneMapping,
}

stats(0)

const isActive = (state: boolean) => console.log(state)
</script>

Expand All @@ -27,3 +29,4 @@ const isActive = (state: boolean) => console.log(state)
<TresAmbientLight :intensity="1" />
</TresCanvas>
</template>

7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions src/core/misc/Stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { onUnmounted, onMounted } from 'vue'
import { useRenderLoop } from '@tresjs/core'
import StatsImpl from 'stats.js'

export /**
* Initialize stats with a specific panel.
*
* @param {number} [showPanel=0]
* @return {*}
*/
const stats = (showPanel = 0) => {
const stats = new StatsImpl()

if (stats) {
const node = document.body
stats.showPanel(showPanel)
node?.appendChild(stats.dom)

onMounted(() => {
const { onBeforeLoop, onAfterLoop, resume } = useRenderLoop()
resume()
onBeforeLoop(() => stats.begin())
onAfterLoop(() => stats.end())
})
onUnmounted(() => {
node?.removeChild(stats.dom)
})
}

return null
}
3 changes: 2 additions & 1 deletion src/core/misc/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useTweakPane } from './useTweakPane'
import { stats } from './Stats'

export { useTweakPane }
export { useTweakPane, stats }