webgl_geometry_spline_editor.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. </style>
  15. </head>
  16. <body>
  17. <script src="../build/three.min.js"></script>
  18. <script src="js/controls/OrbitControls.js"></script>
  19. <script src="js/controls/TransformControls.js"></script>
  20. <script src="js/renderers/Projector.js"></script>
  21. <script src="js/controls/DragControls.js"></script>
  22. <script src="js/libs/stats.min.js"></script>
  23. <script>
  24. String.prototype.format = function () {
  25. var str = this;
  26. for ( var i = 0; i < arguments.length; i ++ ) {
  27. str = str.replace( '{' + i + '}', arguments[ i ] );
  28. }
  29. return str;
  30. }
  31. var container, stats;
  32. var camera, scene, renderer;
  33. var splineHelperObjects = [],
  34. splineOutline;
  35. var splinePointsLength = 4;
  36. var positions = [];
  37. var options;
  38. var geometry = new THREE.BoxGeometry( 20, 20, 20 );
  39. var ARC_SEGMENTS = 200;
  40. var splineMesh;
  41. var splines = {
  42. };
  43. init();
  44. animate();
  45. function init() {
  46. container = document.createElement( 'div' );
  47. document.body.appendChild( container );
  48. scene = new THREE.Scene();
  49. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  50. camera.position.z = 1000;
  51. scene.add( camera );
  52. scene.add( new THREE.AmbientLight( 0xf0f0f0 ) );
  53. var light = new THREE.SpotLight( 0xffffff, 1.5 );
  54. light.position.set( 0, 1500, 200 );
  55. light.castShadow = true;
  56. light.shadowCameraNear = 200;
  57. light.shadowCameraFar = camera.far;
  58. light.shadowCameraFov = 70;
  59. light.shadowBias = -0.000222;
  60. light.shadowDarkness = 0.25;
  61. light.shadowMapWidth = 1024;
  62. light.shadowMapHeight = 1024;
  63. scene.add( light );
  64. spotlight = light;
  65. var planeGeometry = new THREE.PlaneGeometry( 2000, 2000, 20, 20 );
  66. planeGeometry.applyMatrix( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) );
  67. var planeMaterials = [ new THREE.MeshBasicMaterial( {
  68. color: 0xeeeeee,
  69. opacity: 0.5
  70. } ), new THREE.MeshBasicMaterial( {
  71. color: 0x405040,
  72. wireframe: true,
  73. opacity: 0.2,
  74. transparent: true
  75. } ) ];
  76. var plane = THREE.SceneUtils.createMultiMaterialObject( planeGeometry, planeMaterials );
  77. plane.position.y = -200;
  78. plane.children[ 0 ].castShadow = false;
  79. plane.children[ 0 ].receiveShadow = true;
  80. scene.add( plane );
  81. var axis = new THREE.AxisHelper();
  82. axis.position.set( -500, -500, -500 );
  83. scene.add( axis );
  84. renderer = new THREE.WebGLRenderer( {
  85. antialias: true
  86. } );
  87. renderer.setClearColor( 0xf0f0f0 );
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. renderer.shadowMapEnabled = true;
  90. renderer.shadowMapSoft = true;
  91. container.appendChild( renderer.domElement );
  92. var info = document.createElement( 'div' );
  93. info.style.position = 'absolute';
  94. info.style.top = '10px';
  95. info.style.width = '100%';
  96. info.style.textAlign = 'center';
  97. info.innerHTML = 'catmull-rom rom spline comparisions';
  98. options = document.createElement( 'div' );
  99. options.style.position = 'absolute';
  100. options.style.top = '30px';
  101. options.style.width = '100%';
  102. options.style.textAlign = 'center';
  103. options.innerHTML = 'Points: <input type="button" onclick="addPoint();" value="+" />\
  104. <input type="button" onclick="removePoint();" value="-" />\
  105. <input type="button" onclick="exportSpline();" value="Export" /><br />\
  106. <input type="checkbox" id="uniform" checked /> <label for="uniform">Uniform Catmull-rom</label> <input type="range" id="tension" onchange="splines.uniform.tension = tension.value;updateSplineOutline();" min=0 max=1 step=0.01 value=0.5 /> <span id="tension_value" /></span> <br />\
  107. <input type="checkbox" id="centripetal" checked /> Centripetal Catmull-rom<br />\
  108. <input type="checkbox" id="chordal" checked /> Chordal Catmull-rom<br />';
  109. container.appendChild( info );
  110. container.appendChild( options );
  111. stats = new Stats();
  112. stats.domElement.style.position = 'absolute';
  113. stats.domElement.style.top = '0px';
  114. container.appendChild( stats.domElement );
  115. // Controls
  116. controls = new THREE.OrbitControls( camera, renderer.domElement );
  117. controls.damping = 0.2;
  118. controls.addEventListener( 'change', render );
  119. transformControl = new THREE.TransformControls( camera, renderer.domElement );
  120. transformControl.addEventListener( 'change', render );
  121. scene.add( transformControl );
  122. // Hiding transform situation is a little in a mess :()
  123. transformControl.addEventListener( 'change', function( e ) {
  124. cancelHideTransorm();
  125. } );
  126. transformControl.addEventListener( 'mouseDown', function( e ) {
  127. cancelHideTransorm();
  128. } );
  129. transformControl.addEventListener( 'mouseUp', function( e ) {
  130. delayHideTransform();
  131. } );
  132. transformControl.addEventListener( 'objectChange', function( e ) {
  133. updateSplineOutline();
  134. } );
  135. var dragcontrols = new THREE.DragControls( camera, splineHelperObjects, renderer.domElement ); //
  136. dragcontrols.on( 'hoveron', function( e ) {
  137. transformControl.attach( e.object );
  138. cancelHideTransorm(); // *
  139. } )
  140. dragcontrols.on( 'hoveroff', function( e ) {
  141. if ( e ) delayHideTransform();
  142. } )
  143. controls.addEventListener( 'start', function() {
  144. cancelHideTransorm();
  145. } );
  146. controls.addEventListener( 'end', function() {
  147. delayHideTransform();
  148. } );
  149. var hiding;
  150. function delayHideTransform() {
  151. cancelHideTransorm();
  152. hideTransform();
  153. }
  154. function hideTransform() {
  155. hiding = setTimeout( function() {
  156. transformControl.detach( transformControl.object );
  157. }, 2500 )
  158. }
  159. function cancelHideTransorm() {
  160. if ( hiding ) clearTimeout( hiding );
  161. }
  162. /*******
  163. * Curves
  164. *********/
  165. var i;
  166. for ( i = 0; i < splinePointsLength; i ++ ) {
  167. addSplineObject( positions[ i ] );
  168. }
  169. positions = [];
  170. for ( i = 0; i < splinePointsLength; i ++ ) {
  171. positions.push( splineHelperObjects[ i ].position );
  172. }
  173. var geometry = new THREE.Geometry();
  174. for ( var i = 0; i < ARC_SEGMENTS; i ++ ) {
  175. geometry.vertices.push( new THREE.Vector3() );
  176. }
  177. var curve;
  178. curve = new THREE.CatmullRomCurve3( positions );
  179. curve.type = 'catmullrom';
  180. curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
  181. color: 0xff0000,
  182. opacity: 0.35,
  183. linewidth: 2
  184. } ) );
  185. splines.uniform = curve;
  186. curve = new THREE.CatmullRomCurve3( positions );
  187. curve.type = 'centripetal';
  188. curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
  189. color: 0x00ff00,
  190. opacity: 0.35,
  191. linewidth: 2
  192. } ) );
  193. splines.centripetal = curve;
  194. curve = new THREE.CatmullRomCurve3( positions );
  195. curve.type = 'chordal';
  196. curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
  197. color: 0x0000ff,
  198. opacity: 0.35,
  199. linewidth: 2
  200. } ) );
  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 object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( {
  213. color: Math.random() * 0xffffff
  214. } ) );
  215. object.material.ambient = object.material.color;
  216. if ( position ) {
  217. object.position.copy( position );
  218. } else {
  219. object.position.x = Math.random() * 1000 - 500;
  220. object.position.y = Math.random() * 600
  221. object.position.z = Math.random() * 800 - 400;
  222. }
  223. object.castShadow = true;
  224. object.receiveShadow = true;
  225. scene.add( object );
  226. splineHelperObjects.push( object );
  227. return object;
  228. }
  229. function addPoint() {
  230. splinePointsLength ++;
  231. positions.push( addSplineObject()
  232. .position );
  233. updateSplineOutline();
  234. }
  235. function removePoint() {
  236. if ( splinePointsLength <= 4 ) {
  237. return;
  238. }
  239. splinePointsLength --;
  240. positions.pop();
  241. scene.remove( splineHelperObjects.pop() );
  242. updateSplineOutline();
  243. }
  244. function updateSplineOutline() {
  245. var p;
  246. for ( var k in splines ) {
  247. var spline = splines[ k ];
  248. splineMesh = spline.mesh;
  249. for ( var i = 0; i < ARC_SEGMENTS; i ++ ) {
  250. p = splineMesh.geometry.vertices[ i ];
  251. p.copy( spline.getPoint( i / ( ARC_SEGMENTS - 1 ) ) );
  252. }
  253. splineMesh.geometry.verticesNeedUpdate = true;
  254. }
  255. }
  256. function exportSpline() {
  257. var p;
  258. var strplace = [];
  259. for ( i = 0; i < splinePointsLength; i ++ ) {
  260. p = splineHelperObjects[ i ].position;
  261. strplace.push( 'new THREE.Vector3({0}, {1}, {2})'.format( p.x, p.y, p.z ) )
  262. }
  263. console.log( strplace.join( ',\n' ) );
  264. var code = '[' + ( strplace.join( ',\n\t' ) ) + ']';
  265. prompt( 'copy and paste code', code );
  266. }
  267. function load( new_positions ) {
  268. while ( new_positions.length > positions.length ) {
  269. addPoint();
  270. }
  271. while ( new_positions.length < positions.length ) {
  272. removePoint();
  273. }
  274. for ( i = 0; i < positions.length; i ++ ) {
  275. positions[ i ].copy( new_positions[ i ] );
  276. }
  277. updateSplineOutline();
  278. }
  279. function animate() {
  280. requestAnimationFrame( animate );
  281. render();
  282. stats.update();
  283. controls.update();
  284. transformControl.update();
  285. }
  286. function render() {
  287. splines.uniform.mesh.visible = uniform.checked;
  288. splines.centripetal.mesh.visible = centripetal.checked;
  289. splines.chordal.mesh.visible = chordal.checked;
  290. renderer.render( scene, camera );
  291. }
  292. </script>
  293. </body>
  294. </html>