TubePainter.js 15 KB

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