webgl_geometry_spline_editor.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 { DragControls } from './jsm/controls/DragControls.js';
  28. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  29. import { TransformControls } from './jsm/controls/TransformControls.js';
  30. String.prototype.format = function () {
  31. var str = this;
  32. for ( var i = 0; i < arguments.length; i ++ ) {
  33. str = str.replace( '{' + i + '}', arguments[ i ] );
  34. }
  35. return str;
  36. };
  37. var container, stats;
  38. var camera, scene, renderer;
  39. var splineHelperObjects = [];
  40. var splinePointsLength = 4;
  41. var positions = [];
  42. var point = new THREE.Vector3();
  43. var geometry = new THREE.BoxBufferGeometry( 20, 20, 20 );
  44. var transformControl;
  45. var ARC_SEGMENTS = 200;
  46. var splines = {};
  47. var params = {
  48. uniform: true,
  49. tension: 0.5,
  50. centripetal: true,
  51. chordal: true,
  52. addPoint: addPoint,
  53. removePoint: removePoint,
  54. exportSpline: exportSpline
  55. };
  56. init();
  57. animate();
  58. function init() {
  59. container = document.getElementById( 'container' );
  60. scene = new THREE.Scene();
  61. scene.background = new THREE.Color( 0xf0f0f0 );
  62. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  63. camera.position.set( 0, 250, 1000 );
  64. scene.add( camera );
  65. scene.add( new THREE.AmbientLight( 0xf0f0f0 ) );
  66. var light = new THREE.SpotLight( 0xffffff, 1.5 );
  67. light.position.set( 0, 1500, 200 );
  68. light.angle = Math.PI * 0.2;
  69. light.castShadow = true;
  70. light.shadow.camera.near = 200;
  71. light.shadow.camera.far = 2000;
  72. light.shadow.bias = - 0.000222;
  73. light.shadow.mapSize.width = 1024;
  74. light.shadow.mapSize.height = 1024;
  75. scene.add( light );
  76. var planeGeometry = new THREE.PlaneBufferGeometry( 2000, 2000 );
  77. planeGeometry.rotateX( - Math.PI / 2 );
  78. var planeMaterial = new THREE.ShadowMaterial( { opacity: 0.2 } );
  79. var plane = new THREE.Mesh( planeGeometry, planeMaterial );
  80. plane.position.y = - 200;
  81. plane.receiveShadow = true;
  82. scene.add( plane );
  83. var helper = new THREE.GridHelper( 2000, 100 );
  84. helper.position.y = - 199;
  85. helper.material.opacity = 0.25;
  86. helper.material.transparent = true;
  87. scene.add( helper );
  88. //var axes = new AxesHelper( 1000 );
  89. //axes.position.set( - 500, - 500, - 500 );
  90. //scene.add( axes );
  91. renderer = new THREE.WebGLRenderer( { antialias: true } );
  92. renderer.setPixelRatio( window.devicePixelRatio );
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. renderer.shadowMap.enabled = true;
  95. container.appendChild( renderer.domElement );
  96. stats = new Stats();
  97. container.appendChild( stats.dom );
  98. var gui = new GUI();
  99. gui.add( params, 'uniform' );
  100. gui.add( params, 'tension', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
  101. splines.uniform.tension = value;
  102. updateSplineOutline();
  103. } );
  104. gui.add( params, 'centripetal' );
  105. gui.add( params, 'chordal' );
  106. gui.add( params, 'addPoint' );
  107. gui.add( params, 'removePoint' );
  108. gui.add( params, 'exportSpline' );
  109. gui.open();
  110. // Controls
  111. var controls = new OrbitControls( camera, renderer.domElement );
  112. controls.damping = 0.2;
  113. controls.addEventListener( 'change', render );
  114. controls.addEventListener( 'start', function () {
  115. cancelHideTransform();
  116. } );
  117. controls.addEventListener( 'end', function () {
  118. delayHideTransform();
  119. } );
  120. transformControl = new TransformControls( camera, renderer.domElement );
  121. transformControl.addEventListener( 'change', render );
  122. transformControl.addEventListener( 'dragging-changed', function ( event ) {
  123. controls.enabled = ! event.value;
  124. } );
  125. scene.add( transformControl );
  126. // Hiding transform situation is a little in a mess :()
  127. transformControl.addEventListener( 'change', function () {
  128. cancelHideTransform();
  129. } );
  130. transformControl.addEventListener( 'mouseDown', function () {
  131. cancelHideTransform();
  132. } );
  133. transformControl.addEventListener( 'mouseUp', function () {
  134. delayHideTransform();
  135. } );
  136. transformControl.addEventListener( 'objectChange', function () {
  137. updateSplineOutline();
  138. } );
  139. var dragcontrols = new DragControls( splineHelperObjects, camera, renderer.domElement ); //
  140. dragcontrols.enabled = false;
  141. dragcontrols.addEventListener( 'hoveron', function ( event ) {
  142. transformControl.attach( event.object );
  143. cancelHideTransform();
  144. } );
  145. dragcontrols.addEventListener( 'hoveroff', function () {
  146. delayHideTransform();
  147. } );
  148. var hiding;
  149. function delayHideTransform() {
  150. cancelHideTransform();
  151. hideTransform();
  152. }
  153. function hideTransform() {
  154. hiding = setTimeout( function () {
  155. transformControl.detach( transformControl.object );
  156. }, 2500 );
  157. }
  158. function cancelHideTransform() {
  159. if ( hiding ) clearTimeout( hiding );
  160. }
  161. /*******
  162. * Curves
  163. *********/
  164. for ( var i = 0; i < splinePointsLength; i ++ ) {
  165. addSplineObject( positions[ i ] );
  166. }
  167. positions = [];
  168. for ( var i = 0; i < splinePointsLength; i ++ ) {
  169. positions.push( splineHelperObjects[ i ].position );
  170. }
  171. var geometry = new THREE.BufferGeometry();
  172. geometry.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( ARC_SEGMENTS * 3 ), 3 ) );
  173. var curve = new THREE.CatmullRomCurve3( positions );
  174. curve.curveType = 'catmullrom';
  175. curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
  176. color: 0xff0000,
  177. opacity: 0.35
  178. } ) );
  179. curve.mesh.castShadow = true;
  180. splines.uniform = curve;
  181. curve = new THREE.CatmullRomCurve3( positions );
  182. curve.curveType = 'centripetal';
  183. curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
  184. color: 0x00ff00,
  185. opacity: 0.35
  186. } ) );
  187. curve.mesh.castShadow = true;
  188. splines.centripetal = curve;
  189. curve = new THREE.CatmullRomCurve3( positions );
  190. curve.curveType = 'chordal';
  191. curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
  192. color: 0x0000ff,
  193. opacity: 0.35
  194. } ) );
  195. curve.mesh.castShadow = true;
  196. splines.chordal = curve;
  197. for ( var k in splines ) {
  198. var spline = splines[ k ];
  199. scene.add( spline.mesh );
  200. }
  201. load( [ new THREE.Vector3( 289.76843686945404, 452.51481137238443, 56.10018915737797 ),
  202. new THREE.Vector3( - 53.56300074753207, 171.49711742836848, - 14.495472686253045 ),
  203. new THREE.Vector3( - 91.40118730204415, 176.4306956436485, - 6.958271935582161 ),
  204. new THREE.Vector3( - 383.785318791128, 491.1365363371675, 47.869296953772746 ) ] );
  205. }
  206. function addSplineObject( position ) {
  207. var material = new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } );
  208. var object = new THREE.Mesh( geometry, material );
  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().position );
  225. updateSplineOutline();
  226. }
  227. function removePoint() {
  228. if ( splinePointsLength <= 4 ) {
  229. return;
  230. }
  231. splinePointsLength --;
  232. positions.pop();
  233. scene.remove( splineHelperObjects.pop() );
  234. updateSplineOutline();
  235. }
  236. function updateSplineOutline() {
  237. for ( var k in splines ) {
  238. var spline = splines[ k ];
  239. var splineMesh = spline.mesh;
  240. var position = splineMesh.geometry.attributes.position;
  241. for ( var i = 0; i < ARC_SEGMENTS; i ++ ) {
  242. var t = i / ( ARC_SEGMENTS - 1 );
  243. spline.getPoint( t, point );
  244. position.setXYZ( i, point.x, point.y, point.z );
  245. }
  246. position.needsUpdate = true;
  247. }
  248. }
  249. function exportSpline() {
  250. var strplace = [];
  251. for ( var i = 0; i < splinePointsLength; i ++ ) {
  252. var p = splineHelperObjects[ i ].position;
  253. strplace.push( 'new THREE.Vector3({0}, {1}, {2})'.format( p.x, p.y, p.z ) );
  254. }
  255. console.log( strplace.join( ',\n' ) );
  256. var code = '[' + ( strplace.join( ',\n\t' ) ) + ']';
  257. prompt( 'copy and paste code', code );
  258. }
  259. function load( new_positions ) {
  260. while ( new_positions.length > positions.length ) {
  261. addPoint();
  262. }
  263. while ( new_positions.length < positions.length ) {
  264. removePoint();
  265. }
  266. for ( var i = 0; i < positions.length; i ++ ) {
  267. positions[ i ].copy( new_positions[ i ] );
  268. }
  269. updateSplineOutline();
  270. }
  271. function animate() {
  272. requestAnimationFrame( animate );
  273. render();
  274. stats.update();
  275. }
  276. function render() {
  277. splines.uniform.mesh.visible = params.uniform;
  278. splines.centripetal.mesh.visible = params.centripetal;
  279. splines.chordal.mesh.visible = params.chordal;
  280. renderer.render( scene, camera );
  281. }
  282. </script>
  283. </body>
  284. </html>