webgl_geometry_extrude_splines.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - spline extrusion</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #f0f0f0;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="container"></div>
  20. <div id="info">
  21. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - spline extrusion examples
  22. </div>
  23. <script type="module">
  24. import * as THREE from '../build/three.module.js';
  25. import Stats from './jsm/libs/stats.module.js';
  26. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  27. import { Curves } from './jsm/curves/CurveExtras.js';
  28. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  29. let container, stats;
  30. let camera, scene, renderer, splineCamera, cameraHelper, cameraEye;
  31. const direction = new THREE.Vector3();
  32. const binormal = new THREE.Vector3();
  33. const normal = new THREE.Vector3();
  34. const position = new THREE.Vector3();
  35. const lookAt = new THREE.Vector3();
  36. const pipeSpline = new THREE.CatmullRomCurve3( [
  37. new THREE.Vector3( 0, 10, - 10 ), new THREE.Vector3( 10, 0, - 10 ),
  38. new THREE.Vector3( 20, 0, 0 ), new THREE.Vector3( 30, 0, 10 ),
  39. new THREE.Vector3( 30, 0, 20 ), new THREE.Vector3( 20, 0, 30 ),
  40. new THREE.Vector3( 10, 0, 30 ), new THREE.Vector3( 0, 0, 30 ),
  41. new THREE.Vector3( - 10, 10, 30 ), new THREE.Vector3( - 10, 20, 30 ),
  42. new THREE.Vector3( 0, 30, 30 ), new THREE.Vector3( 10, 30, 30 ),
  43. new THREE.Vector3( 20, 30, 15 ), new THREE.Vector3( 10, 30, 10 ),
  44. new THREE.Vector3( 0, 30, 10 ), new THREE.Vector3( - 10, 20, 10 ),
  45. new THREE.Vector3( - 10, 10, 10 ), new THREE.Vector3( 0, 0, 10 ),
  46. new THREE.Vector3( 10, - 10, 10 ), new THREE.Vector3( 20, - 15, 10 ),
  47. new THREE.Vector3( 30, - 15, 10 ), new THREE.Vector3( 40, - 15, 10 ),
  48. new THREE.Vector3( 50, - 15, 10 ), new THREE.Vector3( 60, 0, 10 ),
  49. new THREE.Vector3( 70, 0, 0 ), new THREE.Vector3( 80, 0, 0 ),
  50. new THREE.Vector3( 90, 0, 0 ), new THREE.Vector3( 100, 0, 0 )
  51. ] );
  52. const sampleClosedSpline = new THREE.CatmullRomCurve3( [
  53. new THREE.Vector3( 0, - 40, - 40 ),
  54. new THREE.Vector3( 0, 40, - 40 ),
  55. new THREE.Vector3( 0, 140, - 40 ),
  56. new THREE.Vector3( 0, 40, 40 ),
  57. new THREE.Vector3( 0, - 40, 40 )
  58. ] );
  59. sampleClosedSpline.curveType = 'catmullrom';
  60. sampleClosedSpline.closed = true;
  61. // Keep a dictionary of Curve instances
  62. const splines = {
  63. GrannyKnot: new Curves.GrannyKnot(),
  64. HeartCurve: new Curves.HeartCurve( 3.5 ),
  65. VivianiCurve: new Curves.VivianiCurve( 70 ),
  66. KnotCurve: new Curves.KnotCurve(),
  67. HelixCurve: new Curves.HelixCurve(),
  68. TrefoilKnot: new Curves.TrefoilKnot(),
  69. TorusKnot: new Curves.TorusKnot( 20 ),
  70. CinquefoilKnot: new Curves.CinquefoilKnot( 20 ),
  71. TrefoilPolynomialKnot: new Curves.TrefoilPolynomialKnot( 14 ),
  72. FigureEightPolynomialKnot: new Curves.FigureEightPolynomialKnot(),
  73. DecoratedTorusKnot4a: new Curves.DecoratedTorusKnot4a(),
  74. DecoratedTorusKnot4b: new Curves.DecoratedTorusKnot4b(),
  75. DecoratedTorusKnot5a: new Curves.DecoratedTorusKnot5a(),
  76. DecoratedTorusKnot5c: new Curves.DecoratedTorusKnot5c(),
  77. PipeSpline: pipeSpline,
  78. SampleClosedSpline: sampleClosedSpline
  79. };
  80. let parent, tubeGeometry, mesh;
  81. const params = {
  82. spline: 'GrannyKnot',
  83. scale: 4,
  84. extrusionSegments: 100,
  85. radiusSegments: 3,
  86. closed: true,
  87. animationView: false,
  88. lookAhead: false,
  89. cameraHelper: false,
  90. };
  91. const material = new THREE.MeshLambertMaterial( { color: 0xff00ff } );
  92. const wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, opacity: 0.3, wireframe: true, transparent: true } );
  93. function addTube() {
  94. if ( mesh !== undefined ) {
  95. parent.remove( mesh );
  96. mesh.geometry.dispose();
  97. }
  98. const extrudePath = splines[ params.spline ];
  99. tubeGeometry = new THREE.TubeGeometry( extrudePath, params.extrusionSegments, 2, params.radiusSegments, params.closed );
  100. addGeometry( tubeGeometry );
  101. setScale();
  102. }
  103. function setScale() {
  104. mesh.scale.set( params.scale, params.scale, params.scale );
  105. }
  106. function addGeometry( geometry ) {
  107. // 3D shape
  108. mesh = new THREE.Mesh( geometry, material );
  109. const wireframe = new THREE.Mesh( geometry, wireframeMaterial );
  110. mesh.add( wireframe );
  111. parent.add( mesh );
  112. }
  113. function animateCamera() {
  114. cameraHelper.visible = params.cameraHelper;
  115. cameraEye.visible = params.cameraHelper;
  116. }
  117. init();
  118. animate();
  119. function init() {
  120. container = document.getElementById( 'container' );
  121. // camera
  122. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 10000 );
  123. camera.position.set( 0, 50, 500 );
  124. // scene
  125. scene = new THREE.Scene();
  126. scene.background = new THREE.Color( 0xf0f0f0 );
  127. // light
  128. const light = new THREE.DirectionalLight( 0xffffff );
  129. light.position.set( 0, 0, 1 );
  130. scene.add( light );
  131. // tube
  132. parent = new THREE.Object3D();
  133. scene.add( parent );
  134. splineCamera = new THREE.PerspectiveCamera( 84, window.innerWidth / window.innerHeight, 0.01, 1000 );
  135. parent.add( splineCamera );
  136. cameraHelper = new THREE.CameraHelper( splineCamera );
  137. scene.add( cameraHelper );
  138. addTube();
  139. // debug camera
  140. cameraEye = new THREE.Mesh( new THREE.SphereGeometry( 5 ), new THREE.MeshBasicMaterial( { color: 0xdddddd } ) );
  141. parent.add( cameraEye );
  142. cameraHelper.visible = params.cameraHelper;
  143. cameraEye.visible = params.cameraHelper;
  144. // renderer
  145. renderer = new THREE.WebGLRenderer( { antialias: true } );
  146. renderer.setPixelRatio( window.devicePixelRatio );
  147. renderer.setSize( window.innerWidth, window.innerHeight );
  148. container.appendChild( renderer.domElement );
  149. // stats
  150. stats = new Stats();
  151. container.appendChild( stats.dom );
  152. // dat.GUI
  153. const gui = new GUI( { width: 285 } );
  154. const folderGeometry = gui.addFolder( 'Geometry' );
  155. folderGeometry.add( params, 'spline', Object.keys( splines ) ).onChange( function () {
  156. addTube();
  157. } );
  158. folderGeometry.add( params, 'scale', 2, 10 ).step( 2 ).onChange( function () {
  159. setScale();
  160. } );
  161. folderGeometry.add( params, 'extrusionSegments', 50, 500 ).step( 50 ).onChange( function () {
  162. addTube();
  163. } );
  164. folderGeometry.add( params, 'radiusSegments', 2, 12 ).step( 1 ).onChange( function () {
  165. addTube();
  166. } );
  167. folderGeometry.add( params, 'closed' ).onChange( function () {
  168. addTube();
  169. } );
  170. folderGeometry.open();
  171. const folderCamera = gui.addFolder( 'Camera' );
  172. folderCamera.add( params, 'animationView' ).onChange( function () {
  173. animateCamera();
  174. } );
  175. folderCamera.add( params, 'lookAhead' ).onChange( function () {
  176. animateCamera();
  177. } );
  178. folderCamera.add( params, 'cameraHelper' ).onChange( function () {
  179. animateCamera();
  180. } );
  181. folderCamera.open();
  182. const controls = new OrbitControls( camera, renderer.domElement );
  183. controls.minDistance = 100;
  184. controls.maxDistance = 2000;
  185. window.addEventListener( 'resize', onWindowResize );
  186. }
  187. function onWindowResize() {
  188. camera.aspect = window.innerWidth / window.innerHeight;
  189. camera.updateProjectionMatrix();
  190. renderer.setSize( window.innerWidth, window.innerHeight );
  191. }
  192. //
  193. function animate() {
  194. requestAnimationFrame( animate );
  195. render();
  196. stats.update();
  197. }
  198. function render() {
  199. // animate camera along spline
  200. const time = Date.now();
  201. const looptime = 20 * 1000;
  202. const t = ( time % looptime ) / looptime;
  203. tubeGeometry.parameters.path.getPointAt( t, position );
  204. position.multiplyScalar( params.scale );
  205. // interpolation
  206. const segments = tubeGeometry.tangents.length;
  207. const pickt = t * segments;
  208. const pick = Math.floor( pickt );
  209. const pickNext = ( pick + 1 ) % segments;
  210. binormal.subVectors( tubeGeometry.binormals[ pickNext ], tubeGeometry.binormals[ pick ] );
  211. binormal.multiplyScalar( pickt - pick ).add( tubeGeometry.binormals[ pick ] );
  212. tubeGeometry.parameters.path.getTangentAt( t, direction );
  213. const offset = 15;
  214. normal.copy( binormal ).cross( direction );
  215. // we move on a offset on its binormal
  216. position.add( normal.clone().multiplyScalar( offset ) );
  217. splineCamera.position.copy( position );
  218. cameraEye.position.copy( position );
  219. // using arclength for stablization in look ahead
  220. tubeGeometry.parameters.path.getPointAt( ( t + 30 / tubeGeometry.parameters.path.getLength() ) % 1, lookAt );
  221. lookAt.multiplyScalar( params.scale );
  222. // camera orientation 2 - up orientation via normal
  223. if ( ! params.lookAhead ) lookAt.copy( position ).add( direction );
  224. splineCamera.matrix.lookAt( splineCamera.position, lookAt, normal );
  225. splineCamera.quaternion.setFromRotationMatrix( splineCamera.matrix );
  226. cameraHelper.update();
  227. renderer.render( scene, params.animationView === true ? splineCamera : camera );
  228. }
  229. </script>
  230. </body>
  231. </html>