@qy_better_lib/hooks 0.2.12 → 0.2.14
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.
- package/lib/use-three/index.mjs +101 -80
- package/lib/use-three/interface.d.ts +22 -8
- package/lib/use-three/modules/loader.d.ts +11 -12
- package/package.json +1 -1
package/lib/use-three/index.mjs
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { onMounted, onBeforeUnmount } from "vue";
|
|
2
2
|
import { Box3, Vector3, AxesHelper, OrthographicCamera, PerspectiveCamera, PCFSoftShadowMap, SpotLight, PointLight, DirectionalLight, AmbientLight, WebGLRenderer, Vector2, Raycaster, Cache, BoxHelper, Object3D, Scene, Color } from "three";
|
|
3
3
|
import { VRButton } from "three/examples/jsm/webxr/VRButton";
|
|
4
|
-
import { CSS2DObject } from "three/examples/jsm/renderers/CSS2DRenderer
|
|
4
|
+
import { CSS2DObject, CSS2DRenderer } from "three/examples/jsm/renderers/CSS2DRenderer";
|
|
5
5
|
import { on, off } from "@qy_better_lib/core";
|
|
6
6
|
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
|
|
7
|
-
import { CSS2DRenderer } from "three/examples/jsm/renderers/CSS2DRenderer";
|
|
8
7
|
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
|
|
9
8
|
import { MTLLoader } from "three/examples/jsm/loaders/MTLLoader";
|
|
10
9
|
import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader";
|
|
@@ -17,7 +16,7 @@ import { UnrealBloomPass } from "three/examples/jsm/postprocessing/UnrealBloomPa
|
|
|
17
16
|
import { SSAOPass } from "three/examples/jsm/postprocessing/SSAOPass";
|
|
18
17
|
import { FilmPass } from "three/examples/jsm/postprocessing/FilmPass";
|
|
19
18
|
import { FXAAShader } from "three/examples/jsm/shaders/FXAAShader";
|
|
20
|
-
import {
|
|
19
|
+
import { LuminosityShader } from "three/examples/jsm/shaders/LuminosityShader";
|
|
21
20
|
class ObjectToolTip {
|
|
22
21
|
/**
|
|
23
22
|
* 关联的3D对象
|
|
@@ -732,82 +731,15 @@ function use_loader(current_three) {
|
|
|
732
731
|
model: null
|
|
733
732
|
};
|
|
734
733
|
use_draco_loader(loader);
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
(gltf) => {
|
|
738
|
-
loadingState.loaded = true;
|
|
739
|
-
loadingState.progress = 1;
|
|
740
|
-
loadingState.model = gltf.scene;
|
|
741
|
-
current_three.scene?.add(gltf.scene);
|
|
742
|
-
},
|
|
743
|
-
(xhr) => {
|
|
744
|
-
if (xhr.total) {
|
|
745
|
-
loadingState.progress = xhr.loaded / xhr.total;
|
|
746
|
-
options?.onProgress?.(loadingState.progress);
|
|
747
|
-
}
|
|
748
|
-
},
|
|
749
|
-
(error) => {
|
|
750
|
-
loadingState.error = error;
|
|
751
|
-
options?.onError?.(error);
|
|
752
|
-
}
|
|
753
|
-
);
|
|
754
|
-
return loadingState;
|
|
755
|
-
}
|
|
756
|
-
function load_obj_model(url, mtlUrl, options) {
|
|
757
|
-
const loadingState = {
|
|
758
|
-
progress: 0,
|
|
759
|
-
loaded: false,
|
|
760
|
-
error: null,
|
|
761
|
-
model: null
|
|
762
|
-
};
|
|
763
|
-
if (mtlUrl) {
|
|
764
|
-
const mtlLoader = new MTLLoader();
|
|
765
|
-
mtlLoader.load(
|
|
766
|
-
mtlUrl,
|
|
767
|
-
(materials) => {
|
|
768
|
-
materials.preload();
|
|
769
|
-
const objLoader = new OBJLoader();
|
|
770
|
-
objLoader.setMaterials(materials);
|
|
771
|
-
objLoader.load(
|
|
772
|
-
url,
|
|
773
|
-
(object) => {
|
|
774
|
-
loadingState.loaded = true;
|
|
775
|
-
loadingState.progress = 1;
|
|
776
|
-
loadingState.model = object;
|
|
777
|
-
current_three.scene?.add(object);
|
|
778
|
-
},
|
|
779
|
-
(xhr) => {
|
|
780
|
-
if (xhr.total) {
|
|
781
|
-
loadingState.progress = xhr.loaded / xhr.total;
|
|
782
|
-
options?.onProgress?.(loadingState.progress);
|
|
783
|
-
}
|
|
784
|
-
},
|
|
785
|
-
(error) => {
|
|
786
|
-
loadingState.error = error;
|
|
787
|
-
options?.onError?.(error);
|
|
788
|
-
}
|
|
789
|
-
);
|
|
790
|
-
},
|
|
791
|
-
(xhr) => {
|
|
792
|
-
if (xhr.total) {
|
|
793
|
-
loadingState.progress = xhr.loaded / xhr.total * 0.5;
|
|
794
|
-
options?.onProgress?.(loadingState.progress);
|
|
795
|
-
}
|
|
796
|
-
},
|
|
797
|
-
(error) => {
|
|
798
|
-
loadingState.error = error;
|
|
799
|
-
options?.onError?.(error);
|
|
800
|
-
}
|
|
801
|
-
);
|
|
802
|
-
} else {
|
|
803
|
-
const objLoader = new OBJLoader();
|
|
804
|
-
objLoader.load(
|
|
734
|
+
return new Promise((resolve, reject) => {
|
|
735
|
+
loader.load(
|
|
805
736
|
url,
|
|
806
|
-
(
|
|
737
|
+
(gltf) => {
|
|
807
738
|
loadingState.loaded = true;
|
|
808
739
|
loadingState.progress = 1;
|
|
809
|
-
loadingState.model =
|
|
810
|
-
current_three.scene?.add(
|
|
740
|
+
loadingState.model = gltf.scene;
|
|
741
|
+
current_three.scene?.add(gltf.scene);
|
|
742
|
+
resolve(loadingState);
|
|
811
743
|
},
|
|
812
744
|
(xhr) => {
|
|
813
745
|
if (xhr.total) {
|
|
@@ -818,15 +750,103 @@ function use_loader(current_three) {
|
|
|
818
750
|
(error) => {
|
|
819
751
|
loadingState.error = error;
|
|
820
752
|
options?.onError?.(error);
|
|
753
|
+
resolve(loadingState);
|
|
821
754
|
}
|
|
822
755
|
);
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
function load_obj_model(url, mtlUrl, options) {
|
|
759
|
+
const loadingState = {
|
|
760
|
+
progress: 0,
|
|
761
|
+
loaded: false,
|
|
762
|
+
error: null,
|
|
763
|
+
model: null
|
|
764
|
+
};
|
|
765
|
+
return new Promise((resolve, reject) => {
|
|
766
|
+
if (mtlUrl) {
|
|
767
|
+
const mtlLoader = new MTLLoader();
|
|
768
|
+
mtlLoader.load(
|
|
769
|
+
mtlUrl,
|
|
770
|
+
(materials) => {
|
|
771
|
+
materials.preload();
|
|
772
|
+
const objLoader = new OBJLoader();
|
|
773
|
+
objLoader.setMaterials(materials);
|
|
774
|
+
objLoader.load(
|
|
775
|
+
url,
|
|
776
|
+
(object) => {
|
|
777
|
+
loadingState.loaded = true;
|
|
778
|
+
loadingState.progress = 1;
|
|
779
|
+
loadingState.model = object;
|
|
780
|
+
current_three.scene?.add(object);
|
|
781
|
+
resolve(loadingState);
|
|
782
|
+
},
|
|
783
|
+
(xhr) => {
|
|
784
|
+
if (xhr.total) {
|
|
785
|
+
loadingState.progress = xhr.loaded / xhr.total;
|
|
786
|
+
options?.onProgress?.(loadingState.progress);
|
|
787
|
+
}
|
|
788
|
+
},
|
|
789
|
+
(error) => {
|
|
790
|
+
loadingState.error = error;
|
|
791
|
+
options?.onError?.(error);
|
|
792
|
+
resolve(loadingState);
|
|
793
|
+
}
|
|
794
|
+
);
|
|
795
|
+
},
|
|
796
|
+
(xhr) => {
|
|
797
|
+
if (xhr.total) {
|
|
798
|
+
loadingState.progress = xhr.loaded / xhr.total * 0.5;
|
|
799
|
+
options?.onProgress?.(loadingState.progress);
|
|
800
|
+
}
|
|
801
|
+
},
|
|
802
|
+
(error) => {
|
|
803
|
+
loadingState.error = error;
|
|
804
|
+
options?.onError?.(error);
|
|
805
|
+
reject(error);
|
|
806
|
+
}
|
|
807
|
+
);
|
|
808
|
+
} else {
|
|
809
|
+
const objLoader = new OBJLoader();
|
|
810
|
+
objLoader.load(
|
|
811
|
+
url,
|
|
812
|
+
(object) => {
|
|
813
|
+
loadingState.loaded = true;
|
|
814
|
+
loadingState.progress = 1;
|
|
815
|
+
loadingState.model = object;
|
|
816
|
+
current_three.scene?.add(object);
|
|
817
|
+
resolve(loadingState);
|
|
818
|
+
},
|
|
819
|
+
(xhr) => {
|
|
820
|
+
if (xhr.total) {
|
|
821
|
+
loadingState.progress = xhr.loaded / xhr.total;
|
|
822
|
+
options?.onProgress?.(loadingState.progress);
|
|
823
|
+
}
|
|
824
|
+
},
|
|
825
|
+
(error) => {
|
|
826
|
+
loadingState.error = error;
|
|
827
|
+
options?.onError?.(error);
|
|
828
|
+
resolve(loadingState);
|
|
829
|
+
}
|
|
830
|
+
);
|
|
831
|
+
}
|
|
832
|
+
});
|
|
833
|
+
}
|
|
834
|
+
function load_models(models) {
|
|
835
|
+
const promises = [];
|
|
836
|
+
for (const model of models) {
|
|
837
|
+
if (model.type === "gltf") {
|
|
838
|
+
promises.push(load_gltf_model(model.url, model.options));
|
|
839
|
+
} else if (model.type === "obj") {
|
|
840
|
+
promises.push(load_obj_model(model.url, model.mtlUrl, model.options));
|
|
841
|
+
}
|
|
823
842
|
}
|
|
824
|
-
return
|
|
843
|
+
return Promise.all(promises);
|
|
825
844
|
}
|
|
826
845
|
return {
|
|
827
846
|
use_draco_loader,
|
|
828
847
|
load_gltf_model,
|
|
829
|
-
load_obj_model
|
|
848
|
+
load_obj_model,
|
|
849
|
+
load_models
|
|
830
850
|
};
|
|
831
851
|
}
|
|
832
852
|
function use_resources(current_three) {
|
|
@@ -1073,7 +1093,7 @@ function use_effect_composer(current_three) {
|
|
|
1073
1093
|
console.error("Composer is not initialized");
|
|
1074
1094
|
return null;
|
|
1075
1095
|
}
|
|
1076
|
-
const grayscale_pass = new ShaderPass(
|
|
1096
|
+
const grayscale_pass = new ShaderPass(LuminosityShader);
|
|
1077
1097
|
grayscale_pass.renderToScreen = true;
|
|
1078
1098
|
composer.addPass(grayscale_pass);
|
|
1079
1099
|
return grayscale_pass;
|
|
@@ -1129,7 +1149,7 @@ function use_three(options) {
|
|
|
1129
1149
|
const { add_default_lighting, add_ambient_light, add_directional_light, add_point_light, add_spot_light, enable_shadow } = use_lighting(current_three);
|
|
1130
1150
|
const { init_renderer, init_css2_renderer, set_rendering_status, set_target_fps, render, resize, set_container } = use_renderer(current_three);
|
|
1131
1151
|
const { get_select_object, enable_object_drag, disable_object_drag, enable_touch_support } = use_interaction(current_three);
|
|
1132
|
-
const { load_gltf_model, load_obj_model, use_draco_loader } = use_loader(current_three);
|
|
1152
|
+
const { load_gltf_model, load_obj_model, load_models, use_draco_loader } = use_loader(current_three);
|
|
1133
1153
|
const { clear_resources } = use_resources(current_three);
|
|
1134
1154
|
const { enable_effect, enable_fxaa, enable_unreal_bloom, enable_outline, set_out_line } = use_effect_composer(current_three);
|
|
1135
1155
|
function create_box_helper(color = "#00ffff") {
|
|
@@ -1204,6 +1224,7 @@ function use_three(options) {
|
|
|
1204
1224
|
set_target_fps,
|
|
1205
1225
|
load_gltf_model,
|
|
1206
1226
|
load_obj_model,
|
|
1227
|
+
load_models,
|
|
1207
1228
|
enable_object_drag,
|
|
1208
1229
|
disable_object_drag,
|
|
1209
1230
|
enable_touch_support,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ThreeTarget } from '../shared/type';
|
|
2
|
-
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader
|
|
3
|
-
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer
|
|
4
|
-
import { OutlinePass } from 'three/examples/jsm/postprocessing/OutlinePass
|
|
5
|
-
import { UnrealBloomPass } from 'three/examples/jsm/postprocessing/UnrealBloomPass
|
|
2
|
+
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
|
|
3
|
+
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer';
|
|
4
|
+
import { OutlinePass } from 'three/examples/jsm/postprocessing/OutlinePass';
|
|
5
|
+
import { UnrealBloomPass } from 'three/examples/jsm/postprocessing/UnrealBloomPass';
|
|
6
6
|
import { BoxHelper, Object3D } from 'three';
|
|
7
7
|
import { ObjectToolTip, ToolTipOptions } from './toolTip';
|
|
8
8
|
import { OutLineOptions } from './type';
|
|
@@ -70,23 +70,23 @@ export interface UseThree {
|
|
|
70
70
|
* 加载GLTF模型
|
|
71
71
|
* @param url 模型URL
|
|
72
72
|
* @param options 加载选项
|
|
73
|
-
* @returns
|
|
73
|
+
* @returns Promise<any> 加载完成的Promise,解析为加载状态对象
|
|
74
74
|
*/
|
|
75
75
|
load_gltf_model: (url: string, options?: {
|
|
76
76
|
onProgress?: (progress: number) => void;
|
|
77
77
|
onError?: (error: any) => void;
|
|
78
|
-
}) => any
|
|
78
|
+
}) => Promise<any>;
|
|
79
79
|
/**
|
|
80
80
|
* 加载OBJ模型
|
|
81
81
|
* @param url 模型URL
|
|
82
82
|
* @param mtlUrl 材质URL
|
|
83
83
|
* @param options 加载选项
|
|
84
|
-
* @returns
|
|
84
|
+
* @returns Promise<any> 加载完成的Promise,解析为加载状态对象
|
|
85
85
|
*/
|
|
86
86
|
load_obj_model: (url: string, mtlUrl?: string, options?: {
|
|
87
87
|
onProgress?: (progress: number) => void;
|
|
88
88
|
onError?: (error: any) => void;
|
|
89
|
-
}) => any
|
|
89
|
+
}) => Promise<any>;
|
|
90
90
|
/**
|
|
91
91
|
* 启用对象拖拽
|
|
92
92
|
* @param object 要拖拽的对象
|
|
@@ -201,4 +201,18 @@ export interface UseThree {
|
|
|
201
201
|
* @param out_line_options 配置
|
|
202
202
|
*/
|
|
203
203
|
set_out_line: (outline_pass: OutlinePass, objects: Object3D[], out_line_options?: OutLineOptions) => void;
|
|
204
|
+
/**
|
|
205
|
+
* 批量加载模型
|
|
206
|
+
* @param models 模型加载配置数组
|
|
207
|
+
* @returns Promise<any[]> 全部加载完成的Promise,解析为加载状态对象数组
|
|
208
|
+
*/
|
|
209
|
+
load_models: (models: Array<{
|
|
210
|
+
type: 'gltf' | 'obj';
|
|
211
|
+
url: string;
|
|
212
|
+
mtlUrl?: string;
|
|
213
|
+
options?: {
|
|
214
|
+
onProgress?: (progress: number) => void;
|
|
215
|
+
onError?: (error: any) => void;
|
|
216
|
+
};
|
|
217
|
+
}>) => Promise<any[]>;
|
|
204
218
|
}
|
|
@@ -10,19 +10,18 @@ export declare function use_loader(current_three: ThreeTarget): {
|
|
|
10
10
|
load_gltf_model: (url: string, options?: {
|
|
11
11
|
onProgress?: (progress: number) => void;
|
|
12
12
|
onError?: (error: any) => void;
|
|
13
|
-
}) =>
|
|
14
|
-
progress: number;
|
|
15
|
-
loaded: boolean;
|
|
16
|
-
error: null;
|
|
17
|
-
model: any;
|
|
18
|
-
};
|
|
13
|
+
}) => Promise<any>;
|
|
19
14
|
load_obj_model: (url: string, mtlUrl?: string, options?: {
|
|
20
15
|
onProgress?: (progress: number) => void;
|
|
21
16
|
onError?: (error: any) => void;
|
|
22
|
-
}) =>
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
17
|
+
}) => Promise<any>;
|
|
18
|
+
load_models: (models: Array<{
|
|
19
|
+
type: "gltf" | "obj";
|
|
20
|
+
url: string;
|
|
21
|
+
mtlUrl?: string;
|
|
22
|
+
options?: {
|
|
23
|
+
onProgress?: (progress: number) => void;
|
|
24
|
+
onError?: (error: any) => void;
|
|
25
|
+
};
|
|
26
|
+
}>) => Promise<any[]>;
|
|
28
27
|
};
|