2
0
Эх сурвалжийг харах

Lathe geometry in the editor

Added a minimal support of lathe geometry in the editor.
rfm1201 9 жил өмнө
parent
commit
a6b0dc852f

+ 1 - 0
editor/index.html

@@ -119,6 +119,7 @@
 		<script src="js/Sidebar.Geometry.TorusKnotGeometry.js"></script>
 		<script src="../examples/js/geometries/TeapotBufferGeometry.js"></script>
 		<script src="js/Sidebar.Geometry.TeapotBufferGeometry.js"></script>
+		<script src="js/Sidebar.Geometry.LatheGeometry.js"></script>
 		<script src="js/Sidebar.Material.js"></script>
 		<script src="js/Sidebar.Animation.js"></script>
 		<script src="js/Sidebar.Script.js"></script>

+ 33 - 0
editor/js/Menubar.Add.js

@@ -242,6 +242,39 @@ Menubar.Add = function ( editor ) {
 	options.add( option );
 	*/
 
+	// Lathe
+
+	var option = new UI.Row();
+	option.setClass( 'option' );
+	option.setTextContent( 'Lathe geometry' );
+	option.onClick( function() {
+
+		var points = [];
+		var segments = 20;
+		var phiStart = 0;
+		var phiLength = 2 * Math.PI;
+
+		points.push( new THREE.Vector3( 0, 0, 0 ) );
+		points.push( new THREE.Vector3( 4, 0, 0 ) );
+		points.push( new THREE.Vector3( 3.5, 0, 0.5 ) );
+		points.push( new THREE.Vector3( 1, 0, 0.75 ) );
+		points.push( new THREE.Vector3( 0.8, 0, 1 ) );
+		points.push( new THREE.Vector3( 0.8, 0, 4 ) );
+		points.push( new THREE.Vector3( 1, 0, 4.2 ) );
+		points.push( new THREE.Vector3( 1.4, 0, 4.8 ) );
+		points.push( new THREE.Vector3( 2, 0, 5 ) );
+		points.push( new THREE.Vector3( 2.5, 0, 5.4 ) );
+		points.push( new THREE.Vector3( 2.5, 0, 12 ) );
+
+		var geometry = new THREE.LatheGeometry( points, segments, phiStart, phiLength );
+		var mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial( { side: THREE.DoubleSide } ) );
+		mesh.name = 'Lathe ' + ( ++meshCount );
+
+		editor.execute( new AddObjectCommand( mesh ) );
+
+	} );
+	options.add( option );
+
 	// Sprite
 
 	var option = new UI.Row();

+ 134 - 0
editor/js/Sidebar.Geometry.LatheGeometry.js

@@ -0,0 +1,134 @@
+/**
+ * @author rfm1201
+ */
+
+Sidebar.Geometry.LatheGeometry = function( editor, object ) {
+
+	var signals = editor.signals;
+
+	var container = new UI.Row();
+
+	var parameters = object.geometry.parameters;
+
+	// segments
+
+	var segmentsRow = new UI.Row();
+	var segments = new UI.Integer( parameters.segments ).onChange( update );
+
+	segmentsRow.add( new UI.Text( 'Segments' ).setWidth( '90px' ) );
+	segmentsRow.add( segments );
+
+	container.add( segmentsRow );
+
+	// phiStart
+
+	var phiStartRow = new UI.Row();
+	var phiStart = new UI.Number( parameters.phiStart * 180 / Math.PI ).onChange( update );
+
+	phiStartRow.add( new UI.Text( 'Phi start (°)' ).setWidth( '90px' ) );
+	phiStartRow.add( phiStart );
+
+	container.add( phiStartRow );
+
+	// phiLength
+
+	var phiLengthRow = new UI.Row();
+	var phiLength = new UI.Number( parameters.phiLength * 180 / Math.PI ).onChange( update );
+
+	phiLengthRow.add( new UI.Text( 'Phi length (°)' ).setWidth( '90px' ) );
+	phiLengthRow.add( phiLength );
+
+	container.add( phiLengthRow );
+
+	// points
+
+	var lastPointIdx = 0;
+	var pointsUI = [];
+
+	var pointsDiv = new UI.Div();
+	var point;
+	for ( var i = 0; i < parameters.points.length; i ++ ) {
+
+		point = parameters.points[ i ];
+		pointsDiv.add( createPointRow( point.x, point.z ) );
+
+	}
+
+	var pointsRow = new UI.Row().setDisplay( 'flex' );
+
+	var btnAdd = new UI.Button( '+' ).setMarginRight( '15px' ).onClick( function() {
+
+		pointsDiv.add( createPointRow( 0, 0 ) );
+		update();
+
+	} );
+
+	pointsRow.add( new UI.Text( 'Points' ).setWidth( '50px' ), btnAdd, pointsDiv );
+	container.add( pointsRow );
+
+	//
+
+	function createPointRow( x, y ) {
+
+		var pointRow = new UI.Row();
+		var lbl = new UI.Text( lastPointIdx + 1 ).setWidth( '20px' );
+		var txtX = new UI.Number( x ).setRange( 0, Infinity ).setWidth( '40px' ).onChange( update );
+		var txtY = new UI.Number( y ).setWidth( '40px' ).onChange( update );
+		var idx = lastPointIdx;
+		var btn = new UI.Button( '-' ).onClick( function() {
+
+			deletePointRow( idx );
+
+		} );
+
+		pointsUI.push( { row: pointRow, lbl: lbl, x: txtX, y: txtY } );
+		lastPointIdx ++;
+		pointRow.add( lbl, txtX, txtY, btn );
+
+		return pointRow;
+
+	}
+
+	function deletePointRow( idx ) {
+
+		if ( ! pointsUI[ idx ] ) return;
+
+		pointsDiv.remove( pointsUI[ idx ].row );
+		pointsUI[ idx ] = null;
+
+		update();
+
+	}
+
+	function update() {
+
+		var points = [];
+		var count = 0;
+		var pointUI;
+		for ( var i = 0; i < pointsUI.length; i ++ ) {
+
+			pointUI = pointsUI[ i ];
+			if ( ! pointUI ) {
+
+				continue;
+
+			}
+
+			points.push( new THREE.Vector3( pointUI.x.getValue(), 0, pointUI.y.getValue() ) );
+			count ++;
+			pointUI.lbl.setValue( count );
+
+		}
+
+		editor.execute( new SetGeometryCommand( object, new THREE.LatheGeometry(
+					points,
+					segments.getValue(),
+					phiStart.getValue() / 180 * Math.PI,
+					phiLength.getValue() / 180 * Math.PI
+				) ) );
+
+	}
+
+	return container;
+
+}