webgl_helpers.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - helpers</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="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - helpers
  12. </div>
  13. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  27. import { VertexNormalsHelper } from 'three/addons/helpers/VertexNormalsHelper.js';
  28. import { VertexTangentsHelper } from 'three/addons/helpers/VertexTangentsHelper.js';
  29. let scene, renderer;
  30. let camera, light;
  31. let vnh;
  32. let vth;
  33. init();
  34. animate();
  35. function init() {
  36. renderer = new THREE.WebGLRenderer();
  37. renderer.setPixelRatio( window.devicePixelRatio );
  38. renderer.setSize( window.innerWidth, window.innerHeight );
  39. renderer.useLegacyLights = false;
  40. document.body.appendChild( renderer.domElement );
  41. //
  42. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  43. camera.position.z = 400;
  44. scene = new THREE.Scene();
  45. light = new THREE.PointLight();
  46. light.position.set( 200, 100, 150 );
  47. scene.add( light );
  48. scene.add( new THREE.PointLightHelper( light, 15 ) );
  49. const gridHelper = new THREE.GridHelper( 400, 40, 0x0000ff, 0x808080 );
  50. gridHelper.position.y = - 150;
  51. gridHelper.position.x = - 150;
  52. scene.add( gridHelper );
  53. const polarGridHelper = new THREE.PolarGridHelper( 200, 16, 8, 64, 0x0000ff, 0x808080 );
  54. polarGridHelper.position.y = - 150;
  55. polarGridHelper.position.x = 200;
  56. scene.add( polarGridHelper );
  57. const loader = new GLTFLoader();
  58. loader.load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
  59. const mesh = gltf.scene.children[ 0 ];
  60. mesh.geometry.computeTangents(); // generates bad data due to degenerate UVs
  61. const group = new THREE.Group();
  62. group.scale.multiplyScalar( 50 );
  63. scene.add( group );
  64. // To make sure that the matrixWorld is up to date for the boxhelpers
  65. group.updateMatrixWorld( true );
  66. group.add( mesh );
  67. vnh = new VertexNormalsHelper( mesh, 5 );
  68. scene.add( vnh );
  69. vth = new VertexTangentsHelper( mesh, 5 );
  70. scene.add( vth );
  71. scene.add( new THREE.BoxHelper( mesh ) );
  72. const wireframe = new THREE.WireframeGeometry( mesh.geometry );
  73. let line = new THREE.LineSegments( wireframe );
  74. line.material.depthTest = false;
  75. line.material.opacity = 0.25;
  76. line.material.transparent = true;
  77. line.position.x = 4;
  78. group.add( line );
  79. scene.add( new THREE.BoxHelper( line ) );
  80. const edges = new THREE.EdgesGeometry( mesh.geometry );
  81. line = new THREE.LineSegments( edges );
  82. line.material.depthTest = false;
  83. line.material.opacity = 0.25;
  84. line.material.transparent = true;
  85. line.position.x = - 4;
  86. group.add( line );
  87. scene.add( new THREE.BoxHelper( line ) );
  88. scene.add( new THREE.BoxHelper( group ) );
  89. scene.add( new THREE.BoxHelper( scene ) );
  90. } );
  91. //
  92. window.addEventListener( 'resize', onWindowResize );
  93. }
  94. function onWindowResize() {
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. }
  99. function animate() {
  100. requestAnimationFrame( animate );
  101. const time = - performance.now() * 0.0003;
  102. camera.position.x = 400 * Math.cos( time );
  103. camera.position.z = 400 * Math.sin( time );
  104. camera.lookAt( scene.position );
  105. light.position.x = Math.sin( time * 1.7 ) * 300;
  106. light.position.y = Math.cos( time * 1.5 ) * 400;
  107. light.position.z = Math.cos( time * 1.3 ) * 300;
  108. if ( vnh ) vnh.update();
  109. if ( vth ) vth.update();
  110. renderer.render( scene, camera );
  111. }
  112. </script>
  113. </body>
  114. </html>