@tresjs/core 5.0.0-next.0 → 5.0.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,6 +14,7 @@ export type LoaderProto<T> = new (manager?: LoadingManager) => TresLoader<T>;
14
14
  export interface TresLoaderOptions<T, Shallow extends boolean> {
15
15
  manager?: LoadingManager;
16
16
  extensions?: (loader: TresLoader<T>) => void;
17
+ initialValue?: T;
17
18
  asyncOptions?: UseAsyncStateOptions<Shallow, any | null>;
18
19
  }
19
20
  /**
@@ -1,6 +1,6 @@
1
- import { TresEvent, TresInstance } from 'src/types';
2
1
  import { Intersection, Object3D, Object3DEventMap } from 'three';
3
2
  import { ShallowRef } from 'vue';
3
+ import { TresEvent, TresInstance } from '../../types';
4
4
  import { TresContext } from '../useTresContextProvider';
5
5
  export declare const useRaycaster: (objectsWithEvents: ShallowRef<TresInstance[]>, ctx: TresContext) => {
6
6
  intersects: ShallowRef<Intersection<Object3D<Object3DEventMap>>[]>;
@@ -1,5 +1,5 @@
1
1
  import { ColorSpace, Scene, ShadowMapType, ToneMapping, WebGLRendererParameters, WebGLRenderer } from 'three';
2
- import { EmitEventFn, TresColor } from '../../types';
2
+ import { TresColor } from '../../types';
3
3
  import { TresContext } from '../useTresContextProvider';
4
4
  import { RendererPresetsType } from './const';
5
5
  import { MaybeRefOrGetter } from '@vueuse/core';
@@ -7,6 +7,12 @@ import { MaybeRef } from 'vue';
7
7
  type TransformToMaybeRefOrGetter<T> = {
8
8
  [K in keyof T]: MaybeRefOrGetter<T[K]> | MaybeRefOrGetter<T[K]>;
9
9
  };
10
+ /**
11
+ * If set to 'on-demand', the scene will only be rendered when the current frame is invalidated
12
+ * If set to 'manual', the scene will only be rendered when advance() is called
13
+ * If set to 'always', the scene will be rendered every frame
14
+ */
15
+ export type RenderMode = 'always' | 'on-demand' | 'manual';
10
16
  export interface UseRendererOptions extends TransformToMaybeRefOrGetter<WebGLRendererParameters> {
11
17
  /**
12
18
  * Enable shadows in the Renderer
@@ -68,24 +74,26 @@ export interface UseRendererOptions extends TransformToMaybeRefOrGetter<WebGLRen
68
74
  clearColor?: MaybeRefOrGetter<TresColor>;
69
75
  windowSize?: MaybeRefOrGetter<boolean | string>;
70
76
  preset?: MaybeRefOrGetter<RendererPresetsType>;
71
- renderMode?: MaybeRefOrGetter<'always' | 'on-demand' | 'manual'>;
77
+ renderMode?: MaybeRef<RenderMode>;
72
78
  /**
73
79
  * A `number` sets the renderer's device pixel ratio.
74
80
  * `[number, number]` clamp's the renderer's device pixel ratio.
75
81
  */
76
82
  dpr?: MaybeRefOrGetter<number | [number, number]>;
77
83
  }
78
- export declare function useRenderer({ canvas, options, contextParts: { sizes, render, invalidate, advance }, }: {
79
- canvas: MaybeRef<HTMLCanvasElement>;
84
+ export declare function useRendererManager({ scene, canvas, options, contextParts: { sizes, loop, camera }, }: {
80
85
  scene: Scene;
86
+ canvas: MaybeRef<HTMLCanvasElement>;
81
87
  options: UseRendererOptions;
82
- emit: EmitEventFn;
83
- contextParts: Pick<TresContext, 'sizes' | 'camera' | 'render'> & {
84
- invalidate: () => void;
85
- advance: () => void;
86
- };
88
+ contextParts: Pick<TresContext, 'sizes' | 'camera' | 'loop'>;
87
89
  }): {
88
- renderer: import('vue').ShallowRef<WebGLRenderer, WebGLRenderer>;
90
+ instance: import('vue').ShallowRef<WebGLRenderer, WebGLRenderer>;
91
+ isReady: Readonly<import('vue').Ref<boolean, boolean>>;
92
+ advance: () => void;
93
+ onRender: import('@vueuse/core').EventHook<WebGLRenderer>;
94
+ invalidate: (amountOfFramesToInvalidate?: number) => void;
95
+ canBeInvalidated: import('vue').ComputedRef<boolean>;
96
+ amountOfFramesToRender: import('vue').Ref<number, number>;
89
97
  };
90
- export type UseRendererReturn = ReturnType<typeof useRenderer>;
98
+ export type UseRendererManagerReturn = ReturnType<typeof useRendererManager>;
91
99
  export {};
@@ -0,0 +1,36 @@
1
+ import { ComputedRef } from 'vue';
2
+ import { TresContext } from '../useTresContextProvider';
3
+ import { Camera, WebGLRenderer } from 'three';
4
+ export interface TresPartialContext extends Omit<TresContext, 'renderer' | 'camera'> {
5
+ /**
6
+ * The renderer instance
7
+ *
8
+ * @type {WebGLRenderer}
9
+ * @memberof TresPartialContext
10
+ */
11
+ renderer: WebGLRenderer;
12
+ /**
13
+ * The current active camera
14
+ *
15
+ * @type {ComputedRef<Camera | undefined>}
16
+ * @memberof TresPartialContext
17
+ */
18
+ camera: ComputedRef<Camera | undefined>;
19
+ /**
20
+ * Marks the scene as needing an update in the next frame.
21
+ * This is used in on-demand rendering mode to schedule a render.
22
+ *
23
+ * @type {() => void}
24
+ * @memberof TresPartialContext
25
+ */
26
+ invalidate: () => void;
27
+ /**
28
+ * Manually advances the render loop by one frame.
29
+ * This is particularly useful in manual rendering mode where you want explicit control over when frames are rendered.
30
+ *
31
+ * @type {() => void}
32
+ * @memberof TresPartialContext
33
+ */
34
+ advance: () => void;
35
+ }
36
+ export declare function useTres(): TresPartialContext;
@@ -1,74 +1,30 @@
1
- import { Camera, WebGLRenderer, Raycaster } from 'three';
2
- import { ComputedRef, DeepReadonly, MaybeRef, MaybeRefOrGetter, Ref, ShallowRef } from 'vue';
1
+ import { Raycaster } from 'three';
2
+ import { MaybeRef, MaybeRefOrGetter, Ref, ShallowRef } from 'vue';
3
3
  import { RendererLoop } from '../../core/loop';
4
- import { EmitEventFn, TresControl, TresObject, TresScene } from '../../types';
5
- import { UseRendererOptions } from '../useRenderer';
4
+ import { TresControl, TresObject, TresScene } from '../../types';
5
+ import { UseRendererManagerReturn, UseRendererOptions } from '../useRenderer/useRendererManager';
6
+ import { UseCameraReturn } from '../useCamera/';
6
7
  import { SizesType } from '../useSizes';
7
8
  import { TresEventManager } from '../useTresEventManager';
8
- export interface InternalState {
9
- priority: Ref<number>;
10
- frames: Ref<number>;
11
- maxFrames: number;
12
- }
13
- export interface RenderState {
14
- /**
15
- * If set to 'on-demand', the scene will only be rendered when the current frame is invalidated
16
- * If set to 'manual', the scene will only be rendered when advance() is called
17
- * If set to 'always', the scene will be rendered every frame
18
- */
19
- mode: Ref<'always' | 'on-demand' | 'manual'>;
20
- priority: Ref<number>;
21
- frames: Ref<number>;
22
- maxFrames: number;
23
- canBeInvalidated: ComputedRef<boolean>;
24
- }
25
- export interface PerformanceState {
26
- maxFrames: number;
27
- fps: {
28
- value: number;
29
- accumulator: number[];
30
- };
31
- memory: {
32
- currentMem: number;
33
- allocatedMem: number;
34
- accumulator: number[];
35
- };
36
- }
37
9
  export interface TresContext {
38
10
  scene: ShallowRef<TresScene>;
39
11
  sizes: SizesType;
40
12
  extend: (objects: any) => void;
41
- camera: ComputedRef<Camera | undefined>;
42
- cameras: DeepReadonly<Ref<Camera[]>>;
13
+ camera: UseCameraReturn;
43
14
  controls: Ref<TresControl | null>;
44
- renderer: ShallowRef<WebGLRenderer>;
15
+ renderer: UseRendererManagerReturn;
45
16
  raycaster: ShallowRef<Raycaster>;
46
- perf: PerformanceState;
47
- render: RenderState;
48
17
  loop: RendererLoop;
49
- /**
50
- * Invalidates the current frame when renderMode === 'on-demand'
51
- */
52
- invalidate: () => void;
53
- /**
54
- * Advance one frame when renderMode === 'manual'
55
- */
56
- advance: () => void;
57
- registerCamera: (maybeCamera: unknown) => void;
58
- setCameraActive: (cameraOrUuid: Camera | string) => void;
59
- deregisterCamera: (maybeCamera: unknown) => void;
60
18
  eventManager?: TresEventManager;
61
19
  registerObjectAtPointerEventHandler?: (object: TresObject) => void;
62
20
  deregisterObjectAtPointerEventHandler?: (object: TresObject) => void;
63
21
  registerBlockingObjectAtPointerEventHandler?: (object: TresObject) => void;
64
22
  deregisterBlockingObjectAtPointerEventHandler?: (object: TresObject) => void;
65
23
  }
66
- export declare function useTresContextProvider({ scene, canvas, windowSize, rendererOptions, emit, }: {
24
+ export declare function useTresContextProvider({ scene, canvas, windowSize, rendererOptions, }: {
67
25
  scene: TresScene;
68
26
  canvas: MaybeRef<HTMLCanvasElement>;
69
27
  windowSize: MaybeRefOrGetter<boolean>;
70
28
  rendererOptions: UseRendererOptions;
71
- emit: EmitEventFn;
72
29
  }): TresContext;
73
30
  export declare function useTresContext(): TresContext;
74
- export declare const useTres: typeof useTresContext;
@@ -1,7 +1,9 @@
1
- import { EmitEventFn } from 'src/types';
1
+ import { TresPointerEvent } from 'src/types';
2
2
  import { Scene } from 'three';
3
3
  import { TresContext } from '../useTresContextProvider';
4
+ import { EventHookOff } from '@vueuse/core';
4
5
  export interface TresEventManager {
6
+ onEvent: EventHookOff<TresPointerEvent>;
5
7
  /**
6
8
  * Forces the event system to refire events with the previous mouse event
7
9
  */
@@ -16,7 +18,8 @@ export interface TresEventManager {
16
18
  registerPointerMissedObject: (object: unknown) => void;
17
19
  deregisterPointerMissedObject: (object: unknown) => void;
18
20
  }
19
- export declare function useTresEventManager(scene: Scene, context: TresContext, emit: EmitEventFn): {
21
+ export declare function useTresEventManager(scene: Scene, context: TresContext): {
22
+ onEvent: import('@vueuse/core').EventHookOn<TresPointerEvent>;
20
23
  forceUpdate: () => void;
21
24
  registerObject: (maybeTresObject: unknown) => void;
22
25
  deregisterObject: (maybeTresObject: unknown) => void;
@@ -15,8 +15,6 @@ export interface LoopCallbackWithCtx extends LoopCallback {
15
15
  controls: Ref<(EventDispatcher<object> & {
16
16
  enabled: boolean;
17
17
  }) | null>;
18
- invalidate: Fn;
19
- advance: Fn;
20
18
  }
21
19
  export type LoopCallbackFn = (params: LoopCallbackWithCtx) => void;
22
20
  export interface RendererLoop {
@@ -0,0 +1,60 @@
1
+ import { Mesh } from 'three';
2
+ import { TresObject } from '../types';
3
+ import { SceneGraphObject } from './types';
4
+ import { TresContext } from '../composables/useTresContextProvider';
5
+ /**
6
+ * Creates a node representation of a Three.js object for the inspector tree
7
+ * @param object - The Three.js object to create a node for
8
+ * @returns A SceneGraphObject representing the Three.js object with relevant metadata
9
+ */
10
+ export declare const createNode: (object: TresObject) => SceneGraphObject;
11
+ /**
12
+ * Creates a context node for the inspector tree
13
+ * @param key - The key identifier for the context node
14
+ * @param uuid - The unique identifier for the context
15
+ * @param parentKey - Optional parent key for nested context nodes
16
+ * @returns A SceneGraphObject representing the context node
17
+ */
18
+ export declare function createContextNode(key: string, uuid: string, parentKey?: string): SceneGraphObject;
19
+ /**
20
+ * Recursively builds a graph representation of Three.js objects for the inspector
21
+ * @param object - The root Three.js object to build the graph from
22
+ * @param node - The current node in the graph being built
23
+ * @param filter - Optional filter string to filter objects by type or name
24
+ */
25
+ export declare function buildGraph(object: TresObject, node: SceneGraphObject, filter?: string): void;
26
+ /**
27
+ * Recursively builds a graph representation of context objects for the inspector
28
+ * @param object - The root object to build the context graph from
29
+ * @param node - The current node in the graph being built
30
+ * @param visited - WeakSet to track visited objects and prevent circular references
31
+ * @param depth - Current depth in the object tree
32
+ * @param maxDepth - Maximum depth to traverse
33
+ * @param contextUuid - Optional UUID for the context
34
+ * @param parentKey - Optional parent key for nested objects
35
+ */
36
+ export declare function buildContextGraph(object: any, node: SceneGraphObject, visited?: WeakSet<WeakKey>, depth?: number, maxDepth?: number, contextUuid?: string, parentKey?: string): void;
37
+ /**
38
+ * Handler for inspector tree updates
39
+ * @param tres - The TresContext instance
40
+ * @returns A function that handles inspector tree payload updates
41
+ */
42
+ export declare const inspectorTreeHandler: (tres: TresContext) => (payload: any) => void;
43
+ /**
44
+ * Handler for inspector state updates
45
+ * @param tres - The TresContext instance
46
+ * @param options - Options for the handler
47
+ * @param options.highlightMesh - The currently highlighted mesh
48
+ * @param options.prevInstance - The previously selected instance
49
+ * @returns A function that handles inspector state payload updates
50
+ */
51
+ export declare const inspectorStateHandler: (tres: TresContext, { highlightMesh, prevInstance }: {
52
+ highlightMesh: Mesh | null;
53
+ prevInstance: TresObject | null;
54
+ }) => (payload: any) => void;
55
+ /**
56
+ * Handler for inspector state edits
57
+ * @param tres - The TresContext instance
58
+ * @returns A function that handles inspector state edit payload updates
59
+ */
60
+ export declare const inspectorEditStateHandler: (tres: TresContext) => (payload: any) => void;
@@ -1,14 +1,10 @@
1
1
  import { TresContext } from '../composables';
2
+ import { App } from 'vue';
2
3
  export interface Tags {
3
4
  label: string;
4
5
  textColor: number;
5
6
  backgroundColor: number;
6
7
  tooltip?: string;
7
8
  }
8
- export interface SceneGraphObject {
9
- id: string;
10
- label: string;
11
- children: SceneGraphObject[];
12
- tags: Tags[];
13
- }
14
- export declare function registerTresDevtools(app: any, tres: TresContext): void;
9
+ export declare const INSPECTOR_ID = "tres:inspector";
10
+ export declare function registerTresDevtools(app: App, tres: TresContext): void;
@@ -1,2 +1,14 @@
1
1
  import { TresContext } from '../composables';
2
- export declare function setupDevtools(ctx: TresContext): void;
2
+ export interface PerformanceState {
3
+ maxFrames: number;
4
+ fps: {
5
+ value: number;
6
+ accumulator: number[];
7
+ };
8
+ memory: {
9
+ currentMem: number;
10
+ allocatedMem: number;
11
+ accumulator: number[];
12
+ };
13
+ }
14
+ export declare function setupTresDevtools(ctx: TresContext): void;
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Represents a tag that can be displayed in the inspector UI
3
+ * @interface Tags
4
+ */
5
+ export interface Tags {
6
+ /** The text to display in the tag */
7
+ label: string;
8
+ /** The color of the text in hexadecimal format (e.g., 0xFFFFFF) */
9
+ textColor: number;
10
+ /** The background color of the tag in hexadecimal format (e.g., 0x000000) */
11
+ backgroundColor: number;
12
+ /** Optional tooltip text to show on hover */
13
+ tooltip?: string;
14
+ }
15
+ /**
16
+ * Represents a node in the scene graph that can be displayed in the inspector
17
+ * @interface SceneGraphObject
18
+ */
19
+ export interface SceneGraphObject {
20
+ /** Unique identifier for the node */
21
+ id: string;
22
+ /** Display name of the node */
23
+ label: string;
24
+ /** Array of child nodes */
25
+ children: SceneGraphObject[];
26
+ /** Array of tags associated with the node */
27
+ tags: Tags[];
28
+ }
29
+ /**
30
+ * Represents memory usage statistics for Three.js resources
31
+ * @interface MemoryStats
32
+ */
33
+ export interface MemoryStats {
34
+ /** Map of geometry names to their memory usage in bytes */
35
+ geometries: Map<string, number>;
36
+ /** Map of material names to their memory usage in bytes */
37
+ materials: Map<string, number>;
38
+ /** Map of texture names to their memory usage in bytes */
39
+ textures: Map<string, number>;
40
+ }
41
+ /**
42
+ * Represents a property in the inspector state
43
+ * @interface InspectorStateProperty
44
+ */
45
+ export interface InspectorStateProperty {
46
+ /** The name of the property */
47
+ key: string;
48
+ /** The value of the property */
49
+ value: unknown;
50
+ /** Whether the property can be edited in the inspector */
51
+ editable: boolean;
52
+ }
53
+ /**
54
+ * Represents the state of an inspector panel
55
+ * @interface InspectorState
56
+ */
57
+ export interface InspectorState {
58
+ /** Array of properties to display in the inspector */
59
+ object: InspectorStateProperty[];
60
+ }
61
+ /**
62
+ * Represents the internal state of the devtools
63
+ * @interface DevtoolsState
64
+ */
65
+ export interface DevtoolsState {
66
+ /** The current scene graph representation */
67
+ sceneGraph: SceneGraphObject | null;
68
+ /** Timestamp of the last update */
69
+ lastUpdate: number;
70
+ /** Minimum time between updates in milliseconds */
71
+ updateThreshold: number;
72
+ /** Current memory usage statistics */
73
+ memoryStats: MemoryStats;
74
+ }
75
+ /**
76
+ * Represents a payload sent to or received from the devtools
77
+ * @interface DevtoolsPayload
78
+ */
79
+ export interface DevtoolsPayload {
80
+ /** The ID of the inspector panel */
81
+ inspectorId: string;
82
+ /** The ID of the node being inspected */
83
+ nodeId: string;
84
+ /** Optional filter string for searching */
85
+ filter?: string;
86
+ /** Optional state to update */
87
+ state?: InspectorState;
88
+ /** Optional path to the property being edited */
89
+ path?: string[];
90
+ }
@@ -17,8 +17,13 @@ export type Args<T> = T extends ConstructorRepresentation ? ConstructorParameter
17
17
  export interface TresCatalogue {
18
18
  [name: string]: ConstructorRepresentation;
19
19
  }
20
- export type EmitEventName = 'render' | 'ready' | 'click' | 'double-click' | 'context-menu' | 'pointer-move' | 'pointer-up' | 'pointer-down' | 'pointer-enter' | 'pointer-leave' | 'pointer-over' | 'pointer-out' | 'pointer-missed' | 'wheel';
21
- export type EmitEventFn = (event: EmitEventName, ...args: any[]) => void;
20
+ export declare const pointerEventTypes: readonly ["click", "double-click", "context-menu", "pointer-move", "pointer-up", "pointer-down", "pointer-enter", "pointer-leave", "pointer-over", "pointer-out", "pointer-missed", "wheel"];
21
+ export type PointerEventType = typeof pointerEventTypes[number];
22
+ export interface TresPointerEvent {
23
+ type: PointerEventType;
24
+ event: TresEvent;
25
+ intersection?: Intersection;
26
+ }
22
27
  export type TresCamera = THREE.OrthographicCamera | THREE.PerspectiveCamera;
23
28
  /**
24
29
  * Represents the properties of an instance.
@@ -90,7 +95,7 @@ export interface IntersectionEvent<TSourceEvent> extends Intersection {
90
95
  /** The ray that pierced it */
91
96
  ray: THREE.Ray;
92
97
  /** The camera that was used by the raycaster */
93
- camera: TresCamera;
98
+ camera: THREE.Camera;
94
99
  /** stopPropagation will stop underlying handlers from firing */
95
100
  stopPropagation: () => void;
96
101
  /** The original host event */
@@ -9,7 +9,7 @@ export type PriorityEventHookOff<T> = EventHookOff<T>;
9
9
  export type PriorityEventHookTrigger<T = any> = (param?: T) => void;
10
10
  export interface PriorityEventHook<T = any> {
11
11
  on: PriorityEventHookOn<T>;
12
- off: EventHookOff<T>;
12
+ off: (fn: Callback<T>) => void;
13
13
  trigger: PriorityEventHookTrigger<T>;
14
14
  dispose: () => void;
15
15
  count: number;
@@ -1,5 +1,5 @@
1
- import { TresObject, TresPrimitive } from 'src/types';
2
- import { BufferGeometry, Camera, Fog, Light, Material, Object3D, Scene } from 'three';
1
+ import { TresCamera, TresInstance, TresObject, TresPrimitive } from 'src/types';
2
+ import { BufferGeometry, Color, ColorRepresentation, Fog, Light, Material, Object3D, OrthographicCamera, PerspectiveCamera, Scene, Layers } from 'three';
3
3
  /**
4
4
  * Type guard to check if a value is undefined
5
5
  * @param value - The value to check
@@ -134,7 +134,36 @@ export declare function isObject3D(value: unknown): value is Object3D;
134
134
  * }
135
135
  * ```
136
136
  */
137
- export declare function isCamera(value: unknown): value is Camera;
137
+ export declare function isCamera(value: unknown): value is TresCamera;
138
+ export declare function isColor(value: unknown): value is Color;
139
+ export declare function isColorRepresentation(value: unknown): value is ColorRepresentation;
140
+ interface VectorLike {
141
+ set: (...args: any[]) => void;
142
+ constructor?: (...args: any[]) => any;
143
+ }
144
+ export declare function isVectorLike(value: unknown): value is VectorLike;
145
+ interface Copyable {
146
+ copy: (...args: any[]) => void;
147
+ constructor?: (...args: any[]) => any;
148
+ }
149
+ export declare function isCopyable(value: unknown): value is Copyable;
150
+ interface ClassInstance {
151
+ constructor?: (...args: any[]) => any;
152
+ }
153
+ export declare function isClassInstance(object: unknown): object is ClassInstance;
154
+ export declare function isLayers(value: unknown): value is Layers;
155
+ /**
156
+ * Type guard to check if a value is a Three.js OrthographicCamera
157
+ * @param value - The value to check
158
+ * @returns True if the value is a Three.js OrthographicCamera instance, false otherwise
159
+ */
160
+ export declare function isOrthographicCamera(value: unknown): value is OrthographicCamera;
161
+ /**
162
+ * Type guard to check if a value is a Three.js PerspectiveCamera
163
+ * @param value - The value to check
164
+ * @returns True if the value is a Three.js PerspectiveCamera instance, false otherwise
165
+ */
166
+ export declare function isPerspectiveCamera(value: unknown): value is PerspectiveCamera;
138
167
  /**
139
168
  * Type guard to check if a value is a Three.js BufferGeometry
140
169
  * @param value - The value to check
@@ -249,3 +278,18 @@ export declare function isTresObject(value: unknown): value is TresObject;
249
278
  * that can be used directly in the scene without needing to be wrapped in a Three.js object.
250
279
  */
251
280
  export declare function isTresPrimitive(value: unknown): value is TresPrimitive;
281
+ /**
282
+ * Type guard to check if a value is a TresInstance (has __tres property)
283
+ * @param value - The value to check
284
+ * @returns True if the value is a TresInstance (has __tres property), false otherwise
285
+ * @example
286
+ * ```ts
287
+ * const value = new THREE.Mesh()
288
+ * if (isTresInstance(value)) {
289
+ * // TypeScript knows value is TresInstance here
290
+ * // You can safely access value.__tres
291
+ * }
292
+ * ```
293
+ */
294
+ export declare function isTresInstance(value: unknown): value is TresInstance;
295
+ export {};