webgl_geometry_normals.html 3.9 KB

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