Skip to content

Commit 9228092

Browse files
committed
feat: add context to root on instances localstate
1 parent 1f9ce8d commit 9228092

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

src/composables/useTresContextProvider/index.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { UseRendererOptions } from '../useRenderer'
99
import { useRenderer } from '../useRenderer'
1010
import { extend } from '../../core/catalogue'
1111
import { useLogger } from '../useLogger'
12+
import type { TresScene } from '../../types'
1213

1314
export interface InternalState {
1415
priority: Ref<number>
@@ -43,7 +44,7 @@ export interface PerformanceState {
4344
}
4445

4546
export interface TresContext {
46-
scene: ShallowRef<Scene>
47+
scene: ShallowRef<TresScene>
4748
sizes: { height: Ref<number>; width: Ref<number>; aspectRatio: ComputedRef<number> }
4849
extend: (objects: any) => void
4950
camera: ComputedRef<Camera | undefined>
@@ -74,7 +75,7 @@ export function useTresContextProvider({
7475
rendererOptions,
7576
emit,
7677
}: {
77-
scene: Scene
78+
scene: TresScene
7879
canvas: MaybeRef<HTMLCanvasElement>
7980
windowSize: MaybeRefOrGetter<boolean>
8081
disableRender: MaybeRefOrGetter<boolean>
@@ -109,7 +110,7 @@ export function useTresContextProvider({
109110
width: computed(() => debouncedReactiveSize.value.width),
110111
aspectRatio,
111112
}
112-
const localScene = shallowRef<Scene>(scene)
113+
const localScene = shallowRef<TresScene>(scene)
113114
const {
114115
camera,
115116
cameras,
@@ -189,8 +190,8 @@ export function useTresContextProvider({
189190

190191
provide('useTres', ctx)
191192

192-
// Add context to scene.userData
193-
ctx.scene.value.userData.tres__context = ctx
193+
// Add context to scene local state
194+
ctx.scene.value.__tres.root = ctx
194195

195196
// Performance
196197
const updateInterval = 100 // Update interval in milliseconds

src/core/nodeOps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export const nodeOps: RendererOptions<TresObject, TresObject | null> = {
9898
if (parent && parent.isScene) {
9999
scene = parent as unknown as TresScene
100100
if (child) {
101-
child.__tres.root = scene.userData.tres__context as TresContext
101+
child.__tres.root = scene.__tres.root as TresContext
102102
}
103103
}
104104

src/types/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { DefineComponent, VNode, VNodeRef } from 'vue'
33

44
import type * as THREE from 'three'
55
import type { EventProps as PointerEventHandlerEventProps } from '../composables/usePointerEventHandler'
6+
import type { TresContext } from '../composables/useTresContextProvider'
67

78
// Based on React Three Fiber types by Pmndrs
89
// https://github.com/pmndrs/react-three-fiber/blob/v9/packages/fiber/src/three-types.ts
@@ -63,6 +64,9 @@ export interface TresObject3D extends THREE.Object3D<THREE.Object3DEventMap> {
6364
export type TresObject = TresBaseObject & (TresObject3D | THREE.BufferGeometry | THREE.Material | THREE.Fog)
6465

6566
export interface TresScene extends THREE.Scene {
67+
__tres: {
68+
root: TresContext
69+
}
6670
userData: {
6771
// keys are prefixed with tres__ to avoid name collisions
6872
tres__registerCamera?: (newCamera: THREE.Camera, active?: boolean) => void

0 commit comments

Comments
 (0)