webgl_geometry_spline_editor.html 10 KB

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