webgl_geometry_spline_editor.html 10.0 KB

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