123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - geometry - NURBS</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="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - NURBS curve and surface example
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { NURBSCurve } from './jsm/curves/NURBSCurve.js';
- import { NURBSSurface } from './jsm/curves/NURBSSurface.js';
- var container, stats;
- var camera, scene, renderer;
- var group;
- var targetRotation = 0;
- var targetRotationOnMouseDown = 0;
- var mouseX = 0;
- var mouseXOnMouseDown = 0;
- var windowHalfX = window.innerWidth / 2;
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
- camera.position.set( 0, 150, 750 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xf0f0f0 );
- scene.add( new THREE.AmbientLight( 0x808080 ) );
- var light = new THREE.DirectionalLight( 0xffffff, 1 );
- light.position.set( 1, 1, 1 );
- scene.add( light );
- group = new THREE.Group();
- group.position.y = 50;
- scene.add( group );
- // NURBS curve
- var nurbsControlPoints = [];
- var nurbsKnots = [];
- var nurbsDegree = 3;
- for ( var i = 0; i <= nurbsDegree; i ++ ) {
- nurbsKnots.push( 0 );
- }
- for ( var i = 0, j = 20; i < j; i ++ ) {
- nurbsControlPoints.push(
- new THREE.Vector4(
- Math.random() * 400 - 200,
- Math.random() * 400,
- Math.random() * 400 - 200,
- 1 // weight of control point: higher means stronger attraction
- )
- );
- var knot = ( i + 1 ) / ( j - nurbsDegree );
- nurbsKnots.push( THREE.MathUtils.clamp( knot, 0, 1 ) );
- }
- var nurbsCurve = new NURBSCurve( nurbsDegree, nurbsKnots, nurbsControlPoints );
- var nurbsGeometry = new THREE.BufferGeometry();
- nurbsGeometry.setFromPoints( nurbsCurve.getPoints( 200 ) );
- var nurbsMaterial = new THREE.LineBasicMaterial( { color: 0x333333 } );
- var nurbsLine = new THREE.Line( nurbsGeometry, nurbsMaterial );
- nurbsLine.position.set( 200, - 100, 0 );
- group.add( nurbsLine );
- var nurbsControlPointsGeometry = new THREE.BufferGeometry();
- nurbsControlPointsGeometry.setFromPoints( nurbsCurve.controlPoints );
- var nurbsControlPointsMaterial = new THREE.LineBasicMaterial( { color: 0x333333, opacity: 0.25, transparent: true } );
- var nurbsControlPointsLine = new THREE.Line( nurbsControlPointsGeometry, nurbsControlPointsMaterial );
- nurbsControlPointsLine.position.copy( nurbsLine.position );
- group.add( nurbsControlPointsLine );
- // NURBS surface
- var nsControlPoints = [
- [
- new THREE.Vector4( - 200, - 200, 100, 1 ),
- new THREE.Vector4( - 200, - 100, - 200, 1 ),
- new THREE.Vector4( - 200, 100, 250, 1 ),
- new THREE.Vector4( - 200, 200, - 100, 1 )
- ],
- [
- new THREE.Vector4( 0, - 200, 0, 1 ),
- new THREE.Vector4( 0, - 100, - 100, 5 ),
- new THREE.Vector4( 0, 100, 150, 5 ),
- new THREE.Vector4( 0, 200, 0, 1 )
- ],
- [
- new THREE.Vector4( 200, - 200, - 100, 1 ),
- new THREE.Vector4( 200, - 100, 200, 1 ),
- new THREE.Vector4( 200, 100, - 250, 1 ),
- new THREE.Vector4( 200, 200, 100, 1 )
- ]
- ];
- var degree1 = 2;
- var degree2 = 3;
- var knots1 = [ 0, 0, 0, 1, 1, 1 ];
- var knots2 = [ 0, 0, 0, 0, 1, 1, 1, 1 ];
- var nurbsSurface = new NURBSSurface( degree1, degree2, knots1, knots2, nsControlPoints );
- var map = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
- map.wrapS = map.wrapT = THREE.RepeatWrapping;
- map.anisotropy = 16;
- function getSurfacePoint( u, v, target ) {
- return nurbsSurface.getPoint( u, v, target );
- }
- var geometry = new THREE.ParametricBufferGeometry( getSurfacePoint, 20, 20 );
- var material = new THREE.MeshLambertMaterial( { map: map, side: THREE.DoubleSide } );
- var object = new THREE.Mesh( geometry, material );
- object.position.set( - 200, 100, 0 );
- object.scale.multiplyScalar( 1 );
- group.add( object );
- //
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- stats = new Stats();
- container.appendChild( stats.dom );
- document.addEventListener( 'mousedown', onDocumentMouseDown, false );
- document.addEventListener( 'touchstart', onDocumentTouchStart, false );
- document.addEventListener( 'touchmove', onDocumentTouchMove, false );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- windowHalfX = window.innerWidth / 2;
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function onDocumentMouseDown( event ) {
- event.preventDefault();
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- document.addEventListener( 'mouseup', onDocumentMouseUp, false );
- document.addEventListener( 'mouseout', onDocumentMouseOut, false );
- mouseXOnMouseDown = event.clientX - windowHalfX;
- targetRotationOnMouseDown = targetRotation;
- }
- function onDocumentMouseMove( event ) {
- mouseX = event.clientX - windowHalfX;
- targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
- }
- function onDocumentMouseUp() {
- document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
- document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
- document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
- }
- function onDocumentMouseOut() {
- document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
- document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
- document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
- }
- function onDocumentTouchStart( event ) {
- if ( event.touches.length == 1 ) {
- event.preventDefault();
- mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
- targetRotationOnMouseDown = targetRotation;
- }
- }
- function onDocumentTouchMove( event ) {
- if ( event.touches.length == 1 ) {
- event.preventDefault();
- mouseX = event.touches[ 0 ].pageX - windowHalfX;
- targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
- }
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|