point.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #info {
  15. position: absolute;
  16. top: 0px;
  17. width: 100%;
  18. padding: 5px;
  19. font-family:Monospace;
  20. font-size:13px;
  21. text-align:center;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="container"></div>
  27. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - geometry - catmull spline editor</div>
  28. <script src="./node_modules/three/build/three.min.js"></script>
  29. <script src="./node_modules/three/examples/js/controls/DragControls.js"></script>
  30. <script src="./node_modules/three/examples/js/controls/OrbitControls.js"></script>
  31. <script src="./node_modules/three/examples/js/controls/TransformControls.js"></script>
  32. <script src="./node_modules/three/examples/js/libs/stats.min.js"></script>
  33. <script src="./node_modules/three/examples/js/libs/dat.gui.min.js"></script>
  34. <script>
  35. String.prototype.format = function () {
  36. var str = this;
  37. for ( var i = 0; i < arguments.length; i ++ ) {
  38. str = str.replace( '{' + i + '}', arguments[ i ] );
  39. }
  40. return str;
  41. };
  42. var container, stats;
  43. var camera, scene, renderer;
  44. var splineHelperObjects = [], splineOutline;
  45. var splinePointsLength = 4;
  46. var positions = [];
  47. var point = new THREE.Vector3();
  48. var options;
  49. var geometry = new THREE.BoxBufferGeometry( 20, 20, 20 );
  50. var transformControl;
  51. var ARC_SEGMENTS = 200;
  52. var splines = {};
  53. var 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. animate();
  64. function init() {
  65. container = document.getElementById( 'container' );
  66. scene = new THREE.Scene();
  67. scene.background = new THREE.Color( 0xf0f0f0 );
  68. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  69. camera.position.set( 0, 250, 1000 );
  70. scene.add( camera );
  71. scene.add( new THREE.AmbientLight( 0xf0f0f0 ) );
  72. var light = new THREE.SpotLight( 0xffffff, 1.5 );
  73. light.position.set( 0, 1500, 200 );
  74. light.castShadow = true;
  75. light.shadow = new THREE.LightShadow( new THREE.PerspectiveCamera( 70, 1, 200, 2000 ) );
  76. light.shadow.bias = -0.000222;
  77. light.shadow.mapSize.width = 1024;
  78. light.shadow.mapSize.height = 1024;
  79. scene.add( light );
  80. spotlight = light;
  81. var planeGeometry = new THREE.PlaneBufferGeometry( 2000, 2000 );
  82. planeGeometry.rotateX( - Math.PI / 2 );
  83. var planeMaterial = new THREE.ShadowMaterial( { opacity: 0.2 } );
  84. var plane = new THREE.Mesh( planeGeometry, planeMaterial );
  85. plane.position.y = -200;
  86. plane.receiveShadow = true;
  87. scene.add( plane );
  88. var helper = new THREE.GridHelper( 2000, 100 );
  89. helper.position.y = - 199;
  90. helper.material.opacity = 0.25;
  91. helper.material.transparent = true;
  92. scene.add( helper );
  93. //var axes = new THREE.AxesHelper( 1000 );
  94. //axes.position.set( - 500, - 500, - 500 );
  95. //scene.add( axes );
  96. renderer = new THREE.WebGLRenderer( { antialias: true } );
  97. renderer.setPixelRatio( window.devicePixelRatio );
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. renderer.shadowMap.enabled = true;
  100. container.appendChild( renderer.domElement );
  101. stats = new Stats();
  102. container.appendChild( stats.dom );
  103. var gui = new dat.GUI();
  104. gui.add( params, 'uniform' );
  105. gui.add( params, 'tension', 0, 1 ).step( 0.01 ).onChange( function( value ) {
  106. splines.uniform.tension = value;
  107. updateSplineOutline();
  108. });
  109. gui.add( params, 'centripetal' );
  110. gui.add( params, 'chordal' );
  111. gui.add( params, 'addPoint' );
  112. gui.add( params, 'removePoint' );
  113. gui.add( params, 'exportSpline' );
  114. gui.open();
  115. // Controls
  116. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  117. controls.damping = 0.2;
  118. controls.addEventListener( 'change', render );
  119. controls.addEventListener( 'start', function() {
  120. cancelHideTransorm();
  121. } );
  122. controls.addEventListener( 'end', function() {
  123. delayHideTransform();
  124. } );
  125. transformControl = new THREE.TransformControls( camera, renderer.domElement );
  126. transformControl.addEventListener( 'change', render );
  127. scene.add( transformControl );
  128. // Hiding transform situation is a little in a mess :()
  129. transformControl.addEventListener( 'change', function( e ) {
  130. cancelHideTransorm();
  131. } );
  132. transformControl.addEventListener( 'mouseDown', function( e ) {
  133. cancelHideTransorm();
  134. } );
  135. transformControl.addEventListener( 'mouseUp', function( e ) {
  136. delayHideTransform();
  137. } );
  138. transformControl.addEventListener( 'objectChange', function( e ) {
  139. updateSplineOutline();
  140. } );
  141. var dragcontrols = new THREE.DragControls( splineHelperObjects, camera, renderer.domElement ); //
  142. dragcontrols.enabled = false;
  143. dragcontrols.addEventListener( 'hoveron', function ( event ) {
  144. transformControl.attach( event.object );
  145. cancelHideTransorm();
  146. } );
  147. dragcontrols.addEventListener( 'hoveroff', function ( event ) {
  148. delayHideTransform();
  149. } );
  150. var hiding;
  151. function delayHideTransform() {
  152. cancelHideTransorm();
  153. hideTransform();
  154. }
  155. function hideTransform() {
  156. hiding = setTimeout( function() {
  157. transformControl.detach( transformControl.object );
  158. }, 2500 )
  159. }
  160. function cancelHideTransorm() {
  161. if ( hiding ) clearTimeout( hiding );
  162. }
  163. /*******
  164. * Curves
  165. *********/
  166. for ( var i = 0; i < splinePointsLength; i ++ ) {
  167. addSplineObject( positions[ i ] );
  168. }
  169. positions = [];
  170. for ( var i = 0; i < splinePointsLength; i ++ ) {
  171. positions.push( splineHelperObjects[ i ].position );
  172. }
  173. var geometry = new THREE.BufferGeometry();
  174. geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( ARC_SEGMENTS * 3 ), 3 ) );
  175. var curve = new THREE.CatmullRomCurve3( positions );
  176. curve.curveType = 'catmullrom';
  177. curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
  178. color: 0xff0000,
  179. opacity: 0.35,
  180. linewidth: 2
  181. } ) );
  182. curve.mesh.castShadow = true;
  183. splines.uniform = curve;
  184. curve = new THREE.CatmullRomCurve3( positions );
  185. curve.curveType = 'centripetal';
  186. curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
  187. color: 0x00ff00,
  188. opacity: 0.35,
  189. linewidth: 2
  190. } ) );
  191. curve.mesh.castShadow = true;
  192. splines.centripetal = curve;
  193. curve = new THREE.CatmullRomCurve3( positions );
  194. curve.curveType = 'chordal';
  195. curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
  196. color: 0x0000ff,
  197. opacity: 0.35,
  198. linewidth: 2
  199. } ) );
  200. curve.mesh.castShadow = true;
  201. splines.chordal = curve;
  202. for ( var k in splines ) {
  203. var spline = splines[ k ];
  204. scene.add( spline.mesh );
  205. }
  206. load( [ new THREE.Vector3( 289.76843686945404, 452.51481137238443, 56.10018915737797 ),
  207. new THREE.Vector3( -53.56300074753207, 171.49711742836848, -14.495472686253045 ),
  208. new THREE.Vector3( -91.40118730204415, 176.4306956436485, -6.958271935582161 ),
  209. new THREE.Vector3( -383.785318791128, 491.1365363371675, 47.869296953772746 ) ] );
  210. }
  211. function addSplineObject( position ) {
  212. var material = new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } );
  213. var object = new THREE.Mesh( geometry, material );
  214. if ( position ) {
  215. object.position.copy( position );
  216. } else {
  217. object.position.x = Math.random() * 1000 - 500;
  218. object.position.y = Math.random() * 600;
  219. object.position.z = Math.random() * 800 - 400;
  220. }
  221. object.castShadow = true;
  222. object.receiveShadow = true;
  223. scene.add( object );
  224. splineHelperObjects.push( object );
  225. return object;
  226. }
  227. function addPoint() {
  228. splinePointsLength ++;
  229. positions.push( addSplineObject().position );
  230. updateSplineOutline();
  231. }
  232. function removePoint() {
  233. if ( splinePointsLength <= 4 ) {
  234. return;
  235. }
  236. splinePointsLength --;
  237. positions.pop();
  238. scene.remove( splineHelperObjects.pop() );
  239. updateSplineOutline();
  240. }
  241. function updateSplineOutline() {
  242. for ( var k in splines ) {
  243. var spline = splines[ k ];
  244. var splineMesh = spline.mesh;
  245. var position = splineMesh.geometry.attributes.position;
  246. for ( var i = 0; i < ARC_SEGMENTS; i ++ ) {
  247. var t = i / ( ARC_SEGMENTS - 1 );
  248. spline.getPoint( t, point );
  249. position.setXYZ( i, point.x, point.y, point.z );
  250. }
  251. position.needsUpdate = true;
  252. }
  253. }
  254. function exportSpline() {
  255. var strplace = [];
  256. for ( var i = 0; i < splinePointsLength; i ++ ) {
  257. var p = splineHelperObjects[ i ].position;
  258. strplace.push( 'new THREE.Vector3({0}, {1}, {2})'.format( p.x, p.y, p.z ) )
  259. }
  260. console.log( strplace.join( ',\n' ) );
  261. var code = '[' + ( strplace.join( ',\n\t' ) ) + ']';
  262. prompt( 'copy and paste code', code );
  263. }
  264. function load( new_positions ) {
  265. while ( new_positions.length > positions.length ) {
  266. addPoint();
  267. }
  268. while ( new_positions.length < positions.length ) {
  269. removePoint();
  270. }
  271. for ( var i = 0; i < positions.length; i ++ ) {
  272. positions[ i ].copy( new_positions[ i ] );
  273. }
  274. updateSplineOutline();
  275. }
  276. function animate() {
  277. requestAnimationFrame( animate );
  278. render();
  279. stats.update();
  280. transformControl.update();
  281. }
  282. function render() {
  283. splines.uniform.mesh.visible = params.uniform;
  284. splines.centripetal.mesh.visible = params.centripetal;
  285. splines.chordal.mesh.visible = params.chordal;
  286. renderer.render( scene, camera );
  287. }
  288. </script>
  289. </body>
  290. </html>