three-zoo 0.0.2 → 0.0.3

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/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "three-zoo",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "zoo of tiny three js tools",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
7
10
  "exports": {
8
11
  ".": {
9
12
  "import": "./dist/index.js",
package/eslint.config.js DELETED
@@ -1,45 +0,0 @@
1
- import tsPlugin from "@typescript-eslint/eslint-plugin";
2
- import tsParser from "@typescript-eslint/parser";
3
-
4
- export default [
5
- {
6
- files: ["src/**/*.ts", "src/**/*.tsx"],
7
- ignores: ["dist/**", "node_modules/**"],
8
- languageOptions: {
9
- parser: tsParser,
10
- parserOptions: {
11
- project: "./tsconfig.json",
12
- tsconfigRootDir: process.cwd(),
13
- sourceType: "module",
14
- },
15
- },
16
- plugins: {
17
- "@typescript-eslint": tsPlugin,
18
- },
19
- rules: {
20
- "no-unused-vars": "off",
21
- "@typescript-eslint/no-unused-vars": [
22
- "error",
23
- { argsIgnorePattern: "^_" },
24
- ],
25
- "no-console": "warn",
26
- eqeqeq: ["error", "always"],
27
-
28
- "@typescript-eslint/explicit-function-return-type": "error",
29
- "@typescript-eslint/explicit-module-boundary-types": "error",
30
- "@typescript-eslint/no-explicit-any": "error",
31
- "@typescript-eslint/no-inferrable-types": "warn",
32
-
33
- "@typescript-eslint/ban-ts-comment": "error",
34
- "@typescript-eslint/no-floating-promises": "error",
35
-
36
- "@typescript-eslint/member-ordering": "warn",
37
- "@typescript-eslint/naming-convention": [
38
- "error",
39
- { selector: "default", format: ["camelCase"] },
40
- { selector: "variableLike", format: ["camelCase", "UPPER_CASE"] },
41
- { selector: "typeLike", format: ["PascalCase"] },
42
- ],
43
- },
44
- },
45
- ];
package/src/Bounds.ts DELETED
@@ -1,22 +0,0 @@
1
- import { Box3, Vector3 } from "three";
2
-
3
- export class Bounds extends Box3 {
4
- private readonly tempVector3: Vector3 = new Vector3();
5
-
6
- public get width(): number {
7
- return this.max.x - this.min.x;
8
- }
9
-
10
- public get height(): number {
11
- return this.max.y - this.min.y;
12
- }
13
-
14
- public get depth(): number {
15
- return this.max.z - this.min.z;
16
- }
17
-
18
- public get diagonal(): number {
19
- this.tempVector3.subVectors(this.max, this.min);
20
- return this.tempVector3.length();
21
- }
22
- }
package/src/Enumerator.ts DELETED
@@ -1,89 +0,0 @@
1
- import { Material, Mesh, Object3D } from "three";
2
-
3
- type Constructor<T> = abstract new (...args: never[]) => T;
4
-
5
- export class Enumerator {
6
- public static getObjectByName(
7
- object: Object3D,
8
- name: string,
9
- ): Object3D | null {
10
- if (object.name === name) return object;
11
-
12
- for (const child of object.children) {
13
- const result = Enumerator.getObjectByName(child, name);
14
- if (result) return result;
15
- }
16
-
17
- return null;
18
- }
19
-
20
- public static getMaterialByName(
21
- object: Object3D,
22
- name: string,
23
- ): Material | null {
24
- if (object instanceof Mesh) {
25
- if (Array.isArray(object.material)) {
26
- for (const material of object.material) {
27
- if (material.name === name) return material;
28
- }
29
- } else if (object.material.name === name) {
30
- return object.material;
31
- }
32
- }
33
-
34
- for (const child of object.children) {
35
- const material = Enumerator.getMaterialByName(child, name);
36
- if (material) return material;
37
- }
38
-
39
- return null;
40
- }
41
-
42
- public static enumerateObjectsByType<T>(
43
- object: Object3D,
44
- type: Constructor<T>,
45
- callback: (instance: T) => void,
46
- ): void {
47
- if (object instanceof type) {
48
- callback(object);
49
- }
50
-
51
- for (const child of object.children) {
52
- Enumerator.enumerateObjectsByType(child, type, callback);
53
- }
54
- }
55
-
56
- public static enumerateMaterials(
57
- object: Object3D,
58
- callback: (material: Material) => void,
59
- ): void {
60
- if (object instanceof Mesh) {
61
- if (Array.isArray(object.material)) {
62
- for (const material of object.material) {
63
- callback(material);
64
- }
65
- } else {
66
- callback(object.material);
67
- }
68
- }
69
-
70
- for (const child of object.children) {
71
- Enumerator.enumerateMaterials(child, callback);
72
- }
73
- }
74
-
75
- public static setShadowRecursive(
76
- object: Object3D,
77
- castShadow = true,
78
- receiveShadow = true,
79
- ): void {
80
- if (object instanceof Mesh || "isMesh" in object) {
81
- (object as Mesh).castShadow = castShadow;
82
- (object as Mesh).receiveShadow = receiveShadow;
83
- }
84
-
85
- for (const child of object.children) {
86
- Enumerator.setShadowRecursive(child, castShadow, receiveShadow);
87
- }
88
- }
89
- }
@@ -1,90 +0,0 @@
1
- import { InstancedMesh, Material, Mesh, Object3D } from "three";
2
- import { Enumerator } from "./Enumerator";
3
-
4
- interface IMeshDescriptor {
5
- meshes: Mesh[];
6
- materials: Material[];
7
- castShadow: boolean;
8
- receiveShadow: boolean;
9
- }
10
-
11
- interface IOptions {
12
- container: Object3D;
13
- filter?: (child: Mesh) => boolean;
14
- disposeOriginal?: boolean;
15
- }
16
-
17
- export class InstanceAssembler {
18
- public static assemble(options: IOptions): void {
19
- const dictionary = new Map<string, IMeshDescriptor>();
20
- const instancedMeshes: InstancedMesh[] = [];
21
-
22
- Enumerator.enumerateObjectsByType(
23
- options.container,
24
- Mesh,
25
- (child: Mesh) => {
26
- if (
27
- child.children.length === 0 &&
28
- (!options.filter || options.filter(child))
29
- ) {
30
- const materials = Array.isArray(child.material)
31
- ? child.material
32
- : [child.material];
33
-
34
- const key = `${child.geometry.uuid}|${materials.map((m) => m.uuid).join(",")}`;
35
- const entry = dictionary.get(key) ?? {
36
- meshes: [],
37
- materials: materials,
38
- castShadow: false,
39
- receiveShadow: false,
40
- };
41
-
42
- if (child.castShadow) entry.castShadow = true;
43
- if (child.receiveShadow) entry.receiveShadow = true;
44
-
45
- dictionary.set(key, entry);
46
- }
47
- },
48
- );
49
-
50
- for (const descriptor of dictionary.values()) {
51
- if (descriptor.meshes.length < 2) continue;
52
- const { meshes, materials, castShadow, receiveShadow } = descriptor;
53
-
54
- const sortedMeshes = meshes.sort((a, b) => a.name.localeCompare(b.name));
55
- const defaultMesh = sortedMeshes[0];
56
-
57
- const instancedMesh = new InstancedMesh(
58
- defaultMesh.geometry,
59
- materials.length === 1 ? materials[0] : materials,
60
- sortedMeshes.length,
61
- );
62
-
63
- instancedMesh.name = defaultMesh.name;
64
- instancedMesh.castShadow = castShadow;
65
- instancedMesh.receiveShadow = receiveShadow;
66
-
67
- for (let i = 0; i < sortedMeshes.length; i++) {
68
- const mesh = sortedMeshes[i];
69
- mesh.updateWorldMatrix(true, false);
70
- instancedMesh.setMatrixAt(i, mesh.matrixWorld);
71
- instancedMesh.userData[mesh.uuid] = mesh.userData;
72
- }
73
-
74
- instancedMesh.instanceMatrix.needsUpdate = true;
75
- instancedMeshes.push(instancedMesh);
76
-
77
- for (const mesh of sortedMeshes) {
78
- mesh.parent?.remove(mesh);
79
- }
80
-
81
- if (options.disposeOriginal === true) {
82
- for (const material of materials) {
83
- material.dispose();
84
- }
85
- }
86
- }
87
-
88
- options.container.add(...instancedMeshes);
89
- }
90
- }
@@ -1,53 +0,0 @@
1
- import { FrontSide, Material, Mesh, Object3D } from "three";
2
- import { Enumerator } from "./Enumerator";
3
- import { InstanceAssembler } from "./InstanceAssembler";
4
-
5
- type IPattern = string | RegExp;
6
-
7
- interface IOptions {
8
- asset: Object3D;
9
- castShadowMeshNames?: IPattern[];
10
- receiveShadowMeshNames?: IPattern[];
11
- transparentMaterialNames?: IPattern[];
12
- noDepthWriteMaterialNames?: IPattern[];
13
- }
14
-
15
- export class SceneProcessor {
16
- public static process(options: IOptions): Object3D[] {
17
- const container = options.asset.clone();
18
- InstanceAssembler.assemble({ container: container });
19
-
20
- Enumerator.enumerateMaterials(container, (material: Material) => {
21
- material.transparent = SceneProcessor.matchesAny(
22
- material.name,
23
- options.transparentMaterialNames,
24
- );
25
- material.depthWrite = !SceneProcessor.matchesAny(
26
- material.name,
27
- options.noDepthWriteMaterialNames,
28
- );
29
- material.side = FrontSide;
30
- material.forceSinglePass = true;
31
- material.depthTest = true;
32
- });
33
-
34
- Enumerator.enumerateObjectsByType(container, Mesh, (child: Mesh) => {
35
- child.castShadow = SceneProcessor.matchesAny(
36
- child.name,
37
- options.castShadowMeshNames,
38
- );
39
- child.receiveShadow = SceneProcessor.matchesAny(
40
- child.name,
41
- options.receiveShadowMeshNames,
42
- );
43
- });
44
-
45
- return container.children;
46
- }
47
-
48
- private static matchesAny(value: string, patterns: IPattern[] = []): boolean {
49
- return patterns.some((p) =>
50
- typeof p === "string" ? value === p : p.test(value),
51
- );
52
- }
53
- }
package/src/Sun.ts DELETED
@@ -1,133 +0,0 @@
1
- import {
2
- Box3,
3
- DirectionalLight,
4
- RGBAFormat,
5
- Spherical,
6
- Texture,
7
- Vector3,
8
- } from "three";
9
-
10
- export class Sun extends DirectionalLight {
11
- private tempVector3D0 = new Vector3();
12
- private tempVector3D1 = new Vector3();
13
- private tempVector3D2 = new Vector3();
14
- private tempVector3D3 = new Vector3();
15
- private tempVector3D4 = new Vector3();
16
- private tempVector3D5 = new Vector3();
17
- private tempVector3D6 = new Vector3();
18
- private tempVector3D7 = new Vector3();
19
-
20
- private tempBox3 = new Box3();
21
- private tempSpherical = new Spherical();
22
-
23
- public get distance(): number {
24
- return this.position.length();
25
- }
26
-
27
- public get elevation(): number {
28
- return this.tempSpherical.setFromVector3(this.position).phi;
29
- }
30
-
31
- public get azimuth(): number {
32
- return this.tempSpherical.setFromVector3(this.position).theta;
33
- }
34
-
35
- public set distance(value: number) {
36
- this.tempSpherical.setFromVector3(this.position);
37
- this.position.setFromSphericalCoords(
38
- value,
39
- this.tempSpherical.phi,
40
- this.tempSpherical.theta,
41
- );
42
- }
43
-
44
- public set elevation(value: number) {
45
- this.tempSpherical.setFromVector3(this.position);
46
- this.position.setFromSphericalCoords(
47
- this.tempSpherical.radius,
48
- value,
49
- this.tempSpherical.theta,
50
- );
51
- }
52
-
53
- public set azimuth(value: number) {
54
- this.tempSpherical.setFromVector3(this.position);
55
- this.position.setFromSphericalCoords(
56
- this.tempSpherical.radius,
57
- this.tempSpherical.phi,
58
- value,
59
- );
60
- }
61
-
62
- public setShadowMapFromBox3(box3: Box3): void {
63
- const camera = this.shadow.camera;
64
-
65
- this.target.updateWorldMatrix(true, false);
66
- this.lookAt(this.target.getWorldPosition(this.tempVector3D0));
67
-
68
- this.updateWorldMatrix(true, false);
69
-
70
- const points: Vector3[] = [
71
- this.tempVector3D0.set(box3.min.x, box3.min.y, box3.min.z),
72
- this.tempVector3D1.set(box3.min.x, box3.min.y, box3.max.z),
73
- this.tempVector3D2.set(box3.min.x, box3.max.y, box3.min.z),
74
- this.tempVector3D3.set(box3.min.x, box3.max.y, box3.max.z),
75
- this.tempVector3D4.set(box3.max.x, box3.min.y, box3.min.z),
76
- this.tempVector3D5.set(box3.max.x, box3.min.y, box3.max.z),
77
- this.tempVector3D6.set(box3.max.x, box3.max.y, box3.min.z),
78
- this.tempVector3D7.set(box3.max.x, box3.max.y, box3.max.z),
79
- ];
80
-
81
- const inverseMatrix = this.matrixWorld.clone().invert();
82
-
83
- for (const point of points) {
84
- point.applyMatrix4(inverseMatrix);
85
- }
86
-
87
- const newBox3 = this.tempBox3.setFromPoints(points);
88
-
89
- camera.left = newBox3.min.x;
90
- camera.bottom = newBox3.min.y;
91
- camera.near = -newBox3.max.z;
92
-
93
- camera.right = newBox3.max.x;
94
- camera.top = newBox3.max.y;
95
- camera.far = -newBox3.min.z;
96
-
97
- camera.updateWorldMatrix(true, false);
98
- camera.updateProjectionMatrix();
99
- }
100
-
101
- public setDirectionFromHDR(texture: Texture, distance = 1): void {
102
- const data = texture.image.data;
103
- const width = texture.image.width;
104
- const height = texture.image.height;
105
-
106
- let maxLuminance = 0;
107
- let maxIndex = 0;
108
-
109
- const step = texture.format === RGBAFormat ? 4 : 3;
110
- for (let i = 0; i < data.length; i += step) {
111
- const r = data[i];
112
- const g = data[i + 1];
113
- const b = data[i + 2];
114
- const luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
115
- if (luminance > maxLuminance) {
116
- maxLuminance = luminance;
117
- maxIndex = i;
118
- }
119
- }
120
-
121
- const pixelIndex = maxIndex / step;
122
- const x = pixelIndex % width;
123
- const y = Math.floor(pixelIndex / width);
124
-
125
- const u = x / width;
126
- const v = y / height;
127
-
128
- const elevation = v * Math.PI;
129
- const azimuth = u * -Math.PI * 2 - Math.PI / 2;
130
-
131
- this.position.setFromSphericalCoords(distance, elevation, azimuth);
132
- }
133
- }
package/src/index.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from "./Bounds";
2
- export * from "./Enumerator";
3
- export * from "./InstanceAssembler";
4
- export * from "./SceneProcessor";
5
- export * from "./Sun";
package/tsconfig.json DELETED
@@ -1,21 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES6",
4
- "module": "ESNext",
5
- "moduleResolution": "Bundler",
6
- "strict": true,
7
- "noImplicitAny": true,
8
- "strictNullChecks": true,
9
- "strictFunctionTypes": true,
10
- "strictBindCallApply": true,
11
- "noImplicitThis": true,
12
- "alwaysStrict": true,
13
- "esModuleInterop": true,
14
- "forceConsistentCasingInFileNames": true,
15
- "skipLibCheck": false,
16
- "outDir": "./dist",
17
- "declaration": true,
18
- "sourceMap": true
19
- },
20
- "include": ["src/**/*"]
21
- }