-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
195 lines (161 loc) · 5.84 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
185
186
187
188
189
190
191
192
193
194
195
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/89/three.min.js"></script>
<script src="orbitcontrols.js"></script>
<script src="linemesh.js"></script>
<script>
var container, controls;
var camera, scene, renderer, particles, geometry, material, i, h, color, sprite, size;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
const CONNECT_DISTANCE = 400
const MAX_CONNECT_LINES = 4
const NUM_SPHERES = 100
const SPHERE_DIAMETER = 40
const LINE_WIDTH = 8
let spheres = new THREE.Group()
var lineMaterial = new MeshLineMaterial({ lineWidth: LINE_WIDTH, color: new THREE.Color( 'purple') })
var sphereMaterial = new THREE.MeshBasicMaterial({ color: new THREE.Color( 'purple' ) })
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 55, window.innerWidth / window.innerHeight, 2, 3000 );
camera.position.z = 1000;
controls = new THREE.OrbitControls( camera, container )
controls.enableZoom = false
controls.autoRotateSpeed = 1
controls.autoRotate = true
scene = new THREE.Scene();
scene.fog = new THREE.FogExp2( 0x000000, 0.0006 );
addSpheres()
for (var i = 0; i < spheres.children.length; i ++ ) {
let obj = spheres.children[i]
let near = getNearbyObjectsFor(obj, spheres)
if (near.length == 0) continue
let lines = Math.floor(MAX_CONNECT_LINES * Math.random())
for ( var k = 0; k < near.length && k <= lines; k ++ ) {
var lineGeometry = new THREE.Geometry()
lineGeometry.vertices.push( new THREE.Vector3( obj.position.x, obj.position.y, obj.position.z ) );
lineGeometry.vertices.push( new THREE.Vector3( near[k].position.x, near[k].position.y, near[k].position.z ) );
var g = new MeshLine()
g.setGeometry(lineGeometry)
g.verticesNeedUpdate = true
var mesh = new THREE.Mesh(g.geometry, lineMaterial);
scene.add(mesh)
}
}
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
document.addEventListener( 'touchmove', onDocumentTouchMove, false );
window.addEventListener( 'resize', onWindowResize, false );
}
function addSpheres() {
for ( var i = 0; i < NUM_SPHERES; i ++ ) {
var geometry = new THREE.SphereGeometry( SPHERE_DIAMETER, 32, 32 )
var sphere = new THREE.Mesh( geometry, sphereMaterial )
sphere.position.x = 2000 * Math.random() - 1000;
sphere.position.y = 2000 * Math.random() - 1000;
sphere.position.z = 2000 * Math.random() - 1000;
spheres.add( sphere )
}
scene.add( spheres )
}
function hue2rgb( p, q, t ) {
if ( t < 0 ) t += 1;
if ( t > 1 ) t -= 1;
if ( t < 1 / 6 ) return p + ( q - p ) * 6 * t;
if ( t < 1 / 2 ) return q;
if ( t < 2 / 3 ) return p + ( q - p ) * 6 * ( 2 / 3 - t );
return p;
}
function setHSL( h, s, l ) {
let r,g,b
// h,s,l ranges are in 0.0 - 1.0
h = ( ( h % 1 ) + 1 ) % 1 // _Math.euclideanModulo( h, 1 );
s = Math.max( 0, Math.min( 1, s ) )//_Math.clamp( s, 0, 1 );
l = Math.max( 0, Math.min( 1, l ) )//_Math.clamp( l, 0, 1 );
if ( s === 0 ) {
r = g = b = l;
} else {
var p = l <= 0.5 ? l * ( 1 + s ) : l + s - ( l * s );
var q = ( 2 * l ) - p;
r = hue2rgb( q, p, h + 1 / 3 );
g = hue2rgb( q, p, h );
b = hue2rgb( q, p, h - 1 / 3 );
}
return new THREE.Color(r,g,b);
}
function getNearbyObjectsFor(obj, group) {
let objs = []
for ( var j = 0; j < group.children.length; j ++ ) {
if (obj.uuid == group.children[j].uuid) continue
var dist = obj.position.distanceTo(group.children[j].position)
if(dist < CONNECT_DISTANCE){
objs.push(group.children[j])
}
}
return objs
}
function onWindowResize() {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function onDocumentMouseMove( event ) {
mouseX = event.clientX - windowHalfX;
mouseY = event.clientY - windowHalfY;
}
function onDocumentTouchStart( event ) {
if ( event.touches.length == 1 ) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - windowHalfX;
mouseY = event.touches[ 0 ].pageY - windowHalfY;
}
}
function onDocumentTouchMove( event ) {
if ( event.touches.length == 1 ) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - windowHalfX;
mouseY = event.touches[ 0 ].pageY - windowHalfY;
}
}
//
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
controls.update()
var time = Date.now() * 0.00005;
//optional: moving around mouse changes the camera animation
//camera.position.x += ( mouseX - camera.position.x ) * 0.05;
camera.position.y += ( - mouseY - camera.position.y ) * 0.03;
//camera.lookAt( scene.position );
h = ( 360 * ( 1.0 + time ) % 360 ) / 360;
sphereMaterial.color.setHSL( h, 0.5, 0.5 );
lineMaterial.uniforms.color.value = setHSL(h, 0.5, 0.5)
renderer.render( scene, camera );
}
</script>
</body>
</html>