webgl_geometry_spline_editor.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - catmull spline editor</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> - geometry - catmull spline editor
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.module.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. import { TransformControls } from 'three/addons/controls/TransformControls.js';
  36. let container;
  37. let camera, scene, renderer;
  38. const splineHelperObjects = [];
  39. let splinePointsLength = 4;
  40. const positions = [];
  41. const point = new THREE.Vector3();
  42. const raycaster = new THREE.Raycaster();
  43. const pointer = new THREE.Vector2();
  44. const onUpPosition = new THREE.Vector2();
  45. const onDownPosition = new THREE.Vector2();
  46. const geometry = new THREE.BoxGeometry( 20, 20, 20 );
  47. let transformControl;
  48. const ARC_SEGMENTS = 200;
  49. const splines = {};
  50. const params = {
  51. uniform: true,
  52. tension: 0.5,
  53. centripetal: true,
  54. chordal: true,
  55. addPoint: addPoint,
  56. removePoint: removePoint,
  57. exportSpline: exportSpline
  58. };
  59. init();
  60. function init() {
  61. container = document.getElementById( 'container' );
  62. scene = new THREE.Scene();
  63. scene.background = new THREE.Color( 0xf0f0f0 );
  64. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  65. camera.position.set( 0, 250, 1000 );
  66. scene.add( camera );
  67. scene.add( new THREE.AmbientLight( 0xf0f0f0, 3 ) );
  68. const light = new THREE.SpotLight( 0xffffff, 4.5 );
  69. light.position.set( 0, 1500, 200 );
  70. light.angle = Math.PI * 0.2;
  71. light.decay = 0;
  72. light.castShadow = true;
  73. light.shadow.camera.near = 200;
  74. light.shadow.camera.far = 2000;
  75. light.shadow.bias = - 0.000222;
  76. light.shadow.mapSize.width = 1024;
  77. light.shadow.mapSize.height = 1024;
  78. scene.add( light );
  79. const planeGeometry = new THREE.PlaneGeometry( 2000, 2000 );
  80. planeGeometry.rotateX( - Math.PI / 2 );
  81. const planeMaterial = new THREE.ShadowMaterial( { color: 0x000000, opacity: 0.2 } );
  82. const plane = new THREE.Mesh( planeGeometry, planeMaterial );
  83. plane.position.y = - 200;
  84. plane.receiveShadow = true;
  85. scene.add( plane );
  86. const helper = new THREE.GridHelper( 2000, 100 );
  87. helper.position.y = - 199;
  88. helper.material.opacity = 0.25;
  89. helper.material.transparent = true;
  90. scene.add( helper );
  91. renderer = new THREE.WebGLRenderer( { antialias: true } );
  92. renderer.setPixelRatio( window.devicePixelRatio );
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. renderer.shadowMap.enabled = true;
  95. container.appendChild( renderer.domElement );
  96. const gui = new GUI();
  97. gui.add( params, 'uniform' ).onChange( render );
  98. gui.add( params, 'tension', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
  99. splines.uniform.tension = value;
  100. updateSplineOutline();
  101. render();
  102. } );
  103. gui.add( params, 'centripetal' ).onChange( render );
  104. gui.add( params, 'chordal' ).onChange( render );
  105. gui.add( params, 'addPoint' );
  106. gui.add( params, 'removePoint' );
  107. gui.add( params, 'exportSpline' );
  108. gui.open();
  109. // Controls
  110. const controls = new OrbitControls( camera, renderer.domElement );
  111. controls.damping = 0.2;
  112. controls.addEventListener( 'change', render );
  113. transformControl = new TransformControls( camera, renderer.domElement );
  114. transformControl.addEventListener( 'change', render );
  115. transformControl.addEventListener( 'dragging-changed', function ( event ) {
  116. controls.enabled = ! event.value;
  117. } );
  118. scene.add( transformControl.getHelper() );
  119. transformControl.addEventListener( 'objectChange', function () {
  120. updateSplineOutline();
  121. } );
  122. document.addEventListener( 'pointerdown', onPointerDown );
  123. document.addEventListener( 'pointerup', onPointerUp );
  124. document.addEventListener( 'pointermove', onPointerMove );
  125. window.addEventListener( 'resize', onWindowResize );
  126. /*******
  127. * Curves
  128. *********/
  129. for ( let i = 0; i < splinePointsLength; i ++ ) {
  130. addSplineObject( positions[ i ] );
  131. }
  132. positions.length = 0;
  133. for ( let i = 0; i < splinePointsLength; i ++ ) {
  134. positions.push( splineHelperObjects[ i ].position );
  135. }
  136. const geometry = new THREE.BufferGeometry();
  137. geometry.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( ARC_SEGMENTS * 3 ), 3 ) );
  138. let curve = new THREE.CatmullRomCurve3( positions );
  139. curve.curveType = 'catmullrom';
  140. curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
  141. color: 0xff0000,
  142. opacity: 0.35
  143. } ) );
  144. curve.mesh.castShadow = true;
  145. splines.uniform = curve;
  146. curve = new THREE.CatmullRomCurve3( positions );
  147. curve.curveType = 'centripetal';
  148. curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
  149. color: 0x00ff00,
  150. opacity: 0.35
  151. } ) );
  152. curve.mesh.castShadow = true;
  153. splines.centripetal = curve;
  154. curve = new THREE.CatmullRomCurve3( positions );
  155. curve.curveType = 'chordal';
  156. curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
  157. color: 0x0000ff,
  158. opacity: 0.35
  159. } ) );
  160. curve.mesh.castShadow = true;
  161. splines.chordal = curve;
  162. for ( const k in splines ) {
  163. const spline = splines[ k ];
  164. scene.add( spline.mesh );
  165. }
  166. load( [ new THREE.Vector3( 289.76843686945404, 452.51481137238443, 56.10018915737797 ),
  167. new THREE.Vector3( - 53.56300074753207, 171.49711742836848, - 14.495472686253045 ),
  168. new THREE.Vector3( - 91.40118730204415, 176.4306956436485, - 6.958271935582161 ),
  169. new THREE.Vector3( - 383.785318791128, 491.1365363371675, 47.869296953772746 ) ] );
  170. render();
  171. }
  172. function addSplineObject( position ) {
  173. const material = new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } );
  174. const object = new THREE.Mesh( geometry, material );
  175. if ( position ) {
  176. object.position.copy( position );
  177. } else {
  178. object.position.x = Math.random() * 1000 - 500;
  179. object.position.y = Math.random() * 600;
  180. object.position.z = Math.random() * 800 - 400;
  181. }
  182. object.castShadow = true;
  183. object.receiveShadow = true;
  184. scene.add( object );
  185. splineHelperObjects.push( object );
  186. return object;
  187. }
  188. function addPoint() {
  189. splinePointsLength ++;
  190. positions.push( addSplineObject().position );
  191. updateSplineOutline();
  192. render();
  193. }
  194. function removePoint() {
  195. if ( splinePointsLength <= 4 ) {
  196. return;
  197. }
  198. const point = splineHelperObjects.pop();
  199. splinePointsLength --;
  200. positions.pop();
  201. if ( transformControl.object === point ) transformControl.detach();
  202. scene.remove( point );
  203. updateSplineOutline();
  204. render();
  205. }
  206. function updateSplineOutline() {
  207. for ( const k in splines ) {
  208. const spline = splines[ k ];
  209. const splineMesh = spline.mesh;
  210. const position = splineMesh.geometry.attributes.position;
  211. for ( let i = 0; i < ARC_SEGMENTS; i ++ ) {
  212. const t = i / ( ARC_SEGMENTS - 1 );
  213. spline.getPoint( t, point );
  214. position.setXYZ( i, point.x, point.y, point.z );
  215. }
  216. position.needsUpdate = true;
  217. }
  218. }
  219. function exportSpline() {
  220. const strplace = [];
  221. for ( let i = 0; i < splinePointsLength; i ++ ) {
  222. const p = splineHelperObjects[ i ].position;
  223. strplace.push( `new THREE.Vector3(${p.x}, ${p.y}, ${p.z})` );
  224. }
  225. console.log( strplace.join( ',\n' ) );
  226. const code = '[' + ( strplace.join( ',\n\t' ) ) + ']';
  227. prompt( 'copy and paste code', code );
  228. }
  229. function load( new_positions ) {
  230. while ( new_positions.length > positions.length ) {
  231. addPoint();
  232. }
  233. while ( new_positions.length < positions.length ) {
  234. removePoint();
  235. }
  236. for ( let i = 0; i < positions.length; i ++ ) {
  237. positions[ i ].copy( new_positions[ i ] );
  238. }
  239. updateSplineOutline();
  240. }
  241. function render() {
  242. splines.uniform.mesh.visible = params.uniform;
  243. splines.centripetal.mesh.visible = params.centripetal;
  244. splines.chordal.mesh.visible = params.chordal;
  245. renderer.render( scene, camera );
  246. }
  247. function onPointerDown( event ) {
  248. onDownPosition.x = event.clientX;
  249. onDownPosition.y = event.clientY;
  250. }
  251. function onPointerUp( event ) {
  252. onUpPosition.x = event.clientX;
  253. onUpPosition.y = event.clientY;
  254. if ( onDownPosition.distanceTo( onUpPosition ) === 0 ) {
  255. transformControl.detach();
  256. render();
  257. }
  258. }
  259. function onPointerMove( event ) {
  260. pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  261. pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  262. raycaster.setFromCamera( pointer, camera );
  263. const intersects = raycaster.intersectObjects( splineHelperObjects, false );
  264. if ( intersects.length > 0 ) {
  265. const object = intersects[ 0 ].object;
  266. if ( object !== transformControl.object ) {
  267. transformControl.attach( object );
  268. }
  269. }
  270. }
  271. function onWindowResize() {
  272. camera.aspect = window.innerWidth / window.innerHeight;
  273. camera.updateProjectionMatrix();
  274. renderer.setSize( window.innerWidth, window.innerHeight );
  275. render();
  276. }
  277. </script>
  278. </body>
  279. </html>