webgl_lines_colors.html 6.5 KB

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