webgl_geometry_normals.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - normals</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">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - geometry - normals</a>
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import Stats from './jsm/libs/stats.module.js';
  17. import { GUI } from './jsm/libs/dat.gui.module.js';
  18. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  19. import { VertexNormalsHelper } from './jsm/helpers/VertexNormalsHelper.js';
  20. var container, stats, gui;
  21. var camera, scene, renderer;
  22. var mesh, geometry;
  23. var geometries = [
  24. new THREE.BoxBufferGeometry( 200, 200, 200, 2, 2, 2 ),
  25. new THREE.CircleBufferGeometry( 200, 32 ),
  26. new THREE.CylinderBufferGeometry( 75, 75, 200, 8, 8 ),
  27. new THREE.IcosahedronBufferGeometry( 100, 1 ),
  28. new THREE.OctahedronBufferGeometry( 200, 0 ),
  29. new THREE.PlaneBufferGeometry( 200, 200, 4, 4 ),
  30. new THREE.RingBufferGeometry( 32, 64, 16 ),
  31. new THREE.SphereBufferGeometry( 100, 12, 12 ),
  32. new THREE.TorusBufferGeometry( 64, 16, 12, 12 ),
  33. new THREE.TorusKnotBufferGeometry( 64, 16 )
  34. ];
  35. var options = {
  36. Geometry: 0
  37. };
  38. var material = new THREE.MeshBasicMaterial( { color: 0xfefefe, wireframe: true, opacity: 0.5 } );
  39. init();
  40. animate();
  41. function addMesh() {
  42. if ( mesh !== undefined ) {
  43. scene.remove( mesh );
  44. geometry.dispose();
  45. }
  46. geometry = geometries[ options.Geometry ];
  47. // scale geometry to a uniform size
  48. geometry.computeBoundingSphere();
  49. var scaleFactor = 160 / geometry.boundingSphere.radius;
  50. geometry.scale( scaleFactor, scaleFactor, scaleFactor );
  51. mesh = new THREE.Mesh( geometry, material );
  52. scene.add( mesh );
  53. var vertexNormalsHelper = new VertexNormalsHelper( mesh, 10 );
  54. mesh.add( vertexNormalsHelper );
  55. }
  56. function init() {
  57. container = document.getElementById( 'container' );
  58. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  59. camera.position.z = 500;
  60. scene = new THREE.Scene();
  61. addMesh();
  62. //
  63. renderer = new THREE.WebGLRenderer( { antialias: true } );
  64. renderer.setPixelRatio( window.devicePixelRatio );
  65. renderer.setSize( window.innerWidth, window.innerHeight );
  66. container.appendChild( renderer.domElement );
  67. //
  68. stats = new Stats();
  69. container.appendChild( stats.dom );
  70. //
  71. var geometries = {
  72. BoxBufferGeometry: 0,
  73. CircleBufferGeometry: 1,
  74. CylinderBufferGeometry: 2,
  75. IcosahedronBufferGeometry: 3,
  76. OctahedronBufferGeometry: 4,
  77. PlaneBufferGeometry: 5,
  78. RingBufferGeometry: 6,
  79. SphereBufferGeometry: 7,
  80. TorusBufferGeometry: 8,
  81. TorusKnotBufferGeometry: 9
  82. };
  83. gui = new GUI( { width: 350 } );
  84. gui.add( options, 'Geometry', geometries ).onChange( function () {
  85. addMesh();
  86. } );
  87. //
  88. var controls = new OrbitControls( camera, renderer.domElement );
  89. //
  90. window.addEventListener( 'resize', onWindowResize, false );
  91. }
  92. function onWindowResize() {
  93. camera.aspect = window.innerWidth / window.innerHeight;
  94. camera.updateProjectionMatrix();
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. }
  97. //
  98. function animate() {
  99. requestAnimationFrame( animate );
  100. render();
  101. stats.update();
  102. }
  103. function render() {
  104. renderer.render( scene, camera );
  105. }
  106. </script>
  107. </body>
  108. </html>