webgl_lines_colors.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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="http://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 {
  16. BufferGeometry,
  17. CatmullRomCurve3,
  18. Color,
  19. Float32BufferAttribute,
  20. Line,
  21. LineBasicMaterial,
  22. PerspectiveCamera,
  23. Scene,
  24. Vector3,
  25. VertexColors,
  26. WebGLRenderer
  27. } from "../build/three.module.js";
  28. import { GeometryUtils } from './jsm/utils/GeometryUtils.js';
  29. var mouseX = 0, mouseY = 0;
  30. var windowHalfX = window.innerWidth / 2;
  31. var windowHalfY = window.innerHeight / 2;
  32. var camera, scene, renderer;
  33. init();
  34. animate();
  35. function init() {
  36. camera = new PerspectiveCamera( 33, window.innerWidth / window.innerHeight, 1, 10000 );
  37. camera.position.z = 1000;
  38. scene = new Scene();
  39. renderer = new WebGLRenderer( { antialias: true } );
  40. renderer.setPixelRatio( window.devicePixelRatio );
  41. renderer.setSize( window.innerWidth, window.innerHeight );
  42. document.body.appendChild( renderer.domElement );
  43. //
  44. var hilbertPoints = GeometryUtils.hilbert3D( new Vector3( 0, 0, 0 ), 200.0, 1, 0, 1, 2, 3, 4, 5, 6, 7 );
  45. var geometry1 = new BufferGeometry();
  46. var geometry2 = new BufferGeometry();
  47. var geometry3 = new BufferGeometry();
  48. var subdivisions = 6;
  49. var vertices = [];
  50. var colors1 = [];
  51. var colors2 = [];
  52. var colors3 = [];
  53. var point = new Vector3();
  54. var color = new Color();
  55. var spline = new CatmullRomCurve3( hilbertPoints );
  56. for ( var i = 0; i < hilbertPoints.length * subdivisions; i ++ ) {
  57. var t = i / ( hilbertPoints.length * subdivisions );
  58. spline.getPoint( t, point );
  59. vertices.push( point.x, point.y, point.z );
  60. color.setHSL( 0.6, 1.0, Math.max( 0, - point.x / 200 ) + 0.5 );
  61. colors1.push( color.r, color.g, color.b );
  62. color.setHSL( 0.9, 1.0, Math.max( 0, - point.y / 200 ) + 0.5 );
  63. colors2.push( color.r, color.g, color.b );
  64. color.setHSL( i / ( hilbertPoints.length * subdivisions ), 1.0, 0.5 );
  65. colors3.push( color.r, color.g, color.b );
  66. }
  67. geometry1.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  68. geometry2.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  69. geometry3.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  70. geometry1.addAttribute( 'color', new Float32BufferAttribute( colors1, 3 ) );
  71. geometry2.addAttribute( 'color', new Float32BufferAttribute( colors2, 3 ) );
  72. geometry3.addAttribute( 'color', new Float32BufferAttribute( colors3, 3 ) );
  73. //
  74. var geometry4 = new BufferGeometry();
  75. var geometry5 = new BufferGeometry();
  76. var geometry6 = new BufferGeometry();
  77. vertices = [];
  78. colors1 = [];
  79. colors2 = [];
  80. colors3 = [];
  81. for ( var i = 0; i < hilbertPoints.length; i ++ ) {
  82. var point = hilbertPoints[ i ];
  83. vertices.push( point.x, point.y, point.z );
  84. color.setHSL( 0.6, 1.0, Math.max( 0, ( 200 - hilbertPoints[ i ].x ) / 400 ) * 0.5 + 0.5 );
  85. colors1.push( color.r, color.g, color.b );
  86. color.setHSL( 0.3, 1.0, Math.max( 0, ( 200 + hilbertPoints[ i ].x ) / 400 ) * 0.5 );
  87. colors2.push( color.r, color.g, color.b );
  88. color.setHSL( i / hilbertPoints.length, 1.0, 0.5 );
  89. colors3.push( color.r, color.g, color.b );
  90. }
  91. geometry4.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  92. geometry5.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  93. geometry6.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  94. geometry4.addAttribute( 'color', new Float32BufferAttribute( colors1, 3 ) );
  95. geometry5.addAttribute( 'color', new Float32BufferAttribute( colors2, 3 ) );
  96. geometry6.addAttribute( 'color', new Float32BufferAttribute( colors3, 3 ) );
  97. // Create lines and add to scene
  98. var material = new LineBasicMaterial( { color: 0xffffff, vertexColors: VertexColors } );
  99. var line, p, scale = 0.3, d = 225;
  100. var 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 ( var i = 0; i < parameters.length; i ++ ) {
  109. p = parameters[ i ];
  110. line = new 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.addEventListener( 'mousemove', onDocumentMouseMove, false );
  119. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  120. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  121. //
  122. window.addEventListener( 'resize', onWindowResize, false );
  123. }
  124. function onWindowResize() {
  125. windowHalfX = window.innerWidth / 2;
  126. windowHalfY = window.innerHeight / 2;
  127. camera.aspect = window.innerWidth / window.innerHeight;
  128. camera.updateProjectionMatrix();
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  130. }
  131. //
  132. function onDocumentMouseMove( event ) {
  133. mouseX = event.clientX - windowHalfX;
  134. mouseY = event.clientY - windowHalfY;
  135. }
  136. function onDocumentTouchStart( event ) {
  137. if ( event.touches.length > 1 ) {
  138. event.preventDefault();
  139. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  140. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  141. }
  142. }
  143. function onDocumentTouchMove( event ) {
  144. if ( event.touches.length == 1 ) {
  145. event.preventDefault();
  146. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  147. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  148. }
  149. }
  150. //
  151. function animate() {
  152. requestAnimationFrame( animate );
  153. render();
  154. }
  155. function render() {
  156. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  157. camera.position.y += ( - mouseY + 200 - camera.position.y ) * 0.05;
  158. camera.lookAt( scene.position );
  159. var time = Date.now() * 0.0005;
  160. for ( var i = 0; i < scene.children.length; i ++ ) {
  161. var object = scene.children[ i ];
  162. if ( object.isLine ) {
  163. object.rotation.y = time * ( i % 2 ? 1 : - 1 );
  164. }
  165. }
  166. renderer.render( scene, camera );
  167. }
  168. </script>
  169. </body>
  170. </html>