webgl_helpers.html 4.0 KB

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