three-base 0.0.1 → 1.0.2

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.
@@ -0,0 +1,20 @@
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2022-06-14 09:48:00
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2022-06-14 10:01:13
6
+ */
7
+
8
+ import { Mesh } from "three";
9
+
10
+ // 三维空间中的3D文字模型
11
+ export default class Text3D extends Mesh {
12
+
13
+ constructor() {
14
+ super();
15
+ }
16
+
17
+
18
+
19
+
20
+ }
@@ -0,0 +1,119 @@
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2022-04-07 16:42:26
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2023-02-07 11:11:48
6
+ */
7
+
8
+ import { DefaultLoadingManager, EventDispatcher, RepeatWrapping, Texture, TextureLoader } from "three";
9
+ import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader";
10
+ import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
11
+ import { MTLLoader } from "three/examples/jsm/loaders/MTLLoader";
12
+ import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader";
13
+ import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader";
14
+
15
+ // 引入静态资源
16
+ //@ts-ignore
17
+ import dracoWasmWrapperJS from "./draco/draco_wasm_wrapper.js?url";
18
+ //@ts-ignore
19
+ import dracoDecoderJS from "./draco/draco_decoder.js?url";
20
+ //@ts-ignore
21
+ import dracoDecoderWASM from "./draco/draco_decoder.wasm?url";
22
+
23
+ // 加载器
24
+ class ThreeLoader extends EventDispatcher {
25
+
26
+ constructor() {
27
+ super();
28
+
29
+ DefaultLoadingManager.onStart = () => {
30
+ this.onload = false;
31
+ }
32
+
33
+ // 有资源加载完成时通知渲染器
34
+ DefaultLoadingManager.onProgress = () => {
35
+ this.dispatchEvent({ type: "progress" });
36
+ }
37
+
38
+ DefaultLoadingManager.onLoad = () => {
39
+ this.onload = true;
40
+ }
41
+ }
42
+
43
+ // 各向异性
44
+ anisotropy = 1;
45
+
46
+ // 设置各向异性 由渲染器来调用并设置
47
+ setAnisotropy(v: number) {
48
+ this.anisotropy = v;
49
+ };
50
+
51
+ // 纹理对向缓存
52
+ textureMap = new Map() as Map<string, Texture>;
53
+
54
+ // 纹理加载器
55
+ textureLoader = new TextureLoader();
56
+
57
+ // 获取地址对应的单例纹理对象
58
+ getTexture(src: string) {
59
+
60
+ let texture = this.textureMap.get(src);
61
+
62
+ if (!texture) {
63
+ texture = this.textureLoader.load(src, tex => {
64
+ tex.image.crossOrigin = "anonymous";
65
+ });
66
+ this.textureMap.set(src, texture);
67
+ }
68
+
69
+ texture.wrapS = texture.wrapT = RepeatWrapping;
70
+
71
+ texture.anisotropy = this.anisotropy;
72
+
73
+ return texture as Texture;
74
+ };
75
+
76
+ // fbx模型加载器
77
+ fbxLoader = new FBXLoader();
78
+
79
+ //obj 加载器
80
+ objLoader = new OBJLoader();
81
+
82
+ // 材质加载器
83
+ mtlLoader = new MTLLoader();
84
+
85
+ // gltf 加载器
86
+ gltfLoader = (() => {
87
+ const loader = new GLTFLoader();
88
+ const dracoLoader = new class ThreePlusDRACOLoader extends DRACOLoader {
89
+
90
+ _loadLibrary(url: string, responseType: string) {
91
+ //@ts-ignore
92
+ let urlConf = (process.env.NODE_ENV === "development") ? {
93
+ "draco_decoder.js": "draco_decoder.js",
94
+ "draco_wasm_wrapper.js": "draco_wasm_wrapper.js",
95
+ "draco_decoder.wasm": "draco_decoder.wasm"
96
+ } : {
97
+ "draco_decoder.js": dracoWasmWrapperJS,
98
+ "draco_wasm_wrapper.js": dracoDecoderJS,
99
+ "draco_decoder.wasm": dracoDecoderWASM
100
+ }
101
+ //@ts-ignore
102
+ return super._loadLibrary(urlConf[url], responseType);
103
+ }
104
+
105
+ }();
106
+ //@ts-ignore
107
+ dracoLoader.setDecoderPath((process.env.NODE_ENV === "development") ? '/node_modules/three/examples/js/libs/draco/' : '');
108
+ loader.setDRACOLoader(dracoLoader);
109
+ return loader;
110
+ })()
111
+
112
+ // 是否加载完成
113
+ onload = true;
114
+
115
+ }
116
+
117
+ const threeLoader = new ThreeLoader();
118
+ export default threeLoader;
119
+
@@ -0,0 +1,84 @@
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2022-02-24 22:49:42
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2022-06-21 16:45:33
6
+ */
7
+
8
+ import { Box3, Clock, EventDispatcher, Object3D, Vector3 } from "three";
9
+
10
+ class ThreeUtils extends EventDispatcher {
11
+
12
+ constructor() {
13
+ super();
14
+
15
+ this.requestAnimationFrame();
16
+ }
17
+
18
+ isProd = process.env.NODE_ENV === "production";
19
+
20
+ isDev = process.env.NODE_ENV === "development";
21
+
22
+ timeTemp = performance.now();
23
+
24
+ accumulate = 0;// 事件分发次数
25
+
26
+ box3 = new Box3();
27
+ size = new Vector3();
28
+ center = new Vector3();
29
+
30
+ clock = new Clock();
31
+
32
+ requestAnimationFrame() {
33
+ this.accumulate += 1;
34
+ const timeTemp = this.timeTemp = performance.now();
35
+ this.dispatchEvent({
36
+ type: "requestAnimationFrame",
37
+ timeTemp,
38
+ accumulate: this.accumulate
39
+ });
40
+
41
+ setTimeout(() => {
42
+ this.requestAnimationFrame();
43
+ }, 45 - this.clock.getDelta());
44
+ }
45
+
46
+ // 获取模型大小
47
+ getSize(object3d: Object3D) {
48
+ this.box3.setFromObject(object3d);
49
+ this.box3.getSize(this.size);
50
+ return this.size;
51
+ }
52
+
53
+ // 模型对齐底面
54
+ alignButton(object3d: Object3D, y = 0) {
55
+ this.box3.setFromObject(object3d);
56
+ this.box3.getCenter(this.center);
57
+ this.box3.getSize(this.size);
58
+ object3d.position.y -= (this.center.y - (this.size.y * 0.5));
59
+ object3d.position.y += y;
60
+ object3d.updateMatrixWorld(true);
61
+ }
62
+
63
+ // 居中对象
64
+ centerObject3D(object3d: Object3D) {
65
+ this.box3.setFromObject(object3d);
66
+ this.box3.getCenter(this.center);
67
+ object3d.position.sub(this.center);
68
+ object3d.updateMatrixWorld(true);
69
+ }
70
+
71
+ //缩放至指定大小
72
+ scaleTo(object3d: Object3D, size: number = 1) {
73
+ this.box3.setFromObject(object3d);
74
+ this.box3.getSize(this.size);
75
+ const scale = size / Math.max(this.size.x, this.size.y, this.size.z);
76
+ object3d.scale.multiplyScalar(scale);
77
+ object3d.updateMatrixWorld(true);
78
+ }
79
+
80
+ }
81
+
82
+ const threeUtils = new ThreeUtils();
83
+
84
+ export default threeUtils;
package/lib/type.ts ADDED
@@ -0,0 +1,11 @@
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2021-12-20 14:21:54
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2022-01-05 14:21:42
6
+ */
7
+
8
+ declare module '*?url' {
9
+ const src: string
10
+ export default src
11
+ }
package/package.json CHANGED
@@ -1,27 +1,45 @@
1
- {
2
- "name": "three-base",
3
- "version": "0.0.1",
4
- "description": "three base",
5
- "main": "./dist/index.js",
6
- "scripts": {
7
- "build": "tsc",
8
- "example": "tsc && node ./example/index.js"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "https://github.com/fengtianxi001/NPM-THREE.git"
13
- },
14
- "author": "fengtianxi001",
15
- "license": "ISC",
16
- "homepage": "https://github.com/fengtianxi001/NPM-THREE",
17
- "dependencies": {
18
- "@tweenjs/tween.js": "^18.6.4",
19
- "@types/three": "^0.141.0",
20
- "three": "^0.141.0",
21
- "tsc": "^2.0.4",
22
- "typescript": "^4.7.3"
23
- },
24
- "devDependencies": {
25
- "@types/node": "^17.0.8"
26
- }
27
- }
1
+ {
2
+ "name": "three-base",
3
+ "version": "1.0.2",
4
+ "description": "three 3d three-base",
5
+ "keywords": [
6
+ "three",
7
+ "3d",
8
+ "three-base",
9
+ "three-base",
10
+ "xsh"
11
+ ],
12
+ "main": "lib/index.ts",
13
+ "module": "lib/index.ts",
14
+ "types": "lib/index.ts",
15
+ "files": [
16
+ "declare.d.ts",
17
+ "lib/*.js",
18
+ "lib/*.ts",
19
+ "lib/**/*.ts",
20
+ "lib/**/*.js",
21
+ "lib/**/*.tsx",
22
+ "lib/**/*.css",
23
+ "lib/**/*.jpg",
24
+ "lib/**/*.png",
25
+ "lib/**/*.jpeg",
26
+ "lib/**/*.svg",
27
+ "lib/**/*.obj",
28
+ "lib/**/*.gltf",
29
+ "lib/**/*.fbx",
30
+ "lib/**/*.glb",
31
+ "lib/**/*.json",
32
+ "lib/**/*.wasm"
33
+ ],
34
+ "license": "ISC",
35
+ "devDependencies": {
36
+ "@types/three": "^0"
37
+ },
38
+ "dependencies": {
39
+ "apng-js": "^1",
40
+ "element-resize-event": "^3",
41
+ "lodash": "^4",
42
+ "three": "^0",
43
+ "three-spritetext": "^1"
44
+ }
45
+ }
package/.gitattributes DELETED
@@ -1,2 +0,0 @@
1
- # Auto detect text files and perform LF normalization
2
- * text=auto
@@ -1,22 +0,0 @@
1
- name: Node.js Package
2
-
3
- on:
4
- push:
5
- branches:
6
- - main
7
-
8
- jobs:
9
- publish-npm:
10
- runs-on: ubuntu-latest
11
- steps:
12
- - uses: actions/checkout@v2
13
- - uses: actions/setup-node@v2
14
- with:
15
- node-version: 14
16
- registry-url: https://registry.npmjs.org/
17
- - run: npm install
18
- - run: npm build
19
- - run: npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN
20
- - run: npm publish
21
- env:
22
- NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 肛肠科冯主任
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,27 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8" />
5
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
6
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
- <title>Document</title>
8
- <style>
9
- * {
10
- padding: 0;
11
- margin: 0;
12
- }
13
- #app {
14
- width: 100vw;
15
- height: 100vh;
16
- }
17
- </style>
18
- </head>
19
- <body>
20
- <div id="app"></div>
21
- </body>
22
- <script type="module">
23
- import Three from "../dist/index.js";
24
- const app = document.querySelector("#app");
25
- const three = new Three(app);
26
- </script>
27
- </html>
package/example/index.js DELETED
File without changes
package/lib/index.ts DELETED
@@ -1,112 +0,0 @@
1
- import * as THREE from "three";
2
- import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
3
- import { CSS2DRenderer } from "three/examples/jsm/renderers/CSS2DRenderer";
4
- import { GLTFLoader, GLTF } from "three/examples/jsm/loaders/GLTFLoader";
5
- import { AnimationClip } from "three";
6
- import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
7
- import Stats from "three/examples/jsm/libs/stats.module.js";
8
- //@ts-ignore
9
- import TWEEN from "@tweenjs/tween.js";
10
- class Three {
11
- scene: THREE.Scene;
12
- camera: THREE.PerspectiveCamera;
13
- renderer: THREE.WebGLRenderer;
14
- control: OrbitControls;
15
- CSSRender: CSS2DRenderer;
16
- element: HTMLElement;
17
- mixers: THREE.AnimationMixer[];
18
- composers: Array<EffectComposer>;
19
- clock: THREE.Clock;
20
- renderMixins: Array<Function>;
21
- stats: any;
22
- constructor(element: HTMLElement) {
23
- this.element = element;
24
- this.scene = this.initScene();
25
- this.camera = this.initCamera(element);
26
- this.renderer = this.initRenderer(element);
27
- this.CSSRender = this.initCSSRender(element);
28
- this.control = this.initControl();
29
- this.stats = null;
30
- this.mixers = [];
31
- this.composers = [];
32
- this.renderMixins = [];
33
- this.clock = new THREE.Clock();
34
- }
35
- initScene(): THREE.Scene {
36
- const scene = new THREE.Scene();
37
- return scene;
38
- }
39
- initCSSRender(element: HTMLElement): CSS2DRenderer {
40
- const CSSRender = new CSS2DRenderer();
41
- CSSRender.setSize(element.offsetWidth, element.offsetHeight);
42
- CSSRender.domElement.style.position = "absolute";
43
- CSSRender.domElement.style.top = "0px";
44
- element.appendChild(CSSRender.domElement);
45
- return CSSRender;
46
- }
47
- initCamera(element: HTMLElement): THREE.PerspectiveCamera {
48
- const fov = 20;
49
- const aspect = element.offsetWidth / element.offsetHeight;
50
- const near = 0.1;
51
- const far = 5 * 1000;
52
- const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
53
- camera.position.set(0, 0, 3);
54
- return camera;
55
- }
56
- initRenderer(element: HTMLElement) {
57
- const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
58
- renderer.outputEncoding = THREE.sRGBEncoding;
59
- renderer.localClippingEnabled = true;
60
- renderer.shadowMap.enabled = true;
61
- renderer.setSize(element.offsetWidth, element.offsetHeight);
62
- element.appendChild(renderer.domElement);
63
- return renderer;
64
- }
65
- initControl() {
66
- const control = new OrbitControls(this.camera, this.CSSRender.domElement);
67
- return control;
68
- }
69
- initHelp(size: number) {
70
- const axesHelper = new THREE.AxesHelper(size);
71
- this.scene.add(axesHelper);
72
- }
73
- initStats() {
74
- //@ts-ignore
75
- this.stats = new Stats();
76
- this.element.appendChild(this.stats.dom);
77
- }
78
- loadGLTF(url: string, onProgress = (progress: number) => {}): Promise<GLTF> {
79
- const loader = new GLTFLoader();
80
- return new Promise<GLTF>((resolve) => {
81
- loader.load(
82
- url,
83
- (object: any) => resolve(object),
84
- (xhr: any) => onProgress(Number((xhr.loaded / xhr.total) * 100))
85
- );
86
- });
87
- }
88
- playModelAnimate(
89
- mesh: THREE.Object3D | THREE.AnimationObjectGroup,
90
- animations: AnimationClip[],
91
- animationName: string
92
- ) {
93
- const mixer = new THREE.AnimationMixer(mesh);
94
- const clip = THREE.AnimationClip.findByName(animations, animationName);
95
- if (!clip) return void 0;
96
- const action = mixer.clipAction(clip);
97
- action.play();
98
- this.mixers.push(mixer);
99
- }
100
- render() {
101
- const delta = new THREE.Clock().getDelta();
102
- this.renderer.render(this.scene, this.camera);
103
- requestAnimationFrame(() => this.render());
104
- const mixerUpdateDelta = this.clock.getDelta();
105
- this.mixers.forEach((mixer) => mixer.update(mixerUpdateDelta));
106
- this.composers.forEach((composer) => composer.render(delta));
107
- this.renderMixins.forEach((mixin) => mixin());
108
- TWEEN.update();
109
- this.stats && this.stats?.update();
110
- }
111
- }
112
- export default Three;
package/tsconfig.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "include": ["lib"],
3
- "moduleResolution": "node",
4
- "compilerOptions": {
5
- "target": "es5",
6
- "module": "es6",
7
- "declaration": true,
8
- "outDir": "./dist",
9
- "strict": true
10
- }
11
- }