webgl_geometry_spline_editor.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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/DragControls.js"></script>
  19. <script src="js/controls/OrbitControls.js"></script>
  20. <script src="js/controls/TransformControls.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( 2000, 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( splineHelperObjects, camera, renderer.domElement ); //
  126. dragcontrols.enabled = false;
  127. dragcontrols.addEventListener( 'hoveron', function ( event ) {
  128. transformControl.attach( event.object );
  129. cancelHideTransorm();
  130. } );
  131. dragcontrols.addEventListener( 'hoveroff', function ( event ) {
  132. delayHideTransform();
  133. } );
  134. controls.addEventListener( 'start', function() {
  135. cancelHideTransorm();
  136. } );
  137. controls.addEventListener( 'end', function() {
  138. delayHideTransform();
  139. } );
  140. var hiding;
  141. function delayHideTransform() {
  142. cancelHideTransorm();
  143. hideTransform();
  144. }
  145. function hideTransform() {
  146. hiding = setTimeout( function() {
  147. transformControl.detach( transformControl.object );
  148. }, 2500 )
  149. }
  150. function cancelHideTransorm() {
  151. if ( hiding ) clearTimeout( hiding );
  152. }
  153. /*******
  154. * Curves
  155. *********/
  156. var i;
  157. for ( i = 0; i < splinePointsLength; i ++ ) {
  158. addSplineObject( positions[ i ] );
  159. }
  160. positions = [];
  161. for ( i = 0; i < splinePointsLength; i ++ ) {
  162. positions.push( splineHelperObjects[ i ].position );
  163. }
  164. var geometry = new THREE.Geometry();
  165. for ( var i = 0; i < ARC_SEGMENTS; i ++ ) {
  166. geometry.vertices.push( new THREE.Vector3() );
  167. }
  168. var curve;
  169. curve = new THREE.CatmullRomCurve3( positions );
  170. curve.type = 'catmullrom';
  171. curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
  172. color: 0xff0000,
  173. opacity: 0.35,
  174. linewidth: 2
  175. } ) );
  176. curve.mesh.castShadow = true;
  177. splines.uniform = curve;
  178. curve = new THREE.CatmullRomCurve3( positions );
  179. curve.type = 'centripetal';
  180. curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
  181. color: 0x00ff00,
  182. opacity: 0.35,
  183. linewidth: 2
  184. } ) );
  185. curve.mesh.castShadow = true;
  186. splines.centripetal = curve;
  187. curve = new THREE.CatmullRomCurve3( positions );
  188. curve.type = 'chordal';
  189. curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
  190. color: 0x0000ff,
  191. opacity: 0.35,
  192. linewidth: 2
  193. } ) );
  194. curve.mesh.castShadow = true;
  195. splines.chordal = curve;
  196. for ( var k in splines ) {
  197. var spline = splines[ k ];
  198. scene.add( spline.mesh );
  199. }
  200. load( [ new THREE.Vector3( 289.76843686945404, 452.51481137238443, 56.10018915737797 ),
  201. new THREE.Vector3( -53.56300074753207, 171.49711742836848, -14.495472686253045 ),
  202. new THREE.Vector3( -91.40118730204415, 176.4306956436485, -6.958271935582161 ),
  203. new THREE.Vector3( -383.785318791128, 491.1365363371675, 47.869296953772746 ) ] );
  204. }
  205. function addSplineObject( position ) {
  206. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( {
  207. color: Math.random() * 0xffffff
  208. } ) );
  209. object.material.ambient = object.material.color;
  210. if ( position ) {
  211. object.position.copy( position );
  212. } else {
  213. object.position.x = Math.random() * 1000 - 500;
  214. object.position.y = Math.random() * 600;
  215. object.position.z = Math.random() * 800 - 400;
  216. }
  217. object.castShadow = true;
  218. object.receiveShadow = true;
  219. scene.add( object );
  220. splineHelperObjects.push( object );
  221. return object;
  222. }
  223. function addPoint() {
  224. splinePointsLength ++;
  225. positions.push( addSplineObject()
  226. .position );
  227. updateSplineOutline();
  228. }
  229. function removePoint() {
  230. if ( splinePointsLength <= 4 ) {
  231. return;
  232. }
  233. splinePointsLength --;
  234. positions.pop();
  235. scene.remove( splineHelperObjects.pop() );
  236. updateSplineOutline();
  237. }
  238. function updateSplineOutline() {
  239. var p;
  240. for ( var k in splines ) {
  241. var spline = splines[ k ];
  242. splineMesh = spline.mesh;
  243. for ( var i = 0; i < ARC_SEGMENTS; i ++ ) {
  244. p = splineMesh.geometry.vertices[ i ];
  245. p.copy( spline.getPoint( i / ( ARC_SEGMENTS - 1 ) ) );
  246. }
  247. splineMesh.geometry.verticesNeedUpdate = true;
  248. }
  249. }
  250. function exportSpline() {
  251. var p;
  252. var strplace = [];
  253. for ( i = 0; i < splinePointsLength; i ++ ) {
  254. p = splineHelperObjects[ i ].position;
  255. strplace.push( 'new THREE.Vector3({0}, {1}, {2})'.format( p.x, p.y, p.z ) )
  256. }
  257. console.log( strplace.join( ',\n' ) );
  258. var code = '[' + ( strplace.join( ',\n\t' ) ) + ']';
  259. prompt( 'copy and paste code', code );
  260. }
  261. function load( new_positions ) {
  262. while ( new_positions.length > positions.length ) {
  263. addPoint();
  264. }
  265. while ( new_positions.length < positions.length ) {
  266. removePoint();
  267. }
  268. for ( i = 0; i < positions.length; i ++ ) {
  269. positions[ i ].copy( new_positions[ i ] );
  270. }
  271. updateSplineOutline();
  272. }
  273. function animate() {
  274. requestAnimationFrame( animate );
  275. render();
  276. stats.update();
  277. controls.update();
  278. transformControl.update();
  279. }
  280. function render() {
  281. splines.uniform.mesh.visible = uniform.checked;
  282. splines.centripetal.mesh.visible = centripetal.checked;
  283. splines.chordal.mesh.visible = chordal.checked;
  284. renderer.render( scene, camera );
  285. }
  286. </script>
  287. </body>
  288. </html>