Skip to content

Commit

Permalink
Merge pull request #128 from Tresjs/feature/PamCameraMouse
Browse files Browse the repository at this point in the history
feat(cientos): adding mouse pam effect component
  • Loading branch information
alvarosabu authored Feb 28, 2023
2 parents 072f17e + 079c502 commit b804c96
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default defineConfig({
{ text: 'useAnimations', link: '/cientos/abstractions/use-animations' },
{ text: 'Environment', link: '/cientos/abstractions/environment' },
{ text: 'useEnvironment', link: '/cientos/abstractions/use-environment' },
{ text: 'usePamMouse', link: '/cientos/abstractions/pam-camera-mouse' },
],
},
{
Expand Down
33 changes: 33 additions & 0 deletions docs/cientos/abstractions/pam-camera-mouse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# PamCameraMouse

![](/cientos/PamCameraMouse.gif)

`<PamCameraMouse />` is a component that allow you to create easily the pam mouse camera effect. The camera will update automatically according to the mouse position, creating a beautiful nice effect

## Usage

You only need import it and use it `<PamCameraMouse />` additionally you can pass two props, disabled and factor. Factor is a number to increase the movement range of the camera

```vue
<template>
<TresCanvas>
<PamCameraMouse />
<TresScene>
<Text3D text="TresJS" font="/fonts/FiraCodeRegular.json">
<TresMeshNormalMaterial />
</Text3D>
</TresScene>
</TresCanvas>
</template>
```

::: warning
By the nature of the pam mouse camera effect it creates conflicts if it's used in combination with OrbitControls
:::

## Props [[1]](#1)

| Prop | Description | Default |
| :----------- | :------------------------------------------------------ | ------- |
| **disabled** | enable or disabled the effect, boolean | false |
| **factor** | Number use to increase the range of the camera movement | 5 |
Binary file added docs/public/cientos/PamCameraMouse.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions packages/cientos/src/composables/useLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable no-console */
export const isProd = import.meta.env.MODE === 'production'

const logPrefix = '[TresJS - Cientos ▲ ■ ♥] '

interface LoggerComposition {
logError: (message: string, error?: Error | ErrorEvent) => void
logWarning: (message: string) => void
logMessage: (name: string, value: any) => void
}

export function useLogger(): LoggerComposition {
function logError(message: string, error?: Error | ErrorEvent) {
console.error(`${logPrefix} ${message}`, error || '')
}

function logWarning(message: string) {
console.warn(`${logPrefix} ${message}`)
}

function logMessage(name: string, value: any) {
if (!isProd) {
console.log(`${logPrefix} - ${name}:`, value)
}
}

return {
logError,
logWarning,
logMessage,
}
}
4 changes: 2 additions & 2 deletions packages/cientos/src/core/TransformControls.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ function addEventListeners(controls: TransformControlsImp) {
watch(
() => props.object,
() => {
if (state.camera?.value && state.renderer && state.scene && props.object) {
controls.value = new TransformControlsImp(state.camera.value, unref(state.renderer).domElement)
if (state.camera && state.renderer && state.scene && props.object) {
controls.value = new TransformControlsImp(state.camera, unref(state.renderer).domElement)
controls.value.attach(unref(props.object))
state.scene.add(unref(controls) as TransformControlsImp)
Expand Down
29 changes: 29 additions & 0 deletions packages/cientos/src/core/usePamCameraMouse/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { defineComponent } from 'vue'
import { usePamCameraMouse } from '.'
import { useCientos } from '../useCientos'

export const PamCameraMouse = defineComponent({
name: 'PamCameraMouse',
props: {
disabled: {
type: Boolean,
required: false,
default: false,
},
factor: {
type: Number,
required: false,
default: 5,
},
},
setup(props: any) {
const { state } = useCientos()
const camera = state?.camera

const PamCameraMouse = usePamCameraMouse(props.disabled as boolean, props.factor as number, camera)

return () => {
PamCameraMouse
}
},
})
24 changes: 24 additions & 0 deletions packages/cientos/src/core/usePamCameraMouse/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { watchEffect, computed } from 'vue'
import { Camera } from 'three'
import { useWindowSize, useMouse } from '@vueuse/core'
import { useLogger } from '/@/composables/useLogger'

export function usePamCameraMouse(disabled = false, factor = 5, camera: Camera | undefined) {
const { x, y } = useMouse()
const { logWarning } = useLogger()
const { width, height } = useWindowSize()
const cameraX = computed(() => (x.value / width.value - 0.5) * factor)
const cameraY = computed(() => -(y.value / height.value - 0.5) * factor)
if (camera) {
const { x: initX, y: initY } = camera.position
watchEffect(() => {
if (disabled) return
if (camera) {
camera.position.x = initX + cameraX.value
camera.position.y = initY + cameraY.value
}
})
} else {
logWarning('Scene must contain a Camera component to use this composable')
}
}
3 changes: 3 additions & 0 deletions packages/cientos/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import OrbitControls from './core/OrbitControls.vue'
import TransformControls from './core/TransformControls.vue'
import { PamCameraMouse } from './core/usePamCameraMouse/component'
import { useTweakPane } from './core/useTweakPane'
import { useAnimations } from './core/useAnimations'
import { GLTFModel } from './core/useGLTF/component'
Expand All @@ -23,6 +24,7 @@ import { Environment } from './core/useEnvironment/component'
export * from './core/useGLTF'
export * from './core/useFBX'
export * from './core/useEnvironment'
export * from './core/usePamCameraMouse'
export {
OrbitControls,
TransformControls,
Expand All @@ -45,4 +47,5 @@ export {
Dodecahedron,
useAnimations,
Environment,
PamCameraMouse,
}
2 changes: 1 addition & 1 deletion packages/cientos/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { lightGreen, yellow, gray, bold } from 'kolorist'
import pkg from './package.json'

// eslint-disable-next-line no-console
console.log(`${lightGreen('▲')} ${gray('■')} ${yellow('⚡️')} ${bold('Tres/cientos')} v${pkg.version}`)
console.log(`${lightGreen('▲')} ${gray('■')} ${yellow('')} ${bold('Tres/cientos')} v${pkg.version}`)
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
Expand Down
9 changes: 6 additions & 3 deletions packages/tres/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<script setup lang="ts">
import Responsiveness from '/@/components/Responsiveness.vue'
import { useTweakPane } from '@tresjs/cientos'
import TheEnvironment from '/@/components/TheEnvironment.vue'
// import TheEvents from '/@/components/TheEvents.vue'
useTweakPane()
</script>

<template>
<Suspense>
<Responsiveness />
<!-- <VectorSetProps /> -->
<TheEnvironment />
</Suspense>
</template>

Expand Down
6 changes: 3 additions & 3 deletions packages/tres/src/components/TheEnvironment.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { ref, shallowRef, watch } from 'vue'
import { OrbitControls, Environment, Box } from '../../../cientos/src'
import { Environment, Box, PamCameraMouse } from '../../../cientos/src'
import { TresCanvas } from '../core/useRenderer/component'
/* import { OrbitControls, GLTFModel } from '@tresjs/cientos' */
Expand Down Expand Up @@ -28,8 +28,8 @@ watch(environmentTexture, ({ getTexture }) => {
<template>
<!-- <TresCanvas v-bind="state"> -->
<TresCanvas preset="realistic">
<TresPerspectiveCamera :position="[8, 8, 8]" :fov="45" :near="0.1" :far="1000" :look-at="[-8, 3, -3]" />
<OrbitControls make-default />
<TresPerspectiveCamera :position="[10, 10, 18]" :fov="45" :near="0.1" :far="1000" :look-at="[-8, 3, -3]" />
<PamCameraMouse :factor="2" />
<TresScene>
<Environment
ref="environmentTexture"
Expand Down

0 comments on commit b804c96

Please sign in to comment.