webgl_helpers.html 4.0 KB

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