123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - geometry - catmull spline editor</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- body {
- background-color: #f0f0f0;
- color: #444;
- }
- a {
- color: #08f;
- }
- </style>
- </head>
- <body>
- <div id="container"></div>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - geometry - catmull spline editor
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- import { DragControls } from './jsm/controls/DragControls.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { TransformControls } from './jsm/controls/TransformControls.js';
- String.prototype.format = function () {
- var str = this;
- for ( var i = 0; i < arguments.length; i ++ ) {
- str = str.replace( '{' + i + '}', arguments[ i ] );
- }
- return str;
- };
- var container, stats;
- var camera, scene, renderer;
- var splineHelperObjects = [];
- var splinePointsLength = 4;
- var positions = [];
- var point = new THREE.Vector3();
- var geometry = new THREE.BoxBufferGeometry( 20, 20, 20 );
- var transformControl;
- var ARC_SEGMENTS = 200;
- var splines = {};
- var params = {
- uniform: true,
- tension: 0.5,
- centripetal: true,
- chordal: true,
- addPoint: addPoint,
- removePoint: removePoint,
- exportSpline: exportSpline
- };
- init();
- animate();
- function init() {
- container = document.getElementById( 'container' );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xf0f0f0 );
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
- camera.position.set( 0, 250, 1000 );
- scene.add( camera );
- scene.add( new THREE.AmbientLight( 0xf0f0f0 ) );
- var light = new THREE.SpotLight( 0xffffff, 1.5 );
- light.position.set( 0, 1500, 200 );
- light.angle = Math.PI * 0.2;
- light.castShadow = true;
- light.shadow.camera.near = 200;
- light.shadow.camera.far = 2000;
- light.shadow.bias = - 0.000222;
- light.shadow.mapSize.width = 1024;
- light.shadow.mapSize.height = 1024;
- scene.add( light );
- var planeGeometry = new THREE.PlaneBufferGeometry( 2000, 2000 );
- planeGeometry.rotateX( - Math.PI / 2 );
- var planeMaterial = new THREE.ShadowMaterial( { opacity: 0.2 } );
- var plane = new THREE.Mesh( planeGeometry, planeMaterial );
- plane.position.y = - 200;
- plane.receiveShadow = true;
- scene.add( plane );
- var helper = new THREE.GridHelper( 2000, 100 );
- helper.position.y = - 199;
- helper.material.opacity = 0.25;
- helper.material.transparent = true;
- scene.add( helper );
- //var axes = new AxesHelper( 1000 );
- //axes.position.set( - 500, - 500, - 500 );
- //scene.add( axes );
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.shadowMap.enabled = true;
- container.appendChild( renderer.domElement );
- stats = new Stats();
- container.appendChild( stats.dom );
- var gui = new GUI();
- gui.add( params, 'uniform' );
- gui.add( params, 'tension', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
- splines.uniform.tension = value;
- updateSplineOutline();
- } );
- gui.add( params, 'centripetal' );
- gui.add( params, 'chordal' );
- gui.add( params, 'addPoint' );
- gui.add( params, 'removePoint' );
- gui.add( params, 'exportSpline' );
- gui.open();
- // Controls
- var controls = new OrbitControls( camera, renderer.domElement );
- controls.damping = 0.2;
- controls.addEventListener( 'change', render );
- controls.addEventListener( 'start', function () {
- cancelHideTransform();
- } );
- controls.addEventListener( 'end', function () {
- delayHideTransform();
- } );
- transformControl = new TransformControls( camera, renderer.domElement );
- transformControl.addEventListener( 'change', render );
- transformControl.addEventListener( 'dragging-changed', function ( event ) {
- controls.enabled = ! event.value;
- } );
- scene.add( transformControl );
- // Hiding transform situation is a little in a mess :()
- transformControl.addEventListener( 'change', function () {
- cancelHideTransform();
- } );
- transformControl.addEventListener( 'mouseDown', function () {
- cancelHideTransform();
- } );
- transformControl.addEventListener( 'mouseUp', function () {
- delayHideTransform();
- } );
- transformControl.addEventListener( 'objectChange', function () {
- updateSplineOutline();
- } );
- var dragcontrols = new DragControls( splineHelperObjects, camera, renderer.domElement ); //
- dragcontrols.enabled = false;
- dragcontrols.addEventListener( 'hoveron', function ( event ) {
- transformControl.attach( event.object );
- cancelHideTransform();
- } );
- dragcontrols.addEventListener( 'hoveroff', function () {
- delayHideTransform();
- } );
- var hiding;
- function delayHideTransform() {
- cancelHideTransform();
- hideTransform();
- }
- function hideTransform() {
- hiding = setTimeout( function () {
- transformControl.detach( transformControl.object );
- }, 2500 );
- }
- function cancelHideTransform() {
- if ( hiding ) clearTimeout( hiding );
- }
- /*******
- * Curves
- *********/
- for ( var i = 0; i < splinePointsLength; i ++ ) {
- addSplineObject( positions[ i ] );
- }
- positions = [];
- for ( var i = 0; i < splinePointsLength; i ++ ) {
- positions.push( splineHelperObjects[ i ].position );
- }
- var geometry = new THREE.BufferGeometry();
- geometry.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( ARC_SEGMENTS * 3 ), 3 ) );
- var curve = new THREE.CatmullRomCurve3( positions );
- curve.curveType = 'catmullrom';
- curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
- color: 0xff0000,
- opacity: 0.35
- } ) );
- curve.mesh.castShadow = true;
- splines.uniform = curve;
- curve = new THREE.CatmullRomCurve3( positions );
- curve.curveType = 'centripetal';
- curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
- color: 0x00ff00,
- opacity: 0.35
- } ) );
- curve.mesh.castShadow = true;
- splines.centripetal = curve;
- curve = new THREE.CatmullRomCurve3( positions );
- curve.curveType = 'chordal';
- curve.mesh = new THREE.Line( geometry.clone(), new THREE.LineBasicMaterial( {
- color: 0x0000ff,
- opacity: 0.35
- } ) );
- curve.mesh.castShadow = true;
- splines.chordal = curve;
- for ( var k in splines ) {
- var spline = splines[ k ];
- scene.add( spline.mesh );
- }
- load( [ new THREE.Vector3( 289.76843686945404, 452.51481137238443, 56.10018915737797 ),
- new THREE.Vector3( - 53.56300074753207, 171.49711742836848, - 14.495472686253045 ),
- new THREE.Vector3( - 91.40118730204415, 176.4306956436485, - 6.958271935582161 ),
- new THREE.Vector3( - 383.785318791128, 491.1365363371675, 47.869296953772746 ) ] );
- }
- function addSplineObject( position ) {
- var material = new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } );
- var object = new THREE.Mesh( geometry, material );
- if ( position ) {
- object.position.copy( position );
- } else {
- object.position.x = Math.random() * 1000 - 500;
- object.position.y = Math.random() * 600;
- object.position.z = Math.random() * 800 - 400;
- }
- object.castShadow = true;
- object.receiveShadow = true;
- scene.add( object );
- splineHelperObjects.push( object );
- return object;
- }
- function addPoint() {
- splinePointsLength ++;
- positions.push( addSplineObject().position );
- updateSplineOutline();
- }
- function removePoint() {
- if ( splinePointsLength <= 4 ) {
- return;
- }
- splinePointsLength --;
- positions.pop();
- scene.remove( splineHelperObjects.pop() );
- updateSplineOutline();
- }
- function updateSplineOutline() {
- for ( var k in splines ) {
- var spline = splines[ k ];
- var splineMesh = spline.mesh;
- var position = splineMesh.geometry.attributes.position;
- for ( var i = 0; i < ARC_SEGMENTS; i ++ ) {
- var t = i / ( ARC_SEGMENTS - 1 );
- spline.getPoint( t, point );
- position.setXYZ( i, point.x, point.y, point.z );
- }
- position.needsUpdate = true;
- }
- }
- function exportSpline() {
- var strplace = [];
- for ( var i = 0; i < splinePointsLength; i ++ ) {
- var p = splineHelperObjects[ i ].position;
- strplace.push( 'new THREE.Vector3({0}, {1}, {2})'.format( p.x, p.y, p.z ) );
- }
- console.log( strplace.join( ',\n' ) );
- var code = '[' + ( strplace.join( ',\n\t' ) ) + ']';
- prompt( 'copy and paste code', code );
- }
- function load( new_positions ) {
- while ( new_positions.length > positions.length ) {
- addPoint();
- }
- while ( new_positions.length < positions.length ) {
- removePoint();
- }
- for ( var i = 0; i < positions.length; i ++ ) {
- positions[ i ].copy( new_positions[ i ] );
- }
- updateSplineOutline();
- }
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- splines.uniform.mesh.visible = params.uniform;
- splines.centripetal.mesh.visible = params.centripetal;
- splines.chordal.mesh.visible = params.chordal;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|