webgl_geometries_parametric.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - parametric geometries</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. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - parametric geometries</div>
  12. <script type="module">
  13. import * as THREE from '../build/three.module.js';
  14. import Stats from './jsm/libs/stats.module.js';
  15. import { Curves } from './jsm/curves/CurveExtras.js';
  16. import { ParametricGeometries } from './jsm/geometries/ParametricGeometries.js';
  17. var camera, scene, renderer, stats;
  18. init();
  19. animate();
  20. function init() {
  21. var container = document.getElementById( 'container' );
  22. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  23. camera.position.y = 400;
  24. scene = new THREE.Scene();
  25. //
  26. var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
  27. scene.add( ambientLight );
  28. var pointLight = new THREE.PointLight( 0xffffff, 0.8 );
  29. camera.add( pointLight );
  30. scene.add( camera );
  31. //
  32. var map = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
  33. map.wrapS = map.wrapT = THREE.RepeatWrapping;
  34. map.anisotropy = 16;
  35. var material = new THREE.MeshPhongMaterial( { map: map, side: THREE.DoubleSide } );
  36. //
  37. var geometry, object;
  38. geometry = new THREE.ParametricBufferGeometry( ParametricGeometries.plane( 100, 100 ), 10, 10 );
  39. geometry.center();
  40. object = new THREE.Mesh( geometry, material );
  41. object.position.set( - 200, 0, 200 );
  42. scene.add( object );
  43. geometry = new THREE.ParametricBufferGeometry( ParametricGeometries.klein, 20, 20 );
  44. object = new THREE.Mesh( geometry, material );
  45. object.position.set( 0, 0, 200 );
  46. object.scale.multiplyScalar( 5 );
  47. scene.add( object );
  48. geometry = new THREE.ParametricBufferGeometry( ParametricGeometries.mobius, 20, 20 );
  49. object = new THREE.Mesh( geometry, material );
  50. object.position.set( 200, 0, 200 );
  51. object.scale.multiplyScalar( 30 );
  52. scene.add( object );
  53. //
  54. var GrannyKnot = new Curves.GrannyKnot();
  55. var torus = new ParametricGeometries.TorusKnotGeometry( 50, 10, 50, 20, 2, 3 );
  56. var sphere = new ParametricGeometries.SphereGeometry( 50, 20, 10 );
  57. var tube = new ParametricGeometries.TubeGeometry( GrannyKnot, 100, 3, 8, true, false );
  58. torus = new THREE.BufferGeometry().fromGeometry( torus );
  59. sphere = new THREE.BufferGeometry().fromGeometry( sphere );
  60. tube = new THREE.BufferGeometry().fromGeometry( tube );
  61. object = new THREE.Mesh( torus, material );
  62. object.position.set( - 200, 0, - 200 );
  63. scene.add( object );
  64. object = new THREE.Mesh( sphere, material );
  65. object.position.set( 0, 0, - 200 );
  66. scene.add( object );
  67. object = new THREE.Mesh( tube, material );
  68. object.position.set( 200, 0, - 200 );
  69. object.scale.multiplyScalar( 2 );
  70. scene.add( object );
  71. //
  72. renderer = new THREE.WebGLRenderer( { antialias: true } );
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. container.appendChild( renderer.domElement );
  76. stats = new Stats();
  77. container.appendChild( stats.dom );
  78. window.addEventListener( 'resize', onWindowResize, false );
  79. }
  80. function onWindowResize() {
  81. camera.aspect = window.innerWidth / window.innerHeight;
  82. camera.updateProjectionMatrix();
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. }
  85. function animate() {
  86. requestAnimationFrame( animate );
  87. render();
  88. stats.update();
  89. }
  90. function render() {
  91. var timer = Date.now() * 0.0001;
  92. camera.position.x = Math.cos( timer ) * 800;
  93. camera.position.z = Math.sin( timer ) * 800;
  94. camera.lookAt( scene.position );
  95. scene.traverse( function ( object ) {
  96. if ( object.isMesh === true ) {
  97. object.rotation.x = timer * 5;
  98. object.rotation.y = timer * 2.5;
  99. }
  100. } );
  101. renderer.render( scene, camera );
  102. }
  103. </script>
  104. </body>
  105. </html>