2
0

webgl_helpers.html 4.2 KB

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