-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
184 lines (142 loc) · 6.03 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My first three.js app</title>
<style>
body { margin: 0; }
</style>
</head>
<body>
<script src="js/three.js"></script>
<script type="module">
import { TrackballControls } from 'https://cdn.skypack.dev/[email protected]/examples/jsm/controls/TrackballControls.js';
import { GUI } from 'https://cdn.skypack.dev/[email protected]/examples/jsm/libs/dat.gui.module.js';
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(
window.innerWidth / -2,
window.innerWidth / 2,
window.innerHeight / 2,
window.innerHeight / -2,
-1000,
1000
)
camera.zoom = 20;
camera.updateProjectionMatrix();
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
const material = new THREE.PointsMaterial( {
color: 'white',
size: 0.5,
sizeAttenuation: false
});
//Controls
const controls = new TrackballControls( camera, renderer.domElement );
controls.rotateSpeed = 1.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.noZoom = true;
controls.noPan = false;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
controls.keys = [65, 83, 68];
const zoomDirection = new THREE.Vector3();
const minZoom = 1;
const maxZoom = 1000000;
function mousewheel(event) {
event.preventDefault();
const amount = event.deltaY / 100;
const zoom = camera.zoom - amount;
if (zoom >= minZoom && zoom <= maxZoom) {
const size = new THREE.Vector2();
renderer.getSize(size);
// zoom in to cursor, but zoom out to center of page
const mX = amount > 0 ? 0 : (event.clientX / size.x) * 2 - 1;
const mY = amount > 0 ? 0 : -(event.clientY / size.y) * 2 + 1;
zoomDirection.set(mX, mY, 0.0000001)
.unproject(camera)
.sub(camera.position)
.multiplyScalar(amount / zoom);
camera.position.subVectors(camera.position, zoomDirection);
controls.target.subVectors(controls.target, zoomDirection);
camera.zoom = zoom;
camera.updateProjectionMatrix();
}
}
renderer.domElement.addEventListener('wheel', mousewheel, false);
// Variables
const animate = function () {
requestAnimationFrame( animate );
renderer.render( scene, camera );
};
const randomNumber = (min, max) => {
return Math.random() * (max - min) + min;
}
const getPointInBetweenByPerc = (pointA, pointB, percentage) => {
var dir = pointB.clone().sub(pointA);
var len = dir.length();
dir = dir.normalize().multiplyScalar(len * percentage);
return pointA.clone().add(dir);
}
const sleep = (time) => {
return new Promise(resolve => setTimeout(resolve, time))
}
// Plot original points
const params = {
radius: 20,
segments: 3,
scalar: 0.5,
iterations: 500000
}
const thetaStart = Math.PI / 2;
const thetaLength = Math.PI * 2;
let cancel = false;
let particles = new THREE.Points();
const generate = async function(){
let geometry = new THREE.BufferGeometry();
const vertices = [];;
const points = [];
scene.remove(particles)
const updateGeometry = function () {
const points = new Float32Array(vertices);
geometry.setAttribute( 'position', new THREE.BufferAttribute( points, 3 ) );
particles = new THREE.Points(geometry, material);
}
cancel = true;
for(let i = 0; i <= params.segments; i++){
const segment = thetaStart + i / params.segments * thetaLength;
const x = Math.cos(segment) * params.radius;
const y = Math.sin(segment) * params.radius;
points.push(new THREE.Vector3(x, y, 0))
vertices.push(x, y, 0)
}
// Can be anything
const startPoint = new THREE.Vector3(0, 0);
let prevPoint = startPoint;
cancel = false;
for(let i = 0; i < params.iterations; i++){
if(cancel)
return
// if(i % 10000 == 0){
// await sleep(1);
// updateGeometry();
// }
const roll = Math.floor(randomNumber(0, points.length - 1));
const p = getPointInBetweenByPerc(points[roll], prevPoint, params.scalar);
prevPoint = p;
vertices.push(p.x, p.y, 0);
}
updateGeometry();
scene.add(particles);
}
const gui = new GUI();
gui.add( params, 'radius', 1, 50, 1 ).onChange( generate );
gui.add( params, 'segments', 3, 100, 1 ).onChange( generate );
gui.add( params, 'scalar', 0.01, 1, 0.01).onChange( generate );
gui.add( params, 'iterations', 1000, 10000000, 1000).onChange( generate );
generate();
animate();
</script>
</body>
</html>