123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <!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;
- }
- a {
- color:#0078ff;
- }
- #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> - colors WebGL demo
- [<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/Detector.js"></script>
- <script>
- if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
- var mouseX = 0, mouseY = 0,
- windowHalfX = window.innerWidth / 2,
- windowHalfY = window.innerHeight / 2,
- camera, scene, renderer, material;
- init();
- animate();
- function init() {
- var i, container;
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- 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 );
- container.appendChild( renderer.domElement );
- var hilbertPoints = hilbert3D( new THREE.Vector3( 0,0,0 ), 200.0, 1, 0, 1, 2, 3, 4, 5, 6, 7 );
- // Colors with BufferGeometry
- var buffGeometry0 = new THREE.BufferGeometry(),
- buffGeometry1 = new THREE.BufferGeometry(),
- buffGeometry2 = new THREE.BufferGeometry();
- var subdivisions = 6;
- var position = [],
- colorArray0 = [],
- colorArray1 = [],
- colorArray2 = [];
- var point = new THREE.Vector3();
- var color = new THREE.Color();
- var spline = new THREE.CatmullRomCurve3( hilbertPoints );
- for ( i = 0; i < hilbertPoints.length * subdivisions; i++ ) {
- var t = i / ( hilbertPoints.length * subdivisions );
- spline.getPoint( t, point );
- position.push( point.x, point.y, point.z );
- color.setHSL( 0.6, 1.0, Math.max( 0, - point.x / 200 ) + 0.5 );
- colorArray0.push( color.r, color.g, color.b );
- color.setHSL( 0.9, 1.0, Math.max( 0, - point.y / 200 ) + 0.5 );
- colorArray1.push( color.r, color.g, color.b );
- color.setHSL( i / ( hilbertPoints.length * subdivisions ), 1.0, 0.5 );
- colorArray2.push( color.r, color.g, color.b );
- }
- buffGeometry0.addAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
- buffGeometry1.addAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
- buffGeometry2.addAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
- buffGeometry0.addAttribute( 'color', new THREE.Float32BufferAttribute( colorArray0, 3 ) );
- buffGeometry1.addAttribute( 'color', new THREE.Float32BufferAttribute( colorArray1, 3 ) );
- buffGeometry2.addAttribute( 'color', new THREE.Float32BufferAttribute( colorArray2, 3 ) );
- // Colors with Geometry
- var geometry0 = new THREE.Geometry(),
- geometry2 = new THREE.Geometry(),
- geometry3 = new THREE.Geometry();
- var colors0 = [],
- colors1 = [],
- colors2 = [];
- for ( i = 0; i < hilbertPoints.length; i++ ) {
- geometry0.vertices.push( hilbertPoints[ i ] );
- colors0[ i ] = new THREE.Color( 0xffffff );
- colors0[ i ].setHSL( 0.6, 1.0, Math.max( 0, ( 200 - hilbertPoints[ i ].x ) / 400 ) * 0.5 + 0.5 );
- colors1[ i ] = new THREE.Color( 0xffffff );
- colors1[ i ].setHSL( 0.3, 1.0, Math.max( 0, ( 200 + hilbertPoints[ i ].x ) / 400 ) * 0.5 );
- colors2[ i ] = new THREE.Color( 0xffffff );
- colors2[ i ].setHSL( i / hilbertPoints.length, 1.0, 0.5 );
- }
- geometry2.vertices = geometry3.vertices = geometry0.vertices;
- geometry0.colors = colors0;
- geometry2.colors = colors1;
- geometry3.colors = colors2;
- // Create lines and add to scene
- material = new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 1, linewidth: 3, vertexColors: THREE.VertexColors } );
- var line, p, scale = 0.3, d = 225;
- var parameters = [
- [ material, scale * 1.5, [ - d, - d / 2, 0 ], buffGeometry0 ],
- [ material, scale * 1.5, [ 0, - d / 2, 0 ], buffGeometry1 ],
- [ material, scale * 1.5, [ d, - d / 2, 0 ], buffGeometry2 ],
- [ material, scale * 1.5, [ - d, d / 2, 0 ], geometry0 ],
- [ material, scale * 1.5, [ 0, d / 2, 0 ], geometry2 ],
- [ material, scale * 1.5, [ d, d / 2, 0 ], geometry3 ],
- ];
- for ( 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 );
- }
- // Input Event Listeners
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- document.addEventListener( 'touchstart', onDocumentTouchStart, false );
- document.addEventListener( 'touchmove', onDocumentTouchMove, false );
- // Resize Event Listener
- 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 ) * .05;
- camera.position.y += ( - mouseY + 200 - camera.position.y ) * .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 instanceof THREE.Line ) {
- object.rotation.y = time * ( i % 2 ? 1 : -1 );
- }
- }
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|