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: removed useCamera logic from nodeOps #308

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
2 changes: 0 additions & 2 deletions docs/guide/migration-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ watch(modelRef, model => {

The `TresOrbitControls` component needs to be after the camera in the tree. This is because the controls need to know the camera to work.

Read more about it here: [Troubleshooting](/guide/troubleshooting.md)

Change this:

```vue {3,5}
Expand Down
21 changes: 0 additions & 21 deletions docs/guide/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,6 @@ You followed the [Getting started guide](/guide/getting-started.md) but you stil

These are the most common reasons why you might not be able to see your scene:

### Make sure you have a camera 🎥

The first thing you need to do is to make sure you have a camera in your scene. If you don't have a camera, you won't be able to see anything.

![No camera found](/no-camera-found.png)

```vue
<!-- Wrong ❌ -->
<TresCanvas>
<TresOrbitControls />
</TresCanvas>
```

```vue
<!-- Correct ✅ -->
<TresCanvas>
<TresPerspectiveCamera />
<TresOrbitControls />
</TresCanvas>
```

### Check the height of your canvas 📏

Another common issue is that the `TresCanvas` component is creating by default a `canvas` element takes the `width` and `height` of the parent element. If the parent element has no height, the canvas will have no height either.
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/your-first-scene.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Then you can add a [**PerspectiveCamera**](https://threejs.org/docs/index.html?q
```

::: warning
A common issue is that the camera default position is the origin of the scene (0,0,0), if you still can see your scene try adding a position to the camera `<TresPerspectiveCamera :position="[3, 3, 3]" />`
A common issue is that the camera default position is the origin of the scene (0,0,0), TresJS will automatically set the position of your camera to `[3,3,3]` if the prop `position`. If no camera is defined in you scene, a perspective camera is added automatically.`
:::

## Adding a 🍩
Expand Down
9 changes: 7 additions & 2 deletions playground/src/components/TheEvents.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ function onPointerMove(ev) {
console.log(ev)
}
}

const visible = ref(true)
</script>

<template>
<TresCanvas v-bind="gl">
<TresPerspectiveCamera :position="[11, 11, 11]" :fov="45" :near="0.1" :far="1000" :look-at="[0, 0, 0]" />
<button @click="visible = !visible"></button>
<div v-if="visible">
<TresCanvas window-size v-bind="gl">

<OrbitControls />

<template v-for="x in [-2.5, 0, 2.5]">
Expand All @@ -62,4 +66,5 @@ function onPointerMove(ev) {
<TresDirectionalLight :intensity="1" />
<TresAmbientLight :intensity="1" />
</TresCanvas>
</div>
</template>
35 changes: 19 additions & 16 deletions src/components/TresScene.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { App, defineComponent, h, onMounted, onUnmounted, ref, watch, VNode } from 'vue'
import { App, defineComponent, h, onMounted, onUnmounted, ref, watch } from 'vue'
import * as THREE from 'three'
import { ColorSpace, ShadowMapType, ToneMapping } from 'three'
import { isString } from '@alvarosabu/utils'
import { createTres } from '../core/renderer'
import { TresCamera } from '../types/'
import {
Expand Down Expand Up @@ -80,18 +79,6 @@ export const TresScene = defineComponent<TresSceneProps>({
setState('container', container)
setState('pointerEventHandler', pointerEventHandler)

const isCameraAvailable = ref()

const internal = slots && slots.default && slots.default()

if (internal && internal?.length > 0) {
isCameraAvailable.value =
internal.some((node: VNode) => isString(node.type) && node.type.includes('Camera')) || props.camera
if (!isCameraAvailable.value) {
logWarning('No camera found in the scene, please add one!')
}
}

const { onLoop, resume } = useRenderLoop()

onMounted(() => {
Expand All @@ -104,6 +91,22 @@ export const TresScene = defineComponent<TresSceneProps>({

const { activeCamera, pushCamera, clearCameras } = useCamera()

function setCamera() {
const camera = scene.getObjectByProperty('isCamera', true)

if (!camera) {
// eslint-disable-next-line max-len
logWarning('No camera found. Creating a default perspective camera. To have full control over a camera, please add one to the scene.')
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000)
camera.position.set(3,3,3)
camera.lookAt(0,0,0)
pushCamera(camera)
} else {

pushCamera(camera as TresCamera)
}
}

function initRenderer() {
const { renderer } = useRenderer(props)

Expand All @@ -125,6 +128,7 @@ export const TresScene = defineComponent<TresSceneProps>({
app.provide(TRES_CONTEXT_KEY, tres)
app.provide('extend', extend)
app.mount(scene as unknown)
setCamera()
}
mountApp()

Expand All @@ -138,8 +142,7 @@ export const TresScene = defineComponent<TresSceneProps>({
app = createTres(slots)
app.provide('extend', extend)
app.mount(scene as unknown)
const camera = scene.children.find((child: any) => child.isCamera)
pushCamera(camera as TresCamera)
setCamera()
resume()
}

Expand Down
8 changes: 2 additions & 6 deletions src/core/nodeOps.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RendererOptions } from 'vue'
import { BufferAttribute, Scene } from 'three'
import { useCamera, useLogger } from '../composables'
import { isFunction } from '@alvarosabu/utils'
import { useLogger } from '../composables'
import { catalogue } from './catalogue'
import { TresObject } from '../types'
import { isHTMLTag, kebabToCamel } from '../utils'
Expand All @@ -20,7 +20,6 @@ let scene: Scene | null = null

const { logError } = useLogger()

let firstCamera = true
export const nodeOps: RendererOptions<TresObject, TresObject> = {
createElement(tag, _isSVG, _anchor, props) {
if (!props) props = {}
Expand All @@ -46,16 +45,13 @@ export const nodeOps: RendererOptions<TresObject, TresObject> = {
instance = new target(...props.args)
}

if (instance.isCamera && firstCamera) {
if (instance.isCamera) {
if (!props?.position) {
instance.position.set(3, 3, 3)
}
if (!props?.lookAt) {
instance.lookAt(0, 0, 0)
}
const { setFirstCamera } = useCamera()
setFirstCamera(instance)
firstCamera = false
}

if (props?.attach === undefined) {
Expand Down