TubePainter.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. ( function () {
  2. function TubePainter() {
  3. const BUFFER_SIZE = 1000000 * 3;
  4. const positions = new THREE.BufferAttribute( new Float32Array( BUFFER_SIZE ), 3 );
  5. positions.usage = THREE.DynamicDrawUsage;
  6. const normals = new THREE.BufferAttribute( new Float32Array( BUFFER_SIZE ), 3 );
  7. normals.usage = THREE.DynamicDrawUsage;
  8. const colors = new THREE.BufferAttribute( new Float32Array( BUFFER_SIZE ), 3 );
  9. colors.usage = THREE.DynamicDrawUsage;
  10. const geometry = new THREE.BufferGeometry();
  11. geometry.setAttribute( 'position', positions );
  12. geometry.setAttribute( 'normal', normals );
  13. geometry.setAttribute( 'color', colors );
  14. geometry.drawRange.count = 0;
  15. const material = new THREE.MeshStandardMaterial( {
  16. vertexColors: true
  17. } );
  18. const mesh = new THREE.Mesh( geometry, material );
  19. mesh.frustumCulled = false;
  20. //
  21. function getPoints( size ) {
  22. const PI2 = Math.PI * 2;
  23. const sides = 10;
  24. const array = [];
  25. const radius = 0.01 * size;
  26. for ( let i = 0; i < sides; i ++ ) {
  27. const angle = i / sides * PI2;
  28. array.push( new THREE.Vector3( Math.sin( angle ) * radius, Math.cos( angle ) * radius, 0 ) );
  29. }
  30. return array;
  31. }
  32. //
  33. const vector1 = new THREE.Vector3();
  34. const vector2 = new THREE.Vector3();
  35. const vector3 = new THREE.Vector3();
  36. const vector4 = new THREE.Vector3();
  37. const color = new THREE.Color( 0xffffff );
  38. let size = 1;
  39. function stroke( position1, position2, matrix1, matrix2 ) {
  40. if ( position1.distanceToSquared( position2 ) === 0 ) return;
  41. let count = geometry.drawRange.count;
  42. const points = getPoints( size );
  43. for ( let i = 0, il = points.length; i < il; i ++ ) {
  44. const vertex1 = points[ i ];
  45. const vertex2 = points[ ( i + 1 ) % il ];
  46. // positions
  47. vector1.copy( vertex1 ).applyMatrix4( matrix2 ).add( position2 );
  48. vector2.copy( vertex2 ).applyMatrix4( matrix2 ).add( position2 );
  49. vector3.copy( vertex2 ).applyMatrix4( matrix1 ).add( position1 );
  50. vector4.copy( vertex1 ).applyMatrix4( matrix1 ).add( position1 );
  51. vector1.toArray( positions.array, ( count + 0 ) * 3 );
  52. vector2.toArray( positions.array, ( count + 1 ) * 3 );
  53. vector4.toArray( positions.array, ( count + 2 ) * 3 );
  54. vector2.toArray( positions.array, ( count + 3 ) * 3 );
  55. vector3.toArray( positions.array, ( count + 4 ) * 3 );
  56. vector4.toArray( positions.array, ( count + 5 ) * 3 );
  57. // normals
  58. vector1.copy( vertex1 ).applyMatrix4( matrix2 ).normalize();
  59. vector2.copy( vertex2 ).applyMatrix4( matrix2 ).normalize();
  60. vector3.copy( vertex2 ).applyMatrix4( matrix1 ).normalize();
  61. vector4.copy( vertex1 ).applyMatrix4( matrix1 ).normalize();
  62. vector1.toArray( normals.array, ( count + 0 ) * 3 );
  63. vector2.toArray( normals.array, ( count + 1 ) * 3 );
  64. vector4.toArray( normals.array, ( count + 2 ) * 3 );
  65. vector2.toArray( normals.array, ( count + 3 ) * 3 );
  66. vector3.toArray( normals.array, ( count + 4 ) * 3 );
  67. vector4.toArray( normals.array, ( count + 5 ) * 3 );
  68. // colors
  69. color.toArray( colors.array, ( count + 0 ) * 3 );
  70. color.toArray( colors.array, ( count + 1 ) * 3 );
  71. color.toArray( colors.array, ( count + 2 ) * 3 );
  72. color.toArray( colors.array, ( count + 3 ) * 3 );
  73. color.toArray( colors.array, ( count + 4 ) * 3 );
  74. color.toArray( colors.array, ( count + 5 ) * 3 );
  75. count += 6;
  76. }
  77. geometry.drawRange.count = count;
  78. }
  79. //
  80. const up = new THREE.Vector3( 0, 1, 0 );
  81. const point1 = new THREE.Vector3();
  82. const point2 = new THREE.Vector3();
  83. const matrix1 = new THREE.Matrix4();
  84. const matrix2 = new THREE.Matrix4();
  85. function moveTo( position ) {
  86. point1.copy( position );
  87. matrix1.lookAt( point2, point1, up );
  88. point2.copy( position );
  89. matrix2.copy( matrix1 );
  90. }
  91. function lineTo( position ) {
  92. point1.copy( position );
  93. matrix1.lookAt( point2, point1, up );
  94. stroke( point1, point2, matrix1, matrix2 );
  95. point2.copy( point1 );
  96. matrix2.copy( matrix1 );
  97. }
  98. function setSize( value ) {
  99. size = value;
  100. }
  101. //
  102. let count = 0;
  103. function update() {
  104. const start = count;
  105. const end = geometry.drawRange.count;
  106. if ( start === end ) return;
  107. positions.updateRange.offset = start * 3;
  108. positions.updateRange.count = ( end - start ) * 3;
  109. positions.needsUpdate = true;
  110. normals.updateRange.offset = start * 3;
  111. normals.updateRange.count = ( end - start ) * 3;
  112. normals.needsUpdate = true;
  113. colors.updateRange.offset = start * 3;
  114. colors.updateRange.count = ( end - start ) * 3;
  115. colors.needsUpdate = true;
  116. count = geometry.drawRange.count;
  117. }
  118. return {
  119. mesh: mesh,
  120. moveTo: moveTo,
  121. lineTo: lineTo,
  122. setSize: setSize,
  123. update: update
  124. };
  125. }
  126. THREE.TubePainter = TubePainter;
  127. } )();