-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): Add a new directive, v-rotate
- Loading branch information
1 parent
0be3969
commit ccf5313
Showing
9 changed files
with
170 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# v-rotate | ||
|
||
## Problem | ||
|
||
When you want to simply add rotation to your mesh, you have to use the template reference, [useRenderLoop](/api/composables#userenderloop) and then assign the axis and the speed, but before check if you mesh is already available: | ||
|
||
```vue | ||
<script setup lang="ts"> | ||
import { shallowRef, watch } from 'vue' | ||
import { useRenderLoop } from '@tresjs/core' | ||
const boxRef = shallowRef() | ||
const { onLoop } = useRenderLoop() | ||
onLoop(({ elapsed }) => { | ||
if (boxRef.value) { | ||
boxRef.value.rotation.x = elapsed | ||
} | ||
}) | ||
</script> | ||
<template> | ||
<TresCanvas> | ||
<TresPerspectiveCamera :position="[0, 2, 5]" /> | ||
<TresMesh | ||
ref="boxRef" | ||
:scale="0.5" | ||
> | ||
<TresBoxGeometry /> | ||
<TresMesh> | ||
<OrbitControls /> | ||
</TresMesh> | ||
</TresMesh> | ||
</TresCanvas> | ||
</template> | ||
``` | ||
|
||
And is A LOT of code just for a simple rotation right? Normally we need something fast to see if something is working | ||
|
||
## Usage | ||
|
||
With the new directive v-rotate provided by **TresJS**, you can do this by just adding `v-rotate` to the instance. | ||
|
||
```vue{2,8} | ||
<script setup lang="ts"> | ||
import { vRotate } from '@tresjs/core' | ||
</script> | ||
<template> | ||
<TresCanvas > | ||
<TresPerspectiveCamera :position="[0, 2, 5]" /> | ||
<TresMesh | ||
v-rotate // 😍 | ||
> | ||
<TresBoxGeometry /> | ||
</TresMesh> | ||
</TresCanvas> | ||
</template> | ||
``` | ||
By default `v-rotate` uses [Quaternions](https://threejs.org/docs/index.html?q=quater#api/en/math/Quaternion) so you don't have to worry by [Gimbal Lock](https://en.wikipedia.org/wiki/Gimbal_lock), or check if you mesh is available in the first frames. | ||
|
||
## Modifiers | ||
|
||
You can control the axis and the rotation speed by adding modifiers | ||
|
||
```vue{2,8} | ||
<script setup lang="ts"> | ||
import { vRotate } from '@tresjs/core' | ||
</script> | ||
<template> | ||
<TresCanvas > | ||
<TresPerspectiveCamera :position="[0, 2, 5]" /> | ||
<TresMesh | ||
v-rotate:x.y="0.1" // the axis will be x and y with a speed of 0.1 | ||
> | ||
<TresBoxGeometry /> | ||
</TresMesh> | ||
</TresCanvas> | ||
</template> | ||
``` | ||
|
||
_Note default speed is 0.01_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { shallowRef } from 'vue' | ||
import { Quaternion, Vector3 } from 'three' | ||
import type { TresObject } from '../types' | ||
import { useLogger, useRenderLoop } from '../composables' | ||
|
||
const { logWarning } = useLogger() | ||
|
||
export const vRotate = { | ||
mounted: ( | ||
el: TresObject, | ||
binding: { | ||
arg: 'x' | 'y' | 'z' | ||
value: number | ||
modifiers: Partial<{ x: boolean; y: boolean; z: boolean }> | ||
}, | ||
) => { | ||
if (el.isCamera) { | ||
logWarning(`Rotate the ${el.type} is not a good idea`) | ||
return | ||
} | ||
const speed = binding.value ?? 0.01 | ||
const defaultQuaternion = new Quaternion() | ||
const quaternionX = shallowRef( | ||
binding.modifiers.x || binding.arg === 'x' | ||
? new Quaternion().setFromAxisAngle(new Vector3(1, 0, 0), speed) | ||
: defaultQuaternion, | ||
) | ||
const quaternionY = shallowRef( | ||
binding.modifiers.y || binding.arg === 'y' | ||
? new Quaternion().setFromAxisAngle(new Vector3(0, 1, 0), speed) | ||
: defaultQuaternion, | ||
) | ||
const quaternionZ = shallowRef( | ||
binding.modifiers.z || binding.arg === 'z' | ||
? new Quaternion().setFromAxisAngle(new Vector3(0, 0, 1), speed) | ||
: defaultQuaternion, | ||
) | ||
if ( | ||
quaternionX.value === defaultQuaternion | ||
&& quaternionY.value === defaultQuaternion | ||
&& quaternionZ.value === defaultQuaternion | ||
) { | ||
quaternionZ.value = new Quaternion().setFromAxisAngle( | ||
new Vector3(0, 0, 1), | ||
speed, | ||
) | ||
quaternionY.value = new Quaternion().setFromAxisAngle( | ||
new Vector3(0, 1, 0), | ||
speed, | ||
) | ||
} | ||
|
||
const { onLoop } = useRenderLoop() | ||
|
||
onLoop(() => { | ||
el.applyQuaternion(quaternionX.value) | ||
el.applyQuaternion(quaternionY.value) | ||
el.applyQuaternion(quaternionZ.value) | ||
}) | ||
}, | ||
} |