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: 524 feat add directives to core #525

Merged
merged 5 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
docs: add directives
  • Loading branch information
alvarosabu committed Jan 27, 2024
commit e937625e5f3e2cc13dbd0967ea59efdcbe49955a
10 changes: 10 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ export default defineConfig({
},
],
},
{
text: 'Directives',
collapsed: true,
items: [
{ text: 'v-log', link: '/directives/v-log' },
{ text: 'v-light-helper', link: '/directives/v-light-helper' },
{ text: 'v-always-look-at', link: '/directives/v-always-look-at' },
{ text: 'v-distance-to', link: '/directives/v-distance-to' },
],
},
{
text: 'Ecosystem',
items: [
Expand Down
61 changes: 61 additions & 0 deletions docs/directives/v-always-look-at.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# v-always-look-at 👀

With the new directive v-always-look-at provided by **TresJS**, you can add easily command an [Object3D](https://threejs.org/docs/index.html?q=object#api/en/core/Object3D) to always look at a specific position, this could be passed as a Vector3 or an Array.

## Usage

```vue{3,9}
<script setup lang="ts">
import { TresCanvas } from '@tresjs/core'
import { Box, vAlwaysLookAt } from '@tresjs/cientos'
</script>
<template>
<TresCanvas >
<TresPerspectiveCamera :position="[0, 2, 5]" />
<Box
v-always-look-at="new Vector3(0, 0, 0)"
/>
</TresCanvas>
</template>
```
No matter where the Box move will always look-at the position [0,0,0]

### Why not use the in built method look-at?

You could ask, this is fine but I can use the `:look-at` method directly in the component, why should I need this?

The answers is that with the method `:look-at` you will indicated to look at that position just once, when the instance is mounted, then if the object changes this will not get updated

### You can look at other instance too!

Another advantage is that you can look at an instance in movement, for example with the camera, like so:

```vue{4,6,20,23}
<script setup lang="ts">
import { shallowRef } from 'vue'
import { TresCanvas, useRenderLoop } from '@tresjs/core'
import { Box, vAlwaysLookAt } from '@tresjs/cientos'

const sphereRef = shallowRef()

const { onLoop } = useRenderLoop()

// here we update the position of the sphere and the camera will always follow the object
onLoop(({ elapsed }) => {
if (sphereRef.value) {
sphereRef.value.value.position.y = Math.sin(elapsed) * 1.5
}
})
</script>
<template>
<TresCanvas >
<TresPerspectiveCamera :position="[0, 2, 5]"
v-always-look-at="sphereRef"
/>
<Sphere
ref="sphereRef"
:scale="0.5"
/>
</TresCanvas>
</template>
```
36 changes: 36 additions & 0 deletions docs/directives/v-distance-to.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# v-distance-to

Have you tried to calculate the distance between two Object3Ds?

With the new directive `v-distance-to` it's easier than ever, you should only indicate the target object to perform the measure and the result will appear in your console.

In addition, an arrow will be created to indicate which objects you're measuring.

```vue{2,8,13}
<script setup lang="ts">
import { OrbitControls, Sphere, vLog } from '@tresjs/cientos'
</script>
<template>
<TresCanvas v-bind="gl">
<TresPerspectiveCamera :position="[0, 2, 5]" />
<Sphere
ref="sphere1Ref"
:position="[-2, slider, 0]"
:scale="0.5"
/>
<Sphere
v-distance-to="sphere1Ref"
:position="[2, 0, 0]"
:scale="0.5"
/>
<TresGridHelper :args="[10, 10]" />
<OrbitControls />
</TresCanvas>
</template>
```

The use of `v-distance-to` is reactive, so it works perfectly with @tres/leches 🍰.

::: warning
`v-distance-to` will not measure an object in movement within the renderLoop.
:::
34 changes: 34 additions & 0 deletions docs/directives/v-light-helper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# v-light-helper 🔆

With the new directive v-light-helper provided by **TresJS**, you can add fast the respective helper to your lights with just one line of code 😍.

The following lights are supported:
- DirectionalLight
- PointLight
- SpotLight
- HemisphereLight

## Usage

```vue{2,8,11,14,17}
<script setup lang="ts">
import { OrbitControls, Sphere, vLightHelper } from '@tresjs/cientos'
</script>
<template>
<TresCanvas >
<TresPerspectiveCamera :position="[0, 2, 5]" />
<TresDirectionalLight
v-light-helper
/>
<TresPointLight
v-light-helper
/>
<TresSpotLight
v-light-helper
/>
<TresHemisphereLight
v-light-helper
/>
</TresCanvas>
</template>
```
53 changes: 53 additions & 0 deletions docs/directives/v-log.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# v-log

### Problem

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

```vue
<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?

## Usage

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

```vue{2,10,12}
<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 😍
Loading