Skip to content

Commit

Permalink
fix: disabling pushing more than one camera on nodeOpts
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosabu committed May 9, 2023
1 parent da28abc commit 57a07bf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions playground/src/components/TheExperience.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ watchEffect(() => {
<TheSphere />
<TresAxesHelper :args="[1]" />
<TresDirectionalLight :position="[0, 2, 4]" :intensity="2" cast-shadow />
<TresOrthographicCamera />
</TresCanvas>
</template>
8 changes: 8 additions & 0 deletions src/composables/useCamera/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ interface UseCameraReturn {
updateCamera: () => void
pushCamera: (camera: Camera) => void
clearCameras: () => void
setFirstCamera: (camera: Camera) => void
}

const VERTICAL_FIELD_OF_VIEW = 45
Expand Down Expand Up @@ -175,6 +176,12 @@ export function useCamera(): UseCameraReturn {
setState('camera', camera)
}

function setFirstCamera(camera: Camera): void {
if (state.cameras?.length === 0) {
pushCamera(camera)
}
}

/**
* Clear cameras array
*
Expand All @@ -195,5 +202,6 @@ export function useCamera(): UseCameraReturn {
updateCamera,
pushCamera,
clearCameras,
setFirstCamera,
}
}
8 changes: 6 additions & 2 deletions src/core/nodeOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const OBJECT_3D_USER_DATA_KEYS = {

const { logError } = useLogger()

let firstCamera = true
export const nodeOps: RendererOptions<TresObject, TresObject> = {
createElement(tag, _isSVG, _anchor, props) {
if (!props) props = {}
Expand Down Expand Up @@ -54,8 +55,11 @@ export const nodeOps: RendererOptions<TresObject, TresObject> = {
if (!props?.lookAt) {
instance.lookAt(0, 0, 0)
}
const { pushCamera } = useCamera()
pushCamera(instance)
if (firstCamera) {
const { setFirstCamera } = useCamera()
setFirstCamera(instance)
firstCamera = false
}
}

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

0 comments on commit 57a07bf

Please sign in to comment.