webgl_lines_colors.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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="https://openprocessing.org/user/5654">Thomas Diewald</a>
  13. </div>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import * as GeometryUtils from 'three/addons/utils/GeometryUtils.js';
  28. let mouseX = 0, mouseY = 0;
  29. let windowHalfX = window.innerWidth / 2;
  30. let windowHalfY = window.innerHeight / 2;
  31. let camera, scene, renderer;
  32. init();
  33. animate();
  34. function init() {
  35. camera = new THREE.PerspectiveCamera( 33, window.innerWidth / window.innerHeight, 1, 10000 );
  36. camera.position.z = 1000;
  37. scene = new THREE.Scene();
  38. renderer = new THREE.WebGLRenderer( { antialias: true } );
  39. renderer.setPixelRatio( window.devicePixelRatio );
  40. renderer.setSize( window.innerWidth, window.innerHeight );
  41. document.body.appendChild( renderer.domElement );
  42. //
  43. const hilbertPoints = GeometryUtils.hilbert3D( new THREE.Vector3( 0, 0, 0 ), 200.0, 1, 0, 1, 2, 3, 4, 5, 6, 7 );
  44. const geometry1 = new THREE.BufferGeometry();
  45. const geometry2 = new THREE.BufferGeometry();
  46. const geometry3 = new THREE.BufferGeometry();
  47. const subdivisions = 6;
  48. let vertices = [];
  49. let colors1 = [];
  50. let colors2 = [];
  51. let colors3 = [];
  52. const point = new THREE.Vector3();
  53. const color = new THREE.Color();
  54. const spline = new THREE.CatmullRomCurve3( hilbertPoints );
  55. for ( let i = 0; i < hilbertPoints.length * subdivisions; i ++ ) {
  56. const t = i / ( hilbertPoints.length * subdivisions );
  57. spline.getPoint( t, point );
  58. vertices.push( point.x, point.y, point.z );
  59. color.setHSL( 0.6, 1.0, Math.max( 0, - point.x / 200 ) + 0.5, THREE.SRGBColorSpace );
  60. colors1.push( color.r, color.g, color.b );
  61. color.setHSL( 0.9, 1.0, Math.max( 0, - point.y / 200 ) + 0.5, THREE.SRGBColorSpace );
  62. colors2.push( color.r, color.g, color.b );
  63. color.setHSL( i / ( hilbertPoints.length * subdivisions ), 1.0, 0.5, THREE.SRGBColorSpace );
  64. colors3.push( color.r, color.g, color.b );
  65. }
  66. geometry1.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  67. geometry2.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  68. geometry3.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  69. geometry1.setAttribute( 'color', new THREE.Float32BufferAttribute( colors1, 3 ) );
  70. geometry2.setAttribute( 'color', new THREE.Float32BufferAttribute( colors2, 3 ) );
  71. geometry3.setAttribute( 'color', new THREE.Float32BufferAttribute( colors3, 3 ) );
  72. //
  73. const geometry4 = new THREE.BufferGeometry();
  74. const geometry5 = new THREE.BufferGeometry();
  75. const geometry6 = new THREE.BufferGeometry();
  76. vertices = [];
  77. colors1 = [];
  78. colors2 = [];
  79. colors3 = [];
  80. for ( let i = 0; i < hilbertPoints.length; i ++ ) {
  81. const point = hilbertPoints[ i ];
  82. vertices.push( point.x, point.y, point.z );
  83. color.setHSL( 0.6, 1.0, Math.max( 0, ( 200 - hilbertPoints[ i ].x ) / 400 ) * 0.5 + 0.5, THREE.SRGBColorSpace );
  84. colors1.push( color.r, color.g, color.b );
  85. color.setHSL( 0.3, 1.0, Math.max( 0, ( 200 + hilbertPoints[ i ].x ) / 400 ) * 0.5, THREE.SRGBColorSpace );
  86. colors2.push( color.r, color.g, color.b );
  87. color.setHSL( i / hilbertPoints.length, 1.0, 0.5, THREE.SRGBColorSpace );
  88. colors3.push( color.r, color.g, color.b );
  89. }
  90. geometry4.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  91. geometry5.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  92. geometry6.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  93. geometry4.setAttribute( 'color', new THREE.Float32BufferAttribute( colors1, 3 ) );
  94. geometry5.setAttribute( 'color', new THREE.Float32BufferAttribute( colors2, 3 ) );
  95. geometry6.setAttribute( 'color', new THREE.Float32BufferAttribute( colors3, 3 ) );
  96. // Create lines and add to scene
  97. const material = new THREE.LineBasicMaterial( { color: 0xffffff, vertexColors: true } );
  98. let line, p;
  99. const scale = 0.3, d = 225;
  100. const parameters = [
  101. [ material, scale * 1.5, [ - d, - d / 2, 0 ], geometry1 ],
  102. [ material, scale * 1.5, [ 0, - d / 2, 0 ], geometry2 ],
  103. [ material, scale * 1.5, [ d, - d / 2, 0 ], geometry3 ],
  104. [ material, scale * 1.5, [ - d, d / 2, 0 ], geometry4 ],
  105. [ material, scale * 1.5, [ 0, d / 2, 0 ], geometry5 ],
  106. [ material, scale * 1.5, [ d, d / 2, 0 ], geometry6 ],
  107. ];
  108. for ( let i = 0; i < parameters.length; i ++ ) {
  109. p = parameters[ i ];
  110. line = new THREE.Line( p[ 3 ], p[ 0 ] );
  111. line.scale.x = line.scale.y = line.scale.z = p[ 1 ];
  112. line.position.x = p[ 2 ][ 0 ];
  113. line.position.y = p[ 2 ][ 1 ];
  114. line.position.z = p[ 2 ][ 2 ];
  115. scene.add( line );
  116. }
  117. //
  118. document.body.style.touchAction = 'none';
  119. document.body.addEventListener( 'pointermove', onPointerMove );
  120. //
  121. window.addEventListener( 'resize', onWindowResize );
  122. }
  123. function onWindowResize() {
  124. windowHalfX = window.innerWidth / 2;
  125. windowHalfY = window.innerHeight / 2;
  126. camera.aspect = window.innerWidth / window.innerHeight;
  127. camera.updateProjectionMatrix();
  128. renderer.setSize( window.innerWidth, window.innerHeight );
  129. }
  130. //
  131. function onPointerMove( event ) {
  132. if ( event.isPrimary === false ) return;
  133. mouseX = event.clientX - windowHalfX;
  134. mouseY = event.clientY - windowHalfY;
  135. }
  136. //
  137. function animate() {
  138. requestAnimationFrame( animate );
  139. render();
  140. }
  141. function render() {
  142. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  143. camera.position.y += ( - mouseY + 200 - camera.position.y ) * 0.05;
  144. camera.lookAt( scene.position );
  145. const time = Date.now() * 0.0005;
  146. for ( let i = 0; i < scene.children.length; i ++ ) {
  147. const object = scene.children[ i ];
  148. if ( object.isLine ) {
  149. object.rotation.y = time * ( i % 2 ? 1 : - 1 );
  150. }
  151. }
  152. renderer.render( scene, camera );
  153. }
  154. </script>
  155. </body>
  156. </html>