webgl_lines_colors.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. a {
  14. color:#0078ff;
  15. }
  16. #info {
  17. position: absolute;
  18. top: 10px; width: 100%;
  19. color: #ffffff;
  20. padding: 5px;
  21. font-family: Monospace;
  22. font-size: 13px;
  23. text-align: center;
  24. z-index:100;
  25. }
  26. a {
  27. color: orange;
  28. text-decoration: none;
  29. }
  30. a:hover {
  31. color: #0080ff;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <div id="info">
  37. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - colors WebGL demo
  38. [<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>]
  39. </div>
  40. <script src="../build/three.js"></script>
  41. <script src="js/geometries/hilbert3D.js"></script>
  42. <script src="js/Detector.js"></script>
  43. <script>
  44. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  45. var mouseX = 0, mouseY = 0,
  46. windowHalfX = window.innerWidth / 2,
  47. windowHalfY = window.innerHeight / 2,
  48. camera, scene, renderer, material;
  49. init();
  50. animate();
  51. function init() {
  52. var i, container;
  53. container = document.createElement( 'div' );
  54. document.body.appendChild( container );
  55. camera = new THREE.PerspectiveCamera( 33, window.innerWidth / window.innerHeight, 1, 10000 );
  56. camera.position.z = 1000;
  57. scene = new THREE.Scene();
  58. renderer = new THREE.WebGLRenderer( { antialias: true } );
  59. renderer.setPixelRatio( window.devicePixelRatio );
  60. renderer.setSize( window.innerWidth, window.innerHeight );
  61. container.appendChild( renderer.domElement );
  62. var hilbertPoints = hilbert3D( new THREE.Vector3( 0,0,0 ), 200.0, 1, 0, 1, 2, 3, 4, 5, 6, 7 );
  63. // Colors with BufferGeometry
  64. var buffGeometry0 = new THREE.BufferGeometry(),
  65. buffGeometry1 = new THREE.BufferGeometry(),
  66. buffGeometry2 = new THREE.BufferGeometry();
  67. var subdivisions = 6;
  68. var position = [],
  69. colorArray0 = [],
  70. colorArray1 = [],
  71. colorArray2 = [];
  72. var point = new THREE.Vector3();
  73. var color = new THREE.Color();
  74. var spline = new THREE.CatmullRomCurve3( hilbertPoints );
  75. for ( i = 0; i < hilbertPoints.length * subdivisions; i++ ) {
  76. var t = i / ( hilbertPoints.length * subdivisions );
  77. spline.getPoint( t, point );
  78. position.push( point.x, point.y, point.z );
  79. color.setHSL( 0.6, 1.0, Math.max( 0, - point.x / 200 ) + 0.5 );
  80. colorArray0.push( color.r, color.g, color.b );
  81. color.setHSL( 0.9, 1.0, Math.max( 0, - point.y / 200 ) + 0.5 );
  82. colorArray1.push( color.r, color.g, color.b );
  83. color.setHSL( i / ( hilbertPoints.length * subdivisions ), 1.0, 0.5 );
  84. colorArray2.push( color.r, color.g, color.b );
  85. }
  86. buffGeometry0.addAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
  87. buffGeometry1.addAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
  88. buffGeometry2.addAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
  89. buffGeometry0.addAttribute( 'color', new THREE.Float32BufferAttribute( colorArray0, 3 ) );
  90. buffGeometry1.addAttribute( 'color', new THREE.Float32BufferAttribute( colorArray1, 3 ) );
  91. buffGeometry2.addAttribute( 'color', new THREE.Float32BufferAttribute( colorArray2, 3 ) );
  92. // Colors with Geometry
  93. var geometry0 = new THREE.Geometry(),
  94. geometry2 = new THREE.Geometry(),
  95. geometry3 = new THREE.Geometry();
  96. var colors0 = [],
  97. colors1 = [],
  98. colors2 = [];
  99. for ( i = 0; i < hilbertPoints.length; i++ ) {
  100. geometry0.vertices.push( hilbertPoints[ i ] );
  101. colors0[ i ] = new THREE.Color( 0xffffff );
  102. colors0[ i ].setHSL( 0.6, 1.0, Math.max( 0, ( 200 - hilbertPoints[ i ].x ) / 400 ) * 0.5 + 0.5 );
  103. colors1[ i ] = new THREE.Color( 0xffffff );
  104. colors1[ i ].setHSL( 0.3, 1.0, Math.max( 0, ( 200 + hilbertPoints[ i ].x ) / 400 ) * 0.5 );
  105. colors2[ i ] = new THREE.Color( 0xffffff );
  106. colors2[ i ].setHSL( i / hilbertPoints.length, 1.0, 0.5 );
  107. }
  108. geometry2.vertices = geometry3.vertices = geometry0.vertices;
  109. geometry0.colors = colors0;
  110. geometry2.colors = colors1;
  111. geometry3.colors = colors2;
  112. // Create lines and add to scene
  113. material = new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 1, linewidth: 3, vertexColors: THREE.VertexColors } );
  114. var line, p, scale = 0.3, d = 225;
  115. var parameters = [
  116. [ material, scale * 1.5, [ - d, - d / 2, 0 ], buffGeometry0 ],
  117. [ material, scale * 1.5, [ 0, - d / 2, 0 ], buffGeometry1 ],
  118. [ material, scale * 1.5, [ d, - d / 2, 0 ], buffGeometry2 ],
  119. [ material, scale * 1.5, [ - d, d / 2, 0 ], geometry0 ],
  120. [ material, scale * 1.5, [ 0, d / 2, 0 ], geometry2 ],
  121. [ material, scale * 1.5, [ d, d / 2, 0 ], geometry3 ],
  122. ];
  123. for ( 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. // Input Event Listeners
  133. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  134. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  135. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  136. // Resize Event Listener
  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 ) * .05;
  172. camera.position.y += ( - mouseY + 200 - camera.position.y ) * .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 instanceof THREE.Line ) {
  178. object.rotation.y = time * ( i % 2 ? 1 : -1 );
  179. }
  180. }
  181. renderer.render( scene, camera );
  182. }
  183. </script>
  184. </body>
  185. </html>