1
0

webgl_modifier_simplifier.html 3.0 KB

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