webgl_geometry_nurbs.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - NURBS</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="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - NURBS curve and surface example
  21. </div>
  22. <script type="module">
  23. import * as THREE from '../build/three.module.js';
  24. import Stats from './jsm/libs/stats.module.js';
  25. import { NURBSCurve } from './jsm/curves/NURBSCurve.js';
  26. import { NURBSSurface } from './jsm/curves/NURBSSurface.js';
  27. var container, stats;
  28. var camera, scene, renderer;
  29. var group;
  30. var targetRotation = 0;
  31. var targetRotationOnMouseDown = 0;
  32. var mouseX = 0;
  33. var mouseXOnMouseDown = 0;
  34. var windowHalfX = window.innerWidth / 2;
  35. init();
  36. animate();
  37. function init() {
  38. container = document.createElement( 'div' );
  39. document.body.appendChild( container );
  40. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
  41. camera.position.set( 0, 150, 750 );
  42. scene = new THREE.Scene();
  43. scene.background = new THREE.Color( 0xf0f0f0 );
  44. scene.add( new THREE.AmbientLight( 0x808080 ) );
  45. var light = new THREE.DirectionalLight( 0xffffff, 1 );
  46. light.position.set( 1, 1, 1 );
  47. scene.add( light );
  48. group = new THREE.Group();
  49. group.position.y = 50;
  50. scene.add( group );
  51. // NURBS curve
  52. var nurbsControlPoints = [];
  53. var nurbsKnots = [];
  54. var nurbsDegree = 3;
  55. for ( var i = 0; i <= nurbsDegree; i ++ ) {
  56. nurbsKnots.push( 0 );
  57. }
  58. for ( var i = 0, j = 20; i < j; i ++ ) {
  59. nurbsControlPoints.push(
  60. new THREE.Vector4(
  61. Math.random() * 400 - 200,
  62. Math.random() * 400,
  63. Math.random() * 400 - 200,
  64. 1 // weight of control point: higher means stronger attraction
  65. )
  66. );
  67. var knot = ( i + 1 ) / ( j - nurbsDegree );
  68. nurbsKnots.push( THREE.MathUtils.clamp( knot, 0, 1 ) );
  69. }
  70. var nurbsCurve = new NURBSCurve( nurbsDegree, nurbsKnots, nurbsControlPoints );
  71. var nurbsGeometry = new THREE.BufferGeometry();
  72. nurbsGeometry.setFromPoints( nurbsCurve.getPoints( 200 ) );
  73. var nurbsMaterial = new THREE.LineBasicMaterial( { color: 0x333333 } );
  74. var nurbsLine = new THREE.Line( nurbsGeometry, nurbsMaterial );
  75. nurbsLine.position.set( 200, - 100, 0 );
  76. group.add( nurbsLine );
  77. var nurbsControlPointsGeometry = new THREE.BufferGeometry();
  78. nurbsControlPointsGeometry.setFromPoints( nurbsCurve.controlPoints );
  79. var nurbsControlPointsMaterial = new THREE.LineBasicMaterial( { color: 0x333333, opacity: 0.25, transparent: true } );
  80. var nurbsControlPointsLine = new THREE.Line( nurbsControlPointsGeometry, nurbsControlPointsMaterial );
  81. nurbsControlPointsLine.position.copy( nurbsLine.position );
  82. group.add( nurbsControlPointsLine );
  83. // NURBS surface
  84. var nsControlPoints = [
  85. [
  86. new THREE.Vector4( - 200, - 200, 100, 1 ),
  87. new THREE.Vector4( - 200, - 100, - 200, 1 ),
  88. new THREE.Vector4( - 200, 100, 250, 1 ),
  89. new THREE.Vector4( - 200, 200, - 100, 1 )
  90. ],
  91. [
  92. new THREE.Vector4( 0, - 200, 0, 1 ),
  93. new THREE.Vector4( 0, - 100, - 100, 5 ),
  94. new THREE.Vector4( 0, 100, 150, 5 ),
  95. new THREE.Vector4( 0, 200, 0, 1 )
  96. ],
  97. [
  98. new THREE.Vector4( 200, - 200, - 100, 1 ),
  99. new THREE.Vector4( 200, - 100, 200, 1 ),
  100. new THREE.Vector4( 200, 100, - 250, 1 ),
  101. new THREE.Vector4( 200, 200, 100, 1 )
  102. ]
  103. ];
  104. var degree1 = 2;
  105. var degree2 = 3;
  106. var knots1 = [ 0, 0, 0, 1, 1, 1 ];
  107. var knots2 = [ 0, 0, 0, 0, 1, 1, 1, 1 ];
  108. var nurbsSurface = new NURBSSurface( degree1, degree2, knots1, knots2, nsControlPoints );
  109. var map = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
  110. map.wrapS = map.wrapT = THREE.RepeatWrapping;
  111. map.anisotropy = 16;
  112. function getSurfacePoint( u, v, target ) {
  113. return nurbsSurface.getPoint( u, v, target );
  114. }
  115. var geometry = new THREE.ParametricBufferGeometry( getSurfacePoint, 20, 20 );
  116. var material = new THREE.MeshLambertMaterial( { map: map, side: THREE.DoubleSide } );
  117. var object = new THREE.Mesh( geometry, material );
  118. object.position.set( - 200, 100, 0 );
  119. object.scale.multiplyScalar( 1 );
  120. group.add( object );
  121. //
  122. renderer = new THREE.WebGLRenderer( { antialias: true } );
  123. renderer.setPixelRatio( window.devicePixelRatio );
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. container.appendChild( renderer.domElement );
  126. stats = new Stats();
  127. container.appendChild( stats.dom );
  128. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  129. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  130. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  131. //
  132. window.addEventListener( 'resize', onWindowResize, false );
  133. }
  134. function onWindowResize() {
  135. windowHalfX = window.innerWidth / 2;
  136. camera.aspect = window.innerWidth / window.innerHeight;
  137. camera.updateProjectionMatrix();
  138. renderer.setSize( window.innerWidth, window.innerHeight );
  139. }
  140. //
  141. function onDocumentMouseDown( event ) {
  142. event.preventDefault();
  143. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  144. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  145. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  146. mouseXOnMouseDown = event.clientX - windowHalfX;
  147. targetRotationOnMouseDown = targetRotation;
  148. }
  149. function onDocumentMouseMove( event ) {
  150. mouseX = event.clientX - windowHalfX;
  151. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  152. }
  153. function onDocumentMouseUp() {
  154. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  155. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  156. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  157. }
  158. function onDocumentMouseOut() {
  159. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  160. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  161. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  162. }
  163. function onDocumentTouchStart( event ) {
  164. if ( event.touches.length == 1 ) {
  165. event.preventDefault();
  166. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  167. targetRotationOnMouseDown = targetRotation;
  168. }
  169. }
  170. function onDocumentTouchMove( event ) {
  171. if ( event.touches.length == 1 ) {
  172. event.preventDefault();
  173. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  174. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  175. }
  176. }
  177. //
  178. function animate() {
  179. requestAnimationFrame( animate );
  180. render();
  181. stats.update();
  182. }
  183. function render() {
  184. group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
  185. renderer.render( scene, camera );
  186. }
  187. </script>
  188. </body>
  189. </html>