webgl_modifier_curve.html 4.8 KB

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