forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_lines_colors.html
284 lines (185 loc) · 7.28 KB
/
webgl_lines_colors.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - lines - colors</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000000;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 10px; width: 100%;
color: #ffffff;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: center;
z-index:100;
}
a {
color: orange;
text-decoration: none;
}
a:hover {
color: #0080ff;
}
</style>
</head>
<body>
<div id="info">
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - color lines
[<a href="http://en.wikipedia.org/wiki/Hilbert_curve">Hilbert curve</a> thanks to <a href="http://www.openprocessing.org/visuals/?visualID=15599">Thomas Diewald</a>]
</div>
<script src="../build/three.js"></script>
<script src="js/geometries/hilbert3D.js"></script>
<script src="js/WebGL.js"></script>
<script>
if ( WEBGL.isWebGLAvailable() === false ) {
document.body.appendChild( WEBGL.getWebGLErrorMessage() );
}
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var camera, scene, renderer;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 33, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 1000;
scene = new THREE.Scene();
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
//
var hilbertPoints = hilbert3D( new THREE.Vector3( 0, 0, 0 ), 200.0, 1, 0, 1, 2, 3, 4, 5, 6, 7 );
var geometry1 = new THREE.BufferGeometry();
var geometry2 = new THREE.BufferGeometry();
var geometry3 = new THREE.BufferGeometry();
var subdivisions = 6;
var vertices = [];
var colors1 = [];
var colors2 = [];
var colors3 = [];
var point = new THREE.Vector3();
var color = new THREE.Color();
var spline = new THREE.CatmullRomCurve3( hilbertPoints );
for ( var i = 0; i < hilbertPoints.length * subdivisions; i ++ ) {
var t = i / ( hilbertPoints.length * subdivisions );
spline.getPoint( t, point );
vertices.push( point.x, point.y, point.z );
color.setHSL( 0.6, 1.0, Math.max( 0, - point.x / 200 ) + 0.5 );
colors1.push( color.r, color.g, color.b );
color.setHSL( 0.9, 1.0, Math.max( 0, - point.y / 200 ) + 0.5 );
colors2.push( color.r, color.g, color.b );
color.setHSL( i / ( hilbertPoints.length * subdivisions ), 1.0, 0.5 );
colors3.push( color.r, color.g, color.b );
}
geometry1.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
geometry2.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
geometry3.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
geometry1.addAttribute( 'color', new THREE.Float32BufferAttribute( colors1, 3 ) );
geometry2.addAttribute( 'color', new THREE.Float32BufferAttribute( colors2, 3 ) );
geometry3.addAttribute( 'color', new THREE.Float32BufferAttribute( colors3, 3 ) );
//
var geometry4 = new THREE.BufferGeometry();
var geometry5 = new THREE.BufferGeometry();
var geometry6 = new THREE.BufferGeometry();
vertices = [];
colors1 = [];
colors2 = [];
colors3 = [];
for ( var i = 0; i < hilbertPoints.length; i ++ ) {
var point = hilbertPoints[ i ];
vertices.push( point.x, point.y, point.z );
color.setHSL( 0.6, 1.0, Math.max( 0, ( 200 - hilbertPoints[ i ].x ) / 400 ) * 0.5 + 0.5 );
colors1.push( color.r, color.g, color.b );
color.setHSL( 0.3, 1.0, Math.max( 0, ( 200 + hilbertPoints[ i ].x ) / 400 ) * 0.5 );
colors2.push( color.r, color.g, color.b );
color.setHSL( i / hilbertPoints.length, 1.0, 0.5 );
colors3.push( color.r, color.g, color.b );
}
geometry4.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
geometry5.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
geometry6.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
geometry4.addAttribute( 'color', new THREE.Float32BufferAttribute( colors1, 3 ) );
geometry5.addAttribute( 'color', new THREE.Float32BufferAttribute( colors2, 3 ) );
geometry6.addAttribute( 'color', new THREE.Float32BufferAttribute( colors3, 3 ) );
// Create lines and add to scene
var material = new THREE.LineBasicMaterial( { color: 0xffffff, vertexColors: THREE.VertexColors } );
var line, p, scale = 0.3, d = 225;
var parameters = [
[ material, scale * 1.5, [ - d, - d / 2, 0 ], geometry1 ],
[ material, scale * 1.5, [ 0, - d / 2, 0 ], geometry2 ],
[ material, scale * 1.5, [ d, - d / 2, 0 ], geometry3 ],
[ material, scale * 1.5, [ - d, d / 2, 0 ], geometry4 ],
[ material, scale * 1.5, [ 0, d / 2, 0 ], geometry5 ],
[ material, scale * 1.5, [ d, d / 2, 0 ], geometry6 ],
];
for ( var i = 0; i < parameters.length; i ++ ) {
p = parameters[ i ];
line = new THREE.Line( p[ 3 ], p[ 0 ] );
line.scale.x = line.scale.y = line.scale.z = p[ 1 ];
line.position.x = p[ 2 ][ 0 ];
line.position.y = p[ 2 ][ 1 ];
line.position.z = p[ 2 ][ 2 ];
scene.add( line );
}
//
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
document.addEventListener( 'touchmove', onDocumentTouchMove, false );
//
window.addEventListener( 'resize', onWindowResize, false );
}
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() {
camera.position.x += ( mouseX - camera.position.x ) * 0.05;
camera.position.y += ( - mouseY + 200 - camera.position.y ) * 0.05;
camera.lookAt( scene.position );
var time = Date.now() * 0.0005;
for ( var i = 0; i < scene.children.length; i ++ ) {
var object = scene.children[ i ];
if ( object.isLine ) {
object.rotation.y = time * ( i % 2 ? 1 : - 1 );
}
}
renderer.render( scene, camera );
}
</script>
</body>
</html>