webgl_modifier_simplifier.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - modifier - simplify modifier</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. <!-- Import maps polyfill -->
  11. <!-- Remove this when import maps will be widely supported -->
  12. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. <script type="module">
  21. import * as THREE from 'three';
  22. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  23. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  24. import { SimplifyModifier } from './jsm/modifiers/SimplifyModifier.js';
  25. let renderer, scene, camera;
  26. init();
  27. function init() {
  28. const info = document.createElement( 'div' );
  29. info.style.position = 'absolute';
  30. info.style.top = '10px';
  31. info.style.width = '100%';
  32. info.style.textAlign = 'center';
  33. info.innerHTML = '<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Vertex Reduction using SimplifyModifier';
  34. document.body.appendChild( info );
  35. renderer = new THREE.WebGLRenderer( { antialias: true } );
  36. renderer.setPixelRatio( window.devicePixelRatio );
  37. renderer.setSize( window.innerWidth, window.innerHeight );
  38. document.body.appendChild( renderer.domElement );
  39. scene = new THREE.Scene();
  40. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  41. camera.position.z = 15;
  42. const controls = new OrbitControls( camera, renderer.domElement );
  43. controls.addEventListener( 'change', render ); // use if there is no animation loop
  44. controls.enablePan = false;
  45. controls.enableZoom = false;
  46. scene.add( new THREE.AmbientLight( 0xffffff, 0.2 ) );
  47. const light = new THREE.PointLight( 0xffffff, 0.7 );
  48. camera.add( light );
  49. scene.add( camera );
  50. new GLTFLoader().load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
  51. const mesh = gltf.scene.children[ 0 ];
  52. mesh.position.x = - 3;
  53. mesh.rotation.y = Math.PI / 2;
  54. scene.add( mesh );
  55. const modifier = new SimplifyModifier();
  56. const simplified = mesh.clone();
  57. simplified.material = simplified.material.clone();
  58. simplified.material.flatShading = true;
  59. const count = Math.floor( simplified.geometry.attributes.position.count * 0.875 ); // number of vertices to remove
  60. simplified.geometry = modifier.modify( simplified.geometry, count );
  61. simplified.position.x = 3;
  62. simplified.rotation.y = - Math.PI / 2;
  63. scene.add( simplified );
  64. render();
  65. } );
  66. window.addEventListener( 'resize', onWindowResize );
  67. }
  68. function onWindowResize() {
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. camera.aspect = window.innerWidth / window.innerHeight;
  71. camera.updateProjectionMatrix();
  72. render();
  73. }
  74. function render() {
  75. renderer.render( scene, camera );
  76. }
  77. </script>
  78. </body>
  79. </html>