webgl_helpers.html 3.9 KB

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