TubePainter.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. * @three_import import { TubePainter } from 'three/addons/misc/TubePainter.js';
  24. */
  25. function TubePainter() {
  26. const BUFFER_SIZE = 1000000 * 3;
  27. const positions = new BufferAttribute( new Float32Array( BUFFER_SIZE ), 3 );
  28. positions.usage = DynamicDrawUsage;
  29. const normals = new BufferAttribute( new Float32Array( BUFFER_SIZE ), 3 );
  30. normals.usage = DynamicDrawUsage;
  31. const colors = new BufferAttribute( new Float32Array( BUFFER_SIZE ), 3 );
  32. colors.usage = DynamicDrawUsage;
  33. const geometry = new BufferGeometry();
  34. geometry.setAttribute( 'position', positions );
  35. geometry.setAttribute( 'normal', normals );
  36. geometry.setAttribute( 'color', colors );
  37. geometry.drawRange.count = 0;
  38. const material = new MeshStandardMaterial( { vertexColors: true } );
  39. const mesh = new Mesh( geometry, material );
  40. mesh.frustumCulled = false;
  41. //
  42. function getPoints( size ) {
  43. const PI2 = Math.PI * 2;
  44. const sides = 15;
  45. const array = [];
  46. const radius = 0.01 * size;
  47. for ( let i = 0; i < sides; i ++ ) {
  48. const angle = ( i / sides ) * PI2;
  49. array.push( new Vector3( Math.sin( angle ) * radius, Math.cos( angle ) * radius, 0 ) );
  50. }
  51. return array;
  52. }
  53. //
  54. const vector = new Vector3();
  55. const vector1 = new Vector3();
  56. const vector2 = new Vector3();
  57. const vector3 = new Vector3();
  58. const vector4 = new Vector3();
  59. const color1 = new Color( 0xffffff );
  60. const color2 = new Color( 0xffffff );
  61. let size1 = 1;
  62. let size2 = 1;
  63. function addCap( position, matrix, isEndCap, capSize ) {
  64. let count = geometry.drawRange.count;
  65. const points = getPoints( capSize );
  66. const sides = points.length;
  67. const radius = 0.01 * capSize;
  68. const latSegments = 4;
  69. const directionSign = isEndCap ? - 1 : 1;
  70. for ( let lat = 0; lat < latSegments; lat ++ ) {
  71. const phi1 = ( lat / latSegments ) * Math.PI * 0.5;
  72. const phi2 = ( ( lat + 1 ) / latSegments ) * Math.PI * 0.5;
  73. const z1 = Math.sin( phi1 ) * radius * directionSign;
  74. const r1 = Math.cos( phi1 ) * radius;
  75. const z2 = Math.sin( phi2 ) * radius * directionSign;
  76. const r2 = Math.cos( phi2 ) * radius;
  77. for ( let i = 0; i < sides; i ++ ) {
  78. const theta1 = ( i / sides ) * Math.PI * 2;
  79. const theta2 = ( ( i + 1 ) / sides ) * Math.PI * 2;
  80. // First ring
  81. const x1 = Math.sin( theta1 ) * r1;
  82. const y1 = Math.cos( theta1 ) * r1;
  83. const x2 = Math.sin( theta2 ) * r1;
  84. const y2 = Math.cos( theta2 ) * r1;
  85. // Second ring
  86. const x3 = Math.sin( theta1 ) * r2;
  87. const y3 = Math.cos( theta1 ) * r2;
  88. const x4 = Math.sin( theta2 ) * r2;
  89. const y4 = Math.cos( theta2 ) * r2;
  90. // Transform to world space
  91. vector1.set( x1, y1, z1 ).applyMatrix4( matrix ).add( position );
  92. vector2.set( x2, y2, z1 ).applyMatrix4( matrix ).add( position );
  93. vector3.set( x3, y3, z2 ).applyMatrix4( matrix ).add( position );
  94. vector4.set( x4, y4, z2 ).applyMatrix4( matrix ).add( position );
  95. // First triangle
  96. normal.set( x1, y1, z1 ).normalize().transformDirection( matrix );
  97. vector.set( x2, y2, z1 ).normalize().transformDirection( matrix );
  98. side.set( x3, y3, z2 ).normalize().transformDirection( matrix );
  99. if ( isEndCap ) {
  100. vector1.toArray( positions.array, count * 3 );
  101. vector2.toArray( positions.array, ( count + 1 ) * 3 );
  102. vector3.toArray( positions.array, ( count + 2 ) * 3 );
  103. normal.toArray( normals.array, count * 3 );
  104. vector.toArray( normals.array, ( count + 1 ) * 3 );
  105. side.toArray( normals.array, ( count + 2 ) * 3 );
  106. } else {
  107. vector1.toArray( positions.array, count * 3 );
  108. vector3.toArray( positions.array, ( count + 1 ) * 3 );
  109. vector2.toArray( positions.array, ( count + 2 ) * 3 );
  110. normal.toArray( normals.array, count * 3 );
  111. side.toArray( normals.array, ( count + 1 ) * 3 );
  112. vector.toArray( normals.array, ( count + 2 ) * 3 );
  113. }
  114. color1.toArray( colors.array, count * 3 );
  115. color1.toArray( colors.array, ( count + 1 ) * 3 );
  116. color1.toArray( colors.array, ( count + 2 ) * 3 );
  117. count += 3;
  118. // Second triangle
  119. if ( r2 > 0.001 ) {
  120. normal.set( x2, y2, z1 ).normalize().transformDirection( matrix );
  121. vector.set( x4, y4, z2 ).normalize().transformDirection( matrix );
  122. side.set( x3, y3, z2 ).normalize().transformDirection( matrix );
  123. if ( isEndCap ) {
  124. vector2.toArray( positions.array, count * 3 );
  125. vector4.toArray( positions.array, ( count + 1 ) * 3 );
  126. vector3.toArray( positions.array, ( count + 2 ) * 3 );
  127. normal.toArray( normals.array, count * 3 );
  128. vector.toArray( normals.array, ( count + 1 ) * 3 );
  129. side.toArray( normals.array, ( count + 2 ) * 3 );
  130. } else {
  131. vector3.toArray( positions.array, count * 3 );
  132. vector4.toArray( positions.array, ( count + 1 ) * 3 );
  133. vector2.toArray( positions.array, ( count + 2 ) * 3 );
  134. side.toArray( normals.array, count * 3 );
  135. vector.toArray( normals.array, ( count + 1 ) * 3 );
  136. normal.toArray( normals.array, ( count + 2 ) * 3 );
  137. }
  138. color1.toArray( colors.array, count * 3 );
  139. color1.toArray( colors.array, ( count + 1 ) * 3 );
  140. color1.toArray( colors.array, ( count + 2 ) * 3 );
  141. count += 3;
  142. }
  143. }
  144. }
  145. geometry.drawRange.count = count;
  146. }
  147. function updateEndCap( position, matrix, capSize ) {
  148. if ( endCapStartIndex === null ) return;
  149. const points = getPoints( capSize );
  150. const sides = points.length;
  151. const radius = 0.01 * capSize;
  152. const latSegments = 4;
  153. let count = endCapStartIndex;
  154. for ( let lat = 0; lat < latSegments; lat ++ ) {
  155. const phi1 = ( lat / latSegments ) * Math.PI * 0.5;
  156. const phi2 = ( ( lat + 1 ) / latSegments ) * Math.PI * 0.5;
  157. const z1 = - Math.sin( phi1 ) * radius;
  158. const r1 = Math.cos( phi1 ) * radius;
  159. const z2 = - Math.sin( phi2 ) * radius;
  160. const r2 = Math.cos( phi2 ) * radius;
  161. for ( let i = 0; i < sides; i ++ ) {
  162. const theta1 = ( i / sides ) * Math.PI * 2;
  163. const theta2 = ( ( i + 1 ) / sides ) * Math.PI * 2;
  164. // First ring
  165. const x1 = Math.sin( theta1 ) * r1;
  166. const y1 = Math.cos( theta1 ) * r1;
  167. const x2 = Math.sin( theta2 ) * r1;
  168. const y2 = Math.cos( theta2 ) * r1;
  169. // Second ring
  170. const x3 = Math.sin( theta1 ) * r2;
  171. const y3 = Math.cos( theta1 ) * r2;
  172. const x4 = Math.sin( theta2 ) * r2;
  173. const y4 = Math.cos( theta2 ) * r2;
  174. // Transform positions to world space
  175. vector1.set( x1, y1, z1 ).applyMatrix4( matrix ).add( position );
  176. vector2.set( x2, y2, z1 ).applyMatrix4( matrix ).add( position );
  177. vector3.set( x3, y3, z2 ).applyMatrix4( matrix ).add( position );
  178. vector4.set( x4, y4, z2 ).applyMatrix4( matrix ).add( position );
  179. // Transform normals to world space
  180. normal.set( x1, y1, z1 ).normalize().transformDirection( matrix );
  181. vector.set( x2, y2, z1 ).normalize().transformDirection( matrix );
  182. side.set( x3, y3, z2 ).normalize().transformDirection( matrix );
  183. // First triangle
  184. vector1.toArray( positions.array, count * 3 );
  185. vector2.toArray( positions.array, ( count + 1 ) * 3 );
  186. vector3.toArray( positions.array, ( count + 2 ) * 3 );
  187. normal.toArray( normals.array, count * 3 );
  188. vector.toArray( normals.array, ( count + 1 ) * 3 );
  189. side.toArray( normals.array, ( count + 2 ) * 3 );
  190. color1.toArray( colors.array, count * 3 );
  191. color1.toArray( colors.array, ( count + 1 ) * 3 );
  192. color1.toArray( colors.array, ( count + 2 ) * 3 );
  193. count += 3;
  194. // Second triangle
  195. if ( r2 > 0.001 ) {
  196. normal.set( x2, y2, z1 ).normalize().transformDirection( matrix );
  197. vector.set( x4, y4, z2 ).normalize().transformDirection( matrix );
  198. side.set( x3, y3, z2 ).normalize().transformDirection( matrix );
  199. vector2.toArray( positions.array, count * 3 );
  200. vector4.toArray( positions.array, ( count + 1 ) * 3 );
  201. vector3.toArray( positions.array, ( count + 2 ) * 3 );
  202. normal.toArray( normals.array, count * 3 );
  203. vector.toArray( normals.array, ( count + 1 ) * 3 );
  204. side.toArray( normals.array, ( count + 2 ) * 3 );
  205. color1.toArray( colors.array, count * 3 );
  206. color1.toArray( colors.array, ( count + 1 ) * 3 );
  207. color1.toArray( colors.array, ( count + 2 ) * 3 );
  208. count += 3;
  209. }
  210. }
  211. }
  212. positions.addUpdateRange( endCapStartIndex * 3, endCapVertexCount * 3 );
  213. normals.addUpdateRange( endCapStartIndex * 3, endCapVertexCount * 3 );
  214. colors.addUpdateRange( endCapStartIndex * 3, endCapVertexCount * 3 );
  215. }
  216. function stroke( position1, position2, matrix1, matrix2, size1, size2 ) {
  217. if ( position1.distanceToSquared( position2 ) === 0 ) return;
  218. let count = geometry.drawRange.count;
  219. const points1 = getPoints( size1 );
  220. const points2 = getPoints( size2 );
  221. for ( let i = 0, il = points2.length; i < il; i ++ ) {
  222. const vertex1_2 = points2[ i ];
  223. const vertex2_2 = points2[ ( i + 1 ) % il ];
  224. const vertex1_1 = points1[ i ];
  225. const vertex2_1 = points1[ ( i + 1 ) % il ];
  226. vector1.copy( vertex1_2 ).applyMatrix4( matrix2 ).add( position2 );
  227. vector2.copy( vertex2_2 ).applyMatrix4( matrix2 ).add( position2 );
  228. vector3.copy( vertex2_1 ).applyMatrix4( matrix1 ).add( position1 );
  229. vector4.copy( vertex1_1 ).applyMatrix4( matrix1 ).add( position1 );
  230. vector1.toArray( positions.array, ( count + 0 ) * 3 );
  231. vector2.toArray( positions.array, ( count + 1 ) * 3 );
  232. vector4.toArray( positions.array, ( count + 2 ) * 3 );
  233. vector2.toArray( positions.array, ( count + 3 ) * 3 );
  234. vector3.toArray( positions.array, ( count + 4 ) * 3 );
  235. vector4.toArray( positions.array, ( count + 5 ) * 3 );
  236. vector1.copy( vertex1_2 ).applyMatrix4( matrix2 ).normalize();
  237. vector2.copy( vertex2_2 ).applyMatrix4( matrix2 ).normalize();
  238. vector3.copy( vertex2_1 ).applyMatrix4( matrix1 ).normalize();
  239. vector4.copy( vertex1_1 ).applyMatrix4( matrix1 ).normalize();
  240. vector1.toArray( normals.array, ( count + 0 ) * 3 );
  241. vector2.toArray( normals.array, ( count + 1 ) * 3 );
  242. vector4.toArray( normals.array, ( count + 2 ) * 3 );
  243. vector2.toArray( normals.array, ( count + 3 ) * 3 );
  244. vector3.toArray( normals.array, ( count + 4 ) * 3 );
  245. vector4.toArray( normals.array, ( count + 5 ) * 3 );
  246. color2.toArray( colors.array, ( count + 0 ) * 3 );
  247. color2.toArray( colors.array, ( count + 1 ) * 3 );
  248. color1.toArray( colors.array, ( count + 2 ) * 3 );
  249. color2.toArray( colors.array, ( count + 3 ) * 3 );
  250. color1.toArray( colors.array, ( count + 4 ) * 3 );
  251. color1.toArray( colors.array, ( count + 5 ) * 3 );
  252. count += 6;
  253. }
  254. geometry.drawRange.count = count;
  255. }
  256. //
  257. const direction = new Vector3();
  258. const normal = new Vector3();
  259. const side = new Vector3();
  260. const point1 = new Vector3();
  261. const point2 = new Vector3();
  262. const matrix1 = new Matrix4();
  263. const matrix2 = new Matrix4();
  264. const lastNormal = new Vector3();
  265. const prevDirection = new Vector3();
  266. const rotationAxis = new Vector3();
  267. let isFirstSegment = true;
  268. let endCapStartIndex = null;
  269. let endCapVertexCount = 0;
  270. function calculateRMF() {
  271. if ( isFirstSegment === true ) {
  272. if ( Math.abs( direction.y ) < 0.99 ) {
  273. vector.copy( direction ).multiplyScalar( direction.y );
  274. normal.set( 0, 1, 0 ).sub( vector ).normalize();
  275. } else {
  276. vector.copy( direction ).multiplyScalar( direction.x );
  277. normal.set( 1, 0, 0 ).sub( vector ).normalize();
  278. }
  279. } else {
  280. rotationAxis.crossVectors( prevDirection, direction );
  281. const rotAxisLength = rotationAxis.length();
  282. if ( rotAxisLength > 0.0001 ) {
  283. rotationAxis.divideScalar( rotAxisLength );
  284. vector.addVectors( prevDirection, direction );
  285. const c1 = - 2.0 / ( 1.0 + prevDirection.dot( direction ) );
  286. const dot = lastNormal.dot( vector );
  287. normal.copy( lastNormal ).addScaledVector( vector, c1 * dot );
  288. } else {
  289. normal.copy( lastNormal );
  290. }
  291. }
  292. side.crossVectors( direction, normal ).normalize();
  293. normal.crossVectors( side, direction ).normalize();
  294. if ( isFirstSegment === false ) {
  295. const smoothFactor = 0.3;
  296. normal.lerp( lastNormal, smoothFactor ).normalize();
  297. side.crossVectors( direction, normal ).normalize();
  298. normal.crossVectors( side, direction ).normalize();
  299. }
  300. lastNormal.copy( normal );
  301. prevDirection.copy( direction );
  302. matrix1.makeBasis( side, normal, vector.copy( direction ).negate() );
  303. }
  304. function moveTo( position ) {
  305. point2.copy( position );
  306. lastNormal.set( 0, 1, 0 );
  307. isFirstSegment = true;
  308. endCapStartIndex = null;
  309. endCapVertexCount = 0;
  310. }
  311. function lineTo( position ) {
  312. point1.copy( position );
  313. direction.subVectors( point1, point2 );
  314. const length = direction.length();
  315. if ( length === 0 ) return;
  316. direction.normalize();
  317. calculateRMF();
  318. if ( isFirstSegment === true ) {
  319. color2.copy( color1 );
  320. size2 = size1;
  321. matrix2.copy( matrix1 );
  322. addCap( point2, matrix2, false, size2 );
  323. // End cap is added immediately after start cap and updated in-place
  324. endCapStartIndex = geometry.drawRange.count;
  325. addCap( point1, matrix1, true, size1 );
  326. endCapVertexCount = geometry.drawRange.count - endCapStartIndex;
  327. }
  328. stroke( point1, point2, matrix1, matrix2, size1, size2 );
  329. updateEndCap( point1, matrix1, size1 );
  330. point2.copy( point1 );
  331. matrix2.copy( matrix1 );
  332. color2.copy( color1 );
  333. size2 = size1;
  334. isFirstSegment = false;
  335. }
  336. function setSize( value ) {
  337. size1 = value;
  338. }
  339. function setColor( value ) {
  340. color1.copy( value );
  341. }
  342. //
  343. let count = 0;
  344. function update() {
  345. const start = count;
  346. const end = geometry.drawRange.count;
  347. if ( start === end ) return;
  348. positions.addUpdateRange( start * 3, ( end - start ) * 3 );
  349. positions.needsUpdate = true;
  350. normals.addUpdateRange( start * 3, ( end - start ) * 3 );
  351. normals.needsUpdate = true;
  352. colors.addUpdateRange( start * 3, ( end - start ) * 3 );
  353. colors.needsUpdate = true;
  354. count = end;
  355. }
  356. return {
  357. /**
  358. * The "painted" tube mesh. Must be added to the scene.
  359. *
  360. * @name TubePainter#mesh
  361. * @type {Mesh}
  362. */
  363. mesh: mesh,
  364. /**
  365. * Moves the current painting position to the given value.
  366. *
  367. * @method
  368. * @name TubePainter#moveTo
  369. * @param {Vector3} position The new painting position.
  370. */
  371. moveTo: moveTo,
  372. /**
  373. * Draw a stroke from the current position to the given one.
  374. * This method extends the tube while drawing with the XR
  375. * controllers.
  376. *
  377. * @method
  378. * @name TubePainter#lineTo
  379. * @param {Vector3} position The destination position.
  380. */
  381. lineTo: lineTo,
  382. /**
  383. * Sets the size of newly rendered tube segments.
  384. *
  385. * @method
  386. * @name TubePainter#setSize
  387. * @param {number} size The size.
  388. */
  389. setSize: setSize,
  390. /**
  391. * Sets the color of newly rendered tube segments.
  392. *
  393. * @method
  394. * @name TubePainter#setColor
  395. * @param {Color} color The color.
  396. */
  397. setColor: setColor,
  398. /**
  399. * Updates the internal geometry buffers so the new painted
  400. * segments are rendered.
  401. *
  402. * @method
  403. * @name TubePainter#update
  404. */
  405. update: update
  406. };
  407. }
  408. export { TubePainter };
粤ICP备19079148号