TubePainter.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import {
  2. BufferAttribute,
  3. BufferGeometry,
  4. Color,
  5. DynamicDrawUsage,
  6. Matrix4,
  7. Mesh,
  8. MeshStandardMaterial,
  9. Vector3
  10. } from 'three';
  11. /**
  12. * @classdesc This module can be used to paint tube-like meshes
  13. * along a sequence of points. This module is used in a XR
  14. * painter demo.
  15. *
  16. * ```js
  17. * const painter = new TubePainter();
  18. * scene.add( painter.mesh );
  19. * ```
  20. *
  21. * @name TubePainter
  22. * @class
  23. */
  24. function TubePainter() {
  25. const BUFFER_SIZE = 1000000 * 3;
  26. const positions = new BufferAttribute( new Float32Array( BUFFER_SIZE ), 3 );
  27. positions.usage = DynamicDrawUsage;
  28. const normals = new BufferAttribute( new Float32Array( BUFFER_SIZE ), 3 );
  29. normals.usage = DynamicDrawUsage;
  30. const colors = new BufferAttribute( new Float32Array( BUFFER_SIZE ), 3 );
  31. colors.usage = DynamicDrawUsage;
  32. const geometry = new BufferGeometry();
  33. geometry.setAttribute( 'position', positions );
  34. geometry.setAttribute( 'normal', normals );
  35. geometry.setAttribute( 'color', colors );
  36. geometry.drawRange.count = 0;
  37. const material = new MeshStandardMaterial( {
  38. vertexColors: true
  39. } );
  40. const mesh = new Mesh( geometry, material );
  41. mesh.frustumCulled = false;
  42. //
  43. function getPoints( size ) {
  44. const PI2 = Math.PI * 2;
  45. const sides = 10;
  46. const array = [];
  47. const radius = 0.01 * size;
  48. for ( let i = 0; i < sides; i ++ ) {
  49. const angle = ( i / sides ) * PI2;
  50. array.push( new Vector3( Math.sin( angle ) * radius, Math.cos( angle ) * radius, 0 ) );
  51. }
  52. return array;
  53. }
  54. //
  55. const vector1 = new Vector3();
  56. const vector2 = new Vector3();
  57. const vector3 = new Vector3();
  58. const vector4 = new Vector3();
  59. const color = new Color( 0xffffff );
  60. let size = 1;
  61. function stroke( position1, position2, matrix1, matrix2 ) {
  62. if ( position1.distanceToSquared( position2 ) === 0 ) return;
  63. let count = geometry.drawRange.count;
  64. const points = getPoints( size );
  65. for ( let i = 0, il = points.length; i < il; i ++ ) {
  66. const vertex1 = points[ i ];
  67. const vertex2 = points[ ( i + 1 ) % il ];
  68. // positions
  69. vector1.copy( vertex1 ).applyMatrix4( matrix2 ).add( position2 );
  70. vector2.copy( vertex2 ).applyMatrix4( matrix2 ).add( position2 );
  71. vector3.copy( vertex2 ).applyMatrix4( matrix1 ).add( position1 );
  72. vector4.copy( vertex1 ).applyMatrix4( matrix1 ).add( position1 );
  73. vector1.toArray( positions.array, ( count + 0 ) * 3 );
  74. vector2.toArray( positions.array, ( count + 1 ) * 3 );
  75. vector4.toArray( positions.array, ( count + 2 ) * 3 );
  76. vector2.toArray( positions.array, ( count + 3 ) * 3 );
  77. vector3.toArray( positions.array, ( count + 4 ) * 3 );
  78. vector4.toArray( positions.array, ( count + 5 ) * 3 );
  79. // normals
  80. vector1.copy( vertex1 ).applyMatrix4( matrix2 ).normalize();
  81. vector2.copy( vertex2 ).applyMatrix4( matrix2 ).normalize();
  82. vector3.copy( vertex2 ).applyMatrix4( matrix1 ).normalize();
  83. vector4.copy( vertex1 ).applyMatrix4( matrix1 ).normalize();
  84. vector1.toArray( normals.array, ( count + 0 ) * 3 );
  85. vector2.toArray( normals.array, ( count + 1 ) * 3 );
  86. vector4.toArray( normals.array, ( count + 2 ) * 3 );
  87. vector2.toArray( normals.array, ( count + 3 ) * 3 );
  88. vector3.toArray( normals.array, ( count + 4 ) * 3 );
  89. vector4.toArray( normals.array, ( count + 5 ) * 3 );
  90. // colors
  91. color.toArray( colors.array, ( count + 0 ) * 3 );
  92. color.toArray( colors.array, ( count + 1 ) * 3 );
  93. color.toArray( colors.array, ( count + 2 ) * 3 );
  94. color.toArray( colors.array, ( count + 3 ) * 3 );
  95. color.toArray( colors.array, ( count + 4 ) * 3 );
  96. color.toArray( colors.array, ( count + 5 ) * 3 );
  97. count += 6;
  98. }
  99. geometry.drawRange.count = count;
  100. }
  101. //
  102. const up = new Vector3( 0, 1, 0 );
  103. const point1 = new Vector3();
  104. const point2 = new Vector3();
  105. const matrix1 = new Matrix4();
  106. const matrix2 = new Matrix4();
  107. function moveTo( position ) {
  108. point1.copy( position );
  109. matrix1.lookAt( point2, point1, up );
  110. point2.copy( position );
  111. matrix2.copy( matrix1 );
  112. }
  113. function lineTo( position ) {
  114. point1.copy( position );
  115. matrix1.lookAt( point2, point1, up );
  116. stroke( point1, point2, matrix1, matrix2 );
  117. point2.copy( point1 );
  118. matrix2.copy( matrix1 );
  119. }
  120. function setSize( value ) {
  121. size = value;
  122. }
  123. //
  124. let count = 0;
  125. function update() {
  126. const start = count;
  127. const end = geometry.drawRange.count;
  128. if ( start === end ) return;
  129. positions.addUpdateRange( start * 3, ( end - start ) * 3 );
  130. positions.needsUpdate = true;
  131. normals.addUpdateRange( start * 3, ( end - start ) * 3 );
  132. normals.needsUpdate = true;
  133. colors.addUpdateRange( start * 3, ( end - start ) * 3 );
  134. colors.needsUpdate = true;
  135. count = geometry.drawRange.count;
  136. }
  137. return {
  138. /**
  139. * The "painted" tube mesh. Must be added to the scene.
  140. *
  141. * @name TubePainter#mesh
  142. * @type {Mesh}
  143. */
  144. mesh: mesh,
  145. /**
  146. * Moves the current painting position to the given value.
  147. *
  148. * @method
  149. * @name TubePainter#moveTo
  150. * @param {Vector3} position The new painting position.
  151. */
  152. moveTo: moveTo,
  153. /**
  154. * Draw a stroke from the current position to the given one.
  155. * This method extends the tube while drawing with the XR
  156. * controllers.
  157. *
  158. * @method
  159. * @name TubePainter#lineTo
  160. * @param {Vector3} position The destination position.
  161. */
  162. lineTo: lineTo,
  163. /**
  164. * Sets the size of newly rendered tube segments.
  165. *
  166. * @method
  167. * @name TubePainter#setSize
  168. * @param {number} size The size.
  169. */
  170. setSize: setSize,
  171. /**
  172. * Updates the internal geometry buffers so the new painted
  173. * segments are rendered.
  174. *
  175. * @method
  176. * @name TubePainter#update
  177. */
  178. update: update
  179. };
  180. }
  181. export { TubePainter };
粤ICP备19079148号