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(cientos): add first directive vLog #206

Merged
merged 3 commits into from
Sep 13, 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
7 changes: 7 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ export default defineConfig({
{ text: 'Stats', link: '/guide/misc/stats' },
],
},
{
text: 'Directives',
collapsed: true,
items: [
{ text: 'v-log', link: '/guide/directives/v-log' },
],
},
],

socialLinks: [
Expand Down
56 changes: 56 additions & 0 deletions docs/guide/directives/v-log.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# v-log

### Problem

When you have to log your instance you have to use the template reference and then log them:

```vue{3}
<script setup lang="ts">
import { shallowRef, watch } from 'vue'
const sphereRef = shallowRef()
watch(sphereRef, value => {
console.log(value) // Really for a log?!!! 😫
})
</script>
<template>
<TresCanvas >
<TresPerspectiveCamera :position="[0, 2, 5]" />
<Sphere
ref="sphereRef"
:scale="0.5"
/>
<OrbitControls />
</TresCanvas>
</template>
```

And is A LOT of code just for a simple log right?

## Solution

With the new directive v-log provided by **TresJS**, you can do this by just adding `v-log` to the instance.

```vue{3}
<script setup lang="ts">
import { OrbitControls, Sphere, vLog } from '@tresjs/cientos'
</script>
<template>
<TresCanvas >
<TresPerspectiveCamera :position="[0, 2, 5]" />
<Sphere
ref="sphereRef"
:scale="0.5"
v-log:material <!-- will print just the material 🎉 -->
/>
<OrbitControls v-log />
</TresCanvas>
</template>
```

Note that you can pass a modifier with the name of a property, for example `v-log:material`, and will log directly the `material` property 😍

::: warning
The component `<TresCanvas >` will not log the canvas or the scene.
:::
8 changes: 1 addition & 7 deletions playground/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@ export {}
declare module 'vue' {
export interface GlobalComponents {
AkuAku: typeof import('./src/components/AkuAku.vue')['default']
EnvironmentDemo: typeof import('./src/components/EnvironmentDemo.vue')['default']
GlassMaterialDemo: typeof import('./src/components/GlassMaterialDemo.vue')['default']
DirectivesDemo: typeof import('./src/components/DirectivesDemo.vue')['default']
Gltf: typeof import('./src/components/gltf/index.vue')['default']
LeviosoDemo: typeof import('./src/components/LeviosoDemo.vue')['default']
MapControlsDemo: typeof import('./src/components/MapControlsDemo.vue')['default']
ModelsDemo: typeof import('./src/components/ModelsDemo.vue')['default']
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
TheGizmos: typeof import('./src/components/TheGizmos.vue')['default']
UseVideoTextureDemo: typeof import('./src/components/useVideoTextureDemo.vue')['default']
WobbleMaterial: typeof import('./src/components/WobbleMaterial.vue')['default']
}
}
49 changes: 49 additions & 0 deletions playground/src/components/DirectivesDemo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<script setup lang="ts">
import { reactive, shallowRef } from 'vue'
import { TresCanvas, useRenderLoop } from '@tresjs/core'
import { OrbitControls, Stars, Sphere, vLog } from '@tresjs/cientos'
import { SRGBColorSpace, NoToneMapping } from 'three'
const gl = {
clearColor: '#333',
outputColorSpace: SRGBColorSpace,
toneMapping: NoToneMapping,
}
const options = reactive({
size: 0.1,
sizeAttenuation: true,
count: 5000,
depth: 50,
radius: 100,
})
const sphereRef = shallowRef()
const { onLoop } = useRenderLoop()
onLoop(({ elapsed }) => {
if (sphereRef.value) {
sphereRef.value.value.position.y = Math.sin(elapsed) * 0.5
}
})
</script>

<template>
<TresCanvas v-bind="gl">
<TresPerspectiveCamera :position="[0, 2, 5]" />
<Stars
:radius="options.radius"
:depth="options.depth"
:count="options.count"
:size="options.size"
:size-attenuation="options.sizeAttenuation"
/>
<Sphere
ref="sphereRef"
v-log:material
:scale="0.5"
/>
<TresGridHelper :args="[10, 10]" />
<OrbitControls />
</TresCanvas>
</template>
2 changes: 1 addition & 1 deletion playground/src/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<template>
<Suspense>
<ModelsDemo />
<DirectivesDemo />
</Suspense>
</template>
3 changes: 3 additions & 0 deletions src/core/directives/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { vLog } from './vLog'

export { vLog }
11 changes: 11 additions & 0 deletions src/core/directives/vLog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const vLog = {
updated: (el: any, binding: any) => {
if (binding.arg) {
// eslint-disable-next-line no-console
console.log(`v-log:${binding.arg}`, el[binding.arg])
return
}
// eslint-disable-next-line no-console
console.log('v-log', el)
},
}
1 change: 1 addition & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './materials'
export * from './shapes'
export * from './staging'
export * from './misc'
export * from './directives'
Loading