forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_lines_fat.html
329 lines (211 loc) · 7.45 KB
/
webgl_lines_fat.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - lines - fat</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: #000;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
color: #ffffff;
top: 0px;
width: 100%;
padding: 5px;
font-family:Monospace;
font-size:13px;
text-align:center;
}
a {
color: #fff;
}
</style>
</head>
<body>
<div id="container"></div>
<div id="info"><a href="https://threejs.org" target="_blank">three.js</a> - fat lines</div>
<script src="../build/three.js"></script>
<script src="js/controls/OrbitControls.js"></script>
<script src="js/geometries/hilbert3D.js"></script>
<script src="js/WebGL.js"></script>
<script src="js/libs/stats.min.js"></script>
<script src='js/libs/dat.gui.min.js'></script>
<script src='js/lines/LineSegmentsGeometry.js'></script>
<script src='js/lines/LineGeometry.js'></script>
<script src='js/lines/WireframeGeometry2.js'></script>
<script src='js/lines/LineMaterial.js'></script>
<script src='js/lines/LineSegments2.js'></script>
<script src='js/lines/Line2.js'></script>
<script src='js/lines/Wireframe.js'></script>
<script>
if ( WEBGL.isWebGLAvailable() === false ) {
document.body.appendChild( WEBGL.getWebGLErrorMessage() );
}
var line, renderer, scene, camera, camera2, controls;
var line1;
var matLine, matLineBasic, matLineDashed;
var stats;
var gui;
// viewport
var insetWidth;
var insetHeight;
init();
animate();
function init() {
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setClearColor( 0x000000, 0.0 );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( - 40, 0, 60 );
camera2 = new THREE.PerspectiveCamera( 40, 1, 1, 1000 );
camera2.position.copy( camera.position );
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.minDistance = 10;
controls.maxDistance = 500;
// Position and Color Data
var positions = [];
var colors = [];
var points = hilbert3D( new THREE.Vector3( 0, 0, 0 ), 20.0, 1, 0, 1, 2, 3, 4, 5, 6, 7 );
var spline = new THREE.CatmullRomCurve3( points );
var divisions = Math.round( 12 * points.length );
var color = new THREE.Color();
for ( var i = 0, l = divisions; i < l; i ++ ) {
var point = spline.getPoint( i / l );
positions.push( point.x, point.y, point.z );
color.setHSL( i / l, 1.0, 0.5 );
colors.push( color.r, color.g, color.b );
}
// THREE.Line2 ( LineGeometry, LineMaterial )
var geometry = new THREE.LineGeometry();
geometry.setPositions( positions );
geometry.setColors( colors );
matLine = new THREE.LineMaterial( {
color: 0xffffff,
linewidth: 5, // in pixels
vertexColors: THREE.VertexColors,
//resolution: // to be set by renderer, eventually
dashed: false
} );
line = new THREE.Line2( geometry, matLine );
line.computeLineDistances();
line.scale.set( 1, 1, 1 );
scene.add( line );
// THREE.Line ( BufferGeometry, LineBasicMaterial ) - rendered with gl.LINE_STRIP
var geo = new THREE.BufferGeometry();
geo.addAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
geo.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
matLineBasic = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors } );
matLineDashed = new THREE.LineDashedMaterial( { vertexColors: THREE.VertexColors, scale: 2, dashSize: 1, gapSize: 1 } );
line1 = new THREE.Line( geo, matLineBasic );
line1.computeLineDistances();
line1.visible = false;
scene.add( line1 );
//
window.addEventListener( 'resize', onWindowResize, false );
onWindowResize();
stats = new Stats();
document.body.appendChild( stats.dom );
initGui();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
insetWidth = window.innerHeight / 4; // square
insetHeight = window.innerHeight / 4;
camera2.aspect = insetWidth / insetHeight;
camera2.updateProjectionMatrix();
}
function animate() {
requestAnimationFrame( animate );
stats.update();
// main scene
renderer.setClearColor( 0x000000, 0 );
renderer.setViewport( 0, 0, window.innerWidth, window.innerHeight );
// renderer will set this eventually
matLine.resolution.set( window.innerWidth, window.innerHeight ); // resolution of the viewport
renderer.render( scene, camera );
// inset scene
renderer.setClearColor( 0x222222, 1 );
renderer.clearDepth(); // important!
renderer.setScissorTest( true );
renderer.setScissor( 20, 20, insetWidth, insetHeight );
renderer.setViewport( 20, 20, insetWidth, insetHeight );
camera2.position.copy( camera.position );
camera2.quaternion.copy( camera.quaternion );
// renderer will set this eventually
matLine.resolution.set( insetWidth, insetHeight ); // resolution of the inset viewport
renderer.render( scene, camera2 );
renderer.setScissorTest( false );
}
//
function initGui() {
gui = new dat.GUI();
var param = {
'line type': 0,
'width (px)': 5,
'dashed': false,
'dash scale': 1,
'dash / gap': 1
};
gui.add( param, 'line type', { 'LineGeometry': 0, 'gl.LINE': 1 } ).onChange( function ( val ) {
switch ( val ) {
case '0':
line.visible = true;
line1.visible = false;
break;
case '1':
line.visible = false;
line1.visible = true;
break;
}
} );
gui.add( param, 'width (px)', 1, 10 ).onChange( function ( val ) {
matLine.linewidth = val;
} );
gui.add( param, 'dashed' ).onChange( function ( val ) {
matLine.dashed = val;
// dashed is implemented as a defines -- not as a uniform. this could be changed.
// ... or LineDashedMaterial could be implemented as a separate material
// temporary hack - renderer should do this eventually
if ( val ) matLine.defines.USE_DASH = ""; else delete matLine.defines.USE_DASH;
matLine.needsUpdate = true;
line1.material = val ? matLineDashed : matLineBasic;
} );
gui.add( param, 'dash scale', 0.5, 2, 0.1 ).onChange( function ( val ) {
matLine.dashScale = val;
matLineDashed.scale = val;
} );
gui.add( param, 'dash / gap', { '2 : 1': 0, '1 : 1': 1, '1 : 2': 2 } ).onChange( function ( val ) {
switch ( val ) {
case '0':
matLine.dashSize = 2;
matLine.gapSize = 1;
matLineDashed.dashSize = 2;
matLineDashed.gapSize = 1;
break;
case '1':
matLine.dashSize = 1;
matLine.gapSize = 1;
matLineDashed.dashSize = 1;
matLineDashed.gapSize = 1;
break;
case '2':
matLine.dashSize = 1;
matLine.gapSize = 2;
matLineDashed.dashSize = 1;
matLineDashed.gapSize = 2;
break;
}
} );
}
</script>
</body>
</html>