2
0

webgl_geometry_spline_editor.html 9.4 KB

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