@tresjs/core 4.3.3 → 5.0.0-next.0

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.
@@ -1,8 +1,8 @@
1
1
  import { default as UseLoader } from './useLoader/component.vue';
2
2
  import { default as UseTexture } from './useTexture/component.vue';
3
3
  export * from './useCamera/';
4
+ export * from './useGraph';
4
5
  export * from './useLoader';
5
- export * from './useLogger';
6
6
  export * from './useLoop';
7
7
  export * from './useRaycaster';
8
8
  export * from './useRenderer/';
@@ -0,0 +1,4 @@
1
+ import { Object3D } from 'three';
2
+ import { MaybeRef } from '@vueuse/core';
3
+ import { TresObject } from '../../types';
4
+ export declare function useGraph(object: MaybeRef<Object3D | TresObject>): import('vue').ComputedRef<import('../..').TresObjectMap>;
@@ -1,24 +1,38 @@
1
+ import { LoadingManager } from 'three';
1
2
  import { LoaderProto } from './index';
2
- type __VLS_Props = {
3
- loader: LoaderProto<unknown>;
4
- url: string | string[];
5
- };
6
- declare function __VLS_template(): {
7
- attrs: Partial<{}>;
3
+ import { TresObjectMap } from '../../utils/graph';
4
+ declare const _default: <T extends TresObjectMap>(__VLS_props: NonNullable<Awaited<typeof __VLS_setup>>["props"], __VLS_ctx?: __VLS_PrettifyLocal<Pick<NonNullable<Awaited<typeof __VLS_setup>>, "attrs" | "emit" | "slots">>, __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>["expose"], __VLS_setup?: Promise<{
5
+ props: __VLS_PrettifyLocal<Pick<Partial<{}> & Omit<{
6
+ readonly onLoaded?: ((result: T) => any) | undefined;
7
+ readonly onError?: ((error: unknown) => any) | undefined;
8
+ } & import('vue').VNodeProps & import('vue').AllowedComponentProps & import('vue').ComponentCustomProps, never>, "onLoaded" | "onError"> & {
9
+ /**
10
+ * The THREE.js loader to use
11
+ */
12
+ loader: LoaderProto<T>;
13
+ /**
14
+ * Path to resource
15
+ */
16
+ path: string;
17
+ /**
18
+ * Optional THREE.js LoadingManager
19
+ */
20
+ manager?: LoadingManager;
21
+ } & Partial<{}>> & import('vue').PublicProps;
22
+ expose(exposed: import('vue').ShallowUnwrapRef<{}>): void;
23
+ attrs: any;
8
24
  slots: {
9
25
  default?(_: {
10
- data: unknown;
26
+ state: import('vue').UnwrapRef<T>;
27
+ isLoading: boolean;
28
+ error: unknown;
11
29
  }): any;
12
30
  };
13
- refs: {};
14
- rootEl: any;
31
+ emit: ((evt: "loaded", result: T) => void) & ((evt: "error", error: unknown) => void);
32
+ }>) => import('vue').VNode & {
33
+ __ctx?: Awaited<typeof __VLS_setup>;
15
34
  };
16
- type __VLS_TemplateResult = ReturnType<typeof __VLS_template>;
17
- declare const __VLS_component: import('vue').DefineComponent<__VLS_Props, {}, {}, {}, {}, import('vue').ComponentOptionsMixin, import('vue').ComponentOptionsMixin, {}, string, import('vue').PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import('vue').ComponentProvideOptions, false, {}, any>;
18
- declare const _default: __VLS_WithTemplateSlots<typeof __VLS_component, __VLS_TemplateResult["slots"]>;
19
35
  export default _default;
20
- type __VLS_WithTemplateSlots<T, S> = T & {
21
- new (): {
22
- $slots: S;
23
- };
24
- };
36
+ type __VLS_PrettifyLocal<T> = {
37
+ [K in keyof T]: T[K];
38
+ } & {};
@@ -1,44 +1,50 @@
1
- import { Loader, LoadingManager, Object3D } from 'three';
2
- export interface TresLoader<T> extends Loader {
3
- load: (url: string | string[], onLoad: (result: T) => void, onProgress?: (event: ProgressEvent) => void, onError?: (event: ErrorEvent) => void) => void;
4
- loadAsync: (url: string, onProgress?: (event: ProgressEvent) => void) => Promise<T>;
1
+ import { UseAsyncStateOptions, UseAsyncStateReturn } from '@vueuse/core';
2
+ import { Loader, LoadingManager } from 'three';
3
+ import { MaybeRef } from 'vue';
4
+ export interface LoaderMethods {
5
+ setDRACOLoader: (dracoLoader: any) => void;
6
+ setMeshoptDecoder: (meshoptDecoder: any) => void;
7
+ setKTX2Loader: (ktx2Loader: any) => void;
5
8
  }
9
+ export type TresLoader<T> = Loader & Partial<LoaderMethods> & {
10
+ load: (url: string, onLoad: (result: T) => void, onProgress?: (event: ProgressEvent<EventTarget>) => void, onError?: (err: unknown) => void) => void;
11
+ loadAsync: (url: string, onProgress?: (event: ProgressEvent) => void) => Promise<T>;
12
+ };
6
13
  export type LoaderProto<T> = new (manager?: LoadingManager) => TresLoader<T>;
7
- export type LoaderReturnType<T, L extends LoaderProto<T>> = T extends unknown ? Awaited<ReturnType<InstanceType<L>['loadAsync']>> : T;
14
+ export interface TresLoaderOptions<T, Shallow extends boolean> {
15
+ manager?: LoadingManager;
16
+ extensions?: (loader: TresLoader<T>) => void;
17
+ asyncOptions?: UseAsyncStateOptions<Shallow, any | null>;
18
+ }
8
19
  /**
9
- * Traverse an object and return all the nodes and materials
10
- *
11
- * @export
12
- * @param {Object3D} object
13
- * @return { [key: string]: any }
20
+ * Return type for the useLoader composable
21
+ * @template T - The type of the loaded asset (e.g., GLTF, Texture, etc.)
22
+ * @template Shallow - Whether to use shallow reactivity for better performance
23
+ * @extends {UseAsyncStateReturn<T, [string], Shallow>} - Extends VueUse's useAsyncState return type
14
24
  */
15
- export declare function traverseObjects(object: Object3D): {
16
- [key: string]: any;
25
+ export type UseLoaderReturn<T, Shallow extends boolean> = UseAsyncStateReturn<T, [string], Shallow> & {
26
+ /**
27
+ * Loads a new asset from the given path
28
+ * @param path - The URL or path to the asset to load
29
+ */
30
+ load: (path: string) => void;
31
+ /**
32
+ * Progress of the loading process
33
+ * @property loaded - The number of bytes loaded
34
+ * @property total - The total number of bytes to load
35
+ * @property percentage - The percentage of the loading process
36
+ */
37
+ progress: {
38
+ loaded: number;
39
+ total: number;
40
+ percentage: number;
41
+ };
17
42
  };
18
- export type Extensions<T extends {
19
- prototype: LoaderProto<any>;
20
- }> = (loader: T['prototype']) => void;
21
43
  /**
22
- * Load resources using THREE loaders and return the result as a promise
23
- *
24
- * @see https://tresjs.org/api/composables.html#useloader
25
- * @see https://threejs.org/docs/index.html?q=loader#api/en/loaders/Loader
26
- *
27
- * ```ts
28
- * import { useLoader } from '@tresjs/core'
29
- * import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
30
- *
31
- * const { scene } = await useLoader(THREE.GLTFLoader, 'path/to/asset.gltf')
32
- * ```
33
- *
34
- * @export
35
- * @template LoaderProto<T>
36
- * @template string | string[],
37
- * @param {LoaderProto<T>} Loader
38
- * @param {string | string[],} url
39
- * @param {Extensions<TresLoader<T>>} [extensions]
40
- * @param {(event: ProgressEvent<EventTarget>) => void} [onProgress]
41
- * @param {(proto: TresLoader<T>) => void} [cb]
42
- * @return {*}
44
+ * Vue composable for loading 3D models using Three.js loaders
45
+ * @param Loader - The Three.js loader constructor
46
+ * @param path - The path to the model file
47
+ * @param options - Optional configuration for the loader
48
+ * @returns UseAsyncState composable with the loaded model
43
49
  */
44
- export declare function useLoader<T>(Loader: LoaderProto<T>, url: string | string[], extensions?: (loader: TresLoader<T>) => void, onProgress?: (event: ProgressEvent<EventTarget>) => void, cb?: (proto: TresLoader<T>) => void): Promise<T | T[]>;
50
+ export declare function useLoader<T, Shallow extends boolean = false>(Loader: LoaderProto<T>, path: MaybeRef<string>, options?: TresLoaderOptions<T, Shallow>): UseLoaderReturn<T, Shallow>;
@@ -1,4 +1,3 @@
1
- import { App as DevtoolsApp } from '@vue/devtools-api';
2
1
  import { TresContext } from '../composables';
3
2
  export interface Tags {
4
3
  label: string;
@@ -12,4 +11,4 @@ export interface SceneGraphObject {
12
11
  children: SceneGraphObject[];
13
12
  tags: Tags[];
14
13
  }
15
- export declare function registerTresDevtools(app: DevtoolsApp, tres: TresContext): void;
14
+ export declare function registerTresDevtools(app: any, tres: TresContext): void;
@@ -0,0 +1,2 @@
1
+ import { TresContext } from '../composables';
2
+ export declare function setupDevtools(ctx: TresContext): void;
@@ -8,6 +8,8 @@ export * from './core/catalogue';
8
8
  export * from './core/loop';
9
9
  export * from './directives';
10
10
  export * from './types';
11
+ export * from './utils/graph';
12
+ export * from './utils/logger';
11
13
  export interface TresOptions {
12
14
  extends?: Record<string, unknown>;
13
15
  }
@@ -0,0 +1,14 @@
1
+ import { Material, Mesh, Object3D, Scene } from 'three';
2
+ export interface TresObjectMap {
3
+ nodes: {
4
+ [name: string]: Object3D;
5
+ };
6
+ materials: {
7
+ [name: string]: Material;
8
+ };
9
+ meshes: {
10
+ [name: string]: Mesh;
11
+ };
12
+ scene?: Scene;
13
+ }
14
+ export declare function buildGraph(object: Object3D): TresObjectMap;
@@ -2,6 +2,7 @@ import { nodeOps } from 'src/core/nodeOps';
2
2
  import { AttachType, LocalState, TresInstance, TresObject, TresPrimitive } from 'src/types';
3
3
  import { Material, Mesh, MeshBasicMaterial, Scene, Vector3 } from 'three';
4
4
  import { TresContext } from '../composables/useTresContextProvider';
5
+ export * from './logger';
5
6
  export declare function toSetMethodName(key: string): string;
6
7
  export declare const merge: (target: any, source: any) => any;
7
8
  export declare const isHTMLTag: (key: string) => boolean;
@@ -14,10 +15,6 @@ export declare const get: <T>(obj: any, path: string | string[]) => T | undefine
14
15
  export declare const set: (obj: any, path: string | string[], value: any) => void;
15
16
  export declare function deepEqual(a: any, b: any): boolean;
16
17
  export declare function deepArrayEqual(arr1: any[], arr2: any[]): boolean;
17
- /**
18
- * TypeSafe version of Array.isArray
19
- */
20
- export declare const isArray: (a: any) => a is any[] | readonly any[];
21
18
  export declare function editSceneObject(scene: Scene, objectUuid: string, propertyPath: string[], value: any): void;
22
19
  export declare function createHighlightMaterial(): MeshBasicMaterial;
23
20
  export declare function animateHighlight(highlightMesh: Mesh, startTime: number): void;
@@ -1,18 +1,251 @@
1
1
  import { TresObject, TresPrimitive } from 'src/types';
2
2
  import { BufferGeometry, Camera, Fog, Light, Material, Object3D, Scene } from 'three';
3
- export declare function und(u: unknown): u is undefined;
4
- export declare function arr(u: unknown): u is any[];
5
- export declare function num(u: unknown): u is number;
6
- export declare function str(u: unknown): u is string;
7
- export declare function bool(u: unknown): u is boolean;
8
- export declare function fun(u: unknown): u is (...args: any[]) => any;
9
- export declare function obj(u: unknown): u is Record<string | number | symbol, unknown>;
10
- export declare function object3D(u: unknown): u is Object3D;
11
- export declare function camera(u: unknown): u is Camera;
12
- export declare function bufferGeometry(u: unknown): u is BufferGeometry;
13
- export declare function material(u: unknown): u is Material;
14
- export declare function light(u: unknown): u is Light;
15
- export declare function fog(u: unknown): u is Fog;
16
- export declare function scene(u: unknown): u is Scene;
17
- export declare function tresObject(u: unknown): u is TresObject;
18
- export declare function tresPrimitive(u: unknown): u is TresPrimitive;
3
+ /**
4
+ * Type guard to check if a value is undefined
5
+ * @param value - The value to check
6
+ * @returns True if the value is undefined, false otherwise
7
+ * @example
8
+ * ```ts
9
+ * const value = undefined
10
+ * if (isUndefined(value)) {
11
+ * // TypeScript knows value is undefined here
12
+ * }
13
+ * ```
14
+ */
15
+ export declare function isUndefined(value: unknown): value is undefined;
16
+ /**
17
+ * Type guard to check if a value is an array
18
+ * @param value - The value to check
19
+ * @returns True if the value is an array, false otherwise
20
+ * @example
21
+ * ```ts
22
+ * const value = [1, 2, 3]
23
+ * if (isArray(value)) {
24
+ * // TypeScript knows value is Array<unknown> here
25
+ * value.length // OK
26
+ * value.map(x => x) // OK
27
+ * }
28
+ * ```
29
+ */
30
+ export declare function isArray(value: unknown): value is Array<unknown>;
31
+ /**
32
+ * Type guard to check if a value is a number
33
+ * @param value - The value to check
34
+ * @returns True if the value is a number (including NaN and Infinity), false otherwise
35
+ * @example
36
+ * ```ts
37
+ * const value = 42
38
+ * if (isNumber(value)) {
39
+ * // TypeScript knows value is number here
40
+ * value.toFixed(2) // OK
41
+ * value * 2 // OK
42
+ * }
43
+ * ```
44
+ */
45
+ export declare function isNumber(value: unknown): value is number;
46
+ /**
47
+ * Type guard to check if a value is a string
48
+ * @param value - The value to check
49
+ * @returns True if the value is a string, false otherwise
50
+ * @example
51
+ * ```ts
52
+ * const value = "hello"
53
+ * if (isString(value)) {
54
+ * // TypeScript knows value is string here
55
+ * value.length // OK
56
+ * value.toUpperCase() // OK
57
+ * }
58
+ * ```
59
+ */
60
+ export declare function isString(value: unknown): value is string;
61
+ /**
62
+ * Type guard to check if a value is a boolean
63
+ * @param value - The value to check
64
+ * @returns True if the value is strictly true or false, false otherwise
65
+ * @example
66
+ * ```ts
67
+ * const value = true
68
+ * if (isBoolean(value)) {
69
+ * // TypeScript knows value is boolean here
70
+ * !value // OK
71
+ * value && true // OK
72
+ * }
73
+ * ```
74
+ */
75
+ export declare function isBoolean(value: unknown): value is boolean;
76
+ /**
77
+ * Type guard to check if a value is a function
78
+ * @param value - The value to check
79
+ * @returns True if the value is a callable function, false otherwise
80
+ * @example
81
+ * ```ts
82
+ * const value = () => {}
83
+ * if (isFunction(value)) {
84
+ * // TypeScript knows value is (...args: any[]) => any here
85
+ * value() // OK
86
+ * value.call(null) // OK
87
+ * }
88
+ * ```
89
+ */
90
+ export declare function isFunction(value: unknown): value is (...args: any[]) => any;
91
+ /**
92
+ * Type guard to check if a value is a plain object
93
+ * @param value - The value to check
94
+ * @returns True if the value is a plain object (not null, array, or function), false otherwise
95
+ * @example
96
+ * ```ts
97
+ * const value = { key: 'value' }
98
+ * if (isObject(value)) {
99
+ * // TypeScript knows value is Record<string | number | symbol, unknown> here
100
+ * Object.keys(value) // OK
101
+ * value.key // OK
102
+ * }
103
+ * ```
104
+ */
105
+ export declare function isObject(value: unknown): value is Record<string | number | symbol, unknown>;
106
+ /**
107
+ * Type guard to check if a value is a Three.js Object3D
108
+ * @param value - The value to check
109
+ * @returns True if the value is a Three.js Object3D instance, false otherwise
110
+ * @example
111
+ * ```ts
112
+ * const value = new THREE.Object3D()
113
+ * if (isObject3D(value)) {
114
+ * // TypeScript knows value is Object3D here
115
+ * value.position // OK
116
+ * value.rotation // OK
117
+ * value.scale // OK
118
+ * }
119
+ * ```
120
+ */
121
+ export declare function isObject3D(value: unknown): value is Object3D;
122
+ /**
123
+ * Type guard to check if a value is a Three.js Camera
124
+ * @param value - The value to check
125
+ * @returns True if the value is a Three.js Camera instance, false otherwise
126
+ * @example
127
+ * ```ts
128
+ * const value = new THREE.PerspectiveCamera()
129
+ * if (isCamera(value)) {
130
+ * // TypeScript knows value is Camera here
131
+ * value.fov // OK
132
+ * value.near // OK
133
+ * value.far // OK
134
+ * }
135
+ * ```
136
+ */
137
+ export declare function isCamera(value: unknown): value is Camera;
138
+ /**
139
+ * Type guard to check if a value is a Three.js BufferGeometry
140
+ * @param value - The value to check
141
+ * @returns True if the value is a Three.js BufferGeometry instance, false otherwise
142
+ * @example
143
+ * ```ts
144
+ * const value = new THREE.BufferGeometry()
145
+ * if (isBufferGeometry(value)) {
146
+ * // TypeScript knows value is BufferGeometry here
147
+ * value.attributes // OK
148
+ * value.index // OK
149
+ * value.computeVertexNormals() // OK
150
+ * }
151
+ * ```
152
+ */
153
+ export declare function isBufferGeometry(value: unknown): value is BufferGeometry;
154
+ /**
155
+ * Type guard to check if a value is a Three.js Material
156
+ * @param value - The value to check
157
+ * @returns True if the value is a Three.js Material instance, false otherwise
158
+ * @example
159
+ * ```ts
160
+ * const value = new THREE.MeshStandardMaterial()
161
+ * if (isMaterial(value)) {
162
+ * // TypeScript knows value is Material here
163
+ * value.color // OK
164
+ * value.metalness // OK
165
+ * value.roughness // OK
166
+ * }
167
+ * ```
168
+ */
169
+ export declare function isMaterial(value: unknown): value is Material;
170
+ /**
171
+ * Type guard to check if a value is a Three.js Light
172
+ * @param value - The value to check
173
+ * @returns True if the value is a Three.js Light instance, false otherwise
174
+ * @example
175
+ * ```ts
176
+ * const value = new THREE.DirectionalLight()
177
+ * if (isLight(value)) {
178
+ * // TypeScript knows value is Light here
179
+ * value.intensity // OK
180
+ * value.color // OK
181
+ * value.position // OK
182
+ * }
183
+ * ```
184
+ */
185
+ export declare function isLight(value: unknown): value is Light;
186
+ /**
187
+ * Type guard to check if a value is a Three.js Fog
188
+ * @param value - The value to check
189
+ * @returns True if the value is a Three.js Fog instance, false otherwise
190
+ * @example
191
+ * ```ts
192
+ * const value = new THREE.Fog(0x000000, 1, 1000)
193
+ * if (isFog(value)) {
194
+ * // TypeScript knows value is Fog here
195
+ * value.color // OK
196
+ * value.near // OK
197
+ * value.far // OK
198
+ * }
199
+ * ```
200
+ */
201
+ export declare function isFog(value: unknown): value is Fog;
202
+ /**
203
+ * Type guard to check if a value is a Three.js Scene
204
+ * @param value - The value to check
205
+ * @returns True if the value is a Three.js Scene instance, false otherwise
206
+ * @example
207
+ * ```ts
208
+ * const value = new THREE.Scene()
209
+ * if (isScene(value)) {
210
+ * // TypeScript knows value is Scene here
211
+ * value.children // OK
212
+ * value.add(new THREE.Object3D()) // OK
213
+ * value.remove(new THREE.Object3D()) // OK
214
+ * }
215
+ * ```
216
+ */
217
+ export declare function isScene(value: unknown): value is Scene;
218
+ /**
219
+ * Type guard to check if a value is a TresObject
220
+ * @param value - The value to check
221
+ * @returns True if the value is a TresObject (Object3D | BufferGeometry | Material | Fog), false otherwise
222
+ * @example
223
+ * ```ts
224
+ * const value = new THREE.Mesh()
225
+ * if (isTresObject(value)) {
226
+ * // TypeScript knows value is TresObject here
227
+ * // You can use common properties and methods shared by all TresObjects
228
+ * }
229
+ * ```
230
+ * @remarks
231
+ * TresObject is a union type that represents the core Three.js objects that can be used in TresJS.
232
+ * This includes Object3D, BufferGeometry, Material, and Fog instances.
233
+ */
234
+ export declare function isTresObject(value: unknown): value is TresObject;
235
+ /**
236
+ * Type guard to check if a value is a TresPrimitive
237
+ * @param value - The value to check
238
+ * @returns True if the value is a TresPrimitive instance, false otherwise
239
+ * @example
240
+ * ```ts
241
+ * const value = { isPrimitive: true }
242
+ * if (isTresPrimitive(value)) {
243
+ * // TypeScript knows value is TresPrimitive here
244
+ * // You can use properties and methods specific to TresPrimitives
245
+ * }
246
+ * ```
247
+ * @remarks
248
+ * TresPrimitive is a special type in TresJS that represents primitive objects
249
+ * that can be used directly in the scene without needing to be wrapped in a Three.js object.
250
+ */
251
+ export declare function isTresPrimitive(value: unknown): value is TresPrimitive;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Logger utility for TresJS
3
+ * @module logger
4
+ */
5
+ export declare const isProd: boolean;
6
+ type OneOrMore<T> = {
7
+ 0: T;
8
+ } & Array<T>;
9
+ /**
10
+ * Logs an error message with the TresJS prefix
11
+ * @param args - Arguments to log
12
+ */
13
+ export declare function logError(...args: OneOrMore<any>): void;
14
+ /**
15
+ * Logs a warning message with the TresJS prefix
16
+ * @param args - Arguments to log
17
+ */
18
+ export declare function logWarning(...args: OneOrMore<any>): void;
19
+ /**
20
+ * Logs a message with the TresJS prefix (only in development mode)
21
+ * @param name - Name of the message
22
+ * @param value - Value to log
23
+ */
24
+ export declare function logMessage(name: string, value: any): void;
25
+ export {};
@@ -1,6 +1,7 @@
1
1
  import { Scene } from 'three';
2
2
  import { TresObject } from './../types';
3
3
  export declare function calculateMemoryUsage(object: TresObject | Scene): number;
4
+ export declare function boundedPush<T>(arr: T[], value: T, max: number): void;
4
5
  export declare function bytesToKB(bytes: number): string;
5
6
  export declare function bytesToMB(bytes: number): string;
6
7
  export declare function bytesToGB(bytes: number): string;
@@ -1 +1 @@
1
- export declare function createRetargetingProxy<T extends Record<string | number | symbol, any>, K extends keyof T & string & symbol>(target: T, getters?: Record<string | number | symbol, (t: T) => unknown>, setters?: Partial<Record<K, (val: T[K], t: T, proxy: T, setTarget: (newTarget: T) => void) => boolean>>): T;
1
+ export declare function createRetargetingProxy<T extends Record<string | number | symbol, any>, K extends keyof T & string & symbol>(target: T, getters?: Record<string | number | symbol, (t: T) => unknown>, setters?: Record<K, (val: T[K], t: T, proxy: T, setTarget: (newTarget: T) => void) => boolean>): T;