Skip to content

Commit

Permalink
feat: update parallax mouse abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
JaimeTorrealba committed May 9, 2023
1 parent 0f3fdf4 commit 88885a3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 36 deletions.
1 change: 0 additions & 1 deletion playground/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export {}

declare module '@vue/runtime-core' {
export interface GlobalComponents {
AkuAku: typeof import('./src/components/AkuAku.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']
Expand Down
6 changes: 3 additions & 3 deletions src/core/abstractions/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Text3D from './Text3D.vue'
import { useAnimations } from './useAnimations'
import { Environment } from './useEnvironment/component'
import { PamCameraMouse } from './usePamCameraMouse/component'
import { MouseParallax } from './useParallax/component'
import Stars from './Stars.vue'
import Smoke from './Smoke.vue'
import Levioso from './Levioso.vue'

export * from './usePamCameraMouse'
export * from './useParallax'
export * from './useEnvironment'
export { Text3D, useAnimations, Environment, PamCameraMouse, Stars, Smoke, Levioso }
export { Text3D, useAnimations, Environment, MouseParallax, Stars, Smoke, Levioso }
24 changes: 0 additions & 24 deletions src/core/abstractions/usePamCameraMouse/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
import { defineComponent, watchEffect } from 'vue'
import { usePamCameraMouse } from '.'
import { useMouseParallax } from '.'
import { useCientos } from '/@/core/useCientos'

export interface PamCameraMouseProps {
export interface MouseParallaxProps {
/**
* Whether to disable the mouse controls.
* @type {boolean}
* @default false
* @memberof PamCameraMouseProps
* @memberof MouseParallaxProps
*
*/
disabled?: boolean
/**
* The factor to multiply the mouse movement by.
* @type {number}
* @default 5
* @memberof PamCameraMouseProps
* @default 2.5
* @memberof MouseParallaxProps
*
**/
factor?: number
/**
* The factor to multiply the mouse movement by.
* @type {boolean}
* @default true
* @memberof MouseParallaxProps
*
**/
ease?: boolean
}

export const PamCameraMouse = defineComponent<PamCameraMouseProps>({
export const MouseParallax = defineComponent<MouseParallaxProps>({
name: 'PamCameraMouse',
props: ['disabled', 'factor'] as unknown as undefined,
props: ['disabled', 'factor', 'ease'] as unknown as undefined,
setup(props) {
const { state } = useCientos()

watchEffect(() => {
if (state?.camera) {
const camera = state?.camera

usePamCameraMouse(props.disabled as boolean, props.factor as number, camera)
useMouseParallax(props.disabled as boolean, props.factor as number, props.ease as boolean, camera)
}
})
return () => {
Expand Down
30 changes: 30 additions & 0 deletions src/core/abstractions/useParallax/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { computed } from 'vue'
import { useLogger, useTres, useRenderLoop } from '@tresjs/core'
import { useWindowSize, useMouse } from '@vueuse/core'
import { Camera, Group } from 'three'

export function useMouseParallax(disabled = false, factor = 2.5, ease = true, camera: Camera | undefined) {
const { x, y } = useMouse()
const { logWarning } = useLogger()
const { scene } = useTres()
const { width, height } = useWindowSize()

const { onLoop } = useRenderLoop()
const cameraGroup = new Group()
console.log('jaime ~ useParallax ~ ease:', ease);
const easeFactor = ease ? 2.5 : 30

const cursorX = computed(() => (x.value / width.value - 0.5) * factor)
const cursorY = computed(() => -(y.value / height.value - 0.5) * factor)
if (camera) {
cameraGroup.add(camera)
scene.value.add(cameraGroup)
onLoop(({ delta }) => {
if (disabled) return
cameraGroup.position.x += (cursorX.value - cameraGroup.position.x) * easeFactor * delta
cameraGroup.position.y += (cursorY.value - cameraGroup.position.y) * easeFactor * delta
})
} else {
logWarning('Scene must contain a Camera component to use this composable')
}
}

0 comments on commit 88885a3

Please sign in to comment.