webgl_lines_colors.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. <style>
  8. body {
  9. background-color: #000000;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. #info {
  14. position: absolute;
  15. top: 10px; width: 100%;
  16. color: #ffffff;
  17. padding: 5px;
  18. font-family: Monospace;
  19. font-size: 13px;
  20. text-align: center;
  21. z-index:100;
  22. }
  23. a {
  24. color: orange;
  25. text-decoration: none;
  26. }
  27. a:hover {
  28. color: #0080ff;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div id="info">
  34. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - color lines
  35. [<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>]
  36. </div>
  37. <script src="../build/three.js"></script>
  38. <script src="js/geometries/hilbert3D.js"></script>
  39. <script src="js/WebGL.js"></script>
  40. <script>
  41. if ( WEBGL.isWebGLAvailable() === false ) {
  42. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  43. }
  44. var mouseX = 0, mouseY = 0;
  45. var windowHalfX = window.innerWidth / 2;
  46. var windowHalfY = window.innerHeight / 2;
  47. var camera, scene, renderer;
  48. init();
  49. animate();
  50. function init() {
  51. camera = new THREE.PerspectiveCamera( 33, window.innerWidth / window.innerHeight, 1, 10000 );
  52. camera.position.z = 1000;
  53. scene = new THREE.Scene();
  54. renderer = new THREE.WebGLRenderer( { antialias: true } );
  55. renderer.setPixelRatio( window.devicePixelRatio );
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. document.body.appendChild( renderer.domElement );
  58. //
  59. var hilbertPoints = hilbert3D( new THREE.Vector3( 0, 0, 0 ), 200.0, 1, 0, 1, 2, 3, 4, 5, 6, 7 );
  60. var geometry1 = new THREE.BufferGeometry();
  61. var geometry2 = new THREE.BufferGeometry();
  62. var geometry3 = new THREE.BufferGeometry();
  63. var subdivisions = 6;
  64. var vertices = [];
  65. var colors1 = [];
  66. var colors2 = [];
  67. var colors3 = [];
  68. var point = new THREE.Vector3();
  69. var color = new THREE.Color();
  70. var spline = new THREE.CatmullRomCurve3( hilbertPoints );
  71. for ( var i = 0; i < hilbertPoints.length * subdivisions; i ++ ) {
  72. var t = i / ( hilbertPoints.length * subdivisions );
  73. spline.getPoint( t, point );
  74. vertices.push( point.x, point.y, point.z );
  75. color.setHSL( 0.6, 1.0, Math.max( 0, - point.x / 200 ) + 0.5 );
  76. colors1.push( color.r, color.g, color.b );
  77. color.setHSL( 0.9, 1.0, Math.max( 0, - point.y / 200 ) + 0.5 );
  78. colors2.push( color.r, color.g, color.b );
  79. color.setHSL( i / ( hilbertPoints.length * subdivisions ), 1.0, 0.5 );
  80. colors3.push( color.r, color.g, color.b );
  81. }
  82. geometry1.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  83. geometry2.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  84. geometry3.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  85. geometry1.addAttribute( 'color', new THREE.Float32BufferAttribute( colors1, 3 ) );
  86. geometry2.addAttribute( 'color', new THREE.Float32BufferAttribute( colors2, 3 ) );
  87. geometry3.addAttribute( 'color', new THREE.Float32BufferAttribute( colors3, 3 ) );
  88. //
  89. var geometry4 = new THREE.BufferGeometry();
  90. var geometry5 = new THREE.BufferGeometry();
  91. var geometry6 = new THREE.BufferGeometry();
  92. vertices = [];
  93. colors1 = [];
  94. colors2 = [];
  95. colors3 = [];
  96. for ( var i = 0; i < hilbertPoints.length; i ++ ) {
  97. var point = hilbertPoints[ i ];
  98. vertices.push( point.x, point.y, point.z );
  99. color.setHSL( 0.6, 1.0, Math.max( 0, ( 200 - hilbertPoints[ i ].x ) / 400 ) * 0.5 + 0.5 );
  100. colors1.push( color.r, color.g, color.b );
  101. color.setHSL( 0.3, 1.0, Math.max( 0, ( 200 + hilbertPoints[ i ].x ) / 400 ) * 0.5 );
  102. colors2.push( color.r, color.g, color.b );
  103. color.setHSL( i / hilbertPoints.length, 1.0, 0.5 );
  104. colors3.push( color.r, color.g, color.b );
  105. }
  106. geometry4.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  107. geometry5.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  108. geometry6.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  109. geometry4.addAttribute( 'color', new THREE.Float32BufferAttribute( colors1, 3 ) );
  110. geometry5.addAttribute( 'color', new THREE.Float32BufferAttribute( colors2, 3 ) );
  111. geometry6.addAttribute( 'color', new THREE.Float32BufferAttribute( colors3, 3 ) );
  112. // Create lines and add to scene
  113. var material = new THREE.LineBasicMaterial( { color: 0xffffff, vertexColors: THREE.VertexColors } );
  114. var line, p, scale = 0.3, d = 225;
  115. var parameters = [
  116. [ material, scale * 1.5, [ - d, - d / 2, 0 ], geometry1 ],
  117. [ material, scale * 1.5, [ 0, - d / 2, 0 ], geometry2 ],
  118. [ material, scale * 1.5, [ d, - d / 2, 0 ], geometry3 ],
  119. [ material, scale * 1.5, [ - d, d / 2, 0 ], geometry4 ],
  120. [ material, scale * 1.5, [ 0, d / 2, 0 ], geometry5 ],
  121. [ material, scale * 1.5, [ d, d / 2, 0 ], geometry6 ],
  122. ];
  123. for ( var i = 0; i < parameters.length; i ++ ) {
  124. p = parameters[ i ];
  125. line = new THREE.Line( p[ 3 ], p[ 0 ] );
  126. line.scale.x = line.scale.y = line.scale.z = p[ 1 ];
  127. line.position.x = p[ 2 ][ 0 ];
  128. line.position.y = p[ 2 ][ 1 ];
  129. line.position.z = p[ 2 ][ 2 ];
  130. scene.add( line );
  131. }
  132. //
  133. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  134. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  135. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  136. //
  137. window.addEventListener( 'resize', onWindowResize, false );
  138. }
  139. function onWindowResize() {
  140. windowHalfX = window.innerWidth / 2;
  141. windowHalfY = window.innerHeight / 2;
  142. camera.aspect = window.innerWidth / window.innerHeight;
  143. camera.updateProjectionMatrix();
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. }
  146. //
  147. function onDocumentMouseMove( event ) {
  148. mouseX = event.clientX - windowHalfX;
  149. mouseY = event.clientY - windowHalfY;
  150. }
  151. function onDocumentTouchStart( event ) {
  152. if ( event.touches.length > 1 ) {
  153. event.preventDefault();
  154. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  155. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  156. }
  157. }
  158. function onDocumentTouchMove( event ) {
  159. if ( event.touches.length == 1 ) {
  160. event.preventDefault();
  161. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  162. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  163. }
  164. }
  165. //
  166. function animate() {
  167. requestAnimationFrame( animate );
  168. render();
  169. }
  170. function render() {
  171. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  172. camera.position.y += ( - mouseY + 200 - camera.position.y ) * 0.05;
  173. camera.lookAt( scene.position );
  174. var time = Date.now() * 0.0005;
  175. for ( var i = 0; i < scene.children.length; i ++ ) {
  176. var object = scene.children[ i ];
  177. if ( object.isLine ) {
  178. object.rotation.y = time * ( i % 2 ? 1 : - 1 );
  179. }
  180. }
  181. renderer.render( scene, camera );
  182. }
  183. </script>
  184. </body>
  185. </html>