|
@@ -1,334 +1,193 @@
|
|
|
<!DOCTYPE html>
|
|
|
<html lang="en">
|
|
|
<head>
|
|
|
- <title>three.js webgl - geometry - extrude splines</title>
|
|
|
+ <title>three.js webgl - geometry - extrude shapes</title>
|
|
|
<meta charset="utf-8">
|
|
|
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
|
|
|
|
|
|
<style>
|
|
|
- body {
|
|
|
- font-family: Monospace;
|
|
|
- background-color: #f0f0f0;
|
|
|
- margin: 0px;
|
|
|
- overflow: hidden;
|
|
|
- }
|
|
|
+ body {
|
|
|
+ font-family: Monospace;
|
|
|
+ background-color: #000;
|
|
|
+ margin: 0px;
|
|
|
+ overflow: hidden;
|
|
|
+ }
|
|
|
+ a {
|
|
|
+ color: #f80;
|
|
|
+ }
|
|
|
</style>
|
|
|
</head>
|
|
|
|
|
|
<body>
|
|
|
- <canvas id="debug" style="position:absolute; left:100px"></canvas>
|
|
|
|
|
|
<script src="../build/three.min.js"></script>
|
|
|
- <script src="js/libs/stats.min.js"></script>
|
|
|
-
|
|
|
+ <script src="js/controls/TrackballControls.js"></script>
|
|
|
|
|
|
<script>
|
|
|
|
|
|
- var container, stats;
|
|
|
-
|
|
|
- var camera, scene, renderer;
|
|
|
+ var container;
|
|
|
|
|
|
- var group, text, plane;
|
|
|
-
|
|
|
- var targetRotation = 0;
|
|
|
- var targetRotationOnMouseDown = 0;
|
|
|
-
|
|
|
- var mouseX = 0;
|
|
|
- var mouseXOnMouseDown = 0;
|
|
|
-
|
|
|
- var windowHalfX = window.innerWidth / 2;
|
|
|
- var windowHalfY = window.innerHeight / 2;
|
|
|
+ var camera, scene, renderer, controls;
|
|
|
|
|
|
init();
|
|
|
animate();
|
|
|
|
|
|
function init() {
|
|
|
|
|
|
- container = document.createElement( 'div' );
|
|
|
- document.body.appendChild( container );
|
|
|
-
|
|
|
var info = document.createElement( 'div' );
|
|
|
info.style.position = 'absolute';
|
|
|
info.style.top = '10px';
|
|
|
info.style.width = '100%';
|
|
|
info.style.textAlign = 'center';
|
|
|
- info.innerHTML = 'Shapes Extrusion via Spline path<br/>Drag to spin';
|
|
|
- container.appendChild( info );
|
|
|
+ info.style.color = '#fff';
|
|
|
+ info.style.link = '#f80';
|
|
|
+ info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - geometry extrude shapes';
|
|
|
+ document.body.appendChild( info );
|
|
|
|
|
|
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
|
|
|
- camera.position.set( 0, 150, 500 );
|
|
|
+ renderer = new THREE.WebGLRenderer();
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
+ document.body.appendChild( renderer.domElement );
|
|
|
|
|
|
scene = new THREE.Scene();
|
|
|
|
|
|
- var light = new THREE.DirectionalLight( 0xffffff );
|
|
|
- light.position.set( 0, 0, 1 );
|
|
|
- scene.add( light );
|
|
|
-
|
|
|
- group = new THREE.Object3D();
|
|
|
- group.position.y = 50;
|
|
|
- scene.add( group );
|
|
|
-
|
|
|
- function addGeometry( geometry, color, x, y, z, rx, ry, rz, s ) {
|
|
|
-
|
|
|
- // 3d shape
|
|
|
-
|
|
|
- var mesh = THREE.SceneUtils.createMultiMaterialObject( geometry, [ new THREE.MeshLambertMaterial( { color: color } ), new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, opacity: 0.3, transparent: true } ) ] );
|
|
|
-
|
|
|
- mesh.position.set( x, y, z - 75 );
|
|
|
- // mesh.rotation.set( rx, ry, rz );
|
|
|
- mesh.scale.set( s, s, s );
|
|
|
-
|
|
|
- if ( geometry.debug ) mesh.add( geometry.debug );
|
|
|
-
|
|
|
- group.add( mesh );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- var extrudeSettings = { amount: 200, bevelEnabled: true, bevelSegments: 2, steps: 150 }; // bevelSegments: 2, steps: 2 , bevelSegments: 5, bevelSize: 8, bevelThickness:5,
|
|
|
-
|
|
|
- // var extrudePath = new THREE.Path();
|
|
|
-
|
|
|
- // extrudePath.moveTo( 0, 0 );
|
|
|
- // extrudePath.lineTo( 10, 10 );
|
|
|
- // extrudePath.quadraticCurveTo( 80, 60, 160, 10 );
|
|
|
- // extrudePath.quadraticCurveTo( 240, -40, 320, 10 );
|
|
|
-
|
|
|
-
|
|
|
- extrudeSettings.bevelEnabled = false;
|
|
|
-
|
|
|
- var extrudeBend = new THREE.SplineCurve3( //Closed
|
|
|
- [
|
|
|
-
|
|
|
- new THREE.Vector3( 30, 12, 83),
|
|
|
- new THREE.Vector3( 40, 20, 67),
|
|
|
- new THREE.Vector3( 60, 40, 99),
|
|
|
- new THREE.Vector3( 10, 60, 49),
|
|
|
- new THREE.Vector3( 25, 80, 40)
|
|
|
-
|
|
|
- ]);
|
|
|
-
|
|
|
- var pipeSpline = new THREE.SplineCurve3([
|
|
|
- new THREE.Vector3(0, 10, -10), new THREE.Vector3(10, 0, -10), new THREE.Vector3(20, 0, 0), new THREE.Vector3(30, 0, 10), new THREE.Vector3(30, 0, 20), new THREE.Vector3(20, 0, 30), new THREE.Vector3(10, 0, 30), new THREE.Vector3(0, 0, 30), new THREE.Vector3(-10, 10, 30), new THREE.Vector3(-10, 20, 30), new THREE.Vector3(0, 30, 30), new THREE.Vector3(10, 30, 30), new THREE.Vector3(20, 30, 15), new THREE.Vector3(10, 30, 10), new THREE.Vector3(0, 30, 10), new THREE.Vector3(-10, 20, 10), new THREE.Vector3(-10, 10, 10), new THREE.Vector3(0, 0, 10), new THREE.Vector3(10, -10, 10), new THREE.Vector3(20, -15, 10), new THREE.Vector3(30, -15, 10), new THREE.Vector3(40, -15, 10), new THREE.Vector3(50, -15, 10), new THREE.Vector3(60, 0, 10), new THREE.Vector3(70, 0, 0), new THREE.Vector3(80, 0, 0), new THREE.Vector3(90, 0, 0), new THREE.Vector3(100, 0, 0)]
|
|
|
- );
|
|
|
-
|
|
|
- var sampleClosedSpline = new THREE.ClosedSplineCurve3([
|
|
|
- new THREE.Vector3(0, -40, -40),
|
|
|
- new THREE.Vector3(0, 40, -40),
|
|
|
- new THREE.Vector3(0, 140, -40),
|
|
|
- new THREE.Vector3(0, 40, 40),
|
|
|
- new THREE.Vector3(0, -40, 40),
|
|
|
- ]);
|
|
|
-
|
|
|
- var randomPoints = [];
|
|
|
+ camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
|
|
|
+ camera.position.set( 0, 0, 500 );
|
|
|
|
|
|
- for ( var i = 0; i < 10; i ++ ) {
|
|
|
+ controls = new THREE.TrackballControls( camera, renderer.domElement );
|
|
|
+ controls.minDistance = 200;
|
|
|
+ controls.maxDistance = 500;
|
|
|
|
|
|
- randomPoints.push( new THREE.Vector3(Math.random() * 200,Math.random() * 200,Math.random() * 200 ) );
|
|
|
+ scene.add( new THREE.AmbientLight( 0x222222 ) );
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- var randomSpline = new THREE.SplineCurve3( randomPoints );
|
|
|
-
|
|
|
- extrudeSettings.extrudePath = randomSpline; // extrudeBend sampleClosedSpline pipeSpline randomSpline
|
|
|
-
|
|
|
- // Circle
|
|
|
+ var light = new THREE.PointLight( 0xffffff );
|
|
|
+ light.position = camera.position;
|
|
|
+ scene.add( light );
|
|
|
|
|
|
- var circleRadius = 4;
|
|
|
- var circleShape = new THREE.Shape();
|
|
|
- circleShape.moveTo( 0, circleRadius );
|
|
|
- circleShape.quadraticCurveTo( circleRadius, circleRadius, circleRadius, 0 );
|
|
|
- circleShape.quadraticCurveTo( circleRadius, -circleRadius, 0, -circleRadius );
|
|
|
- circleShape.quadraticCurveTo( -circleRadius, -circleRadius, -circleRadius, 0 );
|
|
|
- circleShape.quadraticCurveTo( -circleRadius, circleRadius, 0, circleRadius);
|
|
|
|
|
|
- var rectLength = 12, rectWidth = 4;
|
|
|
+ //
|
|
|
|
|
|
- var rectShape = new THREE.Shape();
|
|
|
|
|
|
- rectShape.moveTo( -rectLength/2, -rectWidth/2 );
|
|
|
- rectShape.lineTo( -rectLength/2, rectWidth/2 );
|
|
|
- rectShape.lineTo( rectLength/2, rectWidth/2 );
|
|
|
- rectShape.lineTo( rectLength/2, -rectLength/2 );
|
|
|
- rectShape.lineTo( -rectLength/2, -rectLength/2 );
|
|
|
+ var closedSpline = new THREE.ClosedSplineCurve3( [
|
|
|
+ new THREE.Vector3( -60, -100, 60 ),
|
|
|
+ new THREE.Vector3( -60, 20, 60 ),
|
|
|
+ new THREE.Vector3( -60, 120, 60 ),
|
|
|
+ new THREE.Vector3( 60, 20, -60 ),
|
|
|
+ new THREE.Vector3( 60, -100, -60 )
|
|
|
+ ] );
|
|
|
|
|
|
+ var extrudeSettings = {
|
|
|
+ steps : 100,
|
|
|
+ bevelEnabled : false,
|
|
|
+ extrudePath : closedSpline
|
|
|
+ };
|
|
|
|
|
|
- var pts = [], starPoints = 5, l;
|
|
|
|
|
|
- for ( i = 0; i < starPoints * 2; i ++ ) {
|
|
|
+ var pts = [], count = 3;
|
|
|
|
|
|
- if ( i % 2 == 1 ) {
|
|
|
+ for ( var i = 0; i < count; i ++ ) {
|
|
|
|
|
|
- l = 5;
|
|
|
+ var l = 20;
|
|
|
|
|
|
- } else {
|
|
|
+ var a = 2 * i / count * Math.PI;
|
|
|
|
|
|
- l = 10;
|
|
|
+ pts.push( new THREE.Vector2 ( Math.cos( a ) * l, Math.sin( a ) * l ) );
|
|
|
|
|
|
}
|
|
|
|
|
|
- var a = i / starPoints * Math.PI;
|
|
|
- pts.push( new THREE.Vector2 ( Math.cos( a ) * l, Math.sin( a ) * l ) );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- var starShape = new THREE.Shape( pts );
|
|
|
-
|
|
|
- // Smiley
|
|
|
-
|
|
|
- var smileyShape = new THREE.Shape();
|
|
|
- smileyShape.moveTo( 80, 40 );
|
|
|
- smileyShape.arc( 40, 40, 40, 0, Math.PI*2, false );
|
|
|
-
|
|
|
- var smileyEye1Path = new THREE.Path();
|
|
|
- smileyEye1Path.moveTo( 35, 20 );
|
|
|
- smileyEye1Path.arc( 25, 20, 10, 0, Math.PI*2, true );
|
|
|
- smileyShape.holes.push( smileyEye1Path );
|
|
|
-
|
|
|
- var smileyEye2Path = new THREE.Path();
|
|
|
- smileyEye2Path.moveTo( 65, 20 );
|
|
|
- smileyEye2Path.arc( 55, 20, 10, 0, Math.PI*2, true );
|
|
|
- smileyShape.holes.push( smileyEye2Path );
|
|
|
-
|
|
|
- var smileyMouthPath = new THREE.Path();
|
|
|
-
|
|
|
- smileyMouthPath.moveTo( 20, 40 );
|
|
|
- smileyMouthPath.quadraticCurveTo( 40, 60, 60, 40 );
|
|
|
- smileyMouthPath.bezierCurveTo( 70, 45, 70, 50, 60, 60 );
|
|
|
- smileyMouthPath.quadraticCurveTo( 40, 80, 20, 60 );
|
|
|
- smileyMouthPath.quadraticCurveTo( 5, 50, 20, 40 );
|
|
|
+ var shape = new THREE.Shape( pts );
|
|
|
|
|
|
- smileyShape.holes.push( smileyMouthPath );
|
|
|
+ var geometry = new THREE.ExtrudeGeometry( shape, extrudeSettings );
|
|
|
|
|
|
- var circle3d = starShape.extrude( extrudeSettings ); //circleShape rectShape smileyShape starShape
|
|
|
- // var circle3d = new THREE.ExtrudeGeometry(circleShape, extrudeBend, extrudeSettings );
|
|
|
+ var material = new THREE.MeshLambertMaterial( { color: 0xb00000, wireframe: false } );
|
|
|
|
|
|
- var tube = new THREE.TubeGeometry(extrudeSettings.extrudePath, 150, 4, 5, false, true);
|
|
|
- // new THREE.TubeGeometry(extrudePath, segments, 2, radiusSegments, closed2, debug);
|
|
|
+ var mesh = new THREE.Mesh( geometry, material );
|
|
|
|
|
|
+ scene.add( mesh );
|
|
|
|
|
|
- addGeometry( circle3d, 0xff1111, -100, 0, 0, 0, 0, 0, 1 );
|
|
|
- addGeometry( tube, 0x00ff11, 0, 0, 0, 0, 0, 0, 1 );
|
|
|
- console.log(tube);
|
|
|
|
|
|
- //
|
|
|
+ //
|
|
|
|
|
|
- renderer = new THREE.WebGLRenderer( { antialias: true } );
|
|
|
- renderer.setClearColor( 0xf0f0f0 );
|
|
|
- renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
|
|
|
- container.appendChild( renderer.domElement );
|
|
|
+ var randomPoints = [];
|
|
|
|
|
|
- stats = new Stats();
|
|
|
- stats.domElement.style.position = 'absolute';
|
|
|
- stats.domElement.style.top = '0px';
|
|
|
- container.appendChild( stats.domElement );
|
|
|
+ for ( var i = 0; i < 10; i ++ ) {
|
|
|
|
|
|
- document.addEventListener( 'mousedown', onDocumentMouseDown, false );
|
|
|
- document.addEventListener( 'touchstart', onDocumentTouchStart, false );
|
|
|
- document.addEventListener( 'touchmove', onDocumentTouchMove, false );
|
|
|
+ randomPoints.push( new THREE.Vector3( ( i - 4.5 ) * 50, THREE.Math.randFloat( - 50, 50 ), THREE.Math.randFloat( - 50, 50 ) ) );
|
|
|
|
|
|
- //
|
|
|
-
|
|
|
- window.addEventListener( 'resize', onWindowResize, false );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- function onWindowResize() {
|
|
|
+ }
|
|
|
|
|
|
- windowHalfX = window.innerWidth / 2;
|
|
|
- windowHalfY = window.innerHeight / 2;
|
|
|
+ var randomSpline = new THREE.SplineCurve3( randomPoints );
|
|
|
|
|
|
- camera.aspect = window.innerWidth / window.innerHeight;
|
|
|
- camera.updateProjectionMatrix();
|
|
|
+ //
|
|
|
|
|
|
- renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
+ var extrudeSettings = {
|
|
|
+ steps : 200,
|
|
|
+ bevelEnabled : false,
|
|
|
+ extrudePath : randomSpline
|
|
|
+ };
|
|
|
|
|
|
- }
|
|
|
|
|
|
- //
|
|
|
+ var pts = [], numPts = 5;
|
|
|
|
|
|
- function onDocumentMouseDown( event ) {
|
|
|
+ for ( var i = 0; i < numPts * 2; i ++ ) {
|
|
|
|
|
|
- event.preventDefault();
|
|
|
+ var l = i % 2 == 1 ? 10 : 20;
|
|
|
|
|
|
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
|
|
|
- document.addEventListener( 'mouseup', onDocumentMouseUp, false );
|
|
|
- document.addEventListener( 'mouseout', onDocumentMouseOut, false );
|
|
|
+ var a = i / numPts * Math.PI;
|
|
|
|
|
|
- mouseXOnMouseDown = event.clientX - windowHalfX;
|
|
|
- targetRotationOnMouseDown = targetRotation;
|
|
|
+ pts.push( new THREE.Vector2 ( Math.cos( a ) * l, Math.sin( a ) * l ) );
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- function onDocumentMouseMove( event ) {
|
|
|
+ var shape = new THREE.Shape( pts );
|
|
|
|
|
|
- mouseX = event.clientX - windowHalfX;
|
|
|
+ var geometry = new THREE.ExtrudeGeometry( shape, extrudeSettings );
|
|
|
|
|
|
- targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
|
|
|
+ var material2 = new THREE.MeshLambertMaterial( { color: 0xff8000, wireframe: false } );
|
|
|
|
|
|
- }
|
|
|
+ var mesh = new THREE.Mesh( geometry, material2 );
|
|
|
|
|
|
- function onDocumentMouseUp( event ) {
|
|
|
+ scene.add( mesh );
|
|
|
|
|
|
- document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
|
|
|
- document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
|
|
|
- document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
|
|
|
|
|
|
- }
|
|
|
+ //
|
|
|
|
|
|
- function onDocumentMouseOut( event ) {
|
|
|
|
|
|
- document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
|
|
|
- document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
|
|
|
- document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
|
|
|
+ var materials = [ material, material2 ];
|
|
|
|
|
|
- }
|
|
|
+ var extrudeSettings = {
|
|
|
+ amount : 20,
|
|
|
+ steps : 1,
|
|
|
+ material : 1,
|
|
|
+ extrudeMaterial : 0,
|
|
|
+ bevelEnabled : true,
|
|
|
+ bevelThickness : 2,
|
|
|
+ bevelSize : 4,
|
|
|
+ bevelSegments : 1,
|
|
|
+ };
|
|
|
|
|
|
- function onDocumentTouchStart( event ) {
|
|
|
+ var geometry = new THREE.ExtrudeGeometry( shape, extrudeSettings );
|
|
|
|
|
|
- if ( event.touches.length == 1 ) {
|
|
|
+ var mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( materials ) );
|
|
|
|
|
|
- event.preventDefault();
|
|
|
+ mesh.position.set( 50, 100, 50 );
|
|
|
|
|
|
- mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
|
|
|
- targetRotationOnMouseDown = targetRotation;
|
|
|
+ scene.add( mesh );
|
|
|
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- function onDocumentTouchMove( event ) {
|
|
|
+ function animate() {
|
|
|
|
|
|
- if ( event.touches.length == 1 ) {
|
|
|
+ requestAnimationFrame( animate );
|
|
|
|
|
|
- event.preventDefault();
|
|
|
+ controls.update();
|
|
|
|
|
|
- mouseX = event.touches[ 0 ].pageX - windowHalfX;
|
|
|
- targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
|
|
|
+ renderer.render( scene, camera );
|
|
|
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- //
|
|
|
-
|
|
|
- function animate() {
|
|
|
-
|
|
|
- requestAnimationFrame( animate );
|
|
|
-
|
|
|
- render();
|
|
|
- stats.update();
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- function render() {
|
|
|
-
|
|
|
- group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
|
|
|
- renderer.render( scene, camera );
|
|
|
-
|
|
|
- }
|
|
|
+ </script>
|
|
|
|
|
|
- </script>
|
|
|
+ </body>
|
|
|
|
|
|
-</body>
|
|
|
</html>
|