webgl_modifier_simplifier.html 3.2 KB

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