webgl_modifier_curve.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. <script type="module">
  17. import * as THREE from '../build/three.module.js';
  18. import { TransformControls } from './jsm/controls/TransformControls.js';
  19. import Stats from './jsm/libs/stats.module.js';
  20. import { Flow } from './jsm/modifiers/CurveModifier.js';
  21. import { FontLoader } from './jsm/loaders/FontLoader.js';
  22. import { TextGeometry } from './jsm/geometries/TextGeometry.js';
  23. const ACTION_SELECT = 1, ACTION_NONE = 0;
  24. const curveHandles = [];
  25. const mouse = new THREE.Vector2();
  26. let stats;
  27. let scene,
  28. camera,
  29. renderer,
  30. rayCaster,
  31. control,
  32. flow,
  33. action = ACTION_NONE;
  34. init();
  35. animate();
  36. function init() {
  37. scene = new THREE.Scene();
  38. camera = new THREE.PerspectiveCamera(
  39. 40,
  40. window.innerWidth / window.innerHeight,
  41. 1,
  42. 1000
  43. );
  44. camera.position.set( 2, 2, 4 );
  45. camera.lookAt( scene.position );
  46. const initialPoints = [
  47. { x: 1, y: 0, z: - 1 },
  48. { x: 1, y: 0, z: 1 },
  49. { x: - 1, y: 0, z: 1 },
  50. { x: - 1, y: 0, z: - 1 },
  51. ];
  52. const boxGeometry = new THREE.BoxGeometry( 0.1, 0.1, 0.1 );
  53. const boxMaterial = new THREE.MeshBasicMaterial();
  54. for ( const handlePos of initialPoints ) {
  55. const handle = new THREE.Mesh( boxGeometry, boxMaterial );
  56. handle.position.copy( handlePos );
  57. curveHandles.push( handle );
  58. scene.add( handle );
  59. }
  60. const curve = new THREE.CatmullRomCurve3(
  61. curveHandles.map( ( handle ) => handle.position )
  62. );
  63. curve.curveType = 'centripetal';
  64. curve.closed = true;
  65. const points = curve.getPoints( 50 );
  66. const line = new THREE.LineLoop(
  67. new THREE.BufferGeometry().setFromPoints( points ),
  68. new THREE.LineBasicMaterial( { color: 0x00ff00 } )
  69. );
  70. scene.add( line );
  71. //
  72. const light = new THREE.DirectionalLight( 0xffaa33 );
  73. light.position.set( - 10, 10, 10 );
  74. light.intensity = 1.0;
  75. scene.add( light );
  76. const light2 = new THREE.AmbientLight( 0x003973 );
  77. light2.intensity = 1.0;
  78. scene.add( light2 );
  79. //
  80. const loader = new FontLoader();
  81. loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {
  82. const geometry = new TextGeometry( 'Hello three.js!', {
  83. font: font,
  84. size: 0.2,
  85. height: 0.05,
  86. curveSegments: 12,
  87. bevelEnabled: true,
  88. bevelThickness: 0.02,
  89. bevelSize: 0.01,
  90. bevelOffset: 0,
  91. bevelSegments: 5,
  92. } );
  93. geometry.rotateX( Math.PI );
  94. const material = new THREE.MeshStandardMaterial( {
  95. color: 0x99ffff
  96. } );
  97. const objectToCurve = new THREE.Mesh( geometry, material );
  98. flow = new Flow( objectToCurve );
  99. flow.updateCurve( 0, curve );
  100. scene.add( flow.object3D );
  101. } );
  102. //
  103. renderer = new THREE.WebGLRenderer( { antialias: true } );
  104. renderer.setPixelRatio( window.devicePixelRatio );
  105. renderer.setSize( window.innerWidth, window.innerHeight );
  106. document.body.appendChild( renderer.domElement );
  107. renderer.domElement.addEventListener( 'pointerdown', onPointerDown );
  108. rayCaster = new THREE.Raycaster();
  109. control = new TransformControls( camera, renderer.domElement );
  110. control.addEventListener( 'dragging-changed', function ( event ) {
  111. if ( ! event.value ) {
  112. const points = curve.getPoints( 50 );
  113. line.geometry.setFromPoints( points );
  114. flow.updateCurve( 0, curve );
  115. }
  116. } );
  117. stats = new Stats();
  118. document.body.appendChild( stats.dom );
  119. window.addEventListener( 'resize', onWindowResize );
  120. }
  121. function onWindowResize() {
  122. camera.aspect = window.innerWidth / window.innerHeight;
  123. camera.updateProjectionMatrix();
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. }
  126. function onPointerDown( event ) {
  127. action = ACTION_SELECT;
  128. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  129. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  130. }
  131. function animate() {
  132. requestAnimationFrame( animate );
  133. if ( action === ACTION_SELECT ) {
  134. rayCaster.setFromCamera( mouse, camera );
  135. action = ACTION_NONE;
  136. const intersects = rayCaster.intersectObjects( curveHandles, false );
  137. if ( intersects.length ) {
  138. const target = intersects[ 0 ].object;
  139. control.attach( target );
  140. scene.add( control );
  141. }
  142. }
  143. if ( flow ) {
  144. flow.moveAlongCurve( 0.001 );
  145. }
  146. render();
  147. }
  148. function render() {
  149. renderer.render( scene, camera );
  150. stats.update();
  151. }
  152. </script>
  153. </body>
  154. </html>