UNPKG

3.99 kBTypeScriptView Raw
1import { Camera } from "../cameras/Camera.js";
2import { JSONMeta, Object3D, Object3DEventMap, Object3DJSON, Object3DJSONObject } from "../core/Object3D.js";
3
4export interface LODJSONObject extends Object3DJSONObject {
5 autoUpdate?: boolean;
6
7 levels: Array<{
8 object: string;
9 distance: number;
10 hysteresis: number;
11 }>;
12}
13
14export interface LODJSON extends Object3DJSON {
15 object: LODJSONObject;
16}
17
18/**
19 * Every level is associated with an object, and rendering can be switched between them at the distances specified
20 * @remarks
21 * Typically you would create, say, three meshes, one for far away (low detail), one for mid range (medium detail) and one for close up (high detail).
22 * @example
23 * ```typescript
24 * const {@link LOD} = new THREE.LOD();
25 * //Create spheres with 3 levels of detail and create new {@link LOD} levels for them
26 * for (let i = 0; i & lt; 3; i++) {
27 * const geometry = new THREE.IcosahedronGeometry(10, 3 - i)
28 * const mesh = new THREE.Mesh(geometry, material);
29 * lod.addLevel(mesh, i * 75);
30 * }
31 * scene.add(lod);
32 * ```
33 * @see Example: {@link https://threejs.org/examples/#webgl_lod | webgl / {@link LOD} }
34 * @see {@link https://threejs.org/docs/index.html#api/en/objects/LOD | Official Documentation}
35 * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/LOD.js | Source}
36 */
37export class LOD<TEventMap extends Object3DEventMap = Object3DEventMap> extends Object3D<TEventMap> {
38 /**
39 * Creates a new {@link LOD}.
40 */
41 constructor();
42
43 /**
44 * Read-only flag to check if a given object is of type {@link LOD}.
45 * @remarks This is a _constant_ value
46 * @defaultValue `true`
47 */
48 readonly isLOD: true;
49
50 /**
51 * @override
52 * @defaultValue `LOD`
53 */
54 override readonly type: string | "LOD";
55
56 /**
57 * An array of level objects
58 */
59 levels: Array<{
60 /** The Object3D to display at this level. */
61 object: Object3D;
62 /** The distance at which to display this level of detail. Expects a `Float`. */
63 distance: number;
64 /** Threshold used to avoid flickering at LOD boundaries, as a fraction of distance. Expects a `Float`. */
65 hysteresis: number;
66 }>;
67
68 /**
69 * Whether the {@link LOD} object is updated automatically by the renderer per frame or not.
70 * If set to `false`, you have to call {@link update | .update()} in the render loop by yourself.
71 * @defaultValue `true`
72 */
73 autoUpdate: boolean;
74
75 /**
76 * Adds a mesh that will display at a certain distance and greater. Typically the further away the distance, the lower the detail on the mesh.
77 *
78 * @param object The Object3D to display at this level.
79 * @param distance The distance at which to display this level of detail. Expects a `Float`. Default `0.0`.
80 * @param hysteresis Threshold used to avoid flickering at LOD boundaries, as a fraction of distance. Expects a `Float`. Default `0.0`.
81 */
82 addLevel(object: Object3D, distance?: number, hysteresis?: number): this;
83
84 /**
85 * Removes an existing level, based on the distance from the camera. Returns `true` when the level has been removed.
86 * Otherwise `false`.
87 * @param distance Distance of the level to delete.
88 */
89 removeLabel(distance: number): boolean;
90
91 /**
92 * Get the currently active {@link LOD} level
93 * @remarks
94 * As index of the levels array.
95 */
96 getCurrentLevel(): number;
97
98 /**
99 * Get a reference to the first {@link THREE.Object3D | Object3D} (mesh) that is greater than {@link distance}.
100 * @param distance Expects a `Float`
101 */
102 getObjectForDistance(distance: number): Object3D | null;
103
104 /**
105 * Set the visibility of each {@link levels | level}'s {@link THREE.Object3D | object} based on distance from the {@link THREE.Camera | camera}.
106 * @param camera
107 */
108 update(camera: Camera): void;
109
110 toJSON(meta?: JSONMeta): LODJSON;
111}