webgl_modifier_curve.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - curve modifier</title>
  5. <meta charset="utf-8" />
  6. <meta
  7. name="viewport"
  8. content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"
  9. />
  10. <link type="text/css" rel="stylesheet" href="main.css" />
  11. </head>
  12. <body>
  13. <div id="info">
  14. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - curve modifier
  15. </div>
  16. <!-- Import maps polyfill -->
  17. <!-- Remove this when import maps will be widely supported -->
  18. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.module.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three';
  29. import { TransformControls } from 'three/addons/controls/TransformControls.js';
  30. import Stats from 'three/addons/libs/stats.module.js';
  31. import { Flow } from 'three/addons/modifiers/CurveModifier.js';
  32. import { FontLoader } from 'three/addons/loaders/FontLoader.js';
  33. import { TextGeometry } from 'three/addons/geometries/TextGeometry.js';
  34. const ACTION_SELECT = 1, ACTION_NONE = 0;
  35. const curveHandles = [];
  36. const mouse = new THREE.Vector2();
  37. let stats;
  38. let scene,
  39. camera,
  40. renderer,
  41. rayCaster,
  42. control,
  43. flow,
  44. action = ACTION_NONE;
  45. init();
  46. animate();
  47. function init() {
  48. scene = new THREE.Scene();
  49. camera = new THREE.PerspectiveCamera(
  50. 40,
  51. window.innerWidth / window.innerHeight,
  52. 1,
  53. 1000
  54. );
  55. camera.position.set( 2, 2, 4 );
  56. camera.lookAt( scene.position );
  57. const initialPoints = [
  58. { x: 1, y: 0, z: - 1 },
  59. { x: 1, y: 0, z: 1 },
  60. { x: - 1, y: 0, z: 1 },
  61. { x: - 1, y: 0, z: - 1 },
  62. ];
  63. const boxGeometry = new THREE.BoxGeometry( 0.1, 0.1, 0.1 );
  64. const boxMaterial = new THREE.MeshBasicMaterial();
  65. for ( const handlePos of initialPoints ) {
  66. const handle = new THREE.Mesh( boxGeometry, boxMaterial );
  67. handle.position.copy( handlePos );
  68. curveHandles.push( handle );
  69. scene.add( handle );
  70. }
  71. const curve = new THREE.CatmullRomCurve3(
  72. curveHandles.map( ( handle ) => handle.position )
  73. );
  74. curve.curveType = 'centripetal';
  75. curve.closed = true;
  76. const points = curve.getPoints( 50 );
  77. const line = new THREE.LineLoop(
  78. new THREE.BufferGeometry().setFromPoints( points ),
  79. new THREE.LineBasicMaterial( { color: 0x00ff00 } )
  80. );
  81. scene.add( line );
  82. //
  83. const light = new THREE.DirectionalLight( 0xffaa33, 3 );
  84. light.position.set( - 10, 10, 10 );
  85. scene.add( light );
  86. const light2 = new THREE.AmbientLight( 0x003973, 3 );
  87. scene.add( light2 );
  88. //
  89. const loader = new FontLoader();
  90. loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {
  91. const geometry = new TextGeometry( 'Hello three.js!', {
  92. font: font,
  93. size: 0.2,
  94. height: 0.05,
  95. curveSegments: 12,
  96. bevelEnabled: true,
  97. bevelThickness: 0.02,
  98. bevelSize: 0.01,
  99. bevelOffset: 0,
  100. bevelSegments: 5,
  101. } );
  102. geometry.rotateX( Math.PI );
  103. const material = new THREE.MeshStandardMaterial( {
  104. color: 0x99ffff
  105. } );
  106. const objectToCurve = new THREE.Mesh( geometry, material );
  107. flow = new Flow( objectToCurve );
  108. flow.updateCurve( 0, curve );
  109. scene.add( flow.object3D );
  110. } );
  111. //
  112. renderer = new THREE.WebGLRenderer( { antialias: true } );
  113. renderer.setPixelRatio( window.devicePixelRatio );
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. document.body.appendChild( renderer.domElement );
  116. renderer.domElement.addEventListener( 'pointerdown', onPointerDown );
  117. rayCaster = new THREE.Raycaster();
  118. control = new TransformControls( camera, renderer.domElement );
  119. control.addEventListener( 'dragging-changed', function ( event ) {
  120. if ( ! event.value ) {
  121. const points = curve.getPoints( 50 );
  122. line.geometry.setFromPoints( points );
  123. flow.updateCurve( 0, curve );
  124. }
  125. } );
  126. stats = new Stats();
  127. document.body.appendChild( stats.dom );
  128. window.addEventListener( 'resize', onWindowResize );
  129. }
  130. function onWindowResize() {
  131. camera.aspect = window.innerWidth / window.innerHeight;
  132. camera.updateProjectionMatrix();
  133. renderer.setSize( window.innerWidth, window.innerHeight );
  134. }
  135. function onPointerDown( event ) {
  136. action = ACTION_SELECT;
  137. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  138. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  139. }
  140. function animate() {
  141. requestAnimationFrame( animate );
  142. if ( action === ACTION_SELECT ) {
  143. rayCaster.setFromCamera( mouse, camera );
  144. action = ACTION_NONE;
  145. const intersects = rayCaster.intersectObjects( curveHandles, false );
  146. if ( intersects.length ) {
  147. const target = intersects[ 0 ].object;
  148. control.attach( target );
  149. scene.add( control );
  150. }
  151. }
  152. if ( flow ) {
  153. flow.moveAlongCurve( 0.001 );
  154. }
  155. render();
  156. }
  157. function render() {
  158. renderer.render( scene, camera );
  159. stats.update();
  160. }
  161. </script>
  162. </body>
  163. </html>