webgl_lines_colors.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lines - colors</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - color lines<br/>
  12. <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>
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { GeometryUtils } from './jsm/utils/GeometryUtils.js';
  17. let mouseX = 0, mouseY = 0;
  18. let windowHalfX = window.innerWidth / 2;
  19. let windowHalfY = window.innerHeight / 2;
  20. let camera, scene, renderer;
  21. init();
  22. animate();
  23. function init() {
  24. camera = new THREE.PerspectiveCamera( 33, window.innerWidth / window.innerHeight, 1, 10000 );
  25. camera.position.z = 1000;
  26. scene = new THREE.Scene();
  27. renderer = new THREE.WebGLRenderer( { antialias: true } );
  28. renderer.setPixelRatio( window.devicePixelRatio );
  29. renderer.setSize( window.innerWidth, window.innerHeight );
  30. document.body.appendChild( renderer.domElement );
  31. //
  32. const hilbertPoints = GeometryUtils.hilbert3D( new THREE.Vector3( 0, 0, 0 ), 200.0, 1, 0, 1, 2, 3, 4, 5, 6, 7 );
  33. const geometry1 = new THREE.BufferGeometry();
  34. const geometry2 = new THREE.BufferGeometry();
  35. const geometry3 = new THREE.BufferGeometry();
  36. const subdivisions = 6;
  37. let vertices = [];
  38. let colors1 = [];
  39. let colors2 = [];
  40. let colors3 = [];
  41. const point = new THREE.Vector3();
  42. const color = new THREE.Color();
  43. const spline = new THREE.CatmullRomCurve3( hilbertPoints );
  44. for ( let i = 0; i < hilbertPoints.length * subdivisions; i ++ ) {
  45. const t = i / ( hilbertPoints.length * subdivisions );
  46. spline.getPoint( t, point );
  47. vertices.push( point.x, point.y, point.z );
  48. color.setHSL( 0.6, 1.0, Math.max( 0, - point.x / 200 ) + 0.5 );
  49. colors1.push( color.r, color.g, color.b );
  50. color.setHSL( 0.9, 1.0, Math.max( 0, - point.y / 200 ) + 0.5 );
  51. colors2.push( color.r, color.g, color.b );
  52. color.setHSL( i / ( hilbertPoints.length * subdivisions ), 1.0, 0.5 );
  53. colors3.push( color.r, color.g, color.b );
  54. }
  55. geometry1.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  56. geometry2.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  57. geometry3.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  58. geometry1.setAttribute( 'color', new THREE.Float32BufferAttribute( colors1, 3 ) );
  59. geometry2.setAttribute( 'color', new THREE.Float32BufferAttribute( colors2, 3 ) );
  60. geometry3.setAttribute( 'color', new THREE.Float32BufferAttribute( colors3, 3 ) );
  61. //
  62. const geometry4 = new THREE.BufferGeometry();
  63. const geometry5 = new THREE.BufferGeometry();
  64. const geometry6 = new THREE.BufferGeometry();
  65. vertices = [];
  66. colors1 = [];
  67. colors2 = [];
  68. colors3 = [];
  69. for ( let i = 0; i < hilbertPoints.length; i ++ ) {
  70. const point = hilbertPoints[ i ];
  71. vertices.push( point.x, point.y, point.z );
  72. color.setHSL( 0.6, 1.0, Math.max( 0, ( 200 - hilbertPoints[ i ].x ) / 400 ) * 0.5 + 0.5 );
  73. colors1.push( color.r, color.g, color.b );
  74. color.setHSL( 0.3, 1.0, Math.max( 0, ( 200 + hilbertPoints[ i ].x ) / 400 ) * 0.5 );
  75. colors2.push( color.r, color.g, color.b );
  76. color.setHSL( i / hilbertPoints.length, 1.0, 0.5 );
  77. colors3.push( color.r, color.g, color.b );
  78. }
  79. geometry4.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  80. geometry5.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  81. geometry6.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  82. geometry4.setAttribute( 'color', new THREE.Float32BufferAttribute( colors1, 3 ) );
  83. geometry5.setAttribute( 'color', new THREE.Float32BufferAttribute( colors2, 3 ) );
  84. geometry6.setAttribute( 'color', new THREE.Float32BufferAttribute( colors3, 3 ) );
  85. // Create lines and add to scene
  86. var material = new THREE.LineBasicMaterial( { color: 0xffffff, vertexColors: true } );
  87. let line, p;
  88. const scale = 0.3, d = 225;
  89. const parameters = [
  90. [ material, scale * 1.5, [ - d, - d / 2, 0 ], geometry1 ],
  91. [ material, scale * 1.5, [ 0, - d / 2, 0 ], geometry2 ],
  92. [ material, scale * 1.5, [ d, - d / 2, 0 ], geometry3 ],
  93. [ material, scale * 1.5, [ - d, d / 2, 0 ], geometry4 ],
  94. [ material, scale * 1.5, [ 0, d / 2, 0 ], geometry5 ],
  95. [ material, scale * 1.5, [ d, d / 2, 0 ], geometry6 ],
  96. ];
  97. for ( let i = 0; i < parameters.length; i ++ ) {
  98. p = parameters[ i ];
  99. line = new THREE.Line( p[ 3 ], p[ 0 ] );
  100. line.scale.x = line.scale.y = line.scale.z = p[ 1 ];
  101. line.position.x = p[ 2 ][ 0 ];
  102. line.position.y = p[ 2 ][ 1 ];
  103. line.position.z = p[ 2 ][ 2 ];
  104. scene.add( line );
  105. }
  106. //
  107. document.body.style.touchAction = 'none';
  108. document.body.addEventListener( 'pointermove', onPointerMove );
  109. //
  110. window.addEventListener( 'resize', onWindowResize );
  111. }
  112. function onWindowResize() {
  113. windowHalfX = window.innerWidth / 2;
  114. windowHalfY = window.innerHeight / 2;
  115. camera.aspect = window.innerWidth / window.innerHeight;
  116. camera.updateProjectionMatrix();
  117. renderer.setSize( window.innerWidth, window.innerHeight );
  118. }
  119. //
  120. function onPointerMove( event ) {
  121. if ( event.isPrimary === false ) return;
  122. mouseX = event.clientX - windowHalfX;
  123. mouseY = event.clientY - windowHalfY;
  124. }
  125. //
  126. function animate() {
  127. requestAnimationFrame( animate );
  128. render();
  129. }
  130. function render() {
  131. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  132. camera.position.y += ( - mouseY + 200 - camera.position.y ) * 0.05;
  133. camera.lookAt( scene.position );
  134. const time = Date.now() * 0.0005;
  135. for ( let i = 0; i < scene.children.length; i ++ ) {
  136. const object = scene.children[ i ];
  137. if ( object.isLine ) {
  138. object.rotation.y = time * ( i % 2 ? 1 : - 1 );
  139. }
  140. }
  141. renderer.render( scene, camera );
  142. }
  143. </script>
  144. </body>
  145. </html>