webgl_modifier_simplifier.html 2.9 KB

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