webgl_modifier_curve.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 );
  84. light.position.set( - 10, 10, 10 );
  85. light.intensity = 1.0;
  86. scene.add( light );
  87. const light2 = new THREE.AmbientLight( 0x003973 );
  88. light2.intensity = 1.0;
  89. scene.add( light2 );
  90. //
  91. const loader = new FontLoader();
  92. loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {
  93. const geometry = new TextGeometry( 'Hello three.js!', {
  94. font: font,
  95. size: 0.2,
  96. height: 0.05,
  97. curveSegments: 12,
  98. bevelEnabled: true,
  99. bevelThickness: 0.02,
  100. bevelSize: 0.01,
  101. bevelOffset: 0,
  102. bevelSegments: 5,
  103. } );
  104. geometry.rotateX( Math.PI );
  105. const material = new THREE.MeshStandardMaterial( {
  106. color: 0x99ffff
  107. } );
  108. const objectToCurve = new THREE.Mesh( geometry, material );
  109. flow = new Flow( objectToCurve );
  110. flow.updateCurve( 0, curve );
  111. scene.add( flow.object3D );
  112. } );
  113. //
  114. renderer = new THREE.WebGLRenderer( { antialias: true } );
  115. renderer.setPixelRatio( window.devicePixelRatio );
  116. renderer.setSize( window.innerWidth, window.innerHeight );
  117. document.body.appendChild( renderer.domElement );
  118. renderer.domElement.addEventListener( 'pointerdown', onPointerDown );
  119. rayCaster = new THREE.Raycaster();
  120. control = new TransformControls( camera, renderer.domElement );
  121. control.addEventListener( 'dragging-changed', function ( event ) {
  122. if ( ! event.value ) {
  123. const points = curve.getPoints( 50 );
  124. line.geometry.setFromPoints( points );
  125. flow.updateCurve( 0, curve );
  126. }
  127. } );
  128. stats = new Stats();
  129. document.body.appendChild( stats.dom );
  130. window.addEventListener( 'resize', onWindowResize );
  131. }
  132. function onWindowResize() {
  133. camera.aspect = window.innerWidth / window.innerHeight;
  134. camera.updateProjectionMatrix();
  135. renderer.setSize( window.innerWidth, window.innerHeight );
  136. }
  137. function onPointerDown( event ) {
  138. action = ACTION_SELECT;
  139. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  140. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  141. }
  142. function animate() {
  143. requestAnimationFrame( animate );
  144. if ( action === ACTION_SELECT ) {
  145. rayCaster.setFromCamera( mouse, camera );
  146. action = ACTION_NONE;
  147. const intersects = rayCaster.intersectObjects( curveHandles, false );
  148. if ( intersects.length ) {
  149. const target = intersects[ 0 ].object;
  150. control.attach( target );
  151. scene.add( control );
  152. }
  153. }
  154. if ( flow ) {
  155. flow.moveAlongCurve( 0.001 );
  156. }
  157. render();
  158. }
  159. function render() {
  160. renderer.render( scene, camera );
  161. stats.update();
  162. }
  163. </script>
  164. </body>
  165. </html>