pipes-3d 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Matthew
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.
package/README.md ADDED
@@ -0,0 +1,173 @@
1
+ # pipes-3d
2
+
3
+ A framework-agnostic 3D animated pipes visualization using Three.js.
4
+
5
+ ![License](https://img.shields.io/badge/license-MIT-blue.svg)
6
+ ![npm version](https://img.shields.io/npm/v/pipes-3d.svg)
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ npm install pipes-3d three
12
+ ```
13
+
14
+ > **Note:** Three.js is a peer dependency. You must install it alongside pipes-3d.
15
+
16
+ ## Quick Start
17
+
18
+ ### Vanilla JavaScript/TypeScript
19
+
20
+ ```typescript
21
+ import { PipesRenderer } from 'pipes-3d';
22
+
23
+ // Get your container element
24
+ const container = document.getElementById('pipes-container');
25
+
26
+ // Create and start the renderer
27
+ const renderer = new PipesRenderer(container);
28
+ renderer.start();
29
+
30
+ // Later, when you're done:
31
+ renderer.stop(); // Pause the animation
32
+ renderer.reset(); // Clear and restart
33
+ renderer.dispose(); // Clean up and remove from DOM
34
+ ```
35
+
36
+ ### React
37
+
38
+ ```tsx
39
+ import { useEffect, useRef } from 'react';
40
+ import { PipesRenderer } from 'pipes-3d';
41
+
42
+ function PipesVisualization() {
43
+ const containerRef = useRef<HTMLDivElement>(null);
44
+ const rendererRef = useRef<PipesRenderer | null>(null);
45
+
46
+ useEffect(() => {
47
+ if (!containerRef.current) return;
48
+
49
+ // Create and start renderer
50
+ rendererRef.current = new PipesRenderer(containerRef.current);
51
+ rendererRef.current.start();
52
+
53
+ // Cleanup on unmount
54
+ return () => {
55
+ rendererRef.current?.dispose();
56
+ };
57
+ }, []);
58
+
59
+ return <div ref={containerRef} style={{ width: '100%', height: '100vh' }} />;
60
+ }
61
+ ```
62
+
63
+ ### Vue 3
64
+
65
+ ```vue
66
+ <template>
67
+ <div ref="container" class="pipes-container"></div>
68
+ </template>
69
+
70
+ <script setup lang="ts">
71
+ import { ref, onMounted, onUnmounted } from 'vue';
72
+ import { PipesRenderer } from 'pipes-3d';
73
+
74
+ const container = ref<HTMLDivElement>();
75
+ let renderer: PipesRenderer | null = null;
76
+
77
+ onMounted(() => {
78
+ if (container.value) {
79
+ renderer = new PipesRenderer(container.value);
80
+ renderer.start();
81
+ }
82
+ });
83
+
84
+ onUnmounted(() => {
85
+ renderer?.dispose();
86
+ });
87
+ </script>
88
+
89
+ <style scoped>
90
+ .pipes-container {
91
+ width: 100%;
92
+ height: 100vh;
93
+ }
94
+ </style>
95
+ ```
96
+
97
+ ## API Reference
98
+
99
+ ### `PipesRenderer`
100
+
101
+ The main class for rendering animated pipes.
102
+
103
+ #### Constructor
104
+
105
+ ```typescript
106
+ new PipesRenderer(container: HTMLElement)
107
+ ```
108
+
109
+ Creates a new pipes renderer and attaches it to the specified container element.
110
+
111
+ #### Methods
112
+
113
+ ##### `start(): void`
114
+
115
+ Starts the animation loop. Call this after creating the renderer.
116
+
117
+ ##### `stop(): void`
118
+
119
+ Pauses the animation. The scene remains visible but stops updating.
120
+
121
+ ##### `reset(): void`
122
+
123
+ Clears the current scene and generates a new pipe path. Animation continues if it was running.
124
+
125
+ ##### `dispose(): void`
126
+
127
+ Stops the animation, removes event listeners, and removes the canvas from the DOM. Call this when you're done with the renderer to prevent memory leaks.
128
+
129
+ ## Keyboard Controls
130
+
131
+ - **Enter**: Reset the scene and start a new pipe generation
132
+
133
+ ## Browser Support
134
+
135
+ Requires a browser with WebGL support. Works in all modern browsers (Chrome, Firefox, Safari, Edge).
136
+
137
+ ## Development
138
+
139
+ ### Running the Demo
140
+
141
+ ```bash
142
+ # Install dependencies
143
+ npm install
144
+
145
+ # Run development server
146
+ npm run dev
147
+
148
+ # Build the library
149
+ npm run build
150
+
151
+ # Type check
152
+ npm run type-check
153
+
154
+ # Lint
155
+ npm run lint
156
+
157
+ # Format
158
+ npm run format
159
+ ```
160
+
161
+ ## License
162
+
163
+ MIT © [perrymatthew525](https://github.com/perrymatthew525)
164
+
165
+ ## Contributing
166
+
167
+ Contributions are welcome! Please feel free to submit a pull request.
168
+
169
+ ## Links
170
+
171
+ - [GitHub Repository](https://github.com/perrymatthew525/Pipes)
172
+ - [npm Package](https://www.npmjs.com/package/pipes-3d)
173
+ - [Report Issues](https://github.com/perrymatthew525/Pipes/issues)
Binary file
@@ -0,0 +1,83 @@
1
+ import * as THREE_2 from 'three';
2
+
3
+ export declare interface BanDirection {
4
+ posX?: boolean;
5
+ negX?: boolean;
6
+ posY?: boolean;
7
+ negY?: boolean;
8
+ posZ?: boolean;
9
+ negZ?: boolean;
10
+ }
11
+
12
+ export declare type Direction = "+x" | "-x" | "+y" | "-y" | "+z" | "-z";
13
+
14
+ export declare class Elbow {
15
+ private radius;
16
+ private tube;
17
+ constructor(radius: number, tube: number);
18
+ getElbow(startVector: THREE_2.Vector3, startDirection: Direction, endDirection: Direction, color?: THREE_2.Color): THREE_2.Mesh;
19
+ }
20
+
21
+ export declare class PipesRenderer {
22
+ private container;
23
+ private renderer;
24
+ private scene;
25
+ private camera;
26
+ private controls;
27
+ private clock;
28
+ private elbow;
29
+ private animationId;
30
+ private isRunning;
31
+ private readonly MAX_POINTS;
32
+ private readonly MAX_PIPES;
33
+ private readonly RESET_PIPES;
34
+ private readonly PIPE_RADIUS;
35
+ private readonly MIN_PIPE_LENGTH;
36
+ private readonly MAX_PIPE_LENGTH;
37
+ private readonly PIPES_PER_S;
38
+ private readonly BOUNDING_BOX;
39
+ private readonly BOUND_POS_X;
40
+ private readonly BOUND_NEG_X;
41
+ private readonly BOUND_POS_Y;
42
+ private readonly BOUND_NEG_Y;
43
+ private readonly BOUND_POS_Z;
44
+ private readonly BOUND_NEG_Z;
45
+ private randomColor;
46
+ private randomSaturation;
47
+ private drawCount;
48
+ private pipeCount;
49
+ private xPositions;
50
+ private yPositions;
51
+ private zPositions;
52
+ private direction;
53
+ private boundKeyHandler;
54
+ constructor(container: HTMLElement);
55
+ private handleResize;
56
+ private handleKeyDown;
57
+ private pipe;
58
+ private getRandomDirection;
59
+ private generatePoints;
60
+ private resetColor;
61
+ private resetDraw;
62
+ private resetClock;
63
+ private clearScene;
64
+ private animate;
65
+ /**
66
+ * Start the pipes animation
67
+ */
68
+ start(): void;
69
+ /**
70
+ * Stop the pipes animation
71
+ */
72
+ stop(): void;
73
+ /**
74
+ * Reset the scene and start fresh
75
+ */
76
+ reset(): void;
77
+ /**
78
+ * Clean up resources and remove from DOM
79
+ */
80
+ dispose(): void;
81
+ }
82
+
83
+ export { }
@@ -0,0 +1,383 @@
1
+ var I = Object.defineProperty;
2
+ var y = (P, i, s) => i in P ? I(P, i, { enumerable: !0, configurable: !0, writable: !0, value: s }) : P[i] = s;
3
+ var r = (P, i, s) => (y(P, typeof i != "symbol" ? i + "" : i, s), s);
4
+ import * as e from "three";
5
+ import { OrbitControls as m } from "three/addons/controls/OrbitControls.js";
6
+ class f {
7
+ constructor(i, s) {
8
+ r(this, "radius");
9
+ r(this, "tube");
10
+ this.radius = i, this.tube = s;
11
+ }
12
+ getElbow(i, s, n, l) {
13
+ const o = this.radius / 2, h = this.radius / 2, a = this.radius / 2, d = new e.MeshBasicMaterial({ color: 16711680 });
14
+ l !== void 0 && d.color.set(l);
15
+ const t = new e.TorusGeometry(
16
+ this.radius,
17
+ this.tube,
18
+ 16,
19
+ 100,
20
+ Math.PI / 2
21
+ );
22
+ return t.applyMatrix4(
23
+ new e.Matrix4().makeTranslation(-o, -h, 0)
24
+ ), s === "+x" && (n === "+z" && (t.rotateX(-Math.PI / 2), t.applyMatrix4(
25
+ new e.Matrix4().makeTranslation(
26
+ i.x + o,
27
+ i.y,
28
+ i.z + a
29
+ )
30
+ )), n === "-z" && (t.rotateX(Math.PI / 2), t.applyMatrix4(
31
+ new e.Matrix4().makeTranslation(
32
+ i.x + o,
33
+ i.y,
34
+ i.z - a
35
+ )
36
+ )), n === "+y" && (t.rotateX(Math.PI), t.applyMatrix4(
37
+ new e.Matrix4().makeTranslation(
38
+ i.x + o,
39
+ i.y + h,
40
+ i.z
41
+ )
42
+ )), n === "-y" && t.applyMatrix4(
43
+ new e.Matrix4().makeTranslation(
44
+ i.x + o,
45
+ i.y - h,
46
+ i.z
47
+ )
48
+ )), s === "-x" && (n === "+z" && (t.rotateY(Math.PI), t.rotateX(-Math.PI / 2), t.applyMatrix4(
49
+ new e.Matrix4().makeTranslation(
50
+ i.x - o,
51
+ i.y,
52
+ i.z + a
53
+ )
54
+ )), n === "-z" && (t.rotateY(Math.PI), t.rotateX(Math.PI / 2), t.applyMatrix4(
55
+ new e.Matrix4().makeTranslation(
56
+ i.x - o,
57
+ i.y,
58
+ i.z - a
59
+ )
60
+ )), n === "+y" && (t.rotateZ(Math.PI), t.applyMatrix4(
61
+ new e.Matrix4().makeTranslation(
62
+ i.x - o,
63
+ i.y + h,
64
+ i.z
65
+ )
66
+ )), n === "-y" && (t.rotateZ(Math.PI), t.rotateX(Math.PI), t.applyMatrix4(
67
+ new e.Matrix4().makeTranslation(
68
+ i.x - o,
69
+ i.y - h,
70
+ i.z
71
+ )
72
+ ))), s === "+y" && (n === "+x" && (t.rotateZ(Math.PI / 2), t.applyMatrix4(
73
+ new e.Matrix4().makeTranslation(
74
+ i.x + o,
75
+ i.y + h,
76
+ i.z
77
+ )
78
+ )), n === "-x" && (t.rotateY(-Math.PI), t.rotateZ(-Math.PI / 2), t.applyMatrix4(
79
+ new e.Matrix4().makeTranslation(
80
+ i.x - o,
81
+ i.y + h,
82
+ i.z
83
+ )
84
+ )), n === "+z" && (t.rotateZ(Math.PI / 2), t.rotateY(-Math.PI / 2), t.applyMatrix4(
85
+ new e.Matrix4().makeTranslation(
86
+ i.x,
87
+ i.y + h,
88
+ i.z + a
89
+ )
90
+ )), n === "-z" && (t.rotateZ(Math.PI / 2), t.rotateY(Math.PI / 2), t.applyMatrix4(
91
+ new e.Matrix4().makeTranslation(
92
+ i.x,
93
+ i.y + h,
94
+ i.z - a
95
+ )
96
+ ))), s === "-y" && (n === "+x" && (t.rotateZ(Math.PI), t.applyMatrix4(
97
+ new e.Matrix4().makeTranslation(
98
+ i.x + o,
99
+ i.y - h,
100
+ i.z
101
+ )
102
+ )), n === "-x" && (t.rotateZ(Math.PI), t.rotateY(Math.PI), t.applyMatrix4(
103
+ new e.Matrix4().makeTranslation(
104
+ i.x - o,
105
+ i.y - h,
106
+ i.z
107
+ )
108
+ )), n === "+z" && (t.rotateZ(Math.PI), t.rotateY(-Math.PI / 2), t.applyMatrix4(
109
+ new e.Matrix4().makeTranslation(
110
+ i.x,
111
+ i.y - h,
112
+ i.z + a
113
+ )
114
+ )), n === "-z" && (t.rotateZ(Math.PI), t.rotateY(Math.PI / 2), t.applyMatrix4(
115
+ new e.Matrix4().makeTranslation(
116
+ i.x,
117
+ i.y - h,
118
+ i.z - a
119
+ )
120
+ ))), s === "+z" && (n === "+x" && (t.rotateY(-Math.PI / 2), t.rotateZ(Math.PI / 2), t.applyMatrix4(
121
+ new e.Matrix4().makeTranslation(
122
+ i.x + o,
123
+ i.y,
124
+ i.z + a
125
+ )
126
+ )), n === "-x" && (t.rotateX(-Math.PI / 2), t.rotateY(-Math.PI / 2), t.applyMatrix4(
127
+ new e.Matrix4().makeTranslation(
128
+ i.x - o,
129
+ i.y,
130
+ i.z + a
131
+ )
132
+ )), n === "+y" && (t.rotateZ(Math.PI), t.rotateY(Math.PI / 2), t.applyMatrix4(
133
+ new e.Matrix4().makeTranslation(
134
+ i.x,
135
+ i.y + h,
136
+ i.z + a
137
+ )
138
+ )), n === "-y" && (t.rotateY(-Math.PI / 2), t.applyMatrix4(
139
+ new e.Matrix4().makeTranslation(
140
+ i.x,
141
+ i.y - h,
142
+ i.z + a
143
+ )
144
+ ))), s === "-z" && (n === "+x" && (t.rotateX(-Math.PI / 2), t.rotateY(Math.PI / 2), t.applyMatrix4(
145
+ new e.Matrix4().makeTranslation(
146
+ i.x + o,
147
+ i.y,
148
+ i.z - a
149
+ )
150
+ )), n === "-x" && (t.rotateX(Math.PI / 2), t.rotateY(Math.PI / 2), t.applyMatrix4(
151
+ new e.Matrix4().makeTranslation(
152
+ i.x - o,
153
+ i.y,
154
+ i.z - a
155
+ )
156
+ )), n === "+y" && (t.rotateX(Math.PI), t.rotateY(Math.PI / 2), t.applyMatrix4(
157
+ new e.Matrix4().makeTranslation(
158
+ i.x,
159
+ i.y + h,
160
+ i.z - a
161
+ )
162
+ )), n === "-y" && (t.rotateY(Math.PI / 2), t.applyMatrix4(
163
+ new e.Matrix4().makeTranslation(
164
+ i.x,
165
+ i.y - h,
166
+ i.z - a
167
+ )
168
+ ))), new e.Mesh(t, d);
169
+ }
170
+ }
171
+ function _(P, i) {
172
+ return Math.floor(Math.random() * (i - P) + P);
173
+ }
174
+ function x(P, i) {
175
+ return Math.random() * (i - P) + P;
176
+ }
177
+ const M = /* @__PURE__ */ new Map([
178
+ [0, "+x"],
179
+ [1, "-x"],
180
+ [2, "+y"],
181
+ [3, "-y"],
182
+ [4, "+z"],
183
+ [5, "-z"]
184
+ ]);
185
+ class z {
186
+ constructor(i) {
187
+ r(this, "container");
188
+ r(this, "renderer");
189
+ r(this, "scene");
190
+ r(this, "camera");
191
+ r(this, "controls");
192
+ r(this, "clock");
193
+ r(this, "elbow");
194
+ r(this, "animationId", null);
195
+ r(this, "isRunning", !1);
196
+ // Configuration constants
197
+ r(this, "MAX_POINTS", 250);
198
+ r(this, "MAX_PIPES", 6);
199
+ r(this, "RESET_PIPES", !0);
200
+ r(this, "PIPE_RADIUS", 1);
201
+ r(this, "MIN_PIPE_LENGTH", 5);
202
+ r(this, "MAX_PIPE_LENGTH", 20);
203
+ r(this, "PIPES_PER_S", 40);
204
+ r(this, "BOUNDING_BOX", 50);
205
+ r(this, "BOUND_POS_X", 150);
206
+ r(this, "BOUND_NEG_X", -150);
207
+ r(this, "BOUND_POS_Y", this.BOUNDING_BOX);
208
+ r(this, "BOUND_NEG_Y", -this.BOUNDING_BOX);
209
+ r(this, "BOUND_POS_Z", this.BOUNDING_BOX);
210
+ r(this, "BOUND_NEG_Z", -150);
211
+ // State
212
+ r(this, "randomColor", Math.random());
213
+ r(this, "randomSaturation", Math.random());
214
+ r(this, "drawCount", 0);
215
+ r(this, "pipeCount", 0);
216
+ r(this, "xPositions");
217
+ r(this, "yPositions");
218
+ r(this, "zPositions");
219
+ r(this, "direction");
220
+ r(this, "boundKeyHandler");
221
+ r(this, "animate", () => {
222
+ if (!this.isRunning)
223
+ return;
224
+ const i = this.clock.getElapsedTime();
225
+ if (this.drawCount < this.MAX_POINTS - 1 && i > 1 / this.PIPES_PER_S) {
226
+ this.resetClock();
227
+ const s = new e.Vector3(
228
+ this.xPositions[this.drawCount],
229
+ this.yPositions[this.drawCount],
230
+ this.zPositions[this.drawCount]
231
+ ), n = new e.Vector3(
232
+ this.xPositions[this.drawCount + 1],
233
+ this.yPositions[this.drawCount + 1],
234
+ this.zPositions[this.drawCount + 1]
235
+ ), l = M.get(this.direction[this.drawCount + 1]), o = l !== M.get(this.direction[this.drawCount]), h = l !== M.get(this.direction[this.drawCount + 2]), a = this.PIPE_RADIUS;
236
+ switch (l) {
237
+ case "+x":
238
+ o && s.add(new e.Vector3(a, 0, 0)), h && n.add(new e.Vector3(-a, 0, 0));
239
+ break;
240
+ case "-x":
241
+ o && s.add(new e.Vector3(-a, 0, 0)), h && n.add(new e.Vector3(a, 0, 0));
242
+ break;
243
+ case "+y":
244
+ o && s.add(new e.Vector3(0, a, 0)), h && n.add(new e.Vector3(0, -a, 0));
245
+ break;
246
+ case "-y":
247
+ o && s.add(new e.Vector3(0, -a, 0)), h && n.add(new e.Vector3(0, a, 0));
248
+ break;
249
+ case "+z":
250
+ o && s.add(new e.Vector3(0, 0, a)), h && n.add(new e.Vector3(0, 0, -a));
251
+ break;
252
+ case "-z":
253
+ o && s.add(new e.Vector3(0, 0, -a)), h && n.add(new e.Vector3(0, 0, a));
254
+ break;
255
+ }
256
+ this.pipe(s, n), this.drawCount++;
257
+ }
258
+ this.drawCount === this.MAX_POINTS - 1 && (this.pipeCount++, this.resetDraw(), this.pipeCount === this.MAX_PIPES && this.clearScene()), this.controls.update(), this.renderer.render(this.scene, this.camera), this.animationId = requestAnimationFrame(this.animate);
259
+ });
260
+ this.container = i, this.xPositions = new Float32Array(this.MAX_POINTS), this.yPositions = new Float32Array(this.MAX_POINTS), this.zPositions = new Float32Array(this.MAX_POINTS), this.direction = new Array(this.MAX_POINTS), this.renderer = new e.WebGLRenderer({ antialias: !0 }), this.renderer.setSize(i.clientWidth, i.clientHeight), this.renderer.setPixelRatio(window.devicePixelRatio), this.scene = new e.Scene(), this.camera = new e.PerspectiveCamera(
261
+ 75,
262
+ i.clientWidth / i.clientHeight,
263
+ 0.1,
264
+ 1e3
265
+ ), this.camera.position.set(0, 0, 100), this.camera.lookAt(0, 0, 0), this.controls = new m(this.camera, this.renderer.domElement), this.clock = new e.Clock(), this.elbow = new f(this.PIPE_RADIUS, this.PIPE_RADIUS), this.renderer.render(this.scene, this.camera), this.container.appendChild(this.renderer.domElement), this.boundKeyHandler = this.handleKeyDown.bind(this), document.addEventListener("keydown", this.boundKeyHandler), window.addEventListener("resize", this.handleResize.bind(this));
266
+ }
267
+ handleResize() {
268
+ const i = this.container.clientWidth, s = this.container.clientHeight;
269
+ this.camera.aspect = i / s, this.camera.updateProjectionMatrix(), this.renderer.setSize(i, s);
270
+ }
271
+ handleKeyDown(i) {
272
+ i.key === "Enter" && this.reset();
273
+ }
274
+ pipe(i, s) {
275
+ const n = new e.Vector3().subVectors(s, i), l = M.get(this.direction[this.drawCount + 1]), o = M.get(this.direction[this.drawCount + 2]), h = new e.CylinderGeometry(
276
+ this.PIPE_RADIUS,
277
+ this.PIPE_RADIUS,
278
+ n.length(),
279
+ 16
280
+ );
281
+ h.applyMatrix4(
282
+ new e.Matrix4().makeTranslation(0, n.length() / 2, 0)
283
+ ), h.applyMatrix4(new e.Matrix4().makeRotationX(Math.PI / 2));
284
+ const a = new e.MeshBasicMaterial();
285
+ a.color.setHSL(this.randomColor, this.randomSaturation, 0.5);
286
+ const d = new e.Mesh(h, a);
287
+ if (d.position.copy(i), d.lookAt(s), this.scene.add(d), l && o && l !== o) {
288
+ const t = new e.Color(0, 0, 0);
289
+ a.color.getRGB(t);
290
+ const p = this.elbow.getElbow(s, l, o, t);
291
+ this.scene.add(p);
292
+ }
293
+ }
294
+ getRandomDirection(i) {
295
+ const s = [];
296
+ i != null && i.posX || s.push(0), i != null && i.negX || s.push(1), i != null && i.posY || s.push(2), i != null && i.negY || s.push(3), i != null && i.posZ || s.push(4), i != null && i.negZ || s.push(5);
297
+ const n = _(0, s.length);
298
+ return s[n];
299
+ }
300
+ generatePoints() {
301
+ let i = 0, s = 0, n = 0;
302
+ for (let l = 0; l < this.MAX_POINTS; l++) {
303
+ const o = l > 0 ? M.get(this.direction[l - 1]) : void 0, h = {
304
+ posX: !1,
305
+ negX: !1,
306
+ posY: !1,
307
+ negY: !1,
308
+ posZ: !1,
309
+ negZ: !1
310
+ };
311
+ if (l > 0) {
312
+ const t = this.xPositions[l - 1], p = this.yPositions[l - 1], w = this.zPositions[l - 1];
313
+ t > this.BOUND_POS_X && (h.posX = !0), t < this.BOUND_NEG_X && (h.negX = !0), p > this.BOUND_POS_Y && (h.posY = !0), p < this.BOUND_NEG_Y && (h.negY = !0), w > this.BOUND_POS_Z && (h.posZ = !0), w < this.BOUND_NEG_Z && (h.negZ = !0);
314
+ }
315
+ let a = this.getRandomDirection(h);
316
+ if (l > 0 && o)
317
+ for (; o === "-x" && M.get(a) === "+x" || o === "+x" && M.get(a) === "-x" || o === "-y" && M.get(a) === "+y" || o === "+y" && M.get(a) === "-y" || o === "-z" && M.get(a) === "+z" || o === "+z" && M.get(a) === "-z"; )
318
+ a = this.getRandomDirection(h);
319
+ switch (M.get(a)) {
320
+ case "+x":
321
+ i += x(this.MIN_PIPE_LENGTH, this.MAX_PIPE_LENGTH);
322
+ break;
323
+ case "-x":
324
+ i -= x(this.MIN_PIPE_LENGTH, this.MAX_PIPE_LENGTH);
325
+ break;
326
+ case "+y":
327
+ s += x(this.MIN_PIPE_LENGTH, this.MAX_PIPE_LENGTH);
328
+ break;
329
+ case "-y":
330
+ s -= x(this.MIN_PIPE_LENGTH, this.MAX_PIPE_LENGTH);
331
+ break;
332
+ case "+z":
333
+ n += x(this.MIN_PIPE_LENGTH, this.MAX_PIPE_LENGTH);
334
+ break;
335
+ case "-z":
336
+ n -= x(this.MIN_PIPE_LENGTH, this.MAX_PIPE_LENGTH);
337
+ break;
338
+ }
339
+ this.direction[l] = a, this.xPositions[l] = i, this.yPositions[l] = s, this.zPositions[l] = n;
340
+ }
341
+ }
342
+ resetColor() {
343
+ const i = this.randomColor + 0.37;
344
+ this.randomColor = i % 1, this.randomSaturation = x(0, 1);
345
+ }
346
+ resetDraw() {
347
+ this.RESET_PIPES && (this.drawCount = 0, this.resetColor(), this.generatePoints());
348
+ }
349
+ resetClock() {
350
+ this.clock.stop(), this.clock.start();
351
+ }
352
+ clearScene() {
353
+ this.pipeCount = 0, this.scene.clear();
354
+ }
355
+ /**
356
+ * Start the pipes animation
357
+ */
358
+ start() {
359
+ this.isRunning || (this.isRunning = !0, this.clock.start(), this.generatePoints(), this.animate());
360
+ }
361
+ /**
362
+ * Stop the pipes animation
363
+ */
364
+ stop() {
365
+ this.isRunning = !1, this.animationId !== null && (cancelAnimationFrame(this.animationId), this.animationId = null), this.clock.stop();
366
+ }
367
+ /**
368
+ * Reset the scene and start fresh
369
+ */
370
+ reset() {
371
+ this.clearScene(), this.resetDraw();
372
+ }
373
+ /**
374
+ * Clean up resources and remove from DOM
375
+ */
376
+ dispose() {
377
+ this.stop(), document.removeEventListener("keydown", this.boundKeyHandler), window.removeEventListener("resize", this.handleResize.bind(this)), this.controls.dispose(), this.renderer.dispose(), this.container.removeChild(this.renderer.domElement);
378
+ }
379
+ }
380
+ export {
381
+ f as Elbow,
382
+ z as PipesRenderer
383
+ };
@@ -0,0 +1 @@
1
+ (function(d,P){typeof exports=="object"&&typeof module<"u"?P(exports,require("three"),require("three/addons/controls/OrbitControls.js")):typeof define=="function"&&define.amd?define(["exports","three","three/addons/controls/OrbitControls.js"],P):(d=typeof globalThis<"u"?globalThis:d||self,P(d.Pipes3D={},d.THREE,d.THREE.OrbitControls))})(this,function(d,P,x){"use strict";var T=Object.defineProperty;var z=(d,P,x)=>P in d?T(d,P,{enumerable:!0,configurable:!0,writable:!0,value:x}):d[P]=x;var r=(d,P,x)=>(z(d,typeof P!="symbol"?P+"":P,x),x);function m(p){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(p){for(const s in p)if(s!=="default"){const a=Object.getOwnPropertyDescriptor(p,s);Object.defineProperty(e,s,a.get?a:{enumerable:!0,get:()=>p[s]})}}return e.default=p,Object.freeze(e)}const i=m(P);class I{constructor(e,s){r(this,"radius");r(this,"tube");this.radius=e,this.tube=s}getElbow(e,s,a,l){const h=this.radius/2,o=this.radius/2,n=this.radius/2,f=new i.MeshBasicMaterial({color:16711680});l!==void 0&&f.color.set(l);const t=new i.TorusGeometry(this.radius,this.tube,16,100,Math.PI/2);return t.applyMatrix4(new i.Matrix4().makeTranslation(-h,-o,0)),s==="+x"&&(a==="+z"&&(t.rotateX(-Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x+h,e.y,e.z+n))),a==="-z"&&(t.rotateX(Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x+h,e.y,e.z-n))),a==="+y"&&(t.rotateX(Math.PI),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x+h,e.y+o,e.z))),a==="-y"&&t.applyMatrix4(new i.Matrix4().makeTranslation(e.x+h,e.y-o,e.z))),s==="-x"&&(a==="+z"&&(t.rotateY(Math.PI),t.rotateX(-Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x-h,e.y,e.z+n))),a==="-z"&&(t.rotateY(Math.PI),t.rotateX(Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x-h,e.y,e.z-n))),a==="+y"&&(t.rotateZ(Math.PI),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x-h,e.y+o,e.z))),a==="-y"&&(t.rotateZ(Math.PI),t.rotateX(Math.PI),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x-h,e.y-o,e.z)))),s==="+y"&&(a==="+x"&&(t.rotateZ(Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x+h,e.y+o,e.z))),a==="-x"&&(t.rotateY(-Math.PI),t.rotateZ(-Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x-h,e.y+o,e.z))),a==="+z"&&(t.rotateZ(Math.PI/2),t.rotateY(-Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x,e.y+o,e.z+n))),a==="-z"&&(t.rotateZ(Math.PI/2),t.rotateY(Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x,e.y+o,e.z-n)))),s==="-y"&&(a==="+x"&&(t.rotateZ(Math.PI),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x+h,e.y-o,e.z))),a==="-x"&&(t.rotateZ(Math.PI),t.rotateY(Math.PI),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x-h,e.y-o,e.z))),a==="+z"&&(t.rotateZ(Math.PI),t.rotateY(-Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x,e.y-o,e.z+n))),a==="-z"&&(t.rotateZ(Math.PI),t.rotateY(Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x,e.y-o,e.z-n)))),s==="+z"&&(a==="+x"&&(t.rotateY(-Math.PI/2),t.rotateZ(Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x+h,e.y,e.z+n))),a==="-x"&&(t.rotateX(-Math.PI/2),t.rotateY(-Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x-h,e.y,e.z+n))),a==="+y"&&(t.rotateZ(Math.PI),t.rotateY(Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x,e.y+o,e.z+n))),a==="-y"&&(t.rotateY(-Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x,e.y-o,e.z+n)))),s==="-z"&&(a==="+x"&&(t.rotateX(-Math.PI/2),t.rotateY(Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x+h,e.y,e.z-n))),a==="-x"&&(t.rotateX(Math.PI/2),t.rotateY(Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x-h,e.y,e.z-n))),a==="+y"&&(t.rotateX(Math.PI),t.rotateY(Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x,e.y+o,e.z-n))),a==="-y"&&(t.rotateY(Math.PI/2),t.applyMatrix4(new i.Matrix4().makeTranslation(e.x,e.y-o,e.z-n)))),new i.Mesh(t,f)}}function _(p,e){return Math.floor(Math.random()*(e-p)+p)}function y(p,e){return Math.random()*(e-p)+p}const M=new Map([[0,"+x"],[1,"-x"],[2,"+y"],[3,"-y"],[4,"+z"],[5,"-z"]]);class E{constructor(e){r(this,"container");r(this,"renderer");r(this,"scene");r(this,"camera");r(this,"controls");r(this,"clock");r(this,"elbow");r(this,"animationId",null);r(this,"isRunning",!1);r(this,"MAX_POINTS",250);r(this,"MAX_PIPES",6);r(this,"RESET_PIPES",!0);r(this,"PIPE_RADIUS",1);r(this,"MIN_PIPE_LENGTH",5);r(this,"MAX_PIPE_LENGTH",20);r(this,"PIPES_PER_S",40);r(this,"BOUNDING_BOX",50);r(this,"BOUND_POS_X",150);r(this,"BOUND_NEG_X",-150);r(this,"BOUND_POS_Y",this.BOUNDING_BOX);r(this,"BOUND_NEG_Y",-this.BOUNDING_BOX);r(this,"BOUND_POS_Z",this.BOUNDING_BOX);r(this,"BOUND_NEG_Z",-150);r(this,"randomColor",Math.random());r(this,"randomSaturation",Math.random());r(this,"drawCount",0);r(this,"pipeCount",0);r(this,"xPositions");r(this,"yPositions");r(this,"zPositions");r(this,"direction");r(this,"boundKeyHandler");r(this,"animate",()=>{if(!this.isRunning)return;const e=this.clock.getElapsedTime();if(this.drawCount<this.MAX_POINTS-1&&e>1/this.PIPES_PER_S){this.resetClock();const s=new i.Vector3(this.xPositions[this.drawCount],this.yPositions[this.drawCount],this.zPositions[this.drawCount]),a=new i.Vector3(this.xPositions[this.drawCount+1],this.yPositions[this.drawCount+1],this.zPositions[this.drawCount+1]),l=M.get(this.direction[this.drawCount+1]),h=l!==M.get(this.direction[this.drawCount]),o=l!==M.get(this.direction[this.drawCount+2]),n=this.PIPE_RADIUS;switch(l){case"+x":h&&s.add(new i.Vector3(n,0,0)),o&&a.add(new i.Vector3(-n,0,0));break;case"-x":h&&s.add(new i.Vector3(-n,0,0)),o&&a.add(new i.Vector3(n,0,0));break;case"+y":h&&s.add(new i.Vector3(0,n,0)),o&&a.add(new i.Vector3(0,-n,0));break;case"-y":h&&s.add(new i.Vector3(0,-n,0)),o&&a.add(new i.Vector3(0,n,0));break;case"+z":h&&s.add(new i.Vector3(0,0,n)),o&&a.add(new i.Vector3(0,0,-n));break;case"-z":h&&s.add(new i.Vector3(0,0,-n)),o&&a.add(new i.Vector3(0,0,n));break}this.pipe(s,a),this.drawCount++}this.drawCount===this.MAX_POINTS-1&&(this.pipeCount++,this.resetDraw(),this.pipeCount===this.MAX_PIPES&&this.clearScene()),this.controls.update(),this.renderer.render(this.scene,this.camera),this.animationId=requestAnimationFrame(this.animate)});this.container=e,this.xPositions=new Float32Array(this.MAX_POINTS),this.yPositions=new Float32Array(this.MAX_POINTS),this.zPositions=new Float32Array(this.MAX_POINTS),this.direction=new Array(this.MAX_POINTS),this.renderer=new i.WebGLRenderer({antialias:!0}),this.renderer.setSize(e.clientWidth,e.clientHeight),this.renderer.setPixelRatio(window.devicePixelRatio),this.scene=new i.Scene,this.camera=new i.PerspectiveCamera(75,e.clientWidth/e.clientHeight,.1,1e3),this.camera.position.set(0,0,100),this.camera.lookAt(0,0,0),this.controls=new x.OrbitControls(this.camera,this.renderer.domElement),this.clock=new i.Clock,this.elbow=new I(this.PIPE_RADIUS,this.PIPE_RADIUS),this.renderer.render(this.scene,this.camera),this.container.appendChild(this.renderer.domElement),this.boundKeyHandler=this.handleKeyDown.bind(this),document.addEventListener("keydown",this.boundKeyHandler),window.addEventListener("resize",this.handleResize.bind(this))}handleResize(){const e=this.container.clientWidth,s=this.container.clientHeight;this.camera.aspect=e/s,this.camera.updateProjectionMatrix(),this.renderer.setSize(e,s)}handleKeyDown(e){e.key==="Enter"&&this.reset()}pipe(e,s){const a=new i.Vector3().subVectors(s,e),l=M.get(this.direction[this.drawCount+1]),h=M.get(this.direction[this.drawCount+2]),o=new i.CylinderGeometry(this.PIPE_RADIUS,this.PIPE_RADIUS,a.length(),16);o.applyMatrix4(new i.Matrix4().makeTranslation(0,a.length()/2,0)),o.applyMatrix4(new i.Matrix4().makeRotationX(Math.PI/2));const n=new i.MeshBasicMaterial;n.color.setHSL(this.randomColor,this.randomSaturation,.5);const f=new i.Mesh(o,n);if(f.position.copy(e),f.lookAt(s),this.scene.add(f),l&&h&&l!==h){const t=new i.Color(0,0,0);n.color.getRGB(t);const w=this.elbow.getElbow(s,l,h,t);this.scene.add(w)}}getRandomDirection(e){const s=[];e!=null&&e.posX||s.push(0),e!=null&&e.negX||s.push(1),e!=null&&e.posY||s.push(2),e!=null&&e.negY||s.push(3),e!=null&&e.posZ||s.push(4),e!=null&&e.negZ||s.push(5);const a=_(0,s.length);return s[a]}generatePoints(){let e=0,s=0,a=0;for(let l=0;l<this.MAX_POINTS;l++){const h=l>0?M.get(this.direction[l-1]):void 0,o={posX:!1,negX:!1,posY:!1,negY:!1,posZ:!1,negZ:!1};if(l>0){const t=this.xPositions[l-1],w=this.yPositions[l-1],u=this.zPositions[l-1];t>this.BOUND_POS_X&&(o.posX=!0),t<this.BOUND_NEG_X&&(o.negX=!0),w>this.BOUND_POS_Y&&(o.posY=!0),w<this.BOUND_NEG_Y&&(o.negY=!0),u>this.BOUND_POS_Z&&(o.posZ=!0),u<this.BOUND_NEG_Z&&(o.negZ=!0)}let n=this.getRandomDirection(o);if(l>0&&h)for(;h==="-x"&&M.get(n)==="+x"||h==="+x"&&M.get(n)==="-x"||h==="-y"&&M.get(n)==="+y"||h==="+y"&&M.get(n)==="-y"||h==="-z"&&M.get(n)==="+z"||h==="+z"&&M.get(n)==="-z";)n=this.getRandomDirection(o);switch(M.get(n)){case"+x":e+=y(this.MIN_PIPE_LENGTH,this.MAX_PIPE_LENGTH);break;case"-x":e-=y(this.MIN_PIPE_LENGTH,this.MAX_PIPE_LENGTH);break;case"+y":s+=y(this.MIN_PIPE_LENGTH,this.MAX_PIPE_LENGTH);break;case"-y":s-=y(this.MIN_PIPE_LENGTH,this.MAX_PIPE_LENGTH);break;case"+z":a+=y(this.MIN_PIPE_LENGTH,this.MAX_PIPE_LENGTH);break;case"-z":a-=y(this.MIN_PIPE_LENGTH,this.MAX_PIPE_LENGTH);break}this.direction[l]=n,this.xPositions[l]=e,this.yPositions[l]=s,this.zPositions[l]=a}}resetColor(){const e=this.randomColor+.37;this.randomColor=e%1,this.randomSaturation=y(0,1)}resetDraw(){this.RESET_PIPES&&(this.drawCount=0,this.resetColor(),this.generatePoints())}resetClock(){this.clock.stop(),this.clock.start()}clearScene(){this.pipeCount=0,this.scene.clear()}start(){this.isRunning||(this.isRunning=!0,this.clock.start(),this.generatePoints(),this.animate())}stop(){this.isRunning=!1,this.animationId!==null&&(cancelAnimationFrame(this.animationId),this.animationId=null),this.clock.stop()}reset(){this.clearScene(),this.resetDraw()}dispose(){this.stop(),document.removeEventListener("keydown",this.boundKeyHandler),window.removeEventListener("resize",this.handleResize.bind(this)),this.controls.dispose(),this.renderer.dispose(),this.container.removeChild(this.renderer.domElement)}}d.Elbow=I,d.PipesRenderer=E,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "pipes-3d",
3
+ "version": "0.1.0",
4
+ "description": "A 3D animated pipes visualization using Three.js",
5
+ "private": false,
6
+ "type": "module",
7
+ "license": "MIT",
8
+ "keywords": [
9
+ "three",
10
+ "threejs",
11
+ "3d",
12
+ "pipes",
13
+ "visualization",
14
+ "animation",
15
+ "webgl",
16
+ "screensaver"
17
+ ],
18
+ "author": "perrymatthew525",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/perrymatthew525/Pipes.git"
22
+ },
23
+ "homepage": "https://github.com/perrymatthew525/Pipes#readme",
24
+ "bugs": {
25
+ "url": "https://github.com/perrymatthew525/Pipes/issues"
26
+ },
27
+ "main": "./dist/pipes-3d.umd.js",
28
+ "module": "./dist/pipes-3d.es.js",
29
+ "types": "./dist/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "import": "./dist/pipes-3d.es.js",
33
+ "require": "./dist/pipes-3d.umd.js",
34
+ "types": "./dist/index.d.ts"
35
+ }
36
+ },
37
+ "files": [
38
+ "dist"
39
+ ],
40
+ "scripts": {
41
+ "dev": "vite",
42
+ "build": "vite build",
43
+ "build:demo": "vite build --config vite.demo.config.ts",
44
+ "preview": "vite preview",
45
+ "type-check": "vue-tsc --noEmit -p tsconfig.app.json --composite false",
46
+ "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
47
+ "format": "prettier --write src/ lib/",
48
+ "prepublishOnly": "npm run build"
49
+ },
50
+ "peerDependencies": {
51
+ "three": ">=0.150.0"
52
+ },
53
+ "devDependencies": {
54
+ "@rushstack/eslint-patch": "^1.2.0",
55
+ "@tsconfig/node18": "^2.0.1",
56
+ "@types/node": "^18.16.8",
57
+ "@types/three": "^0.152.1",
58
+ "@typescript-eslint/eslint-plugin": "^5.59.11",
59
+ "@typescript-eslint/parser": "^5.59.11",
60
+ "@vitejs/plugin-vue": "^4.2.3",
61
+ "@vue/eslint-config-prettier": "^7.1.0",
62
+ "@vue/eslint-config-typescript": "^11.0.3",
63
+ "@vue/tsconfig": "^0.4.0",
64
+ "eslint": "^8.42.0",
65
+ "eslint-config-prettier": "^8.8.0",
66
+ "eslint-config-standard-with-typescript": "^35.0.0",
67
+ "eslint-plugin-vue": "^9.14.1",
68
+ "npm-run-all": "^4.1.5",
69
+ "prettier": "2.8.8",
70
+ "three": "^0.153.0",
71
+ "typescript": "^5.1.3",
72
+ "vite": "^4.3.5",
73
+ "vite-plugin-dts": "^3.9.1",
74
+ "vue": "^3.3.2",
75
+ "vue-router": "^4.2.0",
76
+ "vue-tsc": "^1.6.4"
77
+ }
78
+ }